Skip to content

Commit

Permalink
Use np.complexfloating for dtypes that should be complex (#5488)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dabacon committed Jun 23, 2022
1 parent ca8585e commit 3c8b036
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 114 deletions.
7 changes: 3 additions & 4 deletions cirq-core/cirq/circuits/circuit.py
Expand Up @@ -64,7 +64,6 @@

if TYPE_CHECKING:
import cirq
from numpy.typing import DTypeLike


_TGate = TypeVar('_TGate', bound='cirq.Gate')
Expand Down Expand Up @@ -999,7 +998,7 @@ def unitary(
qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT,
qubits_that_should_be_present: Iterable['cirq.Qid'] = (),
ignore_terminal_measurements: bool = True,
dtype: 'DTypeLike' = np.complex64,
dtype: Type[np.complexfloating] = np.complex64,
) -> np.ndarray:
"""Converts the circuit into a unitary matrix, if possible.
Expand Down Expand Up @@ -1089,7 +1088,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['DTypeLike'] = None,
dtype: Optional[Type[np.complexfloating]] = None,
param_resolver: 'cirq.ParamResolverOrSimilarType' = None,
seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None,
) -> np.ndarray:
Expand Down Expand Up @@ -2656,7 +2655,7 @@ def _apply_unitary_circuit(
circuit: 'cirq.AbstractCircuit',
state: np.ndarray,
qubits: Tuple['cirq.Qid', ...],
dtype: 'DTypeLike',
dtype: Type[np.complexfloating],
) -> np.ndarray:
"""Applies a circuit's unitary effect to the given vector or matrix.
Expand Down
7 changes: 3 additions & 4 deletions cirq-core/cirq/sim/density_matrix_simulation_state.py
Expand Up @@ -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, Union
from typing import Any, Callable, List, Optional, Sequence, Tuple, Type, TYPE_CHECKING, Union

import numpy as np

Expand All @@ -24,7 +24,6 @@

if TYPE_CHECKING:
import cirq
from numpy.typing import DTypeLike


class _BufferedDensityMatrix(qis.QuantumStateRepresentation):
Expand Down Expand Up @@ -58,7 +57,7 @@ def create(
*,
initial_state: Union[np.ndarray, 'cirq.STATE_VECTOR_LIKE'] = 0,
qid_shape: Optional[Tuple[int, ...]] = None,
dtype: Optional['DTypeLike'] = None,
dtype: Optional[Type[np.complexfloating]] = None,
buffer: Optional[List[np.ndarray]] = None,
):
"""Creates a buffered density matrix with the requested state.
Expand Down Expand Up @@ -252,7 +251,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: 'DTypeLike' = np.complex64,
dtype: Type[np.complexfloating] = np.complex64,
classical_data: Optional['cirq.ClassicalDataStore'] = None,
):
"""Inits DensityMatrixSimulationState.
Expand Down
7 changes: 3 additions & 4 deletions cirq-core/cirq/sim/density_matrix_simulator.py
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Simulator for density matrices that simulates noisy quantum circuits."""
from typing import Any, Dict, TYPE_CHECKING, Tuple, Union, Sequence, Optional, List
from typing import Any, Dict, List, Optional, Sequence, Type, TYPE_CHECKING, Tuple, Union

import numpy as np

Expand All @@ -22,7 +22,6 @@

if TYPE_CHECKING:
import cirq
from numpy.typing import DTypeLike


class DensityMatrixSimulator(
Expand Down Expand Up @@ -116,7 +115,7 @@ class DensityMatrixSimulator(
def __init__(
self,
*,
dtype: 'DTypeLike' = np.complex64,
dtype: Type[np.complexfloating] = np.complex64,
noise: 'cirq.NOISE_MODEL_LIKE' = None,
seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None,
split_untangled_states: bool = True,
Expand Down Expand Up @@ -250,7 +249,7 @@ def __init__(
self,
sim_state: 'cirq.SimulationStateBase[cirq.DensityMatrixSimulationState]',
simulator: 'cirq.DensityMatrixSimulator' = None,
dtype: 'DTypeLike' = np.complex64,
dtype: Type[np.complexfloating] = np.complex64,
):
"""DensityMatrixStepResult.
Expand Down
95 changes: 49 additions & 46 deletions cirq-core/cirq/sim/density_matrix_simulator_test.py

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions cirq-core/cirq/sim/mux.py
Expand Up @@ -17,7 +17,7 @@
Filename is a reference to multiplexing.
"""

from typing import cast, List, Optional, Sequence, TYPE_CHECKING, Union
from typing import cast, List, Optional, Sequence, Type, TYPE_CHECKING, Union

import numpy as np

Expand Down Expand Up @@ -53,7 +53,7 @@ def sample(
noise: 'cirq.NOISE_MODEL_LIKE' = None,
param_resolver: Optional['cirq.ParamResolver'] = None,
repetitions: int = 1,
dtype: 'DTypeLike' = np.complex64,
dtype: Type[np.complexfloating] = np.complex64,
seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None,
) -> 'cirq.Result':
"""Simulates sampling from the given circuit.
Expand Down Expand Up @@ -108,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: 'DTypeLike' = np.complex64,
dtype: Type[np.complexfloating] = np.complex64,
seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None,
) -> 'np.ndarray':
"""Returns the state vector resulting from acting operations on a state.
Expand Down Expand Up @@ -178,7 +178,7 @@ def sample_sweep(
*,
noise: 'cirq.NOISE_MODEL_LIKE' = None,
repetitions: int = 1,
dtype: 'DTypeLike' = np.complex64,
dtype: Type[np.complexfloating] = np.complex64,
seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None,
) -> Sequence['cirq.Result']:
"""Runs the supplied Circuit, mimicking quantum hardware.
Expand Down Expand Up @@ -224,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: 'DTypeLike' = np.complex64,
dtype: Type[np.complexfloating] = np.complex64,
seed: Optional[Union[int, np.random.RandomState]] = None,
ignore_measurement_results: bool = True,
) -> 'np.ndarray':
Expand Down
3 changes: 2 additions & 1 deletion cirq-core/cirq/sim/simulator_base.py
Expand Up @@ -26,6 +26,7 @@
Optional,
Sequence,
Tuple,
Type,
TypeVar,
TYPE_CHECKING,
)
Expand Down Expand Up @@ -93,7 +94,7 @@ class SimulatorBase(
def __init__(
self,
*,
dtype: 'DTypeLike' = np.complex64,
dtype: Type[np.complexfloating] = np.complex64,
noise: 'cirq.NOISE_MODEL_LIKE' = None,
seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None,
split_untangled_states: bool = False,
Expand Down
7 changes: 3 additions & 4 deletions cirq-core/cirq/sim/sparse_simulator.py
Expand Up @@ -14,7 +14,7 @@

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

from typing import Any, Iterator, List, TYPE_CHECKING, Union, Sequence, Optional
from typing import Any, Iterator, List, TYPE_CHECKING, Union, Sequence, Type, Optional

import numpy as np

Expand All @@ -24,7 +24,6 @@

if TYPE_CHECKING:
import cirq
from numpy.typing import DTypeLike


class Simulator(
Expand Down Expand Up @@ -127,7 +126,7 @@ class Simulator(
def __init__(
self,
*,
dtype: 'DTypeLike' = np.complex64,
dtype: Type[np.complexfloating] = np.complex64,
noise: 'cirq.NOISE_MODEL_LIKE' = None,
seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None,
split_untangled_states: bool = True,
Expand Down Expand Up @@ -231,7 +230,7 @@ def __init__(
self,
sim_state: 'cirq.SimulationStateBase[cirq.StateVectorSimulationState]',
simulator: 'cirq.Simulator' = None,
dtype: 'DTypeLike' = np.complex64,
dtype: Type[np.complexfloating] = np.complex64,
):
"""Results of a step of the simulator.
Expand Down

0 comments on commit 3c8b036

Please sign in to comment.