Repository: Qiskit/qiskit

Issue:
'_' makes matplotlib to render the following character as subscript
### Environment

- **Qiskit Terra version**:  0.19.1
- **Python version**:  3.9.9
- **Operating system**: Linux(Fedora 34)


### What is happening?

The '_' in the name of QuantumRegister forces the following character to be rendered as subscript
In this example see the 'r' is rendered as a subscript. 


![issue_01012022](https://user-images.githubusercontent.com/54737489/147839273-e1afa3b0-4b36-4cf5-9c77-8256223eb666.png)

The problem vanishes when the number of qubits is increased to 2 or more.

![issue_01012022_4](https://user-images.githubusercontent.com/54737489/147839502-27cd3634-d237-4f65-bb28-81522044d4f8.png)



### How can we reproduce the issue?

from qiskit import QuantumRegister, QuantumCircuit
num_of_qubits=1
q_reg=QuantumRegister(num_of_qubits, 'q_reg')
qc=QuantumCircuit(q_reg)
qc.draw(output='mpl')

### What should happen?

This should be the output:
![issue_01012022_3](https://user-images.githubusercontent.com/54737489/147839433-fab0ef9f-7e72-4d0f-9518-c35f88572162.png)



### Any suggestions?

_No response_


Discussion:
Thanks for spotting this. Actually the second case is incorrect as well. It should be q_reg<sub>0</sub>.
@javabster Can you assign this to me? Thanks.
@enavarro51 , actually I was working on this, now, since you have asked for the permission. You go ahead with this and I tell you my recent progress in this:

Go to qiskit/visualization/matplotlib.py#L456
I changed this: 
 words = [word.replace("_", r"\_") if word.count("_") > 1 else word for word in words]
to this:
 words = [word.replace("_", r"\_") if word.count("_") >= 1 else word for word in words]
and got the image I mentioned in the what should happen section above.

and you put "^" in place of "_" and see new hosts of problems

Now, you take it from here :)
Thanks, @MozammilQ. I believe this is a bit of a deeper problem with register naming. We'll probably need to get some input from the IBM side for this. OpenQASM 3.0 says this about names.

"Names of gates, variables, and constants — In OpenQASM 2, the names of registers
and gates must begin with a lower-case alphabetic ASCII character. This constraint is
relaxed in OpenQASM 3: identifiers may now begin with other characters, such as capital
letters, underscores, and a range of unicode characters. For example, angular values used
as gate arguments may now be represented by greek letters. (The identifier π is reserved in
OpenQASM 3 to represent the same constant as pi.) Note that OpenQASM 3 has a slightly
different set of keywords from OpenQASM 2. This may cause errors in OpenQASM circuits
which happen to use a keyword from version 3 as an identifier name: such identifiers would
have to be renamed in order for the source to be valid OpenQASM 3."

In OpenQASM 2.0, names were restricted to alphanumeric and underscores. This means '^' should not have been allowed in register names. In #3827 an attempt was made to fix more than one underscore in a name displaying with matplotlib. But that solution was not complete and no tests were added, so part of that problem is showing up now. 

The final problem is that the text_width calculation in matplotlib strips out the underscores, which for multiple underscores in a name, results in an incorrect text_width which can lead to letters being cut off to the left.

So a solution could be to put a validity check for what are proper 2.0 and 3.0 names., do the replacement of '\_' with '\\\_', and finally fix the text_width calcs to properly handle '\_'.
Oh Nice! I didn't know that the problem could be this deep, @enavarro51 thanks for sharing the information, you do have a deeper understanding of the problem :)

Resolving patch:
diff --git a/qiskit/visualization/matplotlib.py b/qiskit/visualization/matplotlib.py
--- a/qiskit/visualization/matplotlib.py
+++ b/qiskit/visualization/matplotlib.py
@@ -451,23 +451,18 @@ def _get_bit_labels(self):
         initial_qbit = " |0>" if self._initial_state else ""
         initial_cbit = " 0" if self._initial_state else ""
 
-        def _fix_double_script(bit_label):
-            words = bit_label.split(" ")
-            words = [word.replace("_", r"\_") if word.count("_") > 1 else word for word in words]
-            words = [
-                word.replace("^", r"\^{\ }") if word.count("^") > 1 else word for word in words
-            ]
-            bit_label = " ".join(words).replace(" ", "\\;")
-            return bit_label
-
         # quantum register
         for ii, reg in enumerate(self._qubits):
             register = self._bit_locations[reg]["register"]
             index = self._bit_locations[reg]["index"]
+            reg_size = 0 if register is None else register.size
             qubit_label = get_bit_label("mpl", register, index, qubit=True, layout=self._layout)
-            qubit_label = "$" + _fix_double_script(qubit_label) + "$" + initial_qbit
+            qubit_label = "$" + qubit_label + "$" + initial_qbit
 
-            text_width = self._get_text_width(qubit_label, self._fs) * 1.15
+            reg_single = 0 if reg_size < 2 else 1
+            text_width = (
+                self._get_text_width(qubit_label, self._fs, reg_to_remove=reg_single) * 1.15
+            )
             if text_width > longest_bit_label_width:
                 longest_bit_label_width = text_width
             pos = -ii
@@ -487,6 +482,7 @@ def _fix_double_script(bit_label):
             for ii, reg in enumerate(self._clbits):
                 register = self._bit_locations[reg]["register"]
                 index = self._bit_locations[reg]["index"]
+                reg_size = 0 if register is None else register.size
                 if register is None or not self._cregbundle or prev_creg != register:
                     n_lines += 1
                     idx += 1
@@ -495,12 +491,14 @@ def _fix_double_script(bit_label):
                 clbit_label = get_bit_label(
                     "mpl", register, index, qubit=False, cregbundle=self._cregbundle
                 )
-                clbit_label = _fix_double_script(clbit_label)
                 if register is None or not self._cregbundle:
                     clbit_label = "$" + clbit_label + "$"
                 clbit_label += initial_cbit
 
-                text_width = self._get_text_width(clbit_label, self._fs) * 1.15
+                reg_single = 0 if reg_size < 2 or self._cregbundle else 1
+                text_width = (
+                    self._get_text_width(clbit_label, self._fs, reg_to_remove=reg_single) * 1.15
+                )
                 if text_width > longest_bit_label_width:
                     longest_bit_label_width = text_width
                 pos = y_off - idx
@@ -579,7 +577,7 @@ def _get_coords(self, n_lines):
         anchors = [self._q_anchors[ii].get_index() for ii in self._qubits_dict]
         return max(anchors) if anchors else 0
 
-    def _get_text_width(self, text, fontsize, param=False):
+    def _get_text_width(self, text, fontsize, param=False, reg_to_remove=None):
         """Compute the width of a string in the default font"""
         if not text:
             return 0.0
@@ -596,6 +594,11 @@ def _get_text_width(self, text, fontsize, param=False):
         # If there are subscripts or superscripts in mathtext string
         # we need to account for that spacing by manually removing
         # from text string for text length
+
+        # if it's a register and there's a subscript at the end,
+        # remove 1 underscore, otherwise don't remove any
+        if reg_to_remove is not None:
+            num_underscores = reg_to_remove
         if num_underscores:
             text = text.replace("_", "", num_underscores)
         if num_carets:
diff --git a/qiskit/visualization/utils.py b/qiskit/visualization/utils.py
--- a/qiskit/visualization/utils.py
+++ b/qiskit/visualization/utils.py
@@ -185,14 +185,18 @@ def get_bit_label(drawer, register, index, qubit=True, layout=None, cregbundle=T
         reg_name = f"{register.name}"
         reg_name_index = f"{register.name}_{index}"
     else:
-        reg_name = f"{{{register.name}}}"
-        reg_name_index = f"{{{register.name}}}_{{{index}}}"
+        reg_name = f"{{{fix_special_characters(register.name)}}}"
+        reg_name_index = f"{reg_name}_{{{index}}}"
 
     # Clbits
     if not qubit:
-        if cregbundle:
+        if cregbundle and drawer != "latex":
             bit_label = f"{register.name}"
-        elif register.size == 1:
+            return bit_label
+
+        size = register.size
+        if size == 1 or cregbundle:
+            size = 1
             bit_label = reg_name
         else:
             bit_label = reg_name_index
@@ -218,6 +222,8 @@ def get_bit_label(drawer, register, index, qubit=True, layout=None, cregbundle=T
                 bit_label = f"{virt_bit} -> {index}"
             else:
                 bit_label = f"{{{virt_bit}}} \\mapsto {{{index}}}"
+        if drawer != "text":
+            bit_label = bit_label.replace(" ", "\\;")  # use wider spaces
     else:
         bit_label = index_str
 
@@ -272,6 +278,22 @@ def get_condition_label(condition, clbits, bit_locations, cregbundle):
     return label, clbit_mask, vlist
 
 
+def fix_special_characters(label):
+    """
+    Convert any special characters for mpl and latex drawers.
+    Currently only checks for multiple underscores in register names
+    and uses wider space for mpl and latex drawers.
+
+    Args:
+        label (str): the label to fix
+
+    Returns:
+        str: label to display
+    """
+    label = label.replace("_", r"\_").replace(" ", "\\;")
+    return label
+
+
 def generate_latex_label(label):
     """Convert a label to a valid latex string."""
     if not HAS_PYLATEX:


Test patch:
diff --git a/test/ipynb/mpl/circuit/references/multi_underscore_false.png b/test/ipynb/mpl/circuit/references/multi_underscore_false.png
new file mode 100644
Binary files /dev/null and b/test/ipynb/mpl/circuit/references/multi_underscore_false.png differ
diff --git a/test/ipynb/mpl/circuit/references/multi_underscore_true.png b/test/ipynb/mpl/circuit/references/multi_underscore_true.png
new file mode 100644
Binary files /dev/null and b/test/ipynb/mpl/circuit/references/multi_underscore_true.png differ
diff --git a/test/ipynb/mpl/circuit/test_circuit_matplotlib_drawer.py b/test/ipynb/mpl/circuit/test_circuit_matplotlib_drawer.py
--- a/test/ipynb/mpl/circuit/test_circuit_matplotlib_drawer.py
+++ b/test/ipynb/mpl/circuit/test_circuit_matplotlib_drawer.py
@@ -221,6 +221,17 @@ def test_long_name(self):
 
         self.circuit_drawer(circuit, filename="long_name.png")
 
+    def test_multi_underscore_reg_names(self):
+        """Test that multi-underscores in register names display properly"""
+        q_reg1 = QuantumRegister(1, "q1_re__g__g")
+        q_reg3 = QuantumRegister(3, "q3_re_g__g")
+        c_reg1 = ClassicalRegister(1, "c1_re_g__g")
+        c_reg3 = ClassicalRegister(3, "c3_re_g__g")
+        circuit = QuantumCircuit(q_reg1, q_reg3, c_reg1, c_reg3)
+
+        self.circuit_drawer(circuit, cregbundle=True, filename="multi_underscore_true.png")
+        self.circuit_drawer(circuit, cregbundle=False, filename="multi_underscore_false.png")
+
     def test_conditional(self):
         """Test that circuits with conditionals draw correctly"""
         qr = QuantumRegister(2, "q")
diff --git a/test/python/visualization/references/test_latex_4597.tex b/test/python/visualization/references/test_latex_4597.tex
--- a/test/python/visualization/references/test_latex_4597.tex
+++ b/test/python/visualization/references/test_latex_4597.tex
@@ -9,6 +9,6 @@
 	 	\nghost{{q}_{0} :  } & \lstick{{q}_{0} :  } & \qw & \qw & \qw & \qw\\
 	 	\nghost{{q}_{1} :  } & \lstick{{q}_{1} :  } & \qw & \qw & \qw & \qw\\
 	 	\nghost{{q}_{2} :  } & \lstick{{q}_{2} :  } & \qw & \gate{\mathrm{X}} & \qw & \qw\\
-	 	\nghost{\mathrm{c :  }} & \lstick{\mathrm{c :  }} & \lstick{/_{_{3}}} \cw & \control \cw^(0.0){^{\mathtt{0x2}}} \cwx[-1] & \cw & \cw\\
+	 	\nghost{\mathrm{{c} :  }} & \lstick{\mathrm{{c} :  }} & \lstick{/_{_{3}}} \cw & \control \cw^(0.0){^{\mathtt{0x2}}} \cwx[-1] & \cw & \cw\\
 \\ }}
 \end{document}
\ No newline at end of file
diff --git a/test/python/visualization/references/test_latex_cif_single_bit_bundle.tex b/test/python/visualization/references/test_latex_cif_single_bit_bundle.tex
--- a/test/python/visualization/references/test_latex_cif_single_bit_bundle.tex
+++ b/test/python/visualization/references/test_latex_cif_single_bit_bundle.tex
@@ -8,6 +8,6 @@
 \Qcircuit @C=1.0em @R=0.2em @!R { \\
 	 	\nghost{{qr}_{0} :  } & \lstick{{qr}_{0} :  } & \qw & \gate{\mathrm{H}} & \qw & \qw & \qw\\
 	 	\nghost{{qr}_{1} :  } & \lstick{{qr}_{1} :  } & \qw & \qw & \gate{\mathrm{X}} & \qw & \qw\\
-	 	\nghost{\mathrm{cr :  }} & \lstick{\mathrm{cr :  }} & \lstick{/_{_{2}}} \cw & \controlo \cw^(0.0){^{\mathtt{cr_1=0x0}}} \cwx[-2] & \control \cw^(0.0){^{\mathtt{cr_0=0x1}}} \cwx[-1] & \cw & \cw\\
+	 	\nghost{\mathrm{{cr} :  }} & \lstick{\mathrm{{cr} :  }} & \lstick{/_{_{2}}} \cw & \controlo \cw^(0.0){^{\mathtt{cr_1=0x0}}} \cwx[-2] & \control \cw^(0.0){^{\mathtt{cr_0=0x1}}} \cwx[-1] & \cw & \cw\\
 \\ }}
 \end{document}
\ No newline at end of file
diff --git a/test/python/visualization/references/test_latex_conditional.tex b/test/python/visualization/references/test_latex_conditional.tex
--- a/test/python/visualization/references/test_latex_conditional.tex
+++ b/test/python/visualization/references/test_latex_conditional.tex
@@ -8,6 +8,6 @@
 \Qcircuit @C=1.0em @R=0.2em @!R { \\
 	 	\nghost{{q}_{0} :  } & \lstick{{q}_{0} :  } & \gate{\mathrm{H}} & \meter & \qw & \gate{\mathrm{H}} & \qw & \qw\\
 	 	\nghost{{q}_{1} :  } & \lstick{{q}_{1} :  } & \gate{\mathrm{H}} & \qw & \meter & \qw & \qw & \qw\\
-	 	\nghost{\mathrm{c :  }} & \lstick{\mathrm{c :  }} & \lstick{/_{_{2}}} \cw & \dstick{_{_{\hspace{0.0em}0}}} \cw \ar @{<=} [-2,0] & \dstick{_{_{\hspace{0.0em}1}}} \cw \ar @{<=} [-1,0] & \control \cw^(0.0){^{\mathtt{0x2}}} \cwx[-2] & \cw & \cw\\
+	 	\nghost{\mathrm{{c} :  }} & \lstick{\mathrm{{c} :  }} & \lstick{/_{_{2}}} \cw & \dstick{_{_{\hspace{0.0em}0}}} \cw \ar @{<=} [-2,0] & \dstick{_{_{\hspace{0.0em}1}}} \cw \ar @{<=} [-1,0] & \control \cw^(0.0){^{\mathtt{0x2}}} \cwx[-2] & \cw & \cw\\
 \\ }}
 \end{document}
\ No newline at end of file
diff --git a/test/python/visualization/references/test_latex_creg_initial_true.tex b/test/python/visualization/references/test_latex_creg_initial_true.tex
--- a/test/python/visualization/references/test_latex_creg_initial_true.tex
+++ b/test/python/visualization/references/test_latex_creg_initial_true.tex
@@ -8,6 +8,6 @@
 \Qcircuit @C=1.0em @R=0.2em @!R { \\
 	 	\nghost{{q}_{0} : \ket{{0}} } & \lstick{{q}_{0} : \ket{{0}} } & \gate{\mathrm{X}} & \gate{\mathrm{H}} & \qw & \qw\\
 	 	\nghost{{q}_{1} : \ket{{0}} } & \lstick{{q}_{1} : \ket{{0}} } & \gate{\mathrm{X}} & \qw & \qw & \qw\\
-	 	\nghost{\mathrm{c : 0 }} & \lstick{\mathrm{c : 0 }} & \lstick{/_{_{2}}} \cw & \cw & \cw & \cw\\
+	 	\nghost{\mathrm{{c} : 0 }} & \lstick{\mathrm{{c} : 0 }} & \lstick{/_{_{2}}} \cw & \cw & \cw & \cw\\
 \\ }}
 \end{document}
\ No newline at end of file
diff --git a/test/python/visualization/references/test_latex_meas_cond_bits_true.tex b/test/python/visualization/references/test_latex_meas_cond_bits_true.tex
--- a/test/python/visualization/references/test_latex_meas_cond_bits_true.tex
+++ b/test/python/visualization/references/test_latex_meas_cond_bits_true.tex
@@ -10,8 +10,8 @@
 	 	\nghost{{1} :  } & \lstick{{1} :  } & \qw & \qw & \qw & \qw & \qw\\
 	 	\nghost{\mathrm{{0} :  }} & \lstick{\mathrm{{0} :  }} & \cw & \cw & \cw & \cw & \cw\\
 	 	\nghost{\mathrm{{1} :  }} & \lstick{\mathrm{{1} :  }} & \cw & \cw & \dstick{_{_{\hspace{0.0em}}}} \cw \ar @{<=} [-3,0] & \cw & \cw\\
-	 	\nghost{\mathrm{cr :  }} & \lstick{\mathrm{cr :  }} & \lstick{/_{_{2}}} \cw & \cw & \cw & \cw & \cw\\
+	 	\nghost{\mathrm{{cr} :  }} & \lstick{\mathrm{{cr} :  }} & \lstick{/_{_{2}}} \cw & \cw & \cw & \cw & \cw\\
 	 	\nghost{\mathrm{{4} :  }} & \lstick{\mathrm{{4} :  }} & \cw & \cw & \cw & \cw & \cw\\
-	 	\nghost{\mathrm{cs :  }} & \lstick{\mathrm{cs :  }} & \lstick{/_{_{3}}} \cw & \controlo \cw^(0.0){^{\mathtt{cs_1=0x0}}} \cwx[-6] & \cw & \cw & \cw\\
+	 	\nghost{\mathrm{{cs} :  }} & \lstick{\mathrm{{cs} :  }} & \lstick{/_{_{3}}} \cw & \controlo \cw^(0.0){^{\mathtt{cs_1=0x0}}} \cwx[-6] & \cw & \cw & \cw\\
 \\ }}
 \end{document}
\ No newline at end of file
diff --git a/test/python/visualization/references/test_latex_meas_cond_true.tex b/test/python/visualization/references/test_latex_meas_cond_true.tex
--- a/test/python/visualization/references/test_latex_meas_cond_true.tex
+++ b/test/python/visualization/references/test_latex_meas_cond_true.tex
@@ -8,7 +8,7 @@
 \Qcircuit @C=1.0em @R=0.2em @!R { \\
 	 	\nghost{{qr}_{0} :  } & \lstick{{qr}_{0} :  } & \gate{\mathrm{H}} & \meter & \qw & \gate{\mathrm{H}} & \qw & \qw\\
 	 	\nghost{{qr}_{1} :  } & \lstick{{qr}_{1} :  } & \gate{\mathrm{H}} & \qw & \meter & \qw & \qw & \qw\\
-	 	\nghost{\mathrm{cr1 :  }} & \lstick{\mathrm{cr1 :  }} & \lstick{/_{_{2}}} \cw & \dstick{_{_{\hspace{0.0em}1}}} \cw \ar @{<=} [-2,0] & \control \cw^(-0.3){^{\mathtt{0x1}}} \cwx[-1] & \cw & \cw & \cw\\
-	 	\nghost{\mathrm{cr2 :  }} & \lstick{\mathrm{cr2 :  }} & \lstick{/_{_{2}}} \cw & \cw & \dstick{_{_{\hspace{1.5em}0}}} \cw \ar @{<=} [-2,0] & \control \cw^(0.0){^{\mathtt{0x3}}} \cwx[-3] & \cw & \cw\\
+	 	\nghost{\mathrm{{cr1} :  }} & \lstick{\mathrm{{cr1} :  }} & \lstick{/_{_{2}}} \cw & \dstick{_{_{\hspace{0.0em}1}}} \cw \ar @{<=} [-2,0] & \control \cw^(-0.3){^{\mathtt{0x1}}} \cwx[-1] & \cw & \cw & \cw\\
+	 	\nghost{\mathrm{{cr2} :  }} & \lstick{\mathrm{{cr2} :  }} & \lstick{/_{_{2}}} \cw & \cw & \dstick{_{_{\hspace{1.5em}0}}} \cw \ar @{<=} [-2,0] & \control \cw^(0.0){^{\mathtt{0x3}}} \cwx[-3] & \cw & \cw\\
 \\ }}
 \end{document}
\ No newline at end of file
diff --git a/test/python/visualization/references/test_latex_meas_condition.tex b/test/python/visualization/references/test_latex_meas_condition.tex
--- a/test/python/visualization/references/test_latex_meas_condition.tex
+++ b/test/python/visualization/references/test_latex_meas_condition.tex
@@ -8,6 +8,6 @@
 \Qcircuit @C=1.0em @R=0.2em @!R { \\
 	 	\nghost{{qr}_{0} :  } & \lstick{{qr}_{0} :  } & \gate{\mathrm{H}} & \meter & \qw & \qw & \qw\\
 	 	\nghost{{qr}_{1} :  } & \lstick{{qr}_{1} :  } & \qw & \qw & \gate{\mathrm{H}} & \qw & \qw\\
-	 	\nghost{\mathrm{cr :  }} & \lstick{\mathrm{cr :  }} & \lstick{/_{_{2}}} \cw & \dstick{_{_{\hspace{0.0em}0}}} \cw \ar @{<=} [-2,0] & \control \cw^(0.0){^{\mathtt{0x1}}} \cwx[-1] & \cw & \cw\\
+	 	\nghost{\mathrm{{cr} :  }} & \lstick{\mathrm{{cr} :  }} & \lstick{/_{_{2}}} \cw & \dstick{_{_{\hspace{0.0em}0}}} \cw \ar @{<=} [-2,0] & \control \cw^(0.0){^{\mathtt{0x1}}} \cwx[-1] & \cw & \cw\\
 \\ }}
 \end{document}
\ No newline at end of file
diff --git a/test/python/visualization/references/test_latex_multi_underscore_false.tex b/test/python/visualization/references/test_latex_multi_underscore_false.tex
new file mode 100644
--- /dev/null
+++ b/test/python/visualization/references/test_latex_multi_underscore_false.tex
@@ -0,0 +1,18 @@
+\documentclass[border=2px]{standalone}
+
+\usepackage[braket, qm]{qcircuit}
+\usepackage{graphicx}
+
+\begin{document}
+\scalebox{1.0}{
+\Qcircuit @C=1.0em @R=1.0em @!R { \\
+	 	\nghost{{q1\_re\_\_g\_\_g} :  } & \lstick{{q1\_re\_\_g\_\_g} :  } & \qw & \qw\\
+	 	\nghost{{q3\_re\_g\_\_g}_{0} :  } & \lstick{{q3\_re\_g\_\_g}_{0} :  } & \qw & \qw\\
+	 	\nghost{{q3\_re\_g\_\_g}_{1} :  } & \lstick{{q3\_re\_g\_\_g}_{1} :  } & \qw & \qw\\
+	 	\nghost{{q3\_re\_g\_\_g}_{2} :  } & \lstick{{q3\_re\_g\_\_g}_{2} :  } & \qw & \qw\\
+	 	\nghost{{c1\_re\_g\_\_g} :  } & \lstick{{c1\_re\_g\_\_g} :  } & \cw & \cw\\
+	 	\nghost{{c3\_re\_g\_\_g}_{0} :  } & \lstick{{c3\_re\_g\_\_g}_{0} :  } & \cw & \cw\\
+	 	\nghost{{c3\_re\_g\_\_g}_{1} :  } & \lstick{{c3\_re\_g\_\_g}_{1} :  } & \cw & \cw\\
+	 	\nghost{{c3\_re\_g\_\_g}_{2} :  } & \lstick{{c3\_re\_g\_\_g}_{2} :  } & \cw & \cw\\
+\\ }}
+\end{document}
\ No newline at end of file
diff --git a/test/python/visualization/references/test_latex_multi_underscore_true.tex b/test/python/visualization/references/test_latex_multi_underscore_true.tex
new file mode 100644
--- /dev/null
+++ b/test/python/visualization/references/test_latex_multi_underscore_true.tex
@@ -0,0 +1,16 @@
+\documentclass[border=2px]{standalone}
+
+\usepackage[braket, qm]{qcircuit}
+\usepackage{graphicx}
+
+\begin{document}
+\scalebox{1.0}{
+\Qcircuit @C=1.0em @R=1.0em @!R { \\
+	 	\nghost{{q1\_re\_\_g\_\_g} :  } & \lstick{{q1\_re\_\_g\_\_g} :  } & \qw & \qw\\
+	 	\nghost{{q3\_re\_g\_\_g}_{0} :  } & \lstick{{q3\_re\_g\_\_g}_{0} :  } & \qw & \qw\\
+	 	\nghost{{q3\_re\_g\_\_g}_{1} :  } & \lstick{{q3\_re\_g\_\_g}_{1} :  } & \qw & \qw\\
+	 	\nghost{{q3\_re\_g\_\_g}_{2} :  } & \lstick{{q3\_re\_g\_\_g}_{2} :  } & \qw & \qw\\
+	 	\nghost{\mathrm{{c1\_re\_g\_\_g} :  }} & \lstick{\mathrm{{c1\_re\_g\_\_g} :  }} & \lstick{/_{_{1}}} \cw & \cw\\
+	 	\nghost{\mathrm{{c3\_re\_g\_\_g} :  }} & \lstick{\mathrm{{c3\_re\_g\_\_g} :  }} & \lstick{/_{_{3}}} \cw & \cw\\
+\\ }}
+\end{document}
\ No newline at end of file
diff --git a/test/python/visualization/references/test_latex_no_barriers_false.tex b/test/python/visualization/references/test_latex_no_barriers_false.tex
--- a/test/python/visualization/references/test_latex_no_barriers_false.tex
+++ b/test/python/visualization/references/test_latex_no_barriers_false.tex
@@ -8,6 +8,6 @@
 \Qcircuit @C=1.0em @R=0.2em @!R { \\
 	 	\nghost{{q}_{0} :  } & \lstick{{q}_{0} :  } & \gate{\mathrm{H}} & \qw & \qw\\
 	 	\nghost{{q}_{1} :  } & \lstick{{q}_{1} :  } & \gate{\mathrm{H}} & \qw & \qw\\
-	 	\nghost{\mathrm{c :  }} & \lstick{\mathrm{c :  }} & \lstick{/_{_{2}}} \cw & \cw & \cw\\
+	 	\nghost{\mathrm{{c} :  }} & \lstick{\mathrm{{c} :  }} & \lstick{/_{_{2}}} \cw & \cw & \cw\\
 \\ }}
 \end{document}
\ No newline at end of file
diff --git a/test/python/visualization/references/test_latex_no_ops.tex b/test/python/visualization/references/test_latex_no_ops.tex
--- a/test/python/visualization/references/test_latex_no_ops.tex
+++ b/test/python/visualization/references/test_latex_no_ops.tex
@@ -8,6 +8,6 @@
 \Qcircuit @C=1.0em @R=1.0em @!R { \\
 	 	\nghost{{q}_{0} :  } & \lstick{{q}_{0} :  } & \qw & \qw\\
 	 	\nghost{{q}_{1} :  } & \lstick{{q}_{1} :  } & \qw & \qw\\
-	 	\nghost{\mathrm{c :  }} & \lstick{\mathrm{c :  }} & \lstick{/_{_{3}}} \cw & \cw\\
+	 	\nghost{\mathrm{{c} :  }} & \lstick{\mathrm{{c} :  }} & \lstick{/_{_{3}}} \cw & \cw\\
 \\ }}
 \end{document}
\ No newline at end of file
diff --git a/test/python/visualization/references/test_latex_partial_layout.tex b/test/python/visualization/references/test_latex_partial_layout.tex
--- a/test/python/visualization/references/test_latex_partial_layout.tex
+++ b/test/python/visualization/references/test_latex_partial_layout.tex
@@ -6,10 +6,10 @@
 \begin{document}
 \scalebox{1.0}{
 \Qcircuit @C=1.0em @R=0.2em @!R { \\
-	 	\nghost{{q}_{2} \mapsto {0} :  } & \lstick{{q}_{2} \mapsto {0} :  } & \qw & \qw & \qw\\
-	 	\nghost{{q}_{0} \mapsto {1} :  } & \lstick{{q}_{0} \mapsto {1} :  } & \qw & \qw & \qw\\
-	 	\nghost{{q}_{1} \mapsto {2} :  } & \lstick{{q}_{1} \mapsto {2} :  } & \gate{\mathrm{U_2}\,(\mathrm{0,\pi})} & \qw & \qw\\
-	 	\nghost{{ancilla}_{0} \mapsto {3} :  } & \lstick{{ancilla}_{0} \mapsto {3} :  } & \qw & \qw & \qw\\
-	 	\nghost{{ancilla}_{1} \mapsto {4} :  } & \lstick{{ancilla}_{1} \mapsto {4} :  } & \qw & \qw & \qw\\
+	 	\nghost{{q}_{2}\;\mapsto\;{0} :  } & \lstick{{q}_{2}\;\mapsto\;{0} :  } & \qw & \qw & \qw\\
+	 	\nghost{{q}_{0}\;\mapsto\;{1} :  } & \lstick{{q}_{0}\;\mapsto\;{1} :  } & \qw & \qw & \qw\\
+	 	\nghost{{q}_{1}\;\mapsto\;{2} :  } & \lstick{{q}_{1}\;\mapsto\;{2} :  } & \gate{\mathrm{U_2}\,(\mathrm{0,\pi})} & \qw & \qw\\
+	 	\nghost{{ancilla}_{0}\;\mapsto\;{3} :  } & \lstick{{ancilla}_{0}\;\mapsto\;{3} :  } & \qw & \qw & \qw\\
+	 	\nghost{{ancilla}_{1}\;\mapsto\;{4} :  } & \lstick{{ancilla}_{1}\;\mapsto\;{4} :  } & \qw & \qw & \qw\\
 \\ }}
 \end{document}
\ No newline at end of file
diff --git a/test/python/visualization/references/test_latex_plot_barriers_false.tex b/test/python/visualization/references/test_latex_plot_barriers_false.tex
--- a/test/python/visualization/references/test_latex_plot_barriers_false.tex
+++ b/test/python/visualization/references/test_latex_plot_barriers_false.tex
@@ -8,6 +8,6 @@
 \Qcircuit @C=1.0em @R=0.2em @!R { \\
 	 	\nghost{{q}_{0} :  } & \lstick{{q}_{0} :  } & \gate{\mathrm{H}} & \qw & \qw & \qw & \qw & \qw\\
 	 	\nghost{{q}_{1} :  } & \lstick{{q}_{1} :  } & \qw & \qw & \gate{\mathrm{H}} & \qw & \qw & \qw\\
-	 	\nghost{\mathrm{c :  }} & \lstick{\mathrm{c :  }} & \lstick{/_{_{2}}} \cw & \cw & \cw & \cw & \cw & \cw\\
+	 	\nghost{\mathrm{{c} :  }} & \lstick{\mathrm{{c} :  }} & \lstick{/_{_{2}}} \cw & \cw & \cw & \cw & \cw & \cw\\
 \\ }}
 \end{document}
\ No newline at end of file
diff --git a/test/python/visualization/references/test_latex_plot_barriers_true.tex b/test/python/visualization/references/test_latex_plot_barriers_true.tex
--- a/test/python/visualization/references/test_latex_plot_barriers_true.tex
+++ b/test/python/visualization/references/test_latex_plot_barriers_true.tex
@@ -8,6 +8,6 @@
 \Qcircuit @C=1.0em @R=0.2em @!R { \\
 	 	\nghost{{q}_{0} :  } & \lstick{{q}_{0} :  } & \gate{\mathrm{H}} \barrier[0em]{1} & \qw & \qw \barrier[0em]{1} & \qw & \qw & \qw\\
 	 	\nghost{{q}_{1} :  } & \lstick{{q}_{1} :  } & \qw & \qw & \gate{\mathrm{H}} & \qw & \qw & \qw\\
-	 	\nghost{\mathrm{c :  }} & \lstick{\mathrm{c :  }} & \lstick{/_{_{2}}} \cw & \cw & \cw & \cw & \cw & \cw\\
+	 	\nghost{\mathrm{{c} :  }} & \lstick{\mathrm{{c} :  }} & \lstick{/_{_{2}}} \cw & \cw & \cw & \cw & \cw & \cw\\
 \\ }}
 \end{document}
\ No newline at end of file
diff --git a/test/python/visualization/references/test_latex_plot_partial_barriers.tex b/test/python/visualization/references/test_latex_plot_partial_barriers.tex
--- a/test/python/visualization/references/test_latex_plot_partial_barriers.tex
+++ b/test/python/visualization/references/test_latex_plot_partial_barriers.tex
@@ -8,6 +8,6 @@
 \Qcircuit @C=1.0em @R=0.2em @!R { \\
 	 	\nghost{{q}_{0} :  } & \lstick{{q}_{0} :  } & \gate{\mathrm{H}} \barrier[0em]{0} & \qw & \gate{\mathrm{H}} & \qw & \qw\\
 	 	\nghost{{q}_{1} :  } & \lstick{{q}_{1} :  } & \qw & \qw & \qw & \qw & \qw\\
-	 	\nghost{\mathrm{c :  }} & \lstick{\mathrm{c :  }} & \lstick{/_{_{2}}} \cw & \cw & \cw & \cw & \cw\\
+	 	\nghost{\mathrm{{c} :  }} & \lstick{\mathrm{{c} :  }} & \lstick{/_{_{2}}} \cw & \cw & \cw & \cw & \cw\\
 \\ }}
 \end{document}
\ No newline at end of file
diff --git a/test/python/visualization/references/test_latex_registerless_one_bit.tex b/test/python/visualization/references/test_latex_registerless_one_bit.tex
--- a/test/python/visualization/references/test_latex_registerless_one_bit.tex
+++ b/test/python/visualization/references/test_latex_registerless_one_bit.tex
@@ -13,6 +13,6 @@
 	 	\nghost{{qry} :  } & \lstick{{qry} :  } & \qw & \qw\\
 	 	\nghost{\mathrm{{0} :  }} & \lstick{\mathrm{{0} :  }} & \cw & \cw\\
 	 	\nghost{\mathrm{{1} :  }} & \lstick{\mathrm{{1} :  }} & \cw & \cw\\
-	 	\nghost{\mathrm{crx :  }} & \lstick{\mathrm{crx :  }} & \lstick{/_{_{2}}} \cw & \cw\\
+	 	\nghost{\mathrm{{crx} :  }} & \lstick{\mathrm{{crx} :  }} & \lstick{/_{_{2}}} \cw & \cw\\
 \\ }}
 \end{document}
\ No newline at end of file
diff --git a/test/python/visualization/references/test_latex_teleport.tex b/test/python/visualization/references/test_latex_teleport.tex
--- a/test/python/visualization/references/test_latex_teleport.tex
+++ b/test/python/visualization/references/test_latex_teleport.tex
@@ -9,6 +9,6 @@
 	 	\nghost{{q}_{0} :  } & \lstick{{q}_{0} :  } & \gate{\mathrm{U}\,(\mathrm{0.3,0.2,0.1})} & \qw \barrier[0em]{2} & \qw & \ctrl{1} & \gate{\mathrm{H}} & \meter & \qw & \qw & \qw & \qw & \qw\\
 	 	\nghost{{q}_{1} :  } & \lstick{{q}_{1} :  } & \gate{\mathrm{H}} & \ctrl{1} & \qw & \targ & \meter & \qw & \qw & \qw & \qw & \qw & \qw\\
 	 	\nghost{{q}_{2} :  } & \lstick{{q}_{2} :  } & \qw & \targ & \qw & \qw & \qw & \qw & \gate{\mathrm{Z}} & \gate{\mathrm{X}} & \meter & \qw & \qw\\
-	 	\nghost{\mathrm{c :  }} & \lstick{\mathrm{c :  }} & \lstick{/_{_{3}}} \cw & \cw & \cw & \cw & \dstick{_{_{\hspace{0.0em}1}}} \cw \ar @{<=} [-2,0] & \dstick{_{_{\hspace{0.0em}0}}} \cw \ar @{<=} [-3,0] & \control \cw^(0.0){^{\mathtt{0x1}}} \cwx[-1] & \control \cw^(0.0){^{\mathtt{0x2}}} \cwx[-1] & \dstick{_{_{\hspace{0.0em}2}}} \cw \ar @{<=} [-1,0] & \cw & \cw\\
+	 	\nghost{\mathrm{{c} :  }} & \lstick{\mathrm{{c} :  }} & \lstick{/_{_{3}}} \cw & \cw & \cw & \cw & \dstick{_{_{\hspace{0.0em}1}}} \cw \ar @{<=} [-2,0] & \dstick{_{_{\hspace{0.0em}0}}} \cw \ar @{<=} [-3,0] & \control \cw^(0.0){^{\mathtt{0x1}}} \cwx[-1] & \control \cw^(0.0){^{\mathtt{0x2}}} \cwx[-1] & \dstick{_{_{\hspace{0.0em}2}}} \cw \ar @{<=} [-1,0] & \cw & \cw\\
 \\ }}
 \end{document}
\ No newline at end of file
diff --git a/test/python/visualization/test_circuit_latex.py b/test/python/visualization/test_circuit_latex.py
--- a/test/python/visualization/test_circuit_latex.py
+++ b/test/python/visualization/test_circuit_latex.py
@@ -58,6 +58,20 @@ def test_tiny_circuit(self):
 
         self.assertEqualToReference(filename)
 
+    def test_multi_underscore_reg_names(self):
+        """Test multi-underscores in register names display properly"""
+        filename1 = self._get_resource_path("test_latex_multi_underscore_true.tex")
+        filename2 = self._get_resource_path("test_latex_multi_underscore_false.tex")
+        q_reg1 = QuantumRegister(1, "q1_re__g__g")
+        q_reg3 = QuantumRegister(3, "q3_re_g__g")
+        c_reg1 = ClassicalRegister(1, "c1_re_g__g")
+        c_reg3 = ClassicalRegister(3, "c3_re_g__g")
+        circuit = QuantumCircuit(q_reg1, q_reg3, c_reg1, c_reg3)
+        circuit_drawer(circuit, cregbundle=True, filename=filename1, output="latex_source")
+        circuit_drawer(circuit, cregbundle=False, filename=filename2, output="latex_source")
+        self.assertEqualToReference(filename1)
+        self.assertEqualToReference(filename2)
+
     def test_normal_circuit(self):
         """Test draw normal size circuit."""
         filename = self._get_resource_path("test_latex_normal.tex")