Skip to content

Commit 957f224

Browse files
authored
Speed up circuit construction when contents are a list of moments (quantumlib#5898)
* Speed up circuit construction when contents are a list of moments * Add explicit cast to fix mypy errors
1 parent dd6524e commit 957f224

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

cirq/circuits/circuit.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1741,11 +1741,15 @@ def __init__(
17411741
circuit.
17421742
"""
17431743
self._moments: List['cirq.Moment'] = []
1744+
flattened_contents = tuple(ops.flatten_to_ops_or_moments(contents))
1745+
if all(isinstance(c, Moment) for c in flattened_contents):
1746+
self._moments[:] = cast(Iterable[Moment], flattened_contents)
1747+
return
17441748
with _compat.block_overlapping_deprecation('.*'):
17451749
if strategy == InsertStrategy.EARLIEST:
1746-
self._load_contents_with_earliest_strategy(contents)
1750+
self._load_contents_with_earliest_strategy(flattened_contents)
17471751
else:
1748-
self.append(contents, strategy=strategy)
1752+
self.append(flattened_contents, strategy=strategy)
17491753

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

0 commit comments

Comments
 (0)