Level 6 · Practitioner’s Corner

Your First Qiskit Circuit

About 5,418 words · roughly 25 minute read · sources checked December 2, 2026

Reading about qubits is one thing. Writing a circuit, running it and getting numbers back is how the ideas become real. This lesson takes you through a complete first program in Qiskit, the Python toolkit from IBM, one step at a time: build a two-qubit circuit that creates an entangled Bell state, run it, read the results and check that they make sense.

Every output quoted here was computed independently with a small matrix simulation that I wrote, because the exact counts depend on randomness and on the version. Where the lesson says what Qiskit does, it is based on IBM’s current documentation pages that I opened, and the lesson says so when something is from memory or was not checked. The aim is that when you run the code yourself, you know what to expect and can tell whether something went wrong.

You need the gates and entanglement lessons first. You do not need any previous programming experience beyond reading simple Python.

What you will learn

  • How to describe a circuit in Qiskit and what each line does.
  • How to run it with the Sampler primitive and read the counts.
  • What shot noise is, and how many shots to use.
  • Why Qiskit prints bitstrings backwards, and how to read them.
  • How to get exact expectation values with the Estimator.
  • How to sweep a parameter and compare with the exact curve.
  • Which older Qiskit code no longer works, so tutorials do not trip you up.

Helpful first: Quantum Gates, Entanglement, What Is a Qubit?.

What we will build

The program has four parts: describe a circuit, run it many times, collect how often each result appears, and compare with what theory predicts. The circuit is the Bell-state circuit from the entanglement lesson. A Hadamard gate on the first qubit puts it into an equal superposition, and a CNOT gate then copies that choice onto the second qubit in the way that creates entanglement. The final state is (|00⟩ + |11⟩)/√2.

Measured in the computational basis, the two qubits should always agree. About half of the runs give 00 and half give 11, and the results 01 and 10 should never occur on a perfect simulator. That is a sharp, checkable prediction, which is why it is a good first test.

q0q1cHgatecontrol • targetmeasuretime runs left to right
A Bell circuit with its two measurements. Horizontal lines are qubits, boxes are gates, and the double line at the bottom is the classical register that stores the results.

Why start here

A Bell circuit is small enough to predict completely by hand, and the two outcomes 00 and 11 test two things at once: the superposition from the Hadamard and the correlation from the CNOT.

Setting up

Qiskit is a Python library. You install it with Python’s package manager, in a virtual environment if you can, so that it does not interfere with other projects.

python -m venv qenv
source qenv/bin/activate        # on Windows: qenv\Scripts\activate
pip install qiskit
Installing Qiskit. The commands are standard Python practice; I did not run them here.

Two further packages are common. The package qiskit-aer is a separate, faster simulator; the Qiskit 1.0 notes say it is installed separately, with pip install qiskit-aer, and imported as qiskit_aer. The package qiskit-ibm-runtime is for real IBM hardware and is used in a later lesson. For this lesson you need only qiskit itself, because it includes simple exact simulators that are enough for small circuits.

Version warning

Qiskit changes between major versions. The 1.0 release removed the old execute function, and the 2.0 release removed the first-generation primitives, the old BackendV1 model, and the pulse module. Many online tutorials were written before that, so if a tutorial’s code fails with an import error, check its date first.

Step 1: build the circuit

A circuit is an object of the class QuantumCircuit. You tell it how many qubits it has, then add gates in the order you want them applied.

from qiskit import QuantumCircuit

qc = QuantumCircuit(2)   # two qubits, both start in |0>
qc.h(0)                  # Hadamard on qubit 0
qc.cx(0, 1)              # CNOT: control = qubit 0, target = qubit 1
qc.measure_all()         # add a measurement of every qubit

print(qc.draw())
The complete Bell circuit.

Line by line:

  • QuantumCircuit(2) creates a circuit with two qubits. All qubits start in |0⟩, which is Qiskit’s convention.
  • qc.h(0) applies a Hadamard gate to qubit 0. Counting starts from zero.
  • qc.cx(0, 1) applies a controlled-NOT. The arguments are (control, target), as stated on the bit-ordering page. It flips qubit 1 if qubit 0 is 1.
  • qc.measure_all() adds measurement operations on every qubit, together with a new classical register to hold the results. The IBM primitive documentation shows that a circuit built this way gives a register named meas, which is what we will read results from.
  • qc.draw() prints a text picture of the circuit.

Notice that the circuit is only a description at this point. Nothing has run. Building a circuit costs nothing, and the same circuit can be run on a simulator or on a real device.

Worked example

What the circuit does to the state, by hand

  1. Start with |00⟩ = |0⟩|0⟩.
  2. After the Hadamard on qubit 0: (|0⟩ + |1⟩)/√2 for qubit 0, and |0⟩ for qubit 1. In Qiskit’s ordering, with qubit 0 on the right, the two terms are |00⟩ and |01⟩.
  3. After the CNOT, which flips qubit 1 only when qubit 0 is 1: (|00⟩ + |11⟩)/√2. The term |01⟩ becomes |11⟩.
  4. So the measurement should give 00 or 11, each with probability 1/2. I checked this by simulation: the probabilities were 0.5 for 00 and 0.5 for 11, and nothing else.

Step 2: run it with the Sampler

Qiskit runs circuits through objects called primitives. The Sampler runs a circuit many times and reports the measured bitstrings. The Estimator computes an expectation value, which we meet later. The simulator-based versions that come with Qiskit are called StatevectorSampler and StatevectorEstimator, and they compute exact quantum probabilities on your own computer.

from qiskit.primitives import StatevectorSampler

sampler = StatevectorSampler()
job = sampler.run([(qc, None, 1024)])   # one PUB: (circuit, parameter values, shots)
result = job.result()
counts = result[0].data.meas.get_counts()
print(counts)
Running the circuit 1,024 times. This follows the example on IBM’s primitive input and output page.

The work unit passed to run is a list of tuples. IBM calls each tuple a PUB, a primitive unified bloc. For the Sampler it has the form (circuit, parameter values, shots). The circuit must include measurements, and the parameter values are needed only if the circuit has unbound parameters, so we pass None. The result holds one entry per PUB. Its data attribute has one field for each classical register, and our register is named meas, so result[0].data.meas holds our shots. The method get_counts() turns them into a dictionary from bitstrings to counts.

Worked example

What output to expect

  1. The counts should look like {'00': 5xx, '11': 4xx}, two keys that sum to 1,024. Neither 01 nor 10 should appear, because a noiseless simulation of this circuit gives them probability exactly zero.
  2. The exact numbers differ from run to run, since each shot is a random sample. Do not worry if you see 489 and 535 instead of 512 and 512.
  3. If you do see 01 or 10 on this simulator, something is wrong with the circuit, most likely a missing CNOT or the wrong qubit order.

How many shots, and why the numbers wobble

A shot is one run of the circuit followed by one measurement of all the qubits. A quantum computer’s answer to a question about probabilities is built up from many shots, so the estimate has a statistical error. For a result with true probability 1/2, the number of times it appears in N shots has a spread (standard deviation) of √(N × 1/2 × 1/2) = √N / 2.

Shots NStandard deviation of countStandard deviation of fractionChance fraction lies within 0.5 ± 0.02
1005.00.0500.38
1,02416.00.0160.80
4,09632.00.00780.99
10,00050.00.0050about 1.0

Statistical spread for a fair two-outcome result, computed from the binomial distribution. The last column is the probability that the measured fraction is within two percentage points of 0.5.

Two practical rules follow. First, the error shrinks as 1/√N, so cutting the error in half costs four times as many shots. Second, a 1,024-shot run of a fair result is within two points of 50% only about 80% of the time, so a count of 540 out of 1,024 (52.7%) is not evidence that anything is wrong.

Worked example

How many shots to tell 50% from 55%?

  1. The difference is 0.05. The fraction’s spread at N shots is about 0.5/√N. To separate the two values by three spreads we need 3 × 0.5/√N ≤ 0.05, so √N ≥ 30 and N ≥ 900.
  2. With only 100 shots, the spread is 0.05, equal to the difference you are trying to detect, so the two cases cannot be told apart reliably.
  3. On real hardware the shots also carry noise from the device, which is a separate error from the sampling error computed here.

Reading the bitstrings: Qiskit’s bit order

For a Bell state the bit order does not matter, since 00 and 11 read the same either way. As soon as a circuit has a result such as 01, the order matters, and Qiskit’s convention surprises almost everyone. IBM’s documentation says that Qiskit follows the little-endian convention: qubit 0 is the least significant bit. In a printed bitstring the leftmost character is the highest-numbered qubit and the rightmost character is qubit 0.

circuit: X on qubit 1q0q1q2Xprinted key0q21q10q0leftmost = highest qubitrightmost = qubit 0Key '010'. Integer value = q0·1 + q1·2 + q2·4 = 2.
Qiskit’s convention. Flipping qubit 1 of a three-qubit register gives the key 010, with qubit 1 in the middle, and the integer value is 2.

Worked example

Checking with simulations

  1. Flip qubit 1 only, in a two-qubit circuit with X on qubit 1 and measure_all. Result: the bitstring '10'. This is the same example the documentation gives. The leftmost character is qubit 1 (value 1) and the rightmost is qubit 0 (value 0), so the integer is 2.
  2. Apply a Hadamard to qubit 0 alone. The results are '00' and '01' with probability 0.5 each, because qubit 0 is the rightmost character.
  3. Apply a Hadamard to qubit 1 alone, instead, and the results are '00' and '10'.
  4. A CNOT with control 0 and target 1 after a Hadamard on 0 gives the Bell state, so '00' and '11'. A CNOT with control 1 and target 0 after the same Hadamard on qubit 0 does not entangle anything: qubit 1 is still |0⟩, so the control never fires, and the results are '00' and '01'. I computed both.

Drawing versus ordering

By default the drawing puts qubit 0 at the top. The option draw(reverse_bits=True) puts qubit 0 at the bottom. The documentation notes that this only changes the picture, not the circuit. Do not use it expecting to change which qubit a gate acts on.

A warning from the same page: ordering in the primitives’ outputs is a separate matter. The documentation says the BitArray class defaults to big-endian, while the pages on IBM’s Sampler and Estimator mention different conventions. In practice get_counts() on a measured register has given the familiar little-endian strings in the documentation examples, but if you ever process raw bit arrays yourself, check the order with a test circuit like the X on qubit 1 above. That one-line test settles the question.

Seeing the exact state instead of sampling

A simulator can show things a real device cannot: the exact state. The class Statevector in qiskit.quantum_info does this. You can create one from a circuit without measurements and print its amplitudes or probabilities. The page on bit ordering shows Statevector.from_label, with the label '0+' meaning qubit 1 in |0⟩ and qubit 0 in |+⟩, whose probabilities are 0.5 for '00' and 0.5 for '01'. I reproduced that calculation.

This is a debugging tool. If sampling gives a strange result, check the exact probabilities first. If the exact state is already wrong, the problem is in the circuit. If the exact state is right and the samples are only noisy, the problem is statistics. On real hardware you do not have this choice, which is why simulators come first.

Worked example

A three-qubit entangled state

  1. Extend the circuit: H on qubit 0, then CNOT(0, 1), then CNOT(1, 2).
  2. The state is (|000⟩ + |111⟩)/√2, the GHZ state from the entanglement lesson. I computed its probabilities: 0.5 for '000' and 0.5 for '111'.
  3. This is a good first exercise. Predict the result before you run it, then check that only two bitstrings appear.

Under the hood: the circuit is a matrix

It helps to know what Qiskit does with your gates. For n qubits, the state is a list of 2ⁿ amplitudes, and each gate is a matrix that multiplies that list. A circuit is the product of its gate matrices, and the first gate you add is the rightmost factor, because it acts on the state first.

Worked example

The Bell circuit as one 4 × 4 matrix

  1. With Qiskit’s ordering, the amplitude list is indexed by the bitstring q1q0: positions 00, 01, 10, 11. The Hadamard on qubit 0 is I ⊗ H, with the identity on the left for qubit 1. The CNOT with control 0 and target 1 swaps the amplitudes at positions 01 and 11.
  2. Multiplying, the whole circuit is U = CNOT × (I ⊗ H). Scaled by √2, its entries are the rows (1, 1, 0, 0), (0, 0, 1, −1), (0, 0, 1, 1) and (1, −1, 0, 0). I computed this and checked that U is unitary, meaning U times its transpose is the identity.
  3. The first column is the result of acting on |00⟩: (1, 0, 0, 1)/√2. That is the Bell state (|00⟩ + |11⟩)/√2, as predicted.
  4. The other three columns are the other three Bell states up to signs and ordering, so the same circuit turns the four basis states into the four Bell states.

Order matters, which is why circuit diagrams are read left to right while the matrix product is written right to left. Apply H and then Z to |0⟩ and you get (1, −1)/√2, which is |−⟩. Apply Z and then H and you get (1, 1)/√2, which is |+⟩. The two circuits use the same gates and produce different states, and I confirmed both results numerically. In Qiskit, the order in which you write the method calls is the order of the gates.

Why this view is worth having

When a circuit gives a surprising result, writing out the matrices for two or three qubits is a reliable way to find out whether the circuit is wrong or your expectation is wrong. It is also why simulators become slow: a circuit on 30 qubits needs a list of 2³⁰, about a billion, amplitudes, as the simulation lesson explained.

Is my result consistent with theory?

Sooner or later you will see counts like 489 and 535 out of 1,024 and wonder whether they agree with a fair 50/50 prediction. A standard check is the chi-square statistic, which adds up, for every outcome, (observed − expected)² / expected. Larger values mean a worse fit. For two outcomes, a value above 3.84 is unlikely (below a 5% chance) if the prediction is right.

Observed (00, 11)χ² against 512/512Chance of a value this large if the prediction is correct
489, 5352.070.15 (fine)
540, 4843.060.08 (borderline, not alarming)
560, 4649.00.003 (suspicious)
600, 42430.3below 0.0001 (something is wrong)

Chi-square check for a fair two-outcome result at 1,024 shots, with one degree of freedom. My computation.

On a perfect simulator, a Bell circuit should pass this check about 95% of the time, and a failed check usually means a circuit bug. On real hardware, small failures are normal because the device has noise, which a later lesson covers. A chi-square check will not tell you why a result is off, only that it is off, which is still useful before you spend time debugging.

Three exercises

Try these, predicting the result first. The predictions below are computed, so you can compare.

Worked example

Exercise 1: a uniform superposition

  1. Apply H to each of three qubits and measure. All eight bitstrings 000 to 111 should appear with probability 1/8.
  2. With 1,024 shots, the expected count of each is 128, with a spread of √(1,024 × 1/8 × 7/8) = 10.6. So counts between about 107 and 149 are normal.
  3. If one bitstring never appears, one of the Hadamards is missing.

Worked example

Exercise 2: a four-qubit GHZ state

  1. Apply H to qubit 0 and then CNOT(0, 1), CNOT(1, 2) and CNOT(2, 3). By the same logic as the three-qubit case, only '0000' and '1111' should appear, each with probability 1/2.
  2. Now put CNOT(2, 3) first. It acts when qubit 2 is still 0, so it does nothing, and qubit 3 never receives the choice. Predict the counts, then check: the keys should be '0000' and '0111', because qubit 3 (the leftmost character) stays 0 while qubits 0, 1 and 2 form a three-qubit GHZ state.

Worked example

Exercise 3: build the other three Bell states

  1. Put an X gate on qubit 0 before the Hadamard. By hand: X gives |01⟩, the Hadamard gives (|00⟩ − |01⟩)/√2, and the CNOT turns |01⟩ into |11⟩, so the result is (|00⟩ − |11⟩)/√2. A Z gate on qubit 0 after the Hadamard gives the same state. Predict the counts: the minus sign is invisible in computational-basis counts, so you still see only '00' and '11', but the Estimator’s ⟨XX⟩ changes from +1 to −1 while ⟨ZZ⟩ stays +1. I checked those signs by simulation.
  2. This is a good illustration that counts alone do not show phases, a point the common-mistakes lesson returns to.

Seeing a phase: Hadamard, rotate, Hadamard

Counts only show probabilities, and probabilities do not show phase directly. But a phase can be made visible by interference: put the phase between two Hadamard gates. The first Hadamard creates a superposition, the rotation shifts the relative phase of the two parts, and the second Hadamard makes them interfere, converting the phase into a probability.

from qiskit import QuantumCircuit
from qiskit.circuit import Parameter

phi = Parameter('phi')
qc = QuantumCircuit(1)
qc.h(0)
qc.rz(phi, 0)    # rotation about Z by angle phi
qc.h(0)
qc.measure_all()
An interferometer on one qubit, the same structure as the Mach–Zehnder circuit in the superposition lesson.
φP(measure 1) = sin²(φ/2)
00
π/40.146
π/20.5
π1
3π/20.5

Exact result for the H, Rz(φ), H circuit, computed with matrices. It matches the formula sin²(φ/2) at every point.

Worked example

A fixed phase as a special case

  1. The T gate adds a phase of π/4 to |1⟩. Using T in place of Rz(φ) gives P(1) = sin²(π/8) = 0.146, matching the table’s π/4 row. The reason is that T and Rz(π/4) differ only by a global phase, which cannot be measured.
  2. At φ = 0 the two Hadamards cancel (H is its own inverse) and the qubit returns to |0⟩, so P(1) = 0. At φ = π the rotation flips the relative sign and the two Hadamards turn |+⟩ into |−⟩ and then |1⟩, so P(1) = 1.
  3. This is the smallest example of what quantum algorithms do: arrange for the wanted answers’ amplitudes to add and the others’ to cancel.

Compare this to the Bell exercise where a Z gate changed a sign without changing the counts. The difference is that here the sign is followed by a second basis-changing gate. A phase is real and has consequences, but you must design the circuit so that something converts it into a measurable difference.

Putting it together: a complete script

Here is the whole first program in one place. It builds the Bell circuit twice, once with measurements for the Sampler and once without for the Estimator, and prints both results. The comments say what to expect.

from qiskit import QuantumCircuit
from qiskit.primitives import StatevectorSampler, StatevectorEstimator

# 1. circuit without measurement (for the Estimator and for inspection)
bell = QuantumCircuit(2)
bell.h(0)
bell.cx(0, 1)

# 2. copy with measurement (for the Sampler)
measured = bell.copy()
measured.measure_all()

# 3. sampling: expect only '00' and '11', about half each
counts = StatevectorSampler().run([(measured, None, 1024)]).result()[0].data.meas.get_counts()
print('counts:', counts)

# 4. expectation values: ZZ should be 1 (exact)
evs = StatevectorEstimator().run([(bell, 'ZZ', None)]).result()[0].data.evs
print('ZZ:', evs)
A complete first program. Expected: counts with only '00' and '11' summing to 1,024, and ZZ equal to 1. I could not run this code, but each expected output was computed separately.

Keeping a measured and an unmeasured copy of the circuit is a good habit, because the two primitives need different things. A short checklist before you run any circuit:

  1. Predict the result on paper, including the bitstring order.
  2. Run on an exact simulator and compare.
  3. If the counts disagree, look at the exact statevector for the unmeasured circuit.
  4. Only after the simulator agrees, think about hardware.

The last lesson of this level, from simulator to real hardware, picks up exactly here and shows what changes when the same circuit runs on a real device.

From counts to an estimate with error bars

The Estimator gives expectation values directly, but you can also compute them yourself from Sampler counts, which is worth doing once to see how the numbers connect. For two qubits measured in the Z basis, ⟨ZZ⟩ is the fraction of shots in which the bits agree minus the fraction in which they differ.

Worked example

A worked estimate (made-up counts)

  1. Suppose a run of 1,024 shots gave 00: 497, 11: 491, 01: 19 and 10: 17. This is an invented illustration of a noisy result, since a perfect simulator would give no 01 or 10.
  2. Agreeing shots: 497 + 491 = 988. Disagreeing: 19 + 17 = 36. So ⟨ZZ⟩ = (988 − 36)/1,024 = 0.930.
  3. The statistical uncertainty of such an average of ±1 values is √((1 − ⟨ZZ⟩²)/N) = √((1 − 0.864)/1,024) = 0.0115. So the estimate is 0.930 ± 0.012.
  4. For a single qubit, ⟨Z⟩ on qubit 0 uses the last character of each key: 497 + 17 = 514 shots with qubit 0 equal to 0, and 491 + 19 = 510 with qubit 0 equal to 1. So ⟨Z⟩ = (514 − 510)/1,024 = 0.004, with an uncertainty near 1/√1,024 = 0.031. That is consistent with zero, as expected for one half of an entangled pair.
  5. The pair of results is the signature of entanglement in miniature: a strongly correlated pair, ⟨ZZ⟩ close to 1, with each member looking random, ⟨Z⟩ close to 0.

Reporting an uncertainty alongside every estimate is a habit worth forming from the first program. A value such as 0.930 on its own invites over-reading, while 0.930 ± 0.012 says how much to trust the last digit.

Things to try next

  • Change the Bell circuit to a three-qubit GHZ state and compute ⟨ZZ⟩ between qubits 0 and 2 from the counts. Predict 1 first.
  • Add an X gate on qubit 0 after the CNOT and predict the new counts and ⟨ZZ⟩ (the sign of ⟨ZZ⟩ flips to −1).
  • Run the Ry sweep at five more angles and plot P(1) against θ next to the curve sin²(θ/2).
  • Run the interference circuit (H, Rz, H) with Rz angles from 0 to 2π and see the oscillation in P(1).
  • Write the five-test suite from the framework lesson in your own file, and keep it.

Step 3: expectation values with the Estimator

Counting bitstrings is one way to extract information. Often what you want is the average value of an observable, such as the correlation between two qubits. The Estimator computes this. Instead of sampling and counting, you pass the circuit without measurements and an observable written as a Pauli string.

from qiskit import QuantumCircuit
from qiskit.primitives import StatevectorEstimator

bell = QuantumCircuit(2)
bell.h(0)
bell.cx(0, 1)              # no measurements for the Estimator

estimator = StatevectorEstimator()
job = estimator.run([(bell, 'ZZ', None)])   # (circuit, observable, parameter values)
print(job.result()[0].data.evs)             # expectation value
Following the Estimator example on IBM’s page. The circuit has no measurement gates.

The result is an expectation value, written in the output as evs, together with a standard deviation stds. For the exact simulator the standard deviation is zero because nothing is sampled. For the Bell state Φ⁺ the three Pauli correlations are known: ⟨ZZ⟩ = +1, ⟨XX⟩ = +1 and ⟨YY⟩ = −1. I computed ⟨ZZ⟩ numerically and got 1.0 to machine precision, and I computed the single-qubit ⟨Z⟩ on either qubit and got 0, which is the signature of an entangled pair: each qubit alone looks random while the pair is perfectly correlated.

Worked example

Sampler and Estimator say the same thing

  1. The Sampler’s counts give P(00) = P(11) = 1/2, P(01) = P(10) = 0.
  2. The expectation value ⟨ZZ⟩ is the probability that the two bits agree minus the probability that they disagree, which is (0.5 + 0.5) − (0 + 0) = 1. It matches the Estimator.
  3. The single-qubit ⟨Z⟩ is P(0) − P(1) = 0.5 − 0.5 = 0 for each qubit.
  4. Why bother with two tools? To get ⟨XX⟩ from counts you would have to add basis-change gates before measuring. The Estimator does that for you, and on real hardware it can also apply error-mitigation techniques, which the hardware lesson covers.

A check on Pauli label order

Pauli strings in Qiskit, such as 'ZI', also list qubits with the highest number on the left. I believe this matches the bit-ordering convention, but I did not find it stated on the pages I opened. If you use a label such as 'ZI' or 'IZ', test it on a state where the two qubits differ, for example X on qubit 1, which has qubit 1 at 1 and qubit 0 at 0: the label that gives −1 is the one acting on qubit 1.

Step 4: sweep a parameter

Circuits can include gate angles that you fill in later. Qiskit calls them Parameters. The primitives accept a list of values to try, which is how the algorithms that tune a circuit (such as the variational ones in the algorithms lessons) are run. Here is the smallest useful example, a single qubit rotated by an angle θ about the Y axis.

from qiskit import QuantumCircuit
from qiskit.circuit import Parameter
from qiskit.primitives import StatevectorSampler
import numpy as np

theta = Parameter('theta')
qc = QuantumCircuit(1)
qc.ry(theta, 0)
qc.measure_all()

angles = np.linspace(0, np.pi, 5).reshape(-1, 1)   # 5 values of theta
sampler = StatevectorSampler()
res = sampler.run([(qc, angles, 1024)]).result()[0]
print(res.data.meas.get_counts())     # counts for each theta, see docs for indexing
A parameter sweep. The shape (5, 1) lets the five angles be broadcast over the single parameter, in the NumPy style that the primitives page describes.

The theory is simple. Ry(θ) takes |0⟩ to cos(θ/2)|0⟩ + sin(θ/2)|1⟩, so the probability of measuring 1 is sin²(θ/2) and the expectation ⟨Z⟩ is cos θ. The table below is what an exact computation gives.

θP(1) = sin²(θ/2)⟨Z⟩ = cos θExpected count of ‘1’ in 1,024 shots
0010
π/40.1460.707about 150
π/20.50about 512
3π/40.854−0.707about 874
π1−11,024

Exact curve for a single-qubit Ry rotation. Real counts scatter around the last column with a spread of up to 16.

Worked example

Reading a sweep

  1. At θ = π/4, the exact probability is sin²(π/8) = 0.1464. The expected count is 0.1464 × 1,024 = 150, and the spread is √(1,024 × 0.1464 × 0.8536) = 11.3. So the observed count should be between about 127 and 173 in 95% of runs.
  2. If your count is 200, the sweep is not behaving as theory says. Check the angle units: Qiskit angles are in radians, and entering 45 instead of π/4 is a common mistake.
  3. At θ = 0 and π the counts are exactly 0 and 1,024 on a simulator, because those probabilities are 0 and 1.

Why older tutorials fail

Search results for Qiskit contain much code written for versions that no longer exist. These changes come from IBM’s migration pages that I opened.

Old codeWhat happenedUse instead
execute(qc, backend)The Qiskit 1.0 notes say qiskit.execute is not availableRun through a primitive such as the Sampler, or transpile and then backend.run()
from qiskit import Aerqiskit.Aer is unavailable in 1.0pip install qiskit-aer, then import from qiskit_aer
from qiskit import BasicAerRemoved in 1.0BasicProvider from qiskit.providers.basic_provider, or the Statevector and Operator classes
from qiskit.tools.jupyter import *The qiskit.tools module was removed in 1.0No replacement for the monitor widgets
Version 1 primitives (Sampler, Estimator from older releases)V1 primitives removed in 2.0The V2 classes such as StatevectorSampler and StatevectorEstimator
BackendV1 and fake backends based on itBackendV1 removed in 2.0BackendV2, and GenericBackendV2 for simulated devices
qc.c_if(...)c_if removed in 2.0if_test control-flow blocks

Removed features and their replacements, from the Qiskit 1.0 and 2.0 documentation pages I opened.

The practical habit: put the Qiskit version in your notes (print qiskit.__version__), read the date on any tutorial, and when an import fails, search the migration notes, not only the error message.

Check your circuits against the browser simulator

A good habit is to verify every Qiskit circuit you write against an independent simulator. This site has one that runs in your browser, so you can compare your expected results with a second tool and catch mistakes such as reversed bit order. The widgets below match the circuits you build in this lesson. As always, predict, then run. Results follow Qiskit's bit order, with qubit 0 as the rightmost bit.

One Hadamard

Run this circuit

One Hadamard: a fair quantum coin

q0H

Bit order: q0 is the rightmost digit of each result, as in Qiskit. All qubits start in |0⟩ unless marked. This is an ideal simulator with no hardware noise.

Predict first: Starting from |0⟩, one H gate. What fractions of 0 and 1 do you expect over many shots?

Press Run to simulate. Results appear here with a short explanation.

Two Hadamards in a row

Run this circuit

H then H: interference undoes the superposition

q0HH

Bit order: q0 is the rightmost digit of each result, as in Qiskit. All qubits start in |0⟩ unless marked. This is an ideal simulator with no hardware noise.

Predict first: Two Hadamards in a row. If H made the qubit truly random, a second H should keep it random. Does it?

Press Run to simulate. Results appear here with a short explanation.

The Bell pair

Run this circuit

Bell pair: H then CNOT

q0q1H

Bit order: q0 is the rightmost digit of each result, as in Qiskit. All qubits start in |0⟩ unless marked. This is an ideal simulator with no hardware noise.

Predict first: H puts qubit 0 in superposition; CNOT copies its value onto qubit 1 as an XOR. Which of 00, 01, 10, 11 can appear?

Press Run to simulate. Results appear here with a short explanation.

If your Qiskit circuit for any of these gives counts that disagree with the widget, look first at the order of gates, then at the register size, then at whether you measured all qubits. The most common cause of a disagreement is a measurement that was left out, which makes Qiskit return nothing to count.

Why a second simulator helps

A bug that looks like a quantum mystery is often a plain programming mistake. Comparing against a tool you did not write is the quickest way to separate the two.

A short checklist for the first circuit

  • The circuit has the right number of qubits and classical bits.
  • Every gate acts on the intended qubit index.
  • Measurements are added at the end, not in the middle, unless you mean it.
  • You read the counts in Qiskit's order, with qubit 0 on the right.
  • The number of shots is large enough that the fractions are stable: about 1,000 gives roughly a 1.6 percentage point standard deviation on a 50/50 outcome.

When all five boxes are ticked and the browser agrees with your output, you have a trustworthy first circuit, and everything else in this course is a variation on the same pattern.

Mistakes in a first program

SymptomLikely causeHow to check
Result has 01 and 10 on a noiseless run of a Bell circuitCNOT missing, or control and target reversedPrint the circuit and compare with the Bell diagram
Counts show only '00'Forgot the Hadamard, or ran a circuit with no gatesPrint the circuit
Error: no measurementsThe Sampler needs measurements in the circuitAdd measure_all()
Estimator gives odd values for a circuit with measurementsObservables should be applied to the state before measurementRemove measurements for the Estimator
Bitstring looks reversedLittle-endian orderingTest with a single X gate
Counts differ between runsShot noiseCompare the spread with √N/2
Angle sweep looks wrongDegrees instead of radiansUse np.pi

A first-aid table.

Practice problems

Check yourself

After H on qubit 0 and CNOT(0, 1), what are the possible counts keys and their probabilities?

'00' and '11', each with probability 1/2.

You run X on qubit 2 of a three-qubit circuit and measure all. What is the counts key?

'100'. Qubit 2 is the leftmost character.

A circuit applies H to qubit 1 only on two qubits. What counts keys appear?

'00' and '10'.

How big is the spread in the number of ‘00’ results out of 4,096 Bell-circuit shots?

√(4,096 × 0.25) = 32.

What is ⟨ZZ⟩ for the state (|01⟩ + |10⟩)/√2?

The two bits always disagree, so ⟨ZZ⟩ = −1.

Why does the Estimator need a circuit without measurements?

It computes the expectation value of an observable from the state itself, and measuring first would collapse the state and apply its own basis choice.

For Ry(θ) on |0⟩, what θ gives P(1) = 0.25?

sin²(θ/2) = 0.25, so θ/2 = π/6 and θ = π/3.

Why might an old tutorial’s import from qiskit fail?

The function or module was removed, for example execute or Aer in 1.0, or the V1 primitives in 2.0.

How would you check whether you misread a bitstring?

Run a circuit with a single X on qubit 1 and see which character is 1.

How many shots are about enough to separate P = 0.5 from P = 0.55 at three standard deviations?

About 900.

References and further reading

I opened IBM’s Qiskit documentation pages on bit ordering, on Sampler and Estimator inputs and outputs, on the Qiskit 1.0 feature changes and on the Qiskit 2.0 release notes. The code samples that call Sampler and Estimator follow the example on the primitive input and output page. Qiskit itself was not installed in my working environment, so I could not run any of this code. Instead, every expected output was computed with my own NumPy simulation of the same gates, using Qiskit’s documented bit ordering. Run the code on your machine and compare. Package versions change, and I did not check which Qiskit version you will have installed.

  1. Bit ordering (IBM Quantum documentation) Opened. Little-endian convention, qubit 0 rightmost, the X-on-qubit-1 example giving '10', Statevector.from_label, draw(reverse_bits=True), (control, target) ordering.
  2. Primitive input and output (IBM Quantum documentation) Opened. PUB format for Sampler and Estimator, the result structure, the Bell-circuit Sampler and Estimator examples with StatevectorSampler and StatevectorEstimator.
  3. Qiskit v1.0 feature changes (IBM Quantum documentation) Opened. Removal of execute, the Aer and BasicAer imports, and qiskit.tools.
  4. Qiskit SDK 2.0 release notes (IBM Quantum documentation) Opened (first part). Removal of c_if, BackendV1, V1 primitives, pulse, and BackendV1-based fake backends.