cirq-core 1.6.0.dev20250520054601__py3-none-any.whl → 1.6.0.dev20250520183459__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/_compat.py +15 -17
- cirq/_compat_test.py +6 -9
- cirq/_doc.py +2 -2
- cirq/_import.py +6 -6
- cirq/_version.py +1 -1
- cirq/_version_test.py +1 -1
- cirq/circuits/_block_diagram_drawer.py +9 -10
- cirq/circuits/_box_drawing_character_data.py +6 -8
- cirq/circuits/_bucket_priority_queue.py +7 -7
- cirq/circuits/circuit.py +118 -125
- cirq/circuits/circuit_operation.py +38 -52
- cirq/circuits/circuit_test.py +4 -4
- cirq/circuits/frozen_circuit.py +13 -23
- cirq/circuits/moment.py +23 -29
- cirq/circuits/optimization_pass.py +4 -4
- cirq/circuits/optimization_pass_test.py +4 -6
- cirq/circuits/qasm_output.py +11 -11
- cirq/circuits/text_diagram_drawer.py +21 -36
- cirq/contrib/acquaintance/bipartite.py +5 -8
- cirq/contrib/acquaintance/executor.py +5 -5
- cirq/contrib/acquaintance/executor_test.py +3 -3
- cirq/contrib/acquaintance/gates.py +16 -26
- cirq/contrib/acquaintance/gates_test.py +3 -3
- cirq/contrib/acquaintance/mutation_utils.py +4 -4
- cirq/contrib/acquaintance/optimizers.py +4 -4
- cirq/contrib/acquaintance/permutation.py +15 -27
- cirq/contrib/acquaintance/shift.py +3 -3
- cirq/contrib/acquaintance/shift_swap_network.py +4 -4
- cirq/contrib/acquaintance/strategies/cubic.py +2 -2
- cirq/contrib/acquaintance/strategies/quartic_paired.py +6 -6
- cirq/contrib/bayesian_network/bayesian_network_gate.py +9 -10
- cirq/contrib/circuitdag/circuit_dag.py +2 -2
- cirq/contrib/custom_simulators/custom_state_simulator.py +3 -3
- cirq/contrib/custom_simulators/custom_state_simulator_test.py +4 -4
- cirq/contrib/graph_device/graph_device.py +5 -5
- cirq/contrib/graph_device/hypergraph.py +12 -12
- cirq/contrib/graph_device/uniform_graph_device.py +4 -4
- cirq/contrib/paulistring/clifford_optimize.py +2 -2
- cirq/contrib/paulistring/clifford_target_gateset.py +7 -7
- cirq/contrib/paulistring/pauli_string_measurement_with_readout_mitigation.py +31 -31
- cirq/contrib/paulistring/pauli_string_measurement_with_readout_mitigation_test.py +23 -23
- cirq/contrib/paulistring/recombine.py +3 -3
- cirq/contrib/paulistring/separate.py +2 -2
- cirq/contrib/qasm_import/_parser.py +20 -32
- cirq/contrib/qcircuit/qcircuit_diagram_info.py +3 -5
- cirq/contrib/quantum_volume/quantum_volume.py +24 -24
- cirq/contrib/quimb/density_matrix.py +12 -14
- cirq/contrib/quimb/mps_simulator.py +20 -20
- cirq/contrib/quimb/state_vector.py +6 -10
- cirq/contrib/quirk/export_to_quirk.py +3 -3
- cirq/contrib/quirk/quirk_gate.py +15 -15
- cirq/contrib/routing/device.py +3 -3
- cirq/contrib/routing/greedy.py +10 -21
- cirq/contrib/routing/initialization.py +2 -2
- cirq/contrib/routing/swap_network.py +3 -3
- cirq/contrib/routing/utils.py +2 -2
- cirq/contrib/shuffle_circuits/shuffle_circuits_with_readout_benchmarking.py +8 -8
- cirq/contrib/svg/svg.py +3 -3
- cirq/devices/grid_device_metadata.py +10 -10
- cirq/devices/grid_qubit.py +20 -20
- cirq/devices/insertion_noise_model.py +5 -5
- cirq/devices/line_qubit.py +13 -13
- cirq/devices/named_topologies.py +18 -29
- cirq/devices/noise_model.py +3 -3
- cirq/devices/noise_properties.py +2 -2
- cirq/devices/noise_properties_test.py +1 -3
- cirq/devices/noise_utils.py +7 -7
- cirq/devices/superconducting_qubits_noise_properties.py +21 -21
- cirq/devices/superconducting_qubits_noise_properties_test.py +5 -7
- cirq/devices/thermal_noise_model.py +14 -14
- cirq/devices/unconstrained_device.py +2 -2
- cirq/experiments/benchmarking/parallel_xeb.py +29 -31
- cirq/experiments/n_qubit_tomography.py +5 -7
- cirq/experiments/qubit_characterizations.py +29 -40
- cirq/experiments/qubit_characterizations_test.py +1 -1
- cirq/experiments/random_quantum_circuit_generation.py +19 -33
- cirq/experiments/random_quantum_circuit_generation_test.py +6 -6
- cirq/experiments/readout_confusion_matrix.py +14 -14
- cirq/experiments/single_qubit_readout_calibration.py +12 -12
- cirq/experiments/t2_decay_experiment.py +7 -7
- cirq/experiments/two_qubit_xeb.py +32 -32
- cirq/experiments/two_qubit_xeb_test.py +5 -5
- cirq/experiments/xeb_fitting.py +25 -25
- cirq/experiments/xeb_sampling.py +22 -33
- cirq/experiments/xeb_simulation.py +5 -5
- cirq/experiments/xeb_simulation_test.py +3 -3
- cirq/experiments/z_phase_calibration.py +19 -19
- cirq/interop/quirk/cells/arithmetic_cells.py +23 -36
- cirq/interop/quirk/cells/cell.py +9 -21
- cirq/interop/quirk/cells/composite_cell.py +7 -22
- cirq/interop/quirk/cells/control_cells.py +8 -8
- cirq/interop/quirk/cells/input_cells.py +4 -4
- cirq/interop/quirk/cells/input_rotation_cells.py +5 -5
- cirq/interop/quirk/cells/parse.py +20 -23
- cirq/interop/quirk/cells/qubit_permutation_cells.py +3 -3
- cirq/interop/quirk/cells/swap_cell.py +3 -3
- cirq/interop/quirk/cells/testing.py +5 -7
- cirq/interop/quirk/url_to_circuit.py +17 -33
- cirq/json_resolver_cache.py +6 -6
- cirq/linalg/decompositions.py +20 -31
- cirq/linalg/diagonalize.py +4 -4
- cirq/linalg/diagonalize_test.py +3 -4
- cirq/linalg/operator_spaces.py +5 -5
- cirq/linalg/predicates.py +7 -7
- cirq/linalg/transformations.py +20 -20
- cirq/ops/arithmetic_operation.py +13 -15
- cirq/ops/boolean_hamiltonian.py +17 -17
- cirq/ops/classically_controlled_operation.py +13 -25
- cirq/ops/clifford_gate.py +31 -35
- cirq/ops/clifford_gate_test.py +2 -3
- cirq/ops/common_channels.py +30 -32
- cirq/ops/common_gates.py +64 -74
- cirq/ops/control_values.py +12 -12
- cirq/ops/controlled_gate.py +15 -30
- cirq/ops/controlled_gate_test.py +5 -5
- cirq/ops/controlled_operation.py +12 -25
- cirq/ops/controlled_operation_test.py +5 -5
- cirq/ops/dense_pauli_string.py +23 -34
- cirq/ops/dense_pauli_string_test.py +1 -2
- cirq/ops/diagonal_gate.py +9 -20
- cirq/ops/diagonal_gate_test.py +1 -3
- cirq/ops/eigen_gate.py +11 -23
- cirq/ops/eigen_gate_test.py +6 -8
- cirq/ops/fourier_transform.py +5 -5
- cirq/ops/fsim_gate.py +14 -14
- cirq/ops/gate_operation.py +23 -44
- cirq/ops/gateset.py +23 -37
- cirq/ops/gateset_test.py +2 -2
- cirq/ops/global_phase_op.py +8 -10
- cirq/ops/greedy_qubit_manager.py +6 -6
- cirq/ops/identity.py +9 -9
- cirq/ops/kraus_channel.py +7 -7
- cirq/ops/linear_combinations.py +29 -48
- cirq/ops/matrix_gates.py +8 -8
- cirq/ops/measure_util.py +13 -14
- cirq/ops/measurement_gate.py +18 -29
- cirq/ops/mixed_unitary_channel.py +8 -8
- cirq/ops/named_qubit.py +10 -10
- cirq/ops/op_tree.py +7 -7
- cirq/ops/parallel_gate.py +5 -5
- cirq/ops/parity_gates.py +14 -14
- cirq/ops/pauli_gates.py +8 -10
- cirq/ops/pauli_interaction_gate.py +6 -6
- cirq/ops/pauli_measurement_gate.py +11 -23
- cirq/ops/pauli_string.py +35 -52
- cirq/ops/pauli_string_phasor.py +4 -14
- cirq/ops/pauli_string_raw_types.py +3 -3
- cirq/ops/pauli_sum_exponential.py +2 -2
- cirq/ops/permutation_gate.py +4 -4
- cirq/ops/phased_iswap_gate.py +9 -9
- cirq/ops/phased_x_gate.py +10 -10
- cirq/ops/phased_x_z_gate.py +11 -11
- cirq/ops/projector.py +6 -6
- cirq/ops/qubit_manager.py +6 -6
- cirq/ops/qubit_order.py +3 -3
- cirq/ops/random_gate_channel.py +4 -4
- cirq/ops/raw_types.py +48 -70
- cirq/ops/state_preparation_channel.py +3 -3
- cirq/ops/swap_gates.py +9 -9
- cirq/ops/tags.py +2 -4
- cirq/ops/three_qubit_gates.py +20 -38
- cirq/ops/two_qubit_diagonal_gate.py +5 -5
- cirq/ops/uniform_superposition_gate.py +2 -2
- cirq/ops/wait_gate.py +5 -5
- cirq/protocols/act_on_protocol_test.py +3 -3
- cirq/protocols/apply_channel_protocol.py +8 -14
- cirq/protocols/apply_mixture_protocol.py +14 -16
- cirq/protocols/apply_mixture_protocol_test.py +5 -6
- cirq/protocols/apply_unitary_protocol.py +17 -19
- cirq/protocols/circuit_diagram_info_protocol.py +19 -30
- cirq/protocols/decompose_protocol.py +30 -34
- cirq/protocols/inverse_protocol.py +7 -7
- cirq/protocols/json_serialization.py +32 -51
- cirq/protocols/json_serialization_test.py +9 -10
- cirq/protocols/kraus_protocol.py +4 -4
- cirq/protocols/kraus_protocol_test.py +3 -3
- cirq/protocols/measurement_key_protocol.py +11 -13
- cirq/protocols/mixture_protocol.py +4 -4
- cirq/protocols/qasm.py +11 -13
- cirq/protocols/qid_shape_protocol.py +6 -8
- cirq/qis/clifford_tableau.py +12 -12
- cirq/qis/measures.py +7 -7
- cirq/qis/quantum_state_representation.py +3 -3
- cirq/qis/states.py +51 -51
- cirq/sim/classical_simulator.py +10 -10
- cirq/sim/clifford/clifford_simulator.py +6 -6
- cirq/sim/clifford/clifford_tableau_simulation_state_test.py +1 -3
- cirq/sim/clifford/stabilizer_sampler.py +4 -4
- cirq/sim/clifford/stabilizer_state_ch_form.py +3 -3
- cirq/sim/density_matrix_simulation_state.py +15 -15
- cirq/sim/density_matrix_simulator.py +11 -11
- cirq/sim/density_matrix_utils.py +9 -9
- cirq/sim/mux.py +9 -9
- cirq/sim/simulation_product_state.py +9 -9
- cirq/sim/simulation_product_state_test.py +2 -2
- cirq/sim/simulation_state.py +14 -27
- cirq/sim/simulation_state_base.py +8 -24
- cirq/sim/simulation_utils.py +3 -4
- cirq/sim/simulator.py +28 -43
- cirq/sim/simulator_base.py +12 -25
- cirq/sim/simulator_base_test.py +6 -6
- cirq/sim/simulator_test.py +7 -7
- cirq/sim/sparse_simulator.py +8 -8
- cirq/sim/state_vector.py +8 -8
- cirq/sim/state_vector_simulation_state.py +17 -17
- cirq/sim/state_vector_simulator.py +4 -4
- cirq/study/flatten_expressions.py +12 -14
- cirq/study/resolver.py +9 -11
- cirq/study/result.py +11 -24
- cirq/study/sweepable.py +5 -5
- cirq/study/sweeps.py +27 -40
- cirq/testing/circuit_compare.py +5 -5
- cirq/testing/consistent_controlled_gate_op_test.py +7 -11
- cirq/testing/consistent_protocols.py +10 -10
- cirq/testing/consistent_protocols_test.py +7 -7
- cirq/testing/consistent_qasm.py +4 -4
- cirq/testing/consistent_qasm_test.py +2 -3
- cirq/testing/devices.py +4 -5
- cirq/testing/equals_tester.py +2 -2
- cirq/testing/equivalent_basis_map.py +4 -4
- cirq/testing/equivalent_repr_eval.py +3 -3
- cirq/testing/json.py +14 -14
- cirq/testing/logs.py +3 -3
- cirq/testing/no_identifier_qubit.py +2 -3
- cirq/testing/random_circuit.py +7 -7
- cirq/testing/random_circuit_test.py +3 -3
- cirq/transformers/analytical_decompositions/clifford_decomposition.py +16 -16
- cirq/transformers/analytical_decompositions/controlled_gate_decomposition.py +13 -13
- cirq/transformers/analytical_decompositions/cphase_to_fsim.py +5 -5
- cirq/transformers/analytical_decompositions/cphase_to_fsim_test.py +3 -3
- cirq/transformers/analytical_decompositions/pauli_string_decomposition.py +3 -3
- cirq/transformers/analytical_decompositions/quantum_shannon_decomposition.py +4 -4
- cirq/transformers/analytical_decompositions/single_qubit_decompositions.py +6 -7
- cirq/transformers/analytical_decompositions/single_to_two_qubit_isometry.py +2 -2
- cirq/transformers/analytical_decompositions/three_qubit_decomposition.py +7 -7
- cirq/transformers/analytical_decompositions/two_qubit_state_preparation.py +4 -4
- cirq/transformers/analytical_decompositions/two_qubit_to_cz.py +7 -7
- cirq/transformers/analytical_decompositions/two_qubit_to_fsim.py +11 -11
- cirq/transformers/analytical_decompositions/two_qubit_to_ms.py +5 -5
- cirq/transformers/analytical_decompositions/two_qubit_to_sqrt_iswap.py +14 -14
- cirq/transformers/dynamical_decoupling.py +13 -13
- cirq/transformers/dynamical_decoupling_test.py +4 -4
- cirq/transformers/eject_phased_paulis.py +16 -16
- cirq/transformers/eject_z.py +5 -7
- cirq/transformers/gauge_compiling/gauge_compiling.py +38 -38
- cirq/transformers/gauge_compiling/sqrt_cz_gauge.py +2 -2
- cirq/transformers/heuristic_decompositions/two_qubit_gate_tabulation.py +8 -8
- cirq/transformers/insertion_sort.py +5 -5
- cirq/transformers/measurement_transformers.py +14 -14
- cirq/transformers/merge_k_qubit_gates_test.py +1 -3
- cirq/transformers/merge_single_qubit_gates_test.py +1 -3
- cirq/transformers/qubit_management_transformers.py +5 -5
- cirq/transformers/routing/initial_mapper.py +4 -4
- cirq/transformers/routing/line_initial_mapper.py +9 -9
- cirq/transformers/routing/mapping_manager.py +7 -7
- cirq/transformers/routing/route_circuit_cqc.py +27 -27
- cirq/transformers/routing/visualize_routed_circuit.py +4 -4
- cirq/transformers/stratify.py +8 -8
- cirq/transformers/synchronize_terminal_measurements.py +6 -6
- cirq/transformers/target_gatesets/compilation_target_gateset.py +8 -8
- cirq/transformers/target_gatesets/compilation_target_gateset_test.py +2 -2
- cirq/transformers/target_gatesets/cz_gateset.py +4 -4
- cirq/transformers/target_gatesets/sqrt_iswap_gateset.py +5 -5
- cirq/transformers/transformer_api.py +11 -26
- cirq/transformers/transformer_primitives.py +24 -36
- cirq/transformers/transformer_primitives_test.py +3 -3
- cirq/value/classical_data.py +18 -18
- cirq/value/condition.py +8 -8
- cirq/value/digits.py +7 -7
- cirq/value/duration.py +12 -12
- cirq/value/linear_dict.py +8 -12
- cirq/value/linear_dict_test.py +2 -2
- cirq/value/measurement_key.py +8 -8
- cirq/value/product_state.py +9 -9
- cirq/value/value_equality_attr.py +4 -4
- cirq/vis/heatmap.py +23 -35
- cirq/work/collector.py +9 -17
- cirq/work/observable_grouping.py +4 -7
- cirq/work/observable_measurement.py +29 -41
- cirq/work/observable_measurement_data.py +14 -14
- cirq/work/observable_measurement_test.py +2 -2
- cirq/work/observable_settings.py +9 -10
- cirq/work/pauli_sum_collector.py +5 -5
- cirq/work/sampler.py +17 -17
- cirq/work/zeros_sampler.py +3 -3
- {cirq_core-1.6.0.dev20250520054601.dist-info → cirq_core-1.6.0.dev20250520183459.dist-info}/METADATA +1 -1
- {cirq_core-1.6.0.dev20250520054601.dist-info → cirq_core-1.6.0.dev20250520183459.dist-info}/RECORD +290 -290
- {cirq_core-1.6.0.dev20250520054601.dist-info → cirq_core-1.6.0.dev20250520183459.dist-info}/WHEEL +1 -1
- {cirq_core-1.6.0.dev20250520054601.dist-info → cirq_core-1.6.0.dev20250520183459.dist-info}/licenses/LICENSE +0 -0
- {cirq_core-1.6.0.dev20250520054601.dist-info → cirq_core-1.6.0.dev20250520183459.dist-info}/top_level.txt +0 -0
cirq/sim/simulator.py
CHANGED
|
@@ -31,22 +31,7 @@ from __future__ import annotations
|
|
|
31
31
|
|
|
32
32
|
import abc
|
|
33
33
|
import collections
|
|
34
|
-
from typing import
|
|
35
|
-
Any,
|
|
36
|
-
Callable,
|
|
37
|
-
cast,
|
|
38
|
-
Dict,
|
|
39
|
-
Generic,
|
|
40
|
-
Iterator,
|
|
41
|
-
List,
|
|
42
|
-
Mapping,
|
|
43
|
-
Sequence,
|
|
44
|
-
Set,
|
|
45
|
-
Tuple,
|
|
46
|
-
TYPE_CHECKING,
|
|
47
|
-
TypeVar,
|
|
48
|
-
Union,
|
|
49
|
-
)
|
|
34
|
+
from typing import Any, Callable, cast, Generic, Iterator, Mapping, Sequence, TYPE_CHECKING, TypeVar
|
|
50
35
|
|
|
51
36
|
import numpy as np
|
|
52
37
|
|
|
@@ -110,7 +95,7 @@ class SimulatesSamples(work.Sampler, metaclass=abc.ABCMeta):
|
|
|
110
95
|
@abc.abstractmethod
|
|
111
96
|
def _run(
|
|
112
97
|
self, circuit: cirq.AbstractCircuit, param_resolver: cirq.ParamResolver, repetitions: int
|
|
113
|
-
) ->
|
|
98
|
+
) -> dict[str, np.ndarray]:
|
|
114
99
|
"""Run a simulation, mimicking quantum hardware.
|
|
115
100
|
|
|
116
101
|
Args:
|
|
@@ -236,7 +221,7 @@ class SimulatesAmplitudes(metaclass=value.ABCMetaImplementAnyOneOf):
|
|
|
236
221
|
seed: cirq.RANDOM_STATE_OR_SEED_LIKE,
|
|
237
222
|
repetitions: int = 1,
|
|
238
223
|
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
239
|
-
) ->
|
|
224
|
+
) -> dict[int, int]:
|
|
240
225
|
"""Uses amplitude simulation to sample from the given circuit.
|
|
241
226
|
|
|
242
227
|
This implements the algorithm outlined by Bravyi, Gosset, and Liu in
|
|
@@ -279,7 +264,7 @@ class SimulatesAmplitudes(metaclass=value.ABCMetaImplementAnyOneOf):
|
|
|
279
264
|
for m_id, moment in enumerate(solved_circuit[1:]):
|
|
280
265
|
circuit_prefix = solved_circuit[: m_id + 1]
|
|
281
266
|
for t, op in enumerate(moment.operations):
|
|
282
|
-
new_samples:
|
|
267
|
+
new_samples: dict[tuple[int, ...], int] = collections.defaultdict(int)
|
|
283
268
|
qubit_indices = {qmap[q] for q in op.qubits}
|
|
284
269
|
subcircuit = circuit_prefix + circuits.Moment(moment.operations[: t + 1])
|
|
285
270
|
for current_sample, count in current_samples.items():
|
|
@@ -315,12 +300,12 @@ class SimulatesExpectationValues(metaclass=value.ABCMetaImplementAnyOneOf):
|
|
|
315
300
|
def simulate_expectation_values(
|
|
316
301
|
self,
|
|
317
302
|
program: cirq.AbstractCircuit,
|
|
318
|
-
observables:
|
|
303
|
+
observables: cirq.PauliSumLike | list[cirq.PauliSumLike],
|
|
319
304
|
param_resolver: cirq.ParamResolverOrSimilarType = None,
|
|
320
305
|
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
321
306
|
initial_state: Any = None,
|
|
322
307
|
permit_terminal_measurements: bool = False,
|
|
323
|
-
) ->
|
|
308
|
+
) -> list[float]:
|
|
324
309
|
"""Simulates the supplied circuit and calculates exact expectation
|
|
325
310
|
values for the given observables on its final state.
|
|
326
311
|
|
|
@@ -363,12 +348,12 @@ class SimulatesExpectationValues(metaclass=value.ABCMetaImplementAnyOneOf):
|
|
|
363
348
|
def simulate_expectation_values_sweep(
|
|
364
349
|
self,
|
|
365
350
|
program: cirq.AbstractCircuit,
|
|
366
|
-
observables:
|
|
351
|
+
observables: cirq.PauliSumLike | list[cirq.PauliSumLike],
|
|
367
352
|
params: cirq.Sweepable,
|
|
368
353
|
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
369
354
|
initial_state: Any = None,
|
|
370
355
|
permit_terminal_measurements: bool = False,
|
|
371
|
-
) ->
|
|
356
|
+
) -> list[list[float]]:
|
|
372
357
|
"""Wraps computed expectation values in a list.
|
|
373
358
|
|
|
374
359
|
Prefer overriding `simulate_expectation_values_sweep_iter`.
|
|
@@ -387,12 +372,12 @@ class SimulatesExpectationValues(metaclass=value.ABCMetaImplementAnyOneOf):
|
|
|
387
372
|
def _simulate_expectation_values_sweep_to_iter(
|
|
388
373
|
self,
|
|
389
374
|
program: cirq.AbstractCircuit,
|
|
390
|
-
observables:
|
|
375
|
+
observables: cirq.PauliSumLike | list[cirq.PauliSumLike],
|
|
391
376
|
params: cirq.Sweepable,
|
|
392
377
|
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
393
378
|
initial_state: Any = None,
|
|
394
379
|
permit_terminal_measurements: bool = False,
|
|
395
|
-
) -> Iterator[
|
|
380
|
+
) -> Iterator[list[float]]:
|
|
396
381
|
if (
|
|
397
382
|
type(self).simulate_expectation_values_sweep
|
|
398
383
|
== SimulatesExpectationValues.simulate_expectation_values_sweep
|
|
@@ -412,12 +397,12 @@ class SimulatesExpectationValues(metaclass=value.ABCMetaImplementAnyOneOf):
|
|
|
412
397
|
def simulate_expectation_values_sweep_iter(
|
|
413
398
|
self,
|
|
414
399
|
program: cirq.AbstractCircuit,
|
|
415
|
-
observables:
|
|
400
|
+
observables: cirq.PauliSumLike | list[cirq.PauliSumLike],
|
|
416
401
|
params: cirq.Sweepable,
|
|
417
402
|
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
418
403
|
initial_state: Any = None,
|
|
419
404
|
permit_terminal_measurements: bool = False,
|
|
420
|
-
) -> Iterator[
|
|
405
|
+
) -> Iterator[list[float]]:
|
|
421
406
|
"""Simulates the supplied circuit and calculates exact expectation
|
|
422
407
|
values for the given observables on its final state, sweeping over the
|
|
423
408
|
given params.
|
|
@@ -502,7 +487,7 @@ class SimulatesFinalState(
|
|
|
502
487
|
params: cirq.Sweepable,
|
|
503
488
|
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
504
489
|
initial_state: Any = None,
|
|
505
|
-
) ->
|
|
490
|
+
) -> list[TSimulationTrialResult]:
|
|
506
491
|
"""Wraps computed states in a list.
|
|
507
492
|
|
|
508
493
|
Prefer overriding `simulate_sweep_iter`.
|
|
@@ -608,7 +593,7 @@ class SimulatesIntermediateState(
|
|
|
608
593
|
all_step_results = self.simulate_moment_steps(
|
|
609
594
|
program, param_resolver, qubit_order, state
|
|
610
595
|
)
|
|
611
|
-
measurements:
|
|
596
|
+
measurements: dict[str, np.ndarray] = {}
|
|
612
597
|
for step_result in all_step_results:
|
|
613
598
|
for k, v in step_result.measurements.items():
|
|
614
599
|
measurements[k] = np.array(v, dtype=np.uint8)
|
|
@@ -654,7 +639,7 @@ class SimulatesIntermediateState(
|
|
|
654
639
|
|
|
655
640
|
@abc.abstractmethod
|
|
656
641
|
def _base_iterator(
|
|
657
|
-
self, circuit: cirq.AbstractCircuit, qubits:
|
|
642
|
+
self, circuit: cirq.AbstractCircuit, qubits: tuple[cirq.Qid, ...], initial_state: Any
|
|
658
643
|
) -> Iterator[TStepResult]:
|
|
659
644
|
"""Iterator over StepResult from Moments of a Circuit.
|
|
660
645
|
|
|
@@ -675,7 +660,7 @@ class SimulatesIntermediateState(
|
|
|
675
660
|
def _create_simulator_trial_result(
|
|
676
661
|
self,
|
|
677
662
|
params: cirq.ParamResolver,
|
|
678
|
-
measurements:
|
|
663
|
+
measurements: dict[str, np.ndarray],
|
|
679
664
|
final_simulator_state: TSimulatorState,
|
|
680
665
|
) -> TSimulationTrialResult:
|
|
681
666
|
"""This method can be implemented to create a trial result.
|
|
@@ -722,7 +707,7 @@ class StepResult(Generic[TSimulatorState], metaclass=abc.ABCMeta):
|
|
|
722
707
|
@abc.abstractmethod
|
|
723
708
|
def sample(
|
|
724
709
|
self,
|
|
725
|
-
qubits:
|
|
710
|
+
qubits: list[cirq.Qid],
|
|
726
711
|
repetitions: int = 1,
|
|
727
712
|
seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None,
|
|
728
713
|
) -> np.ndarray:
|
|
@@ -746,12 +731,12 @@ class StepResult(Generic[TSimulatorState], metaclass=abc.ABCMeta):
|
|
|
746
731
|
|
|
747
732
|
def sample_measurement_ops(
|
|
748
733
|
self,
|
|
749
|
-
measurement_ops:
|
|
734
|
+
measurement_ops: list[cirq.GateOperation],
|
|
750
735
|
repetitions: int = 1,
|
|
751
736
|
seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None,
|
|
752
737
|
*,
|
|
753
738
|
_allow_repeated=False,
|
|
754
|
-
) ->
|
|
739
|
+
) -> dict[str, np.ndarray]:
|
|
755
740
|
"""Samples from the system at this point in the computation.
|
|
756
741
|
|
|
757
742
|
Note that this does not collapse the state vector.
|
|
@@ -797,7 +782,7 @@ class StepResult(Generic[TSimulatorState], metaclass=abc.ABCMeta):
|
|
|
797
782
|
|
|
798
783
|
# Find measured qubits, ensuring a consistent ordering.
|
|
799
784
|
measured_qubits = []
|
|
800
|
-
seen_qubits:
|
|
785
|
+
seen_qubits: set[cirq.Qid] = set()
|
|
801
786
|
for op in measurement_ops:
|
|
802
787
|
for q in op.qubits:
|
|
803
788
|
if q not in seen_qubits:
|
|
@@ -808,7 +793,7 @@ class StepResult(Generic[TSimulatorState], metaclass=abc.ABCMeta):
|
|
|
808
793
|
indexed_sample = self.sample(measured_qubits, repetitions, seed=seed)
|
|
809
794
|
|
|
810
795
|
# Extract results for each measurement.
|
|
811
|
-
results:
|
|
796
|
+
results: dict[str, Any] = {}
|
|
812
797
|
qubits_to_index = {q: i for i, q in enumerate(measured_qubits)}
|
|
813
798
|
for op in measurement_ops:
|
|
814
799
|
gate = cast(ops.MeasurementGate, op.gate)
|
|
@@ -837,7 +822,7 @@ class StepResult(Generic[TSimulatorState], metaclass=abc.ABCMeta):
|
|
|
837
822
|
self,
|
|
838
823
|
bits: np.ndarray,
|
|
839
824
|
qubits: Sequence[cirq.Qid],
|
|
840
|
-
confusion_map:
|
|
825
|
+
confusion_map: dict[tuple[int, ...], np.ndarray],
|
|
841
826
|
seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None,
|
|
842
827
|
) -> None:
|
|
843
828
|
"""Mutates `bits` using the confusion_map.
|
|
@@ -937,12 +922,12 @@ class SimulationTrialResult(Generic[TSimulatorState]):
|
|
|
937
922
|
"""
|
|
938
923
|
return self._final_simulator_state.qubit_map
|
|
939
924
|
|
|
940
|
-
def _qid_shape_(self) ->
|
|
925
|
+
def _qid_shape_(self) -> tuple[int, ...]:
|
|
941
926
|
return _qubit_map_to_shape(self.qubit_map)
|
|
942
927
|
|
|
943
928
|
|
|
944
|
-
def _qubit_map_to_shape(qubit_map: Mapping[cirq.Qid, int]) ->
|
|
945
|
-
qid_shape:
|
|
929
|
+
def _qubit_map_to_shape(qubit_map: Mapping[cirq.Qid, int]) -> tuple[int, ...]:
|
|
930
|
+
qid_shape: list[int] = [-1] * len(qubit_map)
|
|
946
931
|
try:
|
|
947
932
|
for q, i in qubit_map.items():
|
|
948
933
|
qid_shape[i] = q.dimension
|
|
@@ -965,7 +950,7 @@ def check_all_resolved(circuit):
|
|
|
965
950
|
|
|
966
951
|
def split_into_matching_protocol_then_general(
|
|
967
952
|
circuit: cirq.AbstractCircuit, predicate: Callable[[cirq.Operation], bool]
|
|
968
|
-
) ->
|
|
953
|
+
) -> tuple[cirq.AbstractCircuit, cirq.AbstractCircuit]:
|
|
969
954
|
"""Splits the circuit into a matching prefix and non-matching suffix.
|
|
970
955
|
|
|
971
956
|
The splitting happens in a per-qubit fashion. A non-matching operation on
|
|
@@ -974,8 +959,8 @@ def split_into_matching_protocol_then_general(
|
|
|
974
959
|
the matching part (as long as those qubits have had no non-matching operation
|
|
975
960
|
up to that point). Measurement keys are handled equivalently.
|
|
976
961
|
"""
|
|
977
|
-
blocked_qubits:
|
|
978
|
-
blocked_keys:
|
|
962
|
+
blocked_qubits: set[cirq.Qid] = set()
|
|
963
|
+
blocked_keys: set[cirq.MeasurementKey] = set()
|
|
979
964
|
matching_prefix = circuits.Circuit()
|
|
980
965
|
general_suffix = circuits.Circuit()
|
|
981
966
|
for moment in circuit:
|
cirq/sim/simulator_base.py
CHANGED
|
@@ -18,20 +18,7 @@ from __future__ import annotations
|
|
|
18
18
|
|
|
19
19
|
import abc
|
|
20
20
|
import collections
|
|
21
|
-
from typing import
|
|
22
|
-
Any,
|
|
23
|
-
cast,
|
|
24
|
-
Dict,
|
|
25
|
-
Generic,
|
|
26
|
-
Iterator,
|
|
27
|
-
List,
|
|
28
|
-
Optional,
|
|
29
|
-
Sequence,
|
|
30
|
-
Tuple,
|
|
31
|
-
Type,
|
|
32
|
-
TYPE_CHECKING,
|
|
33
|
-
TypeVar,
|
|
34
|
-
)
|
|
21
|
+
from typing import Any, cast, Generic, Iterator, Sequence, TYPE_CHECKING, TypeVar
|
|
35
22
|
|
|
36
23
|
import numpy as np
|
|
37
24
|
|
|
@@ -93,7 +80,7 @@ class SimulatorBase(
|
|
|
93
80
|
def __init__(
|
|
94
81
|
self,
|
|
95
82
|
*,
|
|
96
|
-
dtype:
|
|
83
|
+
dtype: type[np.complexfloating] = np.complex64,
|
|
97
84
|
noise: cirq.NOISE_MODEL_LIKE = None,
|
|
98
85
|
seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None,
|
|
99
86
|
split_untangled_states: bool = False,
|
|
@@ -173,7 +160,7 @@ class SimulatorBase(
|
|
|
173
160
|
return protocols.has_unitary(val)
|
|
174
161
|
|
|
175
162
|
def _base_iterator(
|
|
176
|
-
self, circuit: cirq.AbstractCircuit, qubits:
|
|
163
|
+
self, circuit: cirq.AbstractCircuit, qubits: tuple[cirq.Qid, ...], initial_state: Any
|
|
177
164
|
) -> Iterator[TStepResultBase]:
|
|
178
165
|
sim_state = self._create_simulation_state(initial_state, qubits)
|
|
179
166
|
return self._core_iterator(circuit, sim_state)
|
|
@@ -206,7 +193,7 @@ class SimulatorBase(
|
|
|
206
193
|
return
|
|
207
194
|
|
|
208
195
|
noisy_moments = self.noise.noisy_moments(circuit, sorted(circuit.all_qubits()))
|
|
209
|
-
measured:
|
|
196
|
+
measured: dict[tuple[cirq.Qid, ...], bool] = collections.defaultdict(bool)
|
|
210
197
|
for moment in noisy_moments:
|
|
211
198
|
for op in ops.flatten_to_ops(moment):
|
|
212
199
|
try:
|
|
@@ -227,7 +214,7 @@ class SimulatorBase(
|
|
|
227
214
|
|
|
228
215
|
def _run(
|
|
229
216
|
self, circuit: cirq.AbstractCircuit, param_resolver: cirq.ParamResolver, repetitions: int
|
|
230
|
-
) ->
|
|
217
|
+
) -> dict[str, np.ndarray]:
|
|
231
218
|
"""See definition in `cirq.SimulatesSamples`."""
|
|
232
219
|
param_resolver = param_resolver or study.ParamResolver({})
|
|
233
220
|
resolved_circuit = protocols.resolve_parameters(circuit, param_resolver)
|
|
@@ -256,7 +243,7 @@ class SimulatorBase(
|
|
|
256
243
|
measurement_ops, repetitions, seed=self._prng, _allow_repeated=True
|
|
257
244
|
)
|
|
258
245
|
|
|
259
|
-
records:
|
|
246
|
+
records: dict[cirq.MeasurementKey, list[Sequence[Sequence[int]]]] = {}
|
|
260
247
|
for i in range(repetitions):
|
|
261
248
|
for step_result in self._core_iterator(
|
|
262
249
|
general_suffix,
|
|
@@ -337,7 +324,7 @@ class SimulatorBase(
|
|
|
337
324
|
|
|
338
325
|
classical_data = value.ClassicalDataDictionaryStore()
|
|
339
326
|
if self._split_untangled_states:
|
|
340
|
-
args_map:
|
|
327
|
+
args_map: dict[cirq.Qid | None, TSimulationState] = {}
|
|
341
328
|
if isinstance(initial_state, int):
|
|
342
329
|
for q in reversed(qubits):
|
|
343
330
|
args_map[q] = self._create_partial_simulation_state(
|
|
@@ -374,7 +361,7 @@ class StepResultBase(
|
|
|
374
361
|
sim_state: The `SimulationStateBase` for this step.
|
|
375
362
|
"""
|
|
376
363
|
super().__init__(sim_state)
|
|
377
|
-
self._merged_sim_state_cache:
|
|
364
|
+
self._merged_sim_state_cache: TSimulationState | None = None
|
|
378
365
|
qubits = sim_state.qubits
|
|
379
366
|
self._qubits = qubits
|
|
380
367
|
self._qubit_mapping = {q: i for i, q in enumerate(qubits)}
|
|
@@ -392,7 +379,7 @@ class StepResultBase(
|
|
|
392
379
|
|
|
393
380
|
def sample(
|
|
394
381
|
self,
|
|
395
|
-
qubits:
|
|
382
|
+
qubits: list[cirq.Qid],
|
|
396
383
|
repetitions: int = 1,
|
|
397
384
|
seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None,
|
|
398
385
|
) -> np.ndarray:
|
|
@@ -407,7 +394,7 @@ class SimulationTrialResultBase(
|
|
|
407
394
|
def __init__(
|
|
408
395
|
self,
|
|
409
396
|
params: study.ParamResolver,
|
|
410
|
-
measurements:
|
|
397
|
+
measurements: dict[str, np.ndarray],
|
|
411
398
|
final_simulator_state: cirq.SimulationStateBase[TSimulationState],
|
|
412
399
|
) -> None:
|
|
413
400
|
"""Initializes the `SimulationTrialResultBase` class.
|
|
@@ -422,7 +409,7 @@ class SimulationTrialResultBase(
|
|
|
422
409
|
trial finishes.
|
|
423
410
|
"""
|
|
424
411
|
super().__init__(params, measurements, final_simulator_state=final_simulator_state)
|
|
425
|
-
self._merged_sim_state_cache:
|
|
412
|
+
self._merged_sim_state_cache: TSimulationState | None = None
|
|
426
413
|
|
|
427
414
|
def get_state_containing_qubit(self, qubit: cirq.Qid) -> TSimulationState:
|
|
428
415
|
"""Returns the independent state space containing the qubit.
|
|
@@ -437,7 +424,7 @@ class SimulationTrialResultBase(
|
|
|
437
424
|
def _get_substates(self) -> Sequence[TSimulationState]:
|
|
438
425
|
state = self._final_simulator_state
|
|
439
426
|
if isinstance(state, SimulationProductState):
|
|
440
|
-
substates:
|
|
427
|
+
substates: dict[TSimulationState, int] = {}
|
|
441
428
|
for q in state.qubits:
|
|
442
429
|
substates[self.get_state_containing_qubit(q)] = 0
|
|
443
430
|
substates[state[None]] = 0
|
cirq/sim/simulator_base_test.py
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
from __future__ import annotations
|
|
16
16
|
|
|
17
17
|
import math
|
|
18
|
-
from typing import Any,
|
|
18
|
+
from typing import Any, Sequence
|
|
19
19
|
|
|
20
20
|
import numpy as np
|
|
21
21
|
import pytest
|
|
@@ -33,7 +33,7 @@ class CountingState(cirq.qis.QuantumStateRepresentation):
|
|
|
33
33
|
|
|
34
34
|
def measure(
|
|
35
35
|
self, axes: Sequence[int], seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None
|
|
36
|
-
) ->
|
|
36
|
+
) -> list[int]:
|
|
37
37
|
self.measurement_count += 1
|
|
38
38
|
return [self.gate_count]
|
|
39
39
|
|
|
@@ -47,7 +47,7 @@ class CountingState(cirq.qis.QuantumStateRepresentation):
|
|
|
47
47
|
|
|
48
48
|
def factor(
|
|
49
49
|
self, axes: Sequence[int], *, validate=True, atol=1e-07
|
|
50
|
-
) ->
|
|
50
|
+
) -> tuple[CountingState, CountingState]:
|
|
51
51
|
return CountingState(
|
|
52
52
|
self.data, self.gate_count, self.measurement_count, self.copy_count
|
|
53
53
|
), CountingState(self.data)
|
|
@@ -98,11 +98,11 @@ class SplittableCountingSimulationState(CountingSimulationState):
|
|
|
98
98
|
class CountingStepResult(cirq.StepResultBase[CountingSimulationState]):
|
|
99
99
|
def sample(
|
|
100
100
|
self,
|
|
101
|
-
qubits:
|
|
101
|
+
qubits: list[cirq.Qid],
|
|
102
102
|
repetitions: int = 1,
|
|
103
103
|
seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None,
|
|
104
104
|
) -> np.ndarray:
|
|
105
|
-
measurements:
|
|
105
|
+
measurements: list[list[int]] = []
|
|
106
106
|
for _ in range(repetitions):
|
|
107
107
|
measurements.append(self._merged_sim_state._perform_measurement(qubits))
|
|
108
108
|
return np.array(measurements, dtype=int)
|
|
@@ -134,7 +134,7 @@ class CountingSimulator(
|
|
|
134
134
|
def _create_simulator_trial_result(
|
|
135
135
|
self,
|
|
136
136
|
params: cirq.ParamResolver,
|
|
137
|
-
measurements:
|
|
137
|
+
measurements: dict[str, np.ndarray],
|
|
138
138
|
final_simulator_state: cirq.SimulationStateBase[CountingSimulationState],
|
|
139
139
|
) -> CountingTrialResult:
|
|
140
140
|
return CountingTrialResult(
|
cirq/sim/simulator_test.py
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
from __future__ import annotations
|
|
18
18
|
|
|
19
19
|
import abc
|
|
20
|
-
from typing import Any,
|
|
20
|
+
from typing import Any, Generic, Sequence
|
|
21
21
|
from unittest import mock
|
|
22
22
|
|
|
23
23
|
import duet
|
|
@@ -41,10 +41,10 @@ from cirq.sim.simulator import (
|
|
|
41
41
|
class FakeSimulatesSamples(SimulatesSamples):
|
|
42
42
|
"""A SimulatesSamples that returns specified values from _run."""
|
|
43
43
|
|
|
44
|
-
def __init__(self, run_output:
|
|
44
|
+
def __init__(self, run_output: dict[str, np.ndarray]):
|
|
45
45
|
self._run_output = run_output
|
|
46
46
|
|
|
47
|
-
def _run(self, *args, **kwargs) ->
|
|
47
|
+
def _run(self, *args, **kwargs) -> dict[str, np.ndarray]:
|
|
48
48
|
return self._run_output
|
|
49
49
|
|
|
50
50
|
|
|
@@ -76,7 +76,7 @@ class SimulatesIntermediateStateImpl(
|
|
|
76
76
|
def _create_simulator_trial_result(
|
|
77
77
|
self,
|
|
78
78
|
params: study.ParamResolver,
|
|
79
|
-
measurements:
|
|
79
|
+
measurements: dict[str, np.ndarray],
|
|
80
80
|
final_simulator_state: cirq.SimulationStateBase[TSimulationState],
|
|
81
81
|
) -> SimulationTrialResult:
|
|
82
82
|
"""This method creates a default trial result.
|
|
@@ -469,12 +469,12 @@ def test_iter_definitions():
|
|
|
469
469
|
def simulate_expectation_values_sweep(
|
|
470
470
|
self,
|
|
471
471
|
program: cirq.AbstractCircuit,
|
|
472
|
-
observables:
|
|
472
|
+
observables: cirq.PauliSumLike | list[cirq.PauliSumLike],
|
|
473
473
|
params: study.Sweepable,
|
|
474
474
|
qubit_order: cirq.QubitOrderOrList = cirq.QubitOrder.DEFAULT,
|
|
475
475
|
initial_state: Any = None,
|
|
476
476
|
permit_terminal_measurements: bool = False,
|
|
477
|
-
) ->
|
|
477
|
+
) -> list[list[float]]:
|
|
478
478
|
return [[1.0]]
|
|
479
479
|
|
|
480
480
|
def simulate_sweep(
|
|
@@ -483,7 +483,7 @@ def test_iter_definitions():
|
|
|
483
483
|
params: study.Sweepable,
|
|
484
484
|
qubit_order: cirq.QubitOrderOrList = cirq.QubitOrder.DEFAULT,
|
|
485
485
|
initial_state: Any = None,
|
|
486
|
-
) ->
|
|
486
|
+
) -> list[SimulationTrialResult]:
|
|
487
487
|
return [mock_trial_result]
|
|
488
488
|
|
|
489
489
|
non_iter_sim = FakeNonIterSimulatorImpl()
|
cirq/sim/sparse_simulator.py
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
|
|
17
17
|
from __future__ import annotations
|
|
18
18
|
|
|
19
|
-
from typing import Any, Iterator,
|
|
19
|
+
from typing import Any, Iterator, Sequence, TYPE_CHECKING
|
|
20
20
|
|
|
21
21
|
import numpy as np
|
|
22
22
|
|
|
@@ -127,7 +127,7 @@ class Simulator(
|
|
|
127
127
|
def __init__(
|
|
128
128
|
self,
|
|
129
129
|
*,
|
|
130
|
-
dtype:
|
|
130
|
+
dtype: type[np.complexfloating] = np.complex64,
|
|
131
131
|
noise: cirq.NOISE_MODEL_LIKE = None,
|
|
132
132
|
seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None,
|
|
133
133
|
split_untangled_states: bool = True,
|
|
@@ -154,7 +154,7 @@ class Simulator(
|
|
|
154
154
|
|
|
155
155
|
def _create_partial_simulation_state(
|
|
156
156
|
self,
|
|
157
|
-
initial_state:
|
|
157
|
+
initial_state: cirq.STATE_VECTOR_LIKE | cirq.StateVectorSimulationState,
|
|
158
158
|
qubits: Sequence[cirq.Qid],
|
|
159
159
|
classical_data: cirq.ClassicalDataStore,
|
|
160
160
|
):
|
|
@@ -192,12 +192,12 @@ class Simulator(
|
|
|
192
192
|
def simulate_expectation_values_sweep_iter(
|
|
193
193
|
self,
|
|
194
194
|
program: cirq.AbstractCircuit,
|
|
195
|
-
observables:
|
|
195
|
+
observables: cirq.PauliSumLike | list[cirq.PauliSumLike],
|
|
196
196
|
params: cirq.Sweepable,
|
|
197
197
|
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
198
198
|
initial_state: Any = None,
|
|
199
199
|
permit_terminal_measurements: bool = False,
|
|
200
|
-
) -> Iterator[
|
|
200
|
+
) -> Iterator[list[float]]:
|
|
201
201
|
if not permit_terminal_measurements and program.are_any_measurements_terminal():
|
|
202
202
|
raise ValueError(
|
|
203
203
|
'Provided circuit has terminal measurements, which may '
|
|
@@ -206,7 +206,7 @@ class Simulator(
|
|
|
206
206
|
)
|
|
207
207
|
qubit_order = ops.QubitOrder.as_qubit_order(qubit_order)
|
|
208
208
|
qmap = {q: i for i, q in enumerate(qubit_order.order_for(program.all_qubits()))}
|
|
209
|
-
if not isinstance(observables,
|
|
209
|
+
if not isinstance(observables, list):
|
|
210
210
|
observables = [observables]
|
|
211
211
|
pslist = [ops.PauliSum.wrap(pslike) for pslike in observables]
|
|
212
212
|
yield from (
|
|
@@ -225,7 +225,7 @@ class SparseSimulatorStep(
|
|
|
225
225
|
def __init__(
|
|
226
226
|
self,
|
|
227
227
|
sim_state: cirq.SimulationStateBase[cirq.StateVectorSimulationState],
|
|
228
|
-
dtype:
|
|
228
|
+
dtype: type[np.complexfloating] = np.complex64,
|
|
229
229
|
):
|
|
230
230
|
"""Results of a step of the simulator.
|
|
231
231
|
|
|
@@ -237,7 +237,7 @@ class SparseSimulatorStep(
|
|
|
237
237
|
qubit_map = {q: i for i, q in enumerate(sim_state.qubits)}
|
|
238
238
|
super().__init__(sim_state=sim_state, qubit_map=qubit_map)
|
|
239
239
|
self._dtype = dtype
|
|
240
|
-
self._state_vector:
|
|
240
|
+
self._state_vector: np.ndarray | None = None
|
|
241
241
|
|
|
242
242
|
def state_vector(self, copy: bool = False):
|
|
243
243
|
"""Return the state vector at this point in the computation.
|
cirq/sim/state_vector.py
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
from __future__ import annotations
|
|
18
18
|
|
|
19
19
|
import abc
|
|
20
|
-
from typing import
|
|
20
|
+
from typing import Mapping, Sequence, TYPE_CHECKING
|
|
21
21
|
|
|
22
22
|
import numpy as np
|
|
23
23
|
|
|
@@ -34,7 +34,7 @@ if TYPE_CHECKING:
|
|
|
34
34
|
class StateVectorMixin:
|
|
35
35
|
"""A mixin that provide methods for objects that have a state vector."""
|
|
36
36
|
|
|
37
|
-
def __init__(self, qubit_map:
|
|
37
|
+
def __init__(self, qubit_map: Mapping[cirq.Qid, int] | None = None, *args, **kwargs):
|
|
38
38
|
"""Inits StateVectorMixin.
|
|
39
39
|
|
|
40
40
|
Args:
|
|
@@ -53,7 +53,7 @@ class StateVectorMixin:
|
|
|
53
53
|
def qubit_map(self) -> Mapping[cirq.Qid, int]:
|
|
54
54
|
return self._qubit_map
|
|
55
55
|
|
|
56
|
-
def _qid_shape_(self) ->
|
|
56
|
+
def _qid_shape_(self) -> tuple[int, ...]:
|
|
57
57
|
if self._qid_shape is None:
|
|
58
58
|
return NotImplemented
|
|
59
59
|
return self._qid_shape
|
|
@@ -105,7 +105,7 @@ class StateVectorMixin:
|
|
|
105
105
|
and non-zero floats of the specified accuracy."""
|
|
106
106
|
return qis.dirac_notation(self.state_vector(), decimals, qid_shape=self._qid_shape)
|
|
107
107
|
|
|
108
|
-
def density_matrix_of(self, qubits:
|
|
108
|
+
def density_matrix_of(self, qubits: list[cirq.Qid] | None = None) -> np.ndarray:
|
|
109
109
|
r"""Returns the density matrix of the state.
|
|
110
110
|
|
|
111
111
|
Calculate the density matrix for the system on the qubits provided.
|
|
@@ -172,7 +172,7 @@ def sample_state_vector(
|
|
|
172
172
|
state_vector: np.ndarray,
|
|
173
173
|
indices: Sequence[int],
|
|
174
174
|
*, # Force keyword args
|
|
175
|
-
qid_shape:
|
|
175
|
+
qid_shape: tuple[int, ...] | None = None,
|
|
176
176
|
repetitions: int = 1,
|
|
177
177
|
seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None,
|
|
178
178
|
) -> np.ndarray:
|
|
@@ -237,10 +237,10 @@ def measure_state_vector(
|
|
|
237
237
|
state_vector: np.ndarray,
|
|
238
238
|
indices: Sequence[int],
|
|
239
239
|
*, # Force keyword args
|
|
240
|
-
qid_shape:
|
|
241
|
-
out:
|
|
240
|
+
qid_shape: tuple[int, ...] | None = None,
|
|
241
|
+
out: np.ndarray | None = None,
|
|
242
242
|
seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None,
|
|
243
|
-
) ->
|
|
243
|
+
) -> tuple[list[int], np.ndarray]:
|
|
244
244
|
"""Performs a measurement of the state in the computational basis.
|
|
245
245
|
|
|
246
246
|
This does not modify `state` unless the optional `out` is `state`.
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
|
|
17
17
|
from __future__ import annotations
|
|
18
18
|
|
|
19
|
-
from typing import Any, Callable,
|
|
19
|
+
from typing import Any, Callable, Sequence, TYPE_CHECKING
|
|
20
20
|
|
|
21
21
|
import numpy as np
|
|
22
22
|
|
|
@@ -32,7 +32,7 @@ if TYPE_CHECKING:
|
|
|
32
32
|
class _BufferedStateVector(qis.QuantumStateRepresentation):
|
|
33
33
|
"""Contains the state vector and buffer for efficient state evolution."""
|
|
34
34
|
|
|
35
|
-
def __init__(self, state_vector: np.ndarray, buffer:
|
|
35
|
+
def __init__(self, state_vector: np.ndarray, buffer: np.ndarray | None = None):
|
|
36
36
|
"""Initializes the object with the inputs.
|
|
37
37
|
|
|
38
38
|
This initializer creates the buffer if necessary.
|
|
@@ -53,10 +53,10 @@ class _BufferedStateVector(qis.QuantumStateRepresentation):
|
|
|
53
53
|
def create(
|
|
54
54
|
cls,
|
|
55
55
|
*,
|
|
56
|
-
initial_state:
|
|
57
|
-
qid_shape:
|
|
58
|
-
dtype:
|
|
59
|
-
buffer:
|
|
56
|
+
initial_state: np.ndarray | cirq.STATE_VECTOR_LIKE = 0,
|
|
57
|
+
qid_shape: tuple[int, ...] | None = None,
|
|
58
|
+
dtype: type[np.complexfloating] | None = None,
|
|
59
|
+
buffer: np.ndarray | None = None,
|
|
60
60
|
):
|
|
61
61
|
"""Initializes the object with the inputs.
|
|
62
62
|
|
|
@@ -116,7 +116,7 @@ class _BufferedStateVector(qis.QuantumStateRepresentation):
|
|
|
116
116
|
|
|
117
117
|
def factor(
|
|
118
118
|
self, axes: Sequence[int], *, validate=True, atol=1e-07
|
|
119
|
-
) ->
|
|
119
|
+
) -> tuple[_BufferedStateVector, _BufferedStateVector]:
|
|
120
120
|
"""Factors a state vector into two independent state vectors.
|
|
121
121
|
|
|
122
122
|
This function should only be called on state vectors that are known to be separable, such
|
|
@@ -179,7 +179,7 @@ class _BufferedStateVector(qis.QuantumStateRepresentation):
|
|
|
179
179
|
self._swap_target_tensor_for(new_target_tensor)
|
|
180
180
|
return True
|
|
181
181
|
|
|
182
|
-
def apply_mixture(self, action: Any, axes: Sequence[int], prng) ->
|
|
182
|
+
def apply_mixture(self, action: Any, axes: Sequence[int], prng) -> int | None:
|
|
183
183
|
"""Apply mixture to state.
|
|
184
184
|
|
|
185
185
|
Args:
|
|
@@ -201,7 +201,7 @@ class _BufferedStateVector(qis.QuantumStateRepresentation):
|
|
|
201
201
|
self._swap_target_tensor_for(self._buffer)
|
|
202
202
|
return index
|
|
203
203
|
|
|
204
|
-
def apply_channel(self, action: Any, axes: Sequence[int], prng) ->
|
|
204
|
+
def apply_channel(self, action: Any, axes: Sequence[int], prng) -> int | None:
|
|
205
205
|
"""Apply channel to state.
|
|
206
206
|
|
|
207
207
|
Args:
|
|
@@ -257,7 +257,7 @@ class _BufferedStateVector(qis.QuantumStateRepresentation):
|
|
|
257
257
|
|
|
258
258
|
def measure(
|
|
259
259
|
self, axes: Sequence[int], seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None
|
|
260
|
-
) ->
|
|
260
|
+
) -> list[int]:
|
|
261
261
|
"""Measures the state vector.
|
|
262
262
|
|
|
263
263
|
Args:
|
|
@@ -320,12 +320,12 @@ class StateVectorSimulationState(SimulationState[_BufferedStateVector]):
|
|
|
320
320
|
def __init__(
|
|
321
321
|
self,
|
|
322
322
|
*,
|
|
323
|
-
available_buffer:
|
|
324
|
-
prng:
|
|
325
|
-
qubits:
|
|
326
|
-
initial_state:
|
|
327
|
-
dtype:
|
|
328
|
-
classical_data:
|
|
323
|
+
available_buffer: np.ndarray | None = None,
|
|
324
|
+
prng: np.random.RandomState | None = None,
|
|
325
|
+
qubits: Sequence[cirq.Qid] | None = None,
|
|
326
|
+
initial_state: np.ndarray | cirq.STATE_VECTOR_LIKE = 0,
|
|
327
|
+
dtype: type[np.complexfloating] = np.complex64,
|
|
328
|
+
classical_data: cirq.ClassicalDataStore | None = None,
|
|
329
329
|
):
|
|
330
330
|
"""Inits StateVectorSimulationState.
|
|
331
331
|
|
|
@@ -375,7 +375,7 @@ class StateVectorSimulationState(SimulationState[_BufferedStateVector]):
|
|
|
375
375
|
def _act_on_fallback_(
|
|
376
376
|
self, action: Any, qubits: Sequence[cirq.Qid], allow_decompose: bool = True
|
|
377
377
|
) -> bool:
|
|
378
|
-
strats:
|
|
378
|
+
strats: list[Callable[[Any, Any, Sequence[cirq.Qid]], bool]] = [
|
|
379
379
|
_strat_act_on_state_vector_from_apply_unitary,
|
|
380
380
|
_strat_act_on_state_vector_from_mixture,
|
|
381
381
|
_strat_act_on_state_vector_from_channel,
|