From 797331ed774f374481bac37984522a94d9e66cf7 Mon Sep 17 00:00:00 2001 From: changsookim <> Date: Mon, 25 Nov 2024 13:56:01 +0400 Subject: [PATCH] fix: type errors, Preprocessor connectivity check, utils.py -> asserts.py --- src/qibo/models/circuit.py | 2 +- src/qibo/transpiler/{utils.py => asserts.py} | 0 src/qibo/transpiler/optimizer.py | 8 ++++---- src/qibo/transpiler/pipeline.py | 4 ++-- src/qibo/transpiler/placer.py | 12 +++++++----- src/qibo/transpiler/router.py | 10 ++++++---- ...ranspiler_utils.py => test_transpiler_asserts.py} | 6 +++--- tests/test_transpiler_decompositions.py | 2 +- tests/test_transpiler_optimizer.py | 5 ----- tests/test_transpiler_pipeline.py | 2 +- tests/test_transpiler_placer.py | 2 +- tests/test_transpiler_router.py | 12 ++++++------ tests/test_transpiler_unroller.py | 2 +- 13 files changed, 33 insertions(+), 34 deletions(-) rename src/qibo/transpiler/{utils.py => asserts.py} (100%) rename tests/{test_transpiler_utils.py => test_transpiler_asserts.py} (99%) diff --git a/src/qibo/models/circuit.py b/src/qibo/models/circuit.py index c0d6bd2b86..31f8570d88 100644 --- a/src/qibo/models/circuit.py +++ b/src/qibo/models/circuit.py @@ -165,7 +165,7 @@ class Circuit: def __init__( self, - nqubits: Optional[Union[str, list]] = None, + nqubits: Optional[Union[int, list]] = None, accelerators=None, density_matrix: bool = False, wire_names: Optional[list] = None, diff --git a/src/qibo/transpiler/utils.py b/src/qibo/transpiler/asserts.py similarity index 100% rename from src/qibo/transpiler/utils.py rename to src/qibo/transpiler/asserts.py diff --git a/src/qibo/transpiler/optimizer.py b/src/qibo/transpiler/optimizer.py index 3b93e5843a..741078e5e9 100644 --- a/src/qibo/transpiler/optimizer.py +++ b/src/qibo/transpiler/optimizer.py @@ -1,3 +1,5 @@ +from typing import Optional + import networkx as nx from qibo import gates @@ -13,13 +15,11 @@ class Preprocessing(Optimizer): connectivity (:class:`networkx.Graph`): hardware chip connectivity. """ - def __init__(self, connectivity: nx.Graph = None): + def __init__(self, connectivity: Optional[nx.Graph] = None): self.connectivity = connectivity def __call__(self, circuit: Circuit) -> Circuit: - if self.connectivity is None or not all( - qubit in self.connectivity.nodes for qubit in circuit.wire_names - ): + if not all(qubit in self.connectivity.nodes for qubit in circuit.wire_names): raise_error( ValueError, "The circuit qubits are not in the connectivity graph.", diff --git a/src/qibo/transpiler/pipeline.py b/src/qibo/transpiler/pipeline.py index 616cbbc87d..c1aae06775 100644 --- a/src/qibo/transpiler/pipeline.py +++ b/src/qibo/transpiler/pipeline.py @@ -8,12 +8,12 @@ TranspilerPipelineError, ) from qibo.transpiler.abstract import Optimizer, Placer, Router -from qibo.transpiler.unroller import DecompositionError, NativeGates, Unroller -from qibo.transpiler.utils import ( +from qibo.transpiler.asserts import ( assert_connectivity, assert_decomposition, assert_placement, ) +from qibo.transpiler.unroller import DecompositionError, NativeGates, Unroller def restrict_connectivity_qubits(connectivity: nx.Graph, qubits: list[str]): diff --git a/src/qibo/transpiler/placer.py b/src/qibo/transpiler/placer.py index 4539272574..48e2cbd8ec 100644 --- a/src/qibo/transpiler/placer.py +++ b/src/qibo/transpiler/placer.py @@ -8,8 +8,8 @@ from qibo.models import Circuit from qibo.transpiler._exceptions import PlacementError from qibo.transpiler.abstract import Placer, Router +from qibo.transpiler.asserts import assert_placement from qibo.transpiler.router import _find_connected_qubit -from qibo.transpiler.utils import assert_placement def _find_gates_qubits_pairs(circuit: Circuit): @@ -48,7 +48,7 @@ class StarConnectivityPlacer(Placer): connectivity (:class:`networkx.Graph`): star connectivity graph. """ - def __init__(self, connectivity: nx.Graph = None): + def __init__(self, connectivity: Optional[nx.Graph] = None): self.connectivity = connectivity self.middle_qubit = None @@ -114,7 +114,7 @@ class Subgraph(Placer): connectivity (:class:`networkx.Graph`): chip connectivity. """ - def __init__(self, connectivity: nx.Graph = None): + def __init__(self, connectivity: Optional[nx.Graph] = None): self.connectivity = connectivity def __call__(self, circuit: Circuit): @@ -172,7 +172,9 @@ class Random(Placer): initializes a generator with a random seed. Defaults to ``None``. """ - def __init__(self, connectivity: nx.Graph = None, samples: int = 100, seed=None): + def __init__( + self, connectivity: Optional[nx.Graph] = None, samples: int = 100, seed=None + ): self.connectivity = connectivity self.samples = samples self.seed = seed @@ -256,7 +258,7 @@ class ReverseTraversal(Placer): def __init__( self, routing_algorithm: Router, - connectivity: nx.Graph = None, + connectivity: Optional[nx.Graph] = None, depth: Optional[int] = None, ): self.connectivity = connectivity diff --git a/src/qibo/transpiler/router.py b/src/qibo/transpiler/router.py index 027b17119b..704e4435b8 100644 --- a/src/qibo/transpiler/router.py +++ b/src/qibo/transpiler/router.py @@ -10,8 +10,8 @@ from qibo.models import Circuit from qibo.transpiler._exceptions import ConnectivityError from qibo.transpiler.abstract import Router +from qibo.transpiler.asserts import assert_placement from qibo.transpiler.blocks import Block, CircuitBlocks -from qibo.transpiler.utils import assert_placement class StarConnectivityRouter(Router): @@ -31,7 +31,7 @@ class StarConnectivityRouter(Router): connectivity (:class:`networkx.Graph`): star connectivity graph. """ - def __init__(self, connectivity: nx.Graph): + def __init__(self, connectivity: Optional[nx.Graph] = None): self.connectivity = connectivity self.middle_qubit = None @@ -314,7 +314,9 @@ class ShortestPaths(Router): If ``None``, defaults to :math:`42`. Defaults to ``None``. """ - def __init__(self, connectivity: nx.Graph = None, seed: Optional[int] = None): + def __init__( + self, connectivity: Optional[nx.Graph] = None, seed: Optional[int] = None + ): self.connectivity = connectivity self._front_layer = None self.circuit_map = None @@ -601,7 +603,7 @@ class Sabre(Router): def __init__( self, - connectivity: nx.Graph = None, + connectivity: Optional[nx.Graph] = None, lookahead: int = 2, decay_lookahead: float = 0.6, delta: float = 0.001, diff --git a/tests/test_transpiler_utils.py b/tests/test_transpiler_asserts.py similarity index 99% rename from tests/test_transpiler_utils.py rename to tests/test_transpiler_asserts.py index 7476e1d80f..8b0b268003 100644 --- a/tests/test_transpiler_utils.py +++ b/tests/test_transpiler_asserts.py @@ -8,14 +8,14 @@ PlacementError, TranspilerPipelineError, ) -from qibo.transpiler.pipeline import restrict_connectivity_qubits -from qibo.transpiler.unroller import NativeGates -from qibo.transpiler.utils import ( +from qibo.transpiler.asserts import ( assert_circuit_equivalence, assert_connectivity, assert_decomposition, assert_placement, ) +from qibo.transpiler.pipeline import restrict_connectivity_qubits +from qibo.transpiler.unroller import NativeGates def test_assert_circuit_equivalence_equal(): diff --git a/tests/test_transpiler_decompositions.py b/tests/test_transpiler_decompositions.py index 108a4cc416..e26af68841 100644 --- a/tests/test_transpiler_decompositions.py +++ b/tests/test_transpiler_decompositions.py @@ -5,8 +5,8 @@ from qibo.backends import NumpyBackend from qibo.models import Circuit from qibo.quantum_info.random_ensembles import random_unitary +from qibo.transpiler.asserts import assert_decomposition from qibo.transpiler.unroller import NativeGates, translate_gate -from qibo.transpiler.utils import assert_decomposition default_natives = NativeGates.Z | NativeGates.RZ | NativeGates.M | NativeGates.I diff --git a/tests/test_transpiler_optimizer.py b/tests/test_transpiler_optimizer.py index eefcd2c1fb..500f507178 100644 --- a/tests/test_transpiler_optimizer.py +++ b/tests/test_transpiler_optimizer.py @@ -8,11 +8,6 @@ def test_preprocessing_error(star_connectivity): circ = Circuit(7) - - preprocesser = Preprocessing() - with pytest.raises(ValueError): - new_circuit = preprocesser(circuit=circ) - preprocesser = Preprocessing(connectivity=star_connectivity()) with pytest.raises(ValueError): new_circuit = preprocesser(circuit=circ) diff --git a/tests/test_transpiler_pipeline.py b/tests/test_transpiler_pipeline.py index 6092957022..a8e43bc6aa 100644 --- a/tests/test_transpiler_pipeline.py +++ b/tests/test_transpiler_pipeline.py @@ -5,12 +5,12 @@ from qibo import gates from qibo.models import Circuit from qibo.transpiler._exceptions import ConnectivityError, TranspilerPipelineError +from qibo.transpiler.asserts import assert_circuit_equivalence, assert_transpiling from qibo.transpiler.optimizer import Preprocessing from qibo.transpiler.pipeline import Passes, restrict_connectivity_qubits from qibo.transpiler.placer import Random, ReverseTraversal from qibo.transpiler.router import Sabre, ShortestPaths from qibo.transpiler.unroller import NativeGates, Unroller -from qibo.transpiler.utils import assert_circuit_equivalence, assert_transpiling def generate_random_circuit(nqubits, ngates, names=None, seed=42): diff --git a/tests/test_transpiler_placer.py b/tests/test_transpiler_placer.py index f9d41ecc55..13fef50904 100644 --- a/tests/test_transpiler_placer.py +++ b/tests/test_transpiler_placer.py @@ -4,6 +4,7 @@ from qibo import gates from qibo.models import Circuit from qibo.transpiler._exceptions import PlacementError, TranspilerPipelineError +from qibo.transpiler.asserts import assert_placement from qibo.transpiler.pipeline import restrict_connectivity_qubits from qibo.transpiler.placer import ( Random, @@ -13,7 +14,6 @@ _find_gates_qubits_pairs, ) from qibo.transpiler.router import ShortestPaths -from qibo.transpiler.utils import assert_placement def star_circuit(names=[0, 1, 2, 3, 4]): diff --git a/tests/test_transpiler_router.py b/tests/test_transpiler_router.py index 79ec82bbc9..06899e78d3 100644 --- a/tests/test_transpiler_router.py +++ b/tests/test_transpiler_router.py @@ -9,6 +9,12 @@ from qibo.models import Circuit from qibo.quantum_info.random_ensembles import random_unitary from qibo.transpiler._exceptions import ConnectivityError +from qibo.transpiler.asserts import ( + _transpose_qubits, + assert_circuit_equivalence, + assert_connectivity, + assert_placement, +) from qibo.transpiler.pipeline import restrict_connectivity_qubits from qibo.transpiler.placer import Random, StarConnectivityPlacer, Subgraph from qibo.transpiler.router import ( @@ -17,12 +23,6 @@ ShortestPaths, StarConnectivityRouter, ) -from qibo.transpiler.utils import ( - _transpose_qubits, - assert_circuit_equivalence, - assert_connectivity, - assert_placement, -) def line_connectivity(n, names=None): diff --git a/tests/test_transpiler_unroller.py b/tests/test_transpiler_unroller.py index 4d33aa8d10..181a348e82 100644 --- a/tests/test_transpiler_unroller.py +++ b/tests/test_transpiler_unroller.py @@ -3,8 +3,8 @@ from qibo import gates from qibo.models import Circuit from qibo.transpiler._exceptions import DecompositionError +from qibo.transpiler.asserts import assert_decomposition from qibo.transpiler.unroller import NativeGates, Unroller, translate_gate -from qibo.transpiler.utils import assert_decomposition def test_native_gates_from_gatelist():