Fix instruction durations in transpile() with BackendV2 (#8001)

* Fix instruction durations in transpile() with BackendV2

When running transpile() with BackendV2 based backends the instruction
durations property from the backend would not be processed correctly
resulting in the absence of the default durations for instructions
supported on the target backend. This commit fixes this by correctly
handling BackendV2 based backends and using those instruction durations
by default for transpile().

* Update test/python/compiler/test_transpiler.py

Co-authored-by: Kevin Hartman <kevin@hart.mn>

Co-authored-by: Kevin Hartman <kevin@hart.mn>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
Matthew Treinish 2022-05-02 11:12:32 -04:00 committed by GitHub
parent 7505b1dc6c
commit 49f39e3a6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 6 deletions

View File

@ -921,11 +921,15 @@ def _parse_instruction_durations(backend, inst_durations, dt, circuits):
take precedence over backend durations, but be superceded by ``inst_duration``s.
"""
if not inst_durations:
backend_durations = InstructionDurations()
try:
backend_durations = InstructionDurations.from_backend(backend)
except AttributeError:
pass
backend_version = getattr(backend, "version", 0)
if backend_version <= 1:
backend_durations = InstructionDurations()
try:
backend_durations = InstructionDurations.from_backend(backend)
except AttributeError:
pass
else:
backend_durations = backend.instruction_durations
durations = []
for circ in circuits:

View File

@ -0,0 +1,8 @@
---
fixes:
- |
Fixed an issue with the :func:`~.transpile` function when run with a
:class:`~.BackendV2` based backend and setting the ``scheduling_method``
keyword argument. Previously, the function would not correctly process
the default durations of the instructions supported by the backend which
would lead to an error.

View File

@ -35,7 +35,7 @@ from qiskit.converters import circuit_to_dag
from qiskit.circuit.library import CXGate, U3Gate, U2Gate, U1Gate, RXGate, RYGate, RZGate, UGate
from qiskit.circuit.measure import Measure
from qiskit.test import QiskitTestCase
from qiskit.test.mock import FakeMelbourne, FakeRueschlikon, FakeAlmaden
from qiskit.test.mock import FakeMelbourne, FakeRueschlikon, FakeAlmaden, FakeMumbaiV2
from qiskit.transpiler import Layout, CouplingMap
from qiskit.transpiler import PassManager
from qiskit.transpiler.target import Target
@ -1242,6 +1242,18 @@ class TestTranspile(QiskitTestCase):
out = transpile(qc, dt=1e-9)
self.assertEqual(out.data[0][0].unit, "dt")
def test_scheduling_backend_v2(self):
"""Test that scheduling method works with Backendv2."""
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()
backend = FakeMumbaiV2()
out = transpile([qc, qc], backend, scheduling_method="alap")
self.assertIn("delay", out[0].count_ops())
self.assertIn("delay", out[1].count_ops())
@data(1, 2, 3)
def test_no_infinite_loop(self, optimization_level):
"""Verify circuit cost always descends and optimization does not flip flop indefinitely."""