# Buggy code
def depth(self):
    """Return circuit depth (i.e. length of critical path).
    This does not include compiler or simulator directives
    such as 'barrier' or 'snapshot'.

    Returns:
        int: Depth of circuit.

    Notes:
        The circuit depth and the DAG depth need not bt the
        same.
    """
    # Labels the registers by ints
    # and then the qubit position in
    # a register is given by reg_int+qubit_num
    reg_offset = 0
    reg_map = {}
    for reg in self.qregs + self.cregs:
        reg_map[reg.name] = reg_offset
        reg_offset += reg.size

    # A list that holds the height of each qubit
    # and classical bit.
    op_stack = [0] * reg_offset
    # Here we are playing a modified version of
    # Tetris where we stack gates, but multi-qubit
    # gates, or measurements have a block for each
    # qubit or cbit that are connected by a virtual
    # line so that they all stacked at the same depth.
    # Conditional gates act on all cbits in the register
    # they are conditioned on.
    # We do not consider barriers or snapshots as
    # They are transpiler and simulator directives.
    # The max stack height is the circuit depth.
    for op in self.data:
        if op[0].name not in ["barrier", "snapshot"]:
            levels = []
            reg_ints = []
            for ind, reg in enumerate(op[1] + op[2]):
                # Add to the stacks of the qubits and
                # cbits used in the gate.
                reg_ints.append(reg_map[reg[0].name] + reg[1])
                levels.append(op_stack[reg_ints[ind]] + 1)
            if op[0].control:
                # Controls operate over all bits in the
                # classical register they use.
                cint = reg_map[op[0].control[0].name]
                for off in range(op[0].control[0].size):
                    if cint + off not in reg_ints:
                        reg_ints.append(cint + off)
                        levels.append(op_stack[cint + off] + 1)

            max_level = max(levels)
            for ind in reg_ints:
                op_stack[ind] = max_level
    return max(op_stack)


# Traceback (AttributeError)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-20-18ab26fa2a60> in <module>
7 #circuit.h(q[2])
8 #circuit.h(q[3])
----> 9 circuit.size()
10 circuit.draw(output='mpl',style=style)

c:\users\francoisvarchon\anaconda3\envs\qis09z\lib\site-packages\qiskit_terra-0.8.0-py3.7-win-amd64.egg\qiskit\circuit\quantumcircuit.py in size(self)
436         gate_ops = 0
437         for item in self.data:
--> 438             if item.name not in ['barrier', 'snapshot']:
439                 gate_ops += 1
440         return gate_ops

AttributeError: 'tuple' object has no attribute 'name'

# Fixed code
def depth(self):
    """Return circuit depth (i.e. length of critical path).
    This does not include compiler or simulator directives
    such as 'barrier' or 'snapshot'.

    Returns:
        int: Depth of circuit.

    Notes:
        The circuit depth and the DAG depth need not bt the
        same.
    """
    # Labels the registers by ints
    # and then the qubit position in
    # a register is given by reg_int+qubit_num
    reg_offset = 0
    reg_map = {}
    for reg in self.qregs + self.cregs:
        reg_map[reg.name] = reg_offset
        reg_offset += reg.size

    # A list that holds the height of each qubit
    # and classical bit.
    op_stack = [0] * reg_offset
    # Here we are playing a modified version of
    # Tetris where we stack gates, but multi-qubit
    # gates, or measurements have a block for each
    # qubit or cbit that are connected by a virtual
    # line so that they all stacked at the same depth.
    # Conditional gates act on all cbits in the register
    # they are conditioned on.
    # We do not consider barriers or snapshots as
    # They are transpiler and simulator directives.
    # The max stack height is the circuit depth.
    for instr, qargs, cargs in self.data:
        if instr.name not in ["barrier", "snapshot"]:
            levels = []
            reg_ints = []
            for ind, reg in enumerate(qargs + cargs):
                # Add to the stacks of the qubits and
                # cbits used in the gate.
                reg_ints.append(reg_map[reg[0].name] + reg[1])
                levels.append(op_stack[reg_ints[ind]] + 1)
            if instr.control:
                # Controls operate over all bits in the
                # classical register they use.
                cint = reg_map[instr.control[0].name]
                for off in range(instr.control[0].size):
                    if cint + off not in reg_ints:
                        reg_ints.append(cint + off)
                        levels.append(op_stack[cint + off] + 1)

            max_level = max(levels)
            for ind in reg_ints:
                op_stack[ind] = max_level
    return max(op_stack)