← Quantum Store · Guides · Lessons · Glossary · About · Türkçe
Qiskit course: real quantum programming · Lesson 2 / 6 · 10 min
Running a circuit: the Sampler and counts
Run your circuit thousands of times with Qiskit’s Sampler, count the results and estimate probabilities from the counts.
Primitives: Sampler and Estimator
Qiskit offers two basic tools for getting results, called primitives. The Sampler runs a circuit again and again and counts the measurement results. The Estimator computes the expectation value of a quantity; we will meet it in the fourth lesson.
StatevectorSampler is a Sampler that simulates an ideal quantum computer on your own machine. The nice part is that the same code runs on IBM’s real devices with only a small change.
Run the Bell circuit 1000 times
sampler.run takes a list of circuits and returns one result per circuit; result[0] is the result of the first. Because measure_all() writes to a register called “meas”, the counts live under data.meas. get_counts() returns a dictionary of how many times each outcome appeared.
from qiskit import QuantumCircuit
from qiskit.primitives import StatevectorSampler
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()
sampler = StatevectorSampler()
job = sampler.run([qc], shots=1000)
result = job.result()
counts = result[0].data.meas.get_counts()
print(counts) # for example {'00': 507, '11': 493}
Why are the counts different every time?
shots is the number of times the circuit is run. Each run gives a random outcome; even when the ideal probability is 50%, getting exactly 500 out of 1000 is rare. Every time you run the code you see different numbers such as 507–493 or 489–511. The more shots, the closer the ratios get to the true probabilities.
To estimate a probability, divide each count by the number of shots. The “Measure 1000 times” button in the playground does exactly that.
shots = 1000
probabilities = {bits: n / shots for bits, n in counts.items()}
print(probabilities) # for example {'00': 0.507, '11': 0.493}
Several circuits and your own classical bits
sampler.run([qc1, qc2]) runs two circuits at once; their results are in result[0] and result[1]. On real devices this is much faster than queueing for each circuit separately.
Instead of measure_all() you can create classical bits yourself: QuantumCircuit(2, 2) creates two qubits and two classical bits, and qc.measure([0, 1], [0, 1]) measures the qubits into them in order. The register is then called “c” and the counts come from data.c.get_counts().
Try it yourself
In the GHZ example press “Measure 1000 times” a few times: the counts for 000 and 111 hover around 500 and no other outcome ever appears. shots=1000 in Qiskit does exactly this.
Open the circuit in your browserThe circuit playground is in Turkish. Button guide: “1000 kez ölç” = measure 1000 times, “Durum vektörünü göster” = show the state vector, “Bu devrenin Qiskit kodu” = this circuit’s Qiskit code, “Geri al” = undo, “Temizle” = clear. Click a gate in the circuit to remove it.
Key points
- The Sampler runs a circuit shots times and counts the results; StatevectorSampler simulates this ideally on your computer.
- For a circuit measured with measure_all(), the counts are result[0].data.meas.get_counts().
- Counts are random; the probability estimate is count divided by shots and improves with more shots.
- Several circuits can be sent in a single run call.
Test yourself
Where do you get the counts of a circuit measured with measure_all()?
- result[0].data.meas.get_counts()
- qc.get_counts()
- sampler.counts
Show the answer
result[0].data.meas.get_counts(). run returns one result per circuit; measure_all() writes to the “meas” register, so the counts are read with result[0].data.meas.get_counts().
← Getting started with Qiskit: write your first circuit in Python · Bit order: reading Qiskit results correctly →