From 4166f30c5dfa852237696cd8f7e0e02dc9e307a9 Mon Sep 17 00:00:00 2001 From: vtomole Date: Mon, 8 Aug 2022 16:09:20 -0500 Subject: [PATCH] Convert iterator to list to list before looping through moments r ops in insert --- cirq-core/cirq/circuits/circuit.py | 2 +- cirq-core/cirq/circuits/circuit_test.py | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/cirq-core/cirq/circuits/circuit.py b/cirq-core/cirq/circuits/circuit.py index 2eda2f8eeb4..4682f4ca066 100644 --- a/cirq-core/cirq/circuits/circuit.py +++ b/cirq-core/cirq/circuits/circuit.py @@ -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 diff --git a/cirq-core/cirq/circuits/circuit_test.py b/cirq-core/cirq/circuits/circuit_test.py index 044544a5687..170d78cc0d0 100644 --- a/cirq-core/cirq/circuits/circuit_test.py +++ b/cirq-core/cirq/circuits/circuit_test.py @@ -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)