Skip to content

Free-threaded Python

CPython 3.14 supports a build with no global interpreter lock. On it Python threads run at the same time on different cores, so one process can run several simulations in parallel without starting a process per run.

TensorKeeper supports it. This page covers installing the right wheel, the one thing you currently have to do to keep the GIL off, and the rule your code has to follow.

Installing

Free-threaded interpreters carry a t suffix, python3.14t, and are a separate installation from python3.14 with their own packages.

python3.14t -m pip install mimiq-tensorkeeper --index-url https://mimiq:<your-token>@license.qperfect.io/pypi/simple

There is nothing to select. A dedicated free-threaded wheel is published, and the installer picks it by tag:

Interpreter Wheel
python3.11 … python3.14 cp311-abi3, one wheel for every version
python3.14t cp314-cp314t, Linux x86_64: glibc 2.35 or newer, and glibc 2.34 for RHEL 9

It exists separately because a free-threaded interpreter has no stable ABI, so the wheel serves exactly one Python version. It carries the same engines as the ordinary Linux wheel: OpenBLAS, MKL, faer and CUDA. The two cannot be mixed up: python3.14t does not accept an abi3 wheel, and reports no matching distribution rather than installing one that would crash.

Free-threaded wheels are built for Linux x86_64 only; ask us if you need macOS or Windows.

Keeping the GIL off: PYTHON_GIL=0

A free-threaded interpreter turns the GIL back on, for the whole process, when it imports an extension module that has not declared itself safe without it, and warns when it does:

RuntimeWarning: The global interpreter lock (GIL) has been enabled to load
module 'symengine.lib.symengine_wrapper', which has not declared that it can
run safely without the GIL.

TensorKeeper's engines declare it, so importing tensorkeeper never causes this. symengine does not yet, and mimiqcircuits imports it to build gate matrices, so import mimiqcircuits re-enables the GIL for the life of the process. Until symengine declares support, override that at startup:

PYTHON_GIL=0 python3.14t my_simulation.py
# or
python3.14t -X gil=0 my_simulation.py

and check it took before trusting a timing:

import sys
import mimiqcircuits as mc
import tensorkeeper as tk

print(sys._is_gil_enabled())   # must be False

PYTHON_GIL=0 overrides every module's declaration in the process, which is why it is opt-in. Keep symbolic work out of your threads: build and parametrize circuits before starting them.

One simulator per thread

A state must be driven by one thread at a time. States hold no shared data, so threads that each own theirs never interact. Two threads driving the same state is refused rather than silently corrupting it:

RuntimeError: Already borrowed

Give each thread its own simulator, which builds its own state:

import threading
import tensorkeeper as tk

def run(seed, results, index):
    sim = tk.TkSimulator(bonddim=256)
    results[index] = sim.execute(circuit, nsamples=1000, seed=seed)

results = [None] * 8
threads = [threading.Thread(target=run, args=(s, results, i)) for i, s in enumerate(range(8))]
for t in threads:
    t.start()
for t in threads:
    t.join()

Sharing the Circuit, as above, is fine: execute only reads it.

What threading buys

TensorKeeper holds the GIL for every call into its engine, so on an ordinary python3.14 threads take turns and running eight of them is no faster than running them one after another. On python3.14t with the GIL off they run concurrently.

Each simulation also calls into a multi-threaded BLAS, so n Python threads on c cores can ask for far more threads than there are cores. Cap each simulator with TkSimulator(max_num_threads=c // n), or set MIMIQ_NUM_THREADS for the process. The cap is process-wide: simulators running at the same time share it, and each runs at the smallest one held.