Skip to content

Commit

Permalink
Fix handling of BackendV1 simulators in PassManagerConfig.from_backend (
Browse files Browse the repository at this point in the history
Qiskit#9719)

This commit fixes two small oversights that would appear when calling
PassManagerConfig.from_backend() with a simulator BackendV1 backend.
The handling of optional fields in the BackendProperties and
PulseDefaults objects for BackendV1 backends was missing that a
BackendProperties object's gates field could be None and that the
defaults() method could return None in the absense of any pulse
calibrations (both of which typically only are True for simulators). In
these cases this would cause an error constructing the
InstructionDurations object and the InstructionScheduleMap object
respectively. This fixes the handling of these edge cases so that
PassManagerConfig.from_backend() will work with any BackendV1 based
backend.

Fixes Qiskit#8546
Fixes Qiskit#9265

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and king-p3nguin committed May 22, 2023
1 parent d8cbe10 commit f2682df
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
20 changes: 11 additions & 9 deletions qiskit/transpiler/instruction_durations.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,17 @@ def from_backend(cls, backend: Backend):
"""
# All durations in seconds in gate_length
instruction_durations = []
for gate, insts in backend.properties()._gates.items():
for qubits, props in insts.items():
if "gate_length" in props:
gate_length = props["gate_length"][0] # Throw away datetime at index 1
instruction_durations.append((gate, qubits, gate_length, "s"))
for q, props in backend.properties()._qubits.items():
if "readout_length" in props:
readout_length = props["readout_length"][0] # Throw away datetime at index 1
instruction_durations.append(("measure", [q], readout_length, "s"))
backend_properties = backend.properties()
if hasattr(backend_properties, "_gates"):
for gate, insts in backend_properties._gates.items():
for qubits, props in insts.items():
if "gate_length" in props:
gate_length = props["gate_length"][0] # Throw away datetime at index 1
instruction_durations.append((gate, qubits, gate_length, "s"))
for q, props in backend.properties()._qubits.items():
if "readout_length" in props:
readout_length = props["readout_length"][0] # Throw away datetime at index 1
instruction_durations.append(("measure", [q], readout_length, "s"))

try:
dt = backend.configuration().dt
Expand Down
4 changes: 3 additions & 1 deletion qiskit/transpiler/passmanager_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ def from_backend(cls, backend, **pass_manager_options):
if res.inst_map is None:
if backend_version < 2:
if hasattr(backend, "defaults"):
res.inst_map = backend.defaults().instruction_schedule_map
defaults = backend.defaults()
if defaults is not None:
res.inst_map = defaults.instruction_schedule_map
else:
res.inst_map = backend.instruction_schedule_map
if res.coupling_map is None:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
fixes:
- |
Fixed an issue with the :meth:`.PassManagerConfig.from_backend`
constructor method when it was used with a :class:`~.BackendV1` based
simulator backend. For some simulator backends which did not populate
some optional fields the constructor would error.
Fixed `#9265 <https://github.com/Qiskit/qiskit-terra/issues/9265>`__ and
`#8546 <https://github.com/Qiskit/qiskit-terra/issues/8546>`__
16 changes: 16 additions & 0 deletions test/python/transpiler/test_passmanager_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from qiskit.providers.backend import Backend
from qiskit.test import QiskitTestCase
from qiskit.providers.fake_provider import FakeMelbourne, FakeArmonk, FakeHanoi, FakeHanoiV2
from qiskit.providers.basicaer import QasmSimulatorPy
from qiskit.transpiler.coupling import CouplingMap
from qiskit.transpiler.passmanager_config import PassManagerConfig

Expand Down Expand Up @@ -71,6 +72,21 @@ def test_from_backend_and_user(self):
)
self.assertEqual(config.initial_layout, initial_layout)

def test_from_backendv1_inst_map_is_none(self):
"""Test that from_backend() works with backend that has defaults defined as None."""
backend = FakeHanoi()
backend.defaults = lambda: None
config = PassManagerConfig.from_backend(backend)
self.assertIsInstance(config, PassManagerConfig)
self.assertIsNone(config.inst_map)

def test_simulator_backend_v1(self):
"""Test that from_backend() works with backendv1 simulator."""
backend = QasmSimulatorPy()
config = PassManagerConfig.from_backend(backend)
self.assertIsInstance(config, PassManagerConfig)
self.assertIsNone(config.inst_map)

def test_invalid_user_option(self):
"""Test from_backend() with an invalid user option."""
with self.assertRaises(TypeError):
Expand Down

0 comments on commit f2682df

Please sign in to comment.