hchevva commited on
Commit
23409c2
·
verified ·
1 Parent(s): 7db25b7

Update quread/gates.py

Browse files
Files changed (1) hide show
  1. quread/gates.py +31 -49
quread/gates.py CHANGED
@@ -1,7 +1,9 @@
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)
@@ -18,7 +20,7 @@ Tdg = np.array([[1, 0], [0, np.exp(-1j * np.pi / 4)]], dtype=complex)
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
 
@@ -41,50 +43,30 @@ def RZ(theta: float) -> np.ndarray:
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}")
 
1
  # quread/gates.py
2
  import numpy as np
3
 
4
+ # Identity
5
+ I = np.array([[1, 0],
6
+ [0, 1]], dtype=complex)
7
 
8
  H = (1 / np.sqrt(2)) * np.array([[1, 1], [1, -1]], dtype=complex)
9
  X = np.array([[0, 1], [1, 0]], dtype=complex)
 
20
  SQRTX = 0.5 * np.array([[1 + 1j, 1 - 1j],
21
  [1 - 1j, 1 + 1j]], dtype=complex)
22
 
23
+ # √Z (principal square root): diag(1, i) == S
24
  SQRTZ = S
25
 
26
 
 
43
  [0, np.exp(1j * theta / 2)]], dtype=complex)
44
 
45
 
46
+ # --- aliases expected by engine.py ---
47
+ def rx(theta: float) -> np.ndarray:
48
+ return RX(theta)
49
+
50
+
51
+ def ry(theta: float) -> np.ndarray:
52
+ return RY(theta)
53
+
54
+
55
+ def rz(theta: float) -> np.ndarray:
56
+ return RZ(theta)
57
+
58
+
59
+ # --- map expected by engine.py ---
60
+ SINGLE_QUBIT_GATES = {
61
+ "I": I,
62
+ "H": H,
63
+ "X": X,
64
+ "Y": Y,
65
+ "Z": Z,
66
+ "S": S,
67
+ "Sdg": Sdg,
68
+ "T": T,
69
+ "Tdg": Tdg,
70
+ "SQRTX": SQRTX,
71
+ "SQRTZ": SQRTZ,
72
+ }