energy_model
qemcmc.model.energy_model ¶
EnergyModel ¶
EnergyModel(
n,
couplings=[],
name=None,
cost_function_signs=[-1, -1],
model_type="ising",
manual_get_energy=None,
)
A base class for energy models for a classical energy function over n spins, defined by dimod.BinaryPolynomial coupling tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of spins. |
required |
couplings
|
list of dimod.BinaryPolynomial or list of np.ndarray
|
List of dimod.BinaryPolynomial representing interaction tensors. |
[]
|
name
|
str
|
Optional label for the model (used in plotting / logging). |
None
|
cost_function_signs
|
list of float
|
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'
|
manual_get_energy
|
callable
|
Optional custom energy function. If provided, this function will be used to calculate energies instead of the default methods. Must agree on energy values with the default method for a sample of states, otherwise a ValueError is raised. |
None
|
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:
| Type | Description |
|---|---|
list of str
|
A list of random initial states. |
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
|
str, list, tuple, or np.ndarray
|
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 dimod.BinaryPolynomial
|
List of coupling polynomials defining the interaction terms. |
required |
cost_function_signs
|
list of float
|
Sign convention applied to each coupling term. |
None
|
Returns:
| 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 of 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 of 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 of dimod.BinaryPolynomial
|
A new list of coupling polynomials 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 of dimod.BinaryPolynomial
|
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 |
|---|---|
float
|
Normalisation factor for the couplings. |
Source code in src/qemcmc/model/energy_model.py
get_energy ¶
Returns the energy of a given state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
str
|
Binary or spin state string. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Energy of the 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 |
|---|---|
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
|
The number of lowest energy states to retrieve. |
required |
return_configurations
|
bool
|
Whether to also return the corresponding configurations of the lowest energy states. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
lowest_energies |
ndarray
|
The lowest energy values. |
lowest_energy_degeneracy |
ndarray
|
The degeneracies of the corresponding energy values. |
lowest_configs |
(list, optional)
|
List of configurations corresponding to each energy (only returned if return_configurations=True). |
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
|
The input array from which to find the lowest values. |
required |
num_values
|
int
|
The number of lowest unique values to find. Defaults to 5. |
5
|
Returns:
| Name | Type | Description |
|---|---|---|
lowest_values |
ndarray
|
The lowest unique values in the array. |
degeneracy |
ndarray
|
The counts of each of the lowest unique values. |
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:
| 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. Defaults to 1.0. |
1.0
|
Returns:
| Type | Description |
|---|---|
float
|
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. Defaults to 1.0. |
1.0
|
Returns:
| Type | Description |
|---|---|
float
|
The un-normalized Boltzmann probability for the given energy. |