Skip to content

Commit 6107799

Browse files
Extended the from_backend method of InstructionDurations to support both BackendV1 and BackendV2 (#12941)
* Extended the `from_backend` method of `InstructionDurations` to support `GenericBackendV2` * Simplified the `from_backend` method to allow using `BackendV2`. Added a test and a releasenote. * Made changes to the releasenote.
1 parent b90c7a7 commit 6107799

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

qiskit/transpiler/instruction_durations.py

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from qiskit.circuit import Barrier, Delay, Instruction, ParameterExpression
1919
from qiskit.circuit.duration import duration_in_dt
2020
from qiskit.providers import Backend
21+
from qiskit.providers.backend import BackendV2
2122
from qiskit.transpiler.exceptions import TranspilerError
2223
from qiskit.utils.units import apply_prefix
2324

@@ -75,6 +76,9 @@ def from_backend(cls, backend: Backend):
7576
TranspilerError: If dt and dtm is different in the backend.
7677
"""
7778
# All durations in seconds in gate_length
79+
if isinstance(backend, BackendV2):
80+
return backend.target.durations()
81+
7882
instruction_durations = []
7983
backend_properties = backend.properties()
8084
if hasattr(backend_properties, "_gates"):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
Fixed a bug where :meth:`.InstructionDurations.from_backend` did not work for :class:`.BackendV2` backends.
5+
Fixed `#12760 <https://github.com/Qiskit/qiskit/issues/12760>`.

test/python/transpiler/test_instruction_durations.py

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from qiskit.circuit import Delay, Parameter
1818
from qiskit.providers.fake_provider import Fake27QPulseV1
19+
from qiskit.providers.fake_provider import GenericBackendV2
1920
from qiskit.transpiler.exceptions import TranspilerError
2021
from qiskit.transpiler.instruction_durations import InstructionDurations
2122
from test import QiskitTestCase # pylint: disable=wrong-import-order
@@ -92,3 +93,10 @@ def test_fail_if_get_unbounded_duration_with_unit_conversion_when_dt_is_not_prov
9293
parameterized_delay = Delay(param, "s")
9394
with self.assertRaises(TranspilerError):
9495
InstructionDurations().get(parameterized_delay, 0)
96+
97+
def test_from_backend_with_backendv2(self):
98+
"""Test if `from_backend()` method allows using BackendV2"""
99+
backend = GenericBackendV2(num_qubits=4, calibrate_instructions=True, seed=42)
100+
inst_durations = InstructionDurations.from_backend(backend)
101+
self.assertEqual(inst_durations, backend.target.durations())
102+
self.assertIsInstance(inst_durations, InstructionDurations)

0 commit comments

Comments
 (0)