Custom operators and observables

Custom operators and observables#

NetKet exposes several extension paths for custom observables and operators. They differ in how much you implement yourself, whether gradients come for free, and whether expect_to_precision() works automatically. All three rely on NetKet’s multiple-dispatch extension mechanism; if you need the background first, start with Overriding defaults in NetKet and in particular NetKet Architecture: Multiple Dispatch.

Use the table below as the entry point, then jump to the tutorial section that matches the interface you want to implement.

Path

What you implement

expect()

expect_and_grad()

expect_to_precision()

Best when

Start here

Scalar operator kernel interface

Subclass AbstractOperator, then define get_local_kernel_arguments() and get_local_kernel().

Automatic

Automatic

Automatic for scalar local estimators

The observable is a standard operator whose expectation is the mean of one local-estimator channel.

Example: the lean operator-kernel interface

Explicit local_estimators() dispatch

Register local_estimators() and return either LocalEstimators or LocalEstimatorsBatch.

Automatic

No

Automatic

You need multi-channel local estimators, delta-method error propagation, or expect_to_precision() without gradients.

Example: V-score custom operator

Explicit observable dispatch

Register expect() for your custom observable, and optionally expect_and_grad().

Yes

Only if you define it

No

You want full control over the computation, or the observable does not fit NetKet’s local-estimator interfaces.

Example: explicit observable dispatch

Recommended starting points