Repository: Qiskit/qiskit

Issue:
Circuit drawers misorders subcircuits which depend only on a classical wire
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues -->

### Information

- **Qiskit Terra version**: master @ 296a745
- **Python version**:  3.6
- **Operating system**: osx 10.15

### What is the current behavior?

Appending a circuit which uses both qubits and clbits can be drawn out of order, if it introduces a dependency which can only be followed along classical wires.

### Steps to reproduce the problem

Base circuit:
```
qc = qk.QuantumCircuit(2,1)
qc.h(0)
qc.measure(0,0)
qc.draw()
```

```
     ┌───┐┌─┐
q_0: ┤ H ├┤M├
     └───┘└╥┘
q_1: ──────╫─
           ║ 
c: 1/══════╩═
           0 
```

Circuit to be appended:

```
qc_2 = qk.QuantumCircuit(1,1)
qc_2.h(0).c_if(qc_2.cregs[0], 1)
qc_2.draw()
```

```
      ┌───┐ 
q_0: ─┤ H ├─
      └─╥─┘ 
     ┌──╨──┐
c: 1/╡ = 1 ╞
     └─────┘
```

Combined circuit:

```
qc.append(qc_2, [1], [0])
qc.draw()
```

```
          ┌───┐     ┌─┐
q_0: ─────┤ H ├─────┤M├
     ┌────┴───┴────┐└╥┘
q_1: ┤0            ├─╫─
     │  circuit111 │ ║ 
c_0: ╡0            ╞═╩═
     └─────────────┘   
```

Matplotlib:

![image](https://user-images.githubusercontent.com/2241698/109668627-58ecc480-7b3f-11eb-99ae-44f686497d36.png)

Latex:

![image](https://user-images.githubusercontent.com/2241698/109668651-5f7b3c00-7b3f-11eb-9f7b-9ab4ba9f1d5e.png)

DAG drawer has the right dependency on `c[0]`:

![image](https://user-images.githubusercontent.com/2241698/109668724-6e61ee80-7b3f-11eb-9650-53ad8bbe5b03.png)

### What is the expected behavior?

The appended circuit should be drawn after the measure.

### Suggested solutions





Discussion:
@kdk I think this is a simple follow-up to #5397. That was looking only at conditional gates in the final circuit. It should be easy to extend that to look for node.cargs that match the measure. I can take a look at it.

Resolving patch:
diff --git a/qiskit/visualization/utils.py b/qiskit/visualization/utils.py
--- a/qiskit/visualization/utils.py
+++ b/qiskit/visualization/utils.py
@@ -232,8 +232,15 @@ def slide_from_left(self, node, index):
         else:
             inserted = False
             curr_index = index
-            index_stop = -1 if not node.condition else self.measure_map[node.condition[0]]
             last_insertable_index = -1
+            index_stop = -1
+            if node.condition:
+                index_stop = self.measure_map[node.condition[0]]
+            elif node.cargs:
+                for carg in node.cargs:
+                    if self.measure_map[carg.register] > index_stop:
+                        index_stop = self.measure_map[carg.register]
+
             while curr_index > index_stop:
                 if self.is_found_in(node, self[curr_index]):
                     break


Test patch:
diff --git a/test/python/visualization/test_utils.py b/test/python/visualization/test_utils.py
--- a/test/python/visualization/test_utils.py
+++ b/test/python/visualization/test_utils.py
@@ -288,6 +288,36 @@ def test_get_layered_instructions_right_justification_less_simple(self):
         self.assertEqual(r_exp,
                          [[(op.name, op.qargs, op.cargs) for op in ops] for ops in layered_ops])
 
+    def test_get_layered_instructions_op_with_cargs(self):
+        """ Test _get_layered_instructions op with cargs right of measure
+                ┌───┐┌─┐
+        q_0: |0>┤ H ├┤M├─────────────
+                └───┘└╥┘┌───────────┐
+        q_1: |0>──────╫─┤0          ├
+                      ║ │  add_circ │
+         c_0: 0 ══════╩═╡0          ╞
+                        └───────────┘
+         c_1: 0 ═════════════════════
+        """
+        qc = QuantumCircuit(2, 2)
+        qc.h(0)
+        qc.measure(0, 0)
+        qc_2 = QuantumCircuit(1, 1, name='add_circ')
+        qc_2.h(0).c_if(qc_2.cregs[0], 1)
+        qc_2.measure(0, 0)
+        qc.append(qc_2, [1], [0])
+
+        (_, _, layered_ops) = utils._get_layered_instructions(qc)
+
+        expected = [[('h', [Qubit(QuantumRegister(2, 'q'), 0)], [])],
+                    [('measure', [Qubit(QuantumRegister(2, 'q'), 0)],
+                     [Clbit(ClassicalRegister(2, 'c'), 0)])],
+                    [('add_circ', [Qubit(QuantumRegister(2, 'q'), 1)],
+                     [Clbit(ClassicalRegister(2, 'c'), 0)])]]
+
+        self.assertEqual(expected,
+                         [[(op.name, op.qargs, op.cargs) for op in ops] for ops in layered_ops])
+
     def test_generate_latex_label_nomathmode(self):
         """Test generate latex label default."""
         self.assertEqual('abc', utils.generate_latex_label('abc'))