gflownet.envs.ising

Ising model: The sample space is an ensemble of discrete variables that can take two values (-1 or +1), arranged spatially in a D-dimensional grid. The state space includes intermediate states with variables at a neutral (not yet selected) spin and states that indicate the variable to be set or unset.

Attributes

TOGGLE_VARIABLE

SET_SPIN

UNSET

TOGGLED

STATE_TYPES

Classes

Ising

Initializes an Ising environment.

Module Contents

gflownet.envs.ising.TOGGLE_VARIABLE = 0[source]
gflownet.envs.ising.SET_SPIN = 1[source]
gflownet.envs.ising.UNSET = 0[source]
gflownet.envs.ising.TOGGLED = 3[source]
gflownet.envs.ising.STATE_TYPES[source]
class gflownet.envs.ising.Ising(n_dim=2, length=4, **kwargs)[source]

Bases: gflownet.envs.base.GFlowNetEnv

Initializes an Ising environment.

Parameters:
  • n_dim (int) – The dimensionality of the Ising model. Default: 2

  • length (int) – The number of variables or cells per dimension. Default: 4

n_dim = 2[source]
length = 4[source]
source[source]
eos[source]
get_action_space()[source]

Constructs list with all possible actions, including EOS.

Actions are represented by two elements:
  • Action type: select variable (TOGGLE_VARIABLE) or set spin (SET_SPIN)

  • Value: value of the action, namely the index of the variable for selecting variables and -1 or 1 for setting the spin.

Returns:

list – A list of tuples representing the actions.

Return type:

List[Tuple]

get_mask_invalid_actions_forward(state=None, done=None)[source]

Returns a list of length the action space indicating which actions are invalid (True) and which are not invalid (False).

Parameters:
  • state (np.array) – Input state. If None, self.state is used.

  • done (bool) – Whether the trajectory is done. If None, self.done is used.

Returns:

A list of boolean values.

Return type:

List[bool]

get_parents(state=None, done=None, action=None)[source]

Determines all parents and actions that lead to state.

Parameters:
  • state (np.array) – Input state. If None, self.state is used.

  • done (bool) – Whether the trajectory is done. If None, self.done is used.

  • action (None) – Ignored

Returns:

  • parents (list) – List of parents in state format. This environment has a single parent per state.

  • actions (list) – List of actions that lead to state for each parent in parents. This environment has a single parent per state.

Return type:

Tuple[List, List]

step(action, skip_mask_check=False)[source]

Executes step given an action.

Variables follow the following cycle in order to be assigned a spin:
  1. An unselected variable (0) is toggled, turning its value into 3.

2. A spin is assigned, turning its value into -2 (for spin -1) or 2 (for spin 1). This is a transitory state where the variable has been assigned a spin but is still toggled. 3. The variable is toggled and receives its final value (-1 from -2) or (1 from 2).

Parameters:
  • action (tuple) – Action to be executed. An action is a tuple with two elements indicating the action type and action value.

  • skip_mask_check (bool) – If True, skip computing forward mask of invalid actions to check if the action is valid.

Returns:

  • self.state (list) – The state after executing the action

  • action (tuple) – Action executed

  • valid (bool) – False, if the action is not allowed for the current state.

Return type:

[List[int], Tuple[int], bool]

states2policy(states)[source]

Prepares a batch of states in “environment format” for the policy model: states are one-hot encoded.

The one-hot-encoding is a 2D tensor, with as many rows as entries in the state, that is length * n_dim. Each row is a one-hot-encoding of each variable. There are 6 possible values: 0, 3, -2, -1, 1, 2.

Parameters:

states (list) – A batch of states in environment format

Returns:

A tensor containing all the states in the batch.

Return type:

torchtyping.TensorType[batch, policy_input_dim]

readable2state(readable, alphabet={})[source]

Converts a human-readable string representing a state into a state as a list of positions.

state2readable(state=None, alphabet={})[source]

Converts a state (a list of positions) into a human-readable string representing a state.

Parameters:

state (Optional[List])