Writing Python code to generate the Bell state is an essential skill in quantum computing. The process involves combining single-qubit superposition with the multi-qubit interaction of the CNOT gate.
The goal is to create a circuit that transforms the default state into the entangled state:
This specific state represents a scenario where two qubits are perfectly correlated. If you measure the first qubit as 0, the second is guaranteed to be 0. If you measure the first as 1, the second is guaranteed to be 1. You will never observe or .
Before writing code, we must visualize the sequence of operations. To create entanglement from a clean state, we need two steps. First, we place the control qubit into a superposition. Second, we use that superposition to control the target qubit.
This sequence links the two qubits inseparable.
Flow of operations to generate the standard Bell state.
We will use a standard quantum software development kit structure. The logic remains consistent across most Python quantum libraries. We define a register for our qubits, apply the gates in sequence, and then simulate the measurement.
First, we initialize the quantum circuit. We require two quantum bits (qubits) to hold the state and two classical bits to store the measurement results.
# Import necessary components
from qiskit import QuantumCircuit, Aer, execute
# Initialize a Quantum Circuit with 2 qubits and 2 classical bits
qc = QuantumCircuit(2, 2)
# Step 1: Apply the Hadamard gate to the first qubit (q0)
# This creates the superposition state on q0
qc.h(0)
# Step 2: Apply the CNOT gate
# q0 is the control, q1 is the target
qc.cx(0, 1)
# Step 3: Measure both qubits
# Map quantum result q0 to classical bit 0
# Map quantum result q1 to classical bit 1
qc.measure([0, 1], [0, 1])
# Display the drawing of the circuit (optional text representation)
print(qc.draw())
When you execute the drawing command, you should see a visual confirmation that the H gate is applied to line q0, followed by a vertical line connecting q0 and q1 representing the CNOT.
In a perfect mathematical model, the probability of measuring is exactly 50%, and the probability of measuring is exactly 50%. However, when we run this on a simulator (or a real quantum computer), we perform the experiment multiple times to build a statistical distribution. These repetitions are often called "shots."
We will run the circuit 1,000 times and count the outcomes.
# Use a local simulator
backend = Aer.get_backend('qasm_simulator')
# Execute the circuit 1000 times
job = execute(qc, backend, shots=1000)
# Get the results
result = job.result()
counts = result.get_counts(qc)
print("Measurement Counts:", counts)
Understanding the Output
You will likely see an output resembling this:
{'00': 492, '11': 508}
Notice two specific details in this result.
First, the numbers are roughly equal but not exactly 500 each. This is due to statistical randomness, similar to flipping a fair coin 1,000 times. You rarely get exactly 500 heads and 500 tails.
Second, and most importantly, the keys '01' and '10' are missing or have zero counts. This absence confirms entanglement. In a non-entangled system where two qubits are in random superposition, you would expect to see all four combinations (). Here, the state of the second qubit is entirely dependent on the first.
We can visualize these results to see the probability distribution clearly. The chart below represents the ideal outcome of our Bell state generator.
The probability distribution shows outcomes are restricted to only correlated states.
The logic we used above generates the state. However, there are four Bell states in total. You can generate the other three by slightly modifying the initial state of the qubits before applying the entanglement sequence (Hadamard + CNOT).
The H + CNOT sequence acts as a generic "entangler." The specific correlation you get depends on the input.
For example, to create the state (), where the results are always opposite (01 or 10), you simply flip the target qubit to start at 1.
# Circuit for Psi+ Bell State
qc2 = QuantumCircuit(2, 2)
# Initialize q1 to |1>
qc2.x(1)
# Begin Entanglement Sequence
qc2.h(0)
qc2.cx(0, 1)
qc2.measure([0, 1], [0, 1])
If you simulate qc2, you will find that the results cluster around '01' and '10', with '00' and '11' disappearing. This demonstrates that while the output states are different, the fundamental property of entanglement, perfect correlation between qubits, remains the defining feature.
Was this section helpful?
© 2026 ApX Machine LearningEngineered with