Skip to content

Commit

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

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

* Add explicit cast to fix mypy errors
  • Loading branch information
tanujkhattar authored and rht committed May 1, 2023
1 parent b35365c commit adb2548
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 adb2548

Please sign in to comment.