model
qemcmc.model ¶
EnergyModel ¶
A base class for energy models for a classical energy function over n spins, defined by arbitrary-order coupling tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of spins |
required |
couplings
|
List[ndarray]
|
List of numpy arrays representing interaction tensors. A 1D array encodes linear terms, a 2D array encodes pairwise terms (expected symmetric), and higher-rank tensors encode higher-order interactions. |
[]
|
name
|
str
|
Optional label for the model (used in plotting / logging). |
None
|
cost_function_signs
|
list
|
Sign convention(s) used by downstream components (e.g. proposal/acceptance conventions).
For example, |
[-1, -1]
|
model_type
|
str
|
Type of model, either 'ising' or 'binary'. This determines how the binary states are interpreted and how the energy is calculated. 'ising' models use spin values {-1, +1}, while 'binary' models use binary values {0, 1}. |
'ising'
|
Notes
- Energies are computed by mapping binary states
{0,1}to spin values{-1,+1}internally. - Brute-force methods such as
get_all_energiesscale as O(2^n) and are intended only for small systems.
Source code in src/qemcmc/model/energy_model.py
get_initial_states ¶
Generates a list of random initial states.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_initial_states
|
int
|
The number of initial states to generate. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
A list of random initial states. |
Source code in src/qemcmc/model/energy_model.py
get_ground_state ¶
Finds an approximate ground state using Simulated Annealing.
Source code in src/qemcmc/model/energy_model.py
calculate_energy ¶
Calculate the energy of a given state for an arbitrary-order Ising/binary model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
array - like(str, list, tuple, array)
|
State configuration. Can be: - Binary: "011", [0,1,1], (0,1,1), etc. - Spin: [-1,1,1], (-1,1,1), etc. |
required |
couplings
|
list of numpy arrays
|
List of coupling tensors where: - 1D arrays represent linear terms (h_i) - 2D arrays represent quadratic terms (J_ij) - 3D arrays represent cubic terms, etc. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
Total energy of the state
|
|
Source code in src/qemcmc/model/energy_model.py
get_subgroup_couplings ¶
Calculates local couplings for a subgroup of spins.
Spins outside the specified subgroup are treated as frozen constants, and their values are absorbed into the couplings of the subgroup. This is useful for calculating local effective Hamiltonians.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subgroup
|
List[int]
|
A list of indices of the spins in the subgroup. |
required |
current_state
|
str
|
The current state of the full system, as a binary string. |
required |
coupling_weights
|
List[float]
|
Optional weights for the couplings. This can be used to effectively remove certain couplings from the proposal (e.g., constraints) by setting their weight to 0. It is important not to normalize the couplings after applying these weights. |
None
|
Returns:
| Type | Description |
|---|---|
List[ndarray]
|
A new list of coupling tensors for the subgroup. |
Notes
It might seem off to do the reweighting at this point, but here are reasons why I [SF] did it!
Basically, when you get the subgroup couplings, the terms jumble up, so the returned local
coupling list necessarily does not respect the order etc. of the different terms in the original.
Source code in src/qemcmc/model/energy_model.py
calculate_alpha ¶
Compute alpha = sqrt(n) / sqrt(sum of squares of UNIQUE coupling coefficients), assuming coupling tensors are symmetric representations.
Any non-symmetric 2-body input raises ValueError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of spins. |
required |
couplings
|
list[ndarray]
|
Couplings to use. Defaults to self.couplings if not provided. |
None
|
eps
|
float
|
Small threshold to avoid division by zero. |
1e-15
|
Returns:
| Type | Description |
|---|---|
np.ndarray :
|
An array of normalising factors for each term in the couplings list. |
Source code in src/qemcmc/model/energy_model.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | |
get_energy ¶
Returns the energy of a given state
Source code in src/qemcmc/model/energy_model.py
get_all_energies ¶
Calculate the energies for all possible spin states. This method generates all possible spin states for the system and calculates the energy for each state.
Returns:
| Type | Description |
|---|---|
np.ndarray :
|
An array containing the energies of all possible spin states. |
Source code in src/qemcmc/model/energy_model.py
get_lowest_energies ¶
Retrieve the lowest energy states and their degeneracies. This method computes all possible energies and then finds the specified number of lowest energy states along with their degeneracies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_states
|
int
|
|
required |
return_configurations
|
bool
|
|
False
|
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, ndarray]
|
|
Notes
This method is intended for small instances due to its brute-force nature, which is extremely memory intensive and slow.
Source code in src/qemcmc/model/energy_model.py
find_lowest_values ¶
Find the lowest unique values in an array and their degeneracies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr
|
ndarray
|
|
required |
num_values
|
int
|
|
5
|
Returns:
| Type | Description |
|---|---|
Tuple[lowest_values(ndarray), degeneracy(ndarray)]
|
|
Source code in src/qemcmc/model/energy_model.py
get_lowest_energy ¶
Calculate and return the lowest energy from all possible energies. This method uses a brute force approach to find the lowest energy, making it extremely memory intensive and slow. It is recommended to use this method only for small instances.
Returns:
| Name | Type | Description |
|---|---|---|
float |
The lowest energy value.
|
|
Notes
If the lowest energy has already been calculated and stored
in self.lowest_energy, it will return that value directly
to save computation time.
Source code in src/qemcmc/model/energy_model.py
get_boltzmann_factor ¶
Get un-normalised boltzmann probability of a given state
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
str
|
configuration of spins for which probability is to be calculated |
required |
beta
|
float
|
inverse temperature (1/T) at which the probability is to be calculated. |
1.0
|
Returns:
| Type | Description |
|---|---|
float corresponding to the un-normalised boltzmann probability of the given state.
|
|
Source code in src/qemcmc/model/energy_model.py
get_boltzmann_factor_from_energy ¶
Get un-normalized Boltzmann probability for a given energy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
E
|
float
|
Energy for which the Boltzmann factor is to be calculated. |
required |
beta
|
float
|
Inverse temperature (1/T) at which the probability is to be calculated. |
1.0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The un-normalized Boltzmann probability for the given energy. |
Source code in src/qemcmc/model/energy_model.py
ModelMaker ¶
Utility class for constructing standard Ising energy models used in simulations and experiments.
This class constructs predefined random energy models used for testing
and experimentation. Depending on the chosen model_type, it generates
coupling tensors and initialises an :class:EnergyModel instance.
Source code in src/qemcmc/model/model_maker.py
make_fully_connected_binary ¶
Transforms the existing Ising couplings into an mathematically equivalent QUBO model via s = 2x - 1.
Source code in src/qemcmc/model/model_maker.py
ConstraintModel ¶
Bases: EnergyModel
A subclass of EnergyModel that incorporates a constraint function to define valid configurations.
The constraint function takes a state as input and returns True if the state is valid (satisfies the constraint) and False otherwise. The energy of invalid states is set to infinity, effectively excluding them from the Boltzmann distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of spins in the model. |
required |
constraint_couplings
|
list[ndarray]
|
List of coupling tensors (numpy arrays) defining the constraint. |
required |
constraint_signs
|
list[float]
|
Sign convention(s) for the constraint couplings. |
required |
couplings
|
list[ndarray]
|
List of coupling tensors (numpy arrays) defining the energy function. |
required |
constraint_func
|
Callable[[str], bool]
|
A function that takes a state (string representation of spin configuration) and returns True if the state satisfies the constraint, and False otherwise. |
required |
name
|
str
|
Optional label for the model (used in plotting / logging). |
required |
cost_function_signs
|
list[float]
|
Sign convention(s) used by downstream components (e.g. proposal/acceptance conventions). |
required |
model_type
|
str
|
Type of model, either 'ising' or 'binary'. This determines how the binary states are interpreted and how the energy is calculated. 'ising' models use spin values {-1, +1}, while 'binary' models use binary values {0, 1}. |
required |
Notes
- The energy of any state that does not satisfy the constraint is set to infinity, which means such states will have zero probability in the Boltzmann distribution.
- This class can be used to model systems with hard constraints on the configurations, such as certain combinatorial optimization problems or physical systems with forbidden states.
Source code in src/qemcmc/model/constraint_model.py
get_initial_states_constraint ¶
Generates a list of random initial states that satisfy the constraint function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_initial_states
|
int
|
The number of initial states to generate. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
A list of random initial states that are valid according to the constraint. |
Source code in src/qemcmc/model/constraint_model.py
get_constraint_energy ¶
Calculate the energy contribution from the constraint couplings for a given state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
str
|
The state for which to calculate the constraint energy. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The energy contribution from the constraint couplings for the given state. |
Source code in src/qemcmc/model/constraint_model.py
get_total_energy ¶
Calculate the total energy of a given state, including both the regular energy and the constraint energy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
str
|
The state for which to calculate the total energy. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The total energy of the given state, including contributions from both the regular couplings and the constraint couplings. |