Skip to content

Commit

Permalink
AbstractCircuit.freeze should not reallocate moments (quantumlib#5878)
Browse files Browse the repository at this point in the history
  • Loading branch information
vtomole authored and rht committed May 1, 2023
1 parent 19bfa58 commit 879f600
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
7 changes: 7 additions & 0 deletions cirq-core/cirq/circuits/circuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4486,6 +4486,13 @@ def test_concat_ragged_alignment():
)


def test_freeze_not_relocate_moments():
q = cirq.q(0)
c = cirq.Circuit(cirq.X(q), cirq.measure(q))
f = c.freeze()
assert [mc is fc for mc, fc in zip(c, f)] == [True, True]


def test_factorize_one_factor():
circuit = cirq.Circuit()
q0, q1, q2 = cirq.LineQubit.range(3)
Expand Down
3 changes: 3 additions & 0 deletions cirq-core/cirq/circuits/moment.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ def with_operations(self, *contents: 'cirq.OP_TREE') -> 'cirq.Moment':
"""
flattened_contents = tuple(op_tree.flatten_to_ops(contents))

if not flattened_contents:
return self

m = Moment()
# Use private variables to facilitate a quick copy.
m._qubit_to_op = self._qubit_to_op.copy()
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/circuits/moment_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ def test_add():

assert m1 + [[[[cirq.Y(b)]]]] == cirq.Moment(cirq.X(a), cirq.Y(b))
assert m1 + [] == m1
assert m1 + [] is m1


def test_sub():
Expand Down Expand Up @@ -703,7 +704,6 @@ def test_kraus():
m = cirq.Moment(cirq.CNOT(a, b))
assert cirq.has_kraus(m)
k = cirq.kraus(m)
print(k[0])
assert len(k) == 1
assert np.allclose(k[0], np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]))

Expand Down

0 comments on commit 879f600

Please sign in to comment.