diff --git a/src/qibo/transpiler/pipeline.py b/src/qibo/transpiler/pipeline.py index 06b047d773..7d9b68e130 100644 --- a/src/qibo/transpiler/pipeline.py +++ b/src/qibo/transpiler/pipeline.py @@ -180,6 +180,8 @@ class Passes: Defaults to :math:`qibo.transpiler.unroller.NativeGates.default`. on_qubits (list, optional): list of physical qubits to be used. If "None" all qubits are used. Defaults to ``None``. + int_qubit_name (bool, optional): if `True` the `final_layout` keys are + cast to integers. """ def __init__( @@ -188,12 +190,15 @@ def __init__( connectivity: nx.Graph = None, native_gates: NativeGates = NativeGates.default(), on_qubits: list = None, + int_qubit_names: bool = False, ): if on_qubits is not None: connectivity = restrict_connectivity_qubits(connectivity, on_qubits) self.connectivity = connectivity self.native_gates = native_gates self.passes = self.default() if passes is None else passes + self.initial_layout = None + self.int_qubit_names = int_qubit_names def default(self): """Return the default transpiler pipeline for the required hardware connectivity.""" @@ -215,8 +220,12 @@ def default(self): return default_passes def __call__(self, circuit): - self.initial_layout = None - final_layout = None + """ + This function returns the compiled circuits and the dictionary mapping + physical (keys) to logical (values) qubit. If `int_qubit_name` is `True` + each key `i` correspond to the `i-th` qubit in the graph. + """ + final_layout = self.initial_layout for transpiler_pass in self.passes: if isinstance(transpiler_pass, Optimizer): transpiler_pass.connectivity = self.connectivity @@ -247,7 +256,9 @@ def __call__(self, circuit): TranspilerPipelineError, f"Unrecognised transpiler pass: {transpiler_pass}", ) - + # TODO: use directly integers keys + if self.int_qubit_names: + final_layout = {int(key[1:]): value for key, value in final_layout.items()} return circuit, final_layout def is_satisfied(self, circuit: Circuit): diff --git a/tests/test_transpiler_pipeline.py b/tests/test_transpiler_pipeline.py index 94d2c4b539..c95ef68cc2 100644 --- a/tests/test_transpiler_pipeline.py +++ b/tests/test_transpiler_pipeline.py @@ -129,6 +129,16 @@ def test_assert_circuit_equivalence_false(): assert_circuit_equivalence(circ1, circ2, final_map=final_map) +def test_int_qubit_names(): + circ = Circuit(2) + final_map = {i: i for i in range(5)} + default_transpiler = Passes( + passes=None, connectivity=star_connectivity(), int_qubit_names=True + ) + _, final_layout = default_transpiler(circ) + assert final_map == final_layout + + def test_assert_circuit_equivalence_wrong_nqubits(): circ1 = Circuit(1) circ2 = Circuit(2)