21#include <unordered_map>
41 static_assert(std::is_floating_point<FloatType>::value,
42 "FloatType must be floating-point type.");
88 if (std::find(nodes.begin(), nodes.end(), to) == nodes.end()) {
113 Sparse(std::size_t num_spins, std::size_t num_edges)
122 explicit Sparse(std::size_t num_spins) :
Sparse(num_spins, num_spins) {}
131 :
Sparse(static_cast<std::size_t>(j[
"num_variables"]), num_edges) {
134 using SparseMatrix = Eigen::SparseMatrix<FloatType, Eigen::RowMajor>;
135 using SpIter =
typename SparseMatrix::InnerIterator;
153 SparseMatrix quadmat = bqm.interaction_matrix();
154 size_t num_variables = quadmat.rows() - 1;
155 for (
int k = 0; k < quadmat.outerSize(); k++) {
156 for (SpIter it(quadmat, k); it; ++it) {
161 if (r == num_variables && c == num_variables)
164 if (c == num_variables) {
219 return this->
energy(spins);
223 Eigen::ColMajor> &spins)
const {
224 return this->
energy(spins);
235 if (!(spins.size() == this->get_num_spins())) {
236 std::out_of_range(
"Out of range in energy in Sparse graph.");
240 for (std::size_t ind = 0; ind < this->
get_num_spins(); ind++) {
243 ret += (1. / 2) * this->
J(ind, adj_ind) * spins[ind] * spins[adj_ind];
245 ret += this->
h(ind) * spins[ind];
253 Eigen::ColMajor> &spins)
const {
255 for (
size_t i = 0; i < temp_spins.size(); i++) {
256 temp_spins[i] = spins(i);
258 return energy(temp_spins);
276 return _J[std::make_pair(std::min(i, j), std::max(i, j))];
290 return _J.at(std::make_pair(std::min(i, j), std::max(i, j)));
303 return _J[std::make_pair(i, i)];
315 return _J.at(std::make_pair(i, i));
Graph(std::size_t num_spins)
Graph constructor.
Definition graph.hpp:46
std::size_t get_num_spins() const noexcept
get number of spins
Definition graph.hpp:89
Sparse(const json &j)
Sparse constructor (from nlohmann::json).
Definition sparse.hpp:180
FloatType calc_energy(const Spins &spins) const
calculate total energy
Definition sparse.hpp:218
FloatType & h(Index i)
access h_{i} (local field)
Definition sparse.hpp:300
const FloatType & J(Index i, Index j) const
access J_{ij}
Definition sparse.hpp:287
std::size_t get_num_edges() const
get number of edges
Definition sparse.hpp:208
Sparse(const json &j, std::size_t num_edges)
Sparse constructor (from nlohmann::json).
Definition sparse.hpp:130
Interactions _J
interactions (the number of intereactions is num_spins*(num_spins+1)/2)
Definition sparse.hpp:61
Sparse(const Sparse< FloatType > &)=default
Sparse copy constructor.
Sparse(std::size_t num_spins)
Sparse delegate constructor.
Definition sparse.hpp:122
bool set_adj_node(Index from, Index to)
add adjacent node from "from" Index to "to" Index
Definition sparse.hpp:81
FloatType value_type
float type
Definition sparse.hpp:54
FloatType & J(Index i, Index j)
access J_{ij}
Definition sparse.hpp:269
std::unordered_map< std::pair< Index, Index >, FloatType, utility::PairHash > Interactions
interaction type
Definition sparse.hpp:48
FloatType calc_energy(const Eigen::Matrix< FloatType, Eigen::Dynamic, 1, Eigen::ColMajor > &spins) const
Definition sparse.hpp:222
Sparse(Sparse< FloatType > &&)=default
Sparse move constructor.
const Nodes & adj_nodes(Index ind) const
list of adjacent nodes
Definition sparse.hpp:201
FloatType energy(const Eigen::Matrix< FloatType, Eigen::Dynamic, 1, Eigen::ColMajor > &spins) const
Definition sparse.hpp:252
std::size_t _num_edges
the uppder limit of the number of edges per site
Definition sparse.hpp:66
const FloatType & h(Index i) const
access h_{i} (local field)
Definition sparse.hpp:313
Sparse(std::size_t num_spins, std::size_t num_edges)
Sparse constructor.
Definition sparse.hpp:113
std::vector< Nodes > _list_adj_nodes
the list of the indices of adjacent nodes
Definition sparse.hpp:71
FloatType energy(const Spins &spins) const
calculate total energy
Definition sparse.hpp:234
Definition binary_polynomial_model.hpp:24
std::vector< Spin > Spins
Definition graph.hpp:27
nlohmann::json json
Definition parse.hpp:33
auto json_parse(const json &obj, bool relabel=true)
parse json object from bqm.to_serializable
Definition parse.hpp:50
std::vector< Index > Nodes
Definition graph.hpp:32
std::size_t Index
Definition graph.hpp:30
Definition algorithm.hpp:24
double FloatType
Note:
Definition compile_config.hpp:37
hash class for std::pair
Definition pairhash.hpp:30