bloqade-circuit 0.4.1__py3-none-any.whl → 0.4.3__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.
Potentially problematic release.
This version of bloqade-circuit might be problematic. Click here for more details.
- bloqade/qasm2/parse/lowering.py +67 -73
- bloqade/squin/cirq/__init__.py +112 -1
- bloqade/squin/cirq/emit/emit_circuit.py +109 -0
- bloqade/squin/cirq/emit/op.py +125 -0
- bloqade/squin/cirq/emit/qubit.py +60 -0
- bloqade/squin/cirq/emit/runtime.py +234 -0
- bloqade/squin/cirq/lowering.py +73 -4
- {bloqade_circuit-0.4.1.dist-info → bloqade_circuit-0.4.3.dist-info}/METADATA +1 -1
- {bloqade_circuit-0.4.1.dist-info → bloqade_circuit-0.4.3.dist-info}/RECORD +11 -7
- {bloqade_circuit-0.4.1.dist-info → bloqade_circuit-0.4.3.dist-info}/WHEEL +0 -0
- {bloqade_circuit-0.4.1.dist-info → bloqade_circuit-0.4.3.dist-info}/licenses/LICENSE +0 -0
bloqade/qasm2/parse/lowering.py
CHANGED
|
@@ -2,9 +2,9 @@ from typing import Any
|
|
|
2
2
|
from dataclasses import field, dataclass
|
|
3
3
|
|
|
4
4
|
from kirin import ir, types, lowering
|
|
5
|
-
from kirin.dialects import cf, func, ilist
|
|
5
|
+
from kirin.dialects import cf, scf, func, ilist
|
|
6
6
|
|
|
7
|
-
from bloqade.qasm2.types import CRegType, QRegType
|
|
7
|
+
from bloqade.qasm2.types import CRegType, QRegType, QubitType
|
|
8
8
|
from bloqade.qasm2.dialects import uop, core, expr, glob, noise, parallel
|
|
9
9
|
|
|
10
10
|
from . import ast
|
|
@@ -101,6 +101,13 @@ class QASM2(lowering.LoweringABC[ast.Node]):
|
|
|
101
101
|
def lower_global(
|
|
102
102
|
self, state: lowering.State[ast.Node], node: ast.Node
|
|
103
103
|
) -> lowering.LoweringABC.Result:
|
|
104
|
+
if isinstance(node, ast.Name):
|
|
105
|
+
# NOTE: might be a lookup for a gate function invoke
|
|
106
|
+
try:
|
|
107
|
+
return lowering.LoweringABC.Result(state.current_frame.globals[node.id])
|
|
108
|
+
except KeyError:
|
|
109
|
+
pass
|
|
110
|
+
|
|
104
111
|
raise lowering.BuildError("Global variables are not supported in QASM 2.0")
|
|
105
112
|
|
|
106
113
|
def visit_MainProgram(self, state: lowering.State[ast.Node], node: ast.MainProgram):
|
|
@@ -171,7 +178,6 @@ class QASM2(lowering.LoweringABC[ast.Node]):
|
|
|
171
178
|
def visit_Reset(self, state: lowering.State[ast.Node], node: ast.Reset):
|
|
172
179
|
state.current_frame.push(core.Reset(qarg=state.lower(node.qarg).expect_one()))
|
|
173
180
|
|
|
174
|
-
# TODO: clean this up? copied from cf dialect with a small modification
|
|
175
181
|
def visit_IfStmt(self, state: lowering.State[ast.Node], node: ast.IfStmt):
|
|
176
182
|
cond_stmt = core.CRegEq(
|
|
177
183
|
lhs=state.lower(node.cond.lhs).expect_one(),
|
|
@@ -179,84 +185,23 @@ class QASM2(lowering.LoweringABC[ast.Node]):
|
|
|
179
185
|
)
|
|
180
186
|
cond = state.current_frame.push(cond_stmt).result
|
|
181
187
|
frame = state.current_frame
|
|
182
|
-
before_block = frame.curr_block
|
|
183
188
|
|
|
184
|
-
with state.frame(node.body
|
|
189
|
+
with state.frame(node.body) as if_frame:
|
|
185
190
|
true_cond = if_frame.entr_block.args.append_from(types.Bool, cond.name)
|
|
186
191
|
if cond.name:
|
|
187
192
|
if_frame.defs[cond.name] = true_cond
|
|
188
193
|
|
|
194
|
+
# NOTE: pass in definitions from outer scope (usually just for the qreg)
|
|
195
|
+
if_frame.defs.update(frame.defs)
|
|
196
|
+
|
|
189
197
|
if_frame.exhaust()
|
|
190
|
-
self.branch_next_if_not_terminated(if_frame)
|
|
191
198
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
if cond.name:
|
|
195
|
-
else_frame.defs[cond.name] = true_cond
|
|
196
|
-
else_frame.exhaust()
|
|
197
|
-
self.branch_next_if_not_terminated(else_frame)
|
|
198
|
-
|
|
199
|
-
with state.frame(frame.stream.split(), region=frame.curr_region) as after_frame:
|
|
200
|
-
after_frame.defs.update(frame.defs)
|
|
201
|
-
phi: set[str] = set()
|
|
202
|
-
for name in if_frame.defs.keys():
|
|
203
|
-
if frame.get(name):
|
|
204
|
-
phi.add(name)
|
|
205
|
-
elif name in else_frame.defs:
|
|
206
|
-
phi.add(name)
|
|
207
|
-
|
|
208
|
-
for name in else_frame.defs.keys():
|
|
209
|
-
if frame.get(name): # not defined in if_frame
|
|
210
|
-
phi.add(name)
|
|
211
|
-
|
|
212
|
-
for name in phi:
|
|
213
|
-
after_frame.defs[name] = after_frame.entr_block.args.append_from(
|
|
214
|
-
types.Any, name
|
|
215
|
-
)
|
|
199
|
+
# NOTE: qasm2 can never yield anything from if
|
|
200
|
+
if_frame.push(scf.Yield())
|
|
216
201
|
|
|
217
|
-
|
|
218
|
-
self.branch_next_if_not_terminated(after_frame)
|
|
219
|
-
after_frame.next_block.stmts.append(
|
|
220
|
-
cf.Branch(arguments=(), successor=frame.next_block)
|
|
221
|
-
)
|
|
202
|
+
then_body = if_frame.curr_region
|
|
222
203
|
|
|
223
|
-
|
|
224
|
-
for name in phi:
|
|
225
|
-
if value := if_frame.get(name):
|
|
226
|
-
if_args.append(value)
|
|
227
|
-
else:
|
|
228
|
-
raise lowering.BuildError(f"undefined variable {name} in if branch")
|
|
229
|
-
|
|
230
|
-
else_args = []
|
|
231
|
-
for name in phi:
|
|
232
|
-
if value := else_frame.get(name):
|
|
233
|
-
else_args.append(value)
|
|
234
|
-
else:
|
|
235
|
-
raise lowering.BuildError(f"undefined variable {name} in else branch")
|
|
236
|
-
|
|
237
|
-
if_frame.next_block.stmts.append(
|
|
238
|
-
cf.Branch(
|
|
239
|
-
arguments=tuple(if_args),
|
|
240
|
-
successor=after_frame.entr_block,
|
|
241
|
-
)
|
|
242
|
-
)
|
|
243
|
-
else_frame.next_block.stmts.append(
|
|
244
|
-
cf.Branch(
|
|
245
|
-
arguments=tuple(else_args),
|
|
246
|
-
successor=after_frame.entr_block,
|
|
247
|
-
)
|
|
248
|
-
)
|
|
249
|
-
before_block.stmts.append(
|
|
250
|
-
cf.ConditionalBranch(
|
|
251
|
-
cond=cond,
|
|
252
|
-
then_arguments=(cond,),
|
|
253
|
-
then_successor=if_frame.entr_block,
|
|
254
|
-
else_arguments=(cond,),
|
|
255
|
-
else_successor=else_frame.entr_block,
|
|
256
|
-
)
|
|
257
|
-
)
|
|
258
|
-
frame.defs.update(after_frame.defs)
|
|
259
|
-
frame.jump_next_block()
|
|
204
|
+
state.current_frame.push(scf.IfElse(cond, then_body=then_body))
|
|
260
205
|
|
|
261
206
|
def branch_next_if_not_terminated(self, frame: lowering.Frame):
|
|
262
207
|
"""Branch to the next block if the current block is not terminated.
|
|
@@ -430,7 +375,56 @@ class QASM2(lowering.LoweringABC[ast.Node]):
|
|
|
430
375
|
raise lowering.BuildError(f"Include {node.filename} not found")
|
|
431
376
|
|
|
432
377
|
def visit_Gate(self, state: lowering.State[ast.Node], node: ast.Gate):
|
|
433
|
-
|
|
378
|
+
arg_names = node.cparams + node.qparams
|
|
379
|
+
arg_types = [types.Float for _ in node.cparams] + [
|
|
380
|
+
QubitType for _ in node.qparams
|
|
381
|
+
]
|
|
382
|
+
|
|
383
|
+
with state.frame(
|
|
384
|
+
stmts=node.body,
|
|
385
|
+
finalize_next=False,
|
|
386
|
+
) as body_frame:
|
|
387
|
+
# NOTE: insert _self as arg
|
|
388
|
+
body_frame.curr_block.args.append_from(
|
|
389
|
+
types.Generic(
|
|
390
|
+
ir.Method, types.Tuple.where(tuple(arg_types)), types.NoneType
|
|
391
|
+
),
|
|
392
|
+
name=node.name + "_self",
|
|
393
|
+
)
|
|
394
|
+
|
|
395
|
+
for arg_type, arg_name in zip(arg_types, arg_names):
|
|
396
|
+
# NOTE: append args as block arguments
|
|
397
|
+
block_arg = body_frame.curr_block.args.append_from(
|
|
398
|
+
arg_type, name=arg_name
|
|
399
|
+
)
|
|
400
|
+
|
|
401
|
+
# NOTE: add arguments as definitions to frame
|
|
402
|
+
body_frame.defs[arg_name] = block_arg
|
|
403
|
+
|
|
404
|
+
body_frame.exhaust()
|
|
405
|
+
|
|
406
|
+
# NOTE: append none as return value
|
|
407
|
+
return_val = func.ConstantNone()
|
|
408
|
+
body_frame.push(return_val)
|
|
409
|
+
body_frame.push(func.Return(return_val))
|
|
410
|
+
|
|
411
|
+
body = body_frame.curr_region
|
|
412
|
+
|
|
413
|
+
gate_func = expr.GateFunction(
|
|
414
|
+
sym_name=node.name,
|
|
415
|
+
signature=func.Signature(inputs=tuple(arg_types), output=types.NoneType),
|
|
416
|
+
body=body,
|
|
417
|
+
)
|
|
418
|
+
|
|
419
|
+
mt = ir.Method(
|
|
420
|
+
mod=None,
|
|
421
|
+
py_func=None,
|
|
422
|
+
sym_name=node.name,
|
|
423
|
+
dialects=self.dialects,
|
|
424
|
+
arg_names=[*node.cparams, *node.qparams],
|
|
425
|
+
code=gate_func,
|
|
426
|
+
)
|
|
427
|
+
state.current_frame.globals[node.name] = mt
|
|
434
428
|
|
|
435
429
|
def visit_Instruction(self, state: lowering.State[ast.Node], node: ast.Instruction):
|
|
436
430
|
params = [state.lower(param).expect_one() for param in node.params]
|
bloqade/squin/cirq/__init__.py
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
|
-
from typing import Any
|
|
1
|
+
from typing import Any, Sequence
|
|
2
2
|
|
|
3
3
|
import cirq
|
|
4
4
|
from kirin import ir, types
|
|
5
|
+
from kirin.emit import EmitError
|
|
5
6
|
from kirin.dialects import func
|
|
6
7
|
|
|
7
8
|
from . import lowering as lowering
|
|
8
9
|
from .. import kernel
|
|
10
|
+
|
|
11
|
+
# NOTE: just to register methods
|
|
12
|
+
from .emit import op as op, qubit as qubit
|
|
9
13
|
from .lowering import Squin
|
|
14
|
+
from .emit.emit_circuit import EmitCirq
|
|
10
15
|
|
|
11
16
|
|
|
12
17
|
def load_circuit(
|
|
@@ -87,3 +92,109 @@ def load_circuit(
|
|
|
87
92
|
dialects=dialects,
|
|
88
93
|
code=code,
|
|
89
94
|
)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def emit_circuit(
|
|
98
|
+
mt: ir.Method,
|
|
99
|
+
qubits: Sequence[cirq.Qid] | None = None,
|
|
100
|
+
) -> cirq.Circuit:
|
|
101
|
+
"""Converts a squin.kernel method to a cirq.Circuit object.
|
|
102
|
+
|
|
103
|
+
Args:
|
|
104
|
+
mt (ir.Method): The kernel method from which to construct the circuit.
|
|
105
|
+
|
|
106
|
+
Keyword Args:
|
|
107
|
+
qubits (Sequence[cirq.Qid] | None):
|
|
108
|
+
A list of qubits to use as the qubits in the circuit. Defaults to None.
|
|
109
|
+
If this is None, then `cirq.LineQubit`s are inserted for every `squin.qubit.new`
|
|
110
|
+
statement in the order they appear inside the kernel.
|
|
111
|
+
**Note**: If a list of qubits is provided, make sure that there is a sufficient
|
|
112
|
+
number of qubits for the resulting circuit.
|
|
113
|
+
|
|
114
|
+
## Examples:
|
|
115
|
+
|
|
116
|
+
Here's a very basic example:
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
from bloqade import squin
|
|
120
|
+
|
|
121
|
+
@squin.kernel
|
|
122
|
+
def main():
|
|
123
|
+
q = squin.qubit.new(2)
|
|
124
|
+
h = squin.op.h()
|
|
125
|
+
squin.qubit.apply(h, q[0])
|
|
126
|
+
cx = squin.op.cx()
|
|
127
|
+
squin.qubit.apply(cx, q)
|
|
128
|
+
|
|
129
|
+
circuit = squin.cirq.emit_circuit(main)
|
|
130
|
+
|
|
131
|
+
print(circuit)
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
You can also compose multiple kernels. Those are emitted as subcircuits within the "main" circuit.
|
|
135
|
+
Subkernels can accept arguments and return a value.
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
from bloqade import squin
|
|
139
|
+
from kirin.dialects import ilist
|
|
140
|
+
from typing import Literal
|
|
141
|
+
import cirq
|
|
142
|
+
|
|
143
|
+
@squin.kernel
|
|
144
|
+
def entangle(q: ilist.IList[squin.qubit.Qubit, Literal[2]]):
|
|
145
|
+
h = squin.op.h()
|
|
146
|
+
squin.qubit.apply(h, q[0])
|
|
147
|
+
cx = squin.op.cx()
|
|
148
|
+
squin.qubit.apply(cx, q)
|
|
149
|
+
return cx
|
|
150
|
+
|
|
151
|
+
@squin.kernel
|
|
152
|
+
def main():
|
|
153
|
+
q = squin.qubit.new(2)
|
|
154
|
+
cx = entangle(q)
|
|
155
|
+
q2 = squin.qubit.new(3)
|
|
156
|
+
squin.qubit.apply(cx, [q[1], q2[2]])
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
# custom list of qubits on grid
|
|
160
|
+
qubits = [cirq.GridQubit(i, i+1) for i in range(5)]
|
|
161
|
+
|
|
162
|
+
circuit = squin.cirq.emit_circuit(main, qubits=qubits)
|
|
163
|
+
print(circuit)
|
|
164
|
+
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
We also passed in a custom list of qubits above. This allows you to provide a custom geometry
|
|
168
|
+
and manipulate the qubits in other circuits directly written in cirq as well.
|
|
169
|
+
"""
|
|
170
|
+
|
|
171
|
+
if isinstance(mt.code, func.Function) and not mt.code.signature.output.is_subseteq(
|
|
172
|
+
types.NoneType
|
|
173
|
+
):
|
|
174
|
+
raise EmitError(
|
|
175
|
+
"The method you are trying to convert to a circuit has a return value, but returning from a circuit is not supported."
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
emitter = EmitCirq(qubits=qubits)
|
|
179
|
+
return emitter.run(mt, args=())
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def dump_circuit(mt: ir.Method, qubits: Sequence[cirq.Qid] | None = None, **kwargs):
|
|
183
|
+
"""Converts a squin.kernel method to a cirq.Circuit object and dumps it as JSON.
|
|
184
|
+
|
|
185
|
+
This just runs `emit_circuit` and calls the `cirq.to_json` function to emit a JSON.
|
|
186
|
+
|
|
187
|
+
Args:
|
|
188
|
+
mt (ir.Method): The kernel method from which to construct the circuit.
|
|
189
|
+
|
|
190
|
+
Keyword Args:
|
|
191
|
+
qubits (Sequence[cirq.Qid] | None):
|
|
192
|
+
A list of qubits to use as the qubits in the circuit. Defaults to None.
|
|
193
|
+
If this is None, then `cirq.LineQubit`s are inserted for every `squin.qubit.new`
|
|
194
|
+
statement in the order they appear inside the kernel.
|
|
195
|
+
**Note**: If a list of qubits is provided, make sure that there is a sufficient
|
|
196
|
+
number of qubits for the resulting circuit.
|
|
197
|
+
|
|
198
|
+
"""
|
|
199
|
+
circuit = emit_circuit(mt, qubits=qubits)
|
|
200
|
+
return cirq.to_json(circuit, **kwargs)
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
from typing import Sequence
|
|
2
|
+
from dataclasses import field, dataclass
|
|
3
|
+
|
|
4
|
+
import cirq
|
|
5
|
+
from kirin import ir
|
|
6
|
+
from kirin.emit import EmitABC, EmitError, EmitFrame
|
|
7
|
+
from kirin.interp import MethodTable, impl
|
|
8
|
+
from kirin.dialects import func
|
|
9
|
+
from typing_extensions import Self
|
|
10
|
+
|
|
11
|
+
from ... import kernel
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass
|
|
15
|
+
class EmitCirqFrame(EmitFrame):
|
|
16
|
+
qubit_index: int = 0
|
|
17
|
+
qubits: Sequence[cirq.Qid] | None = None
|
|
18
|
+
circuit: cirq.Circuit = field(default_factory=cirq.Circuit)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _default_kernel():
|
|
22
|
+
return kernel
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass
|
|
26
|
+
class EmitCirq(EmitABC[EmitCirqFrame, cirq.Circuit]):
|
|
27
|
+
keys = ["emit.cirq", "main"]
|
|
28
|
+
dialects: ir.DialectGroup = field(default_factory=_default_kernel)
|
|
29
|
+
void = cirq.Circuit()
|
|
30
|
+
qubits: Sequence[cirq.Qid] | None = None
|
|
31
|
+
_cached_circuit_operations: dict[int, cirq.CircuitOperation] = field(
|
|
32
|
+
init=False, default_factory=dict
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
def initialize(self) -> Self:
|
|
36
|
+
return super().initialize()
|
|
37
|
+
|
|
38
|
+
def initialize_frame(
|
|
39
|
+
self, code: ir.Statement, *, has_parent_access: bool = False
|
|
40
|
+
) -> EmitCirqFrame:
|
|
41
|
+
return EmitCirqFrame(
|
|
42
|
+
code, has_parent_access=has_parent_access, qubits=self.qubits
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
def run_method(self, method: ir.Method, args: tuple[cirq.Circuit, ...]):
|
|
46
|
+
return self.run_callable(method.code, args)
|
|
47
|
+
|
|
48
|
+
def emit_block(self, frame: EmitCirqFrame, block: ir.Block) -> cirq.Circuit:
|
|
49
|
+
for stmt in block.stmts:
|
|
50
|
+
result = self.eval_stmt(frame, stmt)
|
|
51
|
+
if isinstance(result, tuple):
|
|
52
|
+
frame.set_values(stmt.results, result)
|
|
53
|
+
|
|
54
|
+
return frame.circuit
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@func.dialect.register(key="emit.cirq")
|
|
58
|
+
class FuncEmit(MethodTable):
|
|
59
|
+
|
|
60
|
+
@impl(func.Function)
|
|
61
|
+
def emit_func(self, emit: EmitCirq, frame: EmitCirqFrame, stmt: func.Function):
|
|
62
|
+
emit.run_ssacfg_region(frame, stmt.body, ())
|
|
63
|
+
return (frame.circuit,)
|
|
64
|
+
|
|
65
|
+
@impl(func.Invoke)
|
|
66
|
+
def emit_invoke(self, emit: EmitCirq, frame: EmitCirqFrame, stmt: func.Invoke):
|
|
67
|
+
stmt_hash = hash((stmt.callee, stmt.inputs))
|
|
68
|
+
if (
|
|
69
|
+
cached_circuit_op := emit._cached_circuit_operations.get(stmt_hash)
|
|
70
|
+
) is not None:
|
|
71
|
+
# NOTE: cache hit
|
|
72
|
+
frame.circuit.append(cached_circuit_op)
|
|
73
|
+
return ()
|
|
74
|
+
|
|
75
|
+
ret = stmt.result
|
|
76
|
+
|
|
77
|
+
with emit.new_frame(stmt.callee.code, has_parent_access=True) as sub_frame:
|
|
78
|
+
sub_frame.qubit_index = frame.qubit_index
|
|
79
|
+
sub_frame.qubits = frame.qubits
|
|
80
|
+
|
|
81
|
+
region = stmt.callee.callable_region
|
|
82
|
+
if len(region.blocks) > 1:
|
|
83
|
+
raise EmitError(
|
|
84
|
+
"Subroutine with more than a single block encountered. This is not supported!"
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
# NOTE: get the arguments, "self" is just an empty circuit
|
|
88
|
+
method_self = emit.void
|
|
89
|
+
args = [frame.get(arg_) for arg_ in stmt.inputs]
|
|
90
|
+
emit.run_ssacfg_region(
|
|
91
|
+
sub_frame, stmt.callee.callable_region, args=(method_self, *args)
|
|
92
|
+
)
|
|
93
|
+
sub_circuit = sub_frame.circuit
|
|
94
|
+
|
|
95
|
+
# NOTE: check to see if the call terminates with a return value and fetch the value;
|
|
96
|
+
# we don't support multiple return statements via control flow so we just pick the first one
|
|
97
|
+
block = region.blocks[0]
|
|
98
|
+
return_stmt = next(
|
|
99
|
+
(stmt for stmt in block.stmts if isinstance(stmt, func.Return)), None
|
|
100
|
+
)
|
|
101
|
+
if return_stmt is not None:
|
|
102
|
+
frame.entries[ret] = sub_frame.get(return_stmt.value)
|
|
103
|
+
|
|
104
|
+
circuit_op = cirq.CircuitOperation(
|
|
105
|
+
sub_circuit.freeze(), use_repetition_ids=False
|
|
106
|
+
)
|
|
107
|
+
emit._cached_circuit_operations[stmt_hash] = circuit_op
|
|
108
|
+
frame.circuit.append(circuit_op)
|
|
109
|
+
return ()
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import math
|
|
2
|
+
|
|
3
|
+
import cirq
|
|
4
|
+
import numpy as np
|
|
5
|
+
from kirin.interp import MethodTable, impl
|
|
6
|
+
|
|
7
|
+
from ... import op
|
|
8
|
+
from .runtime import (
|
|
9
|
+
SnRuntime,
|
|
10
|
+
SpRuntime,
|
|
11
|
+
U3Runtime,
|
|
12
|
+
KronRuntime,
|
|
13
|
+
MultRuntime,
|
|
14
|
+
ScaleRuntime,
|
|
15
|
+
AdjointRuntime,
|
|
16
|
+
ControlRuntime,
|
|
17
|
+
UnitaryRuntime,
|
|
18
|
+
HermitianRuntime,
|
|
19
|
+
ProjectorRuntime,
|
|
20
|
+
OperatorRuntimeABC,
|
|
21
|
+
PauliStringRuntime,
|
|
22
|
+
)
|
|
23
|
+
from .emit_circuit import EmitCirq, EmitCirqFrame
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@op.dialect.register(key="emit.cirq")
|
|
27
|
+
class EmitCirqOpMethods(MethodTable):
|
|
28
|
+
|
|
29
|
+
@impl(op.stmts.X)
|
|
30
|
+
@impl(op.stmts.Y)
|
|
31
|
+
@impl(op.stmts.Z)
|
|
32
|
+
@impl(op.stmts.H)
|
|
33
|
+
def hermitian(
|
|
34
|
+
self, emit: EmitCirq, frame: EmitCirqFrame, stmt: op.stmts.ConstantUnitary
|
|
35
|
+
):
|
|
36
|
+
cirq_op = getattr(cirq, stmt.name.upper())
|
|
37
|
+
return (HermitianRuntime(cirq_op),)
|
|
38
|
+
|
|
39
|
+
@impl(op.stmts.S)
|
|
40
|
+
@impl(op.stmts.T)
|
|
41
|
+
def unitary(
|
|
42
|
+
self, emit: EmitCirq, frame: EmitCirqFrame, stmt: op.stmts.ConstantUnitary
|
|
43
|
+
):
|
|
44
|
+
cirq_op = getattr(cirq, stmt.name.upper())
|
|
45
|
+
return (UnitaryRuntime(cirq_op),)
|
|
46
|
+
|
|
47
|
+
@impl(op.stmts.P0)
|
|
48
|
+
@impl(op.stmts.P1)
|
|
49
|
+
def projector(
|
|
50
|
+
self, emit: EmitCirq, frame: EmitCirqFrame, stmt: op.stmts.P0 | op.stmts.P1
|
|
51
|
+
):
|
|
52
|
+
return (ProjectorRuntime(isinstance(stmt, op.stmts.P1)),)
|
|
53
|
+
|
|
54
|
+
@impl(op.stmts.Sn)
|
|
55
|
+
def sn(self, emit: EmitCirq, frame: EmitCirqFrame, stmt: op.stmts.Sn):
|
|
56
|
+
return (SnRuntime(),)
|
|
57
|
+
|
|
58
|
+
@impl(op.stmts.Sp)
|
|
59
|
+
def sp(self, emit: EmitCirq, frame: EmitCirqFrame, stmt: op.stmts.Sp):
|
|
60
|
+
return (SpRuntime(),)
|
|
61
|
+
|
|
62
|
+
@impl(op.stmts.Identity)
|
|
63
|
+
def identity(self, emit: EmitCirq, frame: EmitCirqFrame, stmt: op.stmts.Identity):
|
|
64
|
+
op = HermitianRuntime(cirq.IdentityGate(num_qubits=stmt.sites))
|
|
65
|
+
return (op,)
|
|
66
|
+
|
|
67
|
+
@impl(op.stmts.Control)
|
|
68
|
+
def control(self, emit: EmitCirq, frame: EmitCirqFrame, stmt: op.stmts.Control):
|
|
69
|
+
op: OperatorRuntimeABC = frame.get(stmt.op)
|
|
70
|
+
return (ControlRuntime(op, stmt.n_controls),)
|
|
71
|
+
|
|
72
|
+
@impl(op.stmts.Kron)
|
|
73
|
+
def kron(self, emit: EmitCirq, frame: EmitCirqFrame, stmt: op.stmts.Kron):
|
|
74
|
+
lhs = frame.get(stmt.lhs)
|
|
75
|
+
rhs = frame.get(stmt.rhs)
|
|
76
|
+
op = KronRuntime(lhs, rhs)
|
|
77
|
+
return (op,)
|
|
78
|
+
|
|
79
|
+
@impl(op.stmts.Mult)
|
|
80
|
+
def mult(self, emit: EmitCirq, frame: EmitCirqFrame, stmt: op.stmts.Mult):
|
|
81
|
+
lhs = frame.get(stmt.lhs)
|
|
82
|
+
rhs = frame.get(stmt.rhs)
|
|
83
|
+
op = MultRuntime(lhs, rhs)
|
|
84
|
+
return (op,)
|
|
85
|
+
|
|
86
|
+
@impl(op.stmts.Adjoint)
|
|
87
|
+
def adjoint(self, emit: EmitCirq, frame: EmitCirqFrame, stmt: op.stmts.Adjoint):
|
|
88
|
+
op_ = frame.get(stmt.op)
|
|
89
|
+
return (AdjointRuntime(op_),)
|
|
90
|
+
|
|
91
|
+
@impl(op.stmts.Scale)
|
|
92
|
+
def scale(self, emit: EmitCirq, frame: EmitCirqFrame, stmt: op.stmts.Scale):
|
|
93
|
+
op_ = frame.get(stmt.op)
|
|
94
|
+
factor = frame.get(stmt.factor)
|
|
95
|
+
return (ScaleRuntime(operator=op_, factor=factor),)
|
|
96
|
+
|
|
97
|
+
@impl(op.stmts.U3)
|
|
98
|
+
def u3(self, emit: EmitCirq, frame: EmitCirqFrame, stmt: op.stmts.U3):
|
|
99
|
+
theta = frame.get(stmt.theta)
|
|
100
|
+
phi = frame.get(stmt.phi)
|
|
101
|
+
lam = frame.get(stmt.lam)
|
|
102
|
+
return (U3Runtime(theta=theta, phi=phi, lam=lam),)
|
|
103
|
+
|
|
104
|
+
@impl(op.stmts.PhaseOp)
|
|
105
|
+
def phaseop(self, emit: EmitCirq, frame: EmitCirqFrame, stmt: op.stmts.PhaseOp):
|
|
106
|
+
theta = frame.get(stmt.theta)
|
|
107
|
+
op_ = HermitianRuntime(cirq.IdentityGate(num_qubits=1))
|
|
108
|
+
return (ScaleRuntime(operator=op_, factor=np.exp(1j * theta)),)
|
|
109
|
+
|
|
110
|
+
@impl(op.stmts.ShiftOp)
|
|
111
|
+
def shiftop(self, emit: EmitCirq, frame: EmitCirqFrame, stmt: op.stmts.ShiftOp):
|
|
112
|
+
theta = frame.get(stmt.theta)
|
|
113
|
+
|
|
114
|
+
# NOTE: ShiftOp(theta) == U3(pi, theta, 0)
|
|
115
|
+
return (U3Runtime(math.pi, theta, 0),)
|
|
116
|
+
|
|
117
|
+
@impl(op.stmts.Reset)
|
|
118
|
+
def reset(self, emit: EmitCirq, frame: EmitCirqFrame, stmt: op.stmts.Reset):
|
|
119
|
+
return (HermitianRuntime(cirq.ResetChannel()),)
|
|
120
|
+
|
|
121
|
+
@impl(op.stmts.PauliString)
|
|
122
|
+
def pauli_string(
|
|
123
|
+
self, emit: EmitCirq, frame: EmitCirqFrame, stmt: op.stmts.PauliString
|
|
124
|
+
):
|
|
125
|
+
return (PauliStringRuntime(stmt.string),)
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import cirq
|
|
2
|
+
from kirin.interp import MethodTable, impl
|
|
3
|
+
|
|
4
|
+
from ... import qubit
|
|
5
|
+
from .op import OperatorRuntimeABC
|
|
6
|
+
from .emit_circuit import EmitCirq, EmitCirqFrame
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@qubit.dialect.register(key="emit.cirq")
|
|
10
|
+
class EmitCirqQubitMethods(MethodTable):
|
|
11
|
+
@impl(qubit.New)
|
|
12
|
+
def new(self, emit: EmitCirq, frame: EmitCirqFrame, stmt: qubit.New):
|
|
13
|
+
n_qubits = frame.get(stmt.n_qubits)
|
|
14
|
+
|
|
15
|
+
if frame.qubits is not None:
|
|
16
|
+
cirq_qubits = [frame.qubits[i + frame.qubit_index] for i in range(n_qubits)]
|
|
17
|
+
else:
|
|
18
|
+
cirq_qubits = [
|
|
19
|
+
cirq.LineQubit(i + frame.qubit_index) for i in range(n_qubits)
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
frame.qubit_index += n_qubits
|
|
23
|
+
return (cirq_qubits,)
|
|
24
|
+
|
|
25
|
+
@impl(qubit.Apply)
|
|
26
|
+
def apply(self, emit: EmitCirq, frame: EmitCirqFrame, stmt: qubit.Apply):
|
|
27
|
+
op: OperatorRuntimeABC = frame.get(stmt.operator)
|
|
28
|
+
qbits = frame.get(stmt.qubits)
|
|
29
|
+
operations = op.apply(qbits)
|
|
30
|
+
for operation in operations:
|
|
31
|
+
frame.circuit.append(operation)
|
|
32
|
+
return ()
|
|
33
|
+
|
|
34
|
+
@impl(qubit.Broadcast)
|
|
35
|
+
def broadcast(self, emit: EmitCirq, frame: EmitCirqFrame, stmt: qubit.Broadcast):
|
|
36
|
+
op = frame.get(stmt.operator)
|
|
37
|
+
qbits = frame.get(stmt.qubits)
|
|
38
|
+
|
|
39
|
+
cirq_ops = []
|
|
40
|
+
for qbit in qbits:
|
|
41
|
+
cirq_ops.extend(op.apply([qbit]))
|
|
42
|
+
|
|
43
|
+
frame.circuit.append(cirq.Moment(cirq_ops))
|
|
44
|
+
return ()
|
|
45
|
+
|
|
46
|
+
@impl(qubit.MeasureQubit)
|
|
47
|
+
def measure_qubit(
|
|
48
|
+
self, emit: EmitCirq, frame: EmitCirqFrame, stmt: qubit.MeasureQubit
|
|
49
|
+
):
|
|
50
|
+
qbit = frame.get(stmt.qubit)
|
|
51
|
+
frame.circuit.append(cirq.measure(qbit))
|
|
52
|
+
return ()
|
|
53
|
+
|
|
54
|
+
@impl(qubit.MeasureQubitList)
|
|
55
|
+
def measure_qubit_list(
|
|
56
|
+
self, emit: EmitCirq, frame: EmitCirqFrame, stmt: qubit.MeasureQubitList
|
|
57
|
+
):
|
|
58
|
+
qbits = frame.get(stmt.qubits)
|
|
59
|
+
frame.circuit.append(cirq.measure(qbits))
|
|
60
|
+
return ()
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import math
|
|
2
|
+
from typing import Sequence
|
|
3
|
+
from numbers import Number
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
|
|
6
|
+
import cirq
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass
|
|
10
|
+
class OperatorRuntimeABC:
|
|
11
|
+
def num_qubits(self) -> int: ...
|
|
12
|
+
|
|
13
|
+
def check_qubits(self, qubits: Sequence[cirq.Qid]):
|
|
14
|
+
assert self.num_qubits() == len(qubits)
|
|
15
|
+
|
|
16
|
+
def apply(
|
|
17
|
+
self, qubits: Sequence[cirq.Qid], adjoint: bool = False
|
|
18
|
+
) -> list[cirq.Operation]:
|
|
19
|
+
self.check_qubits(qubits)
|
|
20
|
+
return self.unsafe_apply(qubits, adjoint=adjoint)
|
|
21
|
+
|
|
22
|
+
def unsafe_apply(
|
|
23
|
+
self, qubits: Sequence[cirq.Qid], adjoint: bool = False
|
|
24
|
+
) -> list[cirq.Operation]: ...
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass
|
|
28
|
+
class UnsafeOperatorRuntimeABC(OperatorRuntimeABC):
|
|
29
|
+
def check_qubits(self, qubits: Sequence[cirq.Qid]):
|
|
30
|
+
# NOTE: let's let cirq check this one
|
|
31
|
+
pass
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass
|
|
35
|
+
class BasicOpRuntime(UnsafeOperatorRuntimeABC):
|
|
36
|
+
gate: cirq.Gate
|
|
37
|
+
|
|
38
|
+
def num_qubits(self) -> int:
|
|
39
|
+
return self.gate.num_qubits()
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@dataclass
|
|
43
|
+
class UnitaryRuntime(BasicOpRuntime):
|
|
44
|
+
def unsafe_apply(
|
|
45
|
+
self, qubits: Sequence[cirq.Qid], adjoint: bool = False
|
|
46
|
+
) -> list[cirq.Operation]:
|
|
47
|
+
exponent = (-1) ** adjoint
|
|
48
|
+
return [self.gate(*qubits) ** exponent]
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@dataclass
|
|
52
|
+
class HermitianRuntime(BasicOpRuntime):
|
|
53
|
+
def unsafe_apply(
|
|
54
|
+
self, qubits: Sequence[cirq.Qid], adjoint: bool = False
|
|
55
|
+
) -> list[cirq.Operation]:
|
|
56
|
+
return [self.gate(*qubits)]
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@dataclass
|
|
60
|
+
class ProjectorRuntime(UnsafeOperatorRuntimeABC):
|
|
61
|
+
target_state: bool
|
|
62
|
+
|
|
63
|
+
def num_qubits(self) -> int:
|
|
64
|
+
return 1
|
|
65
|
+
|
|
66
|
+
def unsafe_apply(
|
|
67
|
+
self, qubits: Sequence[cirq.Qid], adjoint: bool = False
|
|
68
|
+
) -> list[cirq.Operation]:
|
|
69
|
+
# NOTE: this doesn't scale well, but works
|
|
70
|
+
sign = (-1) ** self.target_state
|
|
71
|
+
p = (1 + sign * cirq.Z(*qubits)) / 2
|
|
72
|
+
return [p]
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@dataclass
|
|
76
|
+
class SpRuntime(UnsafeOperatorRuntimeABC):
|
|
77
|
+
def num_qubits(self) -> int:
|
|
78
|
+
return 1
|
|
79
|
+
|
|
80
|
+
def unsafe_apply(
|
|
81
|
+
self, qubits: Sequence[cirq.Qid], adjoint: bool = False
|
|
82
|
+
) -> list[cirq.Operation]:
|
|
83
|
+
if adjoint:
|
|
84
|
+
return SnRuntime().unsafe_apply(qubits, adjoint=False)
|
|
85
|
+
|
|
86
|
+
return [(cirq.X(*qubits) - 1j * cirq.Y(*qubits)) / 2] # type: ignore -- we're not dealing with cirq's type issues
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
@dataclass
|
|
90
|
+
class SnRuntime(UnsafeOperatorRuntimeABC):
|
|
91
|
+
def num_qubits(self) -> int:
|
|
92
|
+
return 1
|
|
93
|
+
|
|
94
|
+
def unsafe_apply(
|
|
95
|
+
self, qubits: Sequence[cirq.Qid], adjoint: bool = False
|
|
96
|
+
) -> list[cirq.Operation]:
|
|
97
|
+
if adjoint:
|
|
98
|
+
return SpRuntime().unsafe_apply(qubits, adjoint=False)
|
|
99
|
+
|
|
100
|
+
return [(cirq.X(*qubits) + 1j * cirq.Y(*qubits)) / 2] # type: ignore -- we're not dealing with cirq's type issues
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
@dataclass
|
|
104
|
+
class MultRuntime(OperatorRuntimeABC):
|
|
105
|
+
lhs: OperatorRuntimeABC
|
|
106
|
+
rhs: OperatorRuntimeABC
|
|
107
|
+
|
|
108
|
+
def num_qubits(self) -> int:
|
|
109
|
+
n = self.lhs.num_qubits()
|
|
110
|
+
assert n == self.rhs.num_qubits()
|
|
111
|
+
return n
|
|
112
|
+
|
|
113
|
+
def unsafe_apply(
|
|
114
|
+
self, qubits: Sequence[cirq.Qid], adjoint: bool = False
|
|
115
|
+
) -> list[cirq.Operation]:
|
|
116
|
+
rhs = self.rhs.unsafe_apply(qubits, adjoint=adjoint)
|
|
117
|
+
lhs = self.lhs.unsafe_apply(qubits, adjoint=adjoint)
|
|
118
|
+
|
|
119
|
+
if adjoint:
|
|
120
|
+
return lhs + rhs
|
|
121
|
+
else:
|
|
122
|
+
return rhs + lhs
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
@dataclass
|
|
126
|
+
class KronRuntime(OperatorRuntimeABC):
|
|
127
|
+
lhs: OperatorRuntimeABC
|
|
128
|
+
rhs: OperatorRuntimeABC
|
|
129
|
+
|
|
130
|
+
def num_qubits(self) -> int:
|
|
131
|
+
return self.lhs.num_qubits() + self.rhs.num_qubits()
|
|
132
|
+
|
|
133
|
+
def unsafe_apply(
|
|
134
|
+
self, qubits: Sequence[cirq.Qid], adjoint: bool = False
|
|
135
|
+
) -> list[cirq.Operation]:
|
|
136
|
+
n = self.lhs.num_qubits()
|
|
137
|
+
cirq_ops = self.lhs.unsafe_apply(qubits[:n], adjoint=adjoint)
|
|
138
|
+
cirq_ops.extend(self.rhs.unsafe_apply(qubits[n:], adjoint=adjoint))
|
|
139
|
+
return cirq_ops
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
@dataclass
|
|
143
|
+
class ControlRuntime(OperatorRuntimeABC):
|
|
144
|
+
operator: OperatorRuntimeABC
|
|
145
|
+
n_controls: int
|
|
146
|
+
|
|
147
|
+
def num_qubits(self) -> int:
|
|
148
|
+
return self.n_controls + self.operator.num_qubits()
|
|
149
|
+
|
|
150
|
+
def unsafe_apply(
|
|
151
|
+
self, qubits: Sequence[cirq.Qid], adjoint: bool = False
|
|
152
|
+
) -> list[cirq.Operation]:
|
|
153
|
+
m = len(qubits) - self.n_controls
|
|
154
|
+
cirq_ops = self.operator.unsafe_apply(qubits[m:], adjoint=adjoint)
|
|
155
|
+
controlled_ops = [cirq_op.controlled_by(*qubits[:m]) for cirq_op in cirq_ops]
|
|
156
|
+
return controlled_ops
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
@dataclass
|
|
160
|
+
class AdjointRuntime(OperatorRuntimeABC):
|
|
161
|
+
operator: OperatorRuntimeABC
|
|
162
|
+
|
|
163
|
+
def num_qubits(self) -> int:
|
|
164
|
+
return self.operator.num_qubits()
|
|
165
|
+
|
|
166
|
+
def unsafe_apply(
|
|
167
|
+
self, qubits: Sequence[cirq.Qid], adjoint: bool = False
|
|
168
|
+
) -> list[cirq.Operation]:
|
|
169
|
+
# NOTE: to account for e.g. adjoint(adjoint(op))
|
|
170
|
+
passed_on_adjoint = not adjoint
|
|
171
|
+
return self.operator.unsafe_apply(qubits, adjoint=passed_on_adjoint)
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
@dataclass
|
|
175
|
+
class U3Runtime(UnsafeOperatorRuntimeABC):
|
|
176
|
+
theta: float
|
|
177
|
+
phi: float
|
|
178
|
+
lam: float
|
|
179
|
+
|
|
180
|
+
def num_qubits(self) -> int:
|
|
181
|
+
return 1
|
|
182
|
+
|
|
183
|
+
def angles(self, adjoint: bool) -> tuple[float, float, float]:
|
|
184
|
+
if adjoint:
|
|
185
|
+
# NOTE: adjoint(U(theta, phi, lam)) == U(-theta, -lam, -phi)
|
|
186
|
+
return -self.theta, -self.lam, -self.phi
|
|
187
|
+
else:
|
|
188
|
+
return self.theta, self.phi, self.lam
|
|
189
|
+
|
|
190
|
+
def unsafe_apply(
|
|
191
|
+
self, qubits: Sequence[cirq.Qid], adjoint: bool = False
|
|
192
|
+
) -> list[cirq.Operation]:
|
|
193
|
+
theta, phi, lam = self.angles(adjoint=adjoint)
|
|
194
|
+
|
|
195
|
+
ops = [
|
|
196
|
+
cirq.Rz(rads=lam)(*qubits),
|
|
197
|
+
cirq.Rx(rads=math.pi / 2)(*qubits),
|
|
198
|
+
cirq.Rz(rads=theta)(*qubits),
|
|
199
|
+
cirq.Rx(rads=-math.pi / 2)(*qubits),
|
|
200
|
+
cirq.Rz(rads=phi)(*qubits),
|
|
201
|
+
]
|
|
202
|
+
|
|
203
|
+
return ops
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
@dataclass
|
|
207
|
+
class ScaleRuntime(OperatorRuntimeABC):
|
|
208
|
+
factor: Number
|
|
209
|
+
operator: OperatorRuntimeABC
|
|
210
|
+
|
|
211
|
+
def num_qubits(self) -> int:
|
|
212
|
+
return self.operator.num_qubits()
|
|
213
|
+
|
|
214
|
+
def unsafe_apply(
|
|
215
|
+
self, qubits: Sequence[cirq.Qid], adjoint: bool = False
|
|
216
|
+
) -> list[cirq.Operation]:
|
|
217
|
+
cirq_ops = self.operator.unsafe_apply(qubits=qubits, adjoint=adjoint)
|
|
218
|
+
return [self.factor * cirq_ops[0]] + cirq_ops[1:] # type: ignore
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
@dataclass
|
|
222
|
+
class PauliStringRuntime(OperatorRuntimeABC):
|
|
223
|
+
string: str
|
|
224
|
+
|
|
225
|
+
def num_qubits(self) -> int:
|
|
226
|
+
return len(self.string)
|
|
227
|
+
|
|
228
|
+
def unsafe_apply(
|
|
229
|
+
self, qubits: Sequence[cirq.Qid], adjoint: bool = False
|
|
230
|
+
) -> list[cirq.Operation]:
|
|
231
|
+
pauli_mapping = {
|
|
232
|
+
qbit: pauli_label for (qbit, pauli_label) in zip(qubits, self.string)
|
|
233
|
+
}
|
|
234
|
+
return [cirq.PauliString(pauli_mapping)]
|
bloqade/squin/cirq/lowering.py
CHANGED
|
@@ -5,7 +5,7 @@ from dataclasses import field, dataclass
|
|
|
5
5
|
import cirq
|
|
6
6
|
from kirin import ir, lowering
|
|
7
7
|
from kirin.rewrite import Walk, CFGCompactify
|
|
8
|
-
from kirin.dialects import py, ilist
|
|
8
|
+
from kirin.dialects import py, scf, ilist
|
|
9
9
|
|
|
10
10
|
from .. import op, noise, qubit
|
|
11
11
|
|
|
@@ -150,10 +150,79 @@ class Squin(lowering.LoweringABC[CirqNode]):
|
|
|
150
150
|
):
|
|
151
151
|
if len(node.qubits) == 1:
|
|
152
152
|
qbit = self.lower_qubit_getindex(state, node.qubits[0])
|
|
153
|
-
|
|
153
|
+
stmt = state.current_frame.push(qubit.MeasureQubit(qbit))
|
|
154
|
+
else:
|
|
155
|
+
qbits = self.lower_qubit_getindices(state, node.qubits)
|
|
156
|
+
stmt = state.current_frame.push(qubit.MeasureQubitList(qbits))
|
|
154
157
|
|
|
155
|
-
|
|
156
|
-
|
|
158
|
+
key = node.gate.key
|
|
159
|
+
if isinstance(key, cirq.MeasurementKey):
|
|
160
|
+
key = key.name
|
|
161
|
+
|
|
162
|
+
state.current_frame.defs[key] = stmt.result
|
|
163
|
+
return stmt
|
|
164
|
+
|
|
165
|
+
def visit_ClassicallyControlledOperation(
|
|
166
|
+
self, state: lowering.State[CirqNode], node: cirq.ClassicallyControlledOperation
|
|
167
|
+
):
|
|
168
|
+
conditions: list[ir.SSAValue] = []
|
|
169
|
+
for outcome in node.classical_controls:
|
|
170
|
+
key = outcome.key
|
|
171
|
+
if isinstance(key, cirq.MeasurementKey):
|
|
172
|
+
key = key.name
|
|
173
|
+
measurement_outcome = state.current_frame.defs[key]
|
|
174
|
+
|
|
175
|
+
if measurement_outcome.type.is_subseteq(ilist.IListType):
|
|
176
|
+
# NOTE: there is currently no convenient ilist.any method, so we need to use foldl
|
|
177
|
+
# with a simple function that just does an or
|
|
178
|
+
|
|
179
|
+
def bool_op_or(x: bool, y: bool) -> bool:
|
|
180
|
+
return x or y
|
|
181
|
+
|
|
182
|
+
f_code = state.current_frame.push(
|
|
183
|
+
lowering.Python(self.dialects).python_function(bool_op_or)
|
|
184
|
+
)
|
|
185
|
+
fn = ir.Method(
|
|
186
|
+
mod=None,
|
|
187
|
+
py_func=bool_op_or,
|
|
188
|
+
sym_name="bool_op_or",
|
|
189
|
+
arg_names=[],
|
|
190
|
+
dialects=self.dialects,
|
|
191
|
+
code=f_code,
|
|
192
|
+
)
|
|
193
|
+
f_const = state.current_frame.push(py.constant.Constant(fn))
|
|
194
|
+
init_val = state.current_frame.push(py.Constant(False)).result
|
|
195
|
+
condition = state.current_frame.push(
|
|
196
|
+
ilist.Foldl(f_const.result, measurement_outcome, init=init_val)
|
|
197
|
+
).result
|
|
198
|
+
else:
|
|
199
|
+
condition = measurement_outcome
|
|
200
|
+
|
|
201
|
+
conditions.append(condition)
|
|
202
|
+
|
|
203
|
+
if len(conditions) == 1:
|
|
204
|
+
condition = conditions[0]
|
|
205
|
+
else:
|
|
206
|
+
condition = state.current_frame.push(
|
|
207
|
+
py.boolop.And(conditions[0], conditions[1])
|
|
208
|
+
).result
|
|
209
|
+
for next_cond in conditions[2:]:
|
|
210
|
+
condition = state.current_frame.push(
|
|
211
|
+
py.boolop.And(condition, next_cond)
|
|
212
|
+
).result
|
|
213
|
+
|
|
214
|
+
then_stmt = self.visit(state, node.without_classical_controls())
|
|
215
|
+
|
|
216
|
+
assert isinstance(
|
|
217
|
+
then_stmt, ir.Statement
|
|
218
|
+
), f"Expected operation of classically controlled node {node} to be lowered to a statement, got type {type(then_stmt)}. \
|
|
219
|
+
Please report this issue!"
|
|
220
|
+
|
|
221
|
+
# NOTE: remove stmt from parent block
|
|
222
|
+
then_stmt.detach()
|
|
223
|
+
then_body = ir.Block((then_stmt,))
|
|
224
|
+
|
|
225
|
+
return state.current_frame.push(scf.IfElse(condition, then_body=then_body))
|
|
157
226
|
|
|
158
227
|
def visit_SingleQubitPauliStringGateOperation(
|
|
159
228
|
self,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: bloqade-circuit
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.3
|
|
4
4
|
Summary: The software development toolkit for neutral atom arrays.
|
|
5
5
|
Author-email: Roger-luo <rluo@quera.com>, kaihsin <khwu@quera.com>, weinbe58 <pweinberg@quera.com>, johnzl-777 <jlong@quera.com>
|
|
6
6
|
License-File: LICENSE
|
|
@@ -74,7 +74,7 @@ bloqade/qasm2/emit/impls/noise.py,sha256=-N9PmCbz8MwC6xtd55GOpjDoWMyJPJBMVDWT3G8
|
|
|
74
74
|
bloqade/qasm2/parse/__init__.py,sha256=01tlLfrR015nAAPWw3i_Cs9IXsShpXMnJMagcJ_Vuik,986
|
|
75
75
|
bloqade/qasm2/parse/ast.py,sha256=a48ssf0D_xaE-27PsyeBD5lBvwN2Dojj-RWIBhy7jJE,2904
|
|
76
76
|
bloqade/qasm2/parse/build.py,sha256=2CibD1ZRX3_aknmhb5XvFQcI2sBOn97DlQHomb9CMEw,10621
|
|
77
|
-
bloqade/qasm2/parse/lowering.py,sha256=
|
|
77
|
+
bloqade/qasm2/parse/lowering.py,sha256=VRFhdDsgAeALVzJxmDtlRI3L1xLrAGnz_Zh-qNOChhM,20844
|
|
78
78
|
bloqade/qasm2/parse/parser.py,sha256=fxqp65dv8NnXE-Ie7ryLESfSH3Xr0unx1EBQysctiHM,121
|
|
79
79
|
bloqade/qasm2/parse/print.py,sha256=PaigQ5RbcfhOteWvDdQHoKsTE3tcNefpVfh1sp5eZEI,8973
|
|
80
80
|
bloqade/qasm2/parse/qasm2.lark,sha256=IYrBydUoVLn1VCNDPP5uNN5BHDET3fQ2yG11cOy900k,2238
|
|
@@ -117,8 +117,12 @@ bloqade/squin/analysis/nsites/__init__.py,sha256=RlQg7ivczXCXG5lMeL3ipYKj2oJKC4T
|
|
|
117
117
|
bloqade/squin/analysis/nsites/analysis.py,sha256=rIe1RU1MZRItcE2aB8DYahLrv73HfD3IHCX3E_EGQ1c,1773
|
|
118
118
|
bloqade/squin/analysis/nsites/impls.py,sha256=OaKuAoZ0EAorStYDZxzgc6Dk42kuj19MLkqHWG1MEQM,2592
|
|
119
119
|
bloqade/squin/analysis/nsites/lattice.py,sha256=ruh0808SHtj3ecuT-C3AZTsLY2j3DRhtezGiTZvcuVs,942
|
|
120
|
-
bloqade/squin/cirq/__init__.py,sha256=
|
|
121
|
-
bloqade/squin/cirq/lowering.py,sha256=
|
|
120
|
+
bloqade/squin/cirq/__init__.py,sha256=AptJlelH-KJoFKLnq6phq68SrV785zWzi2NOfLH62ms,5994
|
|
121
|
+
bloqade/squin/cirq/lowering.py,sha256=4-kZFH_qbBbV-c3-C9KhIB5o_cp_D8oxJrS8KicD_A8,14382
|
|
122
|
+
bloqade/squin/cirq/emit/emit_circuit.py,sha256=7puJ3eCFwE9VdPb9NAiSdyRNkoQPwo_uVykz9Yv7c14,3761
|
|
123
|
+
bloqade/squin/cirq/emit/op.py,sha256=z54NP5KqMxffXeFGWamEzvunpTNrxmYuluurk4j2-ps,4000
|
|
124
|
+
bloqade/squin/cirq/emit/qubit.py,sha256=Z2HUsZmJ5F2uHCPGru81ux2usoX77KwtS97_cgeJRMI,1910
|
|
125
|
+
bloqade/squin/cirq/emit/runtime.py,sha256=6_oHod-WK5yv0ae9xziQn-eh4Hn3MZNNqu4kJtOzPeY,6543
|
|
122
126
|
bloqade/squin/noise/__init__.py,sha256=HQl3FE0SZAGEX3qdveapCaMX391lgLvWeWnoE6Z2pYw,332
|
|
123
127
|
bloqade/squin/noise/_dialect.py,sha256=2IR98J-lXm5Y3srP9g-FD4JC-qTq2seureM6mKKq1xg,63
|
|
124
128
|
bloqade/squin/noise/_wrapper.py,sha256=0jD5va_go9jEW5rC6bZSWU30kjCha2-axFogPON3-V0,580
|
|
@@ -191,7 +195,7 @@ bloqade/visual/animation/runtime/atoms.py,sha256=EmjxhujLiHHPS_HtH_B-7TiqeHgvW5u
|
|
|
191
195
|
bloqade/visual/animation/runtime/ppoly.py,sha256=JB9IP53N1w6adBJEue6J5Nmj818Id9JvrlgrmiQTU1I,1385
|
|
192
196
|
bloqade/visual/animation/runtime/qpustate.py,sha256=rlmxQeJSvaohXrTpXQL5y-NJcpvfW33xPaYM1slv7cc,4270
|
|
193
197
|
bloqade/visual/animation/runtime/utils.py,sha256=ju9IzOWX-vKwfpqUjlUKu3Ssr_UFPFFq-tzH_Nqyo_c,1212
|
|
194
|
-
bloqade_circuit-0.4.
|
|
195
|
-
bloqade_circuit-0.4.
|
|
196
|
-
bloqade_circuit-0.4.
|
|
197
|
-
bloqade_circuit-0.4.
|
|
198
|
+
bloqade_circuit-0.4.3.dist-info/METADATA,sha256=ZBwC4D0_sLjDceh6og0S4tzzFghL5gTxP76QQrMsvv4,3683
|
|
199
|
+
bloqade_circuit-0.4.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
200
|
+
bloqade_circuit-0.4.3.dist-info/licenses/LICENSE,sha256=S5GIJwR6QCixPA9wryYb44ZEek0Nz4rt_zLUqP05UbU,13160
|
|
201
|
+
bloqade_circuit-0.4.3.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|