question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Statevector.draw('latex') can't handle near zero numbers

See original GitHub issue

Environment

  • Qiskit Terra version: 0.19.1
  • Python version: 3.8.10
  • Operating system: win11x64

What is happening?

Performing Statevector.draw(‘latex’) on a statevector that includes numbers which are near zero, results in bad output.

How can we reproduce the issue?

A sample CSWAP-test circuit:

qc = QuantumCircuit(3)
qc.h(0)
qc.cswap(0, 1, 2)
qc.h(0)

evolution of a sample input statevector according to the CSWAP-TEST:

sv = Statevector.from_label('--0').evolve(qc)
print(sv.data)

Notice the near zero numbers at indices 1,3,5 and 7:

[ 5.00000000e-01+0.j  3.11164266e-19+0.j -5.00000000e-01+0.j
 -3.11164266e-19+0.j -5.00000000e-01+0.j -3.11164266e-19+0.j
  5.00000000e-01+0.j  3.11164266e-19+0.j]

Performing:

sv.draw('latex')

Produces: badlatex1

What should happen?

Should produce: badlatex2

Any suggestions?

Although a vector output and not Dirac notation, note that performing:

array_to_latex(sv)

does not suffer from the same problem. Is it possible to set up a global “snap to sigma” parameter to the whole system, such that all underlying auxiliary functions can follow? (and it would be nice to change ‘latex’ to ‘dirac’… 😃)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
frankharkinscommented, Jan 11, 2022

Can you assign me please?

0reactions
DrEntropycommented, Mar 7, 2022

Oh that comment was meant to help whoever is going to patch the code, not for end user work around. My work around btw is to do this:

from qiskit.visualization import state_visualization
data2 = Statevector([ state_visualization._round_if_close(v) for v in data._data])
data2.draw('latex')

which is ugly but works 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

qiskit.quantum_info.Statevector.draw
latex : An IPython Latex object for displaying in Jupyter Notebooks. latex_source: Raw, uncompiled ASCII source to generate array using LaTeX. qsphere: ...
Read more >
qiskit - How to get full length output from statevector draw
It seems like a bug. A workaround is to use _state_to_latex_ket directly as follows: from IPython.display import Latex from qiskit.visualization ...
Read more >
backends — ProjectQ 0.8.1.dev14 documentation
Compiler engine that prints command to the standard output. projectq.backends.DeviceNotHandledError. Exception raised if a selected device cannot handle the ...
Read more >
CRAN - Package 'survival'
Arguments formula a formula object, with the response on the left of a '~' operator and the terms, separated by + operators, on...
Read more >
The animate Package
graphics vector graphics animated GIF LaTeX dvips ps2pdf dvipdfmx XeLaTeX ... A frame number <num> may as well be given; <num> is zero-based,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found