netket.errors.UnhashableConstraintError

netket.errors.UnhashableConstraintError#

exception netket.errors.UnhashableConstraintError[source]#

Error thrown when a constraint is not a fully static pytree, and it cannot be hashed.

This error is thrown when a constraint is a PyTree with some leaf nodes. This usually happens because you did not mark all the pytree fields in your constraint as struct.field(pytree_node=False).

For a good example, see the documentation of netket.hilbert.constraint.DiscreteHilbertConstraint. Below, you find a coincise example of how to fix this error:

from typing import Any
from netket.utils import struct
from netket.hilbert.index.constraints import DiscreteHilbertConstraint

class MyCustomConstraint(DiscreteHilbertConstraint):
    fieldA: Any = struct.field(pytree_node=False)
    fieldB: Any = struct.field(pytree_node=False)
    def __init__(self, fieldA, fieldB):
        self.fieldA = fieldA
        self.fieldB = fieldB

    def __call__(self, x):
        ....

    def __hash__(self):
        return hash(("MyCustomConstraint", self.fieldA, self.fieldB))

    def __eq__(self, other):
        if isinstance(other, UnhashableConstraintError):
            return self.fieldA == other.fieldA and self.fieldB == other.fieldB
        return False