Repository: Qiskit/qiskit

Issue:
conditional bug
<!-- ⚠️ 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**: 0.7
- **Python version**: 3.6
- **Operating system**: Mac

### What is the current behavior?

qobj without mapping

```
QobjItem(name='h', params=[], qubits=[1], texparams=[])
QobjItem(name='cx', params=[], qubits=[1, 2], texparams=[])
QobjItem(name='u3', params=[0.3, 0.2, 0.1], qubits=[0], texparams=['0.3', '0.2', '0.1'])
QobjItem(name='barrier', qubits=[0, 1, 2])
QobjItem(name='cx', params=[], qubits=[0, 1], texparams=[])
QobjItem(clbits=[1], memory=[1], name='measure', qubits=[1])
QobjItem(name='h', params=[], qubits=[0], texparams=[])
QobjItem(clbits=[0], memory=[0], name='measure', qubits=[0])
QobjItem(name='barrier', qubits=[0, 1, 2])
QobjItem(conditional=QobjItem(mask='0x1', type='equals', val='0x1'), name='z', params=[], qubits=[2], texparams=[])
QobjItem(conditional=QobjItem(mask='0x2', type='equals', val='0x1'), name='x', params=[], qubits=[2], texparams=[])
QobjItem(clbits=[2], memory=[2], name='measure', qubits=[2])
```

Output

```
{'0 0 0': 269, '0 0 1': 251, '0 1 0': 254, '0 1 1': 229, '1 0 0': 5, '1 0 1': 5, '1 1 0': 6, '1 1 1': 5}
```

Qobj after mapping to a coupling map

```
QobjItem(name='u2', params=[0.0, 3.141592653589793], qubits=[1], texparams=['0', '\\pi'])
QobjItem(name='cx', params=[], qubits=[1, 2], texparams=[])
QobjItem(name='u3', params=[0.3, 0.2, 0.1], qubits=[0], texparams=['0.3', '0.2', '0.1'])
QobjItem(name='barrier', qubits=[0, 1, 2])
QobjItem(name='cx', params=[], qubits=[0, 1], texparams=[])
QobjItem(name='u2', params=[0.0, 3.141592653589793], qubits=[0], texparams=['0', '\\pi'])
QobjItem(name='barrier', qubits=[0, 1, 2])
QobjItem(conditional=QobjItem(mask='0x1', type='equals', val='0x1'), name='u1', params=[3.141592653589793], qubits=[2], texparams=['\\pi'])
QobjItem(clbits=[0], memory=[0], name='measure', qubits=[0])
QobjItem(conditional=QobjItem(mask='0x2', type='equals', val='0x1'), name='u3', params=[3.141592653589793, 0.0, 3.141592653589793], qubits=[2], texparams=['\\pi', '0', '\\pi'])
QobjItem(clbits=[1], memory=[1], name='measure', qubits=[1])
QobjItem(clbits=[2], memory=[2], name='measure', qubits=[2])
```
output

```
{'0 0 0': 233, '0 0 1': 253, '0 1 0': 3, '0 1 1': 8, '1 0 0': 2, '1 0 1': 7, '1 1 0': 252, '1 1 1': 266}
```

The error is the measurements are passing through the conditional. 

### Steps to reproduce the problem

Run the example teleport

### What is the expected behavior?

outcoumes should be the same

### Suggested solutions

Debug the mapper pass 

NOTE you may need to look after #1284 is merged as there was another error with the qubits randomly changing position in the circuit.  



Discussion:

Resolving patch:
diff --git a/examples/python/teleport.py b/examples/python/teleport.py
--- a/examples/python/teleport.py
+++ b/examples/python/teleport.py
@@ -63,11 +63,6 @@
 qobj = compile(qc, backend=backend, coupling_map=None, shots=1024, initial_layout=initial_layout)
 job = backend.run(qobj)
 qobj_exp = qobj.experiments[0]
-print(qobj_exp.header.qubit_labels)
-print(qobj_exp.header.compiled_circuit_qasm)
-print(qobj_exp.header.clbit_labels)
-for i in qobj_exp.instructions:
-            print(i)
 
 result = job.result()
 print(result)
@@ -76,15 +71,10 @@
 # Second version: mapped to 2x8 array coupling graph
 qobj = compile(qc, backend=backend, coupling_map=coupling_map, shots=1024,initial_layout=initial_layout)
 qobj_exp = qobj.experiments[0]
-print(qobj_exp.header.qubit_labels)
 qobj_exp.header.compiled_circuit_qasm = ""
-print(qobj_exp.header.compiled_circuit_qasm)
-print(qobj_exp.header.clbit_labels)
-for i in qobj_exp.instructions:
-            print(i)
 job = backend.run(qobj)
 result = job.result()
 print(result)
 print(result.get_counts(qc))
 
-# Both versions should give the same distribution
\ No newline at end of file
+# Both versions should give the same distribution
diff --git a/qiskit/mapper/_mapping.py b/qiskit/mapper/_mapping.py
--- a/qiskit/mapper/_mapping.py
+++ b/qiskit/mapper/_mapping.py
@@ -850,7 +850,8 @@ def remove_last_measurements(dag_circuit, perform_remove=True):
 
     for idx in meas_nodes:
         _, succ_map = dag_circuit._make_pred_succ_maps(idx)
-        if len(succ_map) == 2:
+        if len(succ_map) == 2 and all([dag_circuit.multi_graph.node[n]["type"] == "out"
+                                       for n in succ_map.values()]):
             # All succesors of the measurement are outputs, one for qubit and one for cbit
             # (As opposed to more gates being applied), and it is safe to remove the
             # measurement node and add it back after the swap mapper is done.


Test patch: