Note

This page was generated from tutorials/circuits/2_plotting_data_in_qiskit.ipynb.

Run interactively in the IBM Quantum lab.

# Qiskit Visualizations¶

```
[1]:
```

```
from qiskit import *
from qiskit.visualization import plot_histogram
from qiskit.tools.monitor import job_monitor
```

## Plot histogram¶

To visualize the data from a quantum circuit run on a real device or `qasm_simulator`

we have made a simple function

`plot_histogram(data)`

As an example we make a 2-qubit Bell state

```
[2]:
```

```
# quantum circuit to make a Bell state
bell = QuantumCircuit(2, 2)
bell.h(0)
bell.cx(0, 1)
meas = QuantumCircuit(2, 2)
meas.measure([0,1], [0,1])
# execute the quantum circuit
backend = BasicAer.get_backend('qasm_simulator') # the device to run on
circ = bell + meas
result = execute(circ, backend, shots=1000).result()
counts = result.get_counts(circ)
print(counts)
```

```
<ipython-input-1-3f698c25cdca>:11: DeprecationWarning: The QuantumCircuit.__add__() method is being deprecated.Use the compose() method which is more flexible w.r.t circuit register compatibility.
circ = bell + meas
/home/runner/work/qiskit/qiskit/.tox/docs/lib/python3.8/site-packages/qiskit/circuit/quantumcircuit.py:869: DeprecationWarning: The QuantumCircuit.combine() method is being deprecated. Use the compose() method which is more flexible w.r.t circuit register compatibility.
return self.combine(rhs)
```

```
{'11': 507, '00': 493}
```

```
[3]:
```

```
plot_histogram(counts)
```

```
[3]:
```

### Options when plotting a histogram¶

The `plot_histogram()`

has a few options to adjust the output graph. The first option is the `legend`

kwarg. This is used to provide a label for the executions. It takes a list of strings use to label each execution’s results. This is mostly useful when plotting multiple execution results in the same histogram. The `sort`

kwarg is used to adjust the order the bars in the histogram are rendered. It can be set to either ascending order with `asc`

or descending order with `desc`

. The
`number_to_keep`

kwarg takes an integer for the number of terms to show, the rest are grouped together in a single bar called rest. You can adjust the color of the bars with the `color`

kwarg which either takes a string or a list of strings for the colors to use for the bars for each execution. You can adjust whether labels are printed above the bars or not with the `bar_labels`

kwarg. The last option available is the `figsize`

kwarg which takes a tuple of the size in inches to make the
output figure.

```
[4]:
```

```
# Execute 2-qubit Bell state again
second_result = execute(circ, backend, shots=1000).result()
second_counts = second_result.get_counts(circ)
# Plot results with legend
legend = ['First execution', 'Second execution']
plot_histogram([counts, second_counts], legend=legend)
```

```
[4]:
```

```
[5]:
```

```
plot_histogram([counts, second_counts], legend=legend, sort='desc', figsize=(15,12),
color=['orange', 'black'], bar_labels=False)
```

```
[5]:
```

### Using the output from plot_histogram()¶

When using the plot_histogram() function it returns a `matplotlib.Figure`

for the rendered visualization. Jupyter notebooks understand this return type and render it for us in this tutorial, but when running outside of Jupyter you do not have this feature automatically. However, the `matplotlib.Figure`

class natively has methods to both display and save the visualization. You can call `.show()`

on the returned object from `plot_histogram()`

to open the image in a new window (assuming
your configured matplotlib backend is interactive). Or alternatively you can call `.savefig('out.png')`

to save the figure to `out.png`

. The `savefig()`

method takes a path so you can adjust the location and filename where you’re saving the output.

## Plot State¶

In many situations you want to see the state of a quantum computer. This could be for debugging. Here we assume you have this state (either from simulation or state tomography) and the goal is to visualize the quantum state. This requires exponential resources, so we advise to only view the state of small quantum systems. There are several functions for generating different types of visualization of a quantum state

```
plot_state_city(quantum_state)
plot_state_qsphere(quantum_state)
plot_state_paulivec(quantum_state)
plot_state_hinton(quantum_state)
plot_bloch_multivector(quantum_state)
```

A quantum state is either a state matrix \(\rho\) (Hermitian matrix) or statevector \(|\psi\rangle\) (complex vector). The state matrix is related to the statevector by

and is more general as it can represent mixed states (positive sum of statevectors)

The visualizations generated by the functions are:

`'plot_state_city'`

: The standard view for quantum states where the real and imaginary (imag) parts of the state matrix are plotted like a city.`'plot_state_qsphere'`

: The Qiskit unique view of a quantum state where the amplitude and phase of the state vector are plotted in a spherical ball. The amplitude is the thickness of the arrow and the phase is the color. For mixed states it will show different`'qsphere'`

for each component.`'plot_state_paulivec'`

: The representation of the state matrix using Pauli operators as the basis \(\rho=\sum_{q=0}^{d^2-1}p_jP_j/d\).`'plot_state_hinton'`

: Same as`'city'`

but where the size of the element represents the value of the matrix element.`'plot_bloch_multivector'`

: The projection of the quantum state onto the single qubit space and plotting on a bloch sphere.

```
[6]:
```

```
from qiskit.visualization import plot_state_city, plot_bloch_multivector
from qiskit.visualization import plot_state_paulivec, plot_state_hinton
from qiskit.visualization import plot_state_qsphere
```

```
[7]:
```

```
# execute the quantum circuit
backend = BasicAer.get_backend('statevector_simulator') # the device to run on
result = execute(bell, backend).result()
psi = result.get_statevector(bell)
```

```
[8]:
```

```
plot_state_city(psi)
```

```
[8]:
```

```
[9]:
```

```
plot_state_hinton(psi)
```

```
[9]:
```

```
[10]:
```

```
plot_state_qsphere(psi)
```

```
[10]:
```

```
[11]:
```

```
plot_state_paulivec(psi)
```

```
[11]:
```

```
[12]:
```

```
plot_bloch_multivector(psi)
```

```
[12]:
```

Here we see that there is no information about the quantum state in the single qubit space as all vectors are zero.

### Options when using state plotting functions¶

The various functions for plotting quantum states provide a number of options to adjust how the plots are rendered. Which options are available depends on the function being used.

**plot_state_city()** options

**title**(str): a string that represents the plot title**figsize**(tuple): figure size in inches (width, height).**color**(list): a list of len=2 giving colors for real and imaginary components of matrix elements.

```
[13]:
```

```
plot_state_city(psi, title="My City", color=['black', 'orange'])
```

```
[13]:
```

**plot_state_hinton()** options

**title**(str): a string that represents the plot title**figsize**(tuple): figure size in inches (width, height).

```
[14]:
```

```
plot_state_hinton(psi, title="My Hinton")
```

```
[14]:
```

**plot_state_paulivec()** options

**title**(str): a string that represents the plot title**figsize**(tuple): figure size in inches (width, height).**color**(list or str): color of the expectation value bars.

```
[15]:
```

```
plot_state_paulivec(psi, title="My Paulivec", color=['purple', 'orange', 'green'])
```

```
[15]:
```

**plot_state_qsphere()** options

**figsize**(tuple): figure size in inches (width, height).

**plot_bloch_multivector()** options

**title**(str): a string that represents the plot title**figsize**(tuple): figure size in inches (width, height).

```
[16]:
```

```
plot_bloch_multivector(psi, title="My Bloch Spheres")
```

```
[16]:
```

### Using the output from state plotting functions¶

When using any of the state plotting functions it returns a `matplotlib.Figure`

for the rendered visualization. Jupyter notebooks understand this return type and render it for us in this tutorial, but when running outside of Jupyter you do not have this feature automatically. However, the `matplotlib.Figure`

class natively has methods to both display and save the visualization. You can call `.show()`

on the returned object to open the image in a new window (assuming your configured
matplotlib backend is interactive). Or alternatively you can call `.savefig('out.png')`

to save the figure to `out.png`

in the current working directory. The `savefig()`

method takes a path so you can adjust the location and filename where you’re saving the output.

## Plot Bloch Vector¶

A standard way of plotting a quantum system is using the Bloch vector. This only works for a single qubit and takes as input the Bloch vector.

The Bloch vector is defined as \([x = \mathrm{Tr}[X \rho], y = \mathrm{Tr}[Y \rho], z = \mathrm{Tr}[Z \rho]]\), where \(X\), \(Y\), and \(Z\) are the Pauli operators for a single qubit and \(\rho\) is the state matrix.

```
[17]:
```

```
from qiskit.visualization import plot_bloch_vector
```

```
[18]:
```

```
plot_bloch_vector([0,1,0])
```

```
[18]:
```

### Options for plot_bloch_vector()¶

**title**(str): a string that represents the plot title**figsize**(tuple): Figure size in inches (width, height).

```
[19]:
```

```
plot_bloch_vector([0,1,0], title='My Bloch Sphere')
```

```
[19]:
```

### Adjusting the output from plot_bloch_vector()¶

When using the `plot_bloch_vector`

function it returns a `matplotlib.Figure`

for the rendered visualization. Jupyter notebooks understand this return type and render it for us in this tutorial, but when running outside of Jupyter you do not have this feature automatically. However, the `matplotlib.Figure`

class natively has methods to both display and save the visualization. You can call `.show()`

on the returned object to open the image in a new window (assuming your configured
matplotlib backend is interactive). Or alternatively you can call `.savefig('out.png')`

to save the figure to `out.png`

in the current working directory. The `savefig()`

method takes a path so you can adjust the location and filename where you’re saving the output.

```
[20]:
```

```
import qiskit.tools.jupyter
%qiskit_version_table
%qiskit_copyright
```

### Version Information

Qiskit Software | Version |
---|---|

Qiskit | 0.26.0 |

Terra | 0.17.3 |

Aer | 0.8.2 |

Ignis | 0.6.0 |

Aqua | 0.9.1 |

IBM Q Provider | 0.13.1 |

System information | |

Python | 3.8.10 (default, May 4 2021, 07:16:51) [GCC 9.3.0] |

OS | Linux |

CPUs | 2 |

Memory (Gb) | 6.791339874267578 |

Mon May 17 19:00:52 2021 UTC |

### This code is a part of Qiskit

© Copyright IBM 2017, 2021.

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

of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.

Any modifications or derivative works of this code must retain this

copyright notice, and modified files need to carry a notice indicating

that they have been altered from the originals.

```
[ ]:
```

```
```