|
| 1 | +# Copyright 2020 The Cirq Developers |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from typing import Dict, List |
| 16 | + |
| 17 | +import numpy as np |
| 18 | + |
| 19 | +import cirq |
| 20 | +from cirq import circuits, protocols, value |
| 21 | +from cirq.sim.clifford.act_on_clifford_tableau_args import \ |
| 22 | + ActOnCliffordTableauArgs |
| 23 | +from cirq.sim.clifford.clifford_tableau import CliffordTableau |
| 24 | +from cirq.work import sampler |
| 25 | + |
| 26 | + |
| 27 | +class StabilizerSampler(sampler.Sampler): |
| 28 | + """An efficient sampler for stabilizer circuits.""" |
| 29 | + |
| 30 | + def __init__(self, *, seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None): |
| 31 | + """ |
| 32 | + Args: |
| 33 | + seed: The random seed or generator to use when sampling. |
| 34 | + """ |
| 35 | + self.init = True |
| 36 | + self._prng = value.parse_random_state(seed) |
| 37 | + |
| 38 | + def run_sweep( |
| 39 | + self, |
| 40 | + program: 'cirq.Circuit', |
| 41 | + params: 'cirq.Sweepable', |
| 42 | + repetitions: int = 1, |
| 43 | + ) -> List['cirq.Result']: |
| 44 | + results: List[cirq.Result] = [] |
| 45 | + for param_resolver in cirq.to_resolvers(params): |
| 46 | + resolved_circuit = cirq.resolve_parameters(program, param_resolver) |
| 47 | + measurements = self._run( |
| 48 | + resolved_circuit, |
| 49 | + repetitions=repetitions, |
| 50 | + ) |
| 51 | + results.append( |
| 52 | + cirq.Result(params=param_resolver, measurements=measurements)) |
| 53 | + return results |
| 54 | + |
| 55 | + def _run(self, circuit: circuits.Circuit, |
| 56 | + repetitions: int) -> Dict[str, np.ndarray]: |
| 57 | + |
| 58 | + measurements: Dict[str, List[int]] = { |
| 59 | + key: [] for key in protocols.measurement_keys(circuit) |
| 60 | + } |
| 61 | + axes_map = {q: i for i, q in enumerate(circuit.all_qubits())} |
| 62 | + |
| 63 | + for _ in range(repetitions): |
| 64 | + state = ActOnCliffordTableauArgs( |
| 65 | + CliffordTableau(num_qubits=len(axes_map)), |
| 66 | + axes=(), |
| 67 | + prng=self._prng, |
| 68 | + log_of_measurement_results={}, |
| 69 | + ) |
| 70 | + for op in circuit.all_operations(): |
| 71 | + state.axes = tuple(axes_map[q] for q in op.qubits) |
| 72 | + protocols.act_on(op, state) |
| 73 | + |
| 74 | + for k, v in state.log_of_measurement_results.items(): |
| 75 | + measurements[k].append(v) |
| 76 | + |
| 77 | + return {k: np.array(v) for k, v in measurements.items()} |
0 commit comments