Skip to content

Commit

Permalink
Fixing the tools for plotting Pauli vec (#10619)
Browse files Browse the repository at this point in the history
* fixing the plot for pauli_vec to scaled to pm 1

* testing

* linting

* ref figure

* Update docstring to define Paulivector

* add reno

---------

Co-authored-by: Julien Gacon <gaconju@gmail.com>
  • Loading branch information
jaygambetta and Cryoris committed Aug 15, 2023
1 parent 61a3a43 commit 213580d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
15 changes: 9 additions & 6 deletions qiskit/visualization/state_visualization.py
Expand Up @@ -627,14 +627,17 @@ def plot_state_city(
def plot_state_paulivec(
state, title="", figsize=None, color=None, ax=None, *, rho=None, filename=None
):
r"""Plot the paulivec representation of a quantum state.
r"""Plot the Pauli-vector representation of a quantum state as bar graph.
Plot a bargraph of the density matrix of a quantum state using as a basis all
possible tensor products of Pauli operators and identities, that is,
:math:`\{\bigotimes_{i=0}^{N-1}P_i\}_{P_i\in \{I,X,Y,Z\}}`, where
:math:`N` is the number of qubits.
The Pauli-vector of a density matrix :math:`\rho` is defined by the expectation of each
possible tensor product of single-qubit Pauli operators (including the identity), that is
.. math ::
\rho = \frac{1}{2^n} \sum_{\sigma \in \{I, X, Y, Z\}^{\otimes n}}
\mathrm{Tr}(\sigma \rho) \sigma.
This function plots the coefficients :math:`\mathrm{Tr}(\sigma\rho)` as bar graph.
Args:
state (Statevector or DensityMatrix or ndarray): an N-qubit quantum state.
Expand Down Expand Up @@ -1574,4 +1577,4 @@ def _paulivec_data(state):
rho = SparsePauliOp.from_operator(DensityMatrix(state))
if rho.num_qubits is None:
raise VisualizationError("Input is not a multi-qubit quantum state.")
return rho.paulis.to_labels(), np.real(rho.coeffs)
return rho.paulis.to_labels(), np.real(rho.coeffs * 2**rho.num_qubits)
@@ -0,0 +1,7 @@
---
fixes:
- |
Fixed :func:`plot_state_paulivec`, which previously damped the state coefficients by a factor of
:math:`2^n`, where :math:`n` is the number of qubits. Now the bar graph correctly displays
the coefficients as :math:`\mathrm{Tr}(\sigma\rho)`, where :math:`\rho` is the state to
be plotted and :math:`\sigma` iterates over all possible tensor products of single-qubit Paulis.
55 changes: 55 additions & 0 deletions test/python/visualization/test_state_plot_tools.py
@@ -0,0 +1,55 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 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
# 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.

"""Tests for functions used in state visualization"""

import unittest
import numpy as np

from qiskit.quantum_info import Statevector
from qiskit.visualization.state_visualization import _paulivec_data
from qiskit.test import QiskitTestCase


class TestStatePlotTools(QiskitTestCase):
"""State Plotting Tools"""

def test_state_paulivec(self):
"""Test paulivec."""

sv = Statevector.from_label("+-rl")
output = _paulivec_data(sv)
labels = [
"IIII",
"IIIY",
"IIYI",
"IIYY",
"IXII",
"IXIY",
"IXYI",
"IXYY",
"XIII",
"XIIY",
"XIYI",
"XIYY",
"XXII",
"XXIY",
"XXYI",
"XXYY",
]
values = [1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1]
self.assertEqual(output[0], labels)
self.assertTrue(np.allclose(output[1], values))


if __name__ == "__main__":
unittest.main(verbosity=2)
Binary file modified test/visual/mpl/graph/references/paulivec.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 213580d

Please sign in to comment.