Skip to content

Commit b9d5fa6

Browse files
dabaconrht
authored andcommitted
Fix one qubit gate docstrings to single standard, start gate zoo (quantumlib#5246)
This PR updates the doc strings for single qubit gates. These gates now all render correctly on quantumai.google. * This moves over to using LaTeX for the documentation. In some cases this makes the code doc string less readable, in some case it makes it is more readable. It renders nicely on quantumai.google This also starts quantumlib#852 for single qubit gates. Adding two and higher qubit gates will come in a follow up PR assuming the pattern in this PR is good.
1 parent 81c4c6f commit b9d5fa6

File tree

5 files changed

+380
-115
lines changed

5 files changed

+380
-115
lines changed

Diff for: cirq-core/cirq/ops/common_gates.py

+110-96
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,23 @@ def _pi(rads):
6868

6969
@value.value_equality
7070
class XPowGate(eigen_gate.EigenGate):
71-
"""A gate that rotates around the X axis of the Bloch sphere.
71+
r"""A gate that rotates around the X axis of the Bloch sphere.
7272
73-
The unitary matrix of ``XPowGate(exponent=t)`` is:
74-
75-
[[g·c, -i·g·s],
76-
[-i·g·s, g·c]]
77-
78-
where:
79-
80-
c = cos(π·t/2)
81-
s = sin(π·t/2)
82-
g = exp(i·π·t/2).
73+
The unitary matrix of `cirq.XPowGate(exponent=t)` is:
74+
$$
75+
\begin{bmatrix}
76+
e^{i \pi t /2} \cos(\pi t) & -i e^{i \pi t /2} \sin(\pi t) \\
77+
-i e^{i \pi t /2} \sin(\pi t) & e^{i \pi t /2} \cos(\pi t)
78+
\end{bmatrix}
79+
$$
8380
8481
Note in particular that this gate has a global phase factor of
85-
e^{i·π·t/2} vs the traditionally defined rotation matrices
86-
about the Pauli X axis. See `cirq.rx` for rotations without the global
82+
$e^{i \pi t / 2}$ vs the traditionally defined rotation matrices
83+
about the Pauli X axis. See `cirq.Rx` for rotations without the global
8784
phase. The global phase factor can be adjusted by using the `global_shift`
8885
parameter when initializing.
8986
90-
`cirq.X`, the Pauli X gate, is an instance of this gate at exponent=1.
87+
`cirq.X`, the Pauli X gate, is an instance of this gate at `exponent=1`.
9188
"""
9289

9390
def _num_qubits_(self) -> int:
@@ -245,14 +242,18 @@ def __repr__(self) -> str:
245242

246243

247244
class Rx(XPowGate):
248-
"""A gate, with matrix e^{-i X rads/2}, that rotates around the X axis of the Bloch sphere.
249-
250-
The unitary matrix of ``Rx(rads=t)`` is:
251-
252-
exp(-i X t/2) = [ cos(t/2) -isin(t/2)]
253-
[-isin(t/2) cos(t/2) ]
254-
255-
The gate corresponds to the traditionally defined rotation matrices about the Pauli X axis.
245+
r"""A gate with matrix $e^{-i X t/2}$ that rotates around the X axis of the Bloch sphere by $t$.
246+
247+
The unitary matrix of `cirq.Rx(rads=t)` is:
248+
$$
249+
e^{-i X t /2} =
250+
\begin{bmatrix}
251+
\cos(t/2) & -i \sin(t/2) \\
252+
-i \sin(t/2) & \cos(t/2)
253+
\end{bmatrix}
254+
$$
255+
256+
This gate corresponds to the traditionally defined rotation matrices about the Pauli X axis.
256257
"""
257258

258259
def __init__(self, *, rads: value.TParamVal):
@@ -286,26 +287,23 @@ def _from_json_dict_(cls, rads, **kwargs) -> 'Rx':
286287

287288
@value.value_equality
288289
class YPowGate(eigen_gate.EigenGate):
289-
"""A gate that rotates around the Y axis of the Bloch sphere.
290-
291-
The unitary matrix of ``YPowGate(exponent=t)`` is:
290+
r"""A gate that rotates around the Y axis of the Bloch sphere.
292291
293-
[[g·c, -g·s],
294-
[g·s, g·c]]
295-
296-
where:
297-
298-
c = cos(π·t/2)
299-
s = sin(π·t/2)
300-
g = exp(i·π·t/2).
292+
The unitary matrix of `cirq.YPowGate(exponent=t)` is:
293+
$$
294+
\begin{bmatrix}
295+
e^{i \pi t /2} \cos(\pi t /2) & - e^{i \pi t /2} \sin(\pi t /2) \\
296+
e^{i \pi t /2} \sin(\pi t /2) & e^{i \pi t /2} \cos(\pi t /2)
297+
\end{bmatrix}
298+
$$
301299
302300
Note in particular that this gate has a global phase factor of
303-
e^{i·π·t/2} vs the traditionally defined rotation matrices
301+
$e^{i \pi t / 2}$ vs the traditionally defined rotation matrices
304302
about the Pauli Y axis. See `cirq.Ry` for rotations without the global
305303
phase. The global phase factor can be adjusted by using the `global_shift`
306304
parameter when initializing.
307305
308-
`cirq.Y`, the Pauli Y gate, is an instance of this gate at exponent=1.
306+
`cirq.Y`, the Pauli Y gate, is an instance of this gate at `exponent=1`.
309307
"""
310308

311309
def _num_qubits_(self) -> int:
@@ -416,14 +414,18 @@ def __repr__(self) -> str:
416414

417415

418416
class Ry(YPowGate):
419-
"""A gate, with matrix e^{-i Y rads/2}, that rotates around the Y axis of the Bloch sphere.
420-
421-
The unitary matrix of ``Ry(rads=t)`` is:
422-
423-
exp(-i Y t/2) = [cos(t/2) -sin(t/2)]
424-
[sin(t/2) cos(t/2) ]
425-
426-
The gate corresponds to the traditionally defined rotation matrices about the Pauli Y axis.
417+
r"""A gate with matrix $e^{-i Y t/2}$ that rotates around the Y axis of the Bloch sphere by $t$.
418+
419+
The unitary matrix of `cirq.Ry(rads=t)` is:
420+
$$
421+
e^{-i Y t / 2} =
422+
\begin{bmatrix}
423+
\cos(t/2) & -\sin(t/2) \\
424+
\sin(t/2) & \cos(t/2)
425+
\end{bmatrix}
426+
$$
427+
428+
This gate corresponds to the traditionally defined rotation matrices about the Pauli Y axis.
427429
"""
428430

429431
def __init__(self, *, rads: value.TParamVal):
@@ -457,24 +459,23 @@ def _from_json_dict_(cls, rads, **kwargs) -> 'Ry':
457459

458460
@value.value_equality
459461
class ZPowGate(eigen_gate.EigenGate):
460-
"""A gate that rotates around the Z axis of the Bloch sphere.
461-
462-
The unitary matrix of ``ZPowGate(exponent=t)`` is:
462+
r"""A gate that rotates around the Z axis of the Bloch sphere.
463463
464-
[[1, 0],
465-
[0, g]]
466-
467-
where:
468-
469-
g = exp(i·π·t).
464+
The unitary matrix of `cirq.ZPowGate(exponent=t)` is:
465+
$$
466+
\begin{bmatrix}
467+
1 & 0 \\
468+
0 & e^{i \pi t}
469+
\end{bmatrix}
470+
$$
470471
471472
Note in particular that this gate has a global phase factor of
472-
e^{i·π·t/2} vs the traditionally defined rotation matrices
473+
$e^{i\pi t/2}$ vs the traditionally defined rotation matrices
473474
about the Pauli Z axis. See `cirq.Rz` for rotations without the global
474475
phase. The global phase factor can be adjusted by using the `global_shift`
475476
parameter when initializing.
476477
477-
`cirq.Z`, the Pauli Z gate, is an instance of this gate at exponent=1.
478+
`cirq.Z`, the Pauli Z gate, is an instance of this gate at `exponent=1`.
478479
"""
479480

480481
def _num_qubits_(self) -> int:
@@ -658,14 +659,18 @@ def _commutes_on_qids_(
658659

659660

660661
class Rz(ZPowGate):
661-
"""A gate, with matrix e^{-i Z rads/2}, that rotates around the Z axis of the Bloch sphere.
662-
663-
The unitary matrix of ``Rz(rads=t)`` is:
664-
665-
exp(-i Z t/2) = [ e^(-it/2) 0 ]
666-
[ 0 e^(it/2)]
667-
668-
The gate corresponds to the traditionally defined rotation matrices about the Pauli Z axis.
662+
r"""A gate with matrix $e^{-i Z t/2}$ that rotates around the Z axis of the Bloch sphere by $t$.
663+
664+
The unitary matrix of `cirq.Rz(rads=t)` is:
665+
$$
666+
e^{-i Z t /2} =
667+
\begin{bmatrix}
668+
e^{-it/2} & 0 \\
669+
0 & e^{it/2}
670+
\end{bmatrix}
671+
$$
672+
673+
This gate corresponds to the traditionally defined rotation matrices about the Pauli Z axis.
669674
"""
670675

671676
def __init__(self, *, rads: value.TParamVal):
@@ -698,20 +703,24 @@ def _from_json_dict_(cls, rads, **kwargs) -> 'Rz':
698703

699704

700705
class HPowGate(eigen_gate.EigenGate):
701-
"""A Gate that performs a rotation around the X+Z axis of the Bloch sphere.
702-
703-
The unitary matrix of ``HPowGate(exponent=t)`` is:
704-
705-
[[g·(c-i·s/sqrt(2)), -i·g·s/sqrt(2)],
706-
[-i·g·s/sqrt(2)], g·(c+i·s/sqrt(2))]]
707-
708-
where
709-
710-
c = cos(π·t/2)
711-
s = sin(π·t/2)
712-
g = exp(i·π·t/2).
713-
714-
Note in particular that for `t=1`, this gives the Hadamard matrix.
706+
r"""A Gate that performs a rotation around the X+Z axis of the Bloch sphere.
707+
708+
The unitary matrix of `cirq.HPowGate(exponent=t)` is:
709+
$$
710+
\begin{bmatrix}
711+
e^{i\pi t/2} \left(\cos(\pi t/2) - i \frac{\sin (\pi t /2)}{\sqrt{2}}\right)
712+
&& -i e^{i\pi t/2} \frac{\sin(\pi t /2)}{\sqrt{2}} \\
713+
-i e^{i\pi t/2} \frac{\sin(\pi t /2)}{\sqrt{2}}
714+
&& e^{i\pi t/2} \left(\cos(\pi t/2) + i \frac{\sin (\pi t /2)}{\sqrt{2}}\right)
715+
\end{bmatrix}
716+
$$
717+
Note in particular that for $t=1$, this gives the Hadamard matrix
718+
$$
719+
\begin{bmatrix}
720+
\frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \\
721+
\frac{1}{\sqrt{2}} & -\frac{1}{\sqrt{2}}
722+
\end{bmatrix}
723+
$$
715724
716725
`cirq.H`, the Hadamard gate, is an instance of this gate at `exponent=1`.
717726
"""
@@ -1001,7 +1010,7 @@ class CXPowGate(eigen_gate.EigenGate):
10011010
or named arguments CNOT(control=q1, target=q2).
10021011
(Mixing the two is not permitted.)
10031012
1004-
The unitary matrix of `CXPowGate(exponent=t)` is:
1013+
The unitary matrix of `cirq.CXPowGate(exponent=t)` is:
10051014
10061015
[[1, 0, 0, 0],
10071016
[0, 1, 0, 0],
@@ -1197,46 +1206,51 @@ def cphase(rads: value.TParamVal) -> CZPowGate:
11971206
H = HPowGate()
11981207
document(
11991208
H,
1200-
"""The Hadamard gate.
1209+
r"""The Hadamard gate.
12011210
12021211
The `exponent=1` instance of `cirq.HPowGate`.
12031212
1204-
Matrix:
1205-
```
1206-
[[s, s],
1207-
[s, -s]]
1208-
```
1209-
where s = sqrt(0.5).
1213+
The unitary matrix of `cirq.H` is:
1214+
$$
1215+
\begin{bmatrix}
1216+
\frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \\
1217+
\frac{1}{\sqrt{2}} & -\frac{1}{\sqrt{2}}
1218+
\end{bmatrix}
1219+
$$
12101220
""",
12111221
)
12121222

12131223
S = ZPowGate(exponent=0.5)
12141224
document(
12151225
S,
1216-
"""The Clifford S gate.
1226+
r"""The Clifford S gate.
12171227
12181228
The `exponent=0.5` instance of `cirq.ZPowGate`.
12191229
1220-
Matrix:
1221-
```
1222-
[[1, 0],
1223-
[0, i]]
1224-
```
1230+
The unitary matrix of `cirq.S` is:
1231+
$$
1232+
\begin{bmatrix}
1233+
1 & 0 \\
1234+
0 & i
1235+
\end{bmatrix}
1236+
$$
12251237
""",
12261238
)
12271239

12281240
T = ZPowGate(exponent=0.25)
12291241
document(
12301242
T,
1231-
"""The non-Clifford T gate.
1243+
r"""The non-Clifford T gate.
12321244
12331245
The `exponent=0.25` instance of `cirq.ZPowGate`.
12341246
1235-
Matrix:
1236-
```
1237-
[[1, 0]
1238-
[0, exp(i pi / 4)]]
1239-
```
1247+
The unitary matrix of `cirq.T` is
1248+
$$
1249+
\begin{bmatrix}
1250+
1 & 0 \\
1251+
0 & e^{i \pi /4}
1252+
\end{bmatrix}
1253+
$$
12401254
""",
12411255
)
12421256

Diff for: cirq-core/cirq/ops/pauli_gates.py

+27-12
Original file line numberDiff line numberDiff line change
@@ -182,36 +182,51 @@ def basis(self: '_PauliZ') -> Dict[int, '_ZEigenState']:
182182
X = _PauliX()
183183
document(
184184
X,
185-
"""The Pauli X gate.
185+
r"""The Pauli X gate.
186186
187-
Matrix:
187+
This is the `exponent=1` instance of the `cirq.XPowGate`.
188188
189-
[[0, 1],
190-
[1, 0]]
189+
The untary matrix of `cirq.X` is:
190+
$$
191+
\begin{bmatrix}
192+
0 & 1 \\
193+
1 & 0
194+
\end{bmatrix}
195+
$$
191196
""",
192197
)
193198

194199
Y = _PauliY()
195200
document(
196201
Y,
197-
"""The Pauli Y gate.
202+
r"""The Pauli Y gate.
198203
199-
Matrix:
204+
This is the `exponent=1` instance of the `cirq.YPowGate`.
200205
201-
[[0, -i],
202-
[i, 0]]
206+
The unitary matrix of `cirq.Y` is:
207+
$$
208+
\begin{bmatrix}
209+
0 & -i \\
210+
i & 0
211+
\end{bmatrix}
212+
$$
203213
""",
204214
)
205215

206216
Z = _PauliZ()
207217
document(
208218
Z,
209-
"""The Pauli Z gate.
219+
r"""The Pauli Z gate.
210220
211-
Matrix:
221+
This is the `exponent=1` instance of the `cirq.ZPowGate`.
212222
213-
[[1, 0],
214-
[0, -1]]
223+
The unitary matrix of `cirq.Z` is:
224+
$$
225+
\begin{bmatrix}
226+
1 & 0 \\
227+
0 & -1
228+
\end{bmatrix}
229+
$$
215230
""",
216231
)
217232

Diff for: cirq-core/cirq/ops/phased_x_gate.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,21 @@
2727

2828
@value.value_equality(manual_cls=True, approximate=True)
2929
class PhasedXPowGate(raw_types.Gate):
30-
"""A gate equivalent to the circuit ───Z^-p───X^t───Z^p───."""
30+
r"""A gate equivalent to $Z^{p} X^t Z^{-p}$.
31+
32+
The unitary matrix of `cirq.PhasedXPowGate(exponent=t, phase_exponent=p)` is:
33+
$$
34+
\begin{bmatrix}
35+
e^{i \pi t /2} \cos(\pi t/2) & -i e^{i \pi (t /2 - p)} \sin(\pi t /2) \\
36+
-i e^{i \pi (t /2 + p)} \sin(\pi t /2) & e^{i \pi t /2} \cos(\pi t/2)
37+
\end{bmatrix}
38+
$$
39+
40+
This gate is like an `cirq.XPowGate`, but which has been "phased",
41+
by applying a `cirq.ZPowGate` before and after this gate. In the language
42+
of the Bloch sphere, $p$ determines the axis in the XY plane about which
43+
a rotation of amount determined by $t$ occurs.
44+
"""
3145

3246
def __init__(
3347
self,

0 commit comments

Comments
 (0)