Skip to content

Inference methods

methods

The inference methods an experiment's infer clause can name.

Each method is declared once, as an ExperimentMethod: its name, what it produces (a run of the entry on the reference machine, or a fit of a program's posterior), its options with their types and defaults, and how it is built from them. An experiment names a method and sets some of its options; the rest take their declared defaults, and the experiment's hash reads every option after defaults are applied, so writing a default out does not change the hash. register_method adds a method.

OptionValue

OptionValue = int | float | bool | None

The value of one method option; None for an absent optional bound.

MethodKind

MethodKind = Literal['run', 'fit']

What a method produces: a run of the entry or a fit of a program.

FitBuilder

FitBuilder = Callable[[Mapping[str, OptionValue], int], InferenceMethod[ModelFit]]

Builds a fit method from its options and the experiment's seed.

RunBuilder

RunBuilder = Callable[[Mapping[str, OptionValue]], RunSettings]

Builds a run's settings from its options.

MethodOption didactic-model

MethodOption(*, name: str, type: Literal['int', 'float', 'bool'], default: OptionValue = None, minimum: float | None = None, description: str = '')

Bases: Model

One option of an inference method.

PARAMETER DESCRIPTION
name

The option's name in an infer clause.

TYPE: str

type

Its value type.

TYPE: Literal['int', 'float', 'bool']

default

The value an experiment that omits it gets; None when the option is an optional bound.

TYPE: OptionValue DEFAULT: None

minimum

The least value it accepts, or None.

TYPE: float | None DEFAULT: None

description

What it sets.

TYPE: str DEFAULT: ''

RunSettings didactic-model

RunSettings(*, fuel: int | None = None, numbers: NumberDomain = field(default_factory=NumberDomain))

Bases: Model

How a run method invokes the entry on the reference machine.

PARAMETER DESCRIPTION
fuel

The step budget, or None for none.

TYPE: int | None DEFAULT: None

numbers

The number domain the run computes in.

TYPE: NumberDomain DEFAULT: field(default_factory=NumberDomain)

ExperimentMethod didactic-model

ExperimentMethod(*, name: str, kind: MethodKind, options: tuple[MethodOption, ...] = (), description: str = '', build_run: RunBuilder | None = field(default=None, opaque=True, extras=OPAQUE_BY_IDENTITY), build_fit: FitBuilder | None = field(default=None, opaque=True, extras=OPAQUE_BY_IDENTITY))

Bases: ValueModel

An inference method an experiment can name.

PARAMETER DESCRIPTION
name

Its name in an infer clause.

TYPE: str

kind

Whether it runs the entry or fits a program.

TYPE: MethodKind

options

Its options.

TYPE: tuple[MethodOption, ...] DEFAULT: ()

description

What it does.

TYPE: str DEFAULT: ''

build_run

For a run method, the settings builder, compared by identity.

TYPE: RunBuilder | None DEFAULT: field(default=None, opaque=True, extras=OPAQUE_BY_IDENTITY)

build_fit

For a fit method, the inference method builder, compared by identity.

TYPE: FitBuilder | None DEFAULT: field(default=None, opaque=True, extras=OPAQUE_BY_IDENTITY)

option

option(name: str) -> MethodOption | None

One option by name.

PARAMETER DESCRIPTION
name

The option's name.

TYPE: str

RETURNS DESCRIPTION
MethodOption | None

The option, or None when the method has none of that name.

Source code in src/quivers/experiments/methods.py
def option(self, name: str) -> MethodOption | None:
    """One option by name.

    Parameters
    ----------
    name : str
        The option's name.

    Returns
    -------
    MethodOption | None
        The option, or None when the method has none of that name.
    """
    return next((item for item in self.options if item.name == name), None)

register_method

register_method(method: ExperimentMethod) -> None

Make an inference method nameable in infer clauses.

PARAMETER DESCRIPTION
method

The method.

TYPE: ExperimentMethod

RAISES DESCRIPTION
ValueError

If a method of that name exists, or the method's builder does not match its kind.

Source code in src/quivers/experiments/methods.py
def register_method(method: ExperimentMethod) -> None:
    """Make an inference method nameable in ``infer`` clauses.

    Parameters
    ----------
    method : ExperimentMethod
        The method.

    Raises
    ------
    ValueError
        If a method of that name exists, or the method's builder does not
        match its kind.
    """
    if method.name in _METHODS:
        raise ValueError(f"an inference method {method.name!r} is already registered")
    if (method.kind == "run") != (method.build_run is not None) or (
        method.kind == "fit"
    ) != (method.build_fit is not None):
        raise ValueError(
            f"method {method.name!r} of kind {method.kind} needs exactly the "
            f"build_{method.kind} builder"
        )
    _METHODS[method.name] = method

experiment_method

experiment_method(name: str) -> ExperimentMethod | None

A registered method by name.

PARAMETER DESCRIPTION
name

The method's name.

TYPE: str

RETURNS DESCRIPTION
ExperimentMethod | None

The method, or None.

Source code in src/quivers/experiments/methods.py
def experiment_method(name: str) -> ExperimentMethod | None:
    """A registered method by name.

    Parameters
    ----------
    name : str
        The method's name.

    Returns
    -------
    ExperimentMethod | None
        The method, or None.
    """
    return _METHODS.get(name)

experiment_methods

experiment_methods() -> tuple[ExperimentMethod, ...]

Every registered method.

RETURNS DESCRIPTION
tuple[ExperimentMethod, ...]

The methods, sorted by name.

Source code in src/quivers/experiments/methods.py
def experiment_methods() -> tuple[ExperimentMethod, ...]:
    """Every registered method.

    Returns
    -------
    tuple[ExperimentMethod, ...]
        The methods, sorted by name.
    """
    return tuple(_METHODS[name] for name in sorted(_METHODS))