cirq-core 1.6.0.dev20250428201230__py3-none-any.whl → 1.6.0.dev20250429161116__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of cirq-core might be problematic. Click here for more details.
- cirq/_version.py +1 -1
- cirq/_version_test.py +1 -1
- cirq/protocols/measurement_key_protocol.py +8 -7
- cirq/protocols/pow_protocol.py +7 -7
- cirq/protocols/qasm.py +5 -3
- cirq/protocols/resolve_parameters.py +5 -3
- cirq/qis/clifford_tableau.py +11 -9
- cirq/qis/measures.py +7 -7
- cirq/qis/quantum_state_representation.py +4 -5
- cirq/qis/states.py +19 -17
- cirq/sim/classical_simulator.py +15 -14
- cirq/sim/clifford/clifford_simulator.py +19 -17
- cirq/sim/clifford/clifford_tableau_simulation_state.py +7 -4
- cirq/sim/clifford/stabilizer_ch_form_simulation_state.py +5 -3
- cirq/sim/clifford/stabilizer_sampler.py +6 -4
- cirq/sim/clifford/stabilizer_simulation_state.py +9 -9
- cirq/sim/clifford/stabilizer_state_ch_form.py +6 -4
- cirq/sim/density_matrix_simulation_state.py +17 -18
- cirq/sim/density_matrix_simulator.py +21 -19
- cirq/sim/density_matrix_utils.py +4 -2
- cirq/sim/mux.py +25 -23
- cirq/sim/simulation_product_state.py +12 -12
- cirq/sim/simulation_product_state_test.py +3 -3
- cirq/sim/simulation_state.py +23 -19
- cirq/sim/simulation_state_base.py +16 -12
- cirq/sim/simulation_state_test.py +1 -1
- cirq/sim/simulator.py +71 -72
- cirq/sim/simulator_base.py +22 -23
- cirq/sim/simulator_base_test.py +12 -9
- cirq/sim/simulator_test.py +12 -8
- cirq/sim/sparse_simulator.py +13 -11
- cirq/sim/state_vector.py +9 -6
- cirq/sim/state_vector_simulation_state.py +20 -20
- cirq/sim/state_vector_simulator.py +13 -10
- cirq/study/flatten_expressions.py +8 -5
- cirq/study/resolver.py +12 -9
- cirq/study/result.py +6 -3
- cirq/study/sweeps.py +17 -14
- cirq/testing/consistent_act_on_test.py +2 -2
- cirq/testing/consistent_controlled_gate_op_test.py +10 -6
- cirq/testing/lin_alg_utils.py +8 -7
- cirq/testing/random_circuit.py +17 -16
- cirq/testing/routing_devices.py +4 -1
- cirq/testing/sample_circuits.py +4 -1
- cirq/transformers/align.py +6 -4
- cirq/transformers/analytical_decompositions/clifford_decomposition.py +9 -7
- cirq/transformers/analytical_decompositions/controlled_gate_decomposition.py +15 -13
- cirq/transformers/analytical_decompositions/cphase_to_fsim.py +7 -5
- cirq/transformers/analytical_decompositions/quantum_shannon_decomposition.py +10 -8
- cirq/transformers/analytical_decompositions/single_to_two_qubit_isometry.py +6 -3
- cirq/transformers/analytical_decompositions/two_qubit_state_preparation.py +8 -10
- {cirq_core-1.6.0.dev20250428201230.dist-info → cirq_core-1.6.0.dev20250429161116.dist-info}/METADATA +1 -1
- {cirq_core-1.6.0.dev20250428201230.dist-info → cirq_core-1.6.0.dev20250429161116.dist-info}/RECORD +56 -56
- {cirq_core-1.6.0.dev20250428201230.dist-info → cirq_core-1.6.0.dev20250429161116.dist-info}/WHEEL +0 -0
- {cirq_core-1.6.0.dev20250428201230.dist-info → cirq_core-1.6.0.dev20250429161116.dist-info}/licenses/LICENSE +0 -0
- {cirq_core-1.6.0.dev20250428201230.dist-info → cirq_core-1.6.0.dev20250429161116.dist-info}/top_level.txt +0 -0
cirq/sim/simulator.py
CHANGED
|
@@ -27,6 +27,8 @@ Simulator types include:
|
|
|
27
27
|
as the simulation iterates through the moments of a cirq.
|
|
28
28
|
"""
|
|
29
29
|
|
|
30
|
+
from __future__ import annotations
|
|
31
|
+
|
|
30
32
|
import abc
|
|
31
33
|
import collections
|
|
32
34
|
from typing import (
|
|
@@ -67,13 +69,13 @@ class SimulatesSamples(work.Sampler, metaclass=abc.ABCMeta):
|
|
|
67
69
|
"""
|
|
68
70
|
|
|
69
71
|
def run_sweep(
|
|
70
|
-
self, program:
|
|
71
|
-
) -> Sequence[
|
|
72
|
+
self, program: cirq.AbstractCircuit, params: cirq.Sweepable, repetitions: int = 1
|
|
73
|
+
) -> Sequence[cirq.Result]:
|
|
72
74
|
return list(self.run_sweep_iter(program, params, repetitions))
|
|
73
75
|
|
|
74
76
|
def run_sweep_iter(
|
|
75
|
-
self, program:
|
|
76
|
-
) -> Iterator[
|
|
77
|
+
self, program: cirq.AbstractCircuit, params: cirq.Sweepable, repetitions: int = 1
|
|
78
|
+
) -> Iterator[cirq.Result]:
|
|
77
79
|
"""Runs the supplied Circuit, mimicking quantum hardware.
|
|
78
80
|
|
|
79
81
|
In contrast to run, this allows for sweeping over different parameter
|
|
@@ -107,10 +109,7 @@ class SimulatesSamples(work.Sampler, metaclass=abc.ABCMeta):
|
|
|
107
109
|
|
|
108
110
|
@abc.abstractmethod
|
|
109
111
|
def _run(
|
|
110
|
-
self,
|
|
111
|
-
circuit: 'cirq.AbstractCircuit',
|
|
112
|
-
param_resolver: 'cirq.ParamResolver',
|
|
113
|
-
repetitions: int,
|
|
112
|
+
self, circuit: cirq.AbstractCircuit, param_resolver: cirq.ParamResolver, repetitions: int
|
|
114
113
|
) -> Dict[str, np.ndarray]:
|
|
115
114
|
"""Run a simulation, mimicking quantum hardware.
|
|
116
115
|
|
|
@@ -142,10 +141,10 @@ class SimulatesAmplitudes(metaclass=value.ABCMetaImplementAnyOneOf):
|
|
|
142
141
|
|
|
143
142
|
def compute_amplitudes(
|
|
144
143
|
self,
|
|
145
|
-
program:
|
|
144
|
+
program: cirq.AbstractCircuit,
|
|
146
145
|
bitstrings: Sequence[int],
|
|
147
|
-
param_resolver:
|
|
148
|
-
qubit_order:
|
|
146
|
+
param_resolver: cirq.ParamResolverOrSimilarType = None,
|
|
147
|
+
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
149
148
|
) -> Sequence[complex]:
|
|
150
149
|
"""Computes the desired amplitudes.
|
|
151
150
|
|
|
@@ -173,10 +172,10 @@ class SimulatesAmplitudes(metaclass=value.ABCMetaImplementAnyOneOf):
|
|
|
173
172
|
|
|
174
173
|
def compute_amplitudes_sweep(
|
|
175
174
|
self,
|
|
176
|
-
program:
|
|
175
|
+
program: cirq.AbstractCircuit,
|
|
177
176
|
bitstrings: Sequence[int],
|
|
178
|
-
params:
|
|
179
|
-
qubit_order:
|
|
177
|
+
params: cirq.Sweepable,
|
|
178
|
+
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
180
179
|
) -> Sequence[Sequence[complex]]:
|
|
181
180
|
"""Wraps computed amplitudes in a list.
|
|
182
181
|
|
|
@@ -186,10 +185,10 @@ class SimulatesAmplitudes(metaclass=value.ABCMetaImplementAnyOneOf):
|
|
|
186
185
|
|
|
187
186
|
def _compute_amplitudes_sweep_to_iter(
|
|
188
187
|
self,
|
|
189
|
-
program:
|
|
188
|
+
program: cirq.AbstractCircuit,
|
|
190
189
|
bitstrings: Sequence[int],
|
|
191
|
-
params:
|
|
192
|
-
qubit_order:
|
|
190
|
+
params: cirq.Sweepable,
|
|
191
|
+
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
193
192
|
) -> Iterator[Sequence[complex]]:
|
|
194
193
|
if type(self).compute_amplitudes_sweep == SimulatesAmplitudes.compute_amplitudes_sweep:
|
|
195
194
|
raise RecursionError(
|
|
@@ -202,10 +201,10 @@ class SimulatesAmplitudes(metaclass=value.ABCMetaImplementAnyOneOf):
|
|
|
202
201
|
)
|
|
203
202
|
def compute_amplitudes_sweep_iter(
|
|
204
203
|
self,
|
|
205
|
-
program:
|
|
204
|
+
program: cirq.AbstractCircuit,
|
|
206
205
|
bitstrings: Sequence[int],
|
|
207
|
-
params:
|
|
208
|
-
qubit_order:
|
|
206
|
+
params: cirq.Sweepable,
|
|
207
|
+
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
209
208
|
) -> Iterator[Sequence[complex]]:
|
|
210
209
|
"""Computes the desired amplitudes.
|
|
211
210
|
|
|
@@ -232,11 +231,11 @@ class SimulatesAmplitudes(metaclass=value.ABCMetaImplementAnyOneOf):
|
|
|
232
231
|
|
|
233
232
|
def sample_from_amplitudes(
|
|
234
233
|
self,
|
|
235
|
-
circuit:
|
|
236
|
-
param_resolver:
|
|
237
|
-
seed:
|
|
234
|
+
circuit: cirq.AbstractCircuit,
|
|
235
|
+
param_resolver: cirq.ParamResolver,
|
|
236
|
+
seed: cirq.RANDOM_STATE_OR_SEED_LIKE,
|
|
238
237
|
repetitions: int = 1,
|
|
239
|
-
qubit_order:
|
|
238
|
+
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
240
239
|
) -> Dict[int, int]:
|
|
241
240
|
"""Uses amplitude simulation to sample from the given circuit.
|
|
242
241
|
|
|
@@ -315,10 +314,10 @@ class SimulatesExpectationValues(metaclass=value.ABCMetaImplementAnyOneOf):
|
|
|
315
314
|
|
|
316
315
|
def simulate_expectation_values(
|
|
317
316
|
self,
|
|
318
|
-
program:
|
|
319
|
-
observables: Union[
|
|
320
|
-
param_resolver:
|
|
321
|
-
qubit_order:
|
|
317
|
+
program: cirq.AbstractCircuit,
|
|
318
|
+
observables: Union[cirq.PauliSumLike, List[cirq.PauliSumLike]],
|
|
319
|
+
param_resolver: cirq.ParamResolverOrSimilarType = None,
|
|
320
|
+
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
322
321
|
initial_state: Any = None,
|
|
323
322
|
permit_terminal_measurements: bool = False,
|
|
324
323
|
) -> List[float]:
|
|
@@ -363,10 +362,10 @@ class SimulatesExpectationValues(metaclass=value.ABCMetaImplementAnyOneOf):
|
|
|
363
362
|
|
|
364
363
|
def simulate_expectation_values_sweep(
|
|
365
364
|
self,
|
|
366
|
-
program:
|
|
367
|
-
observables: Union[
|
|
368
|
-
params:
|
|
369
|
-
qubit_order:
|
|
365
|
+
program: cirq.AbstractCircuit,
|
|
366
|
+
observables: Union[cirq.PauliSumLike, List[cirq.PauliSumLike]],
|
|
367
|
+
params: cirq.Sweepable,
|
|
368
|
+
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
370
369
|
initial_state: Any = None,
|
|
371
370
|
permit_terminal_measurements: bool = False,
|
|
372
371
|
) -> List[List[float]]:
|
|
@@ -387,10 +386,10 @@ class SimulatesExpectationValues(metaclass=value.ABCMetaImplementAnyOneOf):
|
|
|
387
386
|
|
|
388
387
|
def _simulate_expectation_values_sweep_to_iter(
|
|
389
388
|
self,
|
|
390
|
-
program:
|
|
391
|
-
observables: Union[
|
|
392
|
-
params:
|
|
393
|
-
qubit_order:
|
|
389
|
+
program: cirq.AbstractCircuit,
|
|
390
|
+
observables: Union[cirq.PauliSumLike, List[cirq.PauliSumLike]],
|
|
391
|
+
params: cirq.Sweepable,
|
|
392
|
+
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
394
393
|
initial_state: Any = None,
|
|
395
394
|
permit_terminal_measurements: bool = False,
|
|
396
395
|
) -> Iterator[List[float]]:
|
|
@@ -412,10 +411,10 @@ class SimulatesExpectationValues(metaclass=value.ABCMetaImplementAnyOneOf):
|
|
|
412
411
|
)
|
|
413
412
|
def simulate_expectation_values_sweep_iter(
|
|
414
413
|
self,
|
|
415
|
-
program:
|
|
416
|
-
observables: Union[
|
|
417
|
-
params:
|
|
418
|
-
qubit_order:
|
|
414
|
+
program: cirq.AbstractCircuit,
|
|
415
|
+
observables: Union[cirq.PauliSumLike, List[cirq.PauliSumLike]],
|
|
416
|
+
params: cirq.Sweepable,
|
|
417
|
+
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
419
418
|
initial_state: Any = None,
|
|
420
419
|
permit_terminal_measurements: bool = False,
|
|
421
420
|
) -> Iterator[List[float]]:
|
|
@@ -470,9 +469,9 @@ class SimulatesFinalState(
|
|
|
470
469
|
|
|
471
470
|
def simulate(
|
|
472
471
|
self,
|
|
473
|
-
program:
|
|
474
|
-
param_resolver:
|
|
475
|
-
qubit_order:
|
|
472
|
+
program: cirq.AbstractCircuit,
|
|
473
|
+
param_resolver: cirq.ParamResolverOrSimilarType = None,
|
|
474
|
+
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
476
475
|
initial_state: Any = None,
|
|
477
476
|
) -> TSimulationTrialResult:
|
|
478
477
|
"""Simulates the supplied Circuit.
|
|
@@ -499,9 +498,9 @@ class SimulatesFinalState(
|
|
|
499
498
|
|
|
500
499
|
def simulate_sweep(
|
|
501
500
|
self,
|
|
502
|
-
program:
|
|
503
|
-
params:
|
|
504
|
-
qubit_order:
|
|
501
|
+
program: cirq.AbstractCircuit,
|
|
502
|
+
params: cirq.Sweepable,
|
|
503
|
+
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
505
504
|
initial_state: Any = None,
|
|
506
505
|
) -> List[TSimulationTrialResult]:
|
|
507
506
|
"""Wraps computed states in a list.
|
|
@@ -512,9 +511,9 @@ class SimulatesFinalState(
|
|
|
512
511
|
|
|
513
512
|
def _simulate_sweep_to_iter(
|
|
514
513
|
self,
|
|
515
|
-
program:
|
|
516
|
-
params:
|
|
517
|
-
qubit_order:
|
|
514
|
+
program: cirq.AbstractCircuit,
|
|
515
|
+
params: cirq.Sweepable,
|
|
516
|
+
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
518
517
|
initial_state: Any = None,
|
|
519
518
|
) -> Iterator[TSimulationTrialResult]:
|
|
520
519
|
if type(self).simulate_sweep == SimulatesFinalState.simulate_sweep:
|
|
@@ -524,9 +523,9 @@ class SimulatesFinalState(
|
|
|
524
523
|
@value.alternative(requires='simulate_sweep', implementation=_simulate_sweep_to_iter)
|
|
525
524
|
def simulate_sweep_iter(
|
|
526
525
|
self,
|
|
527
|
-
program:
|
|
528
|
-
params:
|
|
529
|
-
qubit_order:
|
|
526
|
+
program: cirq.AbstractCircuit,
|
|
527
|
+
params: cirq.Sweepable,
|
|
528
|
+
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
530
529
|
initial_state: Any = None,
|
|
531
530
|
) -> Iterator[TSimulationTrialResult]:
|
|
532
531
|
"""Simulates the supplied Circuit.
|
|
@@ -572,9 +571,9 @@ class SimulatesIntermediateState(
|
|
|
572
571
|
|
|
573
572
|
def simulate_sweep_iter(
|
|
574
573
|
self,
|
|
575
|
-
program:
|
|
576
|
-
params:
|
|
577
|
-
qubit_order:
|
|
574
|
+
program: cirq.AbstractCircuit,
|
|
575
|
+
params: cirq.Sweepable,
|
|
576
|
+
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
578
577
|
initial_state: Any = None,
|
|
579
578
|
) -> Iterator[TSimulationTrialResult]:
|
|
580
579
|
"""Simulates the supplied Circuit.
|
|
@@ -621,9 +620,9 @@ class SimulatesIntermediateState(
|
|
|
621
620
|
|
|
622
621
|
def simulate_moment_steps(
|
|
623
622
|
self,
|
|
624
|
-
circuit:
|
|
625
|
-
param_resolver:
|
|
626
|
-
qubit_order:
|
|
623
|
+
circuit: cirq.AbstractCircuit,
|
|
624
|
+
param_resolver: cirq.ParamResolverOrSimilarType = None,
|
|
625
|
+
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
627
626
|
initial_state: Any = None,
|
|
628
627
|
) -> Iterator[TStepResult]:
|
|
629
628
|
"""Returns an iterator of StepResults for each moment simulated.
|
|
@@ -655,7 +654,7 @@ class SimulatesIntermediateState(
|
|
|
655
654
|
|
|
656
655
|
@abc.abstractmethod
|
|
657
656
|
def _base_iterator(
|
|
658
|
-
self, circuit:
|
|
657
|
+
self, circuit: cirq.AbstractCircuit, qubits: Tuple[cirq.Qid, ...], initial_state: Any
|
|
659
658
|
) -> Iterator[TStepResult]:
|
|
660
659
|
"""Iterator over StepResult from Moments of a Circuit.
|
|
661
660
|
|
|
@@ -675,7 +674,7 @@ class SimulatesIntermediateState(
|
|
|
675
674
|
@abc.abstractmethod
|
|
676
675
|
def _create_simulator_trial_result(
|
|
677
676
|
self,
|
|
678
|
-
params:
|
|
677
|
+
params: cirq.ParamResolver,
|
|
679
678
|
measurements: Dict[str, np.ndarray],
|
|
680
679
|
final_simulator_state: TSimulatorState,
|
|
681
680
|
) -> TSimulationTrialResult:
|
|
@@ -723,9 +722,9 @@ class StepResult(Generic[TSimulatorState], metaclass=abc.ABCMeta):
|
|
|
723
722
|
@abc.abstractmethod
|
|
724
723
|
def sample(
|
|
725
724
|
self,
|
|
726
|
-
qubits: List[
|
|
725
|
+
qubits: List[cirq.Qid],
|
|
727
726
|
repetitions: int = 1,
|
|
728
|
-
seed:
|
|
727
|
+
seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None,
|
|
729
728
|
) -> np.ndarray:
|
|
730
729
|
"""Samples from the system at this point in the computation.
|
|
731
730
|
|
|
@@ -747,9 +746,9 @@ class StepResult(Generic[TSimulatorState], metaclass=abc.ABCMeta):
|
|
|
747
746
|
|
|
748
747
|
def sample_measurement_ops(
|
|
749
748
|
self,
|
|
750
|
-
measurement_ops: List[
|
|
749
|
+
measurement_ops: List[cirq.GateOperation],
|
|
751
750
|
repetitions: int = 1,
|
|
752
|
-
seed:
|
|
751
|
+
seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None,
|
|
753
752
|
*,
|
|
754
753
|
_allow_repeated=False,
|
|
755
754
|
) -> Dict[str, np.ndarray]:
|
|
@@ -837,9 +836,9 @@ class StepResult(Generic[TSimulatorState], metaclass=abc.ABCMeta):
|
|
|
837
836
|
def _confuse_results(
|
|
838
837
|
self,
|
|
839
838
|
bits: np.ndarray,
|
|
840
|
-
qubits: Sequence[
|
|
839
|
+
qubits: Sequence[cirq.Qid],
|
|
841
840
|
confusion_map: Dict[Tuple[int, ...], np.ndarray],
|
|
842
|
-
seed:
|
|
841
|
+
seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None,
|
|
843
842
|
) -> None:
|
|
844
843
|
"""Mutates `bits` using the confusion_map.
|
|
845
844
|
|
|
@@ -876,7 +875,7 @@ class SimulationTrialResult(Generic[TSimulatorState]):
|
|
|
876
875
|
|
|
877
876
|
def __init__(
|
|
878
877
|
self,
|
|
879
|
-
params:
|
|
878
|
+
params: cirq.ParamResolver,
|
|
880
879
|
measurements: Mapping[str, np.ndarray],
|
|
881
880
|
final_simulator_state: TSimulatorState,
|
|
882
881
|
) -> None:
|
|
@@ -895,7 +894,7 @@ class SimulationTrialResult(Generic[TSimulatorState]):
|
|
|
895
894
|
self._final_simulator_state = final_simulator_state
|
|
896
895
|
|
|
897
896
|
@property
|
|
898
|
-
def params(self) ->
|
|
897
|
+
def params(self) -> cirq.ParamResolver:
|
|
899
898
|
return self._params
|
|
900
899
|
|
|
901
900
|
@property
|
|
@@ -932,7 +931,7 @@ class SimulationTrialResult(Generic[TSimulatorState]):
|
|
|
932
931
|
return self.params, measurements, self._final_simulator_state
|
|
933
932
|
|
|
934
933
|
@property
|
|
935
|
-
def qubit_map(self) -> Mapping[
|
|
934
|
+
def qubit_map(self) -> Mapping[cirq.Qid, int]:
|
|
936
935
|
"""A map from Qid to index used to define the ordering of the basis in
|
|
937
936
|
the result.
|
|
938
937
|
"""
|
|
@@ -942,7 +941,7 @@ class SimulationTrialResult(Generic[TSimulatorState]):
|
|
|
942
941
|
return _qubit_map_to_shape(self.qubit_map)
|
|
943
942
|
|
|
944
943
|
|
|
945
|
-
def _qubit_map_to_shape(qubit_map: Mapping[
|
|
944
|
+
def _qubit_map_to_shape(qubit_map: Mapping[cirq.Qid, int]) -> Tuple[int, ...]:
|
|
946
945
|
qid_shape: List[int] = [-1] * len(qubit_map)
|
|
947
946
|
try:
|
|
948
947
|
for q, i in qubit_map.items():
|
|
@@ -965,8 +964,8 @@ def check_all_resolved(circuit):
|
|
|
965
964
|
|
|
966
965
|
|
|
967
966
|
def split_into_matching_protocol_then_general(
|
|
968
|
-
circuit:
|
|
969
|
-
) -> Tuple[
|
|
967
|
+
circuit: cirq.AbstractCircuit, predicate: Callable[[cirq.Operation], bool]
|
|
968
|
+
) -> Tuple[cirq.AbstractCircuit, cirq.AbstractCircuit]:
|
|
970
969
|
"""Splits the circuit into a matching prefix and non-matching suffix.
|
|
971
970
|
|
|
972
971
|
The splitting happens in a per-qubit fashion. A non-matching operation on
|
cirq/sim/simulator_base.py
CHANGED
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
|
|
15
15
|
"""Batteries-included class for Cirq's built-in simulators."""
|
|
16
16
|
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
17
19
|
import abc
|
|
18
20
|
import collections
|
|
19
21
|
from typing import (
|
|
@@ -92,8 +94,8 @@ class SimulatorBase(
|
|
|
92
94
|
self,
|
|
93
95
|
*,
|
|
94
96
|
dtype: Type[np.complexfloating] = np.complex64,
|
|
95
|
-
noise:
|
|
96
|
-
seed:
|
|
97
|
+
noise: cirq.NOISE_MODEL_LIKE = None,
|
|
98
|
+
seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None,
|
|
97
99
|
split_untangled_states: bool = False,
|
|
98
100
|
):
|
|
99
101
|
"""Initializes the simulator.
|
|
@@ -112,15 +114,15 @@ class SimulatorBase(
|
|
|
112
114
|
self._split_untangled_states = split_untangled_states
|
|
113
115
|
|
|
114
116
|
@property
|
|
115
|
-
def noise(self) ->
|
|
117
|
+
def noise(self) -> cirq.NoiseModel:
|
|
116
118
|
return self._noise
|
|
117
119
|
|
|
118
120
|
@abc.abstractmethod
|
|
119
121
|
def _create_partial_simulation_state(
|
|
120
122
|
self,
|
|
121
123
|
initial_state: Any,
|
|
122
|
-
qubits: Sequence[
|
|
123
|
-
classical_data:
|
|
124
|
+
qubits: Sequence[cirq.Qid],
|
|
125
|
+
classical_data: cirq.ClassicalDataStore,
|
|
124
126
|
) -> TSimulationState:
|
|
125
127
|
"""Creates an instance of the TSimulationState class for the simulator.
|
|
126
128
|
|
|
@@ -171,14 +173,14 @@ class SimulatorBase(
|
|
|
171
173
|
return protocols.has_unitary(val)
|
|
172
174
|
|
|
173
175
|
def _base_iterator(
|
|
174
|
-
self, circuit:
|
|
176
|
+
self, circuit: cirq.AbstractCircuit, qubits: Tuple[cirq.Qid, ...], initial_state: Any
|
|
175
177
|
) -> Iterator[TStepResultBase]:
|
|
176
178
|
sim_state = self._create_simulation_state(initial_state, qubits)
|
|
177
179
|
return self._core_iterator(circuit, sim_state)
|
|
178
180
|
|
|
179
181
|
def _core_iterator(
|
|
180
182
|
self,
|
|
181
|
-
circuit:
|
|
183
|
+
circuit: cirq.AbstractCircuit,
|
|
182
184
|
sim_state: SimulationStateBase[TSimulationState],
|
|
183
185
|
all_measurements_are_terminal: bool = False,
|
|
184
186
|
) -> Iterator[TStepResultBase]:
|
|
@@ -204,7 +206,7 @@ class SimulatorBase(
|
|
|
204
206
|
return
|
|
205
207
|
|
|
206
208
|
noisy_moments = self.noise.noisy_moments(circuit, sorted(circuit.all_qubits()))
|
|
207
|
-
measured: Dict[Tuple[
|
|
209
|
+
measured: Dict[Tuple[cirq.Qid, ...], bool] = collections.defaultdict(bool)
|
|
208
210
|
for moment in noisy_moments:
|
|
209
211
|
for op in ops.flatten_to_ops(moment):
|
|
210
212
|
try:
|
|
@@ -224,10 +226,7 @@ class SimulatorBase(
|
|
|
224
226
|
yield self._create_step_result(sim_state)
|
|
225
227
|
|
|
226
228
|
def _run(
|
|
227
|
-
self,
|
|
228
|
-
circuit: 'cirq.AbstractCircuit',
|
|
229
|
-
param_resolver: 'cirq.ParamResolver',
|
|
230
|
-
repetitions: int,
|
|
229
|
+
self, circuit: cirq.AbstractCircuit, param_resolver: cirq.ParamResolver, repetitions: int
|
|
231
230
|
) -> Dict[str, np.ndarray]:
|
|
232
231
|
"""See definition in `cirq.SimulatesSamples`."""
|
|
233
232
|
param_resolver = param_resolver or study.ParamResolver({})
|
|
@@ -257,7 +256,7 @@ class SimulatorBase(
|
|
|
257
256
|
measurement_ops, repetitions, seed=self._prng, _allow_repeated=True
|
|
258
257
|
)
|
|
259
258
|
|
|
260
|
-
records: Dict[
|
|
259
|
+
records: Dict[cirq.MeasurementKey, List[Sequence[Sequence[int]]]] = {}
|
|
261
260
|
for i in range(repetitions):
|
|
262
261
|
for step_result in self._core_iterator(
|
|
263
262
|
general_suffix,
|
|
@@ -286,9 +285,9 @@ class SimulatorBase(
|
|
|
286
285
|
|
|
287
286
|
def simulate_sweep_iter(
|
|
288
287
|
self,
|
|
289
|
-
program:
|
|
290
|
-
params:
|
|
291
|
-
qubit_order:
|
|
288
|
+
program: cirq.AbstractCircuit,
|
|
289
|
+
params: cirq.Sweepable,
|
|
290
|
+
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
292
291
|
initial_state: Any = None,
|
|
293
292
|
) -> Iterator[TSimulationTrialResult]:
|
|
294
293
|
"""Simulates the supplied Circuit.
|
|
@@ -313,7 +312,7 @@ class SimulatorBase(
|
|
|
313
312
|
possible parameter resolver.
|
|
314
313
|
"""
|
|
315
314
|
|
|
316
|
-
def sweep_prefixable(op:
|
|
315
|
+
def sweep_prefixable(op: cirq.Operation):
|
|
317
316
|
return self._can_be_in_run_prefix(op) and not protocols.is_parameterized(op)
|
|
318
317
|
|
|
319
318
|
qubits = ops.QubitOrder.as_qubit_order(qubit_order).order_for(program.all_qubits())
|
|
@@ -331,14 +330,14 @@ class SimulatorBase(
|
|
|
331
330
|
yield from super().simulate_sweep_iter(suffix, params, qubit_order, sim_state)
|
|
332
331
|
|
|
333
332
|
def _create_simulation_state(
|
|
334
|
-
self, initial_state: Any, qubits: Sequence[
|
|
333
|
+
self, initial_state: Any, qubits: Sequence[cirq.Qid]
|
|
335
334
|
) -> SimulationStateBase[TSimulationState]:
|
|
336
335
|
if isinstance(initial_state, SimulationStateBase):
|
|
337
336
|
return initial_state
|
|
338
337
|
|
|
339
338
|
classical_data = value.ClassicalDataDictionaryStore()
|
|
340
339
|
if self._split_untangled_states:
|
|
341
|
-
args_map: Dict[Optional[
|
|
340
|
+
args_map: Dict[Optional[cirq.Qid], TSimulationState] = {}
|
|
342
341
|
if isinstance(initial_state, int):
|
|
343
342
|
for q in reversed(qubits):
|
|
344
343
|
args_map[q] = self._create_partial_simulation_state(
|
|
@@ -393,9 +392,9 @@ class StepResultBase(
|
|
|
393
392
|
|
|
394
393
|
def sample(
|
|
395
394
|
self,
|
|
396
|
-
qubits: List[
|
|
395
|
+
qubits: List[cirq.Qid],
|
|
397
396
|
repetitions: int = 1,
|
|
398
|
-
seed:
|
|
397
|
+
seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None,
|
|
399
398
|
) -> np.ndarray:
|
|
400
399
|
return self._sim_state.sample(qubits, repetitions, seed)
|
|
401
400
|
|
|
@@ -409,7 +408,7 @@ class SimulationTrialResultBase(
|
|
|
409
408
|
self,
|
|
410
409
|
params: study.ParamResolver,
|
|
411
410
|
measurements: Dict[str, np.ndarray],
|
|
412
|
-
final_simulator_state:
|
|
411
|
+
final_simulator_state: cirq.SimulationStateBase[TSimulationState],
|
|
413
412
|
) -> None:
|
|
414
413
|
"""Initializes the `SimulationTrialResultBase` class.
|
|
415
414
|
|
|
@@ -425,7 +424,7 @@ class SimulationTrialResultBase(
|
|
|
425
424
|
super().__init__(params, measurements, final_simulator_state=final_simulator_state)
|
|
426
425
|
self._merged_sim_state_cache: Optional[TSimulationState] = None
|
|
427
426
|
|
|
428
|
-
def get_state_containing_qubit(self, qubit:
|
|
427
|
+
def get_state_containing_qubit(self, qubit: cirq.Qid) -> TSimulationState:
|
|
429
428
|
"""Returns the independent state space containing the qubit.
|
|
430
429
|
|
|
431
430
|
Args:
|
cirq/sim/simulator_base_test.py
CHANGED
|
@@ -11,6 +11,9 @@
|
|
|
11
11
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
14
17
|
import math
|
|
15
18
|
from typing import Any, Dict, List, Sequence, Tuple
|
|
16
19
|
|
|
@@ -29,12 +32,12 @@ class CountingState(cirq.qis.QuantumStateRepresentation):
|
|
|
29
32
|
self.copy_count = copy_count
|
|
30
33
|
|
|
31
34
|
def measure(
|
|
32
|
-
self, axes: Sequence[int], seed:
|
|
35
|
+
self, axes: Sequence[int], seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None
|
|
33
36
|
) -> List[int]:
|
|
34
37
|
self.measurement_count += 1
|
|
35
38
|
return [self.gate_count]
|
|
36
39
|
|
|
37
|
-
def kron(self, other:
|
|
40
|
+
def kron(self, other: CountingState) -> CountingState:
|
|
38
41
|
return CountingState(
|
|
39
42
|
self.data,
|
|
40
43
|
self.gate_count + other.gate_count,
|
|
@@ -44,15 +47,15 @@ class CountingState(cirq.qis.QuantumStateRepresentation):
|
|
|
44
47
|
|
|
45
48
|
def factor(
|
|
46
49
|
self, axes: Sequence[int], *, validate=True, atol=1e-07
|
|
47
|
-
) -> Tuple[
|
|
50
|
+
) -> Tuple[CountingState, CountingState]:
|
|
48
51
|
return CountingState(
|
|
49
52
|
self.data, self.gate_count, self.measurement_count, self.copy_count
|
|
50
53
|
), CountingState(self.data)
|
|
51
54
|
|
|
52
|
-
def reindex(self, axes: Sequence[int]) ->
|
|
55
|
+
def reindex(self, axes: Sequence[int]) -> CountingState:
|
|
53
56
|
return CountingState(self.data, self.gate_count, self.measurement_count, self.copy_count)
|
|
54
57
|
|
|
55
|
-
def copy(self, deep_copy_buffers: bool = True) ->
|
|
58
|
+
def copy(self, deep_copy_buffers: bool = True) -> CountingState:
|
|
56
59
|
return CountingState(
|
|
57
60
|
self.data, self.gate_count, self.measurement_count, self.copy_count + 1
|
|
58
61
|
)
|
|
@@ -64,7 +67,7 @@ class CountingSimulationState(cirq.SimulationState[CountingState]):
|
|
|
64
67
|
super().__init__(state=state_obj, qubits=qubits, classical_data=classical_data)
|
|
65
68
|
|
|
66
69
|
def _act_on_fallback_(
|
|
67
|
-
self, action: Any, qubits: Sequence[
|
|
70
|
+
self, action: Any, qubits: Sequence[cirq.Qid], allow_decompose: bool = True
|
|
68
71
|
) -> bool:
|
|
69
72
|
self._state.gate_count += 1
|
|
70
73
|
return True
|
|
@@ -121,7 +124,7 @@ class CountingSimulator(
|
|
|
121
124
|
def _create_partial_simulation_state(
|
|
122
125
|
self,
|
|
123
126
|
initial_state: Any,
|
|
124
|
-
qubits: Sequence[
|
|
127
|
+
qubits: Sequence[cirq.Qid],
|
|
125
128
|
classical_data: cirq.ClassicalDataStore,
|
|
126
129
|
) -> CountingSimulationState:
|
|
127
130
|
return CountingSimulationState(
|
|
@@ -132,7 +135,7 @@ class CountingSimulator(
|
|
|
132
135
|
self,
|
|
133
136
|
params: cirq.ParamResolver,
|
|
134
137
|
measurements: Dict[str, np.ndarray],
|
|
135
|
-
final_simulator_state:
|
|
138
|
+
final_simulator_state: cirq.SimulationStateBase[CountingSimulationState],
|
|
136
139
|
) -> CountingTrialResult:
|
|
137
140
|
return CountingTrialResult(
|
|
138
141
|
params, measurements, final_simulator_state=final_simulator_state
|
|
@@ -151,7 +154,7 @@ class SplittableCountingSimulator(CountingSimulator):
|
|
|
151
154
|
def _create_partial_simulation_state(
|
|
152
155
|
self,
|
|
153
156
|
initial_state: Any,
|
|
154
|
-
qubits: Sequence[
|
|
157
|
+
qubits: Sequence[cirq.Qid],
|
|
155
158
|
classical_data: cirq.ClassicalDataStore,
|
|
156
159
|
) -> CountingSimulationState:
|
|
157
160
|
return SplittableCountingSimulationState(
|
cirq/sim/simulator_test.py
CHANGED
|
@@ -11,7 +11,11 @@
|
|
|
11
11
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
|
+
|
|
14
15
|
"""Tests for simulator.py"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
15
19
|
import abc
|
|
16
20
|
from typing import Any, Dict, Generic, List, Sequence, Union
|
|
17
21
|
from unittest import mock
|
|
@@ -64,7 +68,7 @@ class FakeStepResult(cirq.StepResult):
|
|
|
64
68
|
|
|
65
69
|
class SimulatesIntermediateStateImpl(
|
|
66
70
|
Generic[TStepResult, TSimulationState],
|
|
67
|
-
SimulatesIntermediateState[TStepResult,
|
|
71
|
+
SimulatesIntermediateState[TStepResult, SimulationTrialResult, TSimulationState],
|
|
68
72
|
metaclass=abc.ABCMeta,
|
|
69
73
|
):
|
|
70
74
|
"""A SimulatesIntermediateState that uses the default SimulationTrialResult type."""
|
|
@@ -73,8 +77,8 @@ class SimulatesIntermediateStateImpl(
|
|
|
73
77
|
self,
|
|
74
78
|
params: study.ParamResolver,
|
|
75
79
|
measurements: Dict[str, np.ndarray],
|
|
76
|
-
final_simulator_state:
|
|
77
|
-
) ->
|
|
80
|
+
final_simulator_state: cirq.SimulationStateBase[TSimulationState],
|
|
81
|
+
) -> SimulationTrialResult:
|
|
78
82
|
"""This method creates a default trial result.
|
|
79
83
|
|
|
80
84
|
Args:
|
|
@@ -455,7 +459,7 @@ def test_iter_definitions():
|
|
|
455
459
|
|
|
456
460
|
def compute_amplitudes_sweep(
|
|
457
461
|
self,
|
|
458
|
-
program:
|
|
462
|
+
program: cirq.AbstractCircuit,
|
|
459
463
|
bitstrings: Sequence[int],
|
|
460
464
|
params: study.Sweepable,
|
|
461
465
|
qubit_order: cirq.QubitOrderOrList = cirq.QubitOrder.DEFAULT,
|
|
@@ -464,9 +468,9 @@ def test_iter_definitions():
|
|
|
464
468
|
|
|
465
469
|
def simulate_expectation_values_sweep(
|
|
466
470
|
self,
|
|
467
|
-
program:
|
|
468
|
-
observables: Union[
|
|
469
|
-
params:
|
|
471
|
+
program: cirq.AbstractCircuit,
|
|
472
|
+
observables: Union[cirq.PauliSumLike, List[cirq.PauliSumLike]],
|
|
473
|
+
params: study.Sweepable,
|
|
470
474
|
qubit_order: cirq.QubitOrderOrList = cirq.QubitOrder.DEFAULT,
|
|
471
475
|
initial_state: Any = None,
|
|
472
476
|
permit_terminal_measurements: bool = False,
|
|
@@ -475,7 +479,7 @@ def test_iter_definitions():
|
|
|
475
479
|
|
|
476
480
|
def simulate_sweep(
|
|
477
481
|
self,
|
|
478
|
-
program:
|
|
482
|
+
program: cirq.AbstractCircuit,
|
|
479
483
|
params: study.Sweepable,
|
|
480
484
|
qubit_order: cirq.QubitOrderOrList = cirq.QubitOrder.DEFAULT,
|
|
481
485
|
initial_state: Any = None,
|