A beginner-friendly, step-by-step guide to building your first quantum computing program using IBM's Qiskit Python package. Creating a Bell State โ the quantum equivalent of "Hello, World!" โ demonstrating superposition and entanglement.
You don't need a physics degree. If you know basic Python, you're ready.
- What Are We Building?
- Prerequisites
- Installation
- The Full Program
- Step-by-Step Breakdown
- Understanding the Math
- Expected Output
- Circuit Diagram
- Quick Reference
- What's Next?
- Resources
In classical computing, a bit is either 0 or 1. In quantum computing, a qubit can be both at the same time โ this is called superposition. When two qubits are linked so that measuring one instantly determines the other, that's called entanglement.
We're going to:
- Create 2 qubits
- Put one into superposition (both 0 and 1 simultaneously)
- Entangle them (link their fates)
- Measure both
- See that we only ever get
00or11โ never01or10
This is the Bell State, and Einstein famously called it "spooky action at a distance."
- Python 3.8 or higher
- Basic Python knowledge (variables, functions, imports)
pip install qiskitfrom qiskit import QuantumCircuit
from qiskit.primitives import StatevectorSampler
# Step 1: Create a quantum circuit with 2 qubits and 2 classical bits
qc = QuantumCircuit(2, 2)
# Step 2: Apply a Hadamard gate to qubit 0 (superposition)
qc.h(0)
# Step 3: Apply a CNOT gate (entangle qubit 0 and qubit 1)
qc.cx(0, 1)
# Step 4: Measure both qubits
qc.measure([0, 1], [0, 1])
# Step 5: Draw the circuit
print(qc.draw())
# Step 6: Simulate and get results
sampler = StatevectorSampler()
job = sampler.run([qc], shots=1000)
result = job.result()
counts = result[0].data.meas.get_counts()
print(counts)from qiskit import QuantumCircuit
from qiskit.primitives import StatevectorSampler
qc = QuantumCircuit(2, 2)| What | Why |
|---|---|
QuantumCircuit |
The blueprint where you design your quantum program โ like a blank sheet of music. |
StatevectorSampler |
A simulator that runs your quantum circuit on your laptop (no real quantum computer needed). |
QuantumCircuit(2, 2) |
Creates a circuit with 2 qubits (quantum bits) and 2 classical bits (to store measurement results). |
Both qubits start in state |0โฉ โ quantum notation for "definitely zero."
qc.h(0)The Hadamard gate (H) is applied to qubit 0. It transforms the qubit from a definite |0โฉ into a superposition โ equally likely to be measured as 0 or 1.
Analogy: Imagine flipping a coin and freezing it mid-air. It's not heads, not tails โ it's both until you look.
Mathematically:
|0โฉ โ (|0โฉ + |1โฉ) / โ2
Don't worry โ we break this math down fully in the Understanding the Math section below.
qc.cx(0, 1)The CNOT (Controlled-NOT) gate links two qubits:
- Control qubit: qubit 0
- Target qubit: qubit 1
- Rule: "If qubit 0 is 1, flip qubit 1. Otherwise, do nothing."
Since qubit 0 is in superposition, something remarkable happens โ the two qubits become entangled:
- If you measure qubit 0 as
0โ qubit 1 is always0 - If you measure qubit 0 as
1โ qubit 1 is always1
Their fates are now permanently linked. This is the Bell State:
(|00โฉ + |11โฉ) / โ2
qc.measure([0, 1], [0, 1])This measures both qubits and writes the results into the classical bits:
- Qubit 0's result โ classical bit 0
- Qubit 1's result โ classical bit 1
Measurement collapses the superposition. The coin finally lands.
sampler = StatevectorSampler() # Create the simulator
job = sampler.run([qc], shots=1000) # Run the circuit 1000 times
result = job.result() # Get results
counts = result[0].data.c.get_counts() # Count outcomes
print(counts)| Line | What It Does |
|---|---|
StatevectorSampler() |
Initializes a mathematical simulator of a quantum computer. |
shots=1000 |
Runs the experiment 1000 times to build up statistics. |
get_counts() |
Counts how many times each outcome (00, 11, etc.) appeared. |
The | โฉ symbols are Dirac notation (or "ket" notation) โ just a naming convention for quantum states:
|0โฉ= the qubit is definitely 0 (like a light switch that's OFF)|1โฉ= the qubit is definitely 1 (like a light switch that's ON)
Think of a box with a ball inside:
| State | What's in the box | When you open it |
|---|---|---|
|0โฉ |
A red ball (definitely) | Always red. 100% of the time. |
|1โฉ |
A blue ball (definitely) | Always blue. 100% of the time. |
| Superposition | Both at once | Could be either โ you can't predict which. |
Key idea: |0โฉ and |1โฉ are certain states. There's nothing weird about them. The weirdness only starts when a gate like Hadamard mixes them.
A qubit in superposition is written as:
ฮฑ|0โฉ + ฮฒ|1โฉ
Where ฮฑ and ฮฒ are numbers (called amplitudes) that determine the probabilities:
Probability of measuring 0 = ฮฑยฒ
Probability of measuring 1 = ฮฒยฒ
And since probabilities must add to 100%:
ฮฑยฒ + ฮฒยฒ = 1 (always!)
Examples:
| State | ฮฑ | ฮฒ | P(|0โฉ) = ฮฑยฒ | P(|1โฉ) = ฮฒยฒ |
|---|---|---|---|---|
|0โฉ |
1 | 0 | 100% | 0% |
|1โฉ |
0 | 1 | 0% | 100% |
(|0โฉ+|1โฉ)/โ2 |
1/โ2 | 1/โ2 | 50% | 50% |
After the Hadamard gate:
ฮฑ = 1/โ2, ฮฒ = 1/โ2
We need ฮฑ = ฮฒ (equal chances) and ฮฑยฒ + ฮฒยฒ = 1:
ฮฑยฒ + ฮฑยฒ = 1
2ฮฑยฒ = 1
ฮฑยฒ = 1/2
ฮฑ = 1/โ2 โ 0.7071
โ2 is the only number that gives a perfect 50/50 split. It's not arbitrary โ it's mathematically necessary.
โโโโโ โโโ
q_0: โค H โโโโ โโโคMโโโโ
โโโโโโโโดโโโโฅโโโโ
q_1: โโโโโโค X โโโซโโคMโ
โโโโโ โ โโฅโ
c: 2/โโโโโโโโโโโโฉโโโฉโ
0 1
Read left to right: H gate on q_0 โ CNOT connecting q_0 to q_1 โ Measurements.
{'00': 512, '11': 488}
Out of 1000 runs:
00appeared ~500 times11appeared ~500 times01and10never appear โ because the qubits are entangled!
Your exact numbers will vary slightly (it's random!), but the ~50/50 split between
00and11is consistent.
| Concept | Symbol / Code | What It Does |
|---|---|---|
| Qubit in state zero | |0โฉ |
Definitely 0 when measured |
| Qubit in state one | |1โฉ |
Definitely 1 when measured |
| Superposition | (|0โฉ + |1โฉ) / โ2 |
50% chance of 0 or 1 |
| Hadamard gate | qc.h(0) |
Puts a qubit into superposition |
| CNOT gate | qc.cx(0, 1) |
Entangles two qubits |
| Measurement | qc.measure() |
Collapses superposition, gives a definite result |
| Bell State | (|00โฉ + |11โฉ) / โ2 |
Two entangled qubits โ always measured the same |
Now that you've mastered the Bell State, here are some paths to explore:
- More gates โ Try the Pauli-X (
qc.x()), Pauli-Z (qc.z()), and Phase gates - Quantum Teleportation โ Transfer a qubit's state using entanglement
- Deutsch-Jozsa Algorithm โ Your first quantum algorithm that beats classical computers
- Run on real hardware โ Use IBM Quantum to run on actual quantum computers in the cloud
MIT
Built with โค๏ธ and quantum entanglement.