From 12bf8b0e870bc73ed78848b5f33fa42f0ce3225e Mon Sep 17 00:00:00 2001 From: changsookim <> Date: Thu, 24 Oct 2024 13:35:13 +0400 Subject: [PATCH] fix: refactor connectivity graphs in test files --- tests/conftest.py | 49 ++++++++++ tests/test_transpiler_optimizer.py | 18 +--- tests/test_transpiler_pipeline.py | 36 +++----- tests/test_transpiler_placer.py | 64 ++++++------- tests/test_transpiler_router.py | 139 ++++++++++++++++------------- 5 files changed, 170 insertions(+), 136 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index db51f97456..5aaaf1a129 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,6 +5,7 @@ import sys +import networkx as nx import pytest from qibo.backends import _Global, construct_backend @@ -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__ diff --git a/tests/test_transpiler_optimizer.py b/tests/test_transpiler_optimizer.py index 457e25ecf7..cd55115a9b 100644 --- a/tests/test_transpiler_optimizer.py +++ b/tests/test_transpiler_optimizer.py @@ -6,26 +6,14 @@ 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()) @@ -33,7 +21,7 @@ def test_preprocessing_same(): 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()) diff --git a/tests/test_transpiler_pipeline.py b/tests/test_transpiler_pipeline.py index 82fe4c9419..4affd8026a 100644 --- a/tests/test_transpiler_pipeline.py +++ b/tests/test_transpiler_pipeline.py @@ -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"] ) @@ -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) @@ -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) @@ -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] @@ -165,7 +153,7 @@ 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)) @@ -173,7 +161,7 @@ def test_is_satisfied_false_decomposition(): 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)) @@ -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 = [] @@ -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 = [] @@ -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( diff --git a/tests/test_transpiler_placer.py b/tests/test_transpiler_placer.py index 8a2532583c..86295022cb 100644 --- a/tests/test_transpiler_placer.py +++ b/tests/test_transpiler_placer.py @@ -19,18 +19,6 @@ from qibo.transpiler.router import ShortestPaths -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 star_circuit(names=["q0", "q1", "q2", "q3", "q4"]): circuit = Circuit(5, wire_names=names) for i in range(1, 5): @@ -38,7 +26,7 @@ def star_circuit(names=["q0", "q1", "q2", "q3", "q4"]): return circuit -def test_assert_placement_true(): +def test_assert_placement_true(star_connectivity): circuit = Circuit(5) assert_placement(circuit, connectivity=star_connectivity()) @@ -46,7 +34,7 @@ def test_assert_placement_true(): @pytest.mark.parametrize( "qubits, names", [(5, ["A", "B", "C", "D", "F"]), (3, ["A", "B", "C"])] ) -def test_assert_placement_false(qubits, names): +def test_assert_placement_false(qubits, names, star_connectivity): connectivity = star_connectivity() circuit = Circuit(qubits, wire_names=names) with pytest.raises(PlacementError): @@ -54,7 +42,7 @@ def test_assert_placement_false(qubits, names): @pytest.mark.parametrize("qubits", [10, 1]) -def test_assert_placement_error(qubits): +def test_assert_placement_error(qubits, star_connectivity): connectivity = star_connectivity() circuit = Circuit(qubits) with pytest.raises(PlacementError): @@ -62,17 +50,17 @@ def test_assert_placement_error(qubits): @pytest.mark.parametrize("names", [["A", "B", "C", "D", "E"], [0, 1, 2, 3, 4]]) -def test_mapping_consistency(names): +def test_mapping_consistency(names, star_connectivity): assert_mapping_consistency(names, star_connectivity(names)) -def test_mapping_consistency_error(): +def test_mapping_consistency_error(star_connectivity): with pytest.raises(PlacementError): assert_mapping_consistency(["A", "B", "C", "D", "F"], star_connectivity()) @pytest.mark.parametrize("names", [["A", "B", "C", "D", "E"], [0, 1, 2, 3, 4]]) -def test_mapping_consistency_restricted(names): +def test_mapping_consistency_restricted(names, star_connectivity): connectivity = star_connectivity(names) on_qubit = [names[0], names[2]] restricted_connectivity = restrict_connectivity_qubits(connectivity, on_qubit) @@ -80,7 +68,7 @@ def test_mapping_consistency_restricted(names): @pytest.mark.parametrize("names", [["A", "B", "C", "D", "E"], [0, 1, 2, 3, 4]]) -def test_mapping_consistency_restricted_error(names): +def test_mapping_consistency_restricted_error(names, star_connectivity): connectivity = star_connectivity(names) on_qubit = [names[0], names[2]] restricted_connectivity = restrict_connectivity_qubits(connectivity, on_qubit) @@ -104,7 +92,7 @@ def test_gates_qubits_pairs_error(): gates_qubits_pairs = _find_gates_qubits_pairs(circuit) -def test_trivial(): +def test_trivial(star_connectivity): names = ["q4", "q3", "q2", "q1", "q0"] circuit = Circuit(5, wire_names=names) connectivity = star_connectivity(names) @@ -114,14 +102,14 @@ def test_trivial(): assert_placement(circuit, connectivity) -def test_trivial_error(): +def test_trivial_error(star_connectivity): connectivity = star_connectivity() placer = Trivial(connectivity=connectivity) with pytest.raises(ValueError): placer() -def test_trivial_restricted(): +def test_trivial_restricted(star_connectivity): names = ["q0", "q2"] circuit = Circuit(2, wire_names=names) connectivity = star_connectivity() @@ -136,7 +124,7 @@ def test_trivial_restricted(): "custom_layout", [["E", "D", "C", "B", "A"], {"E": 0, "D": 1, "C": 2, "B": 3, "A": 4}], ) -def test_custom(custom_layout): +def test_custom(custom_layout, star_connectivity): circuit = Circuit(5) connectivity = star_connectivity(["A", "B", "C", "D", "E"]) placer = Custom(connectivity=connectivity, initial_map=custom_layout) @@ -147,7 +135,7 @@ def test_custom(custom_layout): @pytest.mark.parametrize( "custom_layout", [[4, 3, 2, 1, 0], {4: 0, 3: 1, 2: 2, 1: 3, 0: 4}] ) -def test_custom_int(custom_layout): +def test_custom_int(custom_layout, star_connectivity): names = [0, 1, 2, 3, 4] circuit = Circuit(5, wire_names=names) connectivity = star_connectivity(names) @@ -157,7 +145,7 @@ def test_custom_int(custom_layout): @pytest.mark.parametrize("custom_layout", [["D", "C"], {"C": 1, "D": 0}]) -def test_custom_restricted(custom_layout): +def test_custom_restricted(custom_layout, star_connectivity): circuit = Circuit(2, wire_names=["C", "D"]) connectivity = star_connectivity(["A", "B", "C", "D", "E"]) restricted_connectivity = restrict_connectivity_qubits(connectivity, ["C", "D"]) @@ -167,7 +155,7 @@ def test_custom_restricted(custom_layout): assert_placement(circuit, restricted_connectivity) -def test_custom_error_circuit(): +def test_custom_error_circuit(star_connectivity): circuit = Circuit(3) custom_layout = [4, 3, 2, 1, 0] connectivity = star_connectivity(names=custom_layout) @@ -176,7 +164,7 @@ def test_custom_error_circuit(): placer(circuit) -def test_custom_error_type(): +def test_custom_error_type(star_connectivity): circuit = Circuit(5) connectivity = star_connectivity() placer = Custom(connectivity=connectivity, initial_map=1) @@ -184,7 +172,7 @@ def test_custom_error_type(): placer(circuit) -def test_subgraph_perfect(): +def test_subgraph_perfect(star_connectivity): connectivity = star_connectivity() placer = Subgraph(connectivity=connectivity) circuit = star_circuit() @@ -207,7 +195,7 @@ def imperfect_circuit(): return circuit -def test_subgraph_non_perfect(): +def test_subgraph_non_perfect(star_connectivity): connectivity = star_connectivity() placer = Subgraph(connectivity=connectivity) circuit = imperfect_circuit() @@ -215,7 +203,7 @@ def test_subgraph_non_perfect(): assert_placement(circuit, connectivity) -def test_subgraph_error(): +def test_subgraph_error(star_connectivity): connectivity = star_connectivity() placer = Subgraph(connectivity=connectivity) circuit = Circuit(5) @@ -223,7 +211,7 @@ def test_subgraph_error(): placer(circuit) -def test_subgraph_restricted(): +def test_subgraph_restricted(star_connectivity): circuit = Circuit(4) circuit.add(gates.CNOT(0, 3)) circuit.add(gates.CNOT(0, 1)) @@ -242,7 +230,7 @@ def test_subgraph_restricted(): @pytest.mark.parametrize("reps", [1, 10, 100]) @pytest.mark.parametrize("names", [["A", "B", "C", "D", "E"], [0, 1, 2, 3, 4]]) -def test_random(reps, names): +def test_random(reps, names, star_connectivity): connectivity = star_connectivity(names) placer = Random(connectivity=connectivity, samples=reps) circuit = star_circuit(names=names) @@ -250,7 +238,7 @@ def test_random(reps, names): assert_placement(circuit, connectivity) -def test_random_restricted(): +def test_random_restricted(star_connectivity): names = [0, 1, 2, 3, 4] circuit = Circuit(4, wire_names=names[:4]) circuit.add(gates.CNOT(1, 3)) @@ -268,7 +256,7 @@ def test_random_restricted(): @pytest.mark.parametrize("ngates", [None, 5, 13]) @pytest.mark.parametrize("names", [["A", "B", "C", "D", "E"], [0, 1, 2, 3, 4]]) -def test_reverse_traversal(ngates, names): +def test_reverse_traversal(ngates, names, star_connectivity): circuit = star_circuit(names=names) connectivity = star_connectivity(names=names) routing = ShortestPaths(connectivity=connectivity) @@ -277,7 +265,7 @@ def test_reverse_traversal(ngates, names): assert_placement(circuit, connectivity) -def test_reverse_traversal_no_gates(): +def test_reverse_traversal_no_gates(star_connectivity): connectivity = star_connectivity() routing = ShortestPaths(connectivity=connectivity) placer = ReverseTraversal(connectivity, routing, depth=10) @@ -286,7 +274,7 @@ def test_reverse_traversal_no_gates(): placer(circuit) -def test_reverse_traversal_restricted(): +def test_reverse_traversal_restricted(star_connectivity): circuit = Circuit(4) circuit.add(gates.CNOT(1, 3)) circuit.add(gates.CNOT(2, 1)) @@ -306,7 +294,7 @@ def test_reverse_traversal_restricted(): assert_placement(circuit, restricted_connectivity) -def test_star_connectivity_placer(): +def test_star_connectivity_placer(star_connectivity): circ = Circuit(5) circ.add(gates.CZ(0, 1)) circ.add(gates.CZ(1, 2)) @@ -319,7 +307,7 @@ def test_star_connectivity_placer(): @pytest.mark.parametrize("first", [True, False]) -def test_star_connectivity_placer_error(first): +def test_star_connectivity_placer_error(first, star_connectivity): circ = Circuit(5) if first: circ.add(gates.CZ(0, 1)) diff --git a/tests/test_transpiler_router.py b/tests/test_transpiler_router.py index 8583d9577b..6d10dfd5b1 100644 --- a/tests/test_transpiler_router.py +++ b/tests/test_transpiler_router.py @@ -32,42 +32,6 @@ ) -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 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 - - -def line_connectivity(n, names): - 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 - - def generate_random_circuit(nqubits, ngates, names=None, seed=42): """Generate a random circuit with RX and CZ gates.""" np.random.seed(seed) @@ -120,26 +84,26 @@ def matched_circuit(names): return circuit -def test_assert_connectivity(): +def test_assert_connectivity(star_connectivity): names = ["A", "B", "C", "D", "E"] assert_connectivity(star_connectivity(names), matched_circuit(names)) -def test_assert_connectivity_false(): +def test_assert_connectivity_false(star_connectivity): circuit = Circuit(5) circuit.add(gates.CZ(0, 1)) with pytest.raises(ConnectivityError): assert_connectivity(star_connectivity(), circuit) -def test_assert_connectivity_3q(): +def test_assert_connectivity_3q(star_connectivity): circuit = Circuit(5) circuit.add(gates.TOFFOLI(0, 1, 2)) with pytest.raises(ConnectivityError): assert_connectivity(star_connectivity(), circuit) -def test_bell_state_3q(): +def test_bell_state_3q(line_connectivity): from qibo.transpiler.pipeline import _transpose_qubits circuit = Circuit(3) @@ -163,10 +127,33 @@ def test_bell_state_3q(): @pytest.mark.parametrize("ngates", [5, 25]) @pytest.mark.parametrize("placer", [Trivial, Random]) -@pytest.mark.parametrize("connectivity", [star_connectivity(), grid_connectivity()]) -def test_random_circuits_5q(ngates, placer, connectivity): - placer = placer(connectivity=connectivity) - transpiler = ShortestPaths(connectivity=connectivity) +def test_random_circuits_5q(ngates, placer, star_connectivity): + connectivity = star_connectivity() + placer = placer(connectivity) + transpiler = ShortestPaths(connectivity) + + circuit = generate_random_circuit(nqubits=5, ngates=ngates) + original_circuit = circuit.copy() + placer(circuit) + transpiled_circuit, final_qubit_map = transpiler(circuit) + + assert transpiler.added_swaps >= 0 + assert_connectivity(connectivity, transpiled_circuit) + assert_placement(transpiled_circuit, connectivity) + assert ngates + transpiler.added_swaps == transpiled_circuit.ngates + assert_circuit_equivalence( + original_circuit=original_circuit, + transpiled_circuit=transpiled_circuit, + final_map=final_qubit_map, + ) + + +@pytest.mark.parametrize("ngates", [5, 25]) +@pytest.mark.parametrize("placer", [Trivial, Random]) +def test_random_circuits_5q_grid(ngates, placer, grid_connectivity): + connectivity = grid_connectivity() + placer = placer(connectivity) + transpiler = ShortestPaths(connectivity) circuit = generate_random_circuit(nqubits=5, ngates=ngates) original_circuit = circuit.copy() @@ -186,7 +173,7 @@ def test_random_circuits_5q(ngates, placer, connectivity): @pytest.mark.parametrize("nqubits", [11, 12, 13, 14, 15]) @pytest.mark.parametrize("ngates", [30, 50]) -def test_random_circuits_15q_50g(nqubits, ngates): +def test_random_circuits_15q_50g(nqubits, ngates, line_connectivity): connectivity = line_connectivity(nqubits, None) placer = Random(connectivity=connectivity) transpiler = Sabre(connectivity=connectivity) @@ -207,7 +194,7 @@ def test_random_circuits_15q_50g(nqubits, ngates): ) -def test_star_circuit(): +def test_star_circuit(star_connectivity): connectivity = star_connectivity() circuit = star_circuit() placer = Subgraph(connectivity=connectivity) @@ -226,7 +213,7 @@ def test_star_circuit(): ) -def test_star_circuit_custom_map(): +def test_star_circuit_custom_map(star_connectivity): connectivity = star_connectivity() circuit = star_circuit() placer = Custom( @@ -247,7 +234,7 @@ def test_star_circuit_custom_map(): ) -def test_routing_with_measurements(): +def test_routing_with_measurements(star_connectivity): connectivity = star_connectivity() circuit = Circuit(5) circuit.add(gates.CNOT(0, 1)) @@ -268,7 +255,7 @@ def test_routing_with_measurements(): ) -def test_sabre_looping(): +def test_sabre_looping(line_connectivity): # Setup where the looping occurs # Line connectivity, gates with gate_array, Trivial placer @@ -304,7 +291,7 @@ def test_sabre_looping(): ) -def test_sabre_shortest_path_routing(): +def test_sabre_shortest_path_routing(line_connectivity): gate_array = [(0, 9), (5, 9), (2, 8)] # The gate (2, 8) should be routed next loop_circ = Circuit(10) @@ -411,7 +398,7 @@ def test_circuit_map(): "names", [["q0", "q1", "q2", "q3", "q4"], [0, 1, 2, 3, 4], ["A", "B", "C", "D", "E"]], ) -def test_sabre_matched(names): +def test_sabre_matched(names, star_connectivity): connectivity = star_connectivity(names=names) circuit = matched_circuit(names) original_circuit = circuit.copy() @@ -431,7 +418,7 @@ def test_sabre_matched(names): @pytest.mark.parametrize("seed", [42]) -def test_sabre_simple(seed): +def test_sabre_simple(seed, star_connectivity): connectivity = star_connectivity() circ = Circuit(5) circ.add(gates.CZ(0, 1)) @@ -459,14 +446,44 @@ def test_sabre_simple(seed): @pytest.mark.parametrize("look", [0, 5]) @pytest.mark.parametrize("decay", [0.5, 1.0]) @pytest.mark.parametrize("placer_param", [Trivial, Random]) -@pytest.mark.parametrize("connectivity", [star_connectivity(), grid_connectivity()]) -def test_sabre_random_circuits(n_gates, look, decay, placer_param, connectivity): +def test_sabre_random_circuits(n_gates, look, decay, placer_param, star_connectivity): + connectivity = star_connectivity() + circuit = generate_random_circuit(nqubits=5, ngates=n_gates) + measurement = gates.M(*range(5)) + circuit.add(measurement) + original_circuit = circuit.copy() + placer = placer_param(connectivity) + router = Sabre(connectivity, lookahead=look, decay_lookahead=decay) + + placer(circuit) + transpiled_circuit, final_qubit_map = router(circuit) + + assert router.added_swaps >= 0 + assert_connectivity(connectivity, transpiled_circuit) + assert_placement(transpiled_circuit, connectivity) + assert n_gates + router.added_swaps + 1 == transpiled_circuit.ngates + assert_circuit_equivalence( + original_circuit=original_circuit, + transpiled_circuit=transpiled_circuit, + final_map=final_qubit_map, + ) + assert transpiled_circuit.queue[-1].register_name == measurement.register_name + + +@pytest.mark.parametrize("n_gates", [10, 40]) +@pytest.mark.parametrize("look", [0, 5]) +@pytest.mark.parametrize("decay", [0.5, 1.0]) +@pytest.mark.parametrize("placer_param", [Trivial, Random]) +def test_sabre_random_circuits_grid( + n_gates, look, decay, placer_param, grid_connectivity +): + connectivity = grid_connectivity() circuit = generate_random_circuit(nqubits=5, ngates=n_gates) measurement = gates.M(*range(5)) circuit.add(measurement) original_circuit = circuit.copy() - placer = placer_param(connectivity=connectivity) - router = Sabre(connectivity=connectivity, lookahead=look, decay_lookahead=decay) + placer = placer_param(connectivity) + router = Sabre(connectivity, lookahead=look, decay_lookahead=decay) placer(circuit) transpiled_circuit, final_qubit_map = router(circuit) @@ -483,7 +500,7 @@ def test_sabre_random_circuits(n_gates, look, decay, placer_param, connectivity) assert transpiled_circuit.queue[-1].register_name == measurement.register_name -def test_sabre_memory_map(): +def test_sabre_memory_map(star_connectivity): placer = Trivial() layout_circ = Circuit(5) placer(layout_circ) @@ -509,7 +526,7 @@ def test_sabre_intermediate_measurements(): @pytest.mark.parametrize("router_algorithm", [Sabre, ShortestPaths]) -def test_restrict_qubits(router_algorithm): +def test_restrict_qubits(router_algorithm, star_connectivity): circ = Circuit(3) circ.add(gates.CZ(0, 1)) circ.add(gates.CZ(0, 2)) @@ -531,7 +548,7 @@ def test_restrict_qubits(router_algorithm): assert routed_circ.wire_names == ["q0", "q3", "q2"] -def test_star_error_multi_qubit(): +def test_star_error_multi_qubit(star_connectivity): circuit = Circuit(3) circuit.add(gates.TOFFOLI(0, 1, 2)) connectivity = star_connectivity(middle_qubit_idx=2) @@ -555,7 +572,9 @@ def test_star_error_multi_qubit(): "names", [["q0", "q1", "q2", "q3", "q4"], [0, 1, 2, 3, 4], ["A", "B", "C", "D", "E"]], ) -def test_star_router(nqubits, depth, middle_qubit, measurements, unitaries, names): +def test_star_router( + nqubits, depth, middle_qubit, measurements, unitaries, names, star_connectivity +): unitary_dim = min(2, nqubits) connectivity = star_connectivity(names=names, middle_qubit_idx=middle_qubit) if unitaries: