Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add common usage explanations and code examples to qiskit.visualization module API page #8569

Merged
merged 27 commits into from
Sep 8, 2022
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d1bb2c1
add draft
HuangJunye Aug 17, 2022
366534a
revert unintended changes
HuangJunye Aug 18, 2022
ac0a996
revert unintended changes
HuangJunye Aug 18, 2022
fe1918c
add example usage
HuangJunye Aug 24, 2022
178abd7
Merge remote-tracking branch 'upstream/main' into add-common-usage-ex…
HuangJunye Aug 24, 2022
34c832a
add common keyword arguments section
HuangJunye Aug 24, 2022
a8202fd
add sections to apis
HuangJunye Aug 31, 2022
b65d46a
fix internal links
HuangJunye Aug 31, 2022
479a5f2
remove generated hist figure
HuangJunye Aug 31, 2022
52ed1c4
Merge branch 'main' of github.com:Qiskit/qiskit-terra into add-common…
1ucian0 Sep 2, 2022
b17bfbf
lint
1ucian0 Sep 2, 2022
46fa3e3
use matplotlib instead of Matplotlib in class reference
HuangJunye Sep 6, 2022
11e09e0
change to a valid denisty matrix in examples
HuangJunye Sep 6, 2022
e9b96d9
import from qiskit.visualization instead of qiskit.tools.visualization
HuangJunye Sep 7, 2022
7b1e00e
remove unintended changes
HuangJunye Sep 7, 2022
257d9a4
split counts and state visualizations
HuangJunye Sep 7, 2022
1c1c14a
remove overview and prerequisite headings
HuangJunye Sep 7, 2022
2df1bca
move common kwargs sections to the top level
HuangJunye Sep 7, 2022
23957d7
remove link to api table
HuangJunye Sep 7, 2022
d98e867
remove apis headings
HuangJunye Sep 7, 2022
9d6f500
remove extra blank lines
HuangJunye Sep 7, 2022
2987132
remove extra blank lines
HuangJunye Sep 7, 2022
af88a05
Merge branch 'main' into add-common-usage-example-to-qiskit-visualiza…
HuangJunye Sep 7, 2022
b96d97b
minor twig on the counts to make them 1000 shots in total
HuangJunye Sep 8, 2022
8778f4b
add an intro sentence before the example for common kwargs
HuangJunye Sep 8, 2022
b47ec79
Merge branch 'main' into add-common-usage-example-to-qiskit-visualiza…
HuangJunye Sep 8, 2022
97331ac
Merge branch 'main' into add-common-usage-example-to-qiskit-visualiza…
mergify[bot] Sep 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 127 additions & 3 deletions qiskit/visualization/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2017, 2018.
# (C) Copyright IBM 2017, 2022.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand All @@ -17,20 +17,144 @@

.. currentmodule:: qiskit.visualization

Counts and State Visualizations
===============================
The visualization module contain functions that visualizes measurement outcome counts, quantum
states, circuits, pulses, devices and more.

To use visualization functions, you are required to install visualization optionals to your
development environment:

.. code-block:: bash

pip install 'qiskit[visualization]'

Common Keyword Arguments
========================

Many of the figures created by visualization functions in this module are created by `Matplotlib
<https://matplotlib.org/>`_ and accept a subset of the following common arguments. Consult the
individual documentation for exact details.

* ``title`` (``str``): a text string to use for the plot title.
* ``legend`` (``list``): a list of strings to use for labels of the data.
* ``figsize`` (``tuple``): figure size in inches .
* ``color`` (``list``): a list of strings for plotting.
* ``ax`` (`matplotlib.axes.Axes <https://matplotlib.org/stable/api/axes_api.html>`_): An optional
``Axes`` object to be used for the visualization output. If none is specified a new
`matplotlib.figure.Figure <https://matplotlib.org/stable/api/figure_api.html>`_ will be created
and used. Additionally, if specified there will be no returned ``Figure`` since it is redundant.
* ``filename`` (``str``): file path to save image to.

The following example demonstrates the common usage of these arguments:

.. jupyter-execute::

from qiskit.visualization import plot_histogram

counts1 = {'00': 499, '11': 501}
counts2 = {'00': 511, '11': 489}

data = [counts1, counts2]
plot_histogram(data)

You can specify ``legend``, ``title``, ``figsize`` and ``color`` by passing to the kwargs.

.. jupyter-execute::

legend = ['First execution', 'Second execution']
title = 'New histogram'
figsize = (10,10)
color=['crimson','midnightblue']
plot_histogram(data, legend=legend, title=title, figsize=figsize, color=color)

You can save the figure to file either by passing the file name to ``filename`` kwarg or use
`matplotlib.figure.Figure.savefig
<https://matplotlib.org/stable/api/figure_api.html#matplotlib.figure.Figure.savefig>`_ method.

.. jupyter-execute::

plot_histogram(data, filename='new_hist.png')

hist = plot_histogram(data)
hist.savefig('new_hist.png')

Counts Visualizations
=====================

This section contains functions that visualize measurement outcome counts.

.. autosummary::
:toctree: ../stubs/

plot_histogram

Example Usage
-------------

Here is an example of using :func:`plot_histogram` to visualize measurement outcome counts:

.. jupyter-execute::

from qiskit.visualization import plot_histogram

counts = {"00": 501, "11": 499}
plot_histogram(counts)

The data can be a dictionary with bit string as key and counts as value, or more commonly a
:class:`~qiskit.result.Counts` object obtained from :meth:`~qiskit.result.Result.get_counts`.

HuangJunye marked this conversation as resolved.
Show resolved Hide resolved
State Visualizations
====================

This section contains functions that visualize quantum states.

.. autosummary::
:toctree: ../stubs/

plot_bloch_vector
plot_bloch_multivector
plot_state_city
plot_state_hinton
plot_state_paulivec
plot_state_qsphere

Example Usage
-------------

Here is an example of using :func:`plot_state_city` to visualize a quantum state:

.. jupyter-execute::

from qiskit.visualization import plot_state_city

state = [[ 0.75 , 0.433j],
[-0.433j, 0.25 ]]
plot_state_city(state)

The state can be array-like list of lists, ``numpy.array``, or more commonly
:class:`~qiskit.quantum_info.Statevector` or :class:`~qiskit.quantum_info.DensityMatrix` objects
obtained from a :class:`~qiskit.circuit.QuantumCircuit`:

.. jupyter-execute::

from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector, DensityMatrix

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

# plot using a Statevector
state = Statevector(qc)
plot_state_city(state)

.. jupyter-execute::

# plot using a DensityMatrix
state = DensityMatrix(qc)
plot_state_city(state)

You can find code examples for each visualization functions on the individual function API page.

Device Visualizations
=====================

Expand Down