analysis
schedule
StmtDag
dataclass
StmtDag(
id_table: IdTable[
Statement
] = lambda: idtable.IdTable()(),
stmts: Dict[str, Statement] = OrderedDict(),
out_edges: Dict[str, Set[str]] = OrderedDict(),
inc_edges: Dict[str, Set[str]] = OrderedDict(),
stmt_index: Dict[Statement, int] = OrderedDict(),
)
Bases: Graph[Statement]
topological_groups
topological_groups()
Split the dag into topological groups where each group contains nodes that have no dependencies on each other, but have dependencies on nodes in one or more previous groups.
Yields:
Type | Description |
---|---|
List[str]: A list of node ids in a topological group |
Raises:
Type | Description |
---|---|
ValueError
|
If a cyclic dependency is detected |
The idea is to yield all nodes with no dependencies, then remove those nodes from the graph repeating until no nodes are left or we reach some upper limit. Worse case is a linear dag, so we can use len(dag.stmts) as the upper limit
If we reach the limit and there are still nodes left, then we have a cyclic dependency.
Source code in .venv/lib/python3.12/site-packages/bloqade/squin/analysis/schedule.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
|
cirq
dump_circuit
dump_circuit(
mt: Method,
qubits: Sequence[Qid] | None = None,
**kwargs
)
Converts a squin.kernel method to a cirq.Circuit object and dumps it as JSON.
This just runs emit_circuit
and calls the cirq.to_json
function to emit a JSON.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mt
|
Method
|
The kernel method from which to construct the circuit. |
required |
Other Parameters:
Name | Type | Description |
---|---|---|
qubits |
Sequence[Qid] | None
|
A list of qubits to use as the qubits in the circuit. Defaults to None.
If this is None, then |
Source code in .venv/lib/python3.12/site-packages/bloqade/squin/cirq/__init__.py
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
|
emit_circuit
emit_circuit(
mt: Method, qubits: Sequence[Qid] | None = None
) -> cirq.Circuit
Converts a squin.kernel method to a cirq.Circuit object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mt
|
Method
|
The kernel method from which to construct the circuit. |
required |
Other Parameters:
Name | Type | Description |
---|---|---|
qubits |
Sequence[Qid] | None
|
A list of qubits to use as the qubits in the circuit. Defaults to None.
If this is None, then |
Examples:
Here's a very basic example:
from bloqade import squin
@squin.kernel
def main():
q = squin.qubit.new(2)
h = squin.op.h()
squin.qubit.apply(h, q[0])
cx = squin.op.cx()
squin.qubit.apply(cx, q)
circuit = squin.cirq.emit_circuit(main)
print(circuit)
You can also compose multiple kernels. Those are emitted as subcircuits within the "main" circuit. Subkernels can accept arguments and return a value.
from bloqade import squin
from kirin.dialects import ilist
from typing import Literal
import cirq
@squin.kernel
def entangle(q: ilist.IList[squin.qubit.Qubit, Literal[2]]):
h = squin.op.h()
squin.qubit.apply(h, q[0])
cx = squin.op.cx()
squin.qubit.apply(cx, q)
return cx
@squin.kernel
def main():
q = squin.qubit.new(2)
cx = entangle(q)
q2 = squin.qubit.new(3)
squin.qubit.apply(cx, [q[1], q2[2]])
# custom list of qubits on grid
qubits = [cirq.GridQubit(i, i+1) for i in range(5)]
circuit = squin.cirq.emit_circuit(main, qubits=qubits)
print(circuit)
We also passed in a custom list of qubits above. This allows you to provide a custom geometry and manipulate the qubits in other circuits directly written in cirq as well.
Source code in .venv/lib/python3.12/site-packages/bloqade/squin/cirq/__init__.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
|
load_circuit
load_circuit(
circuit: Circuit,
kernel_name: str = "main",
dialects: DialectGroup = kernel,
register_as_argument: bool = False,
return_register: bool = False,
register_argument_name: str = "q",
globals: dict[str, Any] | None = None,
file: str | None = None,
lineno_offset: int = 0,
col_offset: int = 0,
compactify: bool = True,
)
Converts a cirq.Circuit object into a squin kernel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
circuit
|
Circuit
|
The circuit to load. |
required |
Other Parameters:
Name | Type | Description |
---|---|---|
kernel_name |
str
|
The name of the kernel to load. Defaults to "main". |
dialects |
DialectGroup | None
|
The dialects to use. Defaults to |
register_as_argument |
bool
|
Determine whether the resulting kernel function should accept
a single |
return_register |
bool
|
Determine whether the resulting kernel functionr returns a
single value of type |
register_argument_name |
str
|
The name of the argument that represents the qubit register.
Only used when |
globals |
dict[str, Any] | None
|
The global variables to use. Defaults to None. |
file |
str | None
|
The file name for error reporting. Defaults to None. |
lineno_offset |
int
|
The line number offset for error reporting. Defaults to 0. |
col_offset |
int
|
The column number offset for error reporting. Defaults to 0. |
compactify |
bool
|
Whether to compactify the output. Defaults to True. |
Usage Examples:
# from cirq's "hello qubit" example
import cirq
from bloqade import squin
# Pick a qubit.
qubit = cirq.GridQubit(0, 0)
# Create a circuit.
circuit = cirq.Circuit(
cirq.X(qubit)**0.5, # Square root of NOT.
cirq.measure(qubit, key='m') # Measurement.
)
# load the circuit as squin
main = squin.load_circuit(circuit)
# print the resulting IR
main.print()
You can also compose kernel functions generated from circuits by passing in and / or returning the respective quantum registers:
q = cirq.LineQubit.range(2)
circuit = cirq.Circuit(cirq.H(q[0]), cirq.CX(*q))
get_entangled_qubits = squin.cirq.load_circuit(
circuit, return_register=True, kernel_name="get_entangled_qubits"
)
get_entangled_qubits.print()
entangle_qubits = squin.cirq.load_circuit(
circuit, register_as_argument=True, kernel_name="entangle_qubits"
)
@squin.kernel
def main():
qreg = get_entangled_qubits()
qreg2 = squin.qubit.new(1)
entangle_qubits([qreg[1], qreg2[0]])
return squin.qubit.measure(qreg2)
Source code in .venv/lib/python3.12/site-packages/bloqade/squin/cirq/__init__.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
|
lowering
Squin
dataclass
Squin(circuit: Circuit)
Bases: LoweringABC[CirqNode]
Lower a cirq.Circuit object to a squin kernel
lowering
ApplyAnyCallLowering
dataclass
ApplyAnyCallLowering()
Bases: FromPythonCall['qubit.ApplyAny']
Custom lowering for ApplyAny that collects vararg qubits into a single tuple argument
noise
rewrite
stmts
Depolarize
Bases: NoiseChannel
Apply depolarize error to qubit
PPError
Bases: NoiseChannel
Pauli Product Error
op
rewrite
Rewrite py.binop.mult to Mult stmt
stdlib
ch
ch() -> types.Op
Control H gate.
Source code in .venv/lib/python3.12/site-packages/bloqade/squin/op/stdlib.py
53 54 55 56 |
|
cphase
cphase(theta: float) -> types.Op
Control Phase gate.
Source code in .venv/lib/python3.12/site-packages/bloqade/squin/op/stdlib.py
59 60 61 62 |
|
cx
cx() -> types.Op
Controlled X gate.
Source code in .venv/lib/python3.12/site-packages/bloqade/squin/op/stdlib.py
35 36 37 38 |
|
cy
cy() -> types.Op
Controlled Y gate.
Source code in .venv/lib/python3.12/site-packages/bloqade/squin/op/stdlib.py
41 42 43 44 |
|
cz
cz() -> types.Op
Control Z gate.
Source code in .venv/lib/python3.12/site-packages/bloqade/squin/op/stdlib.py
47 48 49 50 |
|
rx
rx(theta: float) -> types.Op
Rotation X gate.
Source code in .venv/lib/python3.12/site-packages/bloqade/squin/op/stdlib.py
17 18 19 20 |
|
ry
ry(theta: float) -> types.Op
Rotation Y gate.
Source code in .venv/lib/python3.12/site-packages/bloqade/squin/op/stdlib.py
23 24 25 26 |
|
rz
rz(theta: float) -> types.Op
Rotation Z gate.
Source code in .venv/lib/python3.12/site-packages/bloqade/squin/op/stdlib.py
29 30 31 32 |
|
stmts
P0
Bases: ConstantOp
The \(P_0\) projection operator.
P1
Bases: ConstantOp
The \(P_1\) projection operator.
PhaseOp
Bases: PrimitiveOp
A phase operator.
Reset
Bases: PrimitiveOp
Reset operator for qubits or wires.
ShiftOp
Bases: PrimitiveOp
A phase shift operator.
Sn
Bases: ConstantOp
\(S_{-}\) operator.
Sp
Bases: ConstantOp
\(S_{+}\) operator.
traits
HasSites
dataclass
HasSites()
Bases: StmtTrait
An operator with a sites
attribute.
qubit
qubit dialect for squin language.
This dialect defines the operations that can be performed on qubits.
Depends on:
- bloqade.squin.op
: provides the OpType
type and semantics for operators applied to qubits.
- kirin.dialects.ilist
: provides the ilist.IListType
type for lists of qubits.
broadcast
broadcast(
operator: Op, qubits: IList[Qubit, Any] | list[Qubit]
) -> None
Broadcast and apply an operator to a list of qubits. For example, an operator that expects 2 qubits can be applied to a list of 2n qubits, where n is an integer > 0.
For controlled operators, the list of qubits is interpreted as sets of (controls, targets). For example
apply(CX, [q0, q1, q2, q3])
is equivalent to
apply(CX, [q0, q1])
apply(CX, [q2, q3])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
operator
|
Op
|
The operator to broadcast and apply. |
required |
qubits
|
IList[Qubit, Any] | list[Qubit]
|
The list of qubits to broadcast and apply the operator to. The size of the list must be inferable and match the number of qubits expected by the operator. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in .venv/lib/python3.12/site-packages/bloqade/squin/qubit.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
|
measure
measure(input: Qubit) -> bool
measure(
input: IList[Qubit, Any] | list[Qubit],
) -> ilist.IList[bool, Any]
measure(input: Any) -> Any
Measure a qubit or qubits in the list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input
|
Any
|
A qubit or a list of qubits to measure. |
required |
Returns:
Type | Description |
---|---|
Any
|
bool | list[bool]: The result of the measurement. If a single qubit is measured, a single boolean is returned. If a list of qubits is measured, a list of booleans is returned. |
Source code in .venv/lib/python3.12/site-packages/bloqade/squin/qubit.py
139 140 141 142 143 144 145 146 147 148 149 150 151 |
|
new
new(n_qubits: int) -> ilist.IList[Qubit, Any]
Create a new list of qubits.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_qubits(int)
|
The number of qubits to create. |
required |
Returns:
Type | Description |
---|---|
IList[Qubit, Any]
|
(ilist.IList[Qubit, n_qubits]) A list of qubits. |
Source code in .venv/lib/python3.12/site-packages/bloqade/squin/qubit.py
82 83 84 85 86 87 88 89 90 91 92 |
|
rewrite
U3_to_clifford
SquinU3ToClifford
Bases: RewriteRule
Rewrite squin U3 statements to clifford when possible.
decompose_U3_gates
decompose_U3_gates(
node: U3,
) -> Tuple[List[ir.Statement], ...]
Rewrite U3 statements to clifford gates if possible.
Source code in .venv/lib/python3.12/site-packages/bloqade/squin/rewrite/U3_to_clifford.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
|
resolve_angle
resolve_angle(angle: float) -> int | None
Normalize the angle to be in the range [0, 2π).
Source code in .venv/lib/python3.12/site-packages/bloqade/squin/rewrite/U3_to_clifford.py
76 77 78 79 80 81 82 83 84 85 86 87 88 |
|
rewrite_ApplyOrBroadcast_onU3
rewrite_ApplyOrBroadcast_onU3(
node: Apply | Broadcast,
) -> RewriteResult
Rewrite Apply and Broadcast nodes to their clifford equivalent statements.
Source code in .venv/lib/python3.12/site-packages/bloqade/squin/rewrite/U3_to_clifford.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|
equivalent_u3_para
equivalent_u3_para(
theta_half_pi: int, phi_half_pi: int, lam_half_pi: int
) -> tuple[int, int, int]
- Assume all three angles are in the range [0, 4].
- U3(theta, phi, lam) = -U3(2pi-theta, phi+pi, lam+pi).
Source code in .venv/lib/python3.12/site-packages/bloqade/squin/rewrite/U3_to_clifford.py
48 49 50 51 52 53 54 55 |
|
desugar
ApplyDesugarRule
Bases: RewriteRule
Desugar apply operators in the kernel.
MeasureDesugarRule
Bases: RewriteRule
Desugar measure operations in the circuit.
wire
A NVIDIA QUAKE-like wire dialect.
This dialect is expected to be used in combination with the operator dialect as an intermediate representation for analysis and optimization of quantum circuits. Thus we do not define wrapping functions for the statements in this dialect.