Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't run ElidePermutation if routing is disabled (backport #13184) #13194

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -150,7 +150,8 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
pass_manager_config.unitary_synthesis_plugin_config,
pass_manager_config.hls_config,
)
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 @@ -863,6 +863,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
Loading