I need to create the 2-qubit gates that are not supported by Qiskit (Ex: Controlled - F gate). Is there any way to create a class/object and use it as other basic gates?


qc = qiskit.QuantumCircuit(2,2)
qc.cf(0, 1)



2 Answers 2


One way to achieve this is as follows:

from qiskit import QuantumCircuit

def cf(circuit, qubit1, qubit2):
    # Create a circuit that is equivalent to your gate:
    qc = QuantumCircuit(2)
    qc.cx(0, 1)
    qc.csx(1, 0)
    qc.cx(0, 1)

    # Convert the circuit to a gate:
    sr_swap = qc.to_gate(label = '√SWAP')

    # Add the gate to your circuit which is passed as the first argument to cf function:
    circuit.append(sr_swap, [qubit1, qubit2])

# We need this line to add the method to QuantumCircuit class:
QuantumCircuit.cf = cf

Now, you can use cf as other QuantumCircuit gates:

circ = QuantumCircuit(2)
circ.cf(0, 1)

enter image description here

  • $\begingroup$ Thanks, it's work! $\endgroup$
    – Monad
    Nov 7, 2021 at 2:20

You can create a circuit that implements the gate you want and then convert that circuit to a gate. Once you have that gate, you can append it to other circuits. For example, you can do the following:

cf = QuantumCircuit(2)
cf.unitary(mat, [0, 1])
cf_gate = cf.to_gate()

Where mat is the unitary matrix that represents the effect of the controlled $F$ gate (or any other gate you want). cf_gate will be a Gate as you wanted, and you can append it to other circuits as follows:

qc = QuantumCircuit(2)
qc.append(cf_gate, [0, 1])

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.