Skip to content

Commit

Permalink
Use random seed in CliffordSimulator (quantumlib#2845)
Browse files Browse the repository at this point in the history
  • Loading branch information
fedimser authored Mar 27, 2020
1 parent 5a52d97 commit 4d044cf
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
14 changes: 11 additions & 3 deletions cirq/sim/clifford/clifford_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ class CliffordSimulator(simulator.SimulatesSamples,
simulator.SimulatesIntermediateState):
"""An efficient simulator for Clifford circuits."""

def __init__(self):
def __init__(self, seed: value.RANDOM_STATE_LIKE = None):
"""Creates instance of `CliffordSimulator`.
Args:
seed: The random seed to use for this simulator.
"""
self.init = True
self._prng = value.parse_random_state(seed)

@staticmethod
def get_supported_gates() -> List['cirq.Gate']:
Expand Down Expand Up @@ -89,7 +95,7 @@ def _base_iterator(self, circuit: circuits.Circuit,
elif protocols.is_measurement(op):
key = protocols.measurement_key(op)
measurements[key].extend(
state.perform_measurement(op.qubits))
state.perform_measurement(op.qubits, self._prng))

yield CliffordSimulatorStepResult(measurements=measurements,
state=state)
Expand Down Expand Up @@ -234,6 +240,7 @@ def sample(self,
for _ in range(repetitions):
measurements.append(
self.state.perform_measurement(qubits,
value.parse_random_state(seed),
collapse_wavefunction=False))

return np.array(measurements, dtype=bool)
Expand Down Expand Up @@ -321,6 +328,7 @@ def apply_unitary(self, op: 'cirq.Operation'):

def perform_measurement(self,
qubits: Sequence[ops.Qid],
prng: np.random.RandomState,
collapse_wavefunction=True):
results = []

Expand All @@ -330,7 +338,7 @@ def perform_measurement(self,
state = self.copy()

for qubit in qubits:
result = state.tableau._measure(self.qubit_map[qubit])
result = state.tableau._measure(self.qubit_map[qubit], prng)
state.ch_form.project_Z(self.qubit_map[qubit], result)
results.append(result)

Expand Down
10 changes: 10 additions & 0 deletions cirq/sim/clifford/clifford_simulator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,13 @@ def test_non_clifford_circuit():
with pytest.raises(ValueError,
match="T cannot be run with Clifford simulator"):
cirq.CliffordSimulator().simulate(circuit)


def test_sample_seed():
q = cirq.NamedQubit('q')
circuit = cirq.Circuit(cirq.H(q), cirq.measure(q))
simulator = cirq.CliffordSimulator(seed=1234)
result = simulator.run(circuit, repetitions=20)
measured = result.measurements['q']
result_string = ''.join(map(lambda x: str(int(x[0])), measured))
assert result_string == '11010001111100100000'
4 changes: 2 additions & 2 deletions cirq/sim/clifford/clifford_tableau.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def destabilizers(self) -> List[DensePauliString]:
generators above generate the full Pauli group on n qubits."""
return [self._row_to_dense_pauli(i) for i in range(0, self.n)]

def _measure(self, q):
def _measure(self, q, prng: np.random.RandomState):
""" Performs a projective measurement on the q'th qubit.
Returns: the result (0 or 1) of the measurement.
Expand Down Expand Up @@ -239,6 +239,6 @@ def _measure(self, q):

self.zs[p, q] = True

self.rs[p] = bool(np.random.randint(2))
self.rs[p] = bool(prng.randint(2))

return int(self.rs[p])
2 changes: 1 addition & 1 deletion cirq/sim/mux.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def sample(program: 'cirq.Circuit',
if _is_clifford_circuit(program):
# If all non-measurement operations are clifford, use the Clifford
# simulator.
return clifford_simulator.CliffordSimulator().run(
return clifford_simulator.CliffordSimulator(seed=seed).run(
program, param_resolver=param_resolver, repetitions=repetitions)
if protocols.has_unitary(program):
return sparse_simulator.Simulator(dtype=dtype, seed=seed).run(
Expand Down

0 comments on commit 4d044cf

Please sign in to comment.