# Buggy code
def _initialize_latex_array(self):
    self.img_depth, self.sum_column_widths = self._get_image_depth()
    self.sum_row_heights = self.img_width
    # choose the most compact row spacing, while not squashing them
    if self.has_box:
        self.row_separation = 0.0
    elif self.has_target:
        self.row_separation = 0.2
    else:
        self.row_separation = 1.0
    self._latex = [
        [
            "\\cw" if self.wire_type[self.ordered_regs[j]] else "\\qw"
            for _ in range(self.img_depth + 1)
        ]
        for j in range(self.img_width)
    ]
    self._latex.append([" "] * (self.img_depth + 1))
    if self.cregbundle:
        offset = 0
    for i in range(self.img_width):
        if self.wire_type[self.ordered_regs[i]]:
            if self.cregbundle:
                self._latex[i][0] = (
                    "\\lstick{" + self.ordered_regs[i + offset].register.name + ":"
                )
                clbitsize = self.cregs[self.ordered_regs[i + offset].register]
                self._latex[i][1] = "{/_{_{" + str(clbitsize) + "}}} \\cw"
                offset += clbitsize - 1
            else:
                self._latex[i][0] = (
                    "\\lstick{"
                    + self.ordered_regs[i].register.name
                    + "_{"
                    + str(self.ordered_regs[i].index)
                    + "}:"
                )
            if self.initial_state:
                self._latex[i][0] += "0"
            self._latex[i][0] += "}"
        else:
            if self.layout is None:
                label = "\\lstick{{ {{{}}}_{{{}}} : ".format(
                    self.ordered_regs[i].register.name, self.ordered_regs[i].index
                )
            else:
                label = "\\lstick{{ {{{}}}_{{{}}}\\mapsto{{{}}} : ".format(
                    self.layout[self.ordered_regs[i].index].register.name,
                    self.layout[self.ordered_regs[i].index].index,
                    self.ordered_regs[i].index,
                )
            if self.initial_state:
                label += "\\ket{{0}}"
            label += " }"
            self._latex[i][0] = label


# 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 _initialize_latex_array(self):
    self.img_depth, self.sum_column_widths = self._get_image_depth()
    self.sum_row_heights = self.img_width
    # choose the most compact row spacing, while not squashing them
    if self.has_box:
        self.row_separation = 0.0
    elif self.has_target:
        self.row_separation = 0.2
    else:
        self.row_separation = 1.0
    self._latex = [
        [
            "\\cw" if self.wire_type[self.ordered_regs[j]] else "\\qw"
            for _ in range(self.img_depth + 1)
        ]
        for j in range(self.img_width)
    ]
    self._latex.append([" "] * (self.img_depth + 1))
    if self.cregbundle:
        offset = 0
    for i in range(self.img_width):
        if self.wire_type[self.ordered_regs[i]]:
            if self.cregbundle:
                self._latex[i][0] = (
                    "\\lstick{" + self.ordered_regs[i + offset].register.name + ":"
                )
                clbitsize = self.cregs[self.ordered_regs[i + offset].register]
                self._latex[i][1] = "{/_{_{" + str(clbitsize) + "}}} \\cw"
                offset += clbitsize - 1
            else:
                self._latex[i][0] = (
                    "\\lstick{"
                    + self.ordered_regs[i].register.name
                    + "_{"
                    + str(self.ordered_regs[i].index)
                    + "}:"
                )
            if self.initial_state:
                self._latex[i][0] += "0"
            self._latex[i][0] += "}"
        else:
            if self.layout is None:
                label = "\\lstick{{ {{{}}}_{{{}}} : ".format(
                    self.ordered_regs[i].register.name, self.ordered_regs[i].index
                )
            else:
                if self.layout[self.ordered_regs[i].index]:
                    label = "\\lstick{{ {{{}}}_{{{}}}\\mapsto{{{}}} : ".format(
                        self.layout[self.ordered_regs[i].index].register.name,
                        self.layout[self.ordered_regs[i].index].index,
                        self.ordered_regs[i].index,
                    )
                else:
                    label = "\\lstick{{ {{{}}} : ".format(self.ordered_regs[i].index)
            if self.initial_state:
                label += "\\ket{{0}}"
            label += " }"
            self._latex[i][0] = label