Skip to content
This repository was archived by the owner on Mar 29, 2026. It is now read-only.

Commit 9c1d3ed

Browse files
committed
Create moment unchecked
1 parent 9ba3ebd commit 9c1d3ed

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

cirq-core/cirq/circuits/circuit.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,10 +1741,12 @@ def _create_from_earliest(self, contents):
17411741
ckeys[k] = i
17421742
length = max(length, i + 1)
17431743
for i in range(length):
1744+
curr_ops = []
17441745
if i in moments:
1745-
self._moments.append(moments[i].with_operations(opses[i]))
1746-
else:
1747-
self._moments.append(Moment(opses[i]))
1746+
curr_ops.extend(moments[i]._operations)
1747+
if i in opses:
1748+
curr_ops.extend(opses[i])
1749+
self._moments.append(Moment._create_unchecked(tuple(curr_ops)))
17481750

17491751
def __copy__(self) -> 'cirq.Circuit':
17501752
return self.copy()

cirq-core/cirq/circuits/moment.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def __init__(self, *contents: 'cirq.OP_TREE') -> None:
9494

9595
# An internal dictionary to support efficient operation access by qubit.
9696
self._qubit_to_op: Dict['cirq.Qid', 'cirq.Operation'] = {}
97-
for op in self.operations:
97+
for op in self._operations:
9898
for q in op.qubits:
9999
# Check that operations don't overlap.
100100
if q in self._qubit_to_op:
@@ -105,6 +105,14 @@ def __init__(self, *contents: 'cirq.OP_TREE') -> None:
105105
self._measurement_key_objs: Optional[AbstractSet['cirq.MeasurementKey']] = None
106106
self._control_keys: Optional[FrozenSet['cirq.MeasurementKey']] = None
107107

108+
@classmethod
109+
def _create_unchecked(cls, operations: Tuple['cirq.Operation']):
110+
moment = cls()
111+
moment._operations = operations
112+
moment._qubit_to_op = {q: op for op in operations for q in op.qubits}
113+
moment._qubits = frozenset(moment._qubit_to_op.keys())
114+
return moment
115+
108116
@property
109117
def operations(self) -> Tuple['cirq.Operation', ...]:
110118
return self._operations

0 commit comments

Comments
 (0)