Skip to Content
Qiskit Pocket Guide
book

Qiskit Pocket Guide

by James L. Weaver, Frank J. Harkins
June 2022
Intermediate to advanced content levelIntermediate to advanced
218 pages
4h 4m
English
O'Reilly Media, Inc.
Content preview from Qiskit Pocket Guide

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

The Design of Everyday Things

The Design of Everyday Things

Donald A. Norman
The Kaggle Book

The Kaggle Book

Konrad Banachewicz, Luca Massaron
Storytelling with You

Storytelling with You

Cole Nussbaumer Knaflic
The Goal

The Goal

Eliyahu M. Goldratt, Jeff Cox

Publisher Resources

ISBN: 9781098112462Errata PageSupplemental Content