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])
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]) >>> len(ran) 3 >>> ran.states_to_numbers(0) array(1) >>> ran.numbers_to_states(0) -2 >>> ran.numbers_to_states(1) 0 >>> ran.numbers_to_states(2) 2
- Inheritance
- __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=int64)
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=int64)
- 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,).
- Methods
- all_states(dtype=None)[source]#
Return all elements in the range. Equal to __array__
- Parameters:
dtype (
Any
) –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.
- 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 (
Any
) – 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)#
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.