@guppy decorator

@guppylang.decorator.guppy

A dummy class with the same interface as @guppy that does nothing when used to decorate functions.

We use this during sphinx builds as a mock for the decorator, to ensure that Guppy functions are recognised as regular functions and included in docs.

class guppylang.decorator._Guppy[source]

Class for the @guppy decorator.

comptime(f: Callable[[P], T]) GuppyFunctionDefinition[P, T][source]
const_var(name: str, ty: str) TypeVar[source]

Creates a new const type variable.

constant(name: str, ty: str, value: Value) T[source]

Adds a constant to a module, backed by a hugr.val.Value.

custom(compiler: CustomInoutCallCompiler | None = None, checker: CustomCallChecker | None = None, higher_order_value: bool = True, name: str = '', signature: FunctionType | None = None) Callable[[Callable[[P], T]], GuppyFunctionDefinition[P, T]][source]
declare(f: Callable[[P], T]) GuppyFunctionDefinition[P, T][source]
extend_type(defn: TypeDef) Callable[[type], type][source]
hugr_op(op: Callable[[FunctionType, Sequence[TypeArg | ConstArg], CompilerContext], DataflowOp], checker: CustomCallChecker | None = None, higher_order_value: bool = True, name: str = '', signature: FunctionType | None = None) Callable[[Callable[[P], T]], GuppyFunctionDefinition[P, T]][source]
load_pytket(name: str, input_circuit: Any, *, use_arrays: bool = True) GuppyFunctionDefinition[..., Any][source]

Adds a pytket circuit function definition with implicit signature.

nat_var(name: str) TypeVar[source]

Creates a new const nat variable in a module.

overload(*funcs: Any) Callable[[Callable[[P], T]], GuppyFunctionDefinition[P, T]][source]

Collects multiple function definitions into one overloaded function.

Consider the following example:

@guppy.declare
def variant1(x: int, y: int) -> int: ...

@guppy.declare
def variant2(x: float) -> int: ...

@guppy.overload(variant1, variant2)
def combined(): ...

Now, combined may be called with either one float or two int arguments, delegating to the implementation with the matching signature:

combined(4.2)  # Calls `variant1`
combined(42, 43)  # Calls `variant2`

Note that the compiler will pick the first implementation with matching signature and ignore all following ones, even if they would also match. For example, if we added a third variant

@guppy.declare
def variant3(x: int) -> int: ...

@guppy.overload(variant1, variant2, variant3)
def combined_new(): ...

then a call combined_new(42) will still select the variant1 implementation 42 is a valid argument for variant1 and variant1 comes before variant3 in the @guppy.overload annotation.

pytket(input_circuit: Any) Callable[[Callable[[P], T]], GuppyFunctionDefinition[P, T]][source]

Adds a pytket circuit function definition with explicit signature.

struct(cls: type[T]) type[T][source]
type_var(name: str, copyable: bool = True, droppable: bool = True) TypeVar[source]

Creates a new type variable in a module.

@guppylang.decorator.custom_guppy_decorator[source]

Decorator to mark user-defined decorators that wrap builtin guppy decorators.

Example:

@custom_guppy_decorator
def my_guppy(f):
    # Some custom logic here ...
    return guppy(f)

@my_guppy
def main() -> int: ...

If the custom_guppy_decorator were missing, then the @my_guppy annotation would not produce a valid guppy definition.

guppylang.decorator.get_calling_frame() FrameType[source]

Finds the first frame that called this function outside the compiler modules.