Repository: Qiskit/qiskit

Issue:
BasicMapper error with measurements
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues -->

### Informations

- **Qiskit Terra version**:
- **Python version**:
- **Operating system**:

### What is the current behavior?
```python
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit import Aer
from qiskit.transpiler import PassManager
from qiskit.transpiler.passes import BasicMapper
from qiskit.transpiler import transpile
from qiskit.mapper import Coupling

coupling = [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6]]
qr = QuantumRegister(7, 'q')
cr = ClassicalRegister(7, 'c')
circuit = QuantumCircuit(qr, cr)
circuit.h(qr[3])
circuit.cx(qr[0], qr[6])
circuit.cx(qr[6], qr[0])
circuit.cx(qr[0], qr[1])
circuit.cx(qr[3], qr[1])
circuit.cx(qr[3], qr[0])
circuit.measure(qr, cr)

simulator = Aer.get_backend('qasm_simulator')
coupling_map = Coupling(couplinglist=coupling)
pass_manager = PassManager()
pass_manager.add_passes([BasicMapper(coupling_map=coupling_map)])
basic_circ = transpile(circuit, simulator, pass_manager=pass_manager)
```


Discussion:

Resolving patch:
diff --git a/qiskit/dagcircuit/_dagcircuit.py b/qiskit/dagcircuit/_dagcircuit.py
--- a/qiskit/dagcircuit/_dagcircuit.py
+++ b/qiskit/dagcircuit/_dagcircuit.py
@@ -564,13 +564,16 @@ def _map_condition(self, wire_map, condition):
     def extend_back(self, dag, edge_map=None):
         """Add `dag` at the end of `self`, using `edge_map`.
         """
+        edge_map = edge_map or {}
         for qreg in dag.qregs.values():
             if qreg.name not in self.qregs:
                 self.add_qreg(QuantumRegister(qreg.size, qreg.name))
+            edge_map.update([(qbit, qbit) for qbit in qreg if qbit not in edge_map])
 
         for creg in dag.cregs.values():
             if creg.name not in self.cregs:
                 self.add_creg(ClassicalRegister(creg.size, creg.name))
+            edge_map.update([(cbit, cbit) for cbit in creg if cbit not in edge_map])
 
         self.compose_back(dag, edge_map)
 
diff --git a/qiskit/transpiler/passes/mapping/direction_mapper.py b/qiskit/transpiler/passes/mapping/direction_mapper.py
--- a/qiskit/transpiler/passes/mapping/direction_mapper.py
+++ b/qiskit/transpiler/passes/mapping/direction_mapper.py
@@ -9,7 +9,6 @@
 A direction mapper rearrenges the direction of the cx nodes to make the circuit
 compatible to the directed coupling map.
 """
-from copy import copy
 
 from qiskit.transpiler._basepasses import TransformationPass
 from qiskit.transpiler import MapperError
@@ -38,7 +37,7 @@ def __init__(self, coupling_map, initial_layout=None):
 
         super().__init__()
         self.coupling_map = coupling_map
-        self.initial_layout = initial_layout
+        self.layout = initial_layout
 
     def run(self, dag):
         """
@@ -54,15 +53,14 @@ def run(self, dag):
         """
         new_dag = DAGCircuit()
 
-        if self.initial_layout is None:
+        if self.layout is None:
             # create a one-to-one layout
-            self.initial_layout = Layout()
+            self.layout = Layout()
             wire_no = 0
             for qreg in dag.qregs.values():
                 for index in range(qreg.size):
-                    self.initial_layout[(qreg, index)] = wire_no
+                    self.layout[(qreg, index)] = wire_no
                     wire_no += 1
-        current_layout = copy(self.initial_layout)
 
         for layer in dag.serial_layers():
             subdag = layer['graph']
@@ -72,8 +70,8 @@ def run(self, dag):
                 control = cnot['op'].qargs[0]
                 target = cnot['op'].qargs[1]
 
-                physical_q0 = current_layout[control]
-                physical_q1 = current_layout[target]
+                physical_q0 = self.layout[control]
+                physical_q1 = self.layout[target]
                 if self.coupling_map.distance(physical_q0, physical_q1) != 1:
                     raise MapperError('The circuit requires a connectiontion between the phsycial '
                                       'qubits %s and %s' % (physical_q0, physical_q1))
@@ -97,7 +95,6 @@ def run(self, dag):
                     # Flips the CX
                     cnot['op'].qargs[0], cnot['op'].qargs[1] = target, control
 
-            edge_map = current_layout.combine_into_edge_map(self.initial_layout)
-            new_dag.compose_back(subdag, edge_map)
+            new_dag.extend_back(subdag)
 
         return new_dag


Test patch:
diff --git a/test/python/transpiler/test_direction_mapper.py b/test/python/transpiler/test_direction_mapper.py
--- a/test/python/transpiler/test_direction_mapper.py
+++ b/test/python/transpiler/test_direction_mapper.py
@@ -9,7 +9,7 @@
 
 import unittest
 
-from qiskit import QuantumRegister, QuantumCircuit
+from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit
 from qiskit.transpiler import MapperError
 from qiskit.transpiler.passes import DirectionMapper
 from qiskit.mapper import Coupling
@@ -42,7 +42,7 @@ def test_no_cnots(self):
         self.assertEqual(dag, after)
 
     def test_direction_error(self):
-        """
+        """ The mapping cannot be fixed by direction mapper
          qr0:---------
 
          qr1:---(+)---
@@ -63,7 +63,7 @@ def test_direction_error(self):
             pass_.run(dag)
 
     def test_direction_correct(self):
-        """
+        """ The CX is in the right direction
          qr0:---(+)---
                  |
          qr1:----.----
@@ -82,16 +82,16 @@ def test_direction_correct(self):
         self.assertEqual(dag, after)
 
     def test_direction_flip(self):
-        """
+        """ Flip a CX
          qr0:----.----
                  |
          qr1:---(+)---
 
          Coupling map: [0] -> [1]
 
-         qr0:---(+)---
-                 |
-         qr1:----.----
+         qr0:-[H]-(+)-[H]--
+                   |
+         qr1:-[H]--.--[H]--
         """
         qr = QuantumRegister(2, 'qr')
         circuit = QuantumCircuit(qr)
@@ -111,6 +111,44 @@ def test_direction_flip(self):
 
         self.assertEqual(circuit_to_dag(expected), after)
 
+    def test_flip_with_measure(self):
+        """
+         qr0: -(+)-[m]-
+                |   |
+         qr1: --.---|--
+                    |
+         cr0: ------.--
+
+         Coupling map: [0] -> [1]
+
+         qr0: -[H]--.--[H]-[m]-
+                    |       |
+         qr1: -[H]-(+)-[H]--|--
+                            |
+         cr0: --------------.--
+        """
+        qr = QuantumRegister(2, 'qr')
+        cr = ClassicalRegister(1, 'cr')
+
+        circuit = QuantumCircuit(qr, cr)
+        circuit.cx(qr[1], qr[0])
+        circuit.measure(qr[0], cr[0])
+        coupling = Coupling({0: [1]})
+        dag = circuit_to_dag(circuit)
+
+        expected = QuantumCircuit(qr, cr)
+        expected.h(qr[0])
+        expected.h(qr[1])
+        expected.cx(qr[0], qr[1])
+        expected.h(qr[0])
+        expected.h(qr[1])
+        expected.measure(qr[0], cr[0])
+
+        pass_ = DirectionMapper(coupling)
+        after = pass_.run(dag)
+
+        self.assertEqual(circuit_to_dag(expected), after)
+
 
 if __name__ == '__main__':
     unittest.main()