Skip to content

Commit a3d68e2

Browse files
95-martin-orionrht
authored andcommitted
Improve common_gates docstrings (quantumlib#5722)
Bundle of minor fixes for docstrings in `common_gates.py`.
1 parent 507057b commit a3d68e2

File tree

2 files changed

+116
-29
lines changed

2 files changed

+116
-29
lines changed

cirq-core/cirq/ops/common_gates.py

Lines changed: 111 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,14 @@ def controlled(
197197
this is possible.
198198
199199
The conditions for the override to occur are:
200-
* The `global_shift` of the `XPowGate` is 0.
201-
* The `control_values` and `control_qid_shape` are compatible with
202-
the `CXPowGate`:
203-
* The last value of `control_qid_shape` is a qubit.
204-
* The last value of `control_values` corresponds to the
205-
control being satisfied if that last qubit is 1 and
206-
not satisfied if the last qubit is 0.
200+
201+
* The `global_shift` of the `XPowGate` is 0.
202+
* The `control_values` and `control_qid_shape` are compatible with
203+
the `CXPowGate`:
204+
* The last value of `control_qid_shape` is a qubit.
205+
* The last value of `control_values` corresponds to the
206+
control being satisfied if that last qubit is 1 and
207+
not satisfied if the last qubit is 0.
207208
208209
If these conditions are met, then the returned object is a `CXPowGate`
209210
or, in the case that there is more than one controlled qudit, a
@@ -215,6 +216,22 @@ def controlled(
215216
216217
If the above conditions are not met, a `ControlledGate` of this
217218
gate will be returned.
219+
220+
Args:
221+
num_controls: Total number of control qubits.
222+
control_values: Which control computational basis state to apply the
223+
sub gate. A sequence of length `num_controls` where each
224+
entry is an integer (or set of integers) corresponding to the
225+
computational basis state (or set of possible values) where that
226+
control is enabled. When all controls are enabled, the sub gate is
227+
applied. If unspecified, control values default to 1.
228+
control_qid_shape: The qid shape of the controls. A tuple of the
229+
expected dimension of each control qid. Defaults to
230+
`(2,) * num_controls`. Specify this argument when using qudits.
231+
232+
Returns:
233+
A `cirq.ControlledGate` (or `cirq.CXPowGate` if possible) representing
234+
`self` controlled by the given control values and qubits.
218235
"""
219236
result = super().controlled(num_controls, control_values, control_qid_shape)
220237
if (
@@ -309,6 +326,11 @@ class Rx(XPowGate):
309326
"""
310327

311328
def __init__(self, *, rads: value.TParamVal):
329+
"""Initialize an Rx (`cirq.XPowGate`).
330+
331+
Args:
332+
rads: Radians to rotate about the X axis of the Bloch sphere.
333+
"""
312334
self._rads = rads
313335
super().__init__(exponent=rads / _pi(rads), global_shift=-0.5)
314336

@@ -478,6 +500,11 @@ class Ry(YPowGate):
478500
"""
479501

480502
def __init__(self, *, rads: value.TParamVal):
503+
"""Initialize an Ry (`cirq.YPowGate`).
504+
505+
Args:
506+
rads: Radians to rotate about the Y axis of the Bloch sphere.
507+
"""
481508
self._rads = rads
482509
super().__init__(exponent=rads / _pi(rads), global_shift=-0.5)
483510

@@ -617,13 +644,14 @@ def controlled(
617644
this is possible.
618645
619646
The conditions for the override to occur are:
620-
* The `global_shift` of the `ZPowGate` is 0.
621-
* The `control_values` and `control_qid_shape` are compatible with
622-
the `CZPowGate`:
623-
* The last value of `control_qid_shape` is a qubit.
624-
* The last value of `control_values` corresponds to the
625-
control being satisfied if that last qubit is 1 and
626-
not satisfied if the last qubit is 0.
647+
648+
* The `global_shift` of the `ZPowGate` is 0.
649+
* The `control_values` and `control_qid_shape` are compatible with
650+
the `CZPowGate`:
651+
* The last value of `control_qid_shape` is a qubit.
652+
* The last value of `control_values` corresponds to the
653+
control being satisfied if that last qubit is 1 and
654+
not satisfied if the last qubit is 0.
627655
628656
If these conditions are met, then the returned object is a `CZPowGate`
629657
or, in the case that there is more than one controlled qudit, a
@@ -635,6 +663,22 @@ def controlled(
635663
636664
If the above conditions are not met, a `ControlledGate` of this
637665
gate will be returned.
666+
667+
Args:
668+
num_controls: Total number of control qubits.
669+
control_values: Which control computational basis state to apply the
670+
sub gate. A sequence of length `num_controls` where each
671+
entry is an integer (or set of integers) corresponding to the
672+
computational basis state (or set of possible values) where that
673+
control is enabled. When all controls are enabled, the sub gate is
674+
applied. If unspecified, control values default to 1.
675+
control_qid_shape: The qid shape of the controls. A tuple of the
676+
expected dimension of each control qid. Defaults to
677+
`(2,) * num_controls`. Specify this argument when using qudits.
678+
679+
Returns:
680+
A `cirq.ControlledGate` (or `cirq.CZPowGate` if possible) representing
681+
`self` controlled by the given control values and qubits.
638682
"""
639683
result = super().controlled(num_controls, control_values, control_qid_shape)
640684
if (
@@ -783,6 +827,11 @@ class Rz(ZPowGate):
783827
"""
784828

785829
def __init__(self, *, rads: value.TParamVal):
830+
"""Initialize an Rz (`cirq.ZPowGate`).
831+
832+
Args:
833+
rads: Radians to rotate about the Z axis of the Bloch sphere.
834+
"""
786835
self._rads = rads
787836
super().__init__(exponent=rads / _pi(rads), global_shift=-0.5)
788837

@@ -1031,13 +1080,14 @@ def controlled(
10311080
this is possible.
10321081
10331082
The conditions for the override to occur are:
1034-
* The `global_shift` of the `CZPowGate` is 0.
1035-
* The `control_values` and `control_qid_shape` are compatible with
1036-
the `CCZPowGate`:
1037-
* The last value of `control_qid_shape` is a qubit.
1038-
* The last value of `control_values` corresponds to the
1039-
control being satisfied if that last qubit is 1 and
1040-
not satisfied if the last qubit is 0.
1083+
1084+
* The `global_shift` of the `CZPowGate` is 0.
1085+
* The `control_values` and `control_qid_shape` are compatible with
1086+
the `CCZPowGate`:
1087+
* The last value of `control_qid_shape` is a qubit.
1088+
* The last value of `control_values` corresponds to the
1089+
control being satisfied if that last qubit is 1 and
1090+
not satisfied if the last qubit is 0.
10411091
10421092
If these conditions are met, then the returned object is a `CCZPowGate`
10431093
or, in the case that there is more than one controlled qudit, a
@@ -1049,6 +1099,22 @@ def controlled(
10491099
10501100
If the above conditions are not met, a `ControlledGate` of this
10511101
gate will be returned.
1102+
1103+
Args:
1104+
num_controls: Total number of control qubits.
1105+
control_values: Which control computational basis state to apply the
1106+
sub gate. A sequence of length `num_controls` where each
1107+
entry is an integer (or set of integers) corresponding to the
1108+
computational basis state (or set of possible values) where that
1109+
control is enabled. When all controls are enabled, the sub gate is
1110+
applied. If unspecified, control values default to 1.
1111+
control_qid_shape: The qid shape of the controls. A tuple of the
1112+
expected dimension of each control qid. Defaults to
1113+
`(2,) * num_controls`. Specify this argument when using qudits.
1114+
1115+
Returns:
1116+
A `cirq.ControlledGate` (or `cirq.CCZPowGate` if possible) representing
1117+
`self` controlled by the given control values and qubits.
10521118
"""
10531119
result = super().controlled(num_controls, control_values, control_qid_shape)
10541120
if (
@@ -1213,13 +1279,14 @@ def controlled(
12131279
this is possible.
12141280
12151281
The conditions for the override to occur are:
1216-
* The `global_shift` of the `CXPowGate` is 0.
1217-
* The `control_values` and `control_qid_shape` are compatible with
1218-
the `CCXPowGate`:
1219-
* The last value of `control_qid_shape` is a qubit.
1220-
* The last value of `control_values` corresponds to the
1221-
control being satisfied if that last qubit is 1 and
1222-
not satisfied if the last qubit is 0.
1282+
1283+
* The `global_shift` of the `CXPowGate` is 0.
1284+
* The `control_values` and `control_qid_shape` are compatible with
1285+
the `CCXPowGate`:
1286+
* The last value of `control_qid_shape` is a qubit.
1287+
* The last value of `control_values` corresponds to the
1288+
control being satisfied if that last qubit is 1 and
1289+
not satisfied if the last qubit is 0.
12231290
12241291
If these conditions are met, then the returned object is a `CCXPowGate`
12251292
or, in the case that there is more than one controlled qudit, a
@@ -1231,6 +1298,22 @@ def controlled(
12311298
12321299
If the above conditions are not met, a `ControlledGate` of this
12331300
gate will be returned.
1301+
1302+
Args:
1303+
num_controls: Total number of control qubits.
1304+
control_values: Which control computational basis state to apply the
1305+
sub gate. A sequence of length `num_controls` where each
1306+
entry is an integer (or set of integers) corresponding to the
1307+
computational basis state (or set of possible values) where that
1308+
control is enabled. When all controls are enabled, the sub gate is
1309+
applied. If unspecified, control values default to 1.
1310+
control_qid_shape: The qid shape of the controls. A tuple of the
1311+
expected dimension of each control qid. Defaults to
1312+
`(2,) * num_controls`. Specify this argument when using qudits.
1313+
1314+
Returns:
1315+
A `cirq.ControlledGate` (or `cirq.CCXPowGate` if possible) representing
1316+
`self` controlled by the given control values and qubits.
12341317
"""
12351318
result = super().controlled(num_controls, control_values, control_qid_shape)
12361319
if (

cirq-core/cirq/ops/raw_types.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,6 @@ def controlled(
367367
"""Returns a controlled version of this gate. If no arguments are
368368
specified, defaults to a single qubit control.
369369
370-
371370
Args:
372371
num_controls: Total number of control qubits.
373372
control_values: Which control computational basis state to apply the
@@ -379,6 +378,11 @@ def controlled(
379378
control_qid_shape: The qid shape of the controls. A tuple of the
380379
expected dimension of each control qid. Defaults to
381380
`(2,) * num_controls`. Specify this argument when using qudits.
381+
382+
Returns:
383+
A `cirq.Gate` representing `self` controlled by the given control values
384+
and qubits. This is a `cirq.ControlledGate` in the base
385+
implementation, but subclasses may return a different gate type.
382386
"""
383387

384388
if num_controls == 0:

0 commit comments

Comments
 (0)