Binarg Polynomial Model

Contents

Binarg Polynomial Model#

namespace cimod#

Typedefs

template<typename IndexType, typename FloatType>
using Polynomial = std::unordered_map<std::vector<IndexType>, FloatType, vector_hash>#

Type alias for the polynomial interactions as std::unordered_map.

Template Parameters:
  • IndexType

  • FloatType

template<typename IndexType>
using PolynomialKeyList = std::vector<std::vector<IndexType>>#

Type alias for the indices of the polynomial interactions (namely, the list of keys of the polynomial interactions as std::unordered_map) as std::vector<std::vector>>.

Template Parameters:

IndexType

template<typename FloatType>
using PolynomialValueList = std::vector<FloatType>#

Type alias for the values of the polynomial interactions (namely, the list of values of the polynomial interactions as std::unordered_map) as std::vector.

Template Parameters:

FloatType

template<typename IndexType>
using Sample = std::unordered_map<IndexType, int32_t>#

Type alias for sample, which represents the spin or binary configurations.

Type alias for sample.

Template Parameters:

IndexType

template<typename IndexType, typename FloatType>
class BinaryPolynomialModel#

Class for BinaryPolynomialModel.

Template Parameters:
  • IndexType

  • FloatType

Public Functions

inline BinaryPolynomialModel(const Polynomial<IndexType, FloatType> &poly_map, const Vartype vartype)#

BinaryPolynomialModel constructor.

Parameters:
  • poly_map

  • vartype

inline BinaryPolynomialModel(PolynomialKeyList<IndexType> &key_list, const PolynomialValueList<FloatType> &value_list, const Vartype vartype)#

BinaryPolynomialModel constructor.

Parameters:
  • key_list

  • value_list

  • vartype

inline BinaryPolynomialModel(const PolynomialKeyList<IndexType> &key_list, const PolynomialValueList<FloatType> &value_list, const Vartype vartype)#

BinaryPolynomialModel constructor.

Parameters:
  • key_list

  • value_list

  • vartype

inline BinaryPolynomialModel(const std::vector<IndexType> &variables, const PolynomialKeyList<std::size_t> &poly_key_distance_list, const PolynomialValueList<FloatType> &poly_value_list, const Vartype vartype)#

BinaryPolynomialModel constructor.

This constructor dose not check the input arguments are valid.

Parameters:
  • variables

  • poly_key_distance_list

  • poly_value_list

  • vartype

inline Polynomial<IndexType, FloatType> GetPolynomial() const#

Get the Polynomial object.

Returns:

Polynomial object as std::unordered_map.

inline FloatType GetPolynomial(std::vector<IndexType> &key) const#

Get the specific value of the interaction according to the key representing the indices of the polynomial interactions.

If the interaction corresponding to the key dose not exist, return 0

Parameters:

key

Returns:

Corresponding value of the interaction

inline FloatType GetPolynomial(const std::vector<IndexType> &key) const#

Get the specific value of the interaction according to the key representing the indices of the polynomial interactions.

If the interaction corresponding to the key dose not exist, return 0

Parameters:

key

Returns:

Corresponding value of the interaction

inline const std::unordered_map<IndexType, int64_t> &GetVariablesToIntegers()#

Get variables_to_integers object.

This function may need O(N) calculation time (N is the number of the variables).

Returns:

variables_to_integers object, which represents the correspondence from variables to integer numbers

inline std::unordered_map<IndexType, int64_t> GetVariablesToIntegers() const#

Get variables_to_integers object.

This function may need O(N) calculation time (N is the number of the variables).

Returns:

variables_to_integers, which represents the correspondence from variables to integer numbers

inline int64_t GetVariablesToIntegers(const IndexType &index)#

Get the specific integer number corresponding to the input variable (index).

This function may need O(N) calculation time (N is the number of the variables).

Parameters:

index

Returns:

Non-negative integer number if the input variable is in the BinaryPolynomialModel, else -1

inline int64_t GetVariablesToIntegers(const IndexType &index) const#

Get the specific integer number corresponding to the input variable (index).

This function may need O(N) calculation time (N is the number of the variables).

Parameters:

index

Returns:

Non-negative integer number if the input variable is in the BinaryPolynomialModel, else -1

inline const PolynomialKeyList<IndexType> &GetKeyList() const#

Get the PolynomialKeyList object.

Returns:

PolynomialKeyList object as std::vector<std::vector>>.

inline const PolynomialValueList<FloatType> &GetValueList() const#

Get the PolynomialValueList object.

Returns:

PolynomialValueList object as std::vector.

inline const std::unordered_map<std::vector<IndexType>, std::size_t, vector_hash> &GetKeysInv() const#

Get The inverse key list, which indicates the index of the poly_key_list_ and poly_value_list_.

Returns:

The inverse key list.

inline const std::unordered_set<IndexType> &GetVariables() const#

Return the variables as std::unordered_set.

Returns:

variables

inline const std::vector<IndexType> &GetSortedVariables()#

Return the sorted variables as std::vector.

This function may need O(N) calculation time (N is the number of the variables).

Returns:

sorted variables as std::vector.

inline std::vector<IndexType> GetSortedVariables() const#

Return the sorted variables as std::vector.

This function may need O(N) calculation time (N is the number of the variables).

Returns:

sorted variables as std::vector.

inline std::size_t GetDegree() const#

Return the maximum degree of interaction.

Returns:

degree

inline FloatType GetOffset() const#

Return the offset.

Returns:

The offset

inline Vartype GetVartype() const#

Return the vartype.

Returns:

The vartype

inline std::size_t GetNumInteractions() const#

Return the number of the interactions.

Returns:

The number of the interactions.

inline std::size_t GetNumVariables() const#

Return the number of variables.

Returns:

The number of the variables.

inline BinaryPolynomialModel Empty(const Vartype vartype) const#

Create an empty BinaryPolynomialModel.

Parameters:

vartype

Returns:

The empty BinaryPolynomialModel.

inline void Clear()#

Clear the BinaryPolynomialModel.

inline void RemoveInteraction(std::vector<IndexType> &key)#

Remove the specified interaction from the BinaryPolynomialModel.

Parameters:

key

inline void RemoveInteraction(const std::vector<IndexType> &key)#

Remove the specified interaction from the BinaryPolynomialModel.

Parameters:

key

inline void RemoveInteractionsFrom(PolynomialKeyList<IndexType> &key_list)#

Remove the specified interactions from the BinaryPolynomialModel.

Parameters:

key_list

inline void RemoveInteractionsFrom(const PolynomialKeyList<IndexType> &key_list)#

Remove the specified interactions from the BinaryPolynomialModel.

Parameters:

key_list

inline void RemoveOffset()#

Set the offset of the BinaryPolynomialModel to zero.

inline void RemoveVariable(const IndexType &index)#

Remove a variable from the BinaryPolynomialModel.

Parameters:

index

inline void RemoveVariablesFrom(const std::vector<IndexType> &key)#

Remove the specified variables from the BinaryPolynomialModel.

Parameters:

key

inline void AddInteraction(std::vector<IndexType> &key, const FloatType &value, const Vartype vartype = Vartype::NONE)#

Add an interaction to the BinaryPolynomialModel.

Parameters:
  • key

  • value

  • vartype

inline void AddInteraction(const std::vector<IndexType> &key, const FloatType &value, const Vartype vartype = Vartype::NONE)#

Add an interaction to the BinaryPolynomialModel.

Parameters:
  • key

  • value

  • vartype

inline void AddInteractionsFrom(const Polynomial<IndexType, FloatType> &poly_map, const Vartype vartype = Vartype::NONE)#

Add interactions to the BinaryPolynomialModel.

Parameters:
  • poly_map

  • vartype

inline void AddInteractionsFrom(PolynomialKeyList<IndexType> &key_list, const PolynomialValueList<FloatType> &value_list, const Vartype vartype = Vartype::NONE)#

Add interactions to the BinaryPolynomialModel.

Parameters:
  • key_list

  • value_list

  • vartype

inline void AddInteractionsFrom(const PolynomialKeyList<IndexType> &key_list, const PolynomialValueList<FloatType> &value_list, const Vartype vartype = Vartype::NONE)#

Add interactions to the BinaryPolynomialModel.

Parameters:
  • key_list

  • value_list

  • vartype

inline void AddOffset(FloatType offset)#

Add specified value to the offset of the BinaryPolynomialModel.

Parameters:

offset

inline FloatType Energy(const Sample<IndexType> &sample, bool omp_flag = true) const#

Determine the energy of the specified sample of the BinaryPolynomialModel.

When omp_flag is true, the OpenMP is used to calculate the energy in parallel.

Parameters:
  • sample

  • omp_flag

Returns:

An energy with respect to the sample.

inline FloatType Energy(const std::vector<int32_t> &sample_vec, bool omp_flag = true)#

Determine the energy of the specified sample_vec (as std::vector) of the BinaryPolynomialModel.

When omp_flag is true, the OpenMP is used to calculate the energy in parallel.

Parameters:
  • sample_vec

  • omp_flag

Returns:

An energy with respect to the sample.

inline PolynomialValueList<FloatType> Energies(const std::vector<Sample<IndexType>> &samples) const#

Determine the energies of the given samples.

Parameters:

samples

Returns:

Energies with respect to the samples as std::vector

inline PolynomialValueList<FloatType> Energies(const std::vector<std::vector<int32_t>> &samples_vec)#

Determine the energies of the given samples_vec.

Parameters:

samples_vec

Returns:

Energies with respect to the samples as std::vector

inline void Scale(const FloatType scalar, const PolynomialKeyList<IndexType> &ignored_interactions = {}, const bool ignored_offset = false)#

Multiply by the specified scalar all the values of the interactions of the BinaryPolynomialModel.

Parameters:
  • scalar

  • ignored_interactions

  • ignored_offset

inline void normalize(const std::pair<FloatType, FloatType> &range = {1.0, 1.0}, const PolynomialKeyList<IndexType> &ignored_interactions = {}, const bool ignored_offset = false)#

Normalizes the values of the interactions of the BinaryPolynomialModel such that they fall in the provided range(s).

Parameters:
  • range

  • ignored_interactions

  • ignored_offset

inline BinaryPolynomialModel ChangeVartype(const Vartype vartype, const bool inplace)#

Create a BinaryPolynomialModel with the specified vartype.

Parameters:
  • vartype

  • inplace

Returns:

A new instance of the BinaryPolynomialModel.

inline void ChangeVartype(const Vartype vartype)#

Change the vartype of the BinaryPolynomialModel.

inline bool HasVariable(const IndexType &index)#

Check if the specified index is in the BinaryPolynomialModel.

Parameters:

index

Returns:

true or false

inline Polynomial<IndexType, FloatType> ToHubo() const#

Generate the polynomial interactions corresponding to the vartype being BINARY from the BinaryPolynomialModel.

Returns:

The polynomial interaction as std::unordered_map.

inline Polynomial<IndexType, FloatType> ToHising() const#

Generate the polynomial interactions corresponding to the vartype being SPIN from the BinaryPolynomialModel.

Returns:

The polynomial interaction as std::unordered_map.

inline nlohmann::json ToSerializable() const#

Convert the BinaryPolynomialModel to a serializable object.

Returns:

An object that can be serialized (nlohmann::json)

Public Static Functions

template<typename IndexType_serial = IndexType, typename FloatType_serial = FloatType>
static inline BinaryPolynomialModel<IndexType_serial, FloatType_serial> FromSerializable(const nlohmann::json &input)#

Create a BinaryPolynomialModel instance from a serializable object.

Template Parameters:
  • IndexType_serial

  • FloatType_serial

Parameters:

input

Returns:

BinaryPolynomialModel instance

static inline BinaryPolynomialModel FromHubo(const Polynomial<IndexType, FloatType> &poly_map)#

Create a BinaryPolynomialModel from a Hubo model.

Parameters:

poly_map

Returns:

BinaryPolynomialModel instance with the vartype being BINARY.

static inline BinaryPolynomialModel FromHubo(const PolynomialKeyList<IndexType> &key_list, const PolynomialValueList<FloatType> &value_list)#

Create a BinaryPolynomialModel from a Hubo model.

Parameters:
  • key_list

  • value_list

Returns:

BinaryPolynomialModel instance with the vartype being BINARY.

static inline BinaryPolynomialModel FromHubo(PolynomialKeyList<IndexType> &key_list, const PolynomialValueList<FloatType> &value_list)#

Create a BinaryPolynomialModel from a Hubo model.

Parameters:
  • key_list

  • value_list

Returns:

BinaryPolynomialModel instance with the vartype being BINARY.

static inline BinaryPolynomialModel FromHising(const Polynomial<IndexType, FloatType> &poly_map)#

Create a BinaryPolynomialModel from a higher ordere Ising model.

Parameters:

poly_map

Returns:

BinaryPolynomialModel instance with the vartype being SPIN.

static inline BinaryPolynomialModel FromHising(const PolynomialKeyList<IndexType> &key_list, const PolynomialValueList<FloatType> &value_list)#

Create a BinaryPolynomialModel from a higher ordere Ising model.

Parameters:
  • key_list

  • value_list

Returns:

BinaryPolynomialModel instance with the vartype being SPIN.

static inline BinaryPolynomialModel FromHising(PolynomialKeyList<IndexType> &key_list, const PolynomialValueList<FloatType> &value_list)#

Create a BinaryPolynomialModel from a higher ordere Ising model.

Parameters:
  • key_list

  • value_list

Returns:

BinaryPolynomialModel instance with the vartype being SPIN.

Protected Functions

inline void SetKeyAndValue(const std::vector<IndexType> &key, const FloatType &value)#

Set key and value.

Note that the key is assumed to be sorted.

Parameters:
  • key

  • value

inline std::size_t IntegerPower(std::size_t base, std::size_t exponent) const#

Caluculate the base to the power of exponent (std::pow(base, exponent) is too slow).

Parameters:
  • base

  • exponent

Returns:

The base to the power of exponent

inline std::vector<IndexType> GenerateChangedKey(const std::vector<IndexType> &original_key, const std::size_t num_of_key) const#

Generate the num_of_key-th the key when the vartype is changed.

Parameters:
  • original_key

  • num_of_key

Returns:

The changed key

inline BinaryPolynomialModel ToSpin() const#

Generate BinaryPolynomialModel with the vartype being SPIN.

Returns:

BinaryPolynomialModel instance with the vartype being SPIN.

inline BinaryPolynomialModel ToBinary() const#

Generate BinaryPolynomialModel with the vartype being BINARY.

Returns:

BinaryPolynomialModel instance with the vartype being BINARY.

inline void UpdateVariablesToIntegers()#

Update sorted_variables_ and variables_to_integers_.

inline std::unordered_map<IndexType, int64_t> GenerateVariablesToIntegers() const#

Generate variables_to_integers.

Returns:

variables_to_integers

inline std::vector<IndexType> GenerateSortedVariables() const#

Generate sorted variables.

Returns:

sorted_variables

Protected Attributes

std::unordered_set<IndexType> variables_#

Variable list as std::unordered_set.

std::unordered_map<IndexType, std::size_t> each_variable_num_#

The list of the number of the variables appeared in the polynomial interactions as std::unordered_map.

std::unordered_map<IndexType, int64_t> variables_to_integers_#

The correspondence from variables to the integer numbers.

std::vector<IndexType> sorted_variables_#

Sorted variables is represents the correspondence from integer numbers.to the variables.

bool relabel_flag_for_variables_to_integers_ = true#

If true variable_to_index must be relabeled.

PolynomialKeyList<IndexType> poly_key_list_#

The list of the indices of the polynomial interactions (namely, the list of keys of the polynomial interactions as std::unordered_map) as std::vector<std::vector>>.

PolynomialValueList<FloatType> poly_value_list_#

The list of the values of the polynomial interactions (namely, the list of values of the polynomial interactions as std::unordered_map) as std::vector.

std::unordered_map<std::vector<IndexType>, std::size_t, vector_hash> poly_key_inv_#

The inverse key list, which indicates the index of the poly_key_list_ and poly_value_list_.

Vartype vartype_ = Vartype::NONE#

The model’s type. SPIN or BINARY.