qiskit/visualization/text.py
def _instruction_to_gate(self, instruction, layer):
        """ Convert an instruction into its corresponding Gate object, and establish
        any connections it introduces between qubits"""

        current_cons = []
        connection_label = None

        # add in a gate that operates over multiple qubits
        def add_connected_gate(instruction, gates, layer, current_cons):
            for i, gate in enumerate(gates):
                layer.set_qubit(instruction.qargs[i], gate)
                actual_index = self.qregs.index(instruction.qargs[i])
                current_cons.append((actual_index, gate))

        if instruction.name == 'measure':
            gate = MeasureFrom()
            layer.set_qubit(instruction.qargs[0], gate)
            layer.set_clbit(instruction.cargs[0], MeasureTo())

        elif instruction.name in ['barrier', 'snapshot', 'save', 'load',
                                  'noise']:
            # barrier
            if not self.plotbarriers:
                return layer, current_cons, connection_label

            for qubit in instruction.qargs:
                layer.set_qubit(qubit, Barrier())

        elif instruction.name == 'swap':
            # swap
            gates = [Ex() for _ in range(len(instruction.qargs))]
            add_connected_gate(instruction, gates, layer, current_cons)

        elif instruction.name == 'cswap':
            # cswap
            gates = [Bullet(), Ex(), Ex()]
            add_connected_gate(instruction, gates, layer, current_cons)

        elif instruction.name == 'reset':
            layer.set_qubit(instruction.qargs[0], Reset())

        elif instruction.condition is not None:
            # conditional
            cllabel = TextDrawing.label_for_conditional(instruction)
            qulabel = TextDrawing.label_for_box(instruction)

            layer.set_cl_multibox(instruction.condition[0], cllabel, top_connect='┴')
            layer.set_qubit(instruction.qargs[0], BoxOnQuWire(qulabel, bot_connect='┬'))

        elif instruction.name in ['cx', 'CX', 'ccx']:
            # cx/ccx
            gates = [Bullet() for _ in range(len(instruction.qargs) - 1)]
            gates.append(BoxOnQuWire('X'))
            add_connected_gate(instruction, gates, layer, current_cons)

        elif instruction.name == 'cy':
            # cy
            gates = [Bullet(), BoxOnQuWire('Y')]
            add_connected_gate(instruction, gates, layer, current_cons)

        elif instruction.name == 'cz':
            # cz
            gates = [Bullet(), Bullet()]
            add_connected_gate(instruction, gates, layer, current_cons)

        elif instruction.name == 'ch':
            # ch
            gates = [Bullet(), BoxOnQuWire('H')]
            add_connected_gate(instruction, gates, layer, current_cons)

        elif instruction.name == 'cu1':
            # cu1
            connection_label = TextDrawing.params_for_label(instruction)[0]
            gates = [Bullet(), Bullet()]
            add_connected_gate(instruction, gates, layer, current_cons)

        elif instruction.name == 'rzz':
            # rzz
            connection_label = "zz(%s)" % TextDrawing.params_for_label(instruction)[0]
            gates = [Bullet(), Bullet()]
            add_connected_gate(instruction, gates, layer, current_cons)

        elif instruction.name == 'cu3':
            # cu3
            params = TextDrawing.params_for_label(instruction)
            gates = [Bullet(), BoxOnQuWire("U3(%s)" % ','.join(params))]
            add_connected_gate(instruction, gates, layer, current_cons)

        elif instruction.name == 'crz':
            # crz
            label = "Rz(%s)" % TextDrawing.params_for_label(instruction)[0]

            gates = [Bullet(), BoxOnQuWire(label)]
            add_connected_gate(instruction, gates, layer, current_cons)

        elif len(instruction.qargs) == 1 and not instruction.cargs:
            # unitary gate
            layer.set_qubit(instruction.qargs[0],
                            BoxOnQuWire(TextDrawing.label_for_box(instruction)))

        elif len(instruction.qargs) >= 2 and not instruction.cargs:
            # multiple qubit gate
            label = instruction.name
            params = TextDrawing.params_for_label(instruction)
            if params:
                label += "(%s)" % ','.join(params)
            layer.set_qu_multibox(instruction.qargs, label)

        else:
            raise VisualizationError(
                "Text visualizer does not know how to handle this instruction", instruction)

        # sort into the order they were declared in
        # this ensures that connected boxes have lines in the right direction
        current_cons.sort(key=lambda tup: tup[0])
        current_cons = [g for q, g in current_cons]

        return layer, current_cons, connection_label
def _instruction_to_gate(self, instruction, layer):
        """ Convert an instruction into its corresponding Gate object, and establish
        any connections it introduces between qubits"""

        current_cons = []
        connection_label = None

        # add in a gate that operates over multiple qubits
        def add_connected_gate(instruction, gates, layer, current_cons):
            for i, gate in enumerate(gates):
                layer.set_qubit(instruction.qargs[i], gate)
                actual_index = self.qregs.index(instruction.qargs[i])
                current_cons.append((actual_index, gate))

        if instruction.name == 'measure':
            gate = MeasureFrom()
            layer.set_qubit(instruction.qargs[0], gate)
            layer.set_clbit(instruction.cargs[0], MeasureTo())

        elif instruction.name in ['barrier', 'snapshot', 'save', 'load',
                                  'noise']:
            # barrier
            if not self.plotbarriers:
                return layer, current_cons, connection_label

            for qubit in instruction.qargs:
                layer.set_qubit(qubit, Barrier())

        elif instruction.name == 'swap':
            # swap
            gates = [Ex() for _ in range(len(instruction.qargs))]
            add_connected_gate(instruction, gates, layer, current_cons)

        elif instruction.name == 'cswap':
            # cswap
            gates = [Bullet(), Ex(), Ex()]
            add_connected_gate(instruction, gates, layer, current_cons)

        elif instruction.name == 'reset':
            layer.set_qubit(instruction.qargs[0], Reset())

        elif instruction.condition is not None:
            # conditional
            cllabel = TextDrawing.label_for_conditional(instruction)
            qulabel = TextDrawing.label_for_box(instruction)

            layer.set_cl_multibox(instruction.condition[0], cllabel, top_connect='┴')
            layer.set_qubit(instruction.qargs[0], BoxOnQuWire(qulabel, bot_connect='┬'))

        elif instruction.name in ['cx', 'CX', 'ccx']:
            # cx/ccx
            gates = [Bullet() for _ in range(len(instruction.qargs) - 1)]
            gates.append(BoxOnQuWire('X'))
            add_connected_gate(instruction, gates, layer, current_cons)

        elif instruction.name == 'cy':
            # cy
            gates = [Bullet(), BoxOnQuWire('Y')]
            add_connected_gate(instruction, gates, layer, current_cons)

        elif instruction.name == 'cz':
            # cz
            gates = [Bullet(), Bullet()]
            add_connected_gate(instruction, gates, layer, current_cons)

        elif instruction.name == 'ch':
            # ch
            gates = [Bullet(), BoxOnQuWire('H')]
            add_connected_gate(instruction, gates, layer, current_cons)

        elif instruction.name == 'cu1':
            # cu1
            connection_label = TextDrawing.params_for_label(instruction)[0]
            gates = [Bullet(), Bullet()]
            add_connected_gate(instruction, gates, layer, current_cons)

        elif instruction.name == 'rzz':
            # rzz
            connection_label = "zz(%s)" % TextDrawing.params_for_label(instruction)[0]
            gates = [Bullet(), Bullet()]
            add_connected_gate(instruction, gates, layer, current_cons)

        elif instruction.name == 'cu3':
            # cu3
            params = TextDrawing.params_for_label(instruction)
            gates = [Bullet(), BoxOnQuWire("U3(%s)" % ','.join(params))]
            add_connected_gate(instruction, gates, layer, current_cons)

        elif instruction.name == 'crz':
            # crz
            label = "Rz(%s)" % TextDrawing.params_for_label(instruction)[0]

            gates = [Bullet(), BoxOnQuWire(label)]
            add_connected_gate(instruction, gates, layer, current_cons)

        elif len(instruction.qargs) == 1 and not instruction.cargs:
            # unitary gate
            layer.set_qubit(instruction.qargs[0],
                            BoxOnQuWire(TextDrawing.label_for_box(instruction)))

        elif len(instruction.qargs) >= 2 and not instruction.cargs:
            # multiple qubit gate
            label = instruction.name
            params = TextDrawing.params_for_label(instruction)
            if params:
                label += "(%s)" % ','.join(params)
            layer.set_qu_multibox(instruction.qargs, label)

        else:
            raise VisualizationError(
                "Text visualizer does not know how to handle this instruction", instruction)

        # sort into the order they were declared in
        # this ensures that connected boxes have lines in the right direction
        current_cons.sort(key=lambda tup: tup[0])
        current_cons = [g for q, g in current_cons]

        return layer, current_cons, connection_label
Convert an instruction into its corresponding Gate object, and establish
        any connections it introduces between qubits