Skip to content

Commit

Permalink
Fix missing layout in Commuting2qGateRouter (#12137)
Browse files Browse the repository at this point in the history
* Add layout to property set, add test

* Apply comments from code review

* Add virtual permutation instead of final layout. Test
  • Loading branch information
ElePT committed May 9, 2024
1 parent 4ede470 commit b80885d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,13 @@ def run(self, dag: DAGCircuit) -> DAGCircuit:
if len(dag.qubits) != next(iter(dag.qregs.values())).size:
raise TranspilerError("Circuit has qubits not contained in the qubit register.")

new_dag = dag.copy_empty_like()
# Fix output permutation -- copied from ElidePermutations
input_qubit_mapping = {qubit: index for index, qubit in enumerate(dag.qubits)}
self.property_set["original_layout"] = Layout(input_qubit_mapping)
if self.property_set["original_qubit_indices"] is None:
self.property_set["original_qubit_indices"] = input_qubit_mapping

new_dag = dag.copy_empty_like()
current_layout = Layout.generate_trivial_layout(*dag.qregs.values())

# Used to keep track of nodes that do not decompose using swap strategies.
Expand All @@ -183,6 +188,8 @@ def run(self, dag: DAGCircuit) -> DAGCircuit:

self._compose_non_swap_nodes(accumulator, current_layout, new_dag)

self.property_set["virtual_permutation_layout"] = current_layout

return new_dag

def _compose_non_swap_nodes(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fixes:
- |
Fixed an oversight in the :class:`.Commuting2qGateRouter` transpiler pass where the qreg permutations
were not added to the pass property set, so they would have to be tracked manually by the user. Now it's
possible to access the permutation through the output circuit's ``layout`` property and plug the pass
into any transpilation pipeline without loss of information.
44 changes: 42 additions & 2 deletions test/python/transpiler/test_swap_strategy_router.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2022, 2023.
# (C) Copyright IBM 2022, 2024.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand All @@ -15,12 +15,14 @@
from ddt import ddt, data

from qiskit.circuit import QuantumCircuit, Qubit, QuantumRegister
from qiskit.providers.fake_provider import GenericBackendV2
from qiskit.transpiler import PassManager, CouplingMap, Layout, TranspilerError
from qiskit.circuit.library import PauliEvolutionGate, CXGate
from qiskit.circuit.library.n_local import QAOAAnsatz
from qiskit.converters import circuit_to_dag
from qiskit.exceptions import QiskitError
from qiskit.quantum_info import Pauli, SparsePauliOp
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit.transpiler.passes import FullAncillaAllocation
from qiskit.transpiler.passes import EnlargeWithAncilla
from qiskit.transpiler.passes import ApplyLayout
Expand Down Expand Up @@ -562,9 +564,47 @@ def test_edge_coloring(self, edge_coloring):

self.assertEqual(pm_.run(circ), expected)

def test_permutation_tracking(self):
"""Test that circuit layout permutations are properly tracked in the pass property
set and returned with the output circuit."""

# We use the same scenario as the QAOA test above
mixer = QuantumCircuit(4)
for idx in range(4):
mixer.ry(-idx, idx)

op = SparsePauliOp.from_list([("IZZI", 1), ("ZIIZ", 2), ("ZIZI", 3)])
circ = QAOAAnsatz(op, reps=2, mixer_operator=mixer)

expected_swap_permutation = [3, 1, 2, 0]
expected_full_permutation = [1, 3, 2, 0]

cmap = CouplingMap(couplinglist=[(0, 1), (1, 2), (2, 3)])
swap_strat = SwapStrategy(cmap, swap_layers=[[(0, 1), (2, 3)], [(1, 2)]])

# test standalone
swap_pm = PassManager(
[
FindCommutingPauliEvolutions(),
Commuting2qGateRouter(swap_strat),
]
)
swapped = swap_pm.run(circ.decompose())

# test as pre-routing step
backend = GenericBackendV2(num_qubits=4, coupling_map=[[0, 1], [0, 2], [0, 3]], seed=42)
pm = generate_preset_pass_manager(
optimization_level=3, target=backend.target, seed_transpiler=40
)
pm.pre_routing = swap_pm
full = pm.run(circ.decompose())

self.assertEqual(swapped.layout.routing_permutation(), expected_swap_permutation)
self.assertEqual(full.layout.routing_permutation(), expected_full_permutation)


class TestSwapRouterExceptions(QiskitTestCase):
"""Test that exceptions are properly raises."""
"""Test that exceptions are properly raised."""

def setUp(self):
"""Setup useful variables."""
Expand Down

0 comments on commit b80885d

Please sign in to comment.