27#include <unordered_map>
30#include <cimod/binary_polynomial_model.hpp>
48 static_assert(std::is_floating_point<FloatType>::value,
49 "FloatType must be floating-point type.");
66 const auto &poly_key_list = std::get<0>(v_k_v);
67 const auto &poly_value_list = std::get<1>(v_k_v);
69 if (poly_key_list.size() != poly_value_list.size()) {
70 throw std::runtime_error(
71 "The sizes of key_list and value_list must match each other");
74 int64_t num_interactions =
static_cast<int64_t
>(poly_key_list.size());
79#pragma omp parallel for
80 for (int64_t i = 0; i < num_interactions; ++i) {
85 for (std::size_t i = 0; i < poly_key_list.size(); ++i) {
98 std::sort(key.begin(), key.end());
115 std::vector<Index> copied_key = key;
116 return J(copied_key);
125 std::sort(key.begin(), key.end());
140 std::vector<Index> copied_key = key;
141 return J(copied_key);
151 std::vector<Index> copied_key{(
Index)args...};
152 return J(copied_key);
161 template <
typename... Args>
FloatType J(Args... args)
const {
162 std::vector<Index> copied_key{(
Index)args...};
163 return J(copied_key);
170 cimod::Polynomial<Index, FloatType> poly_map;
180 const cimod::PolynomialKeyList<Index> &
get_keys()
const {
187 const cimod::PolynomialValueList<FloatType> &
get_values()
const {
202 throw std::out_of_range(
"The size of spins/binaries does not equal to "
203 "the size of polynomial graph");
208 int64_t num_interactions =
static_cast<int64_t
>(
poly_key_list_.size());
211#pragma omp parallel for reduction(+ : energy)
212 for (int64_t i = 0; i < num_interactions; ++i) {
213 Spin spin_multiple = 1;
215 spin_multiple *= spins[index];
216 if (spin_multiple == 0.0) {
223 for (int64_t i = 0; i < num_interactions; ++i) {
224 Spin spin_multiple = 1;
226 spin_multiple *= spins[index];
227 if (spin_multiple == 0.0) {
244 return energy(spins, omp_flag);
260 std::unordered_map<std::vector<Index>, std::size_t, cimod::vector_hash>
266 std::stringstream ss;
267 ss <<
"Too small system size. ";
268 ss <<
"The degree of the input polynomial interaction is " << key.size();
269 ss <<
". But the system size is " <<
Graph::size();
270 throw std::runtime_error(ss.str());
272 if (0 < key.size()) {
274 for (std::size_t i = 0; i < key.size() - 1; ++i) {
275 if (key[i] == key[i + 1]) {
276 throw std::runtime_error(
"No self-loops allowed");
279 std::stringstream ss;
280 ss <<
"Too small system size. ";
281 ss <<
"The index of a interaction: " << key[i] <<
" is out of range";
282 throw std::runtime_error(ss.str());
286 std::stringstream ss;
287 ss <<
"Too small system size. ";
288 ss <<
"The index of a interaction: " << key.back()
289 <<
" is out of range";
290 throw std::runtime_error(ss.str());
Graph(std::size_t num_spins)
Graph constructor.
Definition graph.hpp:46
std::size_t size() const noexcept
get number of spins
Definition graph.hpp:96
FloatType energy(const Spins &spins, const bool omp_flag=true) const
Return the total energy corresponding to the input variables, Spins or Binaries.
Definition polynomial.hpp:200
Polynomial(const nlohmann::json &j)
Constructor of Polynomial class to initialize num_variables, and interactions from json.
Definition polynomial.hpp:63
FloatType calc_energy(const Spins &spins, const bool omp_flag=true) const
Return the total energy corresponding to the input variables, Spins or Binaries.
Definition polynomial.hpp:243
FloatType & J(std::vector< Index > &key)
Access the interaction corresponding to the input argument to set an interaction.
Definition polynomial.hpp:97
const cimod::PolynomialKeyList< Index > & get_keys() const
Get the PolynomialKeyList object (all the keys of polynomial interactions).
Definition polynomial.hpp:180
FloatType & J(Args... args)
Access the interaction corresponding to the input argument to set an interaction.
Definition polynomial.hpp:150
FloatType J(Args... args) const
Access the interaction corresponding to the input argument to set an interaction.
Definition polynomial.hpp:161
FloatType & J(const std::vector< Index > &key)
Access the interaction corresponding to the input argument to set an interaction.
Definition polynomial.hpp:114
FloatType J(const std::vector< Index > &key) const
Return the interaction corresponding to the input argument.
Definition polynomial.hpp:139
cimod::PolynomialKeyList< Index > poly_key_list_
The list of the indices of the polynomial interactions (namely, the list of key of the polynomial int...
Definition polynomial.hpp:251
const cimod::PolynomialValueList< FloatType > & get_values() const
Get the PolynomialValueList object (all the values of polynomial interactions).
Definition polynomial.hpp:187
std::unordered_map< std::vector< Index >, std::size_t, cimod::vector_hash > poly_key_inv_
The inverse key list, which indicates the index of the poly_key_list_ and poly_value_list_.
Definition polynomial.hpp:261
Polynomial(const std::size_t num_variables)
Constructor of Polynomial class to initialize variables and vartype.
Definition polynomial.hpp:58
void CheckKeyValid(const std::vector< Index > &key) const
Check if the input keys are valid.
Definition polynomial.hpp:264
FloatType J(std::vector< Index > &key) const
Return the interaction corresponding to the input argument.
Definition polynomial.hpp:124
cimod::Polynomial< Index, FloatType > get_polynomial() const
Generate and return all the polynomial interactions as std::unordered_map<std::vector<Index>,...
Definition polynomial.hpp:169
cimod::PolynomialValueList< FloatType > poly_value_list_
The list of the values of the polynomial interactions (namely, the list of value of the polynomial in...
Definition polynomial.hpp:256
FloatType value_type
Floating-point type.
Definition polynomial.hpp:53
std::size_t get_num_interactions() const
Return the number of all the interactions.
Definition polynomial.hpp:193
Definition binary_polynomial_model.hpp:24
int Spin
Definition graph.hpp:26
std::vector< Spin > Spins
Definition graph.hpp:27
std::size_t Index
Definition graph.hpp:30
auto json_parse_polynomial(const nlohmann::json &obj, const bool relabel=true)
Definition parse.hpp:72
Definition algorithm.hpp:24
double FloatType
Note:
Definition compile_config.hpp:37