# Buggy code
def _draw_regs(self):
    longest_label_width = 0
    if self.initial_state:
        initial_qbit = " |0>"
        initial_cbit = " 0"
    else:
        initial_qbit = ""
        initial_cbit = ""

    def _fix_double_script(label):
        words = 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
        ]
        label = " ".join(words).replace(" ", "\\;")
        return label

    # quantum register
    for ii, reg in enumerate(self._qreg):
        if len(self._qreg) > 1:
            if self.layout is None:
                label = "${{{name}}}_{{{index}}}$".format(
                    name=reg.register.name, index=reg.index
                )
                label = _fix_double_script(label) + initial_qbit
                text_width = self._get_text_width(label, self._style.fs)
            else:
                label = "${{{name}}}_{{{index}}} \\mapsto {{{physical}}}$".format(
                    name=self.layout[reg.index].register.name,
                    index=self.layout[reg.index].index,
                    physical=reg.index,
                )
                label = _fix_double_script(label) + initial_qbit
                text_width = self._get_text_width(label, self._style.fs)
        else:
            label = "{name}".format(name=reg.register.name)
            label = _fix_double_script(label) + initial_qbit
            text_width = self._get_text_width(label, self._style.fs)

        text_width = text_width * 1.15  # to account for larger font used
        if text_width > longest_label_width:
            longest_label_width = text_width

        pos = -ii
        self._qreg_dict[ii] = {
            "y": pos,
            "label": label,
            "index": reg.index,
            "group": reg.register,
        }
        self._cond["n_lines"] += 1

    # classical register
    if self._creg:
        n_creg = self._creg.copy()
        n_creg.pop(0)
        idx = 0
        y_off = -len(self._qreg)
        for ii, (reg, nreg) in enumerate(itertools.zip_longest(self._creg, n_creg)):
            pos = y_off - idx
            if self.cregbundle:
                label = "{}".format(reg.register.name)
                label = _fix_double_script(label) + initial_cbit
                text_width = (
                    self._get_text_width(reg.register.name, self._style.fs) * 1.15
                )
                if text_width > longest_label_width:
                    longest_label_width = text_width
                self._creg_dict[ii] = {
                    "y": pos,
                    "label": label,
                    "index": reg.index,
                    "group": reg.register,
                }
                if not (not nreg or reg.register != nreg.register):
                    continue
            else:
                label = "${}_{{{}}}$".format(reg.register.name, reg.index)
                label = _fix_double_script(label) + initial_cbit
                text_width = (
                    self._get_text_width(reg.register.name, self._style.fs) * 1.15
                )
                if text_width > longest_label_width:
                    longest_label_width = text_width
                self._creg_dict[ii] = {
                    "y": pos,
                    "label": label,
                    "index": reg.index,
                    "group": reg.register,
                }
            self._cond["n_lines"] += 1
            idx += 1

    self._reg_long_text = longest_label_width
    self.x_offset = -1.2 + self._reg_long_text


# Traceback (AttributeError)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~/anaconda3/envs/new-qiskit/lib/python3.7/site-packages/IPython/core/formatters.py in __call__(self, obj)
700                 type_pprinters=self.type_printers,
701                 deferred_pprinters=self.deferred_printers)
--> 702             printer.pretty(obj)
703             printer.flush()
704             return stream.getvalue()

~/anaconda3/envs/new-qiskit/lib/python3.7/site-packages/IPython/lib/pretty.py in pretty(self, obj)
392                         if cls is not object \
393                                 and callable(cls.__dict__.get('__repr__')):
--> 394                             return _repr_pprint(obj, self, cycle)
395
396             return _default_pprint(obj, self, cycle)

~/anaconda3/envs/new-qiskit/lib/python3.7/site-packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle)
698     """A pprint that just redirects to the normal repr function."""
699     # Find newlines and replace them with p.break_()
--> 700     output = repr(obj)
701     lines = output.splitlines()
702     with p.group():

~/anaconda3/envs/new-qiskit/lib/python3.7/site-packages/qiskit/visualization/text.py in __repr__(self)
546
547     def __repr__(self):
--> 548         return self.single_string()
549
550     def single_string(self):

~/anaconda3/envs/new-qiskit/lib/python3.7/site-packages/qiskit/visualization/text.py in single_string(self)
554             str: The lines joined by a newline (``\\n``)
555         """
--> 556         return "\n".join(self.lines())
557
558     def dump(self, filename, encoding="utf8"):

~/anaconda3/envs/new-qiskit/lib/python3.7/site-packages/qiskit/visualization/text.py in lines(self, line_length)
590
591         try:
--> 592             layers = self.build_layers()
593         except TextDrawerCregBundle:
594             self.cregbundle = False

~/anaconda3/envs/new-qiskit/lib/python3.7/site-packages/qiskit/visualization/text.py in build_layers(self)
1061             VisualizationError: When the drawing is, for some reason, impossible to be drawn.
1062         """
-> 1063         wire_names = self.wire_names(with_initial_state=self.initial_state)
1064         if not wire_names:
1065             return []

~/anaconda3/envs/new-qiskit/lib/python3.7/site-packages/qiskit/visualization/text.py in wire_names(self, with_initial_state)
664             for bit in self.qregs:
665                 label = '{name}_{index} -> {physical} ' + initial_qubit_value
--> 666                 qubit_labels.append(label.format(name=self.layout[bit.index].register.name,
667                                                  index=self.layout[bit.index].index,
668                                                  physical=bit.index))

AttributeError: 'NoneType' object has no attribute 'register'

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~/anaconda3/envs/new-qiskit/lib/python3.7/site-packages/IPython/core/formatters.py in __call__(self, obj)
343             method = get_real_method(obj, self.print_method)
344             if method is not None:
--> 345                 return method()
346             return None
347         else:

~/anaconda3/envs/new-qiskit/lib/python3.7/site-packages/qiskit/visualization/text.py in _repr_html_(self)
543                'line-height: 1.1;' \
544                'font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace">' \
--> 545                '%s</pre>' % self.single_string()
546
547     def __repr__(self):

~/anaconda3/envs/new-qiskit/lib/python3.7/site-packages/qiskit/visualization/text.py in single_string(self)
554             str: The lines joined by a newline (``\\n``)
555         """
--> 556         return "\n".join(self.lines())
557
558     def dump(self, filename, encoding="utf8"):

~/anaconda3/envs/new-qiskit/lib/python3.7/site-packages/qiskit/visualization/text.py in lines(self, line_length)
590
591         try:
--> 592             layers = self.build_layers()
593         except TextDrawerCregBundle:
594             self.cregbundle = False

~/anaconda3/envs/new-qiskit/lib/python3.7/site-packages/qiskit/visualization/text.py in build_layers(self)
1061             VisualizationError: When the drawing is, for some reason, impossible to be drawn.
1062         """
-> 1063         wire_names = self.wire_names(with_initial_state=self.initial_state)
1064         if not wire_names:
1065             return []

~/anaconda3/envs/new-qiskit/lib/python3.7/site-packages/qiskit/visualization/text.py in wire_names(self, with_initial_state)
664             for bit in self.qregs:
665                 label = '{name}_{index} -> {physical} ' + initial_qubit_value
--> 666                 qubit_labels.append(label.format(name=self.layout[bit.index].register.name,
667                                                  index=self.layout[bit.index].index,
668                                                  physical=bit.index))

AttributeError: 'NoneType' object has no attribute 'register'

# Fixed code
def _draw_regs(self):
    longest_label_width = 0
    if self.initial_state:
        initial_qbit = " |0>"
        initial_cbit = " 0"
    else:
        initial_qbit = ""
        initial_cbit = ""

    def _fix_double_script(label):
        words = 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
        ]
        label = " ".join(words).replace(" ", "\\;")
        return label

    # quantum register
    for ii, reg in enumerate(self._qreg):
        if len(self._qreg) > 1:
            if self.layout is None:
                label = "${{{name}}}_{{{index}}}$".format(
                    name=reg.register.name, index=reg.index
                )
                label = _fix_double_script(label) + initial_qbit
                text_width = self._get_text_width(label, self._style.fs)
            else:
                if self.layout[reg.index]:
                    label = "${{{name}}}_{{{index}}} \\mapsto {{{physical}}}$".format(
                        name=self.layout[reg.index].register.name,
                        index=self.layout[reg.index].index,
                        physical=reg.index,
                    )
                else:
                    label = "${{{physical}}}$".format(physical=reg.index)
                label = _fix_double_script(label) + initial_qbit
                text_width = self._get_text_width(label, self._style.fs)
        else:
            label = "{name}".format(name=reg.register.name)
            label = _fix_double_script(label) + initial_qbit
            text_width = self._get_text_width(label, self._style.fs)

        text_width = text_width * 1.15  # to account for larger font used
        if text_width > longest_label_width:
            longest_label_width = text_width

        pos = -ii
        self._qreg_dict[ii] = {
            "y": pos,
            "label": label,
            "index": reg.index,
            "group": reg.register,
        }
        self._cond["n_lines"] += 1

    # classical register
    if self._creg:
        n_creg = self._creg.copy()
        n_creg.pop(0)
        idx = 0
        y_off = -len(self._qreg)
        for ii, (reg, nreg) in enumerate(itertools.zip_longest(self._creg, n_creg)):
            pos = y_off - idx
            if self.cregbundle:
                label = "{}".format(reg.register.name)
                label = _fix_double_script(label) + initial_cbit
                text_width = (
                    self._get_text_width(reg.register.name, self._style.fs) * 1.15
                )
                if text_width > longest_label_width:
                    longest_label_width = text_width
                self._creg_dict[ii] = {
                    "y": pos,
                    "label": label,
                    "index": reg.index,
                    "group": reg.register,
                }
                if not (not nreg or reg.register != nreg.register):
                    continue
            else:
                label = "${}_{{{}}}$".format(reg.register.name, reg.index)
                label = _fix_double_script(label) + initial_cbit
                text_width = (
                    self._get_text_width(reg.register.name, self._style.fs) * 1.15
                )
                if text_width > longest_label_width:
                    longest_label_width = text_width
                self._creg_dict[ii] = {
                    "y": pos,
                    "label": label,
                    "index": reg.index,
                    "group": reg.register,
                }
            self._cond["n_lines"] += 1
            idx += 1

    self._reg_long_text = longest_label_width
    self.x_offset = -1.2 + self._reg_long_text