Skip to content

Commit

Permalink
Speed up circuit construction when contents are a list of moments (#5898
Browse files Browse the repository at this point in the history
)

* Speed up circuit construction when contents are a list of moments

* Add explicit cast to fix mypy errors
  • Loading branch information
tanujkhattar authored Sep 29, 2022
1 parent ff81f80 commit 946f247
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions cirq-core/cirq/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1741,11 +1741,15 @@ def __init__(
circuit.
"""
self._moments: List['cirq.Moment'] = []
flattened_contents = tuple(ops.flatten_to_ops_or_moments(contents))
if all(isinstance(c, Moment) for c in flattened_contents):
self._moments[:] = cast(Iterable[Moment], flattened_contents)
return
with _compat.block_overlapping_deprecation('.*'):
if strategy == InsertStrategy.EARLIEST:
self._load_contents_with_earliest_strategy(contents)
self._load_contents_with_earliest_strategy(flattened_contents)
else:
self.append(contents, strategy=strategy)
self.append(flattened_contents, strategy=strategy)

@classmethod
def _from_moments(cls, moments: Iterable['cirq.Moment']) -> 'Circuit':
Expand Down

0 comments on commit 946f247

Please sign in to comment.