84 lines
4.3 KiB
Plaintext
84 lines
4.3 KiB
Plaintext
---
|
||
title: plot_state_qsphere
|
||
description: API reference for qiskit.visualization.plot_state_qsphere
|
||
in_page_toc_min_heading_level: 1
|
||
python_api_type: function
|
||
python_api_name: qiskit.visualization.plot_state_qsphere
|
||
---
|
||
|
||
<span id="qiskit-visualization-plot-state-qsphere" />
|
||
|
||
# qiskit.visualization.plot\_state\_qsphere
|
||
|
||
<Function id="qiskit.visualization.plot_state_qsphere" isDedicatedPage={true} github="https://github.com/qiskit/qiskit/tree/stable/0.25/qiskit/visualization/state_visualization.py" signature="qiskit.visualization.plot_state_qsphere(state, figsize=None, ax=None, show_state_labels=True, show_state_phases=False, use_degrees=False, *, rho=None, filename=None)">
|
||
Plot the qsphere representation of a quantum state. Here, the size of the points is proportional to the probability of the corresponding term in the state and the color represents the phase.
|
||
|
||
<Admonition title="Deprecated since version 0.15.1" type="danger">
|
||
`qiskit.visualization.state_visualization.plot_state_qsphere()`’s argument `rho` is deprecated as of qiskit-terra 0.15.1. It will be removed no earlier than 3 months after the release date. Instead, use the argument `state`, which behaves identically.
|
||
</Admonition>
|
||
|
||
**Parameters**
|
||
|
||
* **state** ([*Statevector*](qiskit.quantum_info.Statevector "qiskit.quantum_info.Statevector") *or*[*DensityMatrix*](qiskit.quantum_info.DensityMatrix "qiskit.quantum_info.DensityMatrix") *or ndarray*) – an N-qubit quantum state.
|
||
* **figsize** ([*tuple*](https://docs.python.org/3/library/stdtypes.html#tuple "(in Python v3.12)")) – Figure size in inches.
|
||
* **ax** ([*matplotlib.axes.Axes*](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.8.0)")) – An optional Axes object to be used for the visualization output. If none is specified a new matplotlib Figure will be created and used. Additionally, if specified there will be no returned Figure since it is redundant.
|
||
* **show\_state\_labels** ([*bool*](https://docs.python.org/3/library/functions.html#bool "(in Python v3.12)")) – An optional boolean indicating whether to show labels for each basis state.
|
||
* **show\_state\_phases** ([*bool*](https://docs.python.org/3/library/functions.html#bool "(in Python v3.12)")) – An optional boolean indicating whether to show the phase for each basis state.
|
||
* **use\_degrees** ([*bool*](https://docs.python.org/3/library/functions.html#bool "(in Python v3.12)")) – An optional boolean indicating whether to use radians or degrees for the phase values in the plot.
|
||
|
||
**Returns**
|
||
|
||
A matplotlib figure instance if the `ax` kwarg is not set
|
||
|
||
**Return type**
|
||
|
||
[`matplotlib.figure.Figure`](https://matplotlib.org/stable/api/figure_api.html#matplotlib.figure.Figure "(in Matplotlib v3.8.0)")
|
||
|
||
**Raises**
|
||
|
||
* [**MissingOptionalLibraryError**](exceptions#qiskit.exceptions.MissingOptionalLibraryError "qiskit.exceptions.MissingOptionalLibraryError") – Requires matplotlib.
|
||
* [**VisualizationError**](visualization#qiskit.visualization.VisualizationError "qiskit.visualization.VisualizationError") – if input is not a valid N-qubit state.
|
||
* [**QiskitError**](exceptions#qiskit.exceptions.QiskitError "qiskit.exceptions.QiskitError") – Input statevector does not have valid dimensions.
|
||
|
||
**Examples**
|
||
|
||
```python
|
||
from qiskit import QuantumCircuit
|
||
from qiskit.quantum_info import Statevector
|
||
from qiskit.visualization import plot_state_qsphere
|
||
|
||
qc = QuantumCircuit(2)
|
||
qc.h(0)
|
||
qc.cx(0, 1)
|
||
|
||
state = Statevector(qc)
|
||
plot_state_qsphere(state)
|
||
```
|
||
|
||

|
||
|
||
```python
|
||
# You can show the phase of each state and use
|
||
# degrees instead of radians
|
||
|
||
from qiskit.quantum_info import DensityMatrix
|
||
import numpy as np
|
||
from qiskit import QuantumCircuit
|
||
from qiskit.visualization import plot_state_qsphere
|
||
|
||
qc = QuantumCircuit(2)
|
||
qc.h([0, 1])
|
||
qc.cz(0,1)
|
||
qc.ry(np.pi/3, 0)
|
||
qc.rx(np.pi/5, 1)
|
||
qc.z(1)
|
||
|
||
matrix = DensityMatrix(qc)
|
||
plot_state_qsphere(matrix,
|
||
show_state_phases = True, use_degrees = True)
|
||
```
|
||
|
||

|
||
</Function>
|
||
|