Skip to content

ParallelStrategy

Bases: Serializable

Bundles program-level chunk dispatch together with optional nested shot-level parallelism, reused identically by every loqs.tools call site that parallelizes "one QuantumProgram per chunk item" work (edesign circuits, error-injected programs, noise-sweep points -- each of which builds and runs exactly one QuantumProgram).

Parameters:

Name Type Description Default
program_executor SubmitExecutor | MapArrayExecutor | ExecutorSpec | Callable | None

How chunks are dispatched; None runs them serially in the driver process. A MapArrayExecutor (e.g. submitit.Executor) gets one bulk .map_array() call and a SubmitExecutor (e.g. loky.get_reusable_executor(), MPIPoolExecutor) one .submit() per chunk; MapArrayExecutor is checked first, since submitit's executors satisfy both.

An ExecutorSpec or a zero-argument factory is called once, on first dispatch, and cached. A spec lets differently sized strategies share loky's process-wide singleton safely, and ExecutorSpec.submitit or ExecutorSpec.mpi4py is how to checkpoint a submitit or MPI run. A factory is the fallback for backends LoQS doesn't know. It is stored as source, so define it at module level in a .py file (in __main__ or a notebook, keep its settings in its body); lambdas and functools.partial can't be stored.

None
n_program_chunks int | None

Number of round-robin chunks to split work into. Required when program_executor is, or builds, a MapArrayExecutor, since one array task per item would be dominated by scheduling overhead (checked at construction, or at first dispatch for a factory). Defaults to one chunk per item for a plain SubmitExecutor; ignored when program_executor is None.

None
n_shot_batches int | None

Number of batches to split shots into when shot_executor is set, governing shot-level parallel dispatch chunking independent of whether checkpointing is on. A batch count, not a size (e.g. n_shot_batches=5 with num_shots=100 produces 5 batches of 20 shots each). None (default) means "auto": max(1, ceil(num_shots / 20)) batches whenever shot_executor is set, resolved by QuantumProgram.run() itself once num_shots is known. Also usable in serial mode (no shot_executor) to control checkpoint-flush batch size instead, via the same count -- see QuantumProgram.run's own n_shot_batches/checkpoint_batch_size resolution for the exact behavior in each mode.

None
shot_executor SubmitExecutor | Callable[[], SubmitExecutor] | None

Nested shot-level parallelism, forwarded as QuantumProgram.run(shot_executor=...) for every program built inside a chunk. None means no shot-level parallelism. When program_executor is None, a live executor is used as-is (no process boundary is crossed). When program_executor is also given, a live executor can't cross the process boundary each dispatched chunk crosses -- a recognized backend (currently just loky) is transparently replaced with an ExecutorSpec built from its own construction parameters, which builds an equivalent fresh executor inside each chunk worker; an unrecognized backend instead requires an ExecutorSpec (such as ExecutorSpec.mpi4py) or an explicit zero-argument factory callable, which is used as given.

None
collect_resource_stats bool

Whether dispatch() should have each dispatched chunk self-report its own worker process's peak memory / mean CPU utilization (see _ResourceSampler). Primarily set (and later read back via pop_resource_stats()) by profile_strategies() itself, not meant to be hand-set for an ordinary (non-profiling) run.

False
resource_sample_interval float

Sampling interval (seconds) used when collect_resource_stats is True.

0.2

ParallelStrategy

ParallelStrategy(program_executor: SubmitExecutor | MapArrayExecutor | ExecutorSpec | Callable[[], SubmitExecutor | MapArrayExecutor] | None = None, n_program_chunks: int | None = None, n_shot_batches: int | None = None, shot_executor: SubmitExecutor | Callable[[], SubmitExecutor] | None = None, collect_resource_stats: bool = False, resource_sample_interval: float = 0.2, _resource_stats: list[ChunkResourceStats] = list(), _resolved_program_executor: SubmitExecutor | MapArrayExecutor | None = None) -> None

Attributes

Name Type Value Doc
collect_resource_stats bool False
is_chunked bool read-only property Whether program_executor is set, i.e. whether chunks are
n_program_chunks int | None None
n_shot_batches int | None None
program_executor SubmitExecutor | MapArrayExecutor | ExecutorSpec | Callable[[], SubmitExecutor | MapArrayExecutor] | None None
resource_sample_interval float 0.2
shot_executor SubmitExecutor | Callable[[], SubmitExecutor] | None None

decode

Inherited static method from Serializable.decode().


Name Type Description Default
program_executor SubmitExecutor | MapArrayExecutor | ExecutorSpec | Callable | None

How chunks are dispatched; None runs them serially in the driver process. A MapArrayExecutor (e.g. submitit.Executor) gets one bulk .map_array() call and a SubmitExecutor (e.g. loky.get_reusable_executor(), MPIPoolExecutor) one .submit() per chunk; MapArrayExecutor is checked first, since submitit's executors satisfy both.

An ExecutorSpec or a zero-argument factory is called once, on first dispatch, and cached. A spec lets differently sized strategies share loky's process-wide singleton safely, and ExecutorSpec.submitit or ExecutorSpec.mpi4py is how to checkpoint a submitit or MPI run. A factory is the fallback for backends LoQS doesn't know. It is stored as source, so define it at module level in a .py file (in __main__ or a notebook, keep its settings in its body); lambdas and functools.partial can't be stored.

None
n_program_chunks int | None

Number of round-robin chunks to split work into. Required when program_executor is, or builds, a MapArrayExecutor, since one array task per item would be dominated by scheduling overhead (checked at construction, or at first dispatch for a factory). Defaults to one chunk per item for a plain SubmitExecutor; ignored when program_executor is None.

None
n_shot_batches int | None

Number of batches to split shots into when shot_executor is set, governing shot-level parallel dispatch chunking independent of whether checkpointing is on. A batch count, not a size (e.g. n_shot_batches=5 with num_shots=100 produces 5 batches of 20 shots each). None (default) means "auto": max(1, ceil(num_shots / 20)) batches whenever shot_executor is set, resolved by QuantumProgram.run() itself once num_shots is known. Also usable in serial mode (no shot_executor) to control checkpoint-flush batch size instead, via the same count -- see QuantumProgram.run's own n_shot_batches/checkpoint_batch_size resolution for the exact behavior in each mode.

None
shot_executor SubmitExecutor | Callable[[], SubmitExecutor] | None

Nested shot-level parallelism, forwarded as QuantumProgram.run(shot_executor=...) for every program built inside a chunk. None means no shot-level parallelism. When program_executor is None, a live executor is used as-is (no process boundary is crossed). When program_executor is also given, a live executor can't cross the process boundary each dispatched chunk crosses -- a recognized backend (currently just loky) is transparently replaced with an ExecutorSpec built from its own construction parameters, which builds an equivalent fresh executor inside each chunk worker; an unrecognized backend instead requires an ExecutorSpec (such as ExecutorSpec.mpi4py) or an explicit zero-argument factory callable, which is used as given.

None
collect_resource_stats bool

Whether dispatch() should have each dispatched chunk self-report its own worker process's peak memory / mean CPU utilization (see _ResourceSampler). Primarily set (and later read back via pop_resource_stats()) by profile_strategies() itself, not meant to be hand-set for an ordinary (non-profiling) run.

False
resource_sample_interval float

Sampling interval (seconds) used when collect_resource_stats is True.

0.2

describe

describe(items: Sequence[T] | None = None, num_shots: int | None = None) -> str

Human-readable summary of this strategy's configured parallelism at each axis, for sanity-checking a configuration before running anything.

Each axis is reported as a short backend tag (e.g. "loky(max_workers=2)", via _describe_executor_tag/ ExecutorSpec.describe), followed by whichever indented fields can actually be computed from what's known -- a field is simply omitted (not printed as "unknown") when a required input (items, num_shots, or a worker count the executor doesn't expose) is missing. The program axis can report a genuine chunk count (n_program_chunks is an explicit, independently-chosen setting) alongside how many chunks land on each worker; the shot axis has no analogous chunking setting -- shots are dispatched one at a time -- so it only reports the total and the resulting average per worker.


Name Type Description Default
program_executor SubmitExecutor | MapArrayExecutor | ExecutorSpec | Callable | None

How chunks are dispatched; None runs them serially in the driver process. A MapArrayExecutor (e.g. submitit.Executor) gets one bulk .map_array() call and a SubmitExecutor (e.g. loky.get_reusable_executor(), MPIPoolExecutor) one .submit() per chunk; MapArrayExecutor is checked first, since submitit's executors satisfy both.

An ExecutorSpec or a zero-argument factory is called once, on first dispatch, and cached. A spec lets differently sized strategies share loky's process-wide singleton safely, and ExecutorSpec.submitit or ExecutorSpec.mpi4py is how to checkpoint a submitit or MPI run. A factory is the fallback for backends LoQS doesn't know. It is stored as source, so define it at module level in a .py file (in __main__ or a notebook, keep its settings in its body); lambdas and functools.partial can't be stored.

None
n_program_chunks int | None

Number of round-robin chunks to split work into. Required when program_executor is, or builds, a MapArrayExecutor, since one array task per item would be dominated by scheduling overhead (checked at construction, or at first dispatch for a factory). Defaults to one chunk per item for a plain SubmitExecutor; ignored when program_executor is None.

None
n_shot_batches int | None

Number of batches to split shots into when shot_executor is set, governing shot-level parallel dispatch chunking independent of whether checkpointing is on. A batch count, not a size (e.g. n_shot_batches=5 with num_shots=100 produces 5 batches of 20 shots each). None (default) means "auto": max(1, ceil(num_shots / 20)) batches whenever shot_executor is set, resolved by QuantumProgram.run() itself once num_shots is known. Also usable in serial mode (no shot_executor) to control checkpoint-flush batch size instead, via the same count -- see QuantumProgram.run's own n_shot_batches/checkpoint_batch_size resolution for the exact behavior in each mode.

None
shot_executor SubmitExecutor | Callable[[], SubmitExecutor] | None

Nested shot-level parallelism, forwarded as QuantumProgram.run(shot_executor=...) for every program built inside a chunk. None means no shot-level parallelism. When program_executor is None, a live executor is used as-is (no process boundary is crossed). When program_executor is also given, a live executor can't cross the process boundary each dispatched chunk crosses -- a recognized backend (currently just loky) is transparently replaced with an ExecutorSpec built from its own construction parameters, which builds an equivalent fresh executor inside each chunk worker; an unrecognized backend instead requires an ExecutorSpec (such as ExecutorSpec.mpi4py) or an explicit zero-argument factory callable, which is used as given.

None
collect_resource_stats bool

Whether dispatch() should have each dispatched chunk self-report its own worker process's peak memory / mean CPU utilization (see _ResourceSampler). Primarily set (and later read back via pop_resource_stats()) by profile_strategies() itself, not meant to be hand-set for an ordinary (non-profiling) run.

False
resource_sample_interval float

Sampling interval (seconds) used when collect_resource_stats is True.

0.2

dispatch

dispatch(worker_fn: Callable[[list[T]], R], chunks: Sequence[list[T]], desc: str = 'Processing chunks', on_poll: Callable[[], None] | None = None, poll_interval: float = 1.0) -> list[R]

Dispatch worker_fn over chunks via program_executor, picking the dispatch mechanism (.submit()-per-chunk vs. a bulk .map_array() call) automatically. Only valid when is_chunked is True.

When collect_resource_stats is set, each chunk's worker is transparently wrapped to self-report its own resource usage (see _self_reporting_worker) -- the wrapping/unwrapping happens entirely here, so this method's own return value (one real result per chunk, in chunks order) is unaffected either way; collected stats are appended to _resource_stats, retrievable via pop_resource_stats().

Parameters:

Name Type Description Default
on_poll Callable[[], None] | None

Optional callback invoked during dispatch polling (only relevant for certain executors/strategies).

None
poll_interval float

Polling interval in seconds (default 1.0).

1.0

dump

Inherited method from Serializable.dump().


encode

Inherited static method from Serializable.encode().


load

Inherited class method from Serializable.load().


Name Type Description Default
program_executor SubmitExecutor | MapArrayExecutor | ExecutorSpec | Callable | None

How chunks are dispatched; None runs them serially in the driver process. A MapArrayExecutor (e.g. submitit.Executor) gets one bulk .map_array() call and a SubmitExecutor (e.g. loky.get_reusable_executor(), MPIPoolExecutor) one .submit() per chunk; MapArrayExecutor is checked first, since submitit's executors satisfy both.

An ExecutorSpec or a zero-argument factory is called once, on first dispatch, and cached. A spec lets differently sized strategies share loky's process-wide singleton safely, and ExecutorSpec.submitit or ExecutorSpec.mpi4py is how to checkpoint a submitit or MPI run. A factory is the fallback for backends LoQS doesn't know. It is stored as source, so define it at module level in a .py file (in __main__ or a notebook, keep its settings in its body); lambdas and functools.partial can't be stored.

None
n_program_chunks int | None

Number of round-robin chunks to split work into. Required when program_executor is, or builds, a MapArrayExecutor, since one array task per item would be dominated by scheduling overhead (checked at construction, or at first dispatch for a factory). Defaults to one chunk per item for a plain SubmitExecutor; ignored when program_executor is None.

None
n_shot_batches int | None

Number of batches to split shots into when shot_executor is set, governing shot-level parallel dispatch chunking independent of whether checkpointing is on. A batch count, not a size (e.g. n_shot_batches=5 with num_shots=100 produces 5 batches of 20 shots each). None (default) means "auto": max(1, ceil(num_shots / 20)) batches whenever shot_executor is set, resolved by QuantumProgram.run() itself once num_shots is known. Also usable in serial mode (no shot_executor) to control checkpoint-flush batch size instead, via the same count -- see QuantumProgram.run's own n_shot_batches/checkpoint_batch_size resolution for the exact behavior in each mode.

None
shot_executor SubmitExecutor | Callable[[], SubmitExecutor] | None

Nested shot-level parallelism, forwarded as QuantumProgram.run(shot_executor=...) for every program built inside a chunk. None means no shot-level parallelism. When program_executor is None, a live executor is used as-is (no process boundary is crossed). When program_executor is also given, a live executor can't cross the process boundary each dispatched chunk crosses -- a recognized backend (currently just loky) is transparently replaced with an ExecutorSpec built from its own construction parameters, which builds an equivalent fresh executor inside each chunk worker; an unrecognized backend instead requires an ExecutorSpec (such as ExecutorSpec.mpi4py) or an explicit zero-argument factory callable, which is used as given.

None
collect_resource_stats bool

Whether dispatch() should have each dispatched chunk self-report its own worker process's peak memory / mean CPU utilization (see _ResourceSampler). Primarily set (and later read back via pop_resource_stats()) by profile_strategies() itself, not meant to be hand-set for an ordinary (non-profiling) run.

False
resource_sample_interval float

Sampling interval (seconds) used when collect_resource_stats is True.

0.2

make_chunks

make_chunks(items: Sequence[T]) -> list[list[T]]

Split items into round-robin chunks per n_program_chunks (defaulting to one chunk per item when unset).


Name Type Description Default
program_executor SubmitExecutor | MapArrayExecutor | ExecutorSpec | Callable | None

How chunks are dispatched; None runs them serially in the driver process. A MapArrayExecutor (e.g. submitit.Executor) gets one bulk .map_array() call and a SubmitExecutor (e.g. loky.get_reusable_executor(), MPIPoolExecutor) one .submit() per chunk; MapArrayExecutor is checked first, since submitit's executors satisfy both.

An ExecutorSpec or a zero-argument factory is called once, on first dispatch, and cached. A spec lets differently sized strategies share loky's process-wide singleton safely, and ExecutorSpec.submitit or ExecutorSpec.mpi4py is how to checkpoint a submitit or MPI run. A factory is the fallback for backends LoQS doesn't know. It is stored as source, so define it at module level in a .py file (in __main__ or a notebook, keep its settings in its body); lambdas and functools.partial can't be stored.

None
n_program_chunks int | None

Number of round-robin chunks to split work into. Required when program_executor is, or builds, a MapArrayExecutor, since one array task per item would be dominated by scheduling overhead (checked at construction, or at first dispatch for a factory). Defaults to one chunk per item for a plain SubmitExecutor; ignored when program_executor is None.

None
n_shot_batches int | None

Number of batches to split shots into when shot_executor is set, governing shot-level parallel dispatch chunking independent of whether checkpointing is on. A batch count, not a size (e.g. n_shot_batches=5 with num_shots=100 produces 5 batches of 20 shots each). None (default) means "auto": max(1, ceil(num_shots / 20)) batches whenever shot_executor is set, resolved by QuantumProgram.run() itself once num_shots is known. Also usable in serial mode (no shot_executor) to control checkpoint-flush batch size instead, via the same count -- see QuantumProgram.run's own n_shot_batches/checkpoint_batch_size resolution for the exact behavior in each mode.

None
shot_executor SubmitExecutor | Callable[[], SubmitExecutor] | None

Nested shot-level parallelism, forwarded as QuantumProgram.run(shot_executor=...) for every program built inside a chunk. None means no shot-level parallelism. When program_executor is None, a live executor is used as-is (no process boundary is crossed). When program_executor is also given, a live executor can't cross the process boundary each dispatched chunk crosses -- a recognized backend (currently just loky) is transparently replaced with an ExecutorSpec built from its own construction parameters, which builds an equivalent fresh executor inside each chunk worker; an unrecognized backend instead requires an ExecutorSpec (such as ExecutorSpec.mpi4py) or an explicit zero-argument factory callable, which is used as given.

None
collect_resource_stats bool

Whether dispatch() should have each dispatched chunk self-report its own worker process's peak memory / mean CPU utilization (see _ResourceSampler). Primarily set (and later read back via pop_resource_stats()) by profile_strategies() itself, not meant to be hand-set for an ordinary (non-profiling) run.

False
resource_sample_interval float

Sampling interval (seconds) used when collect_resource_stats is True.

0.2

plot

plot(items: Sequence[T] | None = None, program_workers: int | None = None, shot_workers: int | None = None, node_count: int = 1, legend: bool = True) -> matplotlib.axes.Axes

Diagram of this strategy's real dispatch structure.

See _plot for full documentation.


Name Type Description Default
program_executor SubmitExecutor | MapArrayExecutor | ExecutorSpec | Callable | None

How chunks are dispatched; None runs them serially in the driver process. A MapArrayExecutor (e.g. submitit.Executor) gets one bulk .map_array() call and a SubmitExecutor (e.g. loky.get_reusable_executor(), MPIPoolExecutor) one .submit() per chunk; MapArrayExecutor is checked first, since submitit's executors satisfy both.

An ExecutorSpec or a zero-argument factory is called once, on first dispatch, and cached. A spec lets differently sized strategies share loky's process-wide singleton safely, and ExecutorSpec.submitit or ExecutorSpec.mpi4py is how to checkpoint a submitit or MPI run. A factory is the fallback for backends LoQS doesn't know. It is stored as source, so define it at module level in a .py file (in __main__ or a notebook, keep its settings in its body); lambdas and functools.partial can't be stored.

None
n_program_chunks int | None

Number of round-robin chunks to split work into. Required when program_executor is, or builds, a MapArrayExecutor, since one array task per item would be dominated by scheduling overhead (checked at construction, or at first dispatch for a factory). Defaults to one chunk per item for a plain SubmitExecutor; ignored when program_executor is None.

None
n_shot_batches int | None

Number of batches to split shots into when shot_executor is set, governing shot-level parallel dispatch chunking independent of whether checkpointing is on. A batch count, not a size (e.g. n_shot_batches=5 with num_shots=100 produces 5 batches of 20 shots each). None (default) means "auto": max(1, ceil(num_shots / 20)) batches whenever shot_executor is set, resolved by QuantumProgram.run() itself once num_shots is known. Also usable in serial mode (no shot_executor) to control checkpoint-flush batch size instead, via the same count -- see QuantumProgram.run's own n_shot_batches/checkpoint_batch_size resolution for the exact behavior in each mode.

None
shot_executor SubmitExecutor | Callable[[], SubmitExecutor] | None

Nested shot-level parallelism, forwarded as QuantumProgram.run(shot_executor=...) for every program built inside a chunk. None means no shot-level parallelism. When program_executor is None, a live executor is used as-is (no process boundary is crossed). When program_executor is also given, a live executor can't cross the process boundary each dispatched chunk crosses -- a recognized backend (currently just loky) is transparently replaced with an ExecutorSpec built from its own construction parameters, which builds an equivalent fresh executor inside each chunk worker; an unrecognized backend instead requires an ExecutorSpec (such as ExecutorSpec.mpi4py) or an explicit zero-argument factory callable, which is used as given.

None
collect_resource_stats bool

Whether dispatch() should have each dispatched chunk self-report its own worker process's peak memory / mean CPU utilization (see _ResourceSampler). Primarily set (and later read back via pop_resource_stats()) by profile_strategies() itself, not meant to be hand-set for an ordinary (non-profiling) run.

False
resource_sample_interval float

Sampling interval (seconds) used when collect_resource_stats is True.

0.2

pop_resource_stats

pop_resource_stats() -> list[ChunkResourceStats]

Return and clear every ChunkResourceStats collected by dispatch() since the last call -- used by profile_strategies() to drain one run's worth of self-reported stats without leaving them to accumulate silently across unrelated later calls.


read

Inherited class method from Serializable.read().


serialize_function

Inherited static method from Serializable.serialize_function().


write

Inherited method from Serializable.write().