Search⌘ K

Implementing the CNOT gate

Explore how to implement the CNOT gate in quantum circuits, understand its function in entangling qubits, and how measurement affects control and target qubit states. Learn how entanglement enables quantum systems to represent complex problems beyond classical computing, preparing you for advanced quantum machine learning concepts.

Applying the CNOT gate with |0> as the control qubit

Javascript (babel-node)
from math import sqrt
from qiskit import QuantumCircuit, Aer, execute
from qiskit.visualization import plot_histogram
# Redefine the quantum circuit
qc = QuantumCircuit(2)
# Initialise the qubits
qc.initialize([1,0], 0)
qc.initialize([1,0], 1)
# Apply the CNOT-gate
qc.cx(0,1)
# Tell Qiskit how to simulate our circuit
backend = Aer.get_backend('statevector_simulator')
# execute the qc
results = execute(qc,backend).result().get_counts()
# plot the results
plot_histogram(results)

In lines 9 to 10, when we initialize both qubits with 0|0\rangle before applying the CNOT-gate in line 13, we always measure 00, and nothing happens.

When we initialize the control qubit with 1|1\rangle and the target qubit with 0|0\rangle, we always measure 11.

Applying the CNOT gate with |1> as the control qubit

Javascript (babel-node)
qc = QuantumCircuit(2)
# Initialise the 0th qubit in the state `initial_state`
qc.initialize([0,1], 0)
qc.initialize([1,0], 1)
# Apply the CNOT-gate
qc.cx(0,1)
# Tell Qiskit how to simulate our circuit
backend = Aer.get_backend('statevector_simulator')
# execute the qc
results = execute(qc,backend).result().get_counts()
# plot the results
plot_histogram(results)

When we only look at the basis states, there is still nothing special going on here. The result equals the result that a classical circuit produces.

It becomes interesting when the control qubit is in a state of superposition. We initialize both qubits in the state 0|0\rangle, again. Then, the Hadamard gate puts the qubit QQ into the state +|+\rangle. When measured, a qubit in this state is either 0 or 1, with a probability of 50% each. The following figure depicts the quantum circuit diagram.

Applying

...