Runtime

The execution-facing layer: the contract every backend adapter implements, the configuration model, and the command-line entry point.

Executor

Base abstraction for quantum circuit executors.

This module defines the contract that all quantum backend adapters must implement, including circuit creation, application construction, and application execution.

It belongs to the runtime layer, so imports from backend-specific packages such as netqasm or cunqa must not appear here.

class netqmpi.runtime.executor.Executor[source]

Bases: ABC

Abstract base class for quantum backend executors.

This interface combines three responsibilities into a single adapter contract:

  1. Circuit factory through create_circuit().

  2. Application builder through build_apps().

  3. Application runner through run().

Variables:
  • size – Number of nodes or resources managed by the executor.

  • config – Backend-specific configuration dictionary.

__init__(size, config)[source]

Initialize the executor.

Parameters:
  • size (int) – Number of available nodes or resources.

  • config (RunConfig) – Backend-specific configuration dictionary.

Return type:

None

property size: int

Return the number of nodes or resources managed by this executor.

Returns:

The executor size.

property config: Dict[str, Any]

Return the backend-specific configuration.

Returns:

The configuration dictionary.

abstractmethod create_circuit(num_qubits, num_clbits, comm)[source]

Create a backend-specific quantum circuit.

Parameters:
  • num_qubits (int) – Number of qubits in the circuit.

  • num_clbits (int) – Number of classical bits in the circuit.

  • comm (QMPICommunicator) – Communicator associated with the circuit.

Returns:

A backend-specific Circuit.

Return type:

Circuit

abstractmethod build_apps(file, size)[source]

Load a script and build the rank-specific application instances.

Each rank receives an injected Environment exposing this executor’s create_circuit() factory.

Parameters:
  • file (str) – Path to the NetQMPI Python script. It must contain a main(env=None) function.

  • size (int) – Number of parallel quantum nodes to simulate.

Returns:

A backend-specific application instance ready to be passed to run().

Return type:

Any

abstractmethod run(apps)[source]

Execute an application instance with the given configuration.

Parameters:
  • app_instance – Object returned by build_apps().

  • config – Simulation or execution parameters. Backend adapters may accept a subclass of RunConfig with additional backend-specific fields.

  • apps (Any)

Return type:

None

metrics()[source]

Return the metrics of the last run(), as comm.metrics has them.

Kept apart from run(), which returns nothing, so that the executors that report no metrics need no change: they inherit this default.

Returns:

The metrics of the run, grouped by who produced them. Empty for a backend that reports none, or before any run.

Return type:

Dict[str, Any]

Run configuration

Backend-agnostic configuration for running a NetQMPI application.

This module defines a runtime configuration object that uses only primitive Python types, ensuring that the runtime layer remains fully decoupled from any specific backend (e.g. NetQASM, CUNQA, Qoala).

Backend adapters may subclass RunConfig to introduce additional fields required by their simulator or hardware. All configuration is loaded from a single YAML file (see read_config_block()) instead of per-backend command-line flags, so the CLI stays small as backends grow.

class netqmpi.runtime.run_config.RunConfig[source]

Bases: object

Configuration for a single NetQMPI simulation run.

All fields rely on plain Python types. Backend-specific parameters (such as NetQASM formalism or Qoala qdevice noise) should be provided by subclasses defined in the corresponding adapter packages.

Variables:

shots (int) – Number of times the simulation is repeated.

shots: int = 1024
classmethod from_dict(data)[source]

Build a config from a plain dict, mapping keys to dataclass fields.

Unknown keys raise ValueError so typos in a YAML config surface immediately instead of being silently ignored. Subclasses with nested/structured fields (e.g. a qdevice block) should override this to translate those keys before delegating here.

Parameters:

data (Dict[str, Any]) – Mapping of field names to values.

Returns:

A config instance of cls.

Return type:

_T

__init__(shots=1024)
Parameters:

shots (int)

Return type:

None

netqmpi.runtime.run_config.read_config_block(path, backend)[source]

Read a NetQMPI YAML config file and return the settings for one backend.

The file mixes generic (shared) settings at the top level with optional per-backend blocks keyed by backend name. Only the block for backend is merged on top of the generic settings; blocks for other backends are ignored. Example:

shots: 1000
seed: 7
qoala:
  link_fidelity: 0.8
  hardware:
    t1: 0

Reading this with backend="qoala" yields {"shots": 1000, "seed": 7, "link_fidelity": 0.8, "hardware": {...}}.

Parameters:
  • path (str) – Path to the YAML config file.

  • backend (str) – Backend whose block should be merged in.

Returns:

The merged settings dict, ready for RunConfig.from_dict.

Raises:

ValueError – If the file or the backend block is not a mapping.

Return type:

Dict[str, Any]

Command-line interface

NetQMPI command-line entry point.

This module is intentionally free of backend-specific imports from the core runtime logic. All simulator-specific behavior is delegated to an Executor implementation provided by the corresponding adapter package.

netqmpi.runtime.cli.simulate(script, num_procs=1, executor=None, config=None, timer=False)[source]

Build and run a NetQMPI script using the given backend executor.

Parameters:
  • script (str) – Path to the NetQMPI Python script.

  • num_procs (int) – Number of parallel quantum nodes.

  • executor (Executor | None) – Backend executor to use. If None, a NetQASMExecutorAdapter is used by default.

  • config (RunConfig | None) – Simulation parameters. If None, a default RunConfig instance is used.

  • timer (bool) – If True, print the wall-clock execution time.

Return type:

None

netqmpi.runtime.cli.write_metrics(path, metrics)[source]

Write the metrics of a run to a JSON file, for --metrics.

Only the metrics are written, not the configuration of the run. A backend that reports none gets an empty object and a note on stderr, so that a sweep over backends always finds a file to read.

Parameters:
Return type:

None

netqmpi.runtime.cli.main()[source]

Parse command-line arguments and execute the requested NetQMPI script.

The selected backend adapter is instantiated from the provided flags and passed to simulate().

Simulation plugins

Simulation plugins: what NetQMPI expects of one, and how it finds it.

A plugin customises how a backend simulates the network: a hardware model, a link model, readout error. NetQMPI does not interpret it. It finds the plugin, hands it plugin_config unread, and keeps the context manager the plugin returns open for the length of the simulation. Which backends accept plugins is up to each backend’s run config, which has a plugin key when it does (today only the NetQASM backend’s).

A plugin is named in one of two ways:

  • By a short name, without a colon, which is looked up in the netqmpi.plugins entry-point group. This is how an installed plugin package is used:

    # pyproject.toml of the plugin package
    [project.entry-points."netqmpi.plugins"]
    nv_2026 = "netqmpi_colorcenters:nv_2026"
    
  • As "module:callable", with the module imported the way python -m would import it: the working directory first.

Nothing here imports a backend, so netqmpi --list-plugins works whatever simulator is installed.

netqmpi.plugins.GROUP = 'netqmpi.plugins'

The entry-point group plugin packages register their plugins in.

class netqmpi.plugins.Plugin[source]

Bases: Protocol

What NetQMPI requires of a plugin, as a typing.Protocol.

Only __call__ is required, so a plain function is a plugin. NetQMPI reads three further members with getattr, each optional:

run_defaults

A mapping of run-config keys to the values the plugin needs, for example {"formalism": "dm", "num_qubits": 3}. They take the place of the backend’s own defaults; anything the config file or the command line sets still wins. They are applied when the config is built from a file or the command line, through the run config’s from_dict.

configure(plugin_config, run_config)

Called once when the run config is built, before any rank is traced, with copies of plugin_config and of the final run config. It validates them and raises ValueError on a bad value or an incompatible setting; whatever it changes is discarded.

backends

The backends the plugin is written for, e.g. ("netqasm",). A backend not in it refuses the plugin. Without it, any backend that accepts plugins does.

__init__(*args, **kwargs)
class netqmpi.plugins.InstalledPlugin[source]

Bases: object

A plugin registered in the netqmpi.plugins entry-point group.

Variables:
  • name (str) – The short name a run selects it by.

  • target (str) – The module:attribute the name points to.

  • distribution (str) – The installed distribution that registers it.

  • version (str) – That distribution’s version.

name: str
target: str
distribution: str
version: str
__init__(name, target, distribution, version)
Parameters:
Return type:

None

netqmpi.plugins.installed()[source]

List the plugins installed through entry points, sorted by name.

Reads each installed distribution’s entry points rather than calling importlib.metadata.entry_points, whose signature changed in Python 3.10; this works the same from 3.8 on and also gives the distribution that provides each plugin. A distribution found twice on sys.path counts once, as the first copy, which is the one Python imports.

Returns:

One entry per plugin and distribution.

Return type:

List[InstalledPlugin]

netqmpi.plugins.load(spec)[source]

Find the plugin a run config names.

Parameters:

spec (str) – A short name registered in the netqmpi.plugins entry-point group, or "package.module:callable".

Returns:

The plugin – a callable that takes plugin_config.

Raises:

ValueError – If no installed plugin has the short name, two distributions provide it, spec is a malformed module:callable, or what it names cannot be imported or is not callable.

Return type:

Callable[[Mapping[str, Any]], ContextManager[Any]]

netqmpi.plugins.run_defaults(plugin, spec, config_cls)[source]

Read and check the run-config defaults a plugin asks for.

Parameters:
  • plugin (Any) – The loaded plugin.

  • spec (str) – The name the run config gave it, for error messages.

  • config_cls (type) – The backend’s run-config dataclass.

Returns:

A copy of plugin.run_defaults, or {} if it has none.

Raises:

ValueError – If run_defaults is not a mapping, names a key the run config does not have, or sets plugin/plugin_config. The message names the plugin: the user’s config is not at fault.

Return type:

Dict[str, Any]

netqmpi.plugins.check_backend(plugin, spec, backend)[source]

Refuse a plugin whose backends does not include backend.

Parameters:
  • plugin (Any) – The loaded plugin.

  • spec (str) – The name the run config gave it, for error messages.

  • backend (str) – The backend about to run it, e.g. "netqasm".

Raises:

ValueError – If the plugin declares backends and backend is not one of them.

Return type:

None

netqmpi.plugins.configure(plugin, run_config)[source]

Let a plugin check the run config it will run under.

The plugin’s configure, if it has one, is called with a deep copy of plugin_config and a copy of run_config that carries another, so it can read both but change neither. The run config itself is copied shallowly: it may hold objects, such as post_function, that cannot be deep-copied.

Parameters:
  • plugin (Any) – The loaded plugin.

  • run_config (Any) – The final run config, with plugin_config.

Raises:

Exception – Whatever the plugin’s configure raises; ValueError is the documented choice.

Return type:

None

Helpers

netqmpi.helpers.load_main(path)[source]