Skip to content

santanu2908/qiskit-bell-state-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

4 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ”ฎ Quantum Hello World โ€” First Quantum Program

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.


๐Ÿ“‹ Table of Contents


What Are We Building?

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:

  1. Create 2 qubits
  2. Put one into superposition (both 0 and 1 simultaneously)
  3. Entangle them (link their fates)
  4. Measure both
  5. See that we only ever get 00 or 11 โ€” never 01 or 10

This is the Bell State, and Einstein famously called it "spooky action at a distance."


Prerequisites

  • Python 3.8 or higher
  • Basic Python knowledge (variables, functions, imports)

Installation

pip install qiskit

The Full Program

from 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)

Step-by-Step Breakdown

Step 1 โ€” Create the Circuit

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."


Step 2 โ€” Hadamard Gate (Superposition)

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.


Step 3 โ€” CNOT Gate (Entanglement)

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 always 0
  • If you measure qubit 0 as 1 โ†’ qubit 1 is always 1

Their fates are now permanently linked. This is the Bell State:

(|00โŸฉ + |11โŸฉ) / โˆš2

Step 4 โ€” Measure

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.


Step 5 โ€” Simulate & Run

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.

Understanding the Math

What Does |0โŸฉ Mean?

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.


What Is Superposition?

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%

Why โˆš2?

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.


Expected Output

Circuit Diagram

     โ”Œโ”€โ”€โ”€โ”     โ”Œโ”€โ”
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.

Measurement Results

{'00': 512, '11': 488}

Out of 1000 runs:

  • 00 appeared ~500 times
  • 11 appeared ~500 times
  • 01 and 10 never appear โ€” because the qubits are entangled!

Your exact numbers will vary slightly (it's random!), but the ~50/50 split between 00 and 11 is consistent.


Quick Reference

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

What's Next?

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

Resources


License

MIT


Built with โค๏ธ and quantum entanglement.

About

๐Ÿ”ฎ First quantum computing program โ€” Build a Bell State (quantum entanglement) with IBM Qiskit, explained line by line. No physics degree needed.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages