Repository: Qiskit/qiskit

Issue:
Latex drawer fails with conditional and cregbundle=True
<!-- ⚠️ 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**: Current master
- **Python version**: 3.8
- **Operating system**: Ubuntu 18.04
### What is the current behavior?
The latex drawer fails on an index out of range when there is a conditional on the last creg and cregbundle=True. This was discovered in test_teleport in test_visualization.py when circuit_drawer sets the default cregbundle to True.
### Steps to reproduce the problem
The following code works with cregbundle=False,
```
from qiskit import QuantumRegister, QuantumCircuit, ClassicalRegister
qr = QuantumRegister(3, 'q')
cr = ClassicalRegister(3, 'c')
qc = QuantumCircuit(qr, cr)
qc.x(qr[2]).c_if(cr, 2)
c = qc.draw(output='latex_source', cregbundle=True)
```
and fails with this if cregbundle is True.
```
Traceback (most recent call last):
  File "test_latex_creg.py", line 6, in <module>
    c = qc.draw(output='latex_source', cregbundle=True)
  File "/home/ed/qiskit/qiskit-terra/qiskit/circuit/quantumcircuit.py", line 994, in draw
    return circuit_drawer(self, scale=scale,
  File "/home/ed/qiskit/qiskit-terra/qiskit/visualization/circuit_visualization.py", line 306, in circuit_drawer
    return _generate_latex_source(circuit,
  File "/home/ed/qiskit/qiskit-terra/qiskit/visualization/circuit_visualization.py", line 605, in _generate_latex_source
    latex = qcimg.latex()
  File "/home/ed/qiskit/qiskit-terra/qiskit/visualization/latex.py", line 149, in latex
    self._build_latex_array(aliases)
  File "/home/ed/qiskit/qiskit-terra/qiskit/visualization/latex.py", line 561, in _build_latex_array
    self._latex[pos_2 + i][column] = \
IndexError: list index out of range
```
### What is the expected behavior?

### Suggested solutions





Discussion:

Resolving patch:
diff --git a/qiskit/visualization/latex.py b/qiskit/visualization/latex.py
--- a/qiskit/visualization/latex.py
+++ b/qiskit/visualization/latex.py
@@ -292,7 +292,7 @@ def _get_image_depth(self):
         columns = 2
 
         # add extra column if needed
-        if self.cregbundle and self.ops[0][0].name == "measure":
+        if self.cregbundle and (self.ops[0][0].name == "measure" or self.ops[0][0].condition):
             columns += 1
 
         # all gates take up 1 column except from those with labels (ie cu1)
@@ -387,7 +387,7 @@ def _build_latex_array(self, aliases=None):
 
         column = 1
         # Leave a column to display number of classical registers if needed
-        if self.cregbundle and self.ops[0][0].name == "measure":
+        if self.cregbundle and (self.ops[0][0].name == "measure" or self.ops[0][0].condition):
             column += 1
         for layer in self.ops:
             num_cols_used = 1
@@ -423,8 +423,9 @@ def _build_latex_array(self, aliases=None):
                         temp.sort(key=int)
                         bottom = temp[len(pos_array) - 1]
                         gap = pos_cond - bottom
-                        for i in range(self.cregs[if_reg]):
-                            if if_value[i] == '1':
+                        creg_rng = 1 if self.cregbundle else self.cregs[if_reg]
+                        for i in range(creg_rng):
+                            if (if_value[i] == '1' or (self.cregbundle and int(if_value) > 0)):
                                 self._latex[pos_cond + i][column] = \
                                     "\\control \\cw \\cwx[-" + str(gap) + "]"
                                 gap = 1
@@ -551,8 +552,9 @@ def _build_latex_array(self, aliases=None):
                                 self._latex[pos_1][column] = ("\\gate{%s}" % nm)
 
                             gap = pos_2 - pos_1
-                            for i in range(self.cregs[if_reg]):
-                                if if_value[i] == '1':
+                            creg_rng = 1 if self.cregbundle else self.cregs[if_reg]
+                            for i in range(creg_rng):
+                                if (if_value[i] == '1' or (self.cregbundle and int(if_value) > 0)):
                                     self._latex[pos_2 + i][column] = \
                                         "\\control \\cw \\cwx[-" + str(gap) + "]"
                                     gap = 1
@@ -623,8 +625,9 @@ def _build_latex_array(self, aliases=None):
                             bottom = temp[1]
 
                             gap = pos_3 - bottom
-                            for i in range(self.cregs[if_reg]):
-                                if if_value[i] == '1':
+                            creg_rng = 1 if self.cregbundle else self.cregs[if_reg]
+                            for i in range(creg_rng):
+                                if (if_value[i] == '1' or (self.cregbundle and int(if_value) > 0)):
                                     self._latex[pos_3 + i][column] = \
                                         "\\control \\cw \\cwx[-" + str(gap) + "]"
                                     gap = 1
@@ -831,8 +834,9 @@ def _build_latex_array(self, aliases=None):
                             bottom = temp[2]
 
                             gap = pos_4 - bottom
-                            for i in range(self.cregs[if_reg]):
-                                if if_value[i] == '1':
+                            creg_rng = 1 if self.cregbundle else self.cregs[if_reg]
+                            for i in range(creg_rng):
+                                if (if_value[i] == '1' or (self.cregbundle and int(if_value) > 0)):
                                     self._latex[pos_4 + i][column] = \
                                         "\\control \\cw \\cwx[-" + str(gap) + "]"
                                     gap = 1


Test patch:
diff --git a/test/python/visualization/test_visualization.py b/test/python/visualization/test_visualization.py
--- a/test/python/visualization/test_visualization.py
+++ b/test/python/visualization/test_visualization.py
@@ -57,12 +57,16 @@ def test_normal_circuit(self):
             if os.path.exists(filename):
                 os.remove(filename)
 
-    def test_wide_circuit(self):
-        """Test draw wide circuit."""
-        filename = self._get_resource_path('test_wide.tex')
-        qc = QuantumCircuit(100)
-        for gate in range(100):
-            qc.h(gate)
+    def test_4597(self):
+        """Test cregbundle and conditional gates.
+        See: https://github.com/Qiskit/qiskit-terra/pull/4597 """
+        filename = self._get_resource_path('test_4597.tex')
+        qr = QuantumRegister(3, 'q')
+        cr = ClassicalRegister(3, 'c')
+        qc = QuantumCircuit(qr, cr)
+        qc.x(qr[2]).c_if(cr, 2)
+        qc.draw(output='latex_source', cregbundle=True)
+
         try:
             circuit_drawer(qc, filename=filename, output='latex_source')
             self.assertNotEqual(os.path.exists(filename), False)