Skip to content

Commit bf8805c

Browse files
Cleanup cirq.Circuit api docs. (#5467)
Gives better rendering.
1 parent 1af17bd commit bf8805c

1 file changed

Lines changed: 88 additions & 43 deletions

File tree

cirq-core/cirq/circuits/circuit.py

Lines changed: 88 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -419,17 +419,21 @@ def reachable_frontier_from(
419419
420420
Examples:
421421
422-
If start_frontier is
422+
If `start_frontier` is
423423
424-
{
425-
cirq.LineQubit(0): 6,
426-
cirq.LineQubit(1): 2,
427-
cirq.LineQubit(2): 2
428-
}
424+
```
425+
{
426+
cirq.LineQubit(0): 6,
427+
cirq.LineQubit(1): 2,
428+
cirq.LineQubit(2): 2
429+
}
430+
```
429431
430432
then the reachable wire locations in the following circuit are
431433
highlighted with '█' characters:
432434
435+
```
436+
433437
0 1 2 3 4 5 6 7 8 9 10 11 12 13
434438
0: ───H───@─────────────────█████████████████████─@───H───
435439
│ │
@@ -438,34 +442,43 @@ def reachable_frontier_from(
438442
2: ─────────██████@███H██─@───────@───H───@───────────────
439443
│ │
440444
3: ───────────────────────@───H───@───────────────────────
445+
```
441446
442-
And the computed end_frontier is
447+
And the computed `end_frontier` is
443448
444-
{
445-
cirq.LineQubit(0): 11,
446-
cirq.LineQubit(1): 9,
447-
cirq.LineQubit(2): 6,
448-
}
449+
```
450+
{
451+
cirq.LineQubit(0): 11,
452+
cirq.LineQubit(1): 9,
453+
cirq.LineQubit(2): 6,
454+
}
455+
```
449456
450457
Note that the frontier indices (shown above the circuit) are
451458
best thought of (and shown) as happening *between* moment indices.
452459
453460
If we specify a blocker as follows:
454461
455-
is_blocker=lambda: op == cirq.CZ(cirq.LineQubit(1),
456-
cirq.LineQubit(2))
462+
```
463+
is_blocker=lambda: op == cirq.CZ(cirq.LineQubit(1),
464+
cirq.LineQubit(2))
465+
```
457466
458-
and use this start_frontier:
467+
and use this `start_frontier`:
459468
460-
{
461-
cirq.LineQubit(0): 0,
462-
cirq.LineQubit(1): 0,
463-
cirq.LineQubit(2): 0,
464-
cirq.LineQubit(3): 0,
465-
}
469+
```
470+
{
471+
cirq.LineQubit(0): 0,
472+
cirq.LineQubit(1): 0,
473+
cirq.LineQubit(2): 0,
474+
cirq.LineQubit(3): 0,
475+
}
476+
```
466477
467478
Then this is the reachable area:
468479
480+
```
481+
469482
0 1 2 3 4 5 6 7 8 9 10 11 12 13
470483
0: ─██H███@██████████████████████████████████████─@───H───
471484
│ │
@@ -475,14 +488,18 @@ def reachable_frontier_from(
475488
│ │
476489
3: ─█████████████████████─@───H───@───────────────────────
477490
478-
and the computed end_frontier is:
491+
```
492+
493+
and the computed `end_frontier` is:
479494
480-
{
481-
cirq.LineQubit(0): 11,
482-
cirq.LineQubit(1): 3,
483-
cirq.LineQubit(2): 3,
484-
cirq.LineQubit(3): 5,
485-
}
495+
```
496+
{
497+
cirq.LineQubit(0): 11,
498+
cirq.LineQubit(1): 3,
499+
cirq.LineQubit(2): 3,
500+
cirq.LineQubit(3): 5,
501+
}
502+
```
486503
487504
Args:
488505
start_frontier: A starting set of reachable locations.
@@ -629,7 +646,7 @@ def findall_operations_until_blocked(
629646
`is_blocker` return `False` or `True`, respectively, when applied to
630647
the gates; `M` indicates that it doesn't matter.
631648
632-
649+
```
633650
─(─F───F─────── ┄(─F───F─)┄┄┄┄┄
634651
│ │ │ │
635652
─(─F───F───T─── => ┄(─F───F─)┄┄┄┄┄
@@ -667,6 +684,7 @@ def findall_operations_until_blocked(
667684
───────F───F─── ┄┄┄┄┄┄┄┄┄(─F─)┄
668685
│ │
669686
─(─────────F─── ┄┄┄┄┄┄┄┄┄(─F─)┄
687+
```
670688
671689
Args:
672690
start_frontier: A starting set of reachable locations.
@@ -754,10 +772,17 @@ def findall_operations_with_gate_type(
754772
yield index, gate_op, cast(_TGate, gate_op.gate)
755773

756774
def has_measurements(self):
775+
"""Returns whether or not this circuit has measurements.
776+
777+
Returns: True if `cirq.is_measurement(self)` is True otherwise False.
778+
"""
757779
return protocols.is_measurement(self)
758780

759781
def are_all_measurements_terminal(self) -> bool:
760-
"""Whether all measurement gates are at the end of the circuit."""
782+
"""Whether all measurement gates are at the end of the circuit.
783+
784+
Returns: True iff no measurement is followed by a gate.
785+
"""
761786
return self.are_all_matches_terminal(protocols.is_measurement)
762787

763788
def are_all_matches_terminal(self, predicate: Callable[['cirq.Operation'], bool]) -> bool:
@@ -799,7 +824,10 @@ def are_all_matches_terminal(self, predicate: Callable[['cirq.Operation'], bool]
799824
return True
800825

801826
def are_any_measurements_terminal(self) -> bool:
802-
"""Whether any measurement gates are at the end of the circuit."""
827+
"""Whether any measurement gates are at the end of the circuit.
828+
829+
Returns: True iff some measurements are not followed by a gate.
830+
"""
803831
return self.are_any_matches_terminal(protocols.is_measurement)
804832

805833
def are_any_matches_terminal(self, predicate: Callable[['cirq.Operation'], bool]) -> bool:
@@ -846,15 +874,17 @@ def _has_op_at(self, moment_index: int, qubits: Iterable['cirq.Qid']) -> bool:
846874
)
847875

848876
def all_qubits(self) -> FrozenSet['cirq.Qid']:
849-
"""Returns the qubits acted upon by Operations in this circuit."""
877+
"""Returns the qubits acted upon by Operations in this circuit.
878+
879+
Returns: FrozenSet of `cirq.Qid` objects acted on by all operations
880+
in this circuit.
881+
"""
850882
return frozenset(q for m in self.moments for q in m.qubits)
851883

852884
def all_operations(self) -> Iterator['cirq.Operation']:
853-
"""Iterates over the operations applied by this circuit.
885+
"""Returns an iterator over the operations in the circuit.
854886
855-
Operations from earlier moments will be iterated over first. Operations
856-
within a moment are iterated in the order they were given to the
857-
moment's constructor.
887+
Returns: Iterator over `cirq.Operation` elements found in this circuit.
858888
"""
859889
return (op for moment in self for op in moment.operations)
860890

@@ -880,16 +910,31 @@ def map_moment(moment: 'cirq.Moment') -> 'cirq.Circuit':
880910
def qid_shape(
881911
self, qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT
882912
) -> Tuple[int, ...]:
913+
"""Get the qubit shapes of all qubits in this circuit.
914+
915+
Returns: A tuple containing the dimensions (shape) of all qudits
916+
found in this circuit according to `qubit_order`.
917+
"""
883918
qids = ops.QubitOrder.as_qubit_order(qubit_order).order_for(self.all_qubits())
884919
return protocols.qid_shape(qids)
885920

886921
def all_measurement_key_objs(self) -> AbstractSet['cirq.MeasurementKey']:
887922
return {key for op in self.all_operations() for key in protocols.measurement_key_objs(op)}
888923

889924
def _measurement_key_objs_(self) -> AbstractSet['cirq.MeasurementKey']:
925+
"""Returns the set of all measurement keys in this circuit.
926+
927+
Returns: AbstractSet of `cirq.MeasurementKey` objects that are
928+
in this circuit.
929+
"""
890930
return self.all_measurement_key_objs()
891931

892932
def all_measurement_key_names(self) -> AbstractSet[str]:
933+
"""Returns the set of all measurement key names in this circuit.
934+
935+
Returns: AbstractSet of strings that are the measurement key
936+
names in this circuit.
937+
"""
893938
return {key for op in self.all_operations() for key in protocols.measurement_key_names(op)}
894939

895940
def _measurement_key_names_(self) -> AbstractSet[str]:
@@ -1704,6 +1749,7 @@ def __copy__(self) -> 'cirq.Circuit':
17041749
return self.copy()
17051750

17061751
def copy(self) -> 'Circuit':
1752+
"""Return a copy of this circuit."""
17071753
copied_circuit = Circuit()
17081754
copied_circuit._moments = self._moments[:]
17091755
return copied_circuit
@@ -2188,11 +2234,10 @@ def batch_insert_into(self, insert_intos: Iterable[Tuple[int, 'cirq.OP_TREE']])
21882234
insert_intos: A sequence of (moment_index, new_op_tree)
21892235
pairs indicating a moment to add new operations into.
21902236
2191-
ValueError:
2192-
One of the insertions collided with an existing operation.
2193-
2194-
IndexError:
2195-
Inserted into a moment index that doesn't exist.
2237+
Raises:
2238+
ValueError: One of the insertions collided with an existing
2239+
operation.
2240+
IndexError: Inserted into a moment index that doesn't exist.
21962241
"""
21972242
copy = self.copy()
21982243
for i, insertions in insert_intos:
@@ -2208,7 +2253,7 @@ def batch_insert(self, insertions: Iterable[Tuple[int, 'cirq.OP_TREE']]) -> None
22082253
causes a new moment to be created, then the insert at "4" will actually
22092254
occur at index 5 to account for the shift from the new moment.
22102255
2211-
All insertions are done with the strategy 'EARLIEST'.
2256+
All insertions are done with the strategy `cirq.InsertStrategy.EARLIEST`.
22122257
22132258
When multiple inserts occur at the same index, the gates from the later
22142259
inserts end up before the gates from the earlier inserts (exactly as if
@@ -2237,7 +2282,7 @@ def append(
22372282
self,
22382283
moment_or_operation_tree: Union['cirq.Moment', 'cirq.OP_TREE'],
22392284
strategy: 'cirq.InsertStrategy' = InsertStrategy.EARLIEST,
2240-
):
2285+
) -> None:
22412286
"""Appends operations onto the end of the circuit.
22422287
22432288
Moments within the operation tree are appended intact.

0 commit comments

Comments
 (0)