Updated: 28/10/25
Interested in learning how to program quantum computers? Then check out our Qiskit textbook Introduction to Quantum Computing with Qiskit.
Introduction
This tutorial will introduce the user to the CNOT gate and how to implement it on IBMs quantum devices.
The CNOT gate is a mulit-qubit gate that consists of two qubits. The first qubit is known as the control qubit and the second is known as the target qubit. If the control qubit is |1〉then it will flip the targets qubit state from |0〉to |1〉or vice versa.
The CNOT gates operation is described by the following matrix:
Using matrix multiplication let’s explore how the CNOT gate operates on the qubits state. Since this gate operates on two qubits the column vectors will have a row for each possible state:
For our first example let’s set the control qubit and target qubit to |0〉such that the combined state will be |00〉
This shows that the target qubits state has remained unchanged as the state is |00〉Now let’s instead set the control qubit to |1〉and the target qubit to |0〉such that the combined state will be |10〉:
Which has flipped the target qubit to |1〉such that the combined state is |11〉
Implementation
In Qiskit the CNOT gate can be implemented by initialising two qubits. The first qubits state is flipped to |1〉and the second remains |0〉. Next a CNOT gate is applied where the first qubit is the control and the second is the target qubit. Next both qubits are measured and the results will be sent back from the device.
The full Qiskit code is in the code section below.
Circuit diagram of the program
Device used
This tutorial uses the Qiskit Aer simulator in order to run the circuit. Note that results from this simulator contain no errors unlike on real quantum devices which are prone to noise.
More information on Qiskit Aer can be found here: https://qiskit.github.io/qiskit-aer/tutorials/1_aersimulator.html
In order to use to a real quantum device follow the steps here: https://quantum.cloud.ibm.com/docs/en/guides/initialize-account
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
Code
from qiskit import QuantumCircuit, transpile, ClassicalRegister, QuantumRegister from qiskit_aer import AerSimulator backend = AerSimulator() # Using local Aer simulator q = QuantumRegister(2,'q') # Initialise quantum register c = ClassicalRegister(2,'c') # Initialise classical register circuit = QuantumCircuit(q,c) # Initialise circuit circuit.x(q[0]) # Put Qubit 0 in to state |1> circuit.cx(q[0],q[1]) # Put Qubit 0 in to superposition using hadamard gate circuit.measure(q,c) # Measure qubit circ = transpile(circuit, backend) # Rewrites the circuit to match the backend's basis gates and coupling map shots = 1024 # Run and get counts result = backend.run(circ,shots=shots).result() counts = result.get_counts(circ) print('RESULT: ',counts) # Print result
Any problems or questions associated with this program? Contact us
