diff --git a/qiskit/transpiler/passes/analysis/count_ops.py b/qiskit/transpiler/passes/analysis/count_ops.py index e1d3cf86d03f..18b45d6a9cfd 100644 --- a/qiskit/transpiler/passes/analysis/count_ops.py +++ b/qiskit/transpiler/passes/analysis/count_ops.py @@ -12,6 +12,13 @@ """Count the operations in a DAG circuit.""" +from __future__ import annotations + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from qiskit.dagcircuit import DAGCircuit + from qiskit.transpiler.basepasses import AnalysisPass @@ -21,10 +28,15 @@ class CountOps(AnalysisPass): The result is saved in ``property_set['count_ops']`` as an integer. """ - def __init__(self, *, recurse=True): + def __init__(self, *, recurse: bool = True) -> None: + """ + Args: + recurse: If ``True`` (default), recursively count operations + inside control-flow blocks. + """ super().__init__() self.recurse = recurse - def run(self, dag): - """Run the CountOps pass on `dag`.""" + def run(self, dag: DAGCircuit) -> None: + """Run the CountOps pass on ``dag``.""" self.property_set["count_ops"] = dag.count_ops(recurse=self.recurse) diff --git a/qiskit/transpiler/passes/analysis/count_ops_longest_path.py b/qiskit/transpiler/passes/analysis/count_ops_longest_path.py index 938f059caf3f..caad81260bff 100644 --- a/qiskit/transpiler/passes/analysis/count_ops_longest_path.py +++ b/qiskit/transpiler/passes/analysis/count_ops_longest_path.py @@ -12,6 +12,13 @@ """Count the operations on the longest path in a DAGCircuit.""" +from __future__ import annotations + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from qiskit.dagcircuit import DAGCircuit + from qiskit.transpiler.basepasses import AnalysisPass @@ -21,6 +28,6 @@ class CountOpsLongestPath(AnalysisPass): The result is saved in ``property_set['count_ops_longest_path']`` as an integer. """ - def run(self, dag): - """Run the CountOpsLongestPath pass on `dag`.""" + def run(self, dag: DAGCircuit) -> None: + """Run the CountOpsLongestPath pass on ``dag``.""" self.property_set["count_ops_longest_path"] = dag.count_ops_longest_path() diff --git a/qiskit/transpiler/passes/analysis/dag_longest_path.py b/qiskit/transpiler/passes/analysis/dag_longest_path.py index 3129bbfe85a3..c24d4558c96d 100644 --- a/qiskit/transpiler/passes/analysis/dag_longest_path.py +++ b/qiskit/transpiler/passes/analysis/dag_longest_path.py @@ -12,6 +12,13 @@ """Return the longest path in a :class:`.DAGCircuit` as a list of DAGNodes.""" +from __future__ import annotations + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from qiskit.dagcircuit import DAGCircuit + from qiskit.transpiler.basepasses import AnalysisPass @@ -19,6 +26,6 @@ class DAGLongestPath(AnalysisPass): """Return the longest path in a :class:`.DAGCircuit` as a list of :class:`.DAGOpNode`\\ s, :class:`.DAGInNode`\\ s, and :class:`.DAGOutNode`\\ s.""" - def run(self, dag): - """Run the DAGLongestPath pass on `dag`.""" + def run(self, dag: DAGCircuit) -> None: + """Run the DAGLongestPath pass on ``dag``.""" self.property_set["dag_longest_path"] = dag.longest_path() diff --git a/qiskit/transpiler/passes/analysis/depth.py b/qiskit/transpiler/passes/analysis/depth.py index 5f9c786517a5..b74fd692806e 100644 --- a/qiskit/transpiler/passes/analysis/depth.py +++ b/qiskit/transpiler/passes/analysis/depth.py @@ -12,13 +12,20 @@ """Calculate the depth of a DAG circuit.""" +from __future__ import annotations + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from qiskit.dagcircuit import DAGCircuit + from qiskit.transpiler.basepasses import AnalysisPass class Depth(AnalysisPass): """Calculate the depth of a DAG circuit.""" - def __init__(self, *, recurse=False): + def __init__(self, *, recurse: bool = False) -> None: """ Args: recurse: whether to allow recursion into control flow. If this is ``False`` (default), @@ -28,6 +35,6 @@ def __init__(self, *, recurse=False): super().__init__() self.recurse = recurse - def run(self, dag): - """Run the Depth pass on `dag`.""" + def run(self, dag: DAGCircuit) -> None: + """Run the Depth pass on ``dag``.""" self.property_set["depth"] = dag.depth(recurse=self.recurse) diff --git a/qiskit/transpiler/passes/analysis/num_qubits.py b/qiskit/transpiler/passes/analysis/num_qubits.py index 26086b9b2e09..30ffea10da3f 100644 --- a/qiskit/transpiler/passes/analysis/num_qubits.py +++ b/qiskit/transpiler/passes/analysis/num_qubits.py @@ -12,6 +12,13 @@ """Calculate the number of qubits of a DAG circuit.""" +from __future__ import annotations + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from qiskit.dagcircuit import DAGCircuit + from qiskit.transpiler.basepasses import AnalysisPass @@ -21,6 +28,6 @@ class NumQubits(AnalysisPass): The result is saved in ``property_set['num_qubits']`` as an integer. """ - def run(self, dag): - """Run the NumQubits pass on `dag`.""" + def run(self, dag: DAGCircuit) -> None: + """Run the NumQubits pass on ``dag``.""" self.property_set["num_qubits"] = dag.num_qubits() diff --git a/qiskit/transpiler/passes/analysis/num_tensor_factors.py b/qiskit/transpiler/passes/analysis/num_tensor_factors.py index f0931339c111..1f86368cfd44 100644 --- a/qiskit/transpiler/passes/analysis/num_tensor_factors.py +++ b/qiskit/transpiler/passes/analysis/num_tensor_factors.py @@ -12,6 +12,13 @@ """Calculate the number of tensor factors of a DAG circuit.""" +from __future__ import annotations + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from qiskit.dagcircuit import DAGCircuit + from qiskit.transpiler.basepasses import AnalysisPass @@ -21,6 +28,6 @@ class NumTensorFactors(AnalysisPass): The result is saved in ``property_set['num_tensor_factors']`` as an integer. """ - def run(self, dag): - """Run the NumTensorFactors pass on `dag`.""" + def run(self, dag: DAGCircuit) -> None: + """Run the NumTensorFactors pass on ``dag``.""" self.property_set["num_tensor_factors"] = dag.num_tensor_factors() diff --git a/qiskit/transpiler/passes/analysis/resource_estimation.py b/qiskit/transpiler/passes/analysis/resource_estimation.py index 4b836bc4eb8f..1f88c35660f5 100644 --- a/qiskit/transpiler/passes/analysis/resource_estimation.py +++ b/qiskit/transpiler/passes/analysis/resource_estimation.py @@ -12,6 +12,13 @@ """Automatically require analysis passes for resource estimation.""" +from __future__ import annotations + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from qiskit.dagcircuit import DAGCircuit + from qiskit.transpiler.basepasses import AnalysisPass from qiskit.transpiler.passes.analysis.depth import Depth from qiskit.transpiler.passes.analysis.width import Width @@ -25,16 +32,18 @@ class ResourceEstimation(AnalysisPass): """Automatically require analysis passes for resource estimation. An analysis pass for automatically running: + * Depth() * Width() * Size() * CountOps() * NumTensorFactors() + * NumQubits() """ - def __init__(self): + def __init__(self) -> None: super().__init__() self.requires += [Depth(), Width(), Size(), CountOps(), NumTensorFactors(), NumQubits()] - def run(self, _): - """Run the ResourceEstimation pass on `dag`.""" + def run(self, dag: DAGCircuit) -> None: + """Run the ResourceEstimation pass on ``dag``.""" diff --git a/qiskit/transpiler/passes/analysis/size.py b/qiskit/transpiler/passes/analysis/size.py index 4d19f547a258..e0d4d379becd 100644 --- a/qiskit/transpiler/passes/analysis/size.py +++ b/qiskit/transpiler/passes/analysis/size.py @@ -12,6 +12,13 @@ """Calculate the size of a DAG circuit.""" +from __future__ import annotations + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from qiskit.dagcircuit import DAGCircuit + from qiskit.transpiler.basepasses import AnalysisPass @@ -21,7 +28,7 @@ class Size(AnalysisPass): The result is saved in ``property_set['size']`` as an integer. """ - def __init__(self, *, recurse=False): + def __init__(self, *, recurse: bool = False) -> None: """ Args: recurse: whether to allow recursion into control flow. If this is ``False`` (default), @@ -31,6 +38,6 @@ def __init__(self, *, recurse=False): super().__init__() self.recurse = recurse - def run(self, dag): - """Run the Size pass on `dag`.""" + def run(self, dag: DAGCircuit) -> None: + """Run the Size pass on ``dag``.""" self.property_set["size"] = dag.size(recurse=self.recurse) diff --git a/qiskit/transpiler/passes/analysis/width.py b/qiskit/transpiler/passes/analysis/width.py index e0e091af748e..daf760e52e52 100644 --- a/qiskit/transpiler/passes/analysis/width.py +++ b/qiskit/transpiler/passes/analysis/width.py @@ -12,6 +12,13 @@ """Calculate the width of a DAG circuit.""" +from __future__ import annotations + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from qiskit.dagcircuit import DAGCircuit + from qiskit.transpiler.basepasses import AnalysisPass @@ -22,6 +29,6 @@ class Width(AnalysisPass): contains the number of qubits + the number of clbits. """ - def run(self, dag): - """Run the Width pass on `dag`.""" + def run(self, dag: DAGCircuit) -> None: + """Run the Width pass on ``dag``.""" self.property_set["width"] = dag.width() diff --git a/qiskit/transpiler/passes/utils/contains_instruction.py b/qiskit/transpiler/passes/utils/contains_instruction.py index 233b029f9b08..f14fe78f79ba 100644 --- a/qiskit/transpiler/passes/utils/contains_instruction.py +++ b/qiskit/transpiler/passes/utils/contains_instruction.py @@ -10,7 +10,15 @@ # copyright notice, and modified files need to carry a notice indicating # that they have been altered from the originals. -"""Check if a property reached a fixed point.""" +"""Check if the DAG contains a specific instruction.""" + +from __future__ import annotations + +from collections.abc import Iterable +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from qiskit.dagcircuit import DAGCircuit from qiskit.transpiler.basepasses import AnalysisPass @@ -23,14 +31,13 @@ class ContainsInstruction(AnalysisPass): that instruction and ``False`` if it does not. """ - def __init__(self, instruction_name, recurse: bool = True): - """ContainsInstruction initializer. - + def __init__(self, instruction_name: str | Iterable[str], recurse: bool = True) -> None: + """ Args: - instruction_name (str | Iterable[str]): The instruction or instructions to check are in + instruction_name: The instruction or instructions to check are in the DAG. The output in the property set is set to ``contains_`` prefixed on each value for this parameter. - recurse (bool): if ``True`` (default), then recurse into control-flow operations. + recurse: if ``True`` (default), then recurse into control-flow operations. """ super().__init__() self._instruction_names = ( @@ -38,8 +45,8 @@ def __init__(self, instruction_name, recurse: bool = True): ) self._recurse = recurse - def run(self, dag): - """Run the ContainsInstruction pass on dag.""" + def run(self, dag: DAGCircuit) -> None: + """Run the ContainsInstruction pass on ``dag``.""" names = dag.count_ops(recurse=self._recurse) for name in self._instruction_names: self.property_set[f"contains_{name}"] = name in names diff --git a/qiskit/transpiler/passes/utils/dag_fixed_point.py b/qiskit/transpiler/passes/utils/dag_fixed_point.py index a0d08a5b3252..e30e33c3ea81 100644 --- a/qiskit/transpiler/passes/utils/dag_fixed_point.py +++ b/qiskit/transpiler/passes/utils/dag_fixed_point.py @@ -12,7 +12,13 @@ """Check if the DAG has reached a fixed point.""" +from __future__ import annotations + from copy import deepcopy +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from qiskit.dagcircuit import DAGCircuit from qiskit.transpiler.basepasses import AnalysisPass @@ -25,8 +31,8 @@ class DAGFixedPoint(AnalysisPass): ``property_set['dag_fixed_point']`` as a boolean. """ - def run(self, dag): - """Run the DAGFixedPoint pass on `dag`.""" + def run(self, dag: DAGCircuit) -> None: + """Run the DAGFixedPoint pass on ``dag``.""" if self.property_set["_dag_fixed_point_previous_dag"] is None: self.property_set["dag_fixed_point"] = False else: diff --git a/qiskit/transpiler/passes/utils/fixed_point.py b/qiskit/transpiler/passes/utils/fixed_point.py index ce07f55e582b..c25d56f1038c 100644 --- a/qiskit/transpiler/passes/utils/fixed_point.py +++ b/qiskit/transpiler/passes/utils/fixed_point.py @@ -12,7 +12,13 @@ """Check if a property reached a fixed point.""" +from __future__ import annotations + from copy import deepcopy +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from qiskit.dagcircuit import DAGCircuit from qiskit.transpiler.basepasses import AnalysisPass @@ -25,20 +31,18 @@ class FixedPoint(AnalysisPass): as a boolean. """ - def __init__(self, property_to_check): - """FixedPoint initializer. - + def __init__(self, property_to_check: str) -> None: + """ Args: - property_to_check (str): The property to check if a fixed point was reached. + property_to_check: The property to check if a fixed point was reached. """ super().__init__() self._property = property_to_check - def run(self, dag): - """Run the FixedPoint pass on `dag`.""" + def run(self, dag: DAGCircuit) -> None: + """Run the FixedPoint pass on ``dag``.""" current_value = self.property_set[self._property] fixed_point_previous_property = f"_fixed_point_previous_{self._property}" - if self.property_set[fixed_point_previous_property] is None: self.property_set[f"{self._property}_fixed_point"] = False else: