-
Notifications
You must be signed in to change notification settings - Fork 990
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Minimal interface - Supports some random operations (depolarize, probabilistic gates) - This is a potential replacement for cirq.CliffordSimulator (which is in a bad state right now, tracking two redundant state objects in a fashion observable to callers)
- Loading branch information
Showing
11 changed files
with
159 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Copyright 2020 The Cirq Developers | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import Dict, List | ||
|
||
import numpy as np | ||
|
||
import cirq | ||
from cirq import circuits, protocols, value | ||
from cirq.sim.clifford.act_on_clifford_tableau_args import \ | ||
ActOnCliffordTableauArgs | ||
from cirq.sim.clifford.clifford_tableau import CliffordTableau | ||
from cirq.work import sampler | ||
|
||
|
||
class StabilizerSampler(sampler.Sampler): | ||
"""An efficient sampler for stabilizer circuits.""" | ||
|
||
def __init__(self, *, seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None): | ||
""" | ||
Args: | ||
seed: The random seed or generator to use when sampling. | ||
""" | ||
self.init = True | ||
self._prng = value.parse_random_state(seed) | ||
|
||
def run_sweep( | ||
self, | ||
program: 'cirq.Circuit', | ||
params: 'cirq.Sweepable', | ||
repetitions: int = 1, | ||
) -> List['cirq.Result']: | ||
results: List[cirq.Result] = [] | ||
for param_resolver in cirq.to_resolvers(params): | ||
resolved_circuit = cirq.resolve_parameters(program, param_resolver) | ||
measurements = self._run( | ||
resolved_circuit, | ||
repetitions=repetitions, | ||
) | ||
results.append( | ||
cirq.Result(params=param_resolver, measurements=measurements)) | ||
return results | ||
|
||
def _run(self, circuit: circuits.Circuit, | ||
repetitions: int) -> Dict[str, np.ndarray]: | ||
|
||
measurements: Dict[str, List[int]] = { | ||
key: [] for key in protocols.measurement_keys(circuit) | ||
} | ||
axes_map = {q: i for i, q in enumerate(circuit.all_qubits())} | ||
|
||
for _ in range(repetitions): | ||
state = ActOnCliffordTableauArgs( | ||
CliffordTableau(num_qubits=len(axes_map)), | ||
axes=(), | ||
prng=self._prng, | ||
log_of_measurement_results={}, | ||
) | ||
for op in circuit.all_operations(): | ||
state.axes = tuple(axes_map[q] for q in op.qubits) | ||
protocols.act_on(op, state) | ||
|
||
for k, v in state.log_of_measurement_results.items(): | ||
measurements[k].append(v) | ||
|
||
return {k: np.array(v) for k, v in measurements.items()} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import cirq | ||
|
||
import numpy as np | ||
|
||
|
||
def test_produces_samples(): | ||
a, b = cirq.LineQubit.range(2) | ||
c = cirq.Circuit( | ||
cirq.H(a), | ||
cirq.CNOT(a, b), | ||
cirq.measure(a, key='a'), | ||
cirq.measure(b, key='b'), | ||
) | ||
|
||
result = cirq.StabilizerSampler().sample(c, repetitions=100) | ||
assert 5 < sum(result['a']) < 95 | ||
assert np.all(result['a'] ^ result['b'] == 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters