netket.utils.StaticRange#

class netket.utils.StaticRange[source]#

Bases: Pytree

An object representing a range similar to python’s range, but that works with jax.jit.

This range object can also be used to convert ‘computational basis’ configurations to integer indices ∈ [0,length].

This object is used inside of Hilbert spaces.

This object can be converted to a numpy or jax array:

>>> import netket as nk; import numpy as np
>>> n_max = 10
>>> ran = nk.utils.StaticRange(start=0, step=1, length=n_max)
>>> np.array(ran)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int8)

And it can be used to convert between integer values starting at 0 and the values in the range.

>>> import netket as nk; import numpy as np
>>> ran = nk.utils.StaticRange(start=-2, step=2, length=3)
>>> np.array(ran)
array([-2,  0,  2], dtype=int8)
>>> len(ran)
3
>>> ran.states_to_numbers(0)
array(1, dtype=int8)
>>> ran.numbers_to_states(0)
np.int8(-2)
>>> ran.numbers_to_states(1)
np.int8(0)
>>> ran.numbers_to_states(2)
np.int8(2)
Inheritance
Inheritance diagram of netket.utils.StaticRange
__init__(start, step, length, dtype=None)[source]#

Constructs a Static Range object.

To construct it, one must specify the start value, the step and the length. It is also possible to specify a dtype. In case it’s not specified, it’s inferred from the input arguments.

For example, the StaticRange of a Fock Hilbert space is constructed as

>>> import netket as nk
>>> n_max = 10
>>> nk.utils.StaticRange(start=0, step=1, length=n_max)
StaticRange(start=0, step=1, length=10, dtype=int8)

and the range of a Spin-1/2 Hilbert space is constructed as:

>>> import netket as nk
>>> n_max = 10
>>> nk.utils.StaticRange(start=-1, step=2, length=2)
StaticRange(start=-1, step=2, length=2, dtype=int8)
Parameters:
  • start (Number) – Value of the first entry

  • step (Number) – Step between the entries

  • length (int) – Length of this range

  • dtype (Union[None, str, type[Any], dtype, _SupportsDType]) – The data type

Attributes
is_indexable#

If the range is indexable. Always True

n_states#

The number of states in the range. Equal to length.

ndim#

The number of dimensions of the range, if converted to an array. It’s always 1.

shape#

The shape of the range, if converted to an array. It’s always (length,).

start: float#

The first value in the range.

step: float#

The difference between two consecutive values in the range.

length: int#

The number of entries in the range.

dtype: Union[None, str, type[Any], dtype, _SupportsDType]#

The dtype of the range.

Methods
all_states(dtype=None)[source]#

Return all elements in the range. Equal to __array__

Parameters:

dtype (Union[None, str, type[Any], dtype, _SupportsDType]) –

Optional dtype to be used for the output.

Returns:

An array with all values from the range. The dtype by default is that of the range.

astype(dtype)[source]#

Returns a new StaticRange with a different dtype.

Parameters:

dtype (None | str | type[Any] | dtype | _SupportsDType)

flip_state(state)[source]#

Only works if this range has length 2. Given a state, returns the other state.

numbers_to_states(i, dtype=None)[source]#

Given an integer index, returns the i-th elements in the range.

Parameters:
  • x – indices to extract from the range.

  • dtype (Union[None, str, type[Any], dtype, _SupportsDType]) – Optional dtype to be used for the output.

Returns:

An array of values from the range. The dtype by default is that of the range.

replace(**kwargs)[source]#

Replace the values of the fields of the object with the values of the keyword arguments. If the object is a dataclass, dataclasses.replace will be used. Otherwise, a new object will be created with the same type as the original object.

Return type:

TypeVar(P, bound= Pytree)

Parameters:
  • self (P)

  • kwargs (Any)

states_to_numbers(x, dtype=None)[source]#

Given an element in the range, returns it’s index.

If the dtype is not specified, it will be the smallest unsigned integer dtype that can hold numbers in (0, length).

Parameters:
  • x – array of elements beloging to this range. No bounds checking is performed.

  • dtype (Union[None, str, type[Any], dtype, _SupportsDType]) – Optional dtype to be used for the output.

Returns:

An array of integers, which can be.