Skip to content

Commit 3c8b036

Browse files
authored
Use np.complexfloating for dtypes that should be complex (#5488)
This changes the type signature for dtypes that should only be `np.complex64` or `np.complex128`. There are also dtypes in `qis.states.py` but I've not changed these over, as they are often called the `dtype` of a `np.array`, and so they will often need to be cast. This fixes some `check/mypy --next` errors. Technically a breaking change in type signature (more restrictive), but we yell when types are not complex.
1 parent ca8585e commit 3c8b036

11 files changed

+127
-114
lines changed

cirq-core/cirq/circuits/circuit.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464

6565
if TYPE_CHECKING:
6666
import cirq
67-
from numpy.typing import DTypeLike
6867

6968

7069
_TGate = TypeVar('_TGate', bound='cirq.Gate')
@@ -999,7 +998,7 @@ def unitary(
999998
qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT,
1000999
qubits_that_should_be_present: Iterable['cirq.Qid'] = (),
10011000
ignore_terminal_measurements: bool = True,
1002-
dtype: 'DTypeLike' = np.complex64,
1001+
dtype: Type[np.complexfloating] = np.complex64,
10031002
) -> np.ndarray:
10041003
"""Converts the circuit into a unitary matrix, if possible.
10051004
@@ -1089,7 +1088,7 @@ def final_state_vector(
10891088
qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT,
10901089
qubits_that_should_be_present: Iterable['cirq.Qid'] = (),
10911090
ignore_terminal_measurements: Optional[bool] = None,
1092-
dtype: Optional['DTypeLike'] = None,
1091+
dtype: Optional[Type[np.complexfloating]] = None,
10931092
param_resolver: 'cirq.ParamResolverOrSimilarType' = None,
10941093
seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None,
10951094
) -> np.ndarray:
@@ -2656,7 +2655,7 @@ def _apply_unitary_circuit(
26562655
circuit: 'cirq.AbstractCircuit',
26572656
state: np.ndarray,
26582657
qubits: Tuple['cirq.Qid', ...],
2659-
dtype: 'DTypeLike',
2658+
dtype: Type[np.complexfloating],
26602659
) -> np.ndarray:
26612660
"""Applies a circuit's unitary effect to the given vector or matrix.
26622661

cirq-core/cirq/sim/density_matrix_simulation_state.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414
"""Objects and methods for acting efficiently on a density matrix."""
1515

16-
from typing import Any, Callable, List, Optional, Sequence, Tuple, TYPE_CHECKING, Union
16+
from typing import Any, Callable, List, Optional, Sequence, Tuple, Type, TYPE_CHECKING, Union
1717

1818
import numpy as np
1919

@@ -24,7 +24,6 @@
2424

2525
if TYPE_CHECKING:
2626
import cirq
27-
from numpy.typing import DTypeLike
2827

2928

3029
class _BufferedDensityMatrix(qis.QuantumStateRepresentation):
@@ -58,7 +57,7 @@ def create(
5857
*,
5958
initial_state: Union[np.ndarray, 'cirq.STATE_VECTOR_LIKE'] = 0,
6059
qid_shape: Optional[Tuple[int, ...]] = None,
61-
dtype: Optional['DTypeLike'] = None,
60+
dtype: Optional[Type[np.complexfloating]] = None,
6261
buffer: Optional[List[np.ndarray]] = None,
6362
):
6463
"""Creates a buffered density matrix with the requested state.
@@ -252,7 +251,7 @@ def __init__(
252251
prng: Optional[np.random.RandomState] = None,
253252
qubits: Optional[Sequence['cirq.Qid']] = None,
254253
initial_state: Union[np.ndarray, 'cirq.STATE_VECTOR_LIKE'] = 0,
255-
dtype: 'DTypeLike' = np.complex64,
254+
dtype: Type[np.complexfloating] = np.complex64,
256255
classical_data: Optional['cirq.ClassicalDataStore'] = None,
257256
):
258257
"""Inits DensityMatrixSimulationState.

cirq-core/cirq/sim/density_matrix_simulator.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
"""Simulator for density matrices that simulates noisy quantum circuits."""
15-
from typing import Any, Dict, TYPE_CHECKING, Tuple, Union, Sequence, Optional, List
15+
from typing import Any, Dict, List, Optional, Sequence, Type, TYPE_CHECKING, Tuple, Union
1616

1717
import numpy as np
1818

@@ -22,7 +22,6 @@
2222

2323
if TYPE_CHECKING:
2424
import cirq
25-
from numpy.typing import DTypeLike
2625

2726

2827
class DensityMatrixSimulator(
@@ -116,7 +115,7 @@ class DensityMatrixSimulator(
116115
def __init__(
117116
self,
118117
*,
119-
dtype: 'DTypeLike' = np.complex64,
118+
dtype: Type[np.complexfloating] = np.complex64,
120119
noise: 'cirq.NOISE_MODEL_LIKE' = None,
121120
seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None,
122121
split_untangled_states: bool = True,
@@ -250,7 +249,7 @@ def __init__(
250249
self,
251250
sim_state: 'cirq.SimulationStateBase[cirq.DensityMatrixSimulationState]',
252251
simulator: 'cirq.DensityMatrixSimulator' = None,
253-
dtype: 'DTypeLike' = np.complex64,
252+
dtype: Type[np.complexfloating] = np.complex64,
254253
):
255254
"""DensityMatrixStepResult.
256255

cirq-core/cirq/sim/density_matrix_simulator_test.py

Lines changed: 49 additions & 46 deletions
Large diffs are not rendered by default.

cirq-core/cirq/sim/mux.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
Filename is a reference to multiplexing.
1818
"""
1919

20-
from typing import cast, List, Optional, Sequence, TYPE_CHECKING, Union
20+
from typing import cast, List, Optional, Sequence, Type, TYPE_CHECKING, Union
2121

2222
import numpy as np
2323

@@ -53,7 +53,7 @@ def sample(
5353
noise: 'cirq.NOISE_MODEL_LIKE' = None,
5454
param_resolver: Optional['cirq.ParamResolver'] = None,
5555
repetitions: int = 1,
56-
dtype: 'DTypeLike' = np.complex64,
56+
dtype: Type[np.complexfloating] = np.complex64,
5757
seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None,
5858
) -> 'cirq.Result':
5959
"""Simulates sampling from the given circuit.
@@ -108,7 +108,7 @@ def final_state_vector(
108108
param_resolver: 'cirq.ParamResolverOrSimilarType' = None,
109109
qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT,
110110
ignore_terminal_measurements: bool = False,
111-
dtype: 'DTypeLike' = np.complex64,
111+
dtype: Type[np.complexfloating] = np.complex64,
112112
seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None,
113113
) -> 'np.ndarray':
114114
"""Returns the state vector resulting from acting operations on a state.
@@ -178,7 +178,7 @@ def sample_sweep(
178178
*,
179179
noise: 'cirq.NOISE_MODEL_LIKE' = None,
180180
repetitions: int = 1,
181-
dtype: 'DTypeLike' = np.complex64,
181+
dtype: Type[np.complexfloating] = np.complex64,
182182
seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None,
183183
) -> Sequence['cirq.Result']:
184184
"""Runs the supplied Circuit, mimicking quantum hardware.
@@ -224,7 +224,7 @@ def final_density_matrix(
224224
initial_state: 'cirq.STATE_VECTOR_LIKE' = 0,
225225
param_resolver: 'cirq.ParamResolverOrSimilarType' = None,
226226
qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT,
227-
dtype: 'DTypeLike' = np.complex64,
227+
dtype: Type[np.complexfloating] = np.complex64,
228228
seed: Optional[Union[int, np.random.RandomState]] = None,
229229
ignore_measurement_results: bool = True,
230230
) -> 'np.ndarray':

cirq-core/cirq/sim/simulator_base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
Optional,
2727
Sequence,
2828
Tuple,
29+
Type,
2930
TypeVar,
3031
TYPE_CHECKING,
3132
)
@@ -93,7 +94,7 @@ class SimulatorBase(
9394
def __init__(
9495
self,
9596
*,
96-
dtype: 'DTypeLike' = np.complex64,
97+
dtype: Type[np.complexfloating] = np.complex64,
9798
noise: 'cirq.NOISE_MODEL_LIKE' = None,
9899
seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None,
99100
split_untangled_states: bool = False,

cirq-core/cirq/sim/sparse_simulator.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
"""A simulator that uses numpy's einsum for sparse matrix operations."""
1616

17-
from typing import Any, Iterator, List, TYPE_CHECKING, Union, Sequence, Optional
17+
from typing import Any, Iterator, List, TYPE_CHECKING, Union, Sequence, Type, Optional
1818

1919
import numpy as np
2020

@@ -24,7 +24,6 @@
2424

2525
if TYPE_CHECKING:
2626
import cirq
27-
from numpy.typing import DTypeLike
2827

2928

3029
class Simulator(
@@ -127,7 +126,7 @@ class Simulator(
127126
def __init__(
128127
self,
129128
*,
130-
dtype: 'DTypeLike' = np.complex64,
129+
dtype: Type[np.complexfloating] = np.complex64,
131130
noise: 'cirq.NOISE_MODEL_LIKE' = None,
132131
seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None,
133132
split_untangled_states: bool = True,
@@ -231,7 +230,7 @@ def __init__(
231230
self,
232231
sim_state: 'cirq.SimulationStateBase[cirq.StateVectorSimulationState]',
233232
simulator: 'cirq.Simulator' = None,
234-
dtype: 'DTypeLike' = np.complex64,
233+
dtype: Type[np.complexfloating] = np.complex64,
235234
):
236235
"""Results of a step of the simulator.
237236

0 commit comments

Comments
 (0)