Skip to content

Commit

Permalink
Pass max_size to Statevector LaTeX drawer (Qiskit#8197)
Browse files Browse the repository at this point in the history
* Fix Qiskit#7516

* Remove unused import

* Improve splitting of string literals

Co-authored-by: Jake Lishman <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 21, 2022
1 parent 0e3e68d commit 4414c4e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion qiskit/visualization/state_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
Fixed a bug in which the latex state vector drawer ignores the `max_size`
parameter.
51 changes: 51 additions & 0 deletions test/python/visualization/test_state_latex_drawer.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 4414c4e

Please sign in to comment.