Skip to content

Commit 213580d

Browse files
jaygambettaCryoris
andauthored
Fixing the tools for plotting Pauli vec (Qiskit#10619)
* 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>
1 parent 61a3a43 commit 213580d

4 files changed

Lines changed: 71 additions & 6 deletions

File tree

qiskit/visualization/state_visualization.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -627,14 +627,17 @@ def plot_state_city(
627627
def plot_state_paulivec(
628628
state, title="", figsize=None, color=None, ax=None, *, rho=None, filename=None
629629
):
630-
r"""Plot the paulivec representation of a quantum state.
630+
r"""Plot the Pauli-vector representation of a quantum state as bar graph.
631631
632-
Plot a bargraph of the density matrix of a quantum state using as a basis all
633-
possible tensor products of Pauli operators and identities, that is,
634-
:math:`\{\bigotimes_{i=0}^{N-1}P_i\}_{P_i\in \{I,X,Y,Z\}}`, where
635-
:math:`N` is the number of qubits.
632+
The Pauli-vector of a density matrix :math:`\rho` is defined by the expectation of each
633+
possible tensor product of single-qubit Pauli operators (including the identity), that is
636634
635+
.. math ::
637636
637+
\rho = \frac{1}{2^n} \sum_{\sigma \in \{I, X, Y, Z\}^{\otimes n}}
638+
\mathrm{Tr}(\sigma \rho) \sigma.
639+
640+
This function plots the coefficients :math:`\mathrm{Tr}(\sigma\rho)` as bar graph.
638641
639642
Args:
640643
state (Statevector or DensityMatrix or ndarray): an N-qubit quantum state.
@@ -1574,4 +1577,4 @@ def _paulivec_data(state):
15741577
rho = SparsePauliOp.from_operator(DensityMatrix(state))
15751578
if rho.num_qubits is None:
15761579
raise VisualizationError("Input is not a multi-qubit quantum state.")
1577-
return rho.paulis.to_labels(), np.real(rho.coeffs)
1580+
return rho.paulis.to_labels(), np.real(rho.coeffs * 2**rho.num_qubits)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
fixes:
3+
- |
4+
Fixed :func:`plot_state_paulivec`, which previously damped the state coefficients by a factor of
5+
:math:`2^n`, where :math:`n` is the number of qubits. Now the bar graph correctly displays
6+
the coefficients as :math:`\mathrm{Tr}(\sigma\rho)`, where :math:`\rho` is the state to
7+
be plotted and :math:`\sigma` iterates over all possible tensor products of single-qubit Paulis.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# This code is part of Qiskit.
2+
#
3+
# (C) Copyright IBM 2022.
4+
#
5+
# This code is licensed under the Apache License, Version 2.0. You may
6+
# obtain a copy of this license in the LICENSE.txt file in the root directory
7+
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
8+
#
9+
# Any modifications or derivative works of this code must retain this
10+
# copyright notice, and modified files need to carry a notice indicating
11+
# that they have been altered from the originals.
12+
13+
"""Tests for functions used in state visualization"""
14+
15+
import unittest
16+
import numpy as np
17+
18+
from qiskit.quantum_info import Statevector
19+
from qiskit.visualization.state_visualization import _paulivec_data
20+
from qiskit.test import QiskitTestCase
21+
22+
23+
class TestStatePlotTools(QiskitTestCase):
24+
"""State Plotting Tools"""
25+
26+
def test_state_paulivec(self):
27+
"""Test paulivec."""
28+
29+
sv = Statevector.from_label("+-rl")
30+
output = _paulivec_data(sv)
31+
labels = [
32+
"IIII",
33+
"IIIY",
34+
"IIYI",
35+
"IIYY",
36+
"IXII",
37+
"IXIY",
38+
"IXYI",
39+
"IXYY",
40+
"XIII",
41+
"XIIY",
42+
"XIYI",
43+
"XIYY",
44+
"XXII",
45+
"XXIY",
46+
"XXYI",
47+
"XXYY",
48+
]
49+
values = [1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1]
50+
self.assertEqual(output[0], labels)
51+
self.assertTrue(np.allclose(output[1], values))
52+
53+
54+
if __name__ == "__main__":
55+
unittest.main(verbosity=2)
-435 Bytes
Loading

0 commit comments

Comments
 (0)