From 4414c4eca6290d8f9a3dc28b7eea65a4fd5d8153 Mon Sep 17 00:00:00 2001 From: Frank Harkins Date: Tue, 21 Jun 2022 17:17:04 +0100 Subject: [PATCH] Pass `max_size` to Statevector LaTeX drawer (#8197) * Fix #7516 * Remove unused import * Improve splitting of string literals Co-authored-by: Jake Lishman Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- qiskit/visualization/state_visualization.py | 2 +- ...x-latex-ket-max-size-f11c3a89215a49e7.yaml | 5 ++ .../visualization/test_state_latex_drawer.py | 51 +++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/fix-latex-ket-max-size-f11c3a89215a49e7.yaml create mode 100644 test/python/visualization/test_state_latex_drawer.py diff --git a/qiskit/visualization/state_visualization.py b/qiskit/visualization/state_visualization.py index c68f84859dce..00273d10302d 100644 --- a/qiskit/visualization/state_visualization.py +++ b/qiskit/visualization/state_visualization.py @@ -1130,7 +1130,7 @@ def state_to_latex( # this means the operator shape should hve no input dimensions and all output dimensions equal to 2 is_qubit_statevector = len(operator_shape.dims_r()) == 0 and set(operator_shape.dims_l()) == {2} if convention == "ket" and is_qubit_statevector: - latex_str = _state_to_latex_ket(state._data) + latex_str = _state_to_latex_ket(state._data, **args) else: latex_str = array_to_latex(state._data, source=True, **args) return prefix + latex_str + suffix diff --git a/releasenotes/notes/fix-latex-ket-max-size-f11c3a89215a49e7.yaml b/releasenotes/notes/fix-latex-ket-max-size-f11c3a89215a49e7.yaml new file mode 100644 index 000000000000..5c45cc07309e --- /dev/null +++ b/releasenotes/notes/fix-latex-ket-max-size-f11c3a89215a49e7.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + Fixed a bug in which the latex state vector drawer ignores the `max_size` + parameter. diff --git a/test/python/visualization/test_state_latex_drawer.py b/test/python/visualization/test_state_latex_drawer.py new file mode 100644 index 000000000000..fa4e7a8b7808 --- /dev/null +++ b/test/python/visualization/test_state_latex_drawer.py @@ -0,0 +1,51 @@ +# 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 visualization of latex state and unitary drawers""" + +import unittest + +from qiskit.quantum_info import Statevector +from qiskit.visualization.state_visualization import state_drawer +from .visualization import QiskitVisualizationTestCase + + +class TestLatexStateDrawer(QiskitVisualizationTestCase): + """Qiskit state and unitary latex drawer.""" + + def test_state(self): + """Test latex state vector drawer works with default settings.""" + + sv = Statevector.from_label("+-rl") + output = state_drawer(sv, "latex_source") + expected_output = ( + r"\frac{1}{4} |0000\rangle- \frac{i}{4} |0001\rangle+\frac{i}{4} |0010\rangle" + r"+\frac{1}{4} |0011\rangle- \frac{1}{4} |0100\rangle+\frac{i}{4} |0101\rangle" + r" + \ldots +\frac{1}{4} |1011\rangle- \frac{1}{4} |1100\rangle" + r"+\frac{i}{4} |1101\rangle- \frac{i}{4} |1110\rangle- \frac{1}{4} |1111\rangle" + ) + self.assertEqual(output, expected_output) + + def test_state_max_size(self): + """Test `max_size` parameter for latex ket notation.""" + + sv = Statevector.from_label("+-rl") + output = state_drawer(sv, "latex_source", max_size=4) + expected_output = ( + r"\frac{1}{4} |0000\rangle- \frac{i}{4} |0001\rangle" + r" + \ldots - \frac{1}{4} |1111\rangle" + ) + self.assertEqual(output, expected_output) + + +if __name__ == "__main__": + unittest.main(verbosity=2)