openjij
Framework for the Ising model and QUBO.
Loading...
Searching...
No Matches
schedule_list.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#include <cmath>
17#include <cstddef>
18#include <tuple>
19#include <utility>
20#include <vector>
21
23
24namespace openjij {
25namespace utility {
26
32template <typename SystemType> struct UpdaterParameter;
33
37template <> struct UpdaterParameter<system::classical_system> {
38 using Tuple = double;
39 UpdaterParameter() = default;
40 UpdaterParameter(Tuple beta) : beta{beta} {}
41 inline Tuple get_tuple() const { return beta; }
42
46 double beta;
47};
48
52template <> struct UpdaterParameter<system::classical_constraint_system> {
53 using Tuple = std::pair<double, double>;
54 UpdaterParameter() = default;
55 UpdaterParameter(double beta, double lambda) : beta{beta}, lambda{lambda} {}
57 : UpdaterParameter(obj.first, obj.second) {}
58 inline Tuple get_tuple() const { return std::make_pair(beta, lambda); }
59
63 double beta;
64
68 double lambda;
69};
70
74template <> struct UpdaterParameter<system::transverse_field_system> {
75 using Tuple = std::pair<double, double>;
76 UpdaterParameter() = default;
77 UpdaterParameter(double beta, double s) : beta{beta}, s{s} {}
79 : UpdaterParameter(obj.first, obj.second) {}
80 inline Tuple get_tuple() const { return std::make_pair(beta, s); }
81
85 double beta;
86
91 double s;
92};
93
94// TODO: add UpdaterParameter here if needed.
95//
96
101
107
113
114// TODO: the above class is only for monte carlo system, add enable_if.
115
121template <typename SystemType> struct Schedule {
122 Schedule() = default;
123 Schedule(const std::pair<UpdaterParameter<SystemType>, std::size_t> &obj)
124 : updater_parameter(obj.first), one_mc_step(obj.second) {}
126 std::size_t one_mc_step;
127};
128
134template <typename SystemType>
135using ScheduleList = std::vector<Schedule<SystemType>>;
136
141
147
153
166make_classical_schedule_list(double beta_min, double beta_max,
167 std::size_t one_mc_step,
168 std::size_t num_call_updater) {
169 double r_beta = std::pow(beta_max / beta_min,
170 1.0 / static_cast<double>(num_call_updater - 1));
171 double beta = beta_min;
172
173 auto schedule_list = ClassicalScheduleList(num_call_updater);
174 for (auto &schedule : schedule_list) {
175 schedule.one_mc_step = one_mc_step;
176 schedule.updater_parameter = ClassicalUpdaterParameter(beta);
177 beta *= r_beta;
178 }
179
180 return schedule_list;
181}
182
194make_transverse_field_schedule_list(double beta, std::size_t one_mc_step,
195 std::size_t num_call_updater) {
196 double ds = 1.0 / static_cast<double>(num_call_updater - 1);
197 double s = 0;
198
199 auto schedule_list = TransverseFieldScheduleList(num_call_updater);
200 for (auto &schedule : schedule_list) {
201 schedule.one_mc_step = one_mc_step;
202 schedule.updater_parameter = TransverseFieldUpdaterParameter(beta, s);
203 s += ds;
204 }
205
206 return schedule_list;
207}
208
222 double lambda, double beta_min, double beta_max, std::size_t one_mc_step,
223 std::size_t num_call_updater) {
224 double r_beta = std::pow(beta_max / beta_min,
225 1.0 / static_cast<double>(num_call_updater - 1));
226 double beta = beta_min;
227
228 auto schedule_list = ClassicalConstraintScheduleList(num_call_updater);
229 for (auto &schedule : schedule_list) {
230 schedule.one_mc_step = one_mc_step;
231 schedule.updater_parameter =
233 beta *= r_beta;
234 }
235
236 return schedule_list;
237}
238
247template <typename SystemType>
249 const std::vector<std::pair<typename UpdaterParameter<SystemType>::Tuple,
250 std::size_t>> &tuplelist) {
251 ScheduleList<SystemType> return_list;
252 return_list.reserve(tuplelist.size());
253 for (auto &elem : tuplelist) {
254 return_list.emplace_back(std::make_pair(elem.first, elem.second));
255 }
256 return return_list;
257}
258
260
262 LINEAR,
263
265 GEOMETRIC,
266
267};
268
275template<typename FloatType>
276std::vector<FloatType> GenerateLinearBetaSchedule(const FloatType beta_min, const FloatType beta_max, const std::int32_t num_sweeps) {
277 if (num_sweeps == 1) {
278 return std::vector<FloatType>{beta_min};
279 }
280 std::vector<FloatType> beta_list(num_sweeps);
281 for (std::int32_t i = 0; i < num_sweeps; ++i) {
282 beta_list[i] = beta_min + i*(beta_max - beta_min)/(num_sweeps - 1);
283 }
284 return beta_list;
285}
286
293template<typename FloatType>
294std::vector<FloatType> GenerateGeometricBetaSchedule(const FloatType beta_min, const FloatType beta_max, const std::int32_t num_sweeps) {
295 if (num_sweeps == 1) {
296 return std::vector<FloatType>{beta_min};
297 }
298 std::vector<FloatType> beta_list(num_sweeps);
299 const FloatType alpha = std::pow(beta_max/beta_min, 1/static_cast<FloatType>(num_sweeps - 1));
300 FloatType beta = beta_min;
301 for (std::int32_t i = 0; i < num_sweeps; ++i) {
302 beta_list[i] = beta;
303 beta = beta*alpha;
304 }
305 return beta_list;
306}
307
315template<typename FloatType>
316std::vector<FloatType> GenerateBetaList(const TemperatureSchedule schedule_type,
317 const FloatType beta_min,
318 const FloatType beta_max,
319 const std::int32_t num_sweeps) {
320 std::vector<FloatType> beta_list;
321 if (schedule_type == TemperatureSchedule::LINEAR) {
322 beta_list = GenerateLinearBetaSchedule(beta_min, beta_max, num_sweeps);
323 }
324 else if (schedule_type == TemperatureSchedule::GEOMETRIC) {
325 beta_list = GenerateGeometricBetaSchedule(beta_min, beta_max, num_sweeps);
326 }
327 else {
328 throw std::runtime_error("Unknwon beta schedule list");
329 }
330 return beta_list;
331}
332
333
334} // namespace utility
335} // namespace openjij
ScheduleList< system::classical_system > ClassicalScheduleList
ClassicalScheduleList alias.
Definition schedule_list.hpp:140
std::vector< Schedule< SystemType > > ScheduleList
schedule list alias
Definition schedule_list.hpp:135
ClassicalScheduleList make_classical_schedule_list(double beta_min, double beta_max, std::size_t one_mc_step, std::size_t num_call_updater)
helper function for making classical schedule list with geometric series of inverse temperatures.
Definition schedule_list.hpp:166
TemperatureSchedule
Definition schedule_list.hpp:259
std::vector< FloatType > GenerateLinearBetaSchedule(const FloatType beta_min, const FloatType beta_max, const std::int32_t num_sweeps)
Generate linear temperature schedule.
Definition schedule_list.hpp:276
ScheduleList< system::classical_constraint_system > ClassicalConstraintScheduleList
ClassicalConstraintScheduleList alias.
Definition schedule_list.hpp:152
ScheduleList< SystemType > make_schedule_list(const std::vector< std::pair< typename UpdaterParameter< SystemType >::Tuple, std::size_t > > &tuplelist)
helper function for making schedulelist from list of tuples
Definition schedule_list.hpp:248
std::vector< FloatType > GenerateGeometricBetaSchedule(const FloatType beta_min, const FloatType beta_max, const std::int32_t num_sweeps)
Generate geometric temperature schedule.
Definition schedule_list.hpp:294
TransverseFieldScheduleList make_transverse_field_schedule_list(double beta, std::size_t one_mc_step, std::size_t num_call_updater)
helper function for making transverse field system schedule list with arithmetic sequence of annealin...
Definition schedule_list.hpp:194
UpdaterParameter< system::classical_constraint_system > ClassicalConstraintUpdaterParameter
ClassicalUpdaterParameter alias.
Definition schedule_list.hpp:106
UpdaterParameter< system::transverse_field_system > TransverseFieldUpdaterParameter
TransverseFieldUpdaterParameter alias.
Definition schedule_list.hpp:112
std::vector< FloatType > GenerateBetaList(const TemperatureSchedule schedule_type, const FloatType beta_min, const FloatType beta_max, const std::int32_t num_sweeps)
Generate temperature schedule from specific schedule.
Definition schedule_list.hpp:316
ScheduleList< system::transverse_field_system > TransverseFieldScheduleList
TransverseFieldScheduleList alias.
Definition schedule_list.hpp:146
ClassicalConstraintScheduleList make_classical_constraint_schedule_list(double lambda, double beta_min, double beta_max, std::size_t one_mc_step, std::size_t num_call_updater)
helper function for making classical constraint schedule list with geometric series of inverse temper...
Definition schedule_list.hpp:221
UpdaterParameter< system::classical_system > ClassicalUpdaterParameter
ClassicalUpdaterParameter alias.
Definition schedule_list.hpp:100
Definition algorithm.hpp:24
double FloatType
Note:
Definition compile_config.hpp:37
schedule struct
Definition schedule_list.hpp:121
Schedule(const std::pair< UpdaterParameter< SystemType >, std::size_t > &obj)
Definition schedule_list.hpp:123
std::size_t one_mc_step
Definition schedule_list.hpp:126
UpdaterParameter< SystemType > updater_parameter
Definition schedule_list.hpp:125
updater paramter for classical ising model with a single constraint
Definition schedule_list.hpp:52
UpdaterParameter(const Tuple &obj)
Definition schedule_list.hpp:56
UpdaterParameter(double beta, double lambda)
Definition schedule_list.hpp:55
std::pair< double, double > Tuple
Definition schedule_list.hpp:53
double lambda
constraint coefficient
Definition schedule_list.hpp:68
double beta
inverse temperature
Definition schedule_list.hpp:63
updater parameter for classical ising system
Definition schedule_list.hpp:37
double beta
inverse temperature
Definition schedule_list.hpp:46
UpdaterParameter(Tuple beta)
Definition schedule_list.hpp:40
Tuple get_tuple() const
Definition schedule_list.hpp:41
updater paramter for transverse ising model
Definition schedule_list.hpp:74
double beta
inverse temperature
Definition schedule_list.hpp:85
UpdaterParameter(double beta, double s)
Definition schedule_list.hpp:77
std::pair< double, double > Tuple
Definition schedule_list.hpp:75
double s
annealing schedule (from 0 (only transverse field) to 1 (only classical Hamiltonian))
Definition schedule_list.hpp:91
UpdaterParameter(const Tuple &obj)
Definition schedule_list.hpp:78
Tuple get_tuple() const
Definition schedule_list.hpp:80
updater parameter for monte carlo simulation
Definition schedule_list.hpp:32