Skip to content

Commit 418208b

Browse files
committed
Scale runtime
1 parent 914b1fd commit 418208b

File tree

4 files changed

+49
-18
lines changed

4 files changed

+49
-18
lines changed

src/bloqade/pyqrack/squin/op.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
from kirin import interp
22

33
from bloqade.squin import op
4-
5-
# from bloqade.pyqrack.reg import QubitState, PyQrackQubit
64
from bloqade.pyqrack.base import PyQrackInterpreter
75

86
from .runtime import (
97
KronRuntime,
108
MultRuntime,
9+
ScaleRuntime,
1110
ControlRuntime,
1211
IdentityRuntime,
1312
OperatorRuntime,
1413
ProjectorRuntime,
1514
)
1615

17-
# from kirin.dialects import ilist
18-
1916

2017
@op.dialect.register(key="pyqrack")
2118
class PyQrackMethods(interp.MethodTable):
@@ -44,14 +41,13 @@ def mult(
4441
# op: ir.SSAValue = info.argument(OpType)
4542
# result: ir.ResultValue = info.result(OpType)
4643

47-
# @interp.impl(op.stmts.Scale)
48-
# def scale(
49-
# self, interp: PyQrackInterpreter, frame: interp.Frame, stmt: op.stmts.Scale
50-
# ):
51-
# is_unitary: bool = info.attribute(default=False)
52-
# op: ir.SSAValue = info.argument(OpType)
53-
# factor: ir.SSAValue = info.argument(Complex)
54-
# result: ir.ResultValue = info.result(OpType)
44+
@interp.impl(op.stmts.Scale)
45+
def scale(
46+
self, interp: PyQrackInterpreter, frame: interp.Frame, stmt: op.stmts.Scale
47+
):
48+
op = frame.get(stmt.op)
49+
factor = frame.get(stmt.factor)
50+
return (ScaleRuntime(op, factor),)
5551

5652
@interp.impl(op.stmts.Control)
5753
def control(

src/bloqade/pyqrack/squin/runtime.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class OperatorRuntime(OperatorRuntimeABC):
1616
method_name: str
1717

1818
def apply(self, *qubits: PyQrackQubit) -> None:
19-
getattr(qubits[-1].sim_reg, self.method_name)(qubits[-1].addr)
19+
getattr(qubits[0].sim_reg, self.method_name)(qubits[0].addr)
2020

2121

2222
@dataclass
@@ -64,7 +64,21 @@ class KronRuntime(OperatorRuntimeABC):
6464
rhs: OperatorRuntimeABC
6565

6666
def apply(self, *qubits: PyQrackQubit) -> None:
67-
assert len(qubits) == 2
68-
qbit1, qbit2 = qubits
69-
self.lhs.apply(qbit1)
70-
self.rhs.apply(qbit2)
67+
self.lhs.apply(qubits[0])
68+
self.rhs.apply(qubits[1])
69+
70+
71+
@dataclass
72+
class ScaleRuntime(OperatorRuntimeABC):
73+
op: OperatorRuntimeABC
74+
factor: complex
75+
76+
def apply(self, *qubits: PyQrackQubit) -> None:
77+
target = qubits[0]
78+
self.op.apply(target)
79+
80+
# NOTE: just factor * eye(2)
81+
mat = [self.factor, 0, 0, self.factor]
82+
83+
# TODO: output seems to always be normalized -- no-op?
84+
target.sim_reg.mtrx(mat, target.addr)

src/bloqade/squin/op/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
def kron(lhs: types.Op, rhs: types.Op) -> types.Op: ...
1212

1313

14-
# FIXME: should we just rewrite the py.binop.mult instead?
1514
@_wraps(stmts.Mult)
1615
def mult(lhs: types.Op, rhs: types.Op) -> types.Op: ...
1716

1817

18+
@_wraps(stmts.Scale)
19+
def scale(op: types.Op, factor: complex) -> types.Op: ...
20+
21+
1922
@_wraps(stmts.Adjoint)
2023
def adjoint(op: types.Op) -> types.Op: ...
2124

test/pyqrack/test_squin.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,28 @@ def main():
133133
assert result == [1, 1]
134134

135135

136+
def test_scale():
137+
@squin.kernel
138+
def main():
139+
q = squin.qubit.new(1)
140+
x = squin.op.x()
141+
142+
# TODO: replace by 2 * x once we have the rewrite
143+
s = squin.op.scale(x, 2)
144+
145+
squin.qubit.apply(s, q)
146+
return squin.qubit.measure(q)
147+
148+
target = PyQrack(1)
149+
result = target.run(main)
150+
assert result == [1]
151+
152+
136153
# TODO: remove
137154
# test_qubit()
138155
# test_x()
139156
# test_basic_ops("x")
140157
# test_cx()
141158
# test_mult()
142159
# test_kron()
160+
# test_scale()

0 commit comments

Comments
 (0)