Skip to content

Commit ac4c4b0

Browse files
committed
Implement simple rotations about X, Y, Z
1 parent a328e07 commit ac4c4b0

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/bloqade/pyqrack/squin/runtime.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,53 @@ def mat(self, adjoint: bool) -> list[complex]:
195195

196196
@dataclass(frozen=True)
197197
class RotRuntime(OperatorRuntimeABC):
198+
AXIS_MAP = {
199+
"x": 1,
200+
"y": 2,
201+
"z": 3,
202+
}
198203
axis: OperatorRuntimeABC
199204
angle: float
200-
# TODO: how does this work?
205+
206+
def apply(self, *qubits: PyQrackQubit, adjoint: bool = False) -> None:
207+
sign = (-1) ** adjoint
208+
angle = sign * self.angle
209+
target = qubits[-1]
210+
211+
if not isinstance(self.axis, OperatorRuntime):
212+
raise RuntimeError(
213+
f"Rotation only supported for Pauli operators! Got {self.axis}"
214+
)
215+
216+
try:
217+
axis = self.AXIS_MAP[self.axis.method_name]
218+
except KeyError:
219+
raise RuntimeError(
220+
f"Rotation only supported for Pauli operators! Got {self.axis}"
221+
)
222+
223+
target.sim_reg.r(axis, angle, target.addr)
224+
225+
def control_apply(self, *qubits: PyQrackQubit, adjoint: bool = False) -> None:
226+
sign = (-1) ** (not adjoint)
227+
angle = sign * self.angle
228+
229+
ctrls = [qbit.addr for qbit in qubits[:-1]]
230+
target = qubits[-1]
231+
232+
if not isinstance(self.axis, OperatorRuntime):
233+
raise RuntimeError(
234+
f"Rotation only supported for Pauli operators! Got {self.axis}"
235+
)
236+
237+
try:
238+
axis = self.AXIS_MAP[self.axis.method_name]
239+
except KeyError:
240+
raise RuntimeError(
241+
f"Rotation only supported for Pauli operators! Got {self.axis}"
242+
)
243+
244+
target.sim_reg.mcr(axis, angle, ctrls, target.addr)
201245

202246

203247
@dataclass(frozen=True)

test/pyqrack/test_squin.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,20 @@ def main():
213213
assert result == [1]
214214

215215

216+
def test_rot():
217+
@squin.kernel
218+
def main():
219+
q = squin.qubit.new(1)
220+
x = squin.op.x()
221+
r = squin.op.rot(x, math.pi / 2)
222+
squin.qubit.apply(r, q)
223+
return squin.qubit.measure(q)
224+
225+
target = PyQrack(1)
226+
result = target.run(main)
227+
assert result == [1]
228+
229+
216230
# TODO: remove
217231
# test_qubit()
218232
# test_x()
@@ -224,3 +238,4 @@ def main():
224238
# test_phase()
225239
# test_sp()
226240
# test_adjoint()
241+
# test_rot()

0 commit comments

Comments
 (0)