Extension

class hugr.ext.Extension(name: ExtensionId, version: Version, types: dict[str, TypeDef] = <factory>, operations: dict[str, OpDef] = <factory>)[source]

Bases: object

HUGR extension declaration.

Methods

add_op_def

Add an operation definition to the extension.

add_type_def

Add a type definition to the extension.

from_json

Deserialize a JSON string to a Extension object.

get_op

Retrieve an operation definition by name.

get_type

Retrieve a type definition by name.

register_op

Register a class as corresponding to an operation definition.

to_json

Serialize the extension to a JSON string.

Attributes

T

name

The name of the extension.

version

The version of the extension.

types

Type definitions in the extension.

operations

Operation definitions in the extension.

exception NotFound(name: str)[source]

Bases: Exception

An object was not found in the extension.

add_note()

Exception.add_note(note) – add a note to the exception

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

exception OperationNotFound(name: str)[source]

Bases: NotFound

Operation not found in extension.

add_note()

Exception.add_note(note) – add a note to the exception

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

exception TypeNotFound(name: str)[source]

Bases: NotFound

Type not found in extension.

add_note()

Exception.add_note(note) – add a note to the exception

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

exception ValueNotFound(name: str)[source]

Bases: NotFound

Value not found in extension.

add_note()

Exception.add_note(note) – add a note to the exception

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

add_op_def(op_def: OpDef) OpDef[source]

Add an operation definition to the extension.

Parameters:

op_def – The operation definition to add.

Returns:

The added operation definition, now associated with the extension.

add_type_def(type_def: TypeDef) TypeDef[source]

Add a type definition to the extension.

Parameters:

type_def – The type definition to add.

Returns:

The added type definition, now associated with the extension.

classmethod from_json(json_str: str) Extension[source]

Deserialize a JSON string to a Extension object.

Parameters:

json_str – The JSON string representing a Extension.

Returns:

The deserialized Extension object.

get_op(name: str) OpDef[source]

Retrieve an operation definition by name.

Parameters:

name – The name of the operation.

Returns:

The operation definition.

Raises:

OperationNotFound – If the operation is not found in the extension.

get_type(name: str) TypeDef[source]

Retrieve a type definition by name.

Parameters:

name – The name of the type.

Returns:

The type definition.

Raises:

TypeNotFound – If the type is not found in the extension.

name: ExtensionId

The name of the extension.

operations: dict[str, OpDef]

Operation definitions in the extension.

register_op(name: str | None = None, signature: OpDefSig | tys.PolyFuncType | tys.FunctionType | None = None, description: str | None = None, misc: dict[str, Any] | None = None, lower_funcs: list[FixedHugr] | None = None) Callable[[type[T]], type[T]][source]

Register a class as corresponding to an operation definition.

If name is not provided, the class name is used. If signature is not provided, a binary signature is assumed. If description is not provided, the class docstring is used.

See OpDef for other parameters.

to_json() str[source]

Serialize the extension to a JSON string.

types: dict[str, TypeDef]

Type definitions in the extension.

version: Version

The version of the extension.