Chapter 3. Visualizing Quantum Measurements and States

In addition to drawing circuits (see “Drawing a quantum circuit”), Qiskit provides visualizations for data such as measurement counts and quantum states.

Visualizing Measurement Counts

To visualize experiments that result in measurement counts, Qiskit contains the plot_histogram() function.

Using the plot_histogram Function

The plot_histogram() function takes a dictionary containing measurement counts and plots them in a bar graph with one bar per basis state. We’ll demonstrate this function in Example 3-1 by plotting the measurement counts from the example in “Using the AerSimulator to hold measurement results”.

Example 3-1. Using the plot_histogram() function to plot measurement counts
from qiskit import QuantumCircuit,Aer,transpile
from qiskit.visualization import plot_histogram

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

backend = Aer.get_backend("aer_simulator")
tqc = transpile(qc, backend)
job = backend.run(tqc, shots=1000)
result = job.result()
counts = result.get_counts(tqc)

plot_histogram(counts)

Figure 3-1 shows the counts expressed as probabilities in a bar graph.

Figure 3-1. Example bar graph using the plot_histogram() function

Table 3-1 contains a list of commonly used plot_histogram parameters. This is implemented with matplotlib, which uses parameters shown in the table, such as figsize

Get Qiskit Pocket Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.