openjij
Framework for the Ising model and QUBO.
Loading...
Searching...
No Matches
single_spin_flip.hpp
Go to the documentation of this file.
1// Copyright 2023 Jij Inc.
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5
6// http://www.apache.org/licenses/LICENSE-2.0
7
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#pragma once
15
16#include <random>
17#include <type_traits>
18
23
24#ifdef USE_OMP
25#include <omp.h>
26#endif
27
28namespace openjij {
29namespace updater {
30
36template <typename System> struct SingleSpinFlip;
37
43template <typename GraphType>
44struct SingleSpinFlip<system::ClassicalIsing<GraphType>> {
45
50
54 using FloatType = typename GraphType::value_type;
55
66 template <typename RandomNumberEngine>
67 inline static void
68 update(ClIsing &system, RandomNumberEngine &random_number_engine,
69 const utility::ClassicalUpdaterParameter &parameter) {
70 // set probability distribution object
71 // to do Metroopolis
72 auto urd = std::uniform_real_distribution<>(0, 1.0);
73
74 Eigen::setNbThreads(1);
75 Eigen::initParallel();
76
77 // do a iteraction except for the auxiliary spin
78 for (std::size_t index = 0; index < system.num_spins; ++index) {
79
80 if (system.dE(index) <= 0 ||
81 std::exp(-parameter.beta * system.dE(index)) >
82 urd(random_number_engine)) {
83 // update dE
84 system.dE += 4 * system.spin(index) *
85 (system.interaction.row(index).transpose().cwiseProduct(
86 system.spin));
87
88 system.dE(index) *= -1;
89 system.spin(index) *= -1;
90 }
91
92 // assure that the dummy spin is not changed.
93 system.spin(system.num_spins) = 1;
94 }
95 }
96};
97
104template <typename GraphType>
105struct SingleSpinFlip<system::TransverseIsing<GraphType>> {
106
111
115 using FloatType = typename GraphType::value_type;
116
127 template <typename RandomNumberEngine>
128 inline static void
129 update(QIsing &system, RandomNumberEngine &random_number_engine,
131
132 // get number of classical spins
133 std::size_t num_classical_spins = system.num_classical_spins;
134 // get number of trotter slices
135 std::size_t num_trotter_slices = system.trotter_spins.cols();
136
137 // do metropolis
138 auto urd = std::uniform_real_distribution<>(0, 1.0);
139
140 // aliases
141 // const auto& spins = system.trotter_spins;
142 const auto &gamma = system.gamma;
143 const auto &beta = parameter.beta;
144 const auto &s = parameter.s;
145
146 // transverse factor
147 FloatType B =
148 (1 / 2.) * log(tanh(beta * gamma * (1.0 - s) / num_trotter_slices));
149
150 // Eigen's thread settings are process-wide. When independent systems run
151 // in parallel (run_systems_in_parallel), the caller configures Eigen once
152 // before the workers start, so workers must not write these settings.
153#ifdef USE_OMP
154 if (!omp_in_parallel())
155#endif
156 {
157 Eigen::setNbThreads(1);
158 Eigen::initParallel();
159 }
160
161 // generate random number (col major)
162 for (std::size_t t = 0; t < num_trotter_slices; t++) {
163 for (std::size_t i = 0; i < num_classical_spins; i++) {
164 system.rand_pool(i, t) = urd(random_number_engine);
165 }
166 }
167
168 // using OpenMP
169
170 // we have to consider the case num_trotter_slices is odd.
171 std::size_t upper_limit = num_trotter_slices % 2 != 0
172 ? num_trotter_slices - 1
173 : num_trotter_slices;
174
175 // NOTE: only signed integer is allowed for OpenMP
176#pragma omp parallel for
177 for (int64_t t = 0; t < (int64_t)upper_limit; t += 2) {
178 for (int64_t i = 0; i < (int64_t)num_classical_spins; i++) {
179 // calculate matrix dot product
180 do_calc(system, parameter, i, t, B);
181 }
182 }
183
184 // using OpenMP
185#pragma omp parallel for
186 for (int64_t t = 1; t < (int64_t)num_trotter_slices; t += 2) {
187 for (int64_t i = 0; i < (int64_t)num_classical_spins; i++) {
188 // calculate matrix dot product
189 do_calc(system, parameter, i, t, B);
190 }
191 }
192
193 // for the case num_trotter_slices is odd.
194 if (num_trotter_slices % 2 != 0) {
195 int64_t t = (int64_t)(num_trotter_slices - 1);
196 for (int64_t i = 0; i < (int64_t)num_classical_spins; i++) {
197 // calculate matrix dot product
198 do_calc(system, parameter, i, t, B);
199 }
200 }
201 }
202
203private:
204 inline static void
206 const utility::TransverseFieldUpdaterParameter &parameter, size_t i,
207 size_t t, FloatType B) {
208
209 // get number of trotter slices
210 std::size_t num_trotter_slices = system.trotter_spins.cols();
211
212 // aliases
213 auto &spins = system.trotter_spins;
214 // const auto& gamma = system.gamma;
215 const auto &beta = parameter.beta;
216 const auto &s = parameter.s;
217
218 // calculate dE for trotter direction
219 FloatType dEtrot = -2 * spins(i, t) *
220 (spins(i, mod_t((int64_t)t + 1, num_trotter_slices)) +
221 spins(i, mod_t((int64_t)t - 1, num_trotter_slices)));
222
223 // calculate total dE
224 // At s = 1 the Trotter coupling is infinite (B = -inf). A flip between
225 // disagreeing neighbours (dEtrot == 0) does not change the Trotter energy,
226 // so take the limit 0 instead of evaluating -inf * 0 = NaN, which would
227 // reject the flip and freeze the slices.
228 FloatType dE = s * (beta / num_trotter_slices) * system.dE(i, t) +
229 (dEtrot == 0 ? FloatType(0) : B * dEtrot);
230
231 // for debugging
232
233 // FloatType testdE = 0;
234
236 // testdE += -2 * s * (beta/num_trotter_slices) * spins(i,
237 // t)*(system.interaction.row(i).dot(spins.col(t)));
238
240 // testdE += -2 * (1/2.) * log(tanh(beta* gamma * (1.0-s)
241 // /num_trotter_slices)) * spins(i, t) *
242 // ( spins(i, mod_t((int64_t)t+1, num_trotter_slices))
243 // + spins(i, mod_t((int64_t)t-1, num_trotter_slices)));
244
245 // std::cout << "s=" << s << std::endl;
246 // std::cout << "dE=" << dE << std::endl;
247 // std::cout << "testdE=" << testdE << std::endl;
248 // assert((std::isinf(dE) && std::isinf(testdE)) || abs(dE-testdE) < 1e-5);
249
250 // metropolis
251
252 if (dE < 0 || exp(-dE) > system.rand_pool(i, t)) {
253
254 // update dE (spatial direction)
255 system.dE.col(t) +=
256 4 * spins(i, t) *
257 (system.interaction.row(i).transpose().cwiseProduct(spins.col(t)));
258 system.dE(i, t) *= -1;
259
260 // update spins
261 spins(i, t) *= -1;
262 }
263 }
264
265 inline static std::size_t mod_t(std::int64_t a,
266 std::size_t num_trotter_slices) {
267 // a -> [-1:num_trotter_slices]
268 // return a%num_trotter_slices (a>0), num_trotter_slices-1 (a==-1)
269 return (a + num_trotter_slices) % num_trotter_slices;
270 }
271};
272
276template <typename GraphType>
277struct SingleSpinFlip<system::ClassicalIsingPolynomial<GraphType>> {
278
281
283 using FloatType = typename GraphType::value_type;
284
291 template <typename RandomNumberEngine>
292 inline static void
293 update(ClPIsing &system, RandomNumberEngine &random_number_engine,
294 const utility::ClassicalUpdaterParameter &parameter) {
295
296 if (system.vartype == cimod::Vartype::SPIN) {
297 update_spin<RandomNumberEngine>(system, random_number_engine, parameter);
298 } else if (system.vartype == cimod::Vartype::BINARY) {
299 update_binary<RandomNumberEngine>(system, random_number_engine,
300 parameter);
301 } else {
302 std::stringstream ss;
303 ss << "Unknown vartype detected in " << __func__ << std::endl;
304 throw std::runtime_error(ss.str());
305 }
306 }
307
314 template <typename RandomNumberEngine>
315 inline static void
316 update_spin(ClPIsing &system, RandomNumberEngine &random_number_engine,
317 const utility::ClassicalUpdaterParameter &parameter) {
318
319 auto urd = std::uniform_real_distribution<>(0, 1.0);
320 for (const auto &index_spin : system.get_active_variables()) {
321 if (system.dE(index_spin) <= 0.0 ||
322 std::exp(-parameter.beta * system.dE(index_spin)) >
323 urd(random_number_engine)) {
324 system.update_spin_system(index_spin);
325 }
326 }
327 }
328
335 template <typename RandomNumberEngine>
336 inline static void
337 update_binary(ClPIsing &system, RandomNumberEngine &random_number_engine,
338 const utility::ClassicalUpdaterParameter &parameter) {
339
340 auto urd = std::uniform_real_distribution<>(0, 1.0);
341 for (const auto &index_binary : system.get_active_variables()) {
342 if (system.dE(index_binary) <= 0.0 ||
343 std::exp(-parameter.beta * system.dE(index_binary)) >
344 urd(random_number_engine)) {
345 system.update_binary_system(index_binary);
346 }
347 }
348 }
349};
350
351
352template<class SystemType, typename RandType>
353void SingleFlipUpdater(SystemType *system,
354 const std::int32_t num_sweeps,
355 const std::vector<typename SystemType::ValueType> &beta_list,
356 const typename RandType::result_type seed,
357 const algorithm::UpdateMethod update_metod) {
358
359 const std::int32_t system_size = system->GetSystemSize();
360
361 // Set random number engine
362 RandType random_number_engine(seed);
363 std::uniform_real_distribution<typename SystemType::ValueType> dist_real(0, 1);
364
365 if (update_metod == algorithm::UpdateMethod::METROPOLIS) {
366 // Do sequential update
367 for (std::int32_t sweep_count = 0; sweep_count < num_sweeps; sweep_count++) {
368 const auto beta = beta_list[sweep_count];
369 for (std::int32_t i = 0; i < system_size; i++) {
370 const auto delta_energy = system->GetEnergyDifference(i);
371 if (delta_energy <= 0 || std::exp(-beta*delta_energy) > dist_real(random_number_engine)) {
372 system->Flip(i);
373 }
374 }
375 }
376 }
377 else if (update_metod == algorithm::UpdateMethod::HEAT_BATH) {
378 // Do sequential update
379 for (std::int32_t sweep_count = 0; sweep_count < num_sweeps; sweep_count++) {
380 const auto beta = beta_list[sweep_count];
381 for (std::int32_t i = 0; i < system_size; i++) {
382 const auto delta_energy = system->GetEnergyDifference(i);
383 if (1/(1 + std::exp(beta*delta_energy)) > dist_real(random_number_engine)) {
384 system->Flip(i);
385 }
386 }
387 }
388 }
389 else {
390 throw std::runtime_error("Unknown UpdateMethod");
391 }
392}
393
394} // namespace updater
395} // namespace openjij
ClassicalIsingPolynomial class, which is a system to solve higher order unconstrained binary optimiza...
Definition classical_ising_polynomial.hpp:36
UpdateMethod
Definition algorithm.hpp:63
@ HEAT_BATH
Heat bath update.
Definition algorithm.hpp:69
@ METROPOLIS
Metropolis update.
Definition algorithm.hpp:66
Definition binary_polynomial_sa_system.hpp:20
Definition continuous_time_swendsen_wang.hpp:30
void SingleFlipUpdater(SystemType *system, const std::int32_t num_sweeps, const std::vector< typename SystemType::ValueType > &beta_list, const typename RandType::result_type seed, const algorithm::UpdateMethod update_metod)
Definition single_spin_flip.hpp:353
UpdaterParameter< system::classical_system > ClassicalUpdaterParameter
ClassicalUpdaterParameter alias.
Definition schedule_list.hpp:101
UpdaterParameter< system::transverse_field_system > TransverseFieldUpdaterParameter
TransverseFieldUpdaterParameter alias.
Definition schedule_list.hpp:112
Definition algorithm.hpp:24
double FloatType
Note:
Definition compile_config.hpp:37
ClassicalIsing structure (system for classical Ising model).
Definition classical_ising.hpp:38
TransverseIsing structure with discrete-time trotter spins.
Definition transverse_ising.hpp:39
typename GraphType::value_type FloatType
floating point type
Definition single_spin_flip.hpp:283
system::ClassicalIsingPolynomial< GraphType > ClPIsing
ClassicalIsingPolynomial system.
Definition single_spin_flip.hpp:280
static void update_binary(ClPIsing &system, RandomNumberEngine &random_number_engine, const utility::ClassicalUpdaterParameter &parameter)
Operate single spin flip when the system is composed of binary variables.
Definition single_spin_flip.hpp:337
static void update(ClPIsing &system, RandomNumberEngine &random_number_engine, const utility::ClassicalUpdaterParameter &parameter)
Operate single spin flip for Ising models with polynomial interactions and polynomial unconstrained b...
Definition single_spin_flip.hpp:293
static void update_spin(ClPIsing &system, RandomNumberEngine &random_number_engine, const utility::ClassicalUpdaterParameter &parameter)
Operate single spin flip when the system is composed of spin variables.
Definition single_spin_flip.hpp:316
typename GraphType::value_type FloatType
float type
Definition single_spin_flip.hpp:54
system::ClassicalIsing< GraphType > ClIsing
ClassicalIsing with dense interactions.
Definition single_spin_flip.hpp:49
static void update(ClIsing &system, RandomNumberEngine &random_number_engine, const utility::ClassicalUpdaterParameter &parameter)
operate single spin flip in a classical ising system
Definition single_spin_flip.hpp:68
system::TransverseIsing< GraphType > QIsing
transverse field ising system
Definition single_spin_flip.hpp:110
static void do_calc(QIsing &system, const utility::TransverseFieldUpdaterParameter &parameter, size_t i, size_t t, FloatType B)
Definition single_spin_flip.hpp:205
static void update(QIsing &system, RandomNumberEngine &random_number_engine, const utility::TransverseFieldUpdaterParameter &parameter)
operate single spin flip in a transverse ising system
Definition single_spin_flip.hpp:129
static std::size_t mod_t(std::int64_t a, std::size_t num_trotter_slices)
Definition single_spin_flip.hpp:265
typename GraphType::value_type FloatType
float type
Definition single_spin_flip.hpp:115
naive single spin flip updater
Definition single_spin_flip.hpp:36
double beta
inverse temperature
Definition schedule_list.hpp:47
double beta
inverse temperature
Definition schedule_list.hpp:86
double s
annealing schedule (from 0 (only transverse field) to 1 (only classical Hamiltonian))
Definition schedule_list.hpp:92