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

Extended the from_backend method of InstructionDurations to support both BackendV1 and BackendV2 (backport #12941) #13009

Merged
merged 1 commit into from
Aug 21, 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
4 changes: 4 additions & 0 deletions qiskit/transpiler/instruction_durations.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from qiskit.circuit import Barrier, Delay, Instruction, ParameterExpression
from qiskit.circuit.duration import duration_in_dt
from qiskit.providers import Backend
from qiskit.providers.backend import BackendV2
from qiskit.transpiler.exceptions import TranspilerError
from qiskit.utils.units import apply_prefix

Expand Down Expand Up @@ -75,6 +76,9 @@ def from_backend(cls, backend: Backend):
TranspilerError: If dt and dtm is different in the backend.
"""
# All durations in seconds in gate_length
if isinstance(backend, BackendV2):
return backend.target.durations()

instruction_durations = []
backend_properties = backend.properties()
if hasattr(backend_properties, "_gates"):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
Fixed a bug where :meth:`.InstructionDurations.from_backend` did not work for :class:`.BackendV2` backends.
Fixed `#12760 <https://github.com/Qiskit/qiskit/issues/12760>`.
8 changes: 8 additions & 0 deletions test/python/transpiler/test_instruction_durations.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from qiskit.circuit import Delay, Parameter
from qiskit.providers.fake_provider import Fake27QPulseV1
from qiskit.providers.fake_provider import GenericBackendV2
from qiskit.transpiler.exceptions import TranspilerError
from qiskit.transpiler.instruction_durations import InstructionDurations
from test import QiskitTestCase # pylint: disable=wrong-import-order
Expand Down Expand Up @@ -92,3 +93,10 @@ def test_fail_if_get_unbounded_duration_with_unit_conversion_when_dt_is_not_prov
parameterized_delay = Delay(param, "s")
with self.assertRaises(TranspilerError):
InstructionDurations().get(parameterized_delay, 0)

def test_from_backend_with_backendv2(self):
"""Test if `from_backend()` method allows using BackendV2"""
backend = GenericBackendV2(num_qubits=4, calibrate_instructions=True, seed=42)
inst_durations = InstructionDurations.from_backend(backend)
self.assertEqual(inst_durations, backend.target.durations())
self.assertIsInstance(inst_durations, InstructionDurations)
Loading