Skip to content

Commit

Permalink
fix: refactor connectivity graphs in test files
Browse files Browse the repository at this point in the history
  • Loading branch information
changsookim committed Oct 24, 2024
1 parent 4e21132 commit 12bf8b0
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 136 deletions.
49 changes: 49 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import sys

import networkx as nx
import pytest

from qibo.backends import _Global, construct_backend
Expand Down Expand Up @@ -81,6 +82,54 @@ def clear():
_Global._transpiler = None


@pytest.fixture
def star_connectivity():
def _star_connectivity(names=["q0", "q1", "q2", "q3", "q4"], middle_qubit_idx=2):
chip = nx.Graph()
chip.add_nodes_from(names)
graph_list = [
(names[i], names[middle_qubit_idx])
for i in range(len(names))
if i != middle_qubit_idx
]
chip.add_edges_from(graph_list)
return chip

return _star_connectivity


@pytest.fixture
def grid_connectivity():
def _grid_connectivity(names=["q0", "q1", "q2", "q3", "q4"]):
chip = nx.Graph()
chip.add_nodes_from(names)
graph_list = [
(names[0], names[1]),
(names[1], names[2]),
(names[2], names[3]),
(names[3], names[0]),
(names[0], names[4]),
]
chip.add_edges_from(graph_list)
return chip

return _grid_connectivity


@pytest.fixture
def line_connectivity():
def _line_connectivity(n, names=None):
if names is None:
names = [f"q{i}" for i in range(n)]
chip = nx.Graph()
chip.add_nodes_from(names)
graph_list = [(names[i], names[i + 1]) for i in range(n - 1)]
chip.add_edges_from(graph_list)
return chip

return _line_connectivity


def pytest_generate_tests(metafunc):
module_name = metafunc.module.__name__

Expand Down
18 changes: 3 additions & 15 deletions tests/test_transpiler_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,22 @@
from qibo.transpiler.optimizer import Preprocessing, Rearrange


def star_connectivity(names=["q0", "q1", "q2", "q3", "q4"], middle_qubit_idx=2):
chip = nx.Graph()
chip.add_nodes_from(names)
graph_list = [
(names[i], names[middle_qubit_idx])
for i in range(len(names))
if i != middle_qubit_idx
]
chip.add_edges_from(graph_list)
return chip


def test_preprocessing_error():
def test_preprocessing_error(star_connectivity):
circ = Circuit(7)
preprocesser = Preprocessing(connectivity=star_connectivity())
with pytest.raises(ValueError):
new_circuit = preprocesser(circuit=circ)


def test_preprocessing_same():
def test_preprocessing_same(star_connectivity):
circ = Circuit(5)
circ.add(gates.CNOT(0, 1))
preprocesser = Preprocessing(connectivity=star_connectivity())
new_circuit = preprocesser(circuit=circ)
assert new_circuit.ngates == 1


def test_preprocessing_add():
def test_preprocessing_add(star_connectivity):
circ = Circuit(3)
circ.add(gates.CNOT(0, 1))
preprocesser = Preprocessing(connectivity=star_connectivity())
Expand Down
36 changes: 13 additions & 23 deletions tests/test_transpiler_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,19 @@ def generate_random_circuit(nqubits, ngates, names=None, seed=42):
return circuit


def star_connectivity(names=["q0", "q1", "q2", "q3", "q4"], middle_qubit_idx=2):
chip = nx.Graph()
chip.add_nodes_from(names)
graph_list = [
(names[i], names[middle_qubit_idx])
for i in range(len(names))
if i != middle_qubit_idx
]
chip.add_edges_from(graph_list)
return chip


def test_restrict_qubits_error_no_subset():
def test_restrict_qubits_error_no_subset(star_connectivity):
with pytest.raises(ConnectivityError) as excinfo:
restrict_connectivity_qubits(star_connectivity(), ["q0", "q1", "q5"])
assert "Some qubits are not in the original connectivity." in str(excinfo.value)


def test_restrict_qubits_error_not_connected():
def test_restrict_qubits_error_not_connected(star_connectivity):
with pytest.raises(ConnectivityError) as excinfo:
restrict_connectivity_qubits(star_connectivity(), ["q0", "q1"])
assert "New connectivity graph is not connected." in str(excinfo.value)


def test_restrict_qubits():
def test_restrict_qubits(star_connectivity):
new_connectivity = restrict_connectivity_qubits(
star_connectivity(["A", "B", "C", "D", "E"]), ["A", "B", "C"]
)
Expand All @@ -85,7 +73,7 @@ def test_restrict_qubits():

@pytest.mark.parametrize("ngates", [5, 10, 50])
@pytest.mark.parametrize("names", [["A", "B", "C", "D", "E"], [0, 1, 2, 3, 4]])
def test_pipeline_default(ngates, names):
def test_pipeline_default(ngates, names, star_connectivity):
circ = generate_random_circuit(nqubits=5, ngates=ngates, names=names)
connectivity = star_connectivity(names)

Expand Down Expand Up @@ -133,7 +121,7 @@ def test_assert_circuit_equivalence_false():
assert_circuit_equivalence(circ1, circ2, final_map=final_map)


def test_int_qubit_names_default():
def test_int_qubit_names_default(star_connectivity):
names = [1244, 1532, 2315, 6563, 8901]
circ = Circuit(5, wire_names=names)
connectivity = star_connectivity(names)
Expand All @@ -156,7 +144,7 @@ def test_error_connectivity():


@pytest.mark.parametrize("qubits", [3, 5])
def test_is_satisfied(qubits):
def test_is_satisfied(qubits, star_connectivity):
default_transpiler = Passes(passes=None, connectivity=star_connectivity())
circuit = Circuit(qubits)
circuit.wire_names = ["q0", "q1", "q2", "q3", "q4"][:qubits]
Expand All @@ -165,15 +153,15 @@ def test_is_satisfied(qubits):
assert default_transpiler.is_satisfied(circuit)


def test_is_satisfied_false_decomposition():
def test_is_satisfied_false_decomposition(star_connectivity):
default_transpiler = Passes(passes=None, connectivity=star_connectivity())
circuit = Circuit(5)
circuit.add(gates.CZ(0, 2))
circuit.add(gates.X(0))
assert not default_transpiler.is_satisfied(circuit)


def test_is_satisfied_false_connectivity():
def test_is_satisfied_false_connectivity(star_connectivity):
default_transpiler = Passes(passes=None, connectivity=star_connectivity())
circuit = Circuit(5)
circuit.add(gates.CZ(0, 1))
Expand All @@ -185,7 +173,7 @@ def test_is_satisfied_false_connectivity():
@pytest.mark.parametrize("ngates", [5, 20])
@pytest.mark.parametrize("placer", [Random, Trivial, ReverseTraversal])
@pytest.mark.parametrize("router", [ShortestPaths, Sabre])
def test_custom_passes(placer, router, ngates, nqubits):
def test_custom_passes(placer, router, ngates, nqubits, star_connectivity):
connectivity = star_connectivity()
circ = generate_random_circuit(nqubits=nqubits, ngates=ngates)
custom_passes = []
Expand Down Expand Up @@ -222,7 +210,9 @@ def test_custom_passes(placer, router, ngates, nqubits):
@pytest.mark.parametrize(
"restrict_names", [["q1", "q2", "q3"], ["q0", "q2", "q4"], ["q4", "q2", "q3"]]
)
def test_custom_passes_restrict(ngates, placer, routing, restrict_names):
def test_custom_passes_restrict(
ngates, placer, routing, restrict_names, star_connectivity
):
connectivity = star_connectivity()
circ = generate_random_circuit(nqubits=3, ngates=ngates, names=restrict_names)
custom_passes = []
Expand Down Expand Up @@ -263,7 +253,7 @@ def test_custom_passes_wrong_pass():
custom_pipeline(circ)


def test_int_qubit_names():
def test_int_qubit_names(star_connectivity):
names = [980, 123, 45, 9, 210464]
connectivity = star_connectivity(names)
transpiler = Passes(
Expand Down
Loading

0 comments on commit 12bf8b0

Please sign in to comment.