Skip to content

Commit

Permalink
Convert iterator to list to list before looping through moments r ops…
Browse files Browse the repository at this point in the history
… in insert (quantumlib#5820)
  • Loading branch information
vtomole authored and rht committed May 1, 2023
1 parent ca46f04 commit c8d30d6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cirq-core/cirq/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2083,7 +2083,7 @@ def insert(
"""
# limit index to 0..len(self._moments), also deal with indices smaller 0
k = max(min(index if index >= 0 else len(self._moments) + index, len(self._moments)), 0)
for moment_or_op in ops.flatten_to_ops_or_moments(moment_or_operation_tree):
for moment_or_op in list(ops.flatten_to_ops_or_moments(moment_or_operation_tree)):
if isinstance(moment_or_op, Moment):
self._moments.insert(k, moment_or_op)
k += 1
Expand Down
6 changes: 6 additions & 0 deletions cirq-core/cirq/circuits/circuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ def test_append_single():
c.append([cirq.X(a)])
assert c == cirq.Circuit([cirq.Moment([cirq.X(a)])])

c = cirq.Circuit(cirq.H(a))
c.append(c)
assert c == cirq.Circuit(
[cirq.Moment(cirq.H(cirq.NamedQubit('a'))), cirq.Moment(cirq.H(cirq.NamedQubit('a')))]
)


def test_append_control_key():
q0, q1, q2 = cirq.LineQubit.range(3)
Expand Down

0 comments on commit c8d30d6

Please sign in to comment.