Simulators
Statevector simulators compute exact amplitudes for debugging, while shot-based samplers mimic real hardware statistics
Source: mortalapps.com- Quantum simulators are classical software programs that mathematically model quantum circuits.
- Statevector simulators calculate the exact complex amplitudes of a quantum state, providing a 'god mode' view for debugging.
- Shot-based simulators (like
StatevectorSampler) mimic physical hardware by sampling measurement outcomes based on quantum probabilities. - Classical simulation complexity grows exponentially ($2^n$), limiting local simulation to approximately 30 qubits.
- The
Statevectorclass in Qiskit allows you to inspect the exact quantum state of a circuit without adding measurement gates. - Simulators are essential for verifying circuit logic before executing jobs on real, noisy quantum hardware.
Why This Matters
Before running a quantum program on real, noisy quantum hardware, developers test and debug their circuits using classical simulators. A quantum simulator is a classical software application that mathematically models the behavior of a quantum computer. Because classical computers can represent quantum states exactly (up to the limits of double-precision floating-point numbers), simulators provide an invaluable tool for verifying the correctness of quantum algorithms.
However, simulating quantum systems classically is extremely resource-intensive. Because the state space of a quantum system grows exponentially with the number of qubits ($2^n$ complex amplitudes for $n$ qubits), a classical computer quickly runs out of memory. For example, simulating 50 qubits requires petabytes of RAM, which is why physical quantum computers are needed. For smaller systems (under 30 qubits), local simulators are incredibly fast, noiseless, and highly versatile.
In this topic, you will explore the different types of simulators available in Qiskit. You will learn the difference between statevector simulators (which calculate exact quantum states) and shot-based simulators (which mimic physical measurement statistics). You will write code to run simulations using both StatevectorSampler and AerSimulator, and understand when to use each tool in your development workflow.
Core Intuition
Think of a quantum simulator like a flight simulator used to train pilots. A flight simulator runs on a standard classical computer, but it perfectly mimics the physics of an airplane. In the simulator, you can pause time, inspect the exact wind resistance on the wings, and reset instantly if you crash. It is a safe, free, and perfect environment to practice before you step into a real, physical airplane where mistakes are costly and environmental conditions are unpredictable.
In quantum programming, a statevector simulator is like having a 'god mode' view of the physics engine. It lets you look directly at the quantum wave function (the exact complex amplitudes of every state) without measuring it. In real life, measuring a quantum state collapses it, destroying the superposition. But in a simulator, we can cheat and inspect the statevector directly, which is incredibly useful for debugging.
On the other hand, a shot-based simulator (like the QASM simulator) is like a flight simulator that forces you to play by the real-world rules. It doesn't let you see the statevector; it only lets you run the circuit and observe the final measurement outcomes, complete with statistical fluctuations. This mimics the actual behavior of physical quantum hardware, helping you prepare for real-world execution.
Visualization
Technical Explanation
Classical simulation of quantum circuits involves tracking the statevector $\lvert \psi \rangle$, which is a vector of $2^n$ complex numbers, where $n$ is the number of qubits. For a 2-qubit system, the statevector is:
$$\lvert \psi \rangle = \alpha_{00}\lvert 00 \rangle + \alpha_{01}\lvert 01 \rangle + \alpha_{10}\lvert 10 \rangle + \alpha_{11}\lvert 11 \rangle$$
Subject to the normalization constraint $\sum \lvert \alpha_i \rvert^2 = 1$. When a gate $U$ is applied, the simulator performs a matrix-vector multiplication: $\lvert \psi' \rangle = U \lvert \psi \rangle$. This is an exact mathematical calculation.
Qiskit 1.x provides two primary ways to simulate circuits locally: 1. Statevector Class: Used to inspect the exact mathematical statevector at any point in the circuit without running shots. 2. StatevectorSampler: A local primitive that calculates the exact probability distribution $P(i) = \lvert \alpha_i \rvert^2$ and then uses a classical pseudo-random number generator to sample outcomes based on those probabilities, mimicking physical shots.
Here is a complete code example demonstrating both simulation methods:
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector
from qiskit.primitives import StatevectorSampler
# Build a simple circuit (Bell state without measurements for statevector inspection)
qc_ideal = QuantumCircuit(2)
qc_ideal.h(0)
qc_ideal.cx(0, 1)
# Method 1: Inspect the exact mathematical statevector
sv = Statevector(qc_ideal)
print("Exact Statevector:")
print(sv.draw('text'))
# Build a circuit with measurements for shot-based simulation
qc_measured = QuantumCircuit(2)
qc_measured.h(0)
qc_measured.cx(0, 1)
qc_measured.measure_all() # Adds measurements to all qubits
# Method 2: Run a shot-based simulation using StatevectorSampler
sampler = StatevectorSampler()
job = sampler.run([qc_measured], shots=1000)
result = job.result()
# Retrieve counts from the first Pub (Primitive Unified Block) result
pub_result = result[0]
counts = pub_result.data.meas.get_counts() # 'meas' is the default name for measure_all()
print("\nShot-based Simulation Counts (1000 shots):")
print(counts)In this code, Statevector(qc_ideal) calculates the exact statevector, which will be $\frac{1}{\sqrt{2}}\lvert 00 \rangle + \frac{1}{\sqrt{2}}\lvert 11 \rangle$. The StatevectorSampler runs 1000 simulated shots, returning a counts dictionary where the sum of the counts equals 1000 (e.g., {'00': 492, '11': 508}).
Key Takeaways
StatevectorSampler) mimic physical hardware by sampling measurement outcomes based on quantum probabilities.Statevector class in Qiskit allows you to inspect the exact quantum state of a circuit without adding measurement gates.