diff --git a/cirq-core/cirq/circuits/circuit.py b/cirq-core/cirq/circuits/circuit.py index 78a9765f8c7..82d5fa44c87 100644 --- a/cirq-core/cirq/circuits/circuit.py +++ b/cirq-core/cirq/circuits/circuit.py @@ -64,6 +64,7 @@ if TYPE_CHECKING: import cirq + from numpy.typing import DTypeLike _TGate = TypeVar('_TGate', bound='cirq.Gate') @@ -998,7 +999,7 @@ def unitary( qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT, qubits_that_should_be_present: Iterable['cirq.Qid'] = (), ignore_terminal_measurements: bool = True, - dtype: Type[np.number] = np.complex64, + dtype: 'DTypeLike' = np.complex64, ) -> np.ndarray: """Converts the circuit into a unitary matrix, if possible. @@ -1088,7 +1089,7 @@ def final_state_vector( qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT, qubits_that_should_be_present: Iterable['cirq.Qid'] = (), ignore_terminal_measurements: Optional[bool] = None, - dtype: Optional[Type[np.number]] = None, + dtype: Optional['DTypeLike'] = None, param_resolver: 'cirq.ParamResolverOrSimilarType' = None, seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None, ) -> np.ndarray: @@ -2655,7 +2656,7 @@ def _apply_unitary_circuit( circuit: 'cirq.AbstractCircuit', state: np.ndarray, qubits: Tuple['cirq.Qid', ...], - dtype: Type[np.number], + dtype: 'DTypeLike', ) -> np.ndarray: """Applies a circuit's unitary effect to the given vector or matrix. diff --git a/cirq-core/cirq/sim/density_matrix_simulation_state.py b/cirq-core/cirq/sim/density_matrix_simulation_state.py index 1486be355b4..ad1c969364e 100644 --- a/cirq-core/cirq/sim/density_matrix_simulation_state.py +++ b/cirq-core/cirq/sim/density_matrix_simulation_state.py @@ -13,7 +13,7 @@ # limitations under the License. """Objects and methods for acting efficiently on a density matrix.""" -from typing import Any, Callable, List, Optional, Sequence, Tuple, TYPE_CHECKING, Type, Union +from typing import Any, Callable, List, Optional, Sequence, Tuple, TYPE_CHECKING, Union import numpy as np @@ -252,7 +252,7 @@ def __init__( prng: Optional[np.random.RandomState] = None, qubits: Optional[Sequence['cirq.Qid']] = None, initial_state: Union[np.ndarray, 'cirq.STATE_VECTOR_LIKE'] = 0, - dtype: Type[np.number] = np.complex64, + dtype: 'DTypeLike' = np.complex64, classical_data: Optional['cirq.ClassicalDataStore'] = None, ): """Inits DensityMatrixSimulationState. diff --git a/cirq-core/cirq/sim/density_matrix_simulator_test.py b/cirq-core/cirq/sim/density_matrix_simulator_test.py index ffe3eabb58c..42dffbf4d40 100644 --- a/cirq-core/cirq/sim/density_matrix_simulator_test.py +++ b/cirq-core/cirq/sim/density_matrix_simulator_test.py @@ -13,13 +13,14 @@ # limitations under the License. import itertools import random -from typing import Type from unittest import mock import numpy as np import pytest import sympy +from numpy.typing import DTypeLike + import cirq import cirq.testing @@ -53,7 +54,7 @@ def test_invalid_dtype(): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_no_measurements(dtype: Type[np.number], split: bool): +def test_run_no_measurements(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) @@ -64,7 +65,7 @@ def test_run_no_measurements(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_no_results(dtype: Type[np.number], split: bool): +def test_run_no_results(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) @@ -75,7 +76,7 @@ def test_run_no_results(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_empty_circuit(dtype: Type[np.number], split: bool): +def test_run_empty_circuit(dtype: DTypeLike, split: bool): simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) with pytest.raises(ValueError, match="no measurements"): simulator.run(cirq.Circuit()) @@ -83,7 +84,7 @@ def test_run_empty_circuit(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_bit_flips(dtype: Type[np.number], split: bool): +def test_run_bit_flips(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -97,7 +98,7 @@ def test_run_bit_flips(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_bit_flips_with_dephasing(dtype: Type[np.number], split: bool): +def test_run_bit_flips_with_dephasing(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -111,7 +112,7 @@ def test_run_bit_flips_with_dephasing(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_qudit_increments(dtype: Type[np.number], split: bool): +def test_run_qudit_increments(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQid.for_qid_shape((3, 4)) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1, 2]: @@ -130,7 +131,7 @@ def test_run_qudit_increments(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_not_channel_op(dtype: Type[np.number], split: bool): +def test_run_not_channel_op(dtype: DTypeLike, split: bool): class BadOp(cirq.Operation): def __init__(self, qubits): self._qubits = qubits @@ -152,7 +153,7 @@ def with_qubits(self, *new_qubits): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_mixture(dtype: Type[np.number], split: bool): +def test_run_mixture(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) circuit = cirq.Circuit(cirq.bit_flip(0.5)(q0), cirq.measure(q0), cirq.measure(q1)) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) @@ -166,7 +167,7 @@ def test_run_mixture(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_qudit_mixture(dtype: Type[np.number], split: bool): +def test_run_qudit_mixture(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQid.for_qid_shape((3, 2)) mixture = _TestMixture( [ @@ -187,7 +188,7 @@ def test_run_qudit_mixture(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_channel(dtype: Type[np.number], split: bool): +def test_run_channel(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) circuit = cirq.Circuit( cirq.X(q0), cirq.amplitude_damp(0.5)(q0), cirq.measure(q0), cirq.measure(q1) @@ -204,7 +205,7 @@ def test_run_channel(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_decomposable_channel(dtype: Type[np.number], split: bool): +def test_run_decomposable_channel(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) circuit = cirq.Circuit( @@ -225,7 +226,7 @@ def test_run_decomposable_channel(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_qudit_channel(dtype: Type[np.number], split: bool): +def test_run_qudit_channel(dtype: DTypeLike, split: bool): class TestChannel(cirq.Gate): def _qid_shape_(self): return (3,) @@ -257,7 +258,7 @@ def _kraus_(self): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_measure_at_end_no_repetitions(dtype: Type[np.number], split: bool): +def test_run_measure_at_end_no_repetitions(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) with mock.patch.object(simulator, '_core_iterator', wraps=simulator._core_iterator) as mock_sim: @@ -276,7 +277,7 @@ def test_run_measure_at_end_no_repetitions(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_repetitions_measure_at_end(dtype: Type[np.number], split: bool): +def test_run_repetitions_measure_at_end(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) with mock.patch.object(simulator, '_core_iterator', wraps=simulator._core_iterator) as mock_sim: @@ -295,7 +296,7 @@ def test_run_repetitions_measure_at_end(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_qudits_repetitions_measure_at_end(dtype: Type[np.number], split: bool): +def test_run_qudits_repetitions_measure_at_end(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQid.for_qid_shape((2, 3)) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) with mock.patch.object(simulator, '_core_iterator', wraps=simulator._core_iterator) as mock_sim: @@ -317,7 +318,7 @@ def test_run_qudits_repetitions_measure_at_end(dtype: Type[np.number], split: bo @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_measurement_not_terminal_no_repetitions(dtype: Type[np.number], split: bool): +def test_run_measurement_not_terminal_no_repetitions(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) with mock.patch.object(simulator, '_core_iterator', wraps=simulator._core_iterator) as mock_sim: @@ -341,7 +342,7 @@ def test_run_measurement_not_terminal_no_repetitions(dtype: Type[np.number], spl @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_repetitions_measurement_not_terminal(dtype: Type[np.number], split: bool): +def test_run_repetitions_measurement_not_terminal(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) with mock.patch.object(simulator, '_core_iterator', wraps=simulator._core_iterator) as mock_sim: @@ -365,7 +366,7 @@ def test_run_repetitions_measurement_not_terminal(dtype: Type[np.number], split: @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_qudits_repetitions_measurement_not_terminal(dtype: Type[np.number], split: bool): +def test_run_qudits_repetitions_measurement_not_terminal(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQid.for_qid_shape((2, 3)) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) with mock.patch.object(simulator, '_core_iterator', wraps=simulator._core_iterator) as mock_sim: @@ -389,7 +390,7 @@ def test_run_qudits_repetitions_measurement_not_terminal(dtype: Type[np.number], @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_param_resolver(dtype: Type[np.number], split: bool): +def test_run_param_resolver(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -409,7 +410,7 @@ def test_run_param_resolver(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_correlations(dtype: Type[np.number], split: bool): +def test_run_correlations(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) circuit = cirq.Circuit(cirq.H(q0), cirq.CNOT(q0, q1), cirq.measure(q0, q1)) @@ -421,7 +422,7 @@ def test_run_correlations(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_measure_multiple_qubits(dtype: Type[np.number], split: bool): +def test_run_measure_multiple_qubits(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -433,7 +434,7 @@ def test_run_measure_multiple_qubits(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_measure_multiple_qudits(dtype: Type[np.number], split: bool): +def test_run_measure_multiple_qudits(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQid.for_qid_shape((2, 3)) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -447,7 +448,7 @@ def test_run_measure_multiple_qudits(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_sweeps_param_resolvers(dtype: Type[np.number], split: bool): +def test_run_sweeps_param_resolvers(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -473,7 +474,7 @@ def test_run_sweeps_param_resolvers(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_no_circuit(dtype: Type[np.number], split: bool): +def test_simulate_no_circuit(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) circuit = cirq.Circuit() @@ -486,7 +487,7 @@ def test_simulate_no_circuit(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate(dtype: Type[np.number], split: bool): +def test_simulate(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) circuit = cirq.Circuit(cirq.H(q0), cirq.H(q1)) @@ -497,7 +498,7 @@ def test_simulate(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_qudits(dtype: Type[np.number], split: bool): +def test_simulate_qudits(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQid.for_qid_shape((2, 3)) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) circuit = cirq.Circuit(cirq.H(q0), cirq.XPowGate(dimension=3)(q1) ** 2) @@ -511,7 +512,7 @@ def test_simulate_qudits(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) def test_reset_one_qubit_does_not_affect_partial_trace_of_other_qubits( - dtype: Type[np.number], split: bool + dtype: DTypeLike, split: bool ): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) @@ -530,7 +531,7 @@ def test_reset_one_qubit_does_not_affect_partial_trace_of_other_qubits( [cirq.testing.random_circuit(cirq.LineQubit.range(4), 5, 0.9) for _ in range(20)], ), ) -def test_simulate_compare_to_state_vector_simulator(dtype: Type[np.number], circuit): +def test_simulate_compare_to_state_vector_simulator(dtype: DTypeLike, circuit): qubits = cirq.LineQubit.range(4) pure_result = ( cirq.Simulator(dtype=dtype).simulate(circuit, qubit_order=qubits).density_matrix_of() @@ -546,7 +547,7 @@ def test_simulate_compare_to_state_vector_simulator(dtype: Type[np.number], circ @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_bit_flips(dtype: Type[np.number], split: bool): +def test_simulate_bit_flips(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -563,7 +564,7 @@ def test_simulate_bit_flips(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_qudit_increments(dtype: Type[np.number], split: bool): +def test_simulate_qudit_increments(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQid.for_qid_shape((2, 3)) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -583,7 +584,7 @@ def test_simulate_qudit_increments(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_initial_state(dtype: Type[np.number], split: bool): +def test_simulate_initial_state(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -597,7 +598,7 @@ def test_simulate_initial_state(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulation_state(dtype: Type[np.number], split: bool): +def test_simulation_state(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -624,7 +625,7 @@ def test_simulate_tps_initial_state(): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_initial_qudit_state(dtype: Type[np.number], split: bool): +def test_simulate_initial_qudit_state(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQid.for_qid_shape((3, 4)) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1, 2]: @@ -644,7 +645,7 @@ def test_simulate_initial_qudit_state(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_qubit_order(dtype: Type[np.number], split: bool): +def test_simulate_qubit_order(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -658,7 +659,7 @@ def test_simulate_qubit_order(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_param_resolver(dtype: Type[np.number], split: bool): +def test_simulate_param_resolver(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -677,7 +678,7 @@ def test_simulate_param_resolver(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_measure_multiple_qubits(dtype: Type[np.number], split: bool): +def test_simulate_measure_multiple_qubits(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -689,7 +690,7 @@ def test_simulate_measure_multiple_qubits(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_measure_multiple_qudits(dtype: Type[np.number], split: bool): +def test_simulate_measure_multiple_qudits(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQid.for_qid_shape((2, 3)) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -703,7 +704,7 @@ def test_simulate_measure_multiple_qudits(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_sweeps_param_resolver(dtype: Type[np.number], split: bool): +def test_simulate_sweeps_param_resolver(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -730,7 +731,7 @@ def test_simulate_sweeps_param_resolver(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_moment_steps(dtype: Type[np.number], split: bool): +def test_simulate_moment_steps(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) circuit = cirq.Circuit(cirq.H(q0), cirq.H(q1), cirq.H(q0), cirq.H(q1)) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) @@ -744,7 +745,7 @@ def test_simulate_moment_steps(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_moment_steps_qudits(dtype: Type[np.number], split: bool): +def test_simulate_moment_steps_qudits(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQid.for_qid_shape((2, 3)) circuit = cirq.Circuit( cirq.XPowGate(dimension=2)(q0), @@ -765,7 +766,7 @@ def test_simulate_moment_steps_qudits(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_moment_steps_empty_circuit(dtype: Type[np.number], split: bool): +def test_simulate_moment_steps_empty_circuit(dtype: DTypeLike, split: bool): circuit = cirq.Circuit() simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) step = None @@ -777,7 +778,7 @@ def test_simulate_moment_steps_empty_circuit(dtype: Type[np.number], split: bool @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_moment_steps_sample(dtype: Type[np.number], split: bool): +def test_simulate_moment_steps_sample(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) circuit = cirq.Circuit(cirq.H(q0), cirq.CNOT(q0, q1)) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) @@ -798,7 +799,7 @@ def test_simulate_moment_steps_sample(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_moment_steps_sample_qudits(dtype: Type[np.number], split: bool): +def test_simulate_moment_steps_sample_qudits(dtype: DTypeLike, split: bool): class TestGate(cirq.Gate): """Swaps the 2nd qid |0> and |2> states when the 1st is |1>.""" @@ -827,7 +828,7 @@ def _apply_unitary_(self, args: cirq.ApplyUnitaryArgs): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_moment_steps_intermediate_measurement(dtype: Type[np.number], split: bool): +def test_simulate_moment_steps_intermediate_measurement(dtype: DTypeLike, split: bool): q0 = cirq.LineQubit(0) circuit = cirq.Circuit(cirq.H(q0), cirq.measure(q0), cirq.H(q0)) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) diff --git a/cirq-core/cirq/sim/density_matrix_utils.py b/cirq-core/cirq/sim/density_matrix_utils.py index c04d69f2c4d..8e0ea6ffca9 100644 --- a/cirq-core/cirq/sim/density_matrix_utils.py +++ b/cirq-core/cirq/sim/density_matrix_utils.py @@ -94,7 +94,7 @@ def measure_density_matrix( density_matrix: np.ndarray, indices: Sequence[int], qid_shape: Optional[Tuple[int, ...]] = None, - out: np.ndarray = None, + out: Optional[np.ndarray] = None, seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None, ) -> Tuple[List[int], np.ndarray]: """Performs a measurement of the density matrix in the computational basis. @@ -142,13 +142,16 @@ def measure_density_matrix( num_qubits = len(qid_shape) meas_shape = _indices_shape(qid_shape, indices) + arrout: np.ndarray = ( + np.copy(density_matrix) + if out is None + else density_matrix + if out is density_matrix + else (np.copyto(dst=out, src=density_matrix), out)[-1] + ) + if len(indices) == 0: - if out is None: - out = np.copy(density_matrix) - elif out is not density_matrix: - np.copyto(dst=out, src=density_matrix) - return ([], out) - # Final else: if out is matrix then matrix will be modified in place. + return ([], arrout) prng = value.parse_random_state(seed) @@ -169,21 +172,15 @@ def measure_density_matrix( # Remove ellipses from last element of mask[result_slice * 2] = False - if out is None: - out = np.copy(density_matrix) - elif out is not density_matrix: - np.copyto(dst=out, src=density_matrix) - # Final else: if out is matrix then matrix will be modified in place. - # Potentially reshape to tensor, and then set masked values to 0. - out.shape = qid_shape * 2 - out[mask] = 0 + arrout.shape = qid_shape * 2 + arrout[mask] = 0 # Restore original shape (if necessary) and renormalize. - out.shape = initial_shape - out /= probs[result] + arrout.shape = initial_shape + arrout /= probs[result] - return measurement_bits, out + return measurement_bits, arrout def _probs( diff --git a/cirq-core/cirq/sim/mux.py b/cirq-core/cirq/sim/mux.py index c0e357284de..288e7454af5 100644 --- a/cirq-core/cirq/sim/mux.py +++ b/cirq-core/cirq/sim/mux.py @@ -17,7 +17,7 @@ Filename is a reference to multiplexing. """ -from typing import cast, List, Optional, Sequence, Type, TYPE_CHECKING, Union +from typing import cast, List, Optional, Sequence, TYPE_CHECKING, Union import numpy as np @@ -29,6 +29,7 @@ if TYPE_CHECKING: import cirq + from numpy.typing import DTypeLike CIRCUIT_LIKE = Union[circuits.Circuit, ops.Gate, ops.OP_TREE] document( @@ -52,7 +53,7 @@ def sample( noise: 'cirq.NOISE_MODEL_LIKE' = None, param_resolver: Optional['cirq.ParamResolver'] = None, repetitions: int = 1, - dtype: Type[np.number] = np.complex64, + dtype: 'DTypeLike' = np.complex64, seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None, ) -> 'cirq.Result': """Simulates sampling from the given circuit. @@ -107,7 +108,7 @@ def final_state_vector( param_resolver: 'cirq.ParamResolverOrSimilarType' = None, qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT, ignore_terminal_measurements: bool = False, - dtype: Type[np.number] = np.complex64, + dtype: 'DTypeLike' = np.complex64, seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None, ) -> 'np.ndarray': """Returns the state vector resulting from acting operations on a state. @@ -177,7 +178,7 @@ def sample_sweep( *, noise: 'cirq.NOISE_MODEL_LIKE' = None, repetitions: int = 1, - dtype: Type[np.number] = np.complex64, + dtype: 'DTypeLike' = np.complex64, seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None, ) -> Sequence['cirq.Result']: """Runs the supplied Circuit, mimicking quantum hardware. @@ -223,7 +224,7 @@ def final_density_matrix( initial_state: 'cirq.STATE_VECTOR_LIKE' = 0, param_resolver: 'cirq.ParamResolverOrSimilarType' = None, qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT, - dtype: Type[np.number] = np.complex64, + dtype: 'DTypeLike' = np.complex64, seed: Optional[Union[int, np.random.RandomState]] = None, ignore_measurement_results: bool = True, ) -> 'np.ndarray': diff --git a/cirq-core/cirq/sim/simulator_base.py b/cirq-core/cirq/sim/simulator_base.py index 38a7941a0be..18a6b2d19d9 100644 --- a/cirq-core/cirq/sim/simulator_base.py +++ b/cirq-core/cirq/sim/simulator_base.py @@ -26,7 +26,6 @@ Optional, Sequence, Tuple, - Type, TypeVar, TYPE_CHECKING, ) @@ -50,6 +49,7 @@ if TYPE_CHECKING: import cirq + from numpy.typing import DTypeLike TStepResultBase = TypeVar('TStepResultBase', bound='StepResultBase') @@ -93,7 +93,7 @@ class SimulatorBase( def __init__( self, *, - dtype: Type[np.number] = np.complex64, + dtype: 'DTypeLike' = np.complex64, noise: 'cirq.NOISE_MODEL_LIKE' = None, seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None, split_untangled_states: bool = False, diff --git a/cirq-core/cirq/sim/sparse_simulator.py b/cirq-core/cirq/sim/sparse_simulator.py index d3c32d2022e..932c231526e 100644 --- a/cirq-core/cirq/sim/sparse_simulator.py +++ b/cirq-core/cirq/sim/sparse_simulator.py @@ -14,7 +14,7 @@ """A simulator that uses numpy's einsum for sparse matrix operations.""" -from typing import Any, Iterator, List, Type, TYPE_CHECKING, Union, Sequence, Optional +from typing import Any, Iterator, List, TYPE_CHECKING, Union, Sequence, Optional import numpy as np @@ -127,7 +127,7 @@ class Simulator( def __init__( self, *, - dtype: Type[np.number] = np.complex64, + dtype: 'DTypeLike' = np.complex64, noise: 'cirq.NOISE_MODEL_LIKE' = None, seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None, split_untangled_states: bool = True, diff --git a/cirq-core/cirq/sim/sparse_simulator_test.py b/cirq-core/cirq/sim/sparse_simulator_test.py index 14966a4bdd2..6bfc7aa3005 100644 --- a/cirq-core/cirq/sim/sparse_simulator_test.py +++ b/cirq-core/cirq/sim/sparse_simulator_test.py @@ -13,9 +13,9 @@ # limitations under the License. import itertools import random -from typing import Type from unittest import mock import numpy as np +from numpy.typing import DTypeLike import pytest import sympy @@ -29,7 +29,7 @@ def test_invalid_dtype(): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_no_measurements(dtype: Type[np.number], split: bool): +def test_run_no_measurements(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) @@ -40,7 +40,7 @@ def test_run_no_measurements(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_no_results(dtype: Type[np.number], split: bool): +def test_run_no_results(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) @@ -51,7 +51,7 @@ def test_run_no_results(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_empty_circuit(dtype: Type[np.number], split: bool): +def test_run_empty_circuit(dtype: DTypeLike, split: bool): simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) with pytest.raises(ValueError, match="no measurements"): simulator.run(cirq.Circuit()) @@ -59,7 +59,7 @@ def test_run_empty_circuit(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_reset(dtype: Type[np.number], split: bool): +def test_run_reset(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQid.for_qid_shape((2, 3)) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) circuit = cirq.Circuit( @@ -79,7 +79,7 @@ def test_run_reset(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_bit_flips(dtype: Type[np.number], split: bool): +def test_run_bit_flips(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -93,7 +93,7 @@ def test_run_bit_flips(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_measure_at_end_no_repetitions(dtype: Type[np.number], split: bool): +def test_run_measure_at_end_no_repetitions(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) with mock.patch.object(simulator, '_core_iterator', wraps=simulator._core_iterator) as mock_sim: @@ -119,7 +119,7 @@ def test_run_repetitions_terminal_measurement_stochastic(): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_repetitions_measure_at_end(dtype: Type[np.number], split: bool): +def test_run_repetitions_measure_at_end(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) with mock.patch.object(simulator, '_core_iterator', wraps=simulator._core_iterator) as mock_sim: @@ -139,7 +139,7 @@ def test_run_repetitions_measure_at_end(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_invert_mask_measure_not_terminal(dtype: Type[np.number], split: bool): +def test_run_invert_mask_measure_not_terminal(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) with mock.patch.object(simulator, '_core_iterator', wraps=simulator._core_iterator) as mock_sim: @@ -160,7 +160,7 @@ def test_run_invert_mask_measure_not_terminal(dtype: Type[np.number], split: boo @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_partial_invert_mask_measure_not_terminal(dtype: Type[np.number], split: bool): +def test_run_partial_invert_mask_measure_not_terminal(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) with mock.patch.object(simulator, '_core_iterator', wraps=simulator._core_iterator) as mock_sim: @@ -181,7 +181,7 @@ def test_run_partial_invert_mask_measure_not_terminal(dtype: Type[np.number], sp @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_measurement_not_terminal_no_repetitions(dtype: Type[np.number], split: bool): +def test_run_measurement_not_terminal_no_repetitions(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) with mock.patch.object(simulator, '_core_iterator', wraps=simulator._core_iterator) as mock_sim: @@ -205,7 +205,7 @@ def test_run_measurement_not_terminal_no_repetitions(dtype: Type[np.number], spl @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_repetitions_measurement_not_terminal(dtype: Type[np.number], split: bool): +def test_run_repetitions_measurement_not_terminal(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) with mock.patch.object(simulator, '_core_iterator', wraps=simulator._core_iterator) as mock_sim: @@ -230,7 +230,7 @@ def test_run_repetitions_measurement_not_terminal(dtype: Type[np.number], split: @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_param_resolver(dtype: Type[np.number], split: bool): +def test_run_param_resolver(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -249,7 +249,7 @@ def test_run_param_resolver(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_mixture(dtype: Type[np.number], split: bool): +def test_run_mixture(dtype: DTypeLike, split: bool): q0 = cirq.LineQubit(0) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) circuit = cirq.Circuit(cirq.bit_flip(0.5)(q0), cirq.measure(q0)) @@ -259,7 +259,7 @@ def test_run_mixture(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_mixture_with_gates(dtype: Type[np.number], split: bool): +def test_run_mixture_with_gates(dtype: DTypeLike, split: bool): q0 = cirq.LineQubit(0) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split, seed=23) circuit = cirq.Circuit(cirq.H(q0), cirq.phase_flip(0.5)(q0), cirq.H(q0), cirq.measure(q0)) @@ -270,7 +270,7 @@ def test_run_mixture_with_gates(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_correlations(dtype: Type[np.number], split: bool): +def test_run_correlations(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) circuit = cirq.Circuit(cirq.H(q0), cirq.CNOT(q0, q1), cirq.measure(q0, q1)) @@ -282,7 +282,7 @@ def test_run_correlations(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_measure_multiple_qubits(dtype: Type[np.number], split: bool): +def test_run_measure_multiple_qubits(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -294,7 +294,7 @@ def test_run_measure_multiple_qubits(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_sweeps_param_resolvers(dtype: Type[np.number], split: bool): +def test_run_sweeps_param_resolvers(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -320,7 +320,7 @@ def test_run_sweeps_param_resolvers(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_random_unitary(dtype: Type[np.number], split: bool): +def test_simulate_random_unitary(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) for _ in range(10): @@ -336,7 +336,7 @@ def test_simulate_random_unitary(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_no_circuit(dtype: Type[np.number], split: bool): +def test_simulate_no_circuit(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) circuit = cirq.Circuit() @@ -347,7 +347,7 @@ def test_simulate_no_circuit(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate(dtype: Type[np.number], split: bool): +def test_simulate(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) circuit = cirq.Circuit(cirq.H(q0), cirq.H(q1)) @@ -369,7 +369,7 @@ def _mixture_(self): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_qudits(dtype: Type[np.number], split: bool): +def test_simulate_qudits(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQid.for_qid_shape((3, 4)) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) circuit = cirq.Circuit(cirq.XPowGate(dimension=3)(q0), cirq.XPowGate(dimension=4)(q1) ** 3) @@ -382,7 +382,7 @@ def test_simulate_qudits(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_mixtures(dtype: Type[np.number], split: bool): +def test_simulate_mixtures(dtype: DTypeLike, split: bool): q0 = cirq.LineQubit(0) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) circuit = cirq.Circuit(cirq.bit_flip(0.5)(q0), cirq.measure(q0)) @@ -400,7 +400,7 @@ def test_simulate_mixtures(dtype: Type[np.number], split: bool): @pytest.mark.parametrize( 'dtype, split', itertools.product([np.complex64, np.complex128], [True, False]) ) -def test_simulate_qudit_mixtures(dtype: Type[np.number], split: bool): +def test_simulate_qudit_mixtures(dtype: DTypeLike, split: bool): q0 = cirq.LineQid(0, 3) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) mixture = _TestMixture( @@ -426,7 +426,7 @@ def test_simulate_qudit_mixtures(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_bit_flips(dtype: Type[np.number], split: bool): +def test_simulate_bit_flips(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -443,7 +443,7 @@ def test_simulate_bit_flips(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_initial_state(dtype: Type[np.number], split: bool): +def test_simulate_initial_state(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -457,7 +457,7 @@ def test_simulate_initial_state(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulation_state(dtype: Type[np.number], split: bool): +def test_simulation_state(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -472,7 +472,7 @@ def test_simulation_state(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_qubit_order(dtype: Type[np.number], split: bool): +def test_simulate_qubit_order(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -486,7 +486,7 @@ def test_simulate_qubit_order(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_param_resolver(dtype: Type[np.number], split: bool): +def test_simulate_param_resolver(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -505,7 +505,7 @@ def test_simulate_param_resolver(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_measure_multiple_qubits(dtype: Type[np.number], split: bool): +def test_simulate_measure_multiple_qubits(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -517,7 +517,7 @@ def test_simulate_measure_multiple_qubits(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_sweeps_param_resolver(dtype: Type[np.number], split: bool): +def test_simulate_sweeps_param_resolver(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -544,7 +544,7 @@ def test_simulate_sweeps_param_resolver(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_moment_steps(dtype: Type[np.number], split: bool): +def test_simulate_moment_steps(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) circuit = cirq.Circuit(cirq.H(q0), cirq.H(q1), cirq.H(q0), cirq.H(q1)) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) @@ -568,7 +568,7 @@ def test_simulate_moment_steps_implicit_copy_deprecated(): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_moment_steps_empty_circuit(dtype: Type[np.number], split: bool): +def test_simulate_moment_steps_empty_circuit(dtype: DTypeLike, split: bool): circuit = cirq.Circuit() simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) step = None @@ -580,7 +580,7 @@ def test_simulate_moment_steps_empty_circuit(dtype: Type[np.number], split: bool @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_moment_steps_sample(dtype: Type[np.number], split: bool): +def test_simulate_moment_steps_sample(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) circuit = cirq.Circuit(cirq.H(q0), cirq.CNOT(q0, q1)) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) @@ -601,7 +601,7 @@ def test_simulate_moment_steps_sample(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_moment_steps_intermediate_measurement(dtype: Type[np.number], split: bool): +def test_simulate_moment_steps_intermediate_measurement(dtype: DTypeLike, split: bool): q0 = cirq.LineQubit(0) circuit = cirq.Circuit(cirq.H(q0), cirq.measure(q0), cirq.H(q0)) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) @@ -618,7 +618,7 @@ def test_simulate_moment_steps_intermediate_measurement(dtype: Type[np.number], @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_expectation_values(dtype: Type[np.number], split: bool): +def test_simulate_expectation_values(dtype: DTypeLike, split: bool): # Compare with test_expectation_from_state_vector_two_qubit_states # in file: cirq/ops/linear_combinations_test.py q0, q1 = cirq.LineQubit.range(2) @@ -643,7 +643,7 @@ def test_simulate_expectation_values(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_expectation_values_terminal_measure(dtype: Type[np.number], split: bool): +def test_simulate_expectation_values_terminal_measure(dtype: DTypeLike, split: bool): q0 = cirq.LineQubit(0) circuit = cirq.Circuit(cirq.H(q0), cirq.measure(q0)) obs = cirq.Z(q0) @@ -681,7 +681,7 @@ def test_simulate_expectation_values_terminal_measure(dtype: Type[np.number], sp @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_expectation_values_qubit_order(dtype: Type[np.number], split: bool): +def test_simulate_expectation_values_qubit_order(dtype: DTypeLike, split: bool): q0, q1, q2 = cirq.LineQubit.range(3) circuit = cirq.Circuit(cirq.H(q0), cirq.H(q1), cirq.X(q2)) obs = cirq.X(q0) + cirq.X(q1) - cirq.Z(q2) diff --git a/cirq-core/cirq/sim/state_vector_simulation_state.py b/cirq-core/cirq/sim/state_vector_simulation_state.py index 1111538f5ee..1bfd7d62226 100644 --- a/cirq-core/cirq/sim/state_vector_simulation_state.py +++ b/cirq-core/cirq/sim/state_vector_simulation_state.py @@ -13,7 +13,7 @@ # limitations under the License. """Objects and methods for acting efficiently on a state vector.""" -from typing import Any, Callable, List, Optional, Sequence, Tuple, TYPE_CHECKING, Type, Union +from typing import Any, Callable, List, Optional, Sequence, Tuple, TYPE_CHECKING, Union import numpy as np @@ -326,7 +326,7 @@ def __init__( prng: Optional[np.random.RandomState] = None, qubits: Optional[Sequence['cirq.Qid']] = None, initial_state: Union[np.ndarray, 'cirq.STATE_VECTOR_LIKE'] = 0, - dtype: Type[np.number] = np.complex64, + dtype: 'DTypeLike' = np.complex64, classical_data: Optional['cirq.ClassicalDataStore'] = None, ): """Inits StateVectorSimulationState. diff --git a/cirq-core/cirq/sim/state_vector_simulator.py b/cirq-core/cirq/sim/state_vector_simulator.py index 44c9aa1a10c..d2e9c86f0c8 100644 --- a/cirq-core/cirq/sim/state_vector_simulator.py +++ b/cirq-core/cirq/sim/state_vector_simulator.py @@ -14,18 +14,7 @@ """Abstract classes for simulations which keep track of state vector.""" import abc -from typing import ( - Any, - Dict, - Iterator, - Sequence, - TYPE_CHECKING, - Tuple, - Generic, - TypeVar, - Type, - Optional, -) +from typing import Any, Dict, Iterator, Sequence, TYPE_CHECKING, Tuple, Generic, TypeVar, Optional import numpy as np @@ -35,6 +24,7 @@ if TYPE_CHECKING: import cirq + from numpy.typing import DTypeLike TStateVectorStepResult = TypeVar('TStateVectorStepResult', bound='StateVectorStepResult') @@ -56,7 +46,7 @@ class SimulatesIntermediateStateVector( def __init__( self, *, - dtype: Type[np.number] = np.complex64, + dtype: 'DTypeLike' = np.complex64, noise: 'cirq.NOISE_MODEL_LIKE' = None, seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None, split_untangled_states: bool = False,