openjij
Framework for the Ising model and QUBO.
Loading...
Searching...
No Matches
square.hpp
Go to the documentation of this file.
1// Copyright 2023 Jij Inc.
2
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6
7// http://www.apache.org/licenses/LICENSE-2.0
8
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <cstddef>
18#include <cstdint>
19#include <type_traits>
20#include <utility>
21#include <exception>
22
24
25namespace openjij {
26namespace graph {
27
31using RowColumn = std::pair<std::size_t, std::size_t>;
32
36enum class Dir {
37
41 PLUS_R,
42
46 MINUS_R,
47
51 PLUS_C,
52
56 MINUS_C,
57};
58
64template <typename FloatType> class Square : public Sparse<FloatType> {
65 static_assert(std::is_floating_point<FloatType>::value,
66 "argument must be an arithmetic type");
67
68private:
73
77 std::size_t _num_row;
78
82 std::size_t _num_column;
83
91 inline std::size_t mod_r(std::int64_t a) const {
92 return (a + _num_row) % _num_row;
93 }
94
102 inline std::size_t mod_c(std::int64_t a) const {
103 return (a + _num_column) % _num_column;
104 }
105
113 inline void _checkpair(Index idx1, Index idx2) 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) || // horizontal connect
118 (c1 == c2 && std::abs(r1 - r2) == 1) || // vertical connect
119 (r1 == r2 && c1 == c2)) // local field
120 ) {
121 throw std::runtime_error("invalid index pair " + std::to_string(idx1) +
122 " " + std::to_string(idx2) +
123 " inserted in Square");
124 }
125 }
126
127public:
136 Index to_ind(std::int64_t r, std::int64_t c) const {
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");
140 }
141
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");
145 }
146
147 return _num_column * mod_r(r) + mod_c(c);
148 }
149
158 if (!(ind < this->get_num_spins())) {
159 throw std::runtime_error("invalid value index=" + std::to_string(ind) +
160 " inserted in Square");
161 }
162
163 std::int64_t r = ind / _num_column;
164 std::int64_t c = ind % _num_column;
165 return std::make_pair(r, c);
166 }
167
175 Square(std::size_t num_row, std::size_t num_column, FloatType init_val = 0)
178 assert(num_row >= 1);
179 assert(num_column >= 1);
180
181 for (std::size_t r = 0; r < _num_row; r++) {
182 for (std::size_t c = 0; c < _num_column; c++) {
183 // open boundary
184 if (r > 0) {
185 // MINUS_R
186 this->Sparse<FloatType>::J(to_ind(r, c), to_ind(r - 1, c)) =
187 _init_val;
188 }
189 if (c > 0) {
190 // MINUS_C
191 this->Sparse<FloatType>::J(to_ind(r, c), to_ind(r, c - 1)) =
192 _init_val;
193 }
194 if (r < num_row - 1) {
195 // PLUS_R
196 this->Sparse<FloatType>::J(to_ind(r, c), to_ind(r + 1, c)) =
197 _init_val;
198 }
199 if (c < num_column - 1) {
200 // PLUS_C
201 this->Sparse<FloatType>::J(to_ind(r, c), to_ind(r, c + 1)) =
202 _init_val;
203 }
204 // local field
206 }
207 }
208 }
209
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");
223 }
224 // define bqm with ising variables
226 // interactions
227 for (auto &&elem : bqm.get_quadratic()) {
228 const auto &key = elem.first;
229 const auto &val = elem.second;
230 _checkpair(key.first, key.second);
231 this->Sparse<FloatType>::J(key.first, key.second) += val;
232 }
233 // local field
234 for (auto &&elem : bqm.get_linear()) {
235 const auto &key = elem.first;
236 const auto &val = elem.second;
237 this->Sparse<FloatType>::h(key) += val;
238 }
239 }
240
245 Square(const Square<FloatType> &) = default;
246
252
258 std::size_t get_num_row() const { return _num_row; }
259
265 std::size_t get_num_column() const { return _num_column; }
266
276 FloatType &J(std::size_t r, std::size_t c, Dir dir) {
277 assert(r < _num_row);
279
280 switch (dir) {
281 case Dir::MINUS_R:
282 return this->Sparse<FloatType>::J(
283 to_ind(r, c), to_ind(static_cast<std::int64_t>(r) - 1, c));
284 case Dir::MINUS_C:
285 return this->Sparse<FloatType>::J(
286 to_ind(r, c), to_ind(r, static_cast<std::int64_t>(c) - 1));
287 case Dir::PLUS_R:
288 return this->Sparse<FloatType>::J(
289 to_ind(r, c), to_ind(static_cast<std::int64_t>(r) + 1, c));
290 case Dir::PLUS_C:
291 return this->Sparse<FloatType>::J(
292 to_ind(r, c), to_ind(r, static_cast<std::int64_t>(c) + 1));
293 default:
294 return _init_val;
295 }
296 }
297
307 const FloatType &J(std::size_t r, std::size_t c, Dir dir) const {
308 assert(r < _num_row);
310
311 switch (dir) {
312 case Dir::MINUS_R:
313 return this->Sparse<FloatType>::J(
314 to_ind(r, c), to_ind(static_cast<std::int64_t>(r) - 1, c));
315 case Dir::MINUS_C:
316 return this->Sparse<FloatType>::J(
317 to_ind(r, c), to_ind(r, static_cast<std::int64_t>(c) - 1));
318 case Dir::PLUS_R:
319 return this->Sparse<FloatType>::J(
320 to_ind(r, c), to_ind(static_cast<std::int64_t>(r) + 1, c));
321 case Dir::PLUS_C:
322 return this->Sparse<FloatType>::J(
323 to_ind(r, c), to_ind(r, static_cast<std::int64_t>(c) + 1));
324 default:
325 return _init_val;
326 }
327 }
328
337 FloatType &h(std::size_t r, std::size_t c) {
338 assert(r < _num_row);
340
341 return this->Sparse<FloatType>::h(to_ind(r, c));
342 }
343
352 const FloatType &h(std::size_t r, std::size_t c) const {
353 assert(r < _num_row);
355
356 return this->Sparse<FloatType>::h(to_ind(r, c));
357 }
358
368 Spin &spin(Spins &spins, std::size_t r, std::size_t c) const {
369 return spins[to_ind(r, c)];
370 }
371
381 const Spin &spin(const Spins &spins, std::size_t r, std::size_t c) const {
382 return spins[to_ind(r, c)];
383 }
384};
385
386} // namespace graph
387} // namespace openjij
std::size_t get_num_spins() const noexcept
get number of spins
Definition graph.hpp:89
Sparse graph: two-body intereactions with O(1) connectivity The Hamiltonian is like.
Definition sparse.hpp:40
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
square lattice graph
Definition square.hpp:64
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
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)
@ MINUS_R
minus-row direction: (r, c, ind) -> (r-1, c, ind)
@ PLUS_C
plus-column direction: (r, c, ind) -> (r, c+1, ind)
@ PLUS_R
plus-row direction: (r, c, ind) -> (r+1, c, ind)
std::vector< Spin > Spins
Definition graph.hpp:27
int Spin
Definition graph.hpp:26
Dir
direction enum class
Definition square.hpp:36
@ MINUS_C
minux-column direction: (r, c) -> (r, c-1)
@ MINUS_R
minus-row direction: (r, c) -> (r-1, c)
@ PLUS_C
plus-column direction: (r, c) -> (r, c+1)
@ PLUS_R
plus-row direction: (r, c) -> (r+1, c)
nlohmann::json json
Definition parse.hpp:33
std::size_t Index
Definition graph.hpp:30
Definition algorithm.hpp:24
double FloatType
Note:
Definition compile_config.hpp:37