Skip to content

Commit

Permalink
Don't run ElidePermutation if routing is disabled (#13184)
Browse files Browse the repository at this point in the history
* Don't run ElidePermutation if routing is disabled

We have a routing plugin named "none" which is used to assert that no
routing should be performed (it errors if there is routing required).
We were previously running the ElidePermutations pass in this case which
is not the expected or correct behavior because it causes the same kind
of permutation as routing and would eliminate and swap gates in the
circuit. As the typical use case for routing_method='none' is when
you've already routed a circuit (or constructed it by hand to match the
connectivity constraints) we shouldn't be doing this. This commit fixes
this behavior so if the routing_method is set to "none" we no longer run
the ElidePermutations pass.

Fixes #13144

* Move release note to the correct location
  • Loading branch information
mtreinish committed Sep 19, 2024
1 parent e97f31b commit 9a896d3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
3 changes: 2 additions & 1 deletion qiskit/transpiler/preset_passmanagers/builtin_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
pass_manager_config.hls_config,
pass_manager_config.qubits_initially_zero,
)
init.append(ElidePermutations())
if pass_manager_config.routing_method != "none":
init.append(ElidePermutations())
init.append(RemoveDiagonalGatesBeforeMeasure())
init.append(
InverseCancellation(
Expand Down
16 changes: 16 additions & 0 deletions releasenotes/notes/no-elide-routing-none-7c1bebf1283d48c0.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
fixes:
- |
Fixed an issue when running :func:`.transpile` or
:meth:`~.StagedPassManager.run` on a pass manager generated by
:func:`.generate_preset_pass_manager` using ``optimization_level`` 2 or 3
when the ``routing_method`` argument is set to ``"none"`` to explicitly
disable routing. Previously under these conditions the transpiler would run
the :class:`.ElidePermutation` pass as part of the init stage as under normal
conditions this is a useful optimization to remove :class:`.SwapGate`
and :class:`.PermutationGate` instances from the circuit. But when
``routing_method="none"`` this optimization wasn't expected as it permutes
the circuit in a similar manner to routing which shouldn't be performed
when ``routing_method="none"``. This has been fixed by no longer
running :class:`.ElidePermutation` if ``routing_method="none"`` is set.
Fixed `#13144 <https://github.com/Qiskit/qiskit/issues/13144>`__
27 changes: 27 additions & 0 deletions test/python/compiler/test_transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,33 @@ def test_do_not_run_gatedirection_with_symmetric_cm(self):
transpile(circ, coupling_map=coupling_map, initial_layout=layout)
self.assertFalse(mock_pass.called)

def test_do_not_run_elide_permutations_no_routing(self):
"""Test the ElidePermutations pass doesn't run if we disable routing
See https://github.com/Qiskit/qiskit/issues/13144 for the details and
reproduce in this test
"""
circuit_routed = QuantumCircuit(4)
circuit_routed.cx(0, 1)
circuit_routed.h(1)
circuit_routed.swap(1, 2)
circuit_routed.cx(2, 3)
pm = generate_preset_pass_manager(
basis_gates=["cx", "sx", "rz"], routing_method="none", optimization_level=2
)
circuit_basis = pm.run(circuit_routed)
cx_gate_qubits = []
for instruction in circuit_basis.data:
if instruction.name == "cx":
cx_gate_qubits.append(instruction.qubits)
# If we did not Elide the existing swaps then the swap should be
# decomposed into 3 cx between 1 and 2 and there are no gates between
# 1 and 3
self.assertIn((circuit_basis.qubits[1], circuit_basis.qubits[2]), cx_gate_qubits)
self.assertIn((circuit_basis.qubits[2], circuit_basis.qubits[1]), cx_gate_qubits)
self.assertNotIn((circuit_basis.qubits[1], circuit_basis.qubits[3]), cx_gate_qubits)
self.assertNotIn((circuit_basis.qubits[3], circuit_basis.qubits[1]), cx_gate_qubits)

def test_optimize_to_nothing(self):
"""Optimize gates up to fixed point in the default pipeline
See https://github.com/Qiskit/qiskit-terra/issues/2035
Expand Down

0 comments on commit 9a896d3

Please sign in to comment.