While computing the carry bit [C=0 XOR (AB)] I am unable to compute that AB in Qiskit. I don't whether Toffoli gate is available in Qiskit. So does anyone know how to perform AB, which is basically the AND gate without using the Toffoli gate if it isn't available?
1 Answer
The Toffoli gate is indeed available in qiskit. It goes by the name ccx
. Here's an example circuit where the Toffoli is used.
from qiskit import QuantumRegister, ClassicalRegister
from qiskit import QuantumCircuit, BasicAer, execute
q = QuantumRegister(3)
c = ClassicalRegister(3)
qc = QuantumCircuit(q, c)
qc.h(q[0])
qc.h(q[1])
qc.ccx(q[0], q[1], q[2])
qc.measure(q, c)
backend = BasicAer.get_backend('qasm_simulator')
job = execute(qc, backend)
result = job.result()
print(result.get_counts(qc))
The third qubit in the argument (q[2]
here) acts as the target. The other two are the controls.
If you are still interested in how to make this gate from two qubit gates, you can check out the blog post here, which explains how the following gate sequence will give you the effect you need (though it isn't quite the same as a Toffoli).
qc.ch(q[0], q[3])
qc.cz(q[1], q[3])
qc.ch(q[0], q[3])