Skip to content

Commit

Permalink
Allow passing None to AbstractCircuit.from_moments
Browse files Browse the repository at this point in the history
  • Loading branch information
maffoo committed Jun 7, 2024
1 parent 543d9cd commit d4a6c0b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
24 changes: 13 additions & 11 deletions cirq-core/cirq/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,29 +145,31 @@ class AbstractCircuit(abc.ABC):
"""

@classmethod
def from_moments(cls: Type[CIRCUIT_TYPE], *moments: 'cirq.OP_TREE') -> CIRCUIT_TYPE:
def from_moments(cls: Type[CIRCUIT_TYPE], *moments: Optional['cirq.OP_TREE']) -> CIRCUIT_TYPE:
"""Create a circuit from moment op trees.
Args:
*moments: Op tree for each moment. If an op tree is a moment, it
will be included directly in the new circuit. If an op tree is
a circuit, it will be frozen, wrapped in a CircuitOperation, and
included in its own moment in the new circuit. Otherwise, the
op tree will be passed to `cirq.Moment` to create a new moment
which is then included in the new circuit. Note that in the
latter case we have the normal restriction that operations in a
moment must be applied to disjoint sets of qubits.
*moments: Op trees for each moment, which can be one of the following:
- Moment: will be included directly in the new circuit.
- AbstractCircuit: will be frozen, wrapped in a CircuitOperation,
and included in its own moment in the new circuit.
- None: will be skipped and omitted from the circuit. This can be
used to include or skip a moment based on a conditional, for example.
- Other OP_TREE: will be passed to `cirq.Moment` to create a new moment
which is then included in the new circuit. Note that in this
case we have the normal restriction that operations in a
moment must be applied to disjoint sets of qubits.
"""
return cls._from_moments(cls._make_moments(moments))

@staticmethod
def _make_moments(moments: Iterable['cirq.OP_TREE']) -> Iterator['cirq.Moment']:
def _make_moments(moments: Iterable[Optional['cirq.OP_TREE']]) -> Iterator['cirq.Moment']:
for m in moments:
if isinstance(m, Moment):
yield m
elif isinstance(m, AbstractCircuit):
yield Moment(m.freeze().to_op())
else:
elif m is not None:
yield Moment(m)

@classmethod
Expand Down
1 change: 1 addition & 0 deletions cirq-core/cirq/circuits/circuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def test_from_moments():
[cirq.X(c)],
[],
cirq.Z(d),
None,
[cirq.measure(a, b, key='ab'), cirq.measure(c, d, key='cd')],
)
assert circuit == cirq.Circuit(
Expand Down

0 comments on commit d4a6c0b

Please sign in to comment.