Skip to content

Commit 2a9fff7

Browse files
authored
Copy states in Clifford simulator (#3664)
During the (ongoing) review of #3630 95-martin-orion@ found a bug in my draft code. He said that I should copy state like in #3109. I changed my code and added a test in that PR. However, I had inspired myself from the Clifford code which could have the same issue (I think), and so I am fixing the code in the present PR.
1 parent e6a89fb commit 2a9fff7

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

cirq/sim/clifford/clifford_simulator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def __init__(self, state, measurements):
225225
method).
226226
"""
227227
self.measurements = measurements
228-
self.state = state
228+
self.state = state.copy()
229229

230230
def __str__(self) -> str:
231231
def bitstring(vals):

cirq/sim/clifford/clifford_simulator_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import itertools
12
import numpy as np
23
import pytest
34
import sympy
@@ -537,3 +538,16 @@ def test_reset():
537538
assert cirq.CliffordSimulator().sample(c)["out"][0] == 0
538539
c = cirq.Circuit(cirq.reset(q), cirq.measure(q, key="out"))
539540
assert cirq.CliffordSimulator().sample(c)["out"][0] == 0
541+
542+
543+
def test_state_copy():
544+
sim = cirq.CliffordSimulator()
545+
546+
q = cirq.LineQubit(0)
547+
circuit = cirq.Circuit(cirq.H(q), cirq.H(q))
548+
549+
state_tableaux = []
550+
for step in sim.simulate_moment_steps(circuit):
551+
state_tableaux.append(step.state.tableau)
552+
for x, y in itertools.combinations(state_tableaux, 2):
553+
assert not np.shares_memory(x.rs, y.rs)

0 commit comments

Comments
 (0)