Skip to content

Commit

Permalink
Replace type_workarounds.NotImplementedType with types.NotImplemented…
Browse files Browse the repository at this point in the history
…Type (#6790)

- Replaced type_workarounds.NotImplementedType with types.NotImplementedType,
  removing the need for the type_workarounds module.

- This change simplifies the codebase by relying on standard types,
  allowing us to delete the now-redundant type_workarounds.py file.

Fixes #6775
  • Loading branch information
Ayobami-00 authored Nov 13, 2024
1 parent d1b0430 commit fd95547
Show file tree
Hide file tree
Showing 40 changed files with 48 additions and 73 deletions.
1 change: 0 additions & 1 deletion cirq-core/cirq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
# Low level
_version,
_doc,
type_workarounds,
)

with _import.delay_import('cirq.protocols'):
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import itertools
import math
from collections import defaultdict
from types import NotImplementedType
from typing import (
AbstractSet,
Any,
Expand Down Expand Up @@ -62,7 +63,6 @@
from cirq.circuits.qasm_output import QasmOutput
from cirq.circuits.text_diagram_drawer import TextDiagramDrawer
from cirq.protocols import circuit_diagram_info_protocol
from cirq.type_workarounds import NotImplementedType

if TYPE_CHECKING:
import cirq
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/circuits/frozen_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
"""An immutable version of the Circuit data structure."""
from functools import cached_property
from types import NotImplementedType
from typing import (
AbstractSet,
FrozenSet,
Expand All @@ -30,7 +31,6 @@
from cirq import protocols, _compat
from cirq.circuits import AbstractCircuit, Alignment, Circuit
from cirq.circuits.insert_strategy import InsertStrategy
from cirq.type_workarounds import NotImplementedType

if TYPE_CHECKING:
import cirq
Expand Down
3 changes: 2 additions & 1 deletion cirq-core/cirq/circuits/moment.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""A simplified time-slice of operations within a sequenced circuit."""

import itertools
from types import NotImplementedType
from typing import (
AbstractSet,
Any,
Expand All @@ -33,6 +34,7 @@
TYPE_CHECKING,
Union,
)

from typing_extensions import Self

import numpy as np
Expand All @@ -41,7 +43,6 @@
from cirq._import import LazyLoader
from cirq.ops import raw_types, op_tree
from cirq.protocols import circuit_diagram_info_protocol
from cirq.type_workarounds import NotImplementedType

if TYPE_CHECKING:
import cirq
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/contrib/acquaintance/permutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import abc
from types import NotImplementedType
from typing import (
Any,
cast,
Expand All @@ -28,7 +29,6 @@
)

from cirq import circuits, ops, protocols, transformers, value
from cirq.type_workarounds import NotImplementedType

if TYPE_CHECKING:
import cirq
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from types import NotImplementedType
from typing import List, Union, Type, cast, TYPE_CHECKING
from enum import Enum

import numpy as np

from cirq import ops, transformers, protocols, linalg
from cirq.type_workarounds import NotImplementedType

if TYPE_CHECKING:
import cirq
Expand Down
6 changes: 3 additions & 3 deletions cirq-core/cirq/ops/clifford_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any, Dict, List, Optional, Sequence, Tuple, TYPE_CHECKING, Union

import functools
from dataclasses import dataclass
from types import NotImplementedType
from typing import Any, Dict, List, Optional, Sequence, Tuple, TYPE_CHECKING, Union

import numpy as np

from cirq import protocols, value, linalg, qis
from cirq._import import LazyLoader
from cirq._compat import cached_method
from cirq.ops import common_gates, named_qubit, raw_types, pauli_gates, phased_x_z_gate
from cirq.ops.pauli_gates import Pauli
from cirq.type_workarounds import NotImplementedType

if TYPE_CHECKING:
import cirq
Expand Down
4 changes: 1 addition & 3 deletions cirq-core/cirq/ops/common_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
raised to a power (i.e. cirq.H**0.5). See the definition in EigenGate.
"""

from types import NotImplementedType
from typing import (
Any,
cast,
Expand All @@ -46,9 +47,6 @@
from cirq._compat import proper_repr
from cirq._doc import document
from cirq.ops import controlled_gate, eigen_gate, gate_features, raw_types, control_values as cv

from cirq.type_workarounds import NotImplementedType

from cirq.ops.swap_gates import ISWAP, SWAP, ISwapPowGate, SwapPowGate
from cirq.ops.measurement_gate import MeasurementGate

Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/ops/controlled_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from types import NotImplementedType
from typing import (
AbstractSet,
Any,
Expand All @@ -35,7 +36,6 @@
matrix_gates,
control_values as cv,
)
from cirq.type_workarounds import NotImplementedType

if TYPE_CHECKING:
import cirq
Expand Down Expand Up @@ -255,7 +255,7 @@ def _unitary_(self) -> Union[np.ndarray, NotImplementedType]:
def _has_mixture_(self) -> bool:
return protocols.has_mixture(self.sub_gate)

def _mixture_(self) -> Union[np.ndarray, NotImplementedType]:
def _mixture_(self) -> Union[Sequence[tuple[float, np.ndarray]], NotImplementedType]:
qubits = line_qubit.LineQid.for_gate(self)
op = self.sub_gate.on(*qubits[self.num_controls() :])
c_op = cop.ControlledOperation(qubits[: self.num_controls()], op, self.control_values)
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/ops/controlled_gate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from types import NotImplementedType
from typing import Union, Tuple, cast

import numpy as np
import pytest
import sympy

import cirq
from cirq.type_workarounds import NotImplementedType


class GateUsingWorkspaceForApplyUnitary(cirq.testing.SingleQubitGate):
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/ops/controlled_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from types import NotImplementedType
from typing import (
AbstractSet,
Any,
Expand All @@ -38,7 +39,6 @@
raw_types,
control_values as cv,
)
from cirq.type_workarounds import NotImplementedType

if TYPE_CHECKING:
import cirq
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/ops/controlled_operation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import itertools
import re
from types import NotImplementedType
from typing import cast, Tuple, Union

import numpy as np
Expand All @@ -22,7 +23,6 @@

import cirq
from cirq import protocols
from cirq.type_workarounds import NotImplementedType


class GateUsingWorkspaceForApplyUnitary(cirq.testing.SingleQubitGate):
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/ops/dense_pauli_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import abc
import numbers
from types import NotImplementedType
from typing import (
AbstractSet,
Any,
Expand All @@ -38,7 +39,6 @@
from cirq import protocols, linalg, value
from cirq._compat import proper_repr
from cirq.ops import raw_types, identity, pauli_gates, global_phase_op, pauli_string
from cirq.type_workarounds import NotImplementedType

if TYPE_CHECKING:
import cirq
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/ops/eigen_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import fractions
import math
import numbers
from types import NotImplementedType
from typing import (
AbstractSet,
Any,
Expand All @@ -35,7 +36,6 @@
from cirq import value, protocols
from cirq.linalg import tolerance
from cirq.ops import raw_types
from cirq.type_workarounds import NotImplementedType

if TYPE_CHECKING:
import cirq
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/ops/gate_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import re
import warnings
from types import NotImplementedType
from typing import (
AbstractSet,
Any,
Expand All @@ -38,7 +39,6 @@

from cirq import ops, protocols, value
from cirq.ops import raw_types, gate_features, control_values as cv
from cirq.type_workarounds import NotImplementedType

if TYPE_CHECKING:
import cirq
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/ops/global_phase_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
"""A no-qubit global phase operation."""

from types import NotImplementedType
from typing import AbstractSet, Any, cast, Dict, Sequence, Tuple, Union, Optional, Collection

import numpy as np
Expand All @@ -21,7 +22,6 @@
import cirq
from cirq import value, protocols
from cirq.ops import raw_types, controlled_gate, control_values as cv
from cirq.type_workarounds import NotImplementedType


@value.value_equality(approximate=True)
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/ops/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
# limitations under the License.
"""IdentityGate."""

from types import NotImplementedType
from typing import Any, Dict, Optional, Tuple, TYPE_CHECKING, Sequence, Union

import numpy as np
import sympy

from cirq import protocols, value
from cirq._doc import document
from cirq.type_workarounds import NotImplementedType
from cirq.ops import raw_types

if TYPE_CHECKING:
Expand Down
3 changes: 1 addition & 2 deletions cirq-core/cirq/ops/parallel_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.


from types import NotImplementedType
from typing import AbstractSet, Union, Any, Optional, Tuple, TYPE_CHECKING, Dict

import numpy as np

from cirq import protocols, value
from cirq.ops import raw_types
from cirq.type_workarounds import NotImplementedType

if TYPE_CHECKING:
import cirq
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/ops/pauli_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import abc
from types import NotImplementedType
from typing import Any, cast, Tuple, TYPE_CHECKING, Union, Dict

from cirq._doc import document
from cirq._import import LazyLoader
from cirq.ops import common_gates, raw_types, identity
from cirq.type_workarounds import NotImplementedType


if TYPE_CHECKING:
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/ops/pauli_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import cmath
import math
import numbers
from types import NotImplementedType
from typing import (
Any,
cast,
Expand Down Expand Up @@ -56,7 +57,6 @@
pauli_interaction_gate,
raw_types,
)
from cirq.type_workarounds import NotImplementedType

if TYPE_CHECKING:
import cirq
Expand Down
6 changes: 4 additions & 2 deletions cirq-core/cirq/ops/phased_x_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""An `XPowGate` conjugated by `ZPowGate`s."""

from types import NotImplementedType
from typing import AbstractSet, Any, cast, Dict, Optional, Sequence, Tuple, Union

import math
import numbers

import numpy as np
import sympy

import cirq
from cirq import value, protocols
from cirq._compat import proper_repr
from cirq.ops import common_gates, raw_types
from cirq.type_workarounds import NotImplementedType


@value.value_equality(manual_cls=True, approximate=True)
Expand Down Expand Up @@ -130,7 +132,7 @@ def _trace_distance_bound_(self) -> Optional[float]:
def _has_unitary_(self):
return not self._is_parameterized_()

def _unitary_(self) -> Union[np.ndarray, NotImplementedType]:
def _unitary_(self) -> Optional[Union[np.ndarray, NotImplementedType]]:
"""See `cirq.SupportsUnitary`."""
if self._is_parameterized_():
return None
Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/ops/raw_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import abc
import functools
from types import NotImplementedType
from typing import (
cast,
AbstractSet,
Expand All @@ -42,7 +43,6 @@
from cirq import protocols, value
from cirq._import import LazyLoader
from cirq._compat import __cirq_debug__, _method_cache_name, cached_method
from cirq.type_workarounds import NotImplementedType
from cirq.ops import control_values as cv

# Lazy imports to break circular dependencies.
Expand Down Expand Up @@ -879,7 +879,7 @@ def _mixture_(self) -> Sequence[Tuple[float, Any]]:
def _has_kraus_(self) -> bool:
return protocols.has_kraus(self.sub_operation)

def _kraus_(self) -> Union[Tuple[np.ndarray], NotImplementedType]:
def _kraus_(self) -> Union[Tuple[np.ndarray, ...], NotImplementedType]:
return protocols.kraus(self.sub_operation, NotImplemented)

@cached_method
Expand Down
Loading

0 comments on commit fd95547

Please sign in to comment.