Skip to content

Commit

Permalink
fix: type errors, Preprocessor connectivity check, utils.py -> assert…
Browse files Browse the repository at this point in the history
…s.py
  • Loading branch information
changsookim committed Nov 25, 2024
1 parent 1f55f3c commit 797331e
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/qibo/models/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions src/qibo/transpiler/optimizer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

import networkx as nx

from qibo import gates
Expand All @@ -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.",
Expand Down
4 changes: 2 additions & 2 deletions src/qibo/transpiler/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
Expand Down
12 changes: 7 additions & 5 deletions src/qibo/transpiler/placer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions src/qibo/transpiler/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion tests/test_transpiler_decompositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 0 additions & 5 deletions tests/test_transpiler_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_transpiler_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_transpiler_placer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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]):
Expand Down
12 changes: 6 additions & 6 deletions tests/test_transpiler_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_transpiler_unroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit 797331e

Please sign in to comment.