Skip to content

Commit

Permalink
Add prefix argument to state_to_latex (fixes #8460)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankharkins committed Aug 15, 2022
1 parent bd64399 commit 5a49b44
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
5 changes: 2 additions & 3 deletions qiskit/visualization/state_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ def mod(v):
return colors


def state_to_latex(state, dims=None, convention="ket", **args):
def state_to_latex(state, dims=None, convention="ket", prefix="", **args):
"""Return a Latex representation of a state. Wrapper function
for `qiskit.visualization.array_to_latex` for convention 'vector'.
Adds dims if necessary.
Expand All @@ -1177,10 +1177,9 @@ def state_to_latex(state, dims=None, convention="ket", **args):
else:
dims = True

prefix = ""
suffix = ""
if dims:
prefix = "\\begin{align}\n"
prefix = "\\begin{align}\n" + prefix
dims_str = state._op_shape.dims_l()
suffix = f"\\\\\n\\text{{dims={dims_str}}}\n\\end{{align}}"

Expand Down
15 changes: 15 additions & 0 deletions test/python/visualization/test_state_latex_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ def test_state_max_size(self):
)
self.assertEqual(output, expected_output)

def test_ket_prefix(self):
"""Test `prefix` parameter for latex ket notation."""

sv = Statevector.from_label("+-rl")
output = state_drawer(sv, "latex_source", prefix=r"|\psi\rangle=")
expected_output = (
"|\\psi\\rangle=\\frac{1}{4} |0000\\rangle - \\frac{i}{4} "
"|0001\\rangle +\\frac{i}{4} |0010\\rangle +\\frac{1}{4} "
"|0011\\rangle - \\frac{1}{4} |0100\\rangle +\\frac{i}{4} "
"|0101\\rangle + \\ldots +\\frac{1}{4} |1011\\rangle - "
"\\frac{1}{4} |1100\\rangle +\\frac{i}{4} |1101\\rangle - "
"\\frac{i}{4} |1110\\rangle - \\frac{1}{4} |1111\\rangle"
)
self.assertEqual(output, expected_output)

def test_state_to_latex_for_none(self):
"""
Test for `\rangleNone` output in latex representation
Expand Down

0 comments on commit 5a49b44

Please sign in to comment.