Repository: Qiskit/qiskit

Issue:
Logical error in transpiler.py
### Environment

- **Qiskit Terra version**: 7ae670a8e74d5a98dd516f696530c74e95f73885
- **Python version**: n/a
- **Operating system**: n/a


### What is happening?

While debugging other issues with the coupling_map, I noticed a puzzling section in the code:

https://github.com/Qiskit/qiskit-terra/blob/1282a04d528dd56d8e401dccc388a32178e9c6cc/qiskit/compiler/transpiler.py#L358-L361

At this point, `coupling_map` is either a `CouplingMap`, a list of lists of integers, or `None` and `unique_transpile_args` is a list of dicts with these keys:

https://github.com/Qiskit/qiskit-terra/blob/1282a04d528dd56d8e401dccc388a32178e9c6cc/qiskit/compiler/transpiler.py#L757-L763

So I don't see how this condition could ever be true.

### How can we reproduce the issue?

Here is some code that can raise an exception because of the problem with the code:

```
from qiskit import QuantumCircuit, transpile

qc = QuantumCircuit(1)

transpile([qc] * 2, coupling_map=[None, None])
```

### What should happen?

No error for the above code.

### Any suggestions?

I think code needs to check if `"coupling_map"` is in the shared dict, pull the coupling maps out of the unique dicts if not.


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
@@ -355,8 +355,8 @@ def callback_func(**kwargs):
         ignore_backend_supplied_default_methods,
     )
     # Get transpile_args to configure the circuit transpilation job(s)
-    if coupling_map in unique_transpile_args:
-        cmap_conf = unique_transpile_args["coupling_map"]
+    if "coupling_map" not in shared_args:
+        cmap_conf = [a["pass_manager_config"]["coupling_map"] for a in unique_transpile_args]
     else:
         cmap_conf = [shared_args["coupling_map"]] * len(circuits)
     _check_circuits_coupling_map(circuits, cmap_conf, backend)


Test patch:
diff --git a/test/python/compiler/test_transpiler.py b/test/python/compiler/test_transpiler.py
--- a/test/python/compiler/test_transpiler.py
+++ b/test/python/compiler/test_transpiler.py
@@ -1955,6 +1955,36 @@ def test_backendv2_and_coupling_map(self, opt_level):
                 qubit_1 = tqc.find_bit(inst.qubits[1]).index
                 self.assertEqual(qubit_1, qubit_0 + 1)
 
+    def test_transpile_with_multiple_coupling_maps(self):
+        """Test passing a different coupling map for every circuit"""
+        backend = FakeNairobiV2()
+
+        qc = QuantumCircuit(3)
+        qc.cx(0, 2)
+
+        # Add a connection between 0 and 2 so that transpile does not change
+        # the gates
+        cmap = CouplingMap.from_line(7)
+        cmap.add_edge(0, 2)
+
+        with self.assertWarnsRegex(
+            DeprecationWarning, "Passing in a list of arguments for coupling_map is deprecated"
+        ):
+            # Initial layout needed to prevent transpiler from relabeling
+            # qubits to avoid doing the swap
+            tqc = transpile(
+                [qc] * 2,
+                backend,
+                coupling_map=[backend.coupling_map, cmap],
+                initial_layout=(0, 1, 2),
+            )
+
+        # Check that the two coupling maps were used. The default should
+        # require swapping (extra cx's) and the second one should not (just the
+        # original cx).
+        self.assertEqual(tqc[0].count_ops()["cx"], 4)
+        self.assertEqual(tqc[1].count_ops()["cx"], 1)
+
     @data(0, 1, 2, 3)
     def test_backend_and_custom_gate(self, opt_level):
         """Test transpile() with BackendV2, custom basis pulse gate."""