Skip to content

Commit

Permalink
handle precision edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
frankharkins committed Aug 15, 2022
1 parent 5a49b44 commit 72e4fb7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
8 changes: 8 additions & 0 deletions qiskit/visualization/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ def num_to_latex(raw_value, precision=15, coefficient=False, first_term=True):

raw_value = np.around(raw_value, precision)
value = sympy.nsimplify(raw_value, rational=False)

if isinstance(value, sympy.core.numbers.Rational) and value.denominator > 50:
# Avoid showing ugly fractions (e.g. 50498971964399/62500000000000)
value = value.evalf() # Display as float

if isinstance(value, sympy.core.numbers.Float):
value = round(value, precision)

element = sympy.latex(value, full_prec=False)

if not coefficient:
Expand Down
1 change: 1 addition & 0 deletions qiskit/visualization/state_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,7 @@ def _state_to_latex_ket(data, max_size=12, precision=15):
data: State vector
max_size: Maximum number of non-zero terms in the expression. If the number of
non-zero terms is larger than the max_size, then the representation is truncated.
precision (int): Number of decimal places to round each amplitude to.
Returns:
String with LaTeX representation of the state vector
Expand Down
2 changes: 1 addition & 1 deletion test/python/visualization/test_state_latex_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def test_number_to_latex_terms(self):
([1 + np.sqrt(2)], ["(1 + \\sqrt{2})"]),
]
for numbers, latex_terms in cases:
terms = numbers_to_latex_terms(numbers, 15)
terms = numbers_to_latex_terms(numbers)
self.assertListEqual(terms, latex_terms)


Expand Down

0 comments on commit 72e4fb7

Please sign in to comment.