diff --git a/qiskit/visualization/state_visualization.py b/qiskit/visualization/state_visualization.py index 3f9d8513181..25c6b8f3d2c 100644 --- a/qiskit/visualization/state_visualization.py +++ b/qiskit/visualization/state_visualization.py @@ -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. @@ -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) diff --git a/releasenotes/notes/paulivecplot-normalization-5dd3cf3393c75afb.yaml b/releasenotes/notes/paulivecplot-normalization-5dd3cf3393c75afb.yaml new file mode 100644 index 00000000000..a22e2a3abc5 --- /dev/null +++ b/releasenotes/notes/paulivecplot-normalization-5dd3cf3393c75afb.yaml @@ -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. diff --git a/test/python/visualization/test_state_plot_tools.py b/test/python/visualization/test_state_plot_tools.py new file mode 100644 index 00000000000..02883598041 --- /dev/null +++ b/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) diff --git a/test/visual/mpl/graph/references/paulivec.png b/test/visual/mpl/graph/references/paulivec.png index 2abce570686..813a387265b 100644 Binary files a/test/visual/mpl/graph/references/paulivec.png and b/test/visual/mpl/graph/references/paulivec.png differ