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.h(0) qc.cf(0, 1)
Quantum Computing Stack Exchange is a question and answer site for engineers, scientists, programmers, and computing professionals interested in quantum computing. It only takes a minute to sign up.Sign up to join this community
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
circ = QuantumCircuit(2) circ.cf(0, 1) circ.draw('mpl')
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()
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])