Skip to content

Commit

Permalink
Fix circuit mypy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Randl committed Mar 20, 2023
1 parent e2f6606 commit 33c0a3c
Show file tree
Hide file tree
Showing 40 changed files with 489 additions and 388 deletions.
20 changes: 10 additions & 10 deletions qiskit/circuit/add_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@
# that they have been altered from the originals.

"""Add control to operation if supported."""

from typing import Union, Optional
from __future__ import annotations

from qiskit.circuit.exceptions import CircuitError
from qiskit.extensions import UnitaryGate
from . import ControlledGate, Gate, QuantumRegister, QuantumCircuit


def add_control(
operation: Union[Gate, ControlledGate],
operation: Gate | ControlledGate,
num_ctrl_qubits: int,
label: Union[str, None],
ctrl_state: Union[int, str, None],
label: str | None,
ctrl_state: str | int | None,
) -> ControlledGate:
"""For standard gates, if the controlled version already exists in the
library, it will be returned (e.g. XGate.control() = CnotGate().
Expand Down Expand Up @@ -62,10 +61,10 @@ def add_control(


def control(
operation: Union[Gate, ControlledGate],
num_ctrl_qubits: Optional[int] = 1,
label: Optional[Union[None, str]] = None,
ctrl_state: Optional[Union[None, int, str]] = None,
operation: Gate | ControlledGate,
num_ctrl_qubits: int | None = 1,
label: str | None = None,
ctrl_state: str | int | None = None,
) -> ControlledGate:
"""Return controlled version of gate using controlled rotations. This function
first checks the name of the operation to see if it knows of a method from which
Expand Down Expand Up @@ -218,7 +217,8 @@ def control(
if isinstance(operation, controlledgate.ControlledGate):
operation.ctrl_state = original_ctrl_state
new_num_ctrl_qubits = num_ctrl_qubits + operation.num_ctrl_qubits
new_ctrl_state = operation.ctrl_state << num_ctrl_qubits | ctrl_state
# TODO: what if ctrl_state is str?
new_ctrl_state: str | int | None = operation.ctrl_state << num_ctrl_qubits | ctrl_state
base_name = operation.base_gate.name
base_gate = operation.base_gate
else:
Expand Down
5 changes: 3 additions & 2 deletions qiskit/circuit/controlflow/control_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

"Container to encapsulate all control flow operations."

from __future__ import annotations
from abc import ABC, abstractmethod
from typing import Tuple, Iterable
from typing import Iterable

from qiskit.circuit import QuantumCircuit, Instruction

Expand All @@ -23,7 +24,7 @@ class ControlFlowOp(Instruction, ABC):

@property
@abstractmethod
def blocks(self) -> Tuple[QuantumCircuit, ...]:
def blocks(self) -> tuple[QuantumCircuit, ...]:
"""Tuple of QuantumCircuits which may be executed as part of the
execution of this ControlFlowOp. May be parameterized by a loop
parameter to be resolved at run time.
Expand Down
7 changes: 4 additions & 3 deletions qiskit/circuit/controlledgate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

"""Controlled unitary gate."""

from __future__ import annotations
import copy
from typing import List, Optional, Union
from typing import Optional, Union

from qiskit.circuit.exceptions import CircuitError

Expand All @@ -31,7 +32,7 @@ def __init__(
self,
name: str,
num_qubits: int,
params: List,
params: list,
label: Optional[str] = None,
num_ctrl_qubits: Optional[int] = 1,
definition: Optional["QuantumCircuit"] = None,
Expand Down Expand Up @@ -103,7 +104,7 @@ def __init__(
self._name = name

@property
def definition(self) -> List:
def definition(self) -> QuantumCircuit:
"""Return definition in terms of other basic gates. If the gate has
open controls, as determined from `self.ctrl_state`, the returned
definition is conjugated with X without changing the internal
Expand Down
21 changes: 13 additions & 8 deletions qiskit/circuit/gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

"""Unitary gate."""

from typing import List, Optional, Union, Tuple
from __future__ import annotations
from typing import Any, Iterator, Iterable
import numpy as np

from qiskit.circuit.parameterexpression import ParameterExpression
Expand All @@ -24,7 +25,7 @@ class Gate(Instruction):
"""Unitary gate."""

def __init__(
self, name: str, num_qubits: int, params: List, label: Optional[str] = None
self, name: str, num_qubits: int, params: list[Any], label: str | None = None
) -> None:
"""Create a new gate.
Expand Down Expand Up @@ -95,8 +96,8 @@ def _return_repeat(self, exponent: float) -> "Gate":
def control(
self,
num_ctrl_qubits: int = 1,
label: Optional[str] = None,
ctrl_state: Optional[Union[int, str]] = None,
label: str | None = None,
ctrl_state: int | str | None = None,
):
"""Return controlled version of gate. See :class:`.ControlledGate` for usage.
Expand All @@ -120,7 +121,7 @@ def control(
return add_control(self, num_ctrl_qubits, label, ctrl_state)

@staticmethod
def _broadcast_single_argument(qarg: List) -> List:
def _broadcast_single_argument(qarg: list[Any]) -> Iterator[tuple[list[Any], list[Any]]]:
"""Expands a single argument.
For example: [q[0], q[1]] -> [q[0]], [q[1]]
Expand All @@ -131,7 +132,9 @@ def _broadcast_single_argument(qarg: List) -> List:
yield [arg0], []

@staticmethod
def _broadcast_2_arguments(qarg0: List, qarg1: List) -> List:
def _broadcast_2_arguments(
qarg0: list[Any], qarg1: list[Any]
) -> Iterator[tuple[list[Any], list[Any]]]:
if len(qarg0) == len(qarg1):
# [[q[0], q[1]], [r[0], r[1]]] -> [q[0], r[0]]
# -> [q[1], r[1]]
Expand All @@ -153,14 +156,16 @@ def _broadcast_2_arguments(qarg0: List, qarg1: List) -> List:
)

@staticmethod
def _broadcast_3_or_more_args(qargs: List) -> List:
def _broadcast_3_or_more_args(qargs: list[Any]) -> Iterator[tuple[list[Any], list[Any]]]:
if all(len(qarg) == len(qargs[0]) for qarg in qargs):
for arg in zip(*qargs):
yield list(arg), []
else:
raise CircuitError("Not sure how to combine these qubit arguments:\n %s\n" % qargs)

def broadcast_arguments(self, qargs: List, cargs: List) -> Tuple[List, List]:
def broadcast_arguments(
self, qargs: list[Any], cargs: list[Any]
) -> Iterable[tuple[list[Any], list[Any]]]:
"""Validation and handling of the arguments and its relationship.
For example, ``cx([q[0],q[1]], q[2])`` means ``cx(q[0], q[2]); cx(q[1], q[2])``. This
Expand Down
24 changes: 17 additions & 7 deletions qiskit/circuit/instructionset.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
Instruction collection.
"""

from __future__ import annotations
import functools
import warnings
from typing import Callable, Optional, Tuple, Union
from typing import Callable

from qiskit.circuit.exceptions import CircuitError
from .classicalregister import Clbit, ClassicalRegister
Expand All @@ -28,7 +29,9 @@
# its creation, so caching this allows us to only pay the register-unrolling penalty once. The
# cache does not need to be large, because in general only one circuit is constructed at once.
@functools.lru_cache(4)
def _requester_from_cregs(cregs: Tuple[ClassicalRegister]) -> Callable:
def _requester_from_cregs(
cregs: tuple[ClassicalRegister],
) -> Callable[[Clbit | ClassicalRegister | int], ClassicalRegister | Clbit]:
"""Get a classical resource requester from an iterable of classical registers.
This implements the deprecated functionality of constructing an :obj:`.InstructionSet` with a
Expand Down Expand Up @@ -60,7 +63,7 @@ def _requester_from_cregs(cregs: Tuple[ClassicalRegister]) -> Callable:
clbit_set = frozenset(clbit_flat)
creg_set = frozenset(cregs)

def requester(classical):
def requester(classical: Clbit | ClassicalRegister | int) -> ClassicalRegister | Clbit:
if isinstance(classical, Clbit):
if classical not in clbit_set:
raise CircuitError(
Expand Down Expand Up @@ -92,7 +95,12 @@ class InstructionSet:

__slots__ = ("_instructions", "_requester")

def __init__(self, circuit_cregs=None, *, resource_requester: Optional[Callable] = None):
def __init__(
self,
circuit_cregs=None,
*,
resource_requester: Callable[..., ClassicalRegister | Clbit] | None = None,
):
"""New collection of instructions.
The context (``qargs`` and ``cargs`` that each instruction is attached to) is also stored
Expand Down Expand Up @@ -124,7 +132,7 @@ def __init__(self, circuit_cregs=None, *, resource_requester: Optional[Callable]
CircuitError: if both ``resource_requester`` and ``circuit_cregs`` are passed. Only one
of these may be passed, and it should be ``resource_requester``.
"""
self._instructions = []
self._instructions: list[CircuitInstruction] = []
if circuit_cregs is not None:
if resource_requester is not None:
raise CircuitError("Cannot pass both 'circuit_cregs' and 'resource_requester'.")
Expand All @@ -135,7 +143,9 @@ def __init__(self, circuit_cregs=None, *, resource_requester: Optional[Callable]
DeprecationWarning,
stacklevel=2,
)
self._requester: Optional[Callable] = _requester_from_cregs(tuple(circuit_cregs))
self._requester: Callable[..., ClassicalRegister | Clbit] = _requester_from_cregs(
tuple(circuit_cregs)
)
else:
self._requester = resource_requester

Expand Down Expand Up @@ -163,7 +173,7 @@ def inverse(self):
self._instructions[i] = instruction.replace(operation=instruction.operation.inverse())
return self

def c_if(self, classical: Union[Clbit, ClassicalRegister, int], val: int) -> "InstructionSet":
def c_if(self, classical: Clbit | ClassicalRegister | int, val: int) -> "InstructionSet":
"""Set a classical equality condition on all the instructions in this set between the
:obj:`.ClassicalRegister` or :obj:`.Clbit` ``classical`` and value ``val``.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# that they have been altered from the originals.

"""Compute the sum of two qubit registers using Classical Addition."""
from __future__ import annotations
from qiskit.circuit.bit import Bit

from qiskit.circuit import QuantumCircuit, QuantumRegister, AncillaRegister

Expand Down Expand Up @@ -78,9 +80,9 @@ def __init__(
super().__init__(num_state_qubits, name=name)

# define the input registers
registers = []
registers: list[QuantumRegister | list[Bit]] = []
if kind == "full":
qr_cin = QuantumRegister(1, name="cin")
qr_cin: QuantumRegister | list[Bit] = QuantumRegister(1, name="cin")
registers.append(qr_cin)
else:
qr_cin = []
Expand All @@ -91,15 +93,17 @@ def __init__(
registers += [qr_a, qr_b]

if kind in ["half", "full"]:
qr_cout = QuantumRegister(1, name="cout")
qr_cout: QuantumRegister | list[Bit] = QuantumRegister(1, name="cout")
registers.append(qr_cout)
else:
qr_cout = []

self.add_register(*registers)

if num_state_qubits > 1:
qr_help = AncillaRegister(num_state_qubits - 1, name="helper")
qr_help: AncillaRegister | list[Bit] = AncillaRegister(
num_state_qubits - 1, name="helper"
)
self.add_register(qr_help)
else:
qr_help = []
Expand Down
16 changes: 8 additions & 8 deletions qiskit/circuit/library/arithmetic/integer_comparator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

"""Integer Comparator."""

from typing import List, Optional
from __future__ import annotations
import warnings
import numpy as np

Expand Down Expand Up @@ -41,8 +41,8 @@ class IntegerComparator(BlueprintCircuit):

def __init__(
self,
num_state_qubits: Optional[int] = None,
value: Optional[int] = None,
num_state_qubits: int | None = None,
value: int | None = None,
geq: bool = True,
name: str = "cmp",
) -> None:
Expand All @@ -57,9 +57,9 @@ def __init__(
"""
super().__init__(name=name)

self._value = None
self._geq = None
self._num_state_qubits = None
self._value: int | None = None
self._geq: bool | None = None
self._num_state_qubits: int | None = None

self.value = value
self.geq = geq
Expand Down Expand Up @@ -120,7 +120,7 @@ def num_state_qubits(self) -> int:
return self._num_state_qubits

@num_state_qubits.setter
def num_state_qubits(self, num_state_qubits: Optional[int]) -> None:
def num_state_qubits(self, num_state_qubits: int | None) -> None:
"""Set the number of state qubits.
Note that this will change the quantum registers.
Expand All @@ -145,7 +145,7 @@ def num_state_qubits(self, num_state_qubits: Optional[int]) -> None:
qr_ancilla = AncillaRegister(num_ancillas)
self.add_register(qr_ancilla)

def _get_twos_complement(self) -> List[int]:
def _get_twos_complement(self) -> list[int]:
"""Returns the 2's complement of ``self.value`` as array.
Returns:
Expand Down
15 changes: 9 additions & 6 deletions qiskit/circuit/library/arithmetic/linear_amplitude_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

"""A class implementing a (piecewise-) linear function on qubit amplitudes."""

from typing import Optional, List, Union, Tuple
from __future__ import annotations
from typing import cast, List
import numpy as np
from qiskit.circuit import QuantumCircuit

Expand Down Expand Up @@ -77,12 +78,12 @@ class LinearAmplitudeFunction(QuantumCircuit):
def __init__(
self,
num_state_qubits: int,
slope: Union[float, List[float]],
offset: Union[float, List[float]],
domain: Tuple[float, float],
image: Tuple[float, float],
slope: float | list[float],
offset: float | list[float],
domain: tuple[float, float],
image: tuple[float, float],
rescaling_factor: float = 1,
breakpoints: Optional[List[float]] = None,
breakpoints: list[float] | None = None,
name: str = "F",
) -> None:
r"""
Expand All @@ -102,8 +103,10 @@ def __init__(
"""
if not hasattr(slope, "__len__"):
slope = [slope]
slope = cast(List[float], slope)
if not hasattr(offset, "__len__"):
offset = [offset]
offset = cast(List[float], offset)

# ensure that the breakpoints include the first point of the domain
if breakpoints is None:
Expand Down
Loading

0 comments on commit 33c0a3c

Please sign in to comment.