Repository: Qiskit/qiskit

Issue:
Add BackendV2 support for `transpiler._parse_inst_map()`
### What should we add?

BackendV2 has removed `defaults` attribute from backend and `transpiler._parse_inst_map()` needs to be updated to support BackendV2.

https://github.com/Qiskit/qiskit-terra/blob/main/qiskit/compiler/transpiler.py#L666-L676
```python
def _parse_inst_map(inst_map, backend, num_circuits):
    # try getting inst_map from user, else backend
    if inst_map is None:
        if hasattr(backend, "defaults"):
            inst_map = getattr(backend.defaults(), "instruction_schedule_map", None)

    # inst_maps could be None, or single entry
    if inst_map is None or isinstance(inst_map, InstructionScheduleMap):
        inst_map = [inst_map] * num_circuits

    return inst_map
```

This was discovered in #7643. Created as a issue with separate PR to follow by @mtreinish https://github.com/Qiskit/qiskit-terra/pull/7643/files/474e529c55e8198493512a02c4df52966ad83063#r823983767


Discussion:

Resolving patch:
diff --git a/qiskit/compiler/transpiler.py b/qiskit/compiler/transpiler.py
--- a/qiskit/compiler/transpiler.py
+++ b/qiskit/compiler/transpiler.py
@@ -666,9 +666,14 @@ def _parse_basis_gates(basis_gates, backend, circuits):
 def _parse_inst_map(inst_map, backend, num_circuits):
     # try getting inst_map from user, else backend
     if inst_map is None:
-        if hasattr(backend, "defaults"):
-            inst_map = getattr(backend.defaults(), "instruction_schedule_map", None)
-
+        backend_version = getattr(backend, "version", 0)
+        if not isinstance(backend_version, int):
+            backend_version = 0
+        if backend_version <= 1:
+            if hasattr(backend, "defaults"):
+                inst_map = getattr(backend.defaults(), "instruction_schedule_map", None)
+        else:
+            inst_map = backend.target.instruction_schedule_map()
     # inst_maps could be None, or single entry
     if inst_map is None or isinstance(inst_map, InstructionScheduleMap):
         inst_map = [inst_map] * num_circuits


Test patch:
diff --git a/test/python/providers/test_backend_v2.py b/test/python/providers/test_backend_v2.py
--- a/test/python/providers/test_backend_v2.py
+++ b/test/python/providers/test_backend_v2.py
@@ -21,6 +21,8 @@
 
 from qiskit.circuit import QuantumCircuit, ClassicalRegister, QuantumRegister
 from qiskit.compiler import transpile
+from qiskit.compiler.transpiler import _parse_inst_map
+from qiskit.pulse.instruction_schedule_map import InstructionScheduleMap
 from qiskit.test.base import QiskitTestCase
 from qiskit.test.mock.fake_backend_v2 import FakeBackendV2, FakeBackend5QV2
 from qiskit.test.mock.fake_mumbai_v2 import FakeMumbaiV2
@@ -165,3 +167,8 @@ def test_transpile_mumbai_target(self):
         expected.measure(qr[0], cr[0])
         expected.measure(qr[1], cr[1])
         self.assertEqual(expected, tqc)
+
+    def test_transpile_parse_inst_map(self):
+        """Test that transpiler._parse_inst_map() supports BackendV2."""
+        inst_map = _parse_inst_map(inst_map=None, backend=self.backend, num_circuits=1)[0]
+        self.assertIsInstance(inst_map, InstructionScheduleMap)