31using RowColumn = std::pair<std::size_t, std::size_t>;
64template <
typename FloatType>
class Square :
public Sparse<FloatType> {
65 static_assert(std::is_floating_point<FloatType>::value,
66 "argument must be an arithmetic type");
91 inline std::size_t
mod_r(std::int64_t a)
const {
102 inline std::size_t
mod_c(std::int64_t a)
const {
114 std::int64_t r1, c1, r2, c2;
115 std::tie(r1, c1) =
to_rc(idx1);
116 std::tie(r2, c2) =
to_rc(idx2);
117 if (!((r1 == r2 && std::abs(c1 - c2) == 1) ||
118 (c1 == c2 && std::abs(r1 - r2) == 1) ||
119 (r1 == r2 && c1 == c2))
121 throw std::runtime_error(
"invalid index pair " + std::to_string(idx1) +
122 " " + std::to_string(idx2) +
123 " inserted in Square");
137 if (!(-1 <= r && r <= (std::int64_t)
_num_row)) {
138 throw std::runtime_error(
"invalid value r=" + std::to_string(r) +
139 " inserted in Square");
142 if (!(-1 <= c && c <= (std::int64_t)
_num_column)) {
143 throw std::runtime_error(
"invalid value c=" + std::to_string(c) +
144 " inserted in Square");
159 throw std::runtime_error(
"invalid value index=" + std::to_string(ind) +
160 " inserted in Square");
165 return std::make_pair(r, c);
178 assert(num_row >= 1);
179 assert(num_column >= 1);
181 for (std::size_t r = 0; r <
_num_row; r++) {
194 if (r < num_row - 1) {
199 if (c < num_column - 1) {
218 Square(
const json &j, std::size_t num_row, std::size_t num_column,
221 if (!(j[
"num_variables"] <= num_row * num_column)) {
222 throw std::runtime_error(
"number of system size does not match");
227 for (
auto &&elem : bqm.get_quadratic()) {
228 const auto &key = elem.first;
229 const auto &val = elem.second;
234 for (
auto &&elem : bqm.get_linear()) {
235 const auto &key = elem.first;
236 const auto &val = elem.second;
283 to_ind(r, c),
to_ind(
static_cast<std::int64_t
>(r) - 1, c));
286 to_ind(r, c),
to_ind(r,
static_cast<std::int64_t
>(c) - 1));
289 to_ind(r, c),
to_ind(
static_cast<std::int64_t
>(r) + 1, c));
292 to_ind(r, c),
to_ind(r,
static_cast<std::int64_t
>(c) + 1));
314 to_ind(r, c),
to_ind(
static_cast<std::int64_t
>(r) - 1, c));
317 to_ind(r, c),
to_ind(r,
static_cast<std::int64_t
>(c) - 1));
320 to_ind(r, c),
to_ind(
static_cast<std::int64_t
>(r) + 1, c));
323 to_ind(r, c),
to_ind(r,
static_cast<std::int64_t
>(c) + 1));
369 return spins[
to_ind(r, c)];
382 return spins[
to_ind(r, c)];
std::size_t get_num_spins() const noexcept
get number of spins
Definition graph.hpp:89
FloatType & h(Index i)
access h_{i} (local field)
Definition sparse.hpp:300
FloatType & J(Index i, Index j)
access J_{ij}
Definition sparse.hpp:269
Sparse(std::size_t num_spins, std::size_t num_edges)
Sparse constructor.
Definition sparse.hpp:113
std::size_t mod_r(std::int64_t a) const
mod function (a mod num_row)
Definition square.hpp:91
const FloatType & h(std::size_t r, std::size_t c) const
access h(row, colum) (local field)
Definition square.hpp:352
void _checkpair(Index idx1, Index idx2) const
check if the pair has a valid connection
Definition square.hpp:113
std::size_t get_num_column() const
get number of columns
Definition square.hpp:265
const FloatType & J(std::size_t r, std::size_t c, Dir dir) const
access J(row, colum, direction)
Definition square.hpp:307
FloatType _init_val
initial value to be set to interactions
Definition square.hpp:72
Square(Square< FloatType > &&)=default
square lattice graph move constructor
FloatType & h(std::size_t r, std::size_t c)
access h(row, colum) (local field)
Definition square.hpp:337
std::size_t _num_row
number of rows
Definition square.hpp:77
FloatType & J(std::size_t r, std::size_t c, Dir dir)
access J(row, colum, direction)
Definition square.hpp:276
RowColumn to_rc(Index ind) const
convert from global index to (row x column) index
Definition square.hpp:157
std::size_t mod_c(std::int64_t a) const
mod function (a mod num_column)
Definition square.hpp:102
const Spin & spin(const Spins &spins, std::size_t r, std::size_t c) const
derive spin value at the index (row x column)
Definition square.hpp:381
Square(std::size_t num_row, std::size_t num_column, FloatType init_val=0)
square lattice graph constructor
Definition square.hpp:175
Spin & spin(Spins &spins, std::size_t r, std::size_t c) const
derive spin value at the index (row x column)
Definition square.hpp:368
Square(const Square< FloatType > &)=default
square lattice graph copy constructor
std::size_t _num_column
number of columns
Definition square.hpp:82
Square(const json &j, std::size_t num_row, std::size_t num_column, FloatType init_val=0)
Square constructor (from nlohmann::json).
Definition square.hpp:218
std::size_t get_num_row() const
get number of rows
Definition square.hpp:258
Index to_ind(std::int64_t r, std::int64_t c) const
convert from (row x column) index to global index
Definition square.hpp:136
Definition binary_polynomial_model.hpp:24
int Spin
Definition graph.hpp:26
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::pair< std::size_t, std::size_t > RowColumn
Row x Column type.
Definition square.hpp:31
@ MINUS_C
minus-column direction: (r, c, ind) -> (r, c-1, ind)
Definition chimera.hpp:66
@ MINUS_R
minus-row direction: (r, c, ind) -> (r-1, c, ind)
Definition chimera.hpp:56
@ PLUS_C
plus-column direction: (r, c, ind) -> (r, c+1, ind)
Definition chimera.hpp:61
@ PLUS_R
plus-row direction: (r, c, ind) -> (r+1, c, ind)
Definition chimera.hpp:51
Dir
direction enum class
Definition square.hpp:36
@ MINUS_C
minux-column direction: (r, c) -> (r, c-1)
Definition square.hpp:56
@ MINUS_R
minus-row direction: (r, c) -> (r-1, c)
Definition square.hpp:46
@ PLUS_C
plus-column direction: (r, c) -> (r, c+1)
Definition square.hpp:51
@ PLUS_R
plus-row direction: (r, c) -> (r+1, c)
Definition square.hpp:41
std::size_t Index
Definition graph.hpp:30
Definition algorithm.hpp:24
double FloatType
Note:
Definition compile_config.hpp:37