Repository: Qiskit/qiskit

Issue:
text drawer cregbundle bug when instructions with classical wires
```python
qr2 = QuantumRegister(2, 'qr')
cr2 = ClassicalRegister(2, 'cr')
inst = QuantumCircuit(qr2, cr2, name='name').to_instruction()
circuit = QuantumCircuit(qr2, cr2)
circuit.append(inst, qr2[:], cr2[:])
circuit.draw(cregbundle=False)
```
```
      ┌───────┐
qr_0: ┤0      ├
      │       │
qr_1: ┤1      ├
      │  name │
cr_0: ╡0      ╞
      │       │
cr_1: ╡1      ╞
      └───────┘
```
but
```python
circuit.draw(cregbundle=True)
```
```
ValueError: max() arg is an empty sequence
```


Discussion:

Resolving patch:
diff --git a/qiskit/visualization/circuit_visualization.py b/qiskit/visualization/circuit_visualization.py
--- a/qiskit/visualization/circuit_visualization.py
+++ b/qiskit/visualization/circuit_visualization.py
@@ -133,6 +133,7 @@ def circuit_drawer(circuit,
             Default: ``False``.
         cregbundle (bool): Optional. If set True bundle classical registers. Not used by
             the ``matplotlib`` output. Default: ``False``.
+
     Returns:
         :class:`PIL.Image` or :class:`matplotlib.figure` or :class:`str` or
         :class:`TextDrawing`:
diff --git a/qiskit/visualization/text.py b/qiskit/visualization/text.py
--- a/qiskit/visualization/text.py
+++ b/qiskit/visualization/text.py
@@ -16,6 +16,7 @@
 A module for drawing circuits in ascii art or some other text representation
 """
 
+from warnings import warn
 from shutil import get_terminal_size
 import sys
 from numpy import ndarray
@@ -31,6 +32,12 @@
 from .exceptions import VisualizationError
 
 
+class TextDrawerCregBundle(VisualizationError):
+    """The parameter "cregbundle" was set to True in an imposible situation. For example, an
+    instruction needs to refer to individual classical wires'"""
+    pass
+
+
 class DrawElement():
     """ An element is an instruction or an operation that need to be drawn."""
 
@@ -581,7 +588,13 @@ def lines(self, line_length=None):
 
         noqubits = len(self.qregs)
 
-        layers = self.build_layers()
+        try:
+            layers = self.build_layers()
+        except TextDrawerCregBundle:
+            self.cregbundle = False
+            warn('The parameter "cregbundle" was disable, since an instruction needs to refer to '
+                 'individual classical wires', RuntimeWarning, 2)
+            layers = self.build_layers()
 
         layer_groups = [[]]
         rest_of_the_line = line_length
@@ -1024,6 +1037,8 @@ def add_connected_gate(instruction, gates, layer, current_cons):
         elif instruction.qargs and instruction.cargs:
             # multiple gate, involving both qargs AND cargs
             label = TextDrawing.label_for_box(instruction)
+            if self.cregbundle and instruction.cargs:
+                raise TextDrawerCregBundle('TODO')
             layer._set_multibox(label, qubits=instruction.qargs, clbits=instruction.cargs,
                                 conditional=conditional)
         else:


Test patch:
diff --git a/test/python/visualization/test_circuit_text_drawer.py b/test/python/visualization/test_circuit_text_drawer.py
--- a/test/python/visualization/test_circuit_text_drawer.py
+++ b/test/python/visualization/test_circuit_text_drawer.py
@@ -1934,6 +1934,26 @@ def test_text_all_2q_2c(self):
 
         self.assertEqual(str(_text_circuit_drawer(circuit)), expected)
 
+    def test_text_all_2q_2c_cregbundle(self):
+        """ Test q0-q1-c0-c1 in q0-q1-c0-c1. Ignore cregbundle=True"""
+        expected = '\n'.join(["         ┌───────┐",
+                              "qr_0: |0>┤0      ├",
+                              "         │       │",
+                              "qr_1: |0>┤1      ├",
+                              "         │  name │",
+                              " cr_0: 0 ╡0      ╞",
+                              "         │       │",
+                              " cr_1: 0 ╡1      ╞",
+                              "         └───────┘"])
+
+        qr2 = QuantumRegister(2, 'qr')
+        cr2 = ClassicalRegister(2, 'cr')
+        inst = QuantumCircuit(qr2, cr2, name='name').to_instruction()
+        circuit = QuantumCircuit(qr2, cr2)
+        circuit.append(inst, qr2[:], cr2[:])
+        with self.assertWarns(RuntimeWarning):
+            self.assertEqual(str(_text_circuit_drawer(circuit, cregbundle=True)), expected)
+
     def test_text_4q_2c(self):
         """ Test q1-q2-q3-q4-c1-c2 in q0-q1-q2-q3-q4-q5-c0-c1-c2-c3-c4-c5"""
         expected = '\n'.join(["                 ",