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

Pass max_size to Statevector LaTeX drawer #8197

Merged
merged 5 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
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)