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

Fix instruction durations in transpile() with BackendV2 #8001

Merged
merged 4 commits into from
May 2, 2022
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
14 changes: 9 additions & 5 deletions qiskit/compiler/transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have added a lot of these in different places. I wonder if there is a better way to handle the versions better than if-else statements. I imagine it would be very tedious to update these code (and may miss some) when we have a newer version of backend.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way the transpile function is structured right now there isn't really a better way to handle this. I think in the near term I'm going to look at rewriting how we pass backend information to the pass managers here and use a Target as the single object we pass to the pass manager constructor. As I agree this code is super hard to deal with (not just for this reason, but also because it makes things like #7789 hard too) and very error prone.

backend_durations = InstructionDurations()
try:
backend_durations = InstructionDurations.from_backend(backend)
except AttributeError:
pass
else:
backend_durations = backend.instruction_durations

durations = []
for circ in circuits:
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
14 changes: 13 additions & 1 deletion test/python/compiler/test_transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
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
Expand Down Expand Up @@ -1242,6 +1242,18 @@ def test_delay_converts_to_dt(self):
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."""
Expand Down