Skip to content

Commit

Permalink
Fix BackendV2 support for PassManagerConfig.from_backend() (Qiskit#8109)
Browse files Browse the repository at this point in the history
This commit fixes the PassManagerConfig.from_backend() constructor
method when running with a BackendV2 object. Previously this was not
correctly handled and it always assumed the input backend was a
BackendV1 object. This would cause an error if you ran the constructor
with a BackendV2.

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
mtreinish and mergify[bot] authored Jun 21, 2022
1 parent 992c261 commit 42c72be
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
35 changes: 24 additions & 11 deletions qiskit/transpiler/passmanager_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,38 @@ def from_backend(cls, backend, **pass_manager_options):
AttributeError: If the backend does not support a `configuration()` method.
"""
res = cls(**pass_manager_options)
config = backend.configuration()
backend_version = getattr(backend, "version", 0)
if not isinstance(backend_version, int):
backend_version = 0
if backend_version < 2:
config = backend.configuration()

if res.basis_gates is None:
res.basis_gates = getattr(config, "basis_gates", None)
if res.inst_map is None and hasattr(backend, "defaults"):
res.inst_map = backend.defaults().instruction_schedule_map
if backend_version < 2:
res.basis_gates = getattr(config, "basis_gates", None)
else:
res.basis_gates = backend.operation_names
if res.inst_map is None:
if backend_version < 2:
if hasattr(backend, "defaults"):
res.inst_map = backend.defaults().instruction_schedule_map
else:
res.inst_map = backend.instruction_schedule_map
if res.coupling_map is None:
res.coupling_map = CouplingMap(getattr(config, "coupling_map", None))
if backend_version < 2:
res.coupling_map = CouplingMap(getattr(config, "coupling_map", None))
else:
res.coupling_map = backend.coupling_map
if res.instruction_durations is None:
res.instruction_durations = InstructionDurations.from_backend(backend)
if res.backend_properties is None:
if backend_version < 2:
res.instruction_durations = InstructionDurations.from_backend(backend)
else:
res.instruction_durations = backend.instruction_durations
if res.backend_properties is None and backend_version < 2:
res.backend_properties = backend.properties()
if res.target is None:
backend_version = getattr(backend, "version", 0)
if not isinstance(backend_version, int):
backend_version = 0
if backend_version >= 2:
res.target = backend.target

return res

def __str__(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fixes:
- |
Fixed support in the :meth:`.PassManagerConfig.from_backend` constructor
method for building a :class:`~.PassManagerConfig` object from a
:class:`~.BackendV2` instance. Previously this wasn't handled correctly
and would fail when running with a :class:`~.BackendV2` object.
10 changes: 9 additions & 1 deletion test/python/transpiler/test_passmanager_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from qiskit.providers.backend import Backend
from qiskit.test import QiskitTestCase
from qiskit.test.mock import FakeMelbourne
from qiskit.test.mock.backends.almaden.fake_almaden import FakeAlmaden
from qiskit.test.mock.backends.almaden.fake_almaden import FakeAlmaden, FakeAlmadenV2
from qiskit.test.mock.backends import FakeArmonk
from qiskit.transpiler.coupling import CouplingMap
from qiskit.transpiler.passmanager_config import PassManagerConfig
Expand All @@ -39,6 +39,14 @@ def test_config_from_backend(self):
str(config.coupling_map), str(CouplingMap(backend.configuration().coupling_map))
)

def test_config_from_backend_v2(self):
"""Test from_backend() with a BackendV2 instance."""
backend = FakeAlmadenV2()
config = PassManagerConfig.from_backend(backend)
self.assertEqual(config.basis_gates, backend.operation_names)
self.assertEqual(config.inst_map, backend.instruction_schedule_map)
self.assertEqual(config.coupling_map.get_edges(), backend.coupling_map.get_edges())

def test_invalid_backend(self):
"""Test from_backend() with an invalid backend."""
with self.assertRaises(AttributeError):
Expand Down

0 comments on commit 42c72be

Please sign in to comment.