1
1
from typing import Any , Sequence
2
+ from warnings import warn
2
3
3
4
import cirq
4
5
from kirin import ir , types
@@ -157,6 +158,8 @@ def main():
157
158
def emit_circuit (
158
159
mt : ir .Method ,
159
160
qubits : Sequence [cirq .Qid ] | None = None ,
161
+ circuit_qubits : Sequence [cirq .Qid ] | None = None ,
162
+ args : tuple = (),
160
163
ignore_returns : bool = False ,
161
164
) -> cirq .Circuit :
162
165
"""Converts a squin.kernel method to a cirq.Circuit object.
@@ -165,12 +168,14 @@ def emit_circuit(
165
168
mt (ir.Method): The kernel method from which to construct the circuit.
166
169
167
170
Keyword Args:
168
- qubits (Sequence[cirq.Qid] | None):
171
+ circuit_qubits (Sequence[cirq.Qid] | None):
169
172
A list of qubits to use as the qubits in the circuit. Defaults to None.
170
173
If this is None, then `cirq.LineQubit`s are inserted for every `squin.qubit.new`
171
174
statement in the order they appear inside the kernel.
172
175
**Note**: If a list of qubits is provided, make sure that there is a sufficient
173
176
number of qubits for the resulting circuit.
177
+ args (tuple):
178
+ The arguments of the kernel function from which to emit a circuit.
174
179
ignore_returns (bool):
175
180
If `False`, emitting a circuit from a kernel that returns a value will error.
176
181
Set it to `True` in order to ignore the return value(s). Defaults to `False`.
@@ -223,7 +228,7 @@ def main():
223
228
# custom list of qubits on grid
224
229
qubits = [cirq.GridQubit(i, i+1) for i in range(5)]
225
230
226
- circuit = squin.cirq.emit_circuit(main, qubits =qubits)
231
+ circuit = squin.cirq.emit_circuit(main, circuit_qubits =qubits)
227
232
print(circuit)
228
233
229
234
```
@@ -232,6 +237,12 @@ def main():
232
237
and manipulate the qubits in other circuits directly written in cirq as well.
233
238
"""
234
239
240
+ if circuit_qubits is None and qubits is not None :
241
+ circuit_qubits = qubits
242
+ warn (
243
+ "The keyword argument `qubits` is deprecated. Use `circuit_qubits` instead."
244
+ )
245
+
235
246
if (
236
247
not ignore_returns
237
248
and isinstance (mt .code , func .Function )
@@ -242,17 +253,24 @@ def main():
242
253
" Set `ignore_returns = True` in order to simply ignore the return values and emit a circuit."
243
254
)
244
255
256
+ if len (args ) != len (mt .args ):
257
+ raise ValueError (
258
+ f"The method from which you're trying to emit a circuit takes { len (mt .args )} as input, but you passed in { len (args )} via the `args` keyword!"
259
+ )
260
+
245
261
emitter = EmitCirq (qubits = qubits )
246
262
247
263
# Rewrite noise statements
248
264
mt_ = mt .similar (mt .dialects )
249
265
RewriteNoiseStmts (mt_ .dialects )(mt_ )
250
266
251
- return emitter .run (mt_ , args = () )
267
+ return emitter .run (mt_ , args = args )
252
268
253
269
254
270
def dump_circuit (
255
271
mt : ir .Method ,
272
+ circuit_qubits : Sequence [cirq .Qid ] | None = None ,
273
+ args : tuple = (),
256
274
qubits : Sequence [cirq .Qid ] | None = None ,
257
275
ignore_returns : bool = False ,
258
276
** kwargs ,
@@ -265,16 +283,24 @@ def dump_circuit(
265
283
mt (ir.Method): The kernel method from which to construct the circuit.
266
284
267
285
Keyword Args:
268
- qubits (Sequence[cirq.Qid] | None):
286
+ circuit_qubits (Sequence[cirq.Qid] | None):
269
287
A list of qubits to use as the qubits in the circuit. Defaults to None.
270
288
If this is None, then `cirq.LineQubit`s are inserted for every `squin.qubit.new`
271
289
statement in the order they appear inside the kernel.
272
290
**Note**: If a list of qubits is provided, make sure that there is a sufficient
273
291
number of qubits for the resulting circuit.
292
+ args (tuple):
293
+ The arguments of the kernel function from which to emit a circuit.
274
294
ignore_returns (bool):
275
295
If `False`, emitting a circuit from a kernel that returns a value will error.
276
296
Set it to `True` in order to ignore the return value(s). Defaults to `False`.
277
297
278
298
"""
279
- circuit = emit_circuit (mt , qubits = qubits , ignore_returns = ignore_returns )
299
+ circuit = emit_circuit (
300
+ mt ,
301
+ circuit_qubits = circuit_qubits ,
302
+ qubits = qubits ,
303
+ args = args ,
304
+ ignore_returns = ignore_returns ,
305
+ )
280
306
return cirq .to_json (circuit , ** kwargs )
0 commit comments