bloqade-circuit 0.3.0__py3-none-any.whl → 0.4.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bloqade/analysis/address/impls.py +3 -16
- bloqade/pyqrack/__init__.py +1 -1
- bloqade/pyqrack/noise/native.py +8 -8
- bloqade/pyqrack/squin/noise/__init__.py +1 -0
- bloqade/pyqrack/squin/noise/native.py +72 -0
- bloqade/pyqrack/squin/op.py +7 -0
- bloqade/pyqrack/squin/qubit.py +0 -29
- bloqade/pyqrack/squin/runtime.py +18 -0
- bloqade/pyqrack/squin/wire.py +0 -36
- bloqade/{noise/native → qasm2/dialects/noise}/__init__.py +1 -7
- bloqade/qasm2/dialects/noise/_dialect.py +3 -0
- bloqade/{noise → qasm2/dialects/noise}/fidelity.py +2 -2
- bloqade/qasm2/dialects/noise/model.py +278 -0
- bloqade/qasm2/emit/impls/__init__.py +1 -1
- bloqade/qasm2/emit/impls/{noise_native.py → noise.py} +11 -11
- bloqade/qasm2/emit/main.py +2 -4
- bloqade/qasm2/emit/target.py +3 -3
- bloqade/qasm2/groups.py +0 -2
- bloqade/{noise/native/_wrappers.py → qasm2/noise.py} +9 -5
- bloqade/qasm2/passes/glob.py +12 -8
- bloqade/qasm2/passes/noise.py +5 -14
- bloqade/qasm2/rewrite/__init__.py +2 -0
- bloqade/qasm2/rewrite/noise/__init__.py +0 -0
- bloqade/qasm2/rewrite/{heuristic_noise.py → noise/heuristic_noise.py} +31 -53
- bloqade/{noise/native/rewrite.py → qasm2/rewrite/noise/remove_noise.py} +2 -2
- bloqade/qbraid/lowering.py +8 -8
- bloqade/squin/__init__.py +16 -1
- bloqade/squin/analysis/nsites/impls.py +0 -9
- bloqade/squin/cirq/__init__.py +89 -0
- bloqade/squin/cirq/lowering.py +303 -0
- bloqade/squin/groups.py +7 -7
- bloqade/squin/lowering.py +27 -0
- bloqade/squin/noise/__init__.py +3 -1
- bloqade/squin/noise/_wrapper.py +7 -3
- bloqade/squin/noise/rewrite.py +111 -0
- bloqade/squin/noise/stmts.py +21 -16
- bloqade/squin/op/__init__.py +1 -0
- bloqade/squin/op/_wrapper.py +4 -0
- bloqade/squin/op/stmts.py +10 -11
- bloqade/squin/op/types.py +2 -0
- bloqade/squin/qubit.py +32 -37
- bloqade/squin/rewrite/desugar.py +65 -0
- bloqade/squin/rewrite/qubit_to_stim.py +0 -23
- bloqade/squin/rewrite/squin_measure.py +2 -27
- bloqade/squin/rewrite/stim_rewrite_util.py +3 -8
- bloqade/squin/rewrite/wire_to_stim.py +0 -21
- bloqade/squin/wire.py +4 -9
- bloqade/stim/__init__.py +2 -1
- bloqade/stim/_wrappers.py +4 -0
- bloqade/stim/dialects/auxiliary/__init__.py +1 -0
- bloqade/stim/dialects/auxiliary/emit.py +17 -2
- bloqade/stim/dialects/auxiliary/stmts/__init__.py +1 -0
- bloqade/stim/dialects/auxiliary/stmts/annotate.py +8 -0
- bloqade/stim/dialects/collapse/emit_str.py +3 -1
- bloqade/stim/dialects/gate/emit.py +9 -2
- bloqade/stim/dialects/noise/emit.py +32 -1
- bloqade/stim/dialects/noise/stmts.py +29 -0
- bloqade/stim/parse/__init__.py +1 -0
- bloqade/stim/parse/lowering.py +686 -0
- {bloqade_circuit-0.3.0.dist-info → bloqade_circuit-0.4.1.dist-info}/METADATA +3 -1
- {bloqade_circuit-0.3.0.dist-info → bloqade_circuit-0.4.1.dist-info}/RECORD +64 -57
- bloqade/noise/__init__.py +0 -2
- bloqade/noise/native/_dialect.py +0 -3
- bloqade/noise/native/model.py +0 -346
- bloqade/qasm2/dialects/noise.py +0 -48
- bloqade/squin/rewrite/measure_desugar.py +0 -33
- /bloqade/{noise/native → qasm2/dialects/noise}/stmts.py +0 -0
- {bloqade_circuit-0.3.0.dist-info → bloqade_circuit-0.4.1.dist-info}/WHEEL +0 -0
- {bloqade_circuit-0.3.0.dist-info → bloqade_circuit-0.4.1.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import math
|
|
2
|
+
from typing import Any
|
|
3
|
+
from dataclasses import field, dataclass
|
|
4
|
+
|
|
5
|
+
import cirq
|
|
6
|
+
from kirin import ir, lowering
|
|
7
|
+
from kirin.rewrite import Walk, CFGCompactify
|
|
8
|
+
from kirin.dialects import py, ilist
|
|
9
|
+
|
|
10
|
+
from .. import op, noise, qubit
|
|
11
|
+
|
|
12
|
+
CirqNode = cirq.Circuit | cirq.Moment | cirq.Gate | cirq.Qid | cirq.Operation
|
|
13
|
+
|
|
14
|
+
DecomposeNode = (
|
|
15
|
+
cirq.SwapPowGate
|
|
16
|
+
| cirq.ISwapPowGate
|
|
17
|
+
| cirq.PhasedXPowGate
|
|
18
|
+
| cirq.PhasedXZGate
|
|
19
|
+
| cirq.CSwapGate
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass
|
|
24
|
+
class Squin(lowering.LoweringABC[CirqNode]):
|
|
25
|
+
"""Lower a cirq.Circuit object to a squin kernel"""
|
|
26
|
+
|
|
27
|
+
circuit: cirq.Circuit
|
|
28
|
+
qreg: qubit.New = field(init=False)
|
|
29
|
+
qreg_index: dict[cirq.Qid, int] = field(init=False, default_factory=dict)
|
|
30
|
+
next_qreg_index: int = field(init=False, default=0)
|
|
31
|
+
|
|
32
|
+
def lower_qubit_getindex(self, state: lowering.State[CirqNode], qid: cirq.Qid):
|
|
33
|
+
index = self.qreg_index.get(qid)
|
|
34
|
+
|
|
35
|
+
if index is None:
|
|
36
|
+
index = self.next_qreg_index
|
|
37
|
+
self.qreg_index[qid] = index
|
|
38
|
+
self.next_qreg_index += 1
|
|
39
|
+
|
|
40
|
+
index_ssa = state.current_frame.push(py.Constant(index)).result
|
|
41
|
+
qbit_getitem = state.current_frame.push(py.GetItem(self.qreg.result, index_ssa))
|
|
42
|
+
return qbit_getitem.result
|
|
43
|
+
|
|
44
|
+
def lower_qubit_getindices(
|
|
45
|
+
self, state: lowering.State[CirqNode], qids: list[cirq.Qid]
|
|
46
|
+
):
|
|
47
|
+
qbits_getitem = [self.lower_qubit_getindex(state, qid) for qid in qids]
|
|
48
|
+
qbits_stmt = ilist.New(values=qbits_getitem)
|
|
49
|
+
qbits_result = state.current_frame.get(qbits_stmt.name)
|
|
50
|
+
|
|
51
|
+
if qbits_result is not None:
|
|
52
|
+
return qbits_result
|
|
53
|
+
|
|
54
|
+
state.current_frame.push(qbits_stmt)
|
|
55
|
+
return qbits_stmt.result
|
|
56
|
+
|
|
57
|
+
def run(
|
|
58
|
+
self,
|
|
59
|
+
stmt: CirqNode,
|
|
60
|
+
*,
|
|
61
|
+
source: str | None = None,
|
|
62
|
+
globals: dict[str, Any] | None = None,
|
|
63
|
+
file: str | None = None,
|
|
64
|
+
lineno_offset: int = 0,
|
|
65
|
+
col_offset: int = 0,
|
|
66
|
+
compactify: bool = True,
|
|
67
|
+
) -> ir.Region:
|
|
68
|
+
|
|
69
|
+
state = lowering.State(
|
|
70
|
+
self,
|
|
71
|
+
file=file,
|
|
72
|
+
lineno_offset=lineno_offset,
|
|
73
|
+
col_offset=col_offset,
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
with state.frame(
|
|
77
|
+
[stmt],
|
|
78
|
+
globals=globals,
|
|
79
|
+
finalize_next=False,
|
|
80
|
+
) as frame:
|
|
81
|
+
# NOTE: create a global register of qubits first
|
|
82
|
+
# TODO: can there be a circuit without qubits?
|
|
83
|
+
n_qubits = cirq.num_qubits(self.circuit)
|
|
84
|
+
n = frame.push(py.Constant(n_qubits))
|
|
85
|
+
self.qreg = frame.push(qubit.New(n_qubits=n.result))
|
|
86
|
+
|
|
87
|
+
self.visit(state, stmt)
|
|
88
|
+
|
|
89
|
+
if compactify:
|
|
90
|
+
Walk(CFGCompactify()).rewrite(frame.curr_region)
|
|
91
|
+
|
|
92
|
+
region = frame.curr_region
|
|
93
|
+
|
|
94
|
+
return region
|
|
95
|
+
|
|
96
|
+
def visit(self, state: lowering.State[CirqNode], node: CirqNode) -> lowering.Result:
|
|
97
|
+
name = node.__class__.__name__
|
|
98
|
+
return getattr(self, f"visit_{name}", self.generic_visit)(state, node)
|
|
99
|
+
|
|
100
|
+
def generic_visit(self, state: lowering.State[CirqNode], node: CirqNode):
|
|
101
|
+
if isinstance(node, CirqNode):
|
|
102
|
+
raise lowering.BuildError(
|
|
103
|
+
f"Cannot lower {node.__class__.__name__} node: {node}"
|
|
104
|
+
)
|
|
105
|
+
raise lowering.BuildError(
|
|
106
|
+
f"Unexpected `{node.__class__.__name__}` node: {repr(node)} is not an AST node"
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
def lower_literal(self, state: lowering.State[CirqNode], value) -> ir.SSAValue:
|
|
110
|
+
raise lowering.BuildError("Literals not supported in cirq circuit")
|
|
111
|
+
|
|
112
|
+
def lower_global(
|
|
113
|
+
self, state: lowering.State[CirqNode], node: CirqNode
|
|
114
|
+
) -> lowering.LoweringABC.Result:
|
|
115
|
+
raise lowering.BuildError("Literals not supported in cirq circuit")
|
|
116
|
+
|
|
117
|
+
def visit_Circuit(
|
|
118
|
+
self, state: lowering.State[CirqNode], node: cirq.Circuit
|
|
119
|
+
) -> lowering.Result:
|
|
120
|
+
for moment in node:
|
|
121
|
+
state.lower(moment)
|
|
122
|
+
|
|
123
|
+
def visit_Moment(
|
|
124
|
+
self, state: lowering.State[CirqNode], node: cirq.Moment
|
|
125
|
+
) -> lowering.Result:
|
|
126
|
+
for op_ in node.operations:
|
|
127
|
+
state.lower(op_)
|
|
128
|
+
|
|
129
|
+
def visit_GateOperation(
|
|
130
|
+
self, state: lowering.State[CirqNode], node: cirq.GateOperation
|
|
131
|
+
):
|
|
132
|
+
if isinstance(node.gate, cirq.MeasurementGate):
|
|
133
|
+
# NOTE: special dispatch here, since measurement is a gate + a qubit in cirq,
|
|
134
|
+
# but a single statement in squin
|
|
135
|
+
return self.lower_measurement(state, node)
|
|
136
|
+
|
|
137
|
+
if isinstance(node.gate, DecomposeNode):
|
|
138
|
+
# NOTE: easier to decompose these, but for that we need the qubits too,
|
|
139
|
+
# so we need to do this within this method
|
|
140
|
+
for subnode in cirq.decompose_once(node):
|
|
141
|
+
state.lower(subnode)
|
|
142
|
+
return
|
|
143
|
+
|
|
144
|
+
op_ = state.lower(node.gate).expect_one()
|
|
145
|
+
qbits = self.lower_qubit_getindices(state, node.qubits)
|
|
146
|
+
return state.current_frame.push(qubit.Apply(operator=op_, qubits=qbits))
|
|
147
|
+
|
|
148
|
+
def lower_measurement(
|
|
149
|
+
self, state: lowering.State[CirqNode], node: cirq.GateOperation
|
|
150
|
+
):
|
|
151
|
+
if len(node.qubits) == 1:
|
|
152
|
+
qbit = self.lower_qubit_getindex(state, node.qubits[0])
|
|
153
|
+
return state.current_frame.push(qubit.MeasureQubit(qbit))
|
|
154
|
+
|
|
155
|
+
qbits = self.lower_qubit_getindices(state, node.qubits)
|
|
156
|
+
return state.current_frame.push(qubit.MeasureQubitList(qbits))
|
|
157
|
+
|
|
158
|
+
def visit_SingleQubitPauliStringGateOperation(
|
|
159
|
+
self,
|
|
160
|
+
state: lowering.State[CirqNode],
|
|
161
|
+
node: cirq.SingleQubitPauliStringGateOperation,
|
|
162
|
+
):
|
|
163
|
+
|
|
164
|
+
match node.pauli:
|
|
165
|
+
case cirq.X:
|
|
166
|
+
op_ = op.stmts.X()
|
|
167
|
+
case cirq.Y:
|
|
168
|
+
op_ = op.stmts.Y()
|
|
169
|
+
case cirq.Z:
|
|
170
|
+
op_ = op.stmts.Z()
|
|
171
|
+
case cirq.I:
|
|
172
|
+
op_ = op.stmts.Identity(sites=1)
|
|
173
|
+
case _:
|
|
174
|
+
raise lowering.BuildError(f"Unexpected Pauli operation {node.pauli}")
|
|
175
|
+
|
|
176
|
+
state.current_frame.push(op_)
|
|
177
|
+
qargs = self.lower_qubit_getindices(state, [node.qubit])
|
|
178
|
+
return state.current_frame.push(qubit.Apply(op_.result, qargs))
|
|
179
|
+
|
|
180
|
+
def visit_HPowGate(self, state: lowering.State[CirqNode], node: cirq.HPowGate):
|
|
181
|
+
if node.exponent == 1:
|
|
182
|
+
return state.current_frame.push(op.stmts.H())
|
|
183
|
+
|
|
184
|
+
return state.lower(node.in_su2())
|
|
185
|
+
|
|
186
|
+
def visit_XPowGate(self, state: lowering.State[CirqNode], node: cirq.XPowGate):
|
|
187
|
+
if node.exponent == 1:
|
|
188
|
+
return state.current_frame.push(op.stmts.X())
|
|
189
|
+
|
|
190
|
+
return self.visit(state, node.in_su2())
|
|
191
|
+
|
|
192
|
+
def visit_YPowGate(self, state: lowering.State[CirqNode], node: cirq.YPowGate):
|
|
193
|
+
if node.exponent == 1:
|
|
194
|
+
return state.current_frame.push(op.stmts.Y())
|
|
195
|
+
|
|
196
|
+
return self.visit(state, node.in_su2())
|
|
197
|
+
|
|
198
|
+
def visit_ZPowGate(self, state: lowering.State[CirqNode], node: cirq.ZPowGate):
|
|
199
|
+
if node.exponent == 0.5:
|
|
200
|
+
return state.current_frame.push(op.stmts.S())
|
|
201
|
+
|
|
202
|
+
if node.exponent == 0.25:
|
|
203
|
+
return state.current_frame.push(op.stmts.T())
|
|
204
|
+
|
|
205
|
+
if node.exponent == 1:
|
|
206
|
+
return state.current_frame.push(op.stmts.Z())
|
|
207
|
+
|
|
208
|
+
# NOTE: just for the Z gate, an arbitrary exponent is equivalent to the ShiftOp
|
|
209
|
+
t = node.exponent
|
|
210
|
+
theta = state.current_frame.push(py.Constant(math.pi * t))
|
|
211
|
+
return state.current_frame.push(op.stmts.ShiftOp(theta=theta.result))
|
|
212
|
+
|
|
213
|
+
def visit_Rx(self, state: lowering.State[CirqNode], node: cirq.Rx):
|
|
214
|
+
x = state.current_frame.push(op.stmts.X())
|
|
215
|
+
angle = state.current_frame.push(py.Constant(value=math.pi * node.exponent))
|
|
216
|
+
return state.current_frame.push(op.stmts.Rot(axis=x.result, angle=angle.result))
|
|
217
|
+
|
|
218
|
+
def visit_Ry(self, state: lowering.State[CirqNode], node: cirq.Ry):
|
|
219
|
+
y = state.current_frame.push(op.stmts.Y())
|
|
220
|
+
angle = state.current_frame.push(py.Constant(value=math.pi * node.exponent))
|
|
221
|
+
return state.current_frame.push(op.stmts.Rot(axis=y.result, angle=angle.result))
|
|
222
|
+
|
|
223
|
+
def visit_Rz(self, state: lowering.State[CirqNode], node: cirq.Rz):
|
|
224
|
+
z = state.current_frame.push(op.stmts.Z())
|
|
225
|
+
angle = state.current_frame.push(py.Constant(value=math.pi * node.exponent))
|
|
226
|
+
return state.current_frame.push(op.stmts.Rot(axis=z.result, angle=angle.result))
|
|
227
|
+
|
|
228
|
+
def visit_CXPowGate(self, state: lowering.State[CirqNode], node: cirq.CXPowGate):
|
|
229
|
+
x = state.lower(cirq.XPowGate(exponent=node.exponent)).expect_one()
|
|
230
|
+
return state.current_frame.push(op.stmts.Control(x, n_controls=1))
|
|
231
|
+
|
|
232
|
+
def visit_CZPowGate(self, state: lowering.State[CirqNode], node: cirq.CZPowGate):
|
|
233
|
+
z = state.lower(cirq.ZPowGate(exponent=node.exponent)).expect_one()
|
|
234
|
+
return state.current_frame.push(op.stmts.Control(z, n_controls=1))
|
|
235
|
+
|
|
236
|
+
def visit_ControlledOperation(
|
|
237
|
+
self, state: lowering.State[CirqNode], node: cirq.ControlledOperation
|
|
238
|
+
):
|
|
239
|
+
return self.visit_GateOperation(state, node)
|
|
240
|
+
|
|
241
|
+
def visit_ControlledGate(
|
|
242
|
+
self, state: lowering.State[CirqNode], node: cirq.ControlledGate
|
|
243
|
+
):
|
|
244
|
+
op_ = state.lower(node.sub_gate).expect_one()
|
|
245
|
+
n_controls = node.num_controls()
|
|
246
|
+
return state.current_frame.push(op.stmts.Control(op_, n_controls=n_controls))
|
|
247
|
+
|
|
248
|
+
def visit_XXPowGate(self, state: lowering.State[CirqNode], node: cirq.XXPowGate):
|
|
249
|
+
x = state.lower(cirq.XPowGate(exponent=node.exponent)).expect_one()
|
|
250
|
+
return state.current_frame.push(op.stmts.Kron(x, x))
|
|
251
|
+
|
|
252
|
+
def visit_YYPowGate(self, state: lowering.State[CirqNode], node: cirq.YYPowGate):
|
|
253
|
+
y = state.lower(cirq.YPowGate(exponent=node.exponent)).expect_one()
|
|
254
|
+
return state.current_frame.push(op.stmts.Kron(y, y))
|
|
255
|
+
|
|
256
|
+
def visit_ZZPowGate(self, state: lowering.State[CirqNode], node: cirq.ZZPowGate):
|
|
257
|
+
z = state.lower(cirq.ZPowGate(exponent=node.exponent)).expect_one()
|
|
258
|
+
return state.current_frame.push(op.stmts.Kron(z, z))
|
|
259
|
+
|
|
260
|
+
def visit_CCXPowGate(self, state: lowering.State[CirqNode], node: cirq.CCXPowGate):
|
|
261
|
+
x = state.lower(cirq.XPowGate(exponent=node.exponent)).expect_one()
|
|
262
|
+
return state.current_frame.push(op.stmts.Control(x, n_controls=2))
|
|
263
|
+
|
|
264
|
+
def visit_CCZPowGate(self, state: lowering.State[CirqNode], node: cirq.CCZPowGate):
|
|
265
|
+
z = state.lower(cirq.ZPowGate(exponent=node.exponent)).expect_one()
|
|
266
|
+
return state.current_frame.push(op.stmts.Control(z, n_controls=2))
|
|
267
|
+
|
|
268
|
+
def visit_BitFlipChannel(
|
|
269
|
+
self, state: lowering.State[CirqNode], node: cirq.BitFlipChannel
|
|
270
|
+
):
|
|
271
|
+
x = state.current_frame.push(op.stmts.X())
|
|
272
|
+
p = state.current_frame.push(py.Constant(node.p))
|
|
273
|
+
return state.current_frame.push(
|
|
274
|
+
noise.stmts.PauliError(basis=x.result, p=p.result)
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
def visit_AmplitudeDampingChannel(
|
|
278
|
+
self, state: lowering.State[CirqNode], node: cirq.AmplitudeDampingChannel
|
|
279
|
+
):
|
|
280
|
+
r = state.current_frame.push(op.stmts.Reset())
|
|
281
|
+
p = state.current_frame.push(py.Constant(node.gamma))
|
|
282
|
+
|
|
283
|
+
# TODO: do we need a dedicated noise stmt for this? Using PauliError
|
|
284
|
+
# with this basis feels like a hack
|
|
285
|
+
noise_channel = state.current_frame.push(
|
|
286
|
+
noise.stmts.PauliError(basis=r.result, p=p.result)
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
return noise_channel
|
|
290
|
+
|
|
291
|
+
def visit_GeneralizedAmplitudeDampingChannel(
|
|
292
|
+
self,
|
|
293
|
+
state: lowering.State[CirqNode],
|
|
294
|
+
node: cirq.GeneralizedAmplitudeDampingChannel,
|
|
295
|
+
):
|
|
296
|
+
raise NotImplementedError("TODO: needs a new operator statement")
|
|
297
|
+
# p = state.current_frame.push(py.Constant(node.p))
|
|
298
|
+
# gamma = state.current_frame.push(py.Constant(node.gamma))
|
|
299
|
+
|
|
300
|
+
# p1 =
|
|
301
|
+
|
|
302
|
+
# x = state.current_frame.push(op.stmts.X())
|
|
303
|
+
# noise_channel1 = noise.stmts.PauliError(basis=x.result, p=)
|
bloqade/squin/groups.py
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
from kirin import ir, passes
|
|
2
2
|
from kirin.prelude import structural_no_opt
|
|
3
|
+
from kirin.rewrite import Walk, Chain
|
|
3
4
|
from kirin.dialects import ilist
|
|
4
|
-
from kirin.rewrite.walk import Walk
|
|
5
5
|
|
|
6
|
-
from . import op, wire, qubit
|
|
6
|
+
from . import op, wire, noise, qubit
|
|
7
7
|
from .op.rewrite import PyMultToSquinMult
|
|
8
|
-
from .rewrite.
|
|
8
|
+
from .rewrite.desugar import ApplyDesugarRule, MeasureDesugarRule
|
|
9
9
|
|
|
10
10
|
|
|
11
|
-
@ir.dialect_group(structural_no_opt.union([op, qubit]))
|
|
11
|
+
@ir.dialect_group(structural_no_opt.union([op, qubit, noise]))
|
|
12
12
|
def kernel(self):
|
|
13
13
|
fold_pass = passes.Fold(self)
|
|
14
14
|
typeinfer_pass = passes.TypeInfer(self)
|
|
15
15
|
ilist_desugar_pass = ilist.IListDesugar(self)
|
|
16
|
-
|
|
16
|
+
desugar_pass = Walk(Chain(MeasureDesugarRule(), ApplyDesugarRule()))
|
|
17
17
|
py_mult_to_mult_pass = PyMultToSquinMult(self)
|
|
18
18
|
|
|
19
19
|
def run_pass(method: ir.Method, *, fold=True, typeinfer=True):
|
|
@@ -25,7 +25,7 @@ def kernel(self):
|
|
|
25
25
|
|
|
26
26
|
if typeinfer:
|
|
27
27
|
typeinfer_pass(method)
|
|
28
|
-
|
|
28
|
+
desugar_pass.rewrite(method.code)
|
|
29
29
|
|
|
30
30
|
ilist_desugar_pass(method)
|
|
31
31
|
|
|
@@ -36,7 +36,7 @@ def kernel(self):
|
|
|
36
36
|
return run_pass
|
|
37
37
|
|
|
38
38
|
|
|
39
|
-
@ir.dialect_group(structural_no_opt.union([op, wire]))
|
|
39
|
+
@ir.dialect_group(structural_no_opt.union([op, wire, noise]))
|
|
40
40
|
def wired(self):
|
|
41
41
|
py_mult_to_mult_pass = PyMultToSquinMult(self)
|
|
42
42
|
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import ast
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
|
|
4
|
+
from kirin import lowering
|
|
5
|
+
|
|
6
|
+
from . import qubit
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass(frozen=True)
|
|
10
|
+
class ApplyAnyCallLowering(lowering.FromPythonCall["qubit.ApplyAny"]):
|
|
11
|
+
"""
|
|
12
|
+
Custom lowering for ApplyAny that collects vararg qubits into a single tuple argument
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
def lower(
|
|
16
|
+
self, stmt: type["qubit.ApplyAny"], state: lowering.State, node: ast.Call
|
|
17
|
+
):
|
|
18
|
+
if len(node.args) < 2:
|
|
19
|
+
raise lowering.BuildError(
|
|
20
|
+
"Apply requires at least one operator and one qubit as arguments!"
|
|
21
|
+
)
|
|
22
|
+
op, *qubits = node.args
|
|
23
|
+
op_ssa = state.lower(op).expect_one()
|
|
24
|
+
qubits_lowered = [state.lower(qbit).expect_one() for qbit in qubits]
|
|
25
|
+
|
|
26
|
+
s = stmt(op_ssa, tuple(qubits_lowered))
|
|
27
|
+
return state.current_frame.push(s)
|
bloqade/squin/noise/__init__.py
CHANGED
|
@@ -4,5 +4,7 @@ from ._wrapper import (
|
|
|
4
4
|
pp_error as pp_error,
|
|
5
5
|
depolarize as depolarize,
|
|
6
6
|
qubit_loss as qubit_loss,
|
|
7
|
-
|
|
7
|
+
pauli_error as pauli_error,
|
|
8
|
+
two_qubit_pauli_channel as two_qubit_pauli_channel,
|
|
9
|
+
single_qubit_pauli_channel as single_qubit_pauli_channel,
|
|
8
10
|
)
|
bloqade/squin/noise/_wrapper.py
CHANGED
|
@@ -14,11 +14,15 @@ def pp_error(op: Op, p: float) -> Op: ...
|
|
|
14
14
|
|
|
15
15
|
|
|
16
16
|
@wraps(stmts.Depolarize)
|
|
17
|
-
def depolarize(
|
|
17
|
+
def depolarize(p: float) -> Op: ...
|
|
18
18
|
|
|
19
19
|
|
|
20
|
-
@wraps(stmts.
|
|
21
|
-
def
|
|
20
|
+
@wraps(stmts.SingleQubitPauliChannel)
|
|
21
|
+
def single_qubit_pauli_channel(params: tuple[float, float, float]) -> Op: ...
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@wraps(stmts.TwoQubitPauliChannel)
|
|
25
|
+
def two_qubit_pauli_channel(params: tuple[float, ...]) -> Op: ...
|
|
22
26
|
|
|
23
27
|
|
|
24
28
|
@wraps(stmts.QubitLoss)
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import itertools
|
|
2
|
+
|
|
3
|
+
from kirin import ir
|
|
4
|
+
from kirin.passes import Pass
|
|
5
|
+
from kirin.rewrite import Walk
|
|
6
|
+
from kirin.dialects import ilist
|
|
7
|
+
from kirin.rewrite.abc import RewriteRule, RewriteResult
|
|
8
|
+
|
|
9
|
+
from .stmts import (
|
|
10
|
+
PPError,
|
|
11
|
+
QubitLoss,
|
|
12
|
+
Depolarize,
|
|
13
|
+
PauliError,
|
|
14
|
+
NoiseChannel,
|
|
15
|
+
TwoQubitPauliChannel,
|
|
16
|
+
SingleQubitPauliChannel,
|
|
17
|
+
StochasticUnitaryChannel,
|
|
18
|
+
)
|
|
19
|
+
from ..op.stmts import X, Y, Z, Kron, Identity
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class _RewriteNoiseStmts(RewriteRule):
|
|
23
|
+
"""Rewrites squin noise statements to StochasticUnitaryChannel"""
|
|
24
|
+
|
|
25
|
+
def rewrite_Statement(self, node: ir.Statement) -> RewriteResult:
|
|
26
|
+
if not isinstance(node, NoiseChannel) or isinstance(node, QubitLoss):
|
|
27
|
+
return RewriteResult()
|
|
28
|
+
|
|
29
|
+
return getattr(self, "rewrite_" + node.name)(node)
|
|
30
|
+
|
|
31
|
+
def rewrite_pauli_error(self, node: PauliError) -> RewriteResult:
|
|
32
|
+
(operators := ilist.New(values=(node.basis,))).insert_before(node)
|
|
33
|
+
(ps := ilist.New(values=(node.p,))).insert_before(node)
|
|
34
|
+
stochastic_channel = StochasticUnitaryChannel(
|
|
35
|
+
operators=operators.result, probabilities=ps.result
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
node.replace_by(stochastic_channel)
|
|
39
|
+
return RewriteResult(has_done_something=True)
|
|
40
|
+
|
|
41
|
+
def rewrite_single_qubit_pauli_channel(
|
|
42
|
+
self, node: SingleQubitPauliChannel
|
|
43
|
+
) -> RewriteResult:
|
|
44
|
+
paulis = (X(), Y(), Z())
|
|
45
|
+
paulis_ssa: list[ir.SSAValue] = []
|
|
46
|
+
for op in paulis:
|
|
47
|
+
op.insert_before(node)
|
|
48
|
+
paulis_ssa.append(op.result)
|
|
49
|
+
|
|
50
|
+
(pauli_ops := ilist.New(values=paulis_ssa)).insert_before(node)
|
|
51
|
+
|
|
52
|
+
stochastic_unitary = StochasticUnitaryChannel(
|
|
53
|
+
operators=pauli_ops.result, probabilities=node.params
|
|
54
|
+
)
|
|
55
|
+
node.replace_by(stochastic_unitary)
|
|
56
|
+
return RewriteResult(has_done_something=True)
|
|
57
|
+
|
|
58
|
+
def rewrite_two_qubit_pauli_channel(
|
|
59
|
+
self, node: TwoQubitPauliChannel
|
|
60
|
+
) -> RewriteResult:
|
|
61
|
+
paulis = (X(), Y(), Z(), Identity(sites=1))
|
|
62
|
+
for op in paulis:
|
|
63
|
+
op.insert_before(node)
|
|
64
|
+
|
|
65
|
+
# NOTE: collect list so we can skip the last entry, which will be two identities
|
|
66
|
+
combinations = list(itertools.product(paulis, repeat=2))[:-1]
|
|
67
|
+
operators: list[ir.SSAValue] = []
|
|
68
|
+
for pauli_1, pauli_2 in combinations:
|
|
69
|
+
op = Kron(pauli_1.result, pauli_2.result)
|
|
70
|
+
op.insert_before(node)
|
|
71
|
+
operators.append(op.result)
|
|
72
|
+
|
|
73
|
+
(operator_list := ilist.New(values=operators)).insert_before(node)
|
|
74
|
+
stochastic_unitary = StochasticUnitaryChannel(
|
|
75
|
+
operators=operator_list.result, probabilities=node.params
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
node.replace_by(stochastic_unitary)
|
|
79
|
+
return RewriteResult(has_done_something=True)
|
|
80
|
+
|
|
81
|
+
def rewrite_p_p_error(self, node: PPError) -> RewriteResult:
|
|
82
|
+
(operators := ilist.New(values=(node.op,))).insert_before(node)
|
|
83
|
+
(ps := ilist.New(values=(node.p,))).insert_before(node)
|
|
84
|
+
stochastic_channel = StochasticUnitaryChannel(
|
|
85
|
+
operators=operators.result, probabilities=ps.result
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
node.replace_by(stochastic_channel)
|
|
89
|
+
return RewriteResult(has_done_something=True)
|
|
90
|
+
|
|
91
|
+
def rewrite_depolarize(self, node: Depolarize) -> RewriteResult:
|
|
92
|
+
paulis = (X(), Y(), Z())
|
|
93
|
+
operators: list[ir.SSAValue] = []
|
|
94
|
+
for op in paulis:
|
|
95
|
+
op.insert_before(node)
|
|
96
|
+
operators.append(op.result)
|
|
97
|
+
|
|
98
|
+
(operator_list := ilist.New(values=operators)).insert_before(node)
|
|
99
|
+
(ps := ilist.New(values=[node.p for _ in range(3)])).insert_before(node)
|
|
100
|
+
|
|
101
|
+
stochastic_unitary = StochasticUnitaryChannel(
|
|
102
|
+
operators=operator_list.result, probabilities=ps.result
|
|
103
|
+
)
|
|
104
|
+
node.replace_by(stochastic_unitary)
|
|
105
|
+
|
|
106
|
+
return RewriteResult(has_done_something=True)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
class RewriteNoiseStmts(Pass):
|
|
110
|
+
def unsafe_run(self, mt: ir.Method):
|
|
111
|
+
return Walk(_RewriteNoiseStmts()).rewrite(mt.code)
|
bloqade/squin/noise/stmts.py
CHANGED
|
@@ -1,21 +1,23 @@
|
|
|
1
|
-
from kirin import ir, types
|
|
1
|
+
from kirin import ir, types, lowering
|
|
2
2
|
from kirin.decl import info, statement
|
|
3
|
+
from kirin.dialects import ilist
|
|
3
4
|
|
|
4
5
|
from bloqade.squin.op.types import OpType
|
|
5
6
|
|
|
6
7
|
from ._dialect import dialect
|
|
8
|
+
from ..op.types import NumOperators
|
|
7
9
|
|
|
8
10
|
|
|
9
11
|
@statement
|
|
10
12
|
class NoiseChannel(ir.Statement):
|
|
11
|
-
|
|
13
|
+
traits = frozenset({lowering.FromPythonCall()})
|
|
14
|
+
result: ir.ResultValue = info.result(OpType)
|
|
12
15
|
|
|
13
16
|
|
|
14
17
|
@statement(dialect=dialect)
|
|
15
18
|
class PauliError(NoiseChannel):
|
|
16
19
|
basis: ir.SSAValue = info.argument(OpType)
|
|
17
20
|
p: ir.SSAValue = info.argument(types.Float)
|
|
18
|
-
result: ir.ResultValue = info.result(OpType)
|
|
19
21
|
|
|
20
22
|
|
|
21
23
|
@statement(dialect=dialect)
|
|
@@ -26,34 +28,37 @@ class PPError(NoiseChannel):
|
|
|
26
28
|
|
|
27
29
|
op: ir.SSAValue = info.argument(OpType)
|
|
28
30
|
p: ir.SSAValue = info.argument(types.Float)
|
|
29
|
-
result: ir.ResultValue = info.result(OpType)
|
|
30
31
|
|
|
31
32
|
|
|
32
33
|
@statement(dialect=dialect)
|
|
33
34
|
class Depolarize(NoiseChannel):
|
|
34
35
|
"""
|
|
35
|
-
Apply
|
|
36
|
-
NOTE For Stim, this can only accept 1 or 2 qubits
|
|
36
|
+
Apply depolarize error to qubit
|
|
37
37
|
"""
|
|
38
38
|
|
|
39
|
-
n_qubits: int = info.attribute(types.Int)
|
|
40
39
|
p: ir.SSAValue = info.argument(types.Float)
|
|
41
|
-
result: ir.ResultValue = info.result(OpType)
|
|
42
40
|
|
|
43
41
|
|
|
44
42
|
@statement(dialect=dialect)
|
|
45
|
-
class
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
params: ir.SSAValue = info.argument(
|
|
52
|
-
result: ir.ResultValue = info.result(OpType)
|
|
43
|
+
class SingleQubitPauliChannel(NoiseChannel):
|
|
44
|
+
params: ir.SSAValue = info.argument(ilist.IListType[types.Float, types.Literal(3)])
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@statement(dialect=dialect)
|
|
48
|
+
class TwoQubitPauliChannel(NoiseChannel):
|
|
49
|
+
params: ir.SSAValue = info.argument(ilist.IListType[types.Float, types.Literal(15)])
|
|
53
50
|
|
|
54
51
|
|
|
55
52
|
@statement(dialect=dialect)
|
|
56
53
|
class QubitLoss(NoiseChannel):
|
|
57
54
|
# NOTE: qubit loss error (not supported by Stim)
|
|
58
55
|
p: ir.SSAValue = info.argument(types.Float)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@statement(dialect=dialect)
|
|
59
|
+
class StochasticUnitaryChannel(ir.Statement):
|
|
60
|
+
operators: ir.SSAValue = info.argument(ilist.IListType[OpType, NumOperators])
|
|
61
|
+
probabilities: ir.SSAValue = info.argument(
|
|
62
|
+
ilist.IListType[types.Float, NumOperators]
|
|
63
|
+
)
|
|
59
64
|
result: ir.ResultValue = info.result(OpType)
|
bloqade/squin/op/__init__.py
CHANGED