Skip to content

Commit dc82123

Browse files
committed
Speed up clifford simulator by short circuiting on known gates
1 parent beaa782 commit dc82123

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

cirq/sim/clifford/clifford_simulator.py

+29-4
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ class CliffordTrialResult(simulator.SimulationTrialResult):
188188
def __init__(self, params: study.ParamResolver,
189189
measurements: Dict[str, np.ndarray],
190190
final_simulator_state: 'CliffordState') -> None:
191-
192191
super().__init__(params=params,
193192
measurements=measurements,
194193
final_simulator_state=final_simulator_state)
@@ -331,7 +330,21 @@ def apply_unitary(self, op: 'cirq.Operation'):
331330

332331
def apply_single_qubit_unitary(self, op: 'cirq.Operation'):
333332
qubit = self.qubit_map[op.qubits[0]]
334-
# Handle H natively as optimization.
333+
if op.gate == cirq.I:
334+
return
335+
336+
if op.gate == cirq.X:
337+
self._apply_X(qubit)
338+
return
339+
340+
if op.gate == cirq.Y:
341+
self._apply_Y(qubit)
342+
return
343+
344+
if op.gate == cirq.Z:
345+
self._apply_Z(qubit)
346+
return
347+
335348
if op.gate == cirq.H:
336349
self._apply_H(qubit)
337350
return
@@ -366,14 +379,26 @@ def apply_single_qubit_unitary(self, op: 'cirq.Operation'):
366379
phase_shift = u[max_idx] / applied_unitary[max_idx]
367380
self.ch_form.omega *= phase_shift
368381

369-
def _apply_H(self, qubit: ops.Qid):
382+
def _apply_H(self, qubit: int):
370383
self.tableau._H(qubit)
371384
self.ch_form._H(qubit)
372385

373-
def _apply_S(self, qubit: ops.Qid):
386+
def _apply_S(self, qubit: int):
374387
self.tableau._S(qubit)
375388
self.ch_form._S(qubit)
376389

390+
def _apply_X(self, qubit: int):
391+
self.tableau._X(qubit)
392+
self.ch_form._X(qubit)
393+
394+
def _apply_Z(self, qubit: int):
395+
self.tableau._Z(qubit)
396+
self.ch_form._Z(qubit)
397+
398+
def _apply_Y(self, qubit: int):
399+
self.tableau._Y(qubit)
400+
self.ch_form._Y(qubit)
401+
377402
def perform_measurement(self,
378403
qubits: Sequence[ops.Qid],
379404
prng: np.random.RandomState,

cirq/sim/clifford/stabilizer_state_ch_form.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ def inner_product_of_state_and_x(self, x: int) -> Union[float, complex]:
9999
if y[p]:
100100
u ^= self.F[p, :]
101101
mu += 2 * (sum(self.M[p, :] & u) % 2)
102-
return self.omega * 2**(-sum(self.v) / 2) * 1j**mu * (
103-
-1)**sum(self.v & u & self.s) * np.all(self.v | (u == self.s))
102+
return (self.omega * 2**(-sum(self.v) / 2) * 1j**mu *
103+
(-1)**sum(self.v & u & self.s) * np.all(self.v | (u == self.s)))
104104

105105
def wave_function(self) -> np.ndarray:
106106
wf = np.zeros(2**self.n, dtype=complex)

0 commit comments

Comments
 (0)