Skip to content

Commit

Permalink
Fixed handling of a list of coupling_maps passed to transpile() (Qisk…
Browse files Browse the repository at this point in the history
…it#9886)

* Fixed handling of a list of coupling_maps passed to transpile()

Closes Qiskit#9885

* Update qiskit/compiler/transpiler.py

* Add test

* Release note

* Fix test
  • Loading branch information
wshanks authored and king-p3nguin committed May 22, 2023
1 parent 461bf32 commit ba08c04
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
4 changes: 2 additions & 2 deletions qiskit/compiler/transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,8 @@ def callback_func(**kwargs):
ignore_backend_supplied_default_methods,
)
# Get transpile_args to configure the circuit transpilation job(s)
if coupling_map in unique_transpile_args:
cmap_conf = unique_transpile_args["coupling_map"]
if "coupling_map" not in shared_args:
cmap_conf = [a["pass_manager_config"]["coupling_map"] for a in unique_transpile_args]
else:
cmap_conf = [shared_args["coupling_map"]] * len(circuits)
_check_circuits_coupling_map(circuits, cmap_conf, backend)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
fixes:
- |
An issue that prevented :func:`~qiskit.transpile` from working when passed
a list of :class:`~qiskit.transpiler.CouplingMap` objects was fixed. Note
that passing such a list of coupling maps is deprecated and will not be
possible starting with Qiskit Terra 0.25. Fixes
`#9885 <https://github.com/Qiskit/qiskit-terra/issues/9885>`_.
30 changes: 30 additions & 0 deletions test/python/compiler/test_transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1980,6 +1980,36 @@ def test_backendv2_and_coupling_map(self, opt_level):
qubit_1 = tqc.find_bit(inst.qubits[1]).index
self.assertEqual(qubit_1, qubit_0 + 1)

def test_transpile_with_multiple_coupling_maps(self):
"""Test passing a different coupling map for every circuit"""
backend = FakeNairobiV2()

qc = QuantumCircuit(3)
qc.cx(0, 2)

# Add a connection between 0 and 2 so that transpile does not change
# the gates
cmap = CouplingMap.from_line(7)
cmap.add_edge(0, 2)

with self.assertWarnsRegex(
DeprecationWarning, "Passing in a list of arguments for coupling_map is deprecated"
):
# Initial layout needed to prevent transpiler from relabeling
# qubits to avoid doing the swap
tqc = transpile(
[qc] * 2,
backend,
coupling_map=[backend.coupling_map, cmap],
initial_layout=(0, 1, 2),
)

# Check that the two coupling maps were used. The default should
# require swapping (extra cx's) and the second one should not (just the
# original cx).
self.assertEqual(tqc[0].count_ops()["cx"], 4)
self.assertEqual(tqc[1].count_ops()["cx"], 1)

@data(0, 1, 2, 3)
def test_backend_and_custom_gate(self, opt_level):
"""Test transpile() with BackendV2, custom basis pulse gate."""
Expand Down

0 comments on commit ba08c04

Please sign in to comment.