Skip to content

Commit fc05585

Browse files
Fix Python 3.12 deprecation warning about os.fork() (#1070)
In Python 3.12, on Linux at least, `src/openfermion/testing/performance_benchmarks_test.py` produces the following warnings: ``` ~/.pyenv/versions/3.12.5/lib/python3.12/multiprocessing/popen_fork.py:66: RuntimeWarning: os.fork() was called. os.fork() is incompatible with multithreaded code, and JAX is multithreaded, so this will likely lead to a deadlock. self.pid = os.fork() src/openfermion/testing/performance_benchmarks_test.py::test_run_linear_qop src/openfermion/testing/performance_benchmarks_test.py::test_run_linear_qop src/openfermion/testing/performance_benchmarks_test.py::test_run_linear_qop src/openfermion/testing/performance_benchmarks_test.py::test_run_linear_qop src/openfermion/testing/performance_benchmarks_test.py::test_run_linear_qop ~/.pyenv/versions/3.12.5/lib/python3.12/multiprocessing/popen_fork.py:66: DeprecationWarning: This process (pid=3069778) is multi-threaded, use of fork() may lead to deadlocks in the child. self.pid = os.fork() ``` This can be resolved by calling `multiprocessing.set_start_method()` in `__init__` of the `ParallelLinearQuibitOperator` class. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent a221db7 commit fc05585

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

src/openfermion/linalg/linear_qubit_operator.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ def _matvec(self, x):
144144
class ParallelLinearQubitOperator(scipy.sparse.linalg.LinearOperator):
145145
"""A LinearOperator from a QubitOperator with multiple processors."""
146146

147+
_start_method_set = False
148+
147149
def __init__(self, qubit_operator, n_qubits=None, options=None):
148150
"""
149151
Args:
@@ -162,6 +164,10 @@ def __init__(self, qubit_operator, n_qubits=None, options=None):
162164
self.n_qubits = n_qubits
163165
self.options = options or LinearQubitOperatorOptions()
164166

167+
if not ParallelLinearQubitOperator._start_method_set:
168+
multiprocessing.set_start_method('forkserver', force=True)
169+
ParallelLinearQubitOperator._start_method_set = True
170+
165171
self.qubit_operator_groups = list(
166172
qubit_operator.get_operator_groups(self.options.processes)
167173
)

0 commit comments

Comments
 (0)