hchevva commited on
Commit
77b3c13
·
verified ·
1 Parent(s): cf8ed7d

Update quread/gates.py

Browse files
Files changed (1) hide show
  1. quread/gates.py +80 -16
quread/gates.py CHANGED
@@ -1,26 +1,90 @@
 
1
  import numpy as np
2
 
3
- # Single-qubit gates (2x2)
4
- I = np.eye(2, dtype=complex)
 
5
  X = np.array([[0, 1], [1, 0]], dtype=complex)
6
  Y = np.array([[0, -1j], [1j, 0]], dtype=complex)
7
  Z = np.array([[1, 0], [0, -1]], dtype=complex)
8
- H = (1/np.sqrt(2)) * np.array([[1, 1], [1, -1]], dtype=complex)
9
  S = np.array([[1, 0], [0, 1j]], dtype=complex)
10
- T = np.array([[1, 0], [0, np.exp(1j*np.pi/4)]], dtype=complex)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- def rz(theta: float) -> np.ndarray:
13
- return np.array([[np.exp(-1j*theta/2), 0],
14
- [0, np.exp(1j*theta/2)]], dtype=complex)
 
 
 
 
 
15
 
16
- def ry(theta: float) -> np.ndarray:
17
- return np.array([[np.cos(theta/2), -np.sin(theta/2)],
18
- [np.sin(theta/2), np.cos(theta/2)]], dtype=complex)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- def rx(theta: float) -> np.ndarray:
21
- return np.array([[np.cos(theta/2), -1j*np.sin(theta/2)],
22
- [-1j*np.sin(theta/2), np.cos(theta/2)]], dtype=complex)
 
 
 
 
 
 
 
 
 
 
23
 
24
- SINGLE_QUBIT_GATES = {
25
- "I": I, "X": X, "Y": Y, "Z": Z, "H": H, "S": S, "T": T
26
- }
 
1
+ # quread/gates.py
2
  import numpy as np
3
 
4
+ I2 = np.eye(2, dtype=complex)
5
+
6
+ H = (1 / np.sqrt(2)) * np.array([[1, 1], [1, -1]], dtype=complex)
7
  X = np.array([[0, 1], [1, 0]], dtype=complex)
8
  Y = np.array([[0, -1j], [1j, 0]], dtype=complex)
9
  Z = np.array([[1, 0], [0, -1]], dtype=complex)
10
+
11
  S = np.array([[1, 0], [0, 1j]], dtype=complex)
12
+ Sdg = np.array([[1, 0], [0, -1j]], dtype=complex)
13
+
14
+ T = np.array([[1, 0], [0, np.exp(1j * np.pi / 4)]], dtype=complex)
15
+ Tdg = np.array([[1, 0], [0, np.exp(-1j * np.pi / 4)]], dtype=complex)
16
+
17
+ # √X (standard definition)
18
+ SQRTX = 0.5 * np.array([[1 + 1j, 1 - 1j],
19
+ [1 - 1j, 1 + 1j]], dtype=complex)
20
+
21
+ # √Z (principal square root): diag(1, e^{iπ/2}) = diag(1, i) == S
22
+ SQRTZ = S
23
+
24
+
25
+ def RX(theta: float) -> np.ndarray:
26
+ c = np.cos(theta / 2)
27
+ s = np.sin(theta / 2)
28
+ return np.array([[c, -1j * s],
29
+ [-1j * s, c]], dtype=complex)
30
+
31
+
32
+ def RY(theta: float) -> np.ndarray:
33
+ c = np.cos(theta / 2)
34
+ s = np.sin(theta / 2)
35
+ return np.array([[c, -s],
36
+ [s, c]], dtype=complex)
37
+
38
+
39
+ def RZ(theta: float) -> np.ndarray:
40
+ return np.array([[np.exp(-1j * theta / 2), 0],
41
+ [0, np.exp(1j * theta / 2)]], dtype=complex)
42
+
43
 
44
+ def single_qubit_gate_matrix(gate: str) -> np.ndarray:
45
+ """
46
+ Supported gate strings:
47
+ H, X, Y, Z, S, Sdg, T, Tdg, SQRTX, SQRTZ,
48
+ RX_PI, RX_PI_2, RY_PI, RY_PI_2, RZ_PI, RZ_PI_2,
49
+ I
50
+ """
51
+ g = gate.strip()
52
 
53
+ if g == "I":
54
+ return I2
55
+ if g == "H":
56
+ return H
57
+ if g == "X":
58
+ return X
59
+ if g == "Y":
60
+ return Y
61
+ if g == "Z":
62
+ return Z
63
+ if g == "S":
64
+ return S
65
+ if g in ("S†", "Sdg"):
66
+ return Sdg
67
+ if g == "T":
68
+ return T
69
+ if g in ("T†", "Tdg"):
70
+ return Tdg
71
+ if g in ("√X", "SQRTX"):
72
+ return SQRTX
73
+ if g in ("√Z", "SQRTZ"):
74
+ return SQRTZ
75
 
76
+ # rotations (fixed angles from your palette)
77
+ if g == "RX(π)":
78
+ return RX(np.pi)
79
+ if g == "RX(π/2)":
80
+ return RX(np.pi / 2)
81
+ if g == "RY(π)":
82
+ return RY(np.pi)
83
+ if g == "RY(π/2)":
84
+ return RY(np.pi / 2)
85
+ if g == "RZ(π)":
86
+ return RZ(np.pi)
87
+ if g == "RZ(π/2)":
88
+ return RZ(np.pi / 2)
89
 
90
+ raise ValueError(f"Unsupported gate: {gate}")