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

qiskit.utils.parallel.should_run_in_parallel() function unexpected return value #13420

Open
StS-Ph opened this issue Nov 10, 2024 · 2 comments
Open
Labels
bug Something isn't working

Comments

@StS-Ph
Copy link

StS-Ph commented Nov 10, 2024

Environment

  • Qiskit version: 1.2.4.
  • Python version: 3.9.12
  • Operating system: macOS 13.6.7

What is happening?

qiskit.utils.parallel.should_run_in_parallel() function returns False if all variables are set for parallel execution.

How can we reproduce the issue?

num_processes = 2
os.environ["QISKIT_PARALLEL"] = "TRUE"
CONFIG={"parallel_enabled": True}
PARALLEL_DEFAULT=True

( num_processes > 1 and os.getenv("QISKIT_IN_PARALLEL", "FALSE") == "FALSE" and CONFIG.get("parallel_enabled", PARALLEL_DEFAULT))

What should happen?

The last line of the above code example is the return value of the qiskit.utils.parallel.should_run_in_parallel() function. Since everything is set for parallel execution, it is expected that the return value gives True but the output if the code is executed yields False.

Any suggestions?

The problem is the second expression os.getenv("QISKIT_IN_PARALLEL", "FALSE") == "FALSE". This expression is False if "QISKIT_IN_PARALLEL" is set to "TRUE". The expression should be replaced with os.getenv("QISKIT_IN_PARALLEL", "FALSE") == "TRUE".

@StS-Ph StS-Ph added the bug Something isn't working label Nov 10, 2024
@jakelishman
Copy link
Member

jakelishman commented Nov 10, 2024

I don't quite understand how you're calling should_run_in_parallel here - you say that's the return value, but that's just its code. You haven't actually shown how you're calling it.

QISKIT_IN_PARALLEL is not an environment variable users should set. That's an environment variable Qiskit uses to talk to its subprocesses - if that's "TRUE" it means the current Qiskit is in a child process of a parent that is already running in parallel, so it shouldn't attempt to parallelise further and should_run_in_parallel should return False. The os.getenv("QISKIT_IN_PARALLEL", "FALSE") == "FALSE" line you've highlighted is correct. The QISKIT_PARALLEL variable (which your example suggests is what you've set) is user facing, and you do want to set that to some string like TRUE (though it's only read once on import qiskit, so if you set that environment variable after that, it has no effect).

If you could show a complete script that returns False for that call, what all your QISKIT_* environment variables are _when Python is launched, and the output of qiskit.utils.local_hardware_info(), it will help track down your issue.

(Fwiw, should_run_in_parallel is not currently part of the public API as of Qiskit 1.2 or 1.3 - it's not documented. In #12412 we're looking at overhauling these utility functions to make them far more suitable for public consumption, and refactoring their internals to make them behave in more expected ways.)

@StS-Ph
Copy link
Author

StS-Ph commented Nov 11, 2024

I see my mistake, I misinterpreted the QISKIT_IN_PARALLEL variable. I thought this flag should be "TRUE" if processes should be run in parallel, while this is set via the QISKIT_PARALLEL variable.
The should_run_in_parallel function is called within the run method of the qiskit.passmanager.BasePassManager class, to decide if the transpilation of the given quantum circuits should run in parallel. By default this is always disabled for local runs on Windows and macOS. My aim is to enable this on my local machine, to test the functionality, that's why I was looking at the should_run_in_parallel function.

@jakelishman jakelishman modified the milestone: 1.3.0 Nov 13, 2024
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

2 participants