quchip.interop.base

Library-agnostic registry and dispatch for third-party model mappings.

A ModelMapping subclass authors a one-directional or bidirectional conversion between a third-party circuit-QED object and a quchip device. Subclasses register themselves at class-definition time, keyed by the third-party type they import from and/or the (library, quchip type) pair they export to. Nothing in this module depends on any specific third-party library; concrete mappings (e.g. for scqubits) live in sibling modules that import both sides.

Functions

export_object(device, library, **opts)

Export quchip device to a third-party object of library.

import_object(obj, **opts)

Import third-party obj into a quchip device via a registered mapping.

registered_mappings()

Return a copy of the import registry, keyed by source key.

source_key(tp)

Return the registry key for third-party type tp.

Classes

ModelMapping()

Base class for a single third-party <-> quchip device conversion.

quchip.interop.base.source_key(tp)[source]

Return the registry key for third-party type tp.

The key is the type’s top-level module name joined with its qualified name, e.g. "scqubits.Transmon". Only the top-level module is used so that a mapping registered against a package root matches classes re-exported from submodules.

Parameters:

tp (type)

Return type:

str

class quchip.interop.base.ModelMapping[source]

Bases: object

Base class for a single third-party <-> quchip device conversion.

source

Registry key (see source_key()) of the third-party type this mapping imports from. None means the mapping supports export only.

Type:

str or None

target

The quchip device type this mapping exports to. Required whenever export_model() is overridden; None means import-only.

Type:

type or None

library

Name of the third-party library this mapping exports for. Defaults to source.split(".")[0] when source is set; export-only mappings must set it explicitly.

Type:

str or None

Subclassing registers the mapping automatically
\* setting ``source`` registers it for :func:`import_object` under that

key; a second subclass reusing the same source raises TypeError at class-definition time.

\* overriding :meth:`export_model` registers it for :func:`export_object`

under (library, target); overriding without setting target raises TypeError, overriding without a resolvable library (no source to default it from) raises TypeError, and a second subclass reusing the same (library, target) pair raises TypeError naming both classes.

The abstract base itself (``source is None`` and no ``export_model``
override) registers nothing.
source: ClassVar[str | None] = None
target: ClassVar[type | None] = None
library: ClassVar[str | None] = None
import_model(obj, **opts)[source]

Convert third-party object obj into a quchip device.

Override to support import. The base implementation raises NotImplementedError.

Parameters:
Return type:

Any

export_model(device, **opts)[source]

Convert quchip device into a third-party object.

Override to support export; overriding requires setting target. The base implementation raises NotImplementedError.

Parameters:
Return type:

Any

quchip.interop.base.import_object(obj, **opts)[source]

Import third-party obj into a quchip device via a registered mapping.

Walks type(obj).__mro__ and dispatches to the first ModelMapping registered under that class’s source_key().

Raises:

LookupError – No mapping is registered for type(obj) or any of its base classes. The message names the missing source key and shows the skeleton for authoring a new ModelMapping.

Parameters:
Return type:

Any

quchip.interop.base.export_object(device, library, **opts)[source]

Export quchip device to a third-party object of library.

Walks type(device).__mro__ and dispatches to the first ModelMapping registered under (library, that class).

Raises:

LookupError – No mapping is registered for (library, type(device)) or any base class. The message lists the device types library can export.

Parameters:
Return type:

Any

quchip.interop.base.registered_mappings()[source]

Return a copy of the import registry, keyed by source key.

For introspection and tests; mutating the returned dict does not affect registration state.

Return type:

dict[str, type[ModelMapping]]