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

Optimize Matplotlib Circuit Drawing: Auto-Close Figures to Prevent Memory Leak #13822

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

miles0428
Copy link

Summary

Description
When drawing quantum circuits using Matplotlib (circ.draw(output='mpl')), the figures are not automatically closed after being created. This leads to a memory leak and results in the following warning when creating many circuits:

RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (matplotlib.pyplot.figure) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam figure.max_open_warning). Consider using matplotlib.pyplot.close().
mpl_figure = plt.figure()

Steps to Reproduce

  1. Create and draw a quantum circuit in a loop:
from qiskit.circuit.random import random_circuit
for i in range(101):
    circ = random_circuit(2, 2, max_operands=2)
    circ.draw(output='mpl')
  1. Run the code via python xxx.py.

Expected Behaviour

  • Figures should be automatically closed after being drawn to avoid excessive memory usage.
  • No warnings should be raised about too many open figures.

Details and comments

To solve this problem, we could either add a generator autodeleter to fix this problem

miles0428 and others added 2 commits February 11, 2025 03:07
### **Potential Memory Issue When Drawing a Circuit**

**Description**  
When drawing quantum circuits using Matplotlib (`circ.draw(output='mpl')`), the figures are not automatically closed after being created. This leads to a memory leak and results in the following warning when creating many circuits:

```
RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (matplotlib.pyplot.figure) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam figure.max_open_warning). Consider using matplotlib.pyplot.close().
mpl_figure = plt.figure()
```

**Steps to Reproduce**  
1. Create and draw a quantum circuit in a loop:
   ```python
   from qiskit import QuantumCircuit

   for i in range(100):
       circ = QuantumCircuit(2)
       circ.h(0)
       circ.cx(0, 1)
       circ.draw(output='mpl')
   ```

2. Run the code via `python xxx.py`.

**Expected Behavior**  
- Figures should be automatically closed after being drawn to avoid excessive memory usage.
- No warnings should be raised about too many open figures.

**Environment**  
- Qiskit Version: 1.3.1  
- Python Version: 3.10.15  
- Running via `python xxx.py`

**Suggested Fix**  
The issue arises because the figure object’s **lifetime** is not being properly managed. Matplotlib figures remain open in memory until explicitly closed, which leads to excessive memory consumption when many figures are drawn.

To solve this problem, we could either add a generator **autodeleter** to fix this problem
@miles0428 miles0428 requested review from nonhermitian and a team as code owners February 10, 2025 22:55
@qiskit-bot qiskit-bot added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Feb 10, 2025
@qiskit-bot
Copy link
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

One or more of the following people are relevant to this code:

@CLAassistant
Copy link

CLAassistant commented Feb 10, 2025

CLA assistant check
All committers have signed the CLA.

@jakelishman
Copy link
Member

It's very common to want to have more than one figure open at once. Even in your example for the functionality, it's necessary to have more than one figure open at once in order to even see the intermediate circuits drawn.

Matplotlib gives you the tools to manage figures itself (e.g. pyplot.close("all")) - it's not clear to me that this is something that should be added to Qiskit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community PR PRs from contributors that are not 'members' of the Qiskit repo
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

4 participants