|
7 | 7 | - `kirin.dialects.ilist`: provides the `ilist.IListType` type for lists of qubits. |
8 | 8 | """ |
9 | 9 |
|
10 | | -from typing import Any |
| 10 | +from typing import Any, overload |
11 | 11 |
|
12 | 12 | from kirin import ir, types, lowering |
13 | 13 | from kirin.decl import info, statement |
@@ -42,7 +42,27 @@ class Broadcast(ir.Statement): |
42 | 42 |
|
43 | 43 |
|
44 | 44 | @statement(dialect=dialect) |
45 | | -class Measure(ir.Statement): |
| 45 | +class MeasureAny(ir.Statement): |
| 46 | + name = "measure" |
| 47 | + |
| 48 | + traits = frozenset({lowering.FromPythonCall()}) |
| 49 | + input: ir.SSAValue = info.argument(types.Any) |
| 50 | + result: ir.ResultValue = info.result(types.Any) |
| 51 | + |
| 52 | + |
| 53 | +@statement(dialect=dialect) |
| 54 | +class MeasureQubit(ir.Statement): |
| 55 | + name = "measure.qubit" |
| 56 | + |
| 57 | + traits = frozenset({lowering.FromPythonCall()}) |
| 58 | + qubit: ir.SSAValue = info.argument(ilist.IListType[QubitType]) |
| 59 | + result: ir.ResultValue = info.result(ilist.IListType[types.Bool]) |
| 60 | + |
| 61 | + |
| 62 | +@statement(dialect=dialect) |
| 63 | +class MeasureQubitList(ir.Statement): |
| 64 | + name = "measure.qubit.list" |
| 65 | + |
46 | 66 | traits = frozenset({lowering.FromPythonCall()}) |
47 | 67 | qubits: ir.SSAValue = info.argument(ilist.IListType[QubitType]) |
48 | 68 | result: ir.ResultValue = info.result(ilist.IListType[types.Bool]) |
@@ -89,31 +109,39 @@ def apply(operator: Op, qubits: ilist.IList[Qubit, Any] | list[Qubit]) -> None: |
89 | 109 | ... |
90 | 110 |
|
91 | 111 |
|
92 | | -@wraps(Broadcast) |
93 | | -def broadcast(operator: Op, qubits: ilist.IList[Qubit, Any] | list[Qubit]) -> None: |
94 | | - """Broadcast and apply an operator to a list of qubits. For example, an operator |
95 | | - that expects 2 qubits can be applied to a list of 2n qubits, where n is an integer > 0. |
| 112 | +@overload |
| 113 | +def measure(input: Qubit) -> bool: ... |
| 114 | +@overload |
| 115 | +def measure(input: ilist.IList[Qubit, Any] | list[Qubit]) -> list[bool]: ... |
| 116 | + |
| 117 | + |
| 118 | +@wraps(MeasureAny) |
| 119 | +def measure(input: Any) -> Any: |
| 120 | + """Measure a qubit or qubits in the list. |
96 | 121 |
|
97 | 122 | Args: |
98 | | - operator: The operator to broadcast and apply. |
99 | | - qubits: The list of qubits to broadcast and apply the operator to. The size of the list |
100 | | - must be inferable and match the number of qubits expected by the operator. |
| 123 | + input: A qubit or a list of qubits to measure. |
101 | 124 |
|
102 | 125 | Returns: |
103 | | - None |
| 126 | + bool | list[bool]: The result of the measurement. If a single qubit is measured, |
| 127 | + a single boolean is returned. If a list of qubits is measured, a list of booleans |
| 128 | + is returned. |
104 | 129 | """ |
105 | 130 | ... |
106 | 131 |
|
107 | 132 |
|
108 | | -@wraps(Measure) |
109 | | -def measure(qubits: ilist.IList[Qubit, Any]) -> int: |
110 | | - """Measure the qubits in the list." |
| 133 | +@wraps(Broadcast) |
| 134 | +def broadcast(operator: Op, qubits: ilist.IList[Qubit, Any] | list[Qubit]) -> None: |
| 135 | + """Broadcast and apply an operator to a list of qubits. For example, an operator |
| 136 | + that expects 2 qubits can be applied to a list of 2n qubits, where n is an integer > 0. |
111 | 137 |
|
112 | 138 | Args: |
113 | | - qubits: The list of qubits to measure. |
| 139 | + operator: The operator to broadcast and apply. |
| 140 | + qubits: The list of qubits to broadcast and apply the operator to. The size of the list |
| 141 | + must be inferable and match the number of qubits expected by the operator. |
114 | 142 |
|
115 | 143 | Returns: |
116 | | - int: The result of the measurement. |
| 144 | + None |
117 | 145 | """ |
118 | 146 | ... |
119 | 147 |
|
|
0 commit comments