Skip to content

Commit 219ed4d

Browse files
committed
Implement stdlib shorthands for direct application of squin gates (#415)
This supersedes #393. There's now `squin.gate` and `squin.parallel`, which allow the user to apply simple gates quickly. Not sure about the naming, but `gate` is basically `apply` and `parallel` is `broadcast`. For example ```python @squin.kernel def main(): q = squin.qubit.new(4) squin.gate.h(q[0]) squin.parallel.x(q) ``` One implementation detail here: Due to the `ApplyDesugar` pass, we need to inline the `gate` calls since it uses `apply`. If we don't inline those, the desugar pass doesn't do its magic and we are stuck with an `apply_any`. This can go once we fully remove the syntax `apply(op: Op, qubits: IList[Qubit])`. For now, however, I also inlined the `parallel` stuff to ensure they behave the same.
1 parent 7c01627 commit 219ed4d

File tree

4 files changed

+555
-0
lines changed

4 files changed

+555
-0
lines changed

src/bloqade/squin/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
)
1010
from .groups import wired as wired, kernel as kernel
1111

12+
# NOTE: it's important to keep these imports here since they import squin.kernel
13+
# we skip isort here
14+
from . import gate as gate, parallel as parallel # isort: skip
15+
1216
try:
1317
# NOTE: make sure optional cirq dependency is installed
1418
import cirq as cirq_package # noqa: F401

src/bloqade/squin/gate.py

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
from bloqade.types import Qubit
2+
3+
from . import op as _op, qubit as _qubit
4+
from .groups import kernel
5+
6+
7+
@kernel
8+
def x(qubit: Qubit) -> None:
9+
"""x gate applied to qubit."""
10+
op = _op.x()
11+
_qubit.apply(op, qubit)
12+
13+
14+
@kernel
15+
def y(qubit: Qubit) -> None:
16+
"""y gate applied to qubit."""
17+
op = _op.y()
18+
_qubit.apply(op, qubit)
19+
20+
21+
@kernel
22+
def z(qubit: Qubit) -> None:
23+
"""z gate applied to qubit."""
24+
op = _op.z()
25+
_qubit.apply(op, qubit)
26+
27+
28+
@kernel
29+
def sqrt_x(qubit: Qubit) -> None:
30+
"""Square root x gate applied to qubit."""
31+
op = _op.sqrt_x()
32+
_qubit.apply(op, qubit)
33+
34+
35+
@kernel
36+
def sqrt_x_adj(qubit: Qubit) -> None:
37+
"""Adjoint sqrt_x gate applied to qubit."""
38+
op = _op.sqrt_x()
39+
_qubit.apply(_op.adjoint(op), qubit)
40+
41+
42+
@kernel
43+
def sqrt_y(qubit: Qubit) -> None:
44+
"""Square root y gate applied to qubit."""
45+
op = _op.sqrt_y()
46+
_qubit.apply(op, qubit)
47+
48+
49+
@kernel
50+
def sqrt_y_adj(qubit: Qubit) -> None:
51+
"""Adjoint sqrt_y gate applied to qubit."""
52+
op = _op.sqrt_y()
53+
_qubit.apply(_op.adjoint(op), qubit)
54+
55+
56+
@kernel
57+
def sqrt_z(qubit: Qubit) -> None:
58+
"""Square root z gate applied to qubit."""
59+
op = _op.s()
60+
_qubit.apply(op, qubit)
61+
62+
63+
@kernel
64+
def sqrt_z_adj(qubit: Qubit) -> None:
65+
"""Adjoint square root z gate applied to qubit."""
66+
op = _op.s()
67+
_qubit.apply(_op.adjoint(op), qubit)
68+
69+
70+
@kernel
71+
def h(qubit: Qubit) -> None:
72+
"""Hadamard gate applied to qubit."""
73+
op = _op.h()
74+
_qubit.apply(op, qubit)
75+
76+
77+
@kernel
78+
def s(qubit: Qubit) -> None:
79+
"""s gate applied to qubit."""
80+
op = _op.s()
81+
_qubit.apply(op, qubit)
82+
83+
84+
@kernel
85+
def s_adj(qubit: Qubit) -> None:
86+
"""Adjoint s gate applied to qubit."""
87+
op = _op.s()
88+
_qubit.apply(_op.adjoint(op), qubit)
89+
90+
91+
@kernel
92+
def t(qubit: Qubit) -> None:
93+
"""t gate applied to qubit."""
94+
op = _op.t()
95+
_qubit.apply(op, qubit)
96+
97+
98+
@kernel
99+
def t_adj(qubit: Qubit) -> None:
100+
"""Adjoint t gate applied to qubit."""
101+
op = _op.t()
102+
_qubit.apply(_op.adjoint(op), qubit)
103+
104+
105+
@kernel
106+
def p0(qubit: Qubit) -> None:
107+
"""Projector on 0 applied to qubit."""
108+
op = _op.p0()
109+
_qubit.apply(op, qubit)
110+
111+
112+
@kernel
113+
def p1(qubit: Qubit) -> None:
114+
"""Projector on 1 applied to qubit."""
115+
op = _op.p1()
116+
_qubit.apply(op, qubit)
117+
118+
119+
@kernel
120+
def spin_n(qubit: Qubit) -> None:
121+
"""Spin lowering gate applied to qubit."""
122+
op = _op.spin_n()
123+
_qubit.apply(op, qubit)
124+
125+
126+
@kernel
127+
def spin_p(qubit: Qubit) -> None:
128+
"""Spin raising gate applied to qubit."""
129+
op = _op.spin_p()
130+
_qubit.apply(op, qubit)
131+
132+
133+
@kernel
134+
def reset(qubit: Qubit) -> None:
135+
"""Reset qubit to 0."""
136+
op = _op.reset()
137+
_qubit.apply(op, qubit)
138+
139+
140+
@kernel
141+
def cx(control: Qubit, target: Qubit) -> None:
142+
"""Controlled x gate applied to control and target"""
143+
op = _op.cx()
144+
_qubit.apply(op, control, target)
145+
146+
147+
@kernel
148+
def cy(control: Qubit, target: Qubit) -> None:
149+
"""Controlled y gate applied to control and target"""
150+
op = _op.cy()
151+
_qubit.apply(op, control, target)
152+
153+
154+
@kernel
155+
def cz(control: Qubit, target: Qubit) -> None:
156+
"""Controlled z gate applied to control and target"""
157+
op = _op.cz()
158+
_qubit.apply(op, control, target)
159+
160+
161+
@kernel
162+
def ch(control: Qubit, target: Qubit) -> None:
163+
"""Controlled Hadamard gate applied to control and target"""
164+
op = _op.ch()
165+
_qubit.apply(op, control, target)
166+
167+
168+
@kernel
169+
def u(theta: float, phi: float, lam: float, qubit: Qubit) -> None:
170+
"""3D rotation gate applied to control and target"""
171+
op = _op.u(theta, phi, lam)
172+
_qubit.apply(op, qubit)
173+
174+
175+
@kernel
176+
def rx(theta: float, qubit: Qubit) -> None:
177+
"""Rotation X gate applied to qubit."""
178+
op = _op.rot(_op.x(), theta)
179+
_qubit.apply(op, qubit)
180+
181+
182+
@kernel
183+
def ry(theta: float, qubit: Qubit) -> None:
184+
"""Rotation Y gate applied to qubit."""
185+
op = _op.rot(_op.y(), theta)
186+
_qubit.apply(op, qubit)
187+
188+
189+
@kernel
190+
def rz(theta: float, qubit: Qubit) -> None:
191+
"""Rotation Z gate applied to qubit."""
192+
op = _op.rot(_op.z(), theta)
193+
_qubit.apply(op, qubit)

0 commit comments

Comments
 (0)