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 handling of BackendV1 simulators in PassManagerConfig.from_backend (backport #9719) #9725

Merged
merged 2 commits into from
Mar 4, 2023
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
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