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

Fixing the tools for plotting Pauli vec #10619

Merged
merged 6 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 9 additions & 6 deletions qiskit/visualization/state_visualization.py
Original file line number Diff line number Diff line change
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)
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.