Update quread/gates.py
Browse files- quread/gates.py +80 -16
quread/gates.py
CHANGED
|
@@ -1,26 +1,90 @@
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 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 |
-
|
| 9 |
S = np.array([[1, 0], [0, 1j]], dtype=complex)
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 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}")
|
|
|
|
|
|