First time studying this topic?

Launch the interactive walkthrough to discover each component, including the 3D Q-Sphere Lab & Python in Colab.

Topic 6.12

Quantum Circuit Simulation: Module 6 Capstone

Synthesize Module 6: build a complete state-vector simulator in Python that evolves multi-qubit states and computes measurement histograms.

Learning Objectives

  • ✓Construct a complete Matrix-based Quantum Simulator in Python
  • ✓Simulate the 2-qubit Bell State and 3-qubit GHZ (Greenberger-Horne-Zeilinger) state
  • ✓Calculate state fidelity and plot output measurement histograms

📖Let's Begin with a Story

In 1989, Daniel Greenberger, Michael Horne, and Anton Zeilinger proposed an extreme quantum state that pushed entanglement to three particles: ∣GHZ⟩=∣000⟩+∣111⟩2|\text{GHZ}\rangle = \frac{|000\rangle + |111\rangle}{\sqrt{2}} In this state, three separate particles across the universe share an unbreakable tri-partite entanglement. In this Module 6 Capstone, you will build a complete quantum circuit simulator from scratch in pure Python. You will assemble the quantum gates, compute tensor products, evolve a 3-qubit register, and verify the generation of the famous GHZ State!

Why Should You Learn Quantum Circuit Simulation: Module 6 Capstone?

🎯 Practical & Engineering Stakes

Mastery of quantum circuit execution

When you can write a working quantum circuit simulator from scratch using linear algebra, you have mastered the foundational mechanics of quantum information science.

What Is It?

Simple Explanation

We are building our own mini quantum computer simulator: 1. Start with 3 qubits all set to 0: ∣000⟩|000\rangle. 2. Apply Hadamard to qubit 0 to make it spin: (∣0⟩+∣1⟩)/2(|0\rangle + |1\rangle)/\sqrt{2}. 3. Use CNOT from qubit 0 to qubit 1: now both are entangled: (∣00⟩+∣11⟩)/2(|00\rangle + |11\rangle)/\sqrt{2}. 4. Use CNOT from qubit 1 to qubit 2: now all three are entangled: (∣000⟩+∣111⟩)/2(|000\rangle + |111\rangle)/\sqrt{2}! This is the famous GHZ state!

Technical Explanation

Full-state vector simulation tracks state vector ∣ψ⟩∈C2n|\psi\rangle \in \mathbb{C}^{2^n}. When gate UU acts on qubit kk, the full operator is embedded via Kronecker products: Ufull=I⊗k⊗U⊗I⊗(n−k−1)U_{full} = I^{\otimes k} \otimes U \otimes I^{\otimes (n-k-1)}. Two-qubit gates are synthesized via projector decomposition. Born rule sampling generates empirical shot distributions.

📚 Key Terms

GHZ State

Simple: A 3-qubit entangled state: (|000> + |111>) / sqrt(2).
Technical: Maximally entangled tri-partite state ∣000⟩+∣111⟩2\frac{|000\rangle + |111\rangle}{\sqrt{2}} exhibiting non-local quantum correlations.

Statevector Simulator

Simple: A simulator that tracks the exact complex amplitudes of all states without noise.
Technical: An idealized quantum simulator that computes exact wavefunction evolution via matrix-vector multiplication without sampling error.

Q-Sphere Lab: Quantum Circuit Simulation: Module 6 Capstone

IBM Q-Sphere Model

Interactive 3D state vector exploration, unitary transformation gates, and Born rule collapse for Topic 6.12

Azimuth: 45°Elevation: 25°
+Z |0⟩-Z |1⟩+X |+⟩+Y |+i⟩01|ψ⟩
Q-Sphere Phase Color:
0 π/2 π 3π/2

Quantum Statevector

dim(H) = 2
|ψ⟩ = (1.000 + 0.000i)|0⟩
     + (0.000 + 0.000i)|1⟩
Polar Angle (θ):0.0° (0.000π rad)
Phase Angle (ϕ):0.0° (0.000π rad)

Born Rule Probabilities: P(x) = |⟨x|ψ⟩|²Σ P = 1.00

P(|0⟩) = |α|²100.0%
P(|1⟩) = |β|²0.0%
Quantum Measurement

Apply Single-Qubit Unitary Gates

Presets:
Continuous Polar Angle θ (0° = |0⟩, 180° = |1⟩):0.0°
Continuous Phase Angle ϕ (0° to 360°):0.0°
Recent Operations Log:
#1Initial state: |0⟩

📐 Mathematical Foundation

LaTeX KaTeX
∣GHZ⟩=CX12⋅CX01⋅(H0⊗I1⊗I2)∣000⟩=∣000⟩+∣111⟩2|\text{GHZ}\rangle = CX_{12} \cdot CX_{01} \cdot (H_0 \otimes I_1 \otimes I_2) |000\rangle = \frac{|000\rangle + |111\rangle}{\sqrt{2}}

What does it mean?

Synthesis of the 3-Qubit Greenberger-Horne-Zeilinger (GHZ) State: The capstone circuit of Module 6. It applies Hadamard to qubit 0 to create superposition, then cascades two CNOT gates (q0 → q1, then q1 → q2) to entangle all three qubits into a tri-partite superposition of ∣000⟩|000\rangle and ∣111⟩|111\rangle.

Understanding the Symbols

Symbol / TermMeaningBeginner Interpretation
∣GHZ⟩|\text{GHZ}\rangleGreenberger-Horne-Zeilinger state3-qubit maximally entangled state (∣000⟩+∣111⟩)/2(|000\rangle + |111\rangle)/\sqrt{2}
H0⊗I1⊗I2H_0 \otimes I_1 \otimes I_2Hadamard on qubit 0Opens qubit 0 into superposition while qubits 1 and 2 remain in ground state
CX01CX_{01}CNOT from qubit 0 to 1Entangles qubit 1 with qubit 0 ((∣000⟩+∣110⟩)/2)((|000\rangle + |110\rangle)/\sqrt{2})
CX12CX_{12}CNOT from qubit 1 to 2Entangles qubit 2 with qubit 1 ((∣000⟩+∣111⟩)/2)((|000\rangle + |111\rangle)/\sqrt{2})

📊 Numerical Example

Simulating 1,000 shots on AerSimulator vs exact Statevector:

Step 1: Statevector Simulation (Theoretical): Statevector: [1/√2, 0, 0, 0, 0, 0, 0, 1/√2]^T Exact Probabilities: P(000) = 50.0%, P(111) = 50.0%, all others = 0% Step 2: Sampling Simulation (1,000 shots): Shot counts: {'000': 498, '111': 502} Empirical Probabilities: P_sim(000) = 498 / 1000 = 49.8% P_sim(111) = 502 / 1000 = 50.2%

Result: Measurement outcomes converge to theoretical 50/50 distribution with zero occurrences of mixed states (like 001, 010, etc.).

⚙️ How Does It Work?

1

State Initialization

Initialize length-8 vector |000>.

2

Hadamard on Q0

Creates (|000> + |100>)/sqrt(2).

3

CNOT 0->1

Flips Q1 if Q0 is 1: (|000> + |110>)/sqrt(2).

4

CNOT 1->2

Flips Q2 if Q1 is 1: (|000> + |111>)/sqrt(2).

🌍 Where Is It Used?

Multi-Party Quantum Secret Sharing

Problem: Sharing an encryption key among 3 intelligence officials such that no two can decrypt without the third.

Solution: GHZ state correlations require all 3 parties to collaborate to reconstruct the key.

🎯 Try It Yourself

In a 3-qubit GHZ state (|000> + |111>)/sqrt(2), what is the probability of measuring the state |010>?

Interactive activity component

🐍 Python Hands-On: Statevector vs Shot-Based Simulation in Qiskit

Compare ideal analytical statevector simulation against stochastic shot-based sampling with AerSimulator in Qiskit.

Open Colab

1Install Required Packages

For Google Colab or Jupyter notebooks, run this shell command to install the required dependencies:

Terminal / Notebook Install Commandbash
!pip install qiskit qiskit-aer
Packages:
qiskitqiskit-aer

2Import Libraries

Import Statementspython
1from qiskit import QuantumCircuit
2from qiskit.quantum_info import Statevector
3from qiskit_aer import AerSimulator

from qiskit import QuantumCircuit → Circuit constructor

from qiskit.quantum_info import Statevector → Analytical statevector simulator

from qiskit_aer import AerSimulator → Stochastic shot-based simulator

3Demonstration Code & Logic

Python Execution Codepython
1# 1. Define quantum circuit
2qc = QuantumCircuit(2, 2)
3qc.h(0)
4qc.cx(0, 1)
5
6# 2. Analytical Simulation: Statevector (Exact Probabilities)
7sv = Statevector.from_instruction(qc)
8print('--- Method 1: Statevector Simulation (Exact) ---')
9print('Exact Probabilities:', sv.probabilities_dict())
10
11# 3. Stochastic Simulation: AerSimulator (Shot-based Sampling)
12qc.measure([0, 1], [0, 1])
13sim = AerSimulator()
14counts_100 = sim.run(qc, shots=100).result().get_counts()
15counts_10000 = sim.run(qc, shots=10000).result().get_counts()
16print('\n--- Method 2: AerSimulator (Statistical Sampling) ---')
17print('100 Shots Counts :', counts_100)
18print('10,000 Shots Counts:', counts_10000)
19print('\nNotice: As shots increase, empirical frequencies converge to exact Born rule probabilities!')
Step-by-Step Code Breakdown:

# 1. Define quantum circuit

Circuit setup

qc = QuantumCircuit(2, 2)

2 qubits

qc.h(0)

Superposition

qc.cx(0, 1)

CNOT

# 2. Analytical Simulation: Statevector (Exact Probabilities)

Method 1

sv = Statevector.from_instruction(qc)

Compute exact amplitudes

print('--- Method 1: Statevector Simulation (Exact) ---')

Header 1

print('Exact Probabilities:', sv.probabilities_dict())

{'00': 0.5, '11': 0.5}

# 3. Stochastic Simulation: AerSimulator (Shot-based Sampling)

Method 2

qc.measure([0, 1], [0, 1])

Add measurements

sim = AerSimulator()

Initialize simulator

counts_100 = sim.run(qc, shots=100).result().get_counts()

Run 100 shots

counts_10000 = sim.run(qc, shots=10000).result().get_counts()

Run 10,000 shots

print('\n--- Method 2: AerSimulator (Statistical Sampling) ---')

Header 2

print('100 Shots Counts :', counts_100)

Show statistical noise at low shots

print('10,000 Shots Counts:', counts_10000)

Show convergence to 50/50 at high shots

print('\nNotice: As shots increase, empirical frequencies converge to exact Born rule probabilities!')

Law of large numbers

📋 Complete Script (All-in-One)

Click "Copy for Colab", open Google Colab, paste into a single cell, and run.

Google Colab Notebook Scriptpython
1# =========================================================
2# Statevector vs Shot-Based Simulation in Qiskit
3# Prepared for Google Colab (Run with Shift + Enter)
4# =========================================================
5
6# Step 1: Install required packages
7!pip install qiskit qiskit-aer
8
9# Step 2: Import libraries
10from qiskit import QuantumCircuit
11from qiskit.quantum_info import Statevector
12from qiskit_aer import AerSimulator
13
14# Step 3: Run Demonstration
15# 1. Define quantum circuit
16qc = QuantumCircuit(2, 2)
17qc.h(0)
18qc.cx(0, 1)
19
20# 2. Analytical Simulation: Statevector (Exact Probabilities)
21sv = Statevector.from_instruction(qc)
22print('--- Method 1: Statevector Simulation (Exact) ---')
23print('Exact Probabilities:', sv.probabilities_dict())
24
25# 3. Stochastic Simulation: AerSimulator (Shot-based Sampling)
26qc.measure([0, 1], [0, 1])
27sim = AerSimulator()
28counts_100 = sim.run(qc, shots=100).result().get_counts()
29counts_10000 = sim.run(qc, shots=10000).result().get_counts()
30print('\n--- Method 2: AerSimulator (Statistical Sampling) ---')
31print('100 Shots Counts :', counts_100)
32print('10,000 Shots Counts:', counts_10000)
33print('\nNotice: As shots increase, empirical frequencies converge to exact Born rule probabilities!')
⚡ How to Run in Google Colab (3 Quick Steps)
  1. Click "Copy for Google Colab" above.
  2. Click Open New Google Colab Notebook (opens in a new tab).
  3. Click on the first empty code cell, press Ctrl + V (or Cmd + V) to paste, then press Shift + Enter to execute!

🔄 Algorithmic Execution Flow

1
Analytical Statevector Method

Compute exact 2^n complex amplitudes analytically without measurement collapse.

→
2
Stochastic Sampling Method

Simulate physical quantum hardware shots, observing statistical sampling variance.

→
3
Convergence Comparison

Observe how 10,000 shots approaches the exact 50.0% theoretical probability.

📥 Input / Initial State
Bell state circuit evaluated with Statevector and AerSimulator (100 vs 10,000 shots).
📤 Expected Output
Exact: {'00': 0.5, '11': 0.5}
100 Shots: {'00': ~48, '11': ~52}
10,000 Shots: {'00': ~5004, '11': ~4996}
💡 What Does This Demonstration Show?

Statevector simulation computes the mathematical wave function directly (useful for small circuits up to 30 qubits). AerSimulator models physical quantum hardware sampling, where multiple shots are required to reconstruct probability distributions.

Course Learning Resource • Slide DeckLesson 311 Slides

Lesson 3: Quantum Measurement with a One-Qubit Circuit

Hadamard gate, quantum registers, simulation, and histogram measurement.

Author: Dr. Syed Muzamil Basha• FDP Approved Curriculum
Open in New Tab
Available Slide Decks:

📝 Learning Check

Test your understanding with these questions. Remember, this is for learning, not examination!

Question 1: In the GHZ state (|000> + |111>)/sqrt(2), if qubit 0 is measured and collapses to outcome "1", what will subsequent measurements of qubits 1 and 2 yield?

Question 2: Why is classical simulation of quantum circuits exponentially expensive?

Question 3: What is the difference between statevector simulation and sampling simulation?

Question 4: What is the purpose of running multiple "shots" in quantum circuit execution?

Question 5: What simulator backends are commonly used in Qiskit?