|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +import cirq |
| 4 | +from kirin import ir, types |
| 5 | +from kirin.dialects import func |
| 6 | + |
| 7 | +from . import lowering as lowering |
| 8 | +from .. import kernel |
| 9 | +from .lowering import Squin |
| 10 | + |
| 11 | + |
| 12 | +def load_circuit( |
| 13 | + circuit: cirq.Circuit, |
| 14 | + kernel_name: str = "main", |
| 15 | + dialects: ir.DialectGroup = kernel, |
| 16 | + globals: dict[str, Any] | None = None, |
| 17 | + file: str | None = None, |
| 18 | + lineno_offset: int = 0, |
| 19 | + col_offset: int = 0, |
| 20 | + compactify: bool = True, |
| 21 | +): |
| 22 | + """Converts a cirq.Circuit object into a squin kernel. |
| 23 | +
|
| 24 | + Args: |
| 25 | + circuit (cirq.Circuit): The circuit to load. |
| 26 | +
|
| 27 | + Keyword Args: |
| 28 | + kernel_name (str): The name of the kernel to load. Defaults to "main". |
| 29 | + dialects (ir.DialectGroup | None): The dialects to use. Defaults to `squin.kernel`. |
| 30 | + globals (dict[str, Any] | None): The global variables to use. Defaults to None. |
| 31 | + file (str | None): The file name for error reporting. Defaults to None. |
| 32 | + lineno_offset (int): The line number offset for error reporting. Defaults to 0. |
| 33 | + col_offset (int): The column number offset for error reporting. Defaults to 0. |
| 34 | + compactify (bool): Whether to compactify the output. Defaults to True. |
| 35 | +
|
| 36 | + Example: |
| 37 | +
|
| 38 | + ```python |
| 39 | + # from cirq's "hello qubit" example |
| 40 | + import cirq |
| 41 | + from bloqade import squin |
| 42 | +
|
| 43 | + # Pick a qubit. |
| 44 | + qubit = cirq.GridQubit(0, 0) |
| 45 | +
|
| 46 | + # Create a circuit. |
| 47 | + circuit = cirq.Circuit( |
| 48 | + cirq.X(qubit)**0.5, # Square root of NOT. |
| 49 | + cirq.measure(qubit, key='m') # Measurement. |
| 50 | + ) |
| 51 | +
|
| 52 | + # load the circuit as squin |
| 53 | + main = squin.load_circuit(circuit) |
| 54 | +
|
| 55 | + # print the resulting IR |
| 56 | + main.print() |
| 57 | + ``` |
| 58 | + """ |
| 59 | + |
| 60 | + target = Squin(dialects=dialects, circuit=circuit) |
| 61 | + body = target.run( |
| 62 | + circuit, |
| 63 | + source=str(circuit), # TODO: proper source string |
| 64 | + file=file, |
| 65 | + globals=globals, |
| 66 | + lineno_offset=lineno_offset, |
| 67 | + col_offset=col_offset, |
| 68 | + compactify=compactify, |
| 69 | + ) |
| 70 | + |
| 71 | + # NOTE: no return value |
| 72 | + return_value = func.ConstantNone() |
| 73 | + body.blocks[0].stmts.append(return_value) |
| 74 | + body.blocks[0].stmts.append(func.Return(value_or_stmt=return_value)) |
| 75 | + |
| 76 | + code = func.Function( |
| 77 | + sym_name=kernel_name, |
| 78 | + signature=func.Signature((), types.NoneType), |
| 79 | + body=body, |
| 80 | + ) |
| 81 | + |
| 82 | + return ir.Method( |
| 83 | + mod=None, |
| 84 | + py_func=None, |
| 85 | + sym_name=kernel_name, |
| 86 | + arg_names=[], |
| 87 | + dialects=dialects, |
| 88 | + code=code, |
| 89 | + ) |
0 commit comments