mqt.core.plugins.qiskit.serializers

Registry of program serializers for QDMI program formats.

A QDMI device accepts a program in one or more program formats, listed by ProgramFormat. A program serializer turns one Qiskit QuantumCircuit into one program in one such format.

A format fixes the kind of payload it carries, so there are two signatures. A text format takes a TextProgramSerializer, which returns str. A binary format takes a BinaryProgramSerializer, which returns bytes. is_binary_program_format() states which kind a format carries. Two formats take no serializer at all, because a serialized circuit is not what they carry; see NON_CIRCUIT_FORMATS.

MQT Core registers its own OpenQASM 2 and OpenQASM 3 serializers here. Every other format belongs to the package that owns the device. Such a package advertises its serializer through the mqt.core.qiskit.program_serializers entry point group. The entry point name is the ProgramFormat member name, and the value points to the serializer:

`toml [project.entry-points."mqt.core.qiskit.program_serializers"] IQM_JSON = "iqm.qdmi.serializers:qiskit_to_iqm_json" `

register_program_serializer() does the same at run time. A registration takes precedence over an entry point for the same format.

A device usually accepts several formats. PROGRAM_FORMAT_PREFERENCE records which one to use, from most to least preferred, and preferred_program_formats() applies that order to the formats a device reports.

Module Contents

ENTRY_POINT_GROUP = 'mqt.core.qiskit.program_serializers'
class TextProgramSerializer

Bases: Protocol

Serializes a circuit into a program format whose payload is text.

__call__(circuit: QuantumCircuit, backend: QDMIBackend, /) → str

Serialize a circuit into a program string.

Parameters:
  • circuit – The circuit to serialize. It has no unbound parameters.

  • backend – The backend that runs the circuit. Its device property provides the site names and metadata a format may need, and its target property provides the supported operations.

Returns:

The program in the serializer’s format.

Raises:
class BinaryProgramSerializer

Bases: Protocol

Serializes a circuit into a program format whose payload is binary.

__call__(circuit: QuantumCircuit, backend: QDMIBackend, /) → bytes

Serialize a circuit into program bytes.

Parameters:
  • circuit – The circuit to serialize. It has no unbound parameters.

  • backend – The backend that runs the circuit. Its device property provides the site names and metadata a format may need, and its target property provides the supported operations.

Returns:

The program in the serializer’s format.

Raises:
ProgramSerializer
NON_CIRCUIT_FORMATS: frozenset[ProgramFormat]
PROGRAM_FORMAT_PREFERENCE: tuple[ProgramFormat, ...]
register_program_serializer(fmt: ProgramFormat, serializer: ProgramSerializer, *, replace: bool = False) → None

Register a serializer for one program format.

Parameters:
  • fmt – The program format the serializer produces.

  • serializer – The serializer to register. It must return str for a text format and bytes for a binary format.

  • replace – Replace an existing serializer for the same format.

Raises:

ValueError – If the format does not carry a serialized circuit, or if the format already has a serializer and replace is false.

unregister_program_serializer(fmt: ProgramFormat) → None

Remove the serializer for one program format.

Parameters:

fmt – The program format whose serializer to remove. A format without a serializer is ignored.

program_serializer(fmt: ProgramFormat) → ProgramSerializer | None

Return the serializer for one program format.

Parameters:

fmt – The program format to look up.

Returns:

The registered serializer, or None if no package provides one.

preferred_program_formats(formats: Iterable[ProgramFormat]) → list[ProgramFormat]

Order the program formats a device reports by PROGRAM_FORMAT_PREFERENCE.

Parameters:

formats – The program formats the device accepts.

Returns:

Those of the given formats that can carry a serialized circuit, most preferred first. A format that PROGRAM_FORMAT_PREFERENCE does not name comes after every format it does name, in the order it was given.