I have been trying to make a gate in qiskit in terms of the basic gates, but I keep on getting an error when I apply it to a circuit.

This is my gate:

class LGate(Gate):
    def __init__(self, label=None):
        super().__init__('l', 1, [], label=label)

    def _define(self):

        from qiskit.circuit.quantumcircuit import QuantumCircuit
        from qiskit.circuit.library import U3Gate
        q = QuantumRegister(1, 'q')
        qc = QuantumCircuit(q, name=self.name)
        rules = [
            (U3Gate(pi, 0, pi), [q[0]], [])
        qc._data = rules
        self.definition = qc

Of course this is just an X gate, but I was just trying a basic example.

Running the program:

circ = QuantumCircuit(2)


AttributeError: 'QuantumCircuit' object has no attribute 'l'

2 Answers 2


I think you can also use the method of composite gates which might be easier to implement. The idea is that you create a circuit with gates and then turn it into an instruction by using the to_instruction() method. Once you've done this, you can consider this instruction as a predefined gate and add it to your new circuit by using the append() method. Let me show you an example code. I'll follow your example for X gate, but this code can be generalized to arbitrary gates.

from qiskit import QuantumCircuit

# customize a gate instruction (e.g., X gate)
qc = QuantumCircuit(1, name='X')
custom_gate = qc.to_instruction()

# append custom gate to a new circuit
new_circ = QuantumCircuit(2)
new_circ.append(custom_gate, [0])

q_0: ┤ X ├
q_1: ─────

As you can see, we first define single-quibit circuit qc with a X gate and turn it into an instruction. Then it can be appended to any new circuit. Here we attach it to a two-qubit circuit with name new_circ.

I hope my answer would help.


You only defined the gate, but not you haven't added the according method to the circuit. Add the following to the QuantumCircuit class:

    def l(self, qubit): 
        # adjust this import to the location of your gate
        from .library.standard_gates.l import LGate
        return self.append(LGate(), [qubit], [])

See for instance here how it works for the SwapGate.

Just as a note: It's best practice to not use the letter l because that can be difficult to distinguish from an I in some fonts.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.