Simulation: AerSimulator and StatevectorSampler
Simulation: AerSimulator and StatevectorSampler
Two ways to run a circuit locally: the AerSimulator after transpile, and the reference primitive StatevectorSampler with the Sampler V2 interface.
In modern Qiskit there are two common ways to sample from a circuit without real hardware.
The first is AerSimulator from the qiskit-aer package: we first transpile the circuit with transpile(qc, aer) to match the gates the simulator supports, then call aer.run(tqc, shots=..., seed_simulator=...) and .result().get_counts(). Aer supports noise models, several simulation methods and relatively large circuits.
The second is the StatevectorSampler primitive from qiskit.primitives: it implements the unified Sampler V2 interface that real hardware also uses. We pass a list of “primitive unified blocs” (PUBs), and each result contains a data field named after the classical register; with measure_all() the name is meas, so we read result[0].data.meas.get_counts(). The seed is passed at construction: StatevectorSampler(seed=7).
Both methods compute the probabilities exactly and then sample; that is why the counts differ between the two simulators even with the same seed (because their random number generators differ), while the theoretical distribution stays the same: $P(00)=P(11)=1/2$. A limited number of qubits cannot be exceeded because memory grows as $2^n$: a state vector for 30 qubits needs about 16 GB in double precision. On real hardware the same interface (Sampler V2) is used through the IBM Quantum service, with the difference that results are affected by noise and there is no seed. Execution here is on a classical simulator (AerSimulator or the Statevector primitives), not on a real quantum computer.
Requirements: pip install qiskit qiskit-aer
Code
from qiskit import QuantumCircuit, transpile
from qiskit_aer import AerSimulator
from qiskit.primitives import StatevectorSampler
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()
aer = AerSimulator()
tqc = transpile(qc, aer)
aer_counts = aer.run(tqc, shots=1000, seed_simulator=7).result().get_counts()
print("AerSimulator:", aer_counts)
sampler = StatevectorSampler(seed=7)
job = sampler.run([qc], shots=1000)
pub_result = job.result()[0]
print("StatevectorSampler:", pub_result.data.meas.get_counts())
Line-by-line explanation of the code
| Line | Explanation |
|---|---|
| 1-3 | Import the circuit, transpile, the Aer simulator and the StatevectorSampler primitive. |
| 5-7 | Bell circuit: H then CNOT. |
| 8 | Measure all qubits into the meas register. |
| 10 | Create an Aer simulator. |
| 11 | Transpile the circuit to the gate set supported by the simulator. |
| 12 | Run 1000 shots with seed 7 and extract the counts. |
| 13 | Print the Aer counts. |
| 15 | Create the primitive with a seed for reproducibility. |
| 16 | Run one PUB (the circuit) with 1000 shots. |
| 17 | Result of the first PUB. |
| 18 | Read the counts of the meas register. |
Output (from an actual run)
AerSimulator: {'11': 511, '00': 489}
StatevectorSampler: {'11': 498, '00': 502}
Run with Python 3.13.18 on 2026-11-20 08:14 UTC.
Aer counts from seed_simulator=7 and StatevectorSampler counts from seed=7; the two simulators use different generators, so their counts differ.
References
- Qiskit contributors (2025). Qiskit Aer documentation. GitHub Pages qiskit.github.io
- IBM Quantum (2025). Qiskit SDK API documentation. IBM Quantum Documentation quantum.cloud.ibm.com
- IBM Quantum (2025). IBM Quantum Documentation – Guides. IBM Quantum Documentation quantum.cloud.ibm.com
