Skip to content

Conversation

@dmeliksetian
Copy link

Summary

This PR fixes a PermissionError: [WinError 32] that occurs on Windows systems when using pass_manager_drawer as described in issue #15472

Details and comments

The current implementation generates a temporary image file, opens it with PIL.Image.open(), and immediately attempts to delete it via os.remove().

On Linux/Unix, deleting an open file is permitted (the inode remains until the file handle is closed).

On Windows, the file system enforces a lock on open files. Because PIL.Image.open() keeps the file handle active, os.remove() fails, crashing the visualization routine.

The Fix I have updated the file handling logic to use a context manager (with statement).

The image is opened within a with block to ensure the file handle is explicitly closed after reading.

The image data is copied to memory (.copy()) so the visualization object remains valid after the file is closed.

os.remove() is called only after the context manager exits (ensuring the file is closed).

Added a try/except block around the deletion as a defensive measure against other OS-level locks (e.g., antivirus scanners).

This approach provides a robust, OS-agnostic fix without requiring conditional logic (e.g., if sys.platform == 'win32').

Related Issue

Fixes WinError 32 during using qiskit.visualization.pass_manager_visualization in qiskit #15472

How to verify

Run the following code on a Windows environment:

Python

from qiskit.transpiler import generate_preset_pass_manager
from qiskit.visualization import pass_manager_drawer

pm = generate_preset_pass_manager(optimization_level=0)

This previously crashed with WinError 32
pass_manager_drawer(pm)
Verify the image renders correctly in the notebook.

Verify no PermissionError is raised.

AI Disclosure: AI tool used: Google Gemini

@dmeliksetian dmeliksetian requested a review from a team as a code owner January 2, 2026 17:37
@qiskit-bot qiskit-bot added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Jan 2, 2026
@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:

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

None yet

Development

Successfully merging this pull request may close these issues.

3 participants