Toffoli gate program

This is a simple program for beginners demonstrating the toffoli (CCNOT) gate.

Download: Here

What does it do

This program entangles 3 qubits with a toffoli gate.

  1. First 3 qubits are initialised.

  2. A pauli X gate is applied to the first qubit so that its state is 1.

  3. A pauli X gate is applied to the second qubit so that its state is 1.

  4. Then a toffoli is applied to all qubits. If the first and second qubit are 1 then the third qubits state will be flipped to 1.

Note: If the qubits state was 0 then it will be flipped to 1. If vice versa then it will be flipped to 0.

Device used

The ibmq_qasm_simulator is used which simulates a real quantum device. Note that results from this simulator contain no errors unlike on the real quantum devices which are fairly noisy.

Note: This program requires that you have an API token. To get one sign up to IBM Q Experience and get your token here: https://quantum-computing.ibm.com/account

Circuit Diagram

Circuit diagram of the program

Circuit diagram of the program

Source

print('\n Quantum Toffoli Program')
print('--------------------------')
print('Programmed by Macauley Coggins for Quantum UK\n')

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute,IBMQ

IBMQ.enable_account('Enter API key here')
provider = IBMQ.get_provider(hub='ibm-q')

q = QuantumRegister(3,'q')
c = ClassicalRegister(3,'c')

circuit = QuantumCircuit(q,c)
circuit.x(q[0])
circuit.x(q[1])
circuit.ccx(q[0],q[1],q[2])
circuit.measure(q,c)

backend = provider.get_backend('ibmq_qasm_simulator')

print('Provider: ',backend)

job = execute(circuit, backend, shots=1024)
                               
print('Executing Job...\n')                  
result = job.result()
counts = result.get_counts(circuit)

print('RESULT: ',counts,'\n')
print('Press any key to close')
input()

Any problems or questions associated with this program? Contact us