UCL-CCS / symmer

An efficient Python-based framework for implementing qubit subspace methods, reducing the resource requirements for near-term quantum simulations.
MIT License
37 stars 9 forks source link

Unsafe multiprocessing #149

Closed AngusMingare closed 10 months ago

AngusMingare commented 11 months ago

Description

Several places in Symmer use multiprocessing in the following way:

with mp.Pool(mp.cpu_count()) as pool: 

New process are created in one of two ways:

Minimal example

Running Symmer on MacOS Monterey (Apple M1 chip), I encountered this problem trying to run the code in notebook 2.2 as a local python script after cloning the Symmer repo to my local machine.

Suggested fixes

Ensuring all experiments run using Symmer are contained in the

if __name__ == "__main__":

idiom will prevent the top-level script being executed when it is imported to the spawned process stopping the infinite loop issue. This should then be documented in Symmer.

Alternatively, it's possible to force the new process to fork by using:

with mp.get_context("fork").Pool(mp.cpu_count()) as pool: 

This also fixes the issue without requiring the above if name == main idiom. However this may be bad practice (?). It would also need to be tested on Windows OS.

AlexisRalli commented 11 months ago

This is a known issue. We are considering moving to the ray library , this should fix the problem and may give other benefits too (due to shared objs not being pickled).

AlexisRalli commented 10 months ago

Fixed in final pull requests: #151 and #153