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

Circuit draw does not close rendered image which appears in the next plot #13878

Open
ironfrown opened this issue Feb 19, 2025 · 13 comments
Open
Labels
bug Something isn't working

Comments

@ironfrown
Copy link

Environment

  • Qiskit version: 1.3.2
  • Python version: 3.11.11
  • Operating system: Ubuntu 22.04.5 LTS

What is happening?

When drawing a circuit followed by chart plotting, the image of the circuit also appears, see below.

Image

How can we reproduce the issue?

Create a circuit, draw it, then plot a matplotlib chart (with %matplotlib inline), used jupyterlab 4.3.5.

What should happen?

Circuit draw should close its rendered image

Any suggestions?

No response

@ironfrown ironfrown added the bug Something isn't working label Feb 19, 2025
@BramDo
Copy link

BramDo commented Feb 19, 2025

@ironfrown Could you give a minimal working example, maybe this will show the way to look for the root problem. There are other visual errors as well for example: Problem with graph in some API docs #13558.

@ironfrown
Copy link
Author

ironfrown commented Feb 20, 2025

@BramDo here is a tiny example, which shows that the error shows up only when "draw" output is "mpl" and only in the plot immediately executed after the "draw". See the notebook (stashed away in a weird repo): here (please ignore some params in functions as these are leftovers from elsewhere)

@ironfrown
Copy link
Author

By the way, the bug shows in both Chrome and Firefox, both tested in Jupyter Lab 4.3.5

@BramDo
Copy link

BramDo commented Feb 20, 2025

I could not replicate the problem with xx_qiskit_draw_bug_test.ipynb in Chrome, however I tested with Jupyterlab 4.1.5 and VScode.

@ironfrown
Copy link
Author

Tomorrow, I'll try a minimum install to eliminate possible package interaction and let you know.

@jakelishman
Copy link
Member

@ironfrown: could you run

import matplotlib

matplotlib.get_backend()

and share the result? We have code that attempts to auto-close the figure if we detect that you're using %matplotlib inline to prevent this happening, but we might be a bit out of date with newer matplotlibs or something.

@ironfrown
Copy link
Author

@BramDo @jakelishman I think I have found the source of the problem, I walked through my installs. The problem appears when you install "spyder" into the environment (using conda).

By the way:

matplotlib.get_backend()
'qtagg'

Which seems to have changed after installing "spyder" (from 'tkagg').

@ironfrown
Copy link
Author

However, when checking the environment in Jupyter lab, it is "inline" (as requested)

@ironfrown
Copy link
Author

I am not sure if this is helpful, however, when I set "%matplotlib tkagg", then the first chart plot opens up in "Figure 1", the next circuit draw opens in "Figure 2", however, the subsequent chart plot opens in the same "Figure 2" and includes both.

@ironfrown
Copy link
Author

A temporary solution is not to install "spyder" in the Qiskit environment (once installed, uninstalling it does nothing). The same may be applicable to other packages relying on QT rather than TK graphical environment.

@jakelishman
Copy link
Member

Yeah, matplotlib has an order of backends, and qtagg is preferred to tkagg if it's available. It's only available if some version of Qt is installed. Probably, if you no longer need spyder, then uninstalling the Python package pyqt5 will get you back on the tkagg backend. You can also configure the backend manually: https://matplotlib.org/stable/users/explain/figure/backends.html, though there might be some differences when running in Jupyter.

I don't immediately know of the best way to detect an inline backend (where we should take care to close the figures automatically to avoid the double-show) over a non-inline one (where we shouldn't, or you'll never get to see it at all) - our previous heuristic seemed to work ok til now. If anybody knows the best way, a PR is welcome to fix it.

@ironfrown
Copy link
Author

@jakelishman so is this "bug" resolved (by avoiding spyder) or someone is going to look at closing figures after circuit draw?

@jakelishman
Copy link
Member

There's still a problem with our heuristic for auto-closing figures, but I can't promise that I'll have time to look deeply into it any time soon. "Don't use spyder" is a workaround more than a fix haha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants