Code exampleBeginnerQiskit labLesson 7

Visualization: Statevector and Histogram

Show the statevector as text, print probabilities, and save a histogram of counts to an image file without a GUI.

Visualisation helps connect the mathematics to the results. Before measurement, the full state can be inspected with Statevector.from_instruction(qc); sv.draw("text") prints the complex amplitudes in the order $|00\rangle,|01\rangle,|10\rangle,|11\rangle$ (binary index $q_1q_0$). For the Bell state, amplitudes of $0.7071$ appear on $|00\rangle$ and $|11\rangle$ and zero elsewhere.

This inspection is possible only in simulation; on real hardware amplitudes cannot be read, so probabilities are estimated from samples or the state is reconstructed by Tomography at considerable cost.

After measurement we get counts, and plot_histogram draws a histogram (it requires matplotlib). To avoid needing a graphical window, we choose the Agg backend and save the figure to a PNG file with fig.savefig. qiskit.visualization provides other tools such as plot_bloch_multivector and plot_state_city, all of which return matplotlib figures that can be saved the same way. You can also use sv.draw("latex_source") to output LaTeX, but it requires the sympy package.

Remember that the histogram shows random samples: the two ratios near $0.5$ do not equal it exactly, and their deviation is of order $\sqrt{0.25/500}\approx0.022$. So when presenting results in a report or lesson, it is good practice to state the number of shots and the seed used, and to plot the theoretical values beside the measured frequencies whenever possible, so the reader can tell statistical fluctuation from a real error.

Requirements: pip install qiskit qiskit-aer matplotlib. The code runs on your machine or in Colab, not inside WordPress.

Code

Qiskit
import matplotlib
matplotlib.use("Agg")
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector
from qiskit.primitives import StatevectorSampler
from qiskit.visualization import plot_histogram

qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)

sv = Statevector.from_instruction(qc)
print(sv.draw("text"))
print({str(k): round(float(v), 4) for k, v in sv.probabilities_dict().items()})

qc.measure_all()
counts = StatevectorSampler(seed=3).run([qc], shots=500).result()[0].data.meas.get_counts()
print(counts)
fig = plot_histogram(counts, title="Bell state")
fig.savefig("bell_histogram.png")
print("saved bell_histogram.png")

Line-by-line code explanation

LineExplanation
1-2Select matplotlib's non-graphical Agg backend before any other plotting import.
3-6Import the circuit, Statevector, the primitive and the histogram plotting function.
8-10A Bell circuit without measurement.
12Compute the exact state vector.
13Print the amplitudes as text.
14Print the rounded probabilities.
16Add measurement to all qubits.
17Take 500 samples with seed 3 and extract the counts.
18Print the counts.
19Draw the histogram and return a matplotlib figure.
20Save the figure to an image file.
21Confirm the save.

Expected output (from an actual run)

[0.70710678+0.j,0.        +0.j,0.        +0.j,0.70710678+0.j]
{'00': 0.5, '11': 0.5}
{'00': 246, '11': 254}
saved bell_histogram.png

Counts from StatevectorSampler(seed=3); the file bell_histogram.png is created in the working directory.

Try it yourself

Related topics

References

  1. IBM Quantum (2025). Qiskit SDK API documentation. IBM Quantum Documentation quantum.cloud.ibm.com
  2. IBM Quantum (2025). IBM Quantum Documentation – Guides. IBM Quantum Documentation quantum.cloud.ibm.com