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 <algorithm>
17#include <cmath>
18#include <cstddef>
19#include <tuple>
20#include <utility>
21#include <vector>
22
24
25namespace openjij {
26namespace utility {
27
33template <typename SystemType> struct UpdaterParameter;
34
38template <> struct UpdaterParameter<system::classical_system> {
39 using Tuple = double;
40 UpdaterParameter() = default;
42 inline Tuple get_tuple() const { return beta; }
43
47 double beta;
48};
49
53template <> struct UpdaterParameter<system::classical_constraint_system> {
54 using Tuple = std::pair<double, double>;
55 UpdaterParameter() = default;
58 : UpdaterParameter(obj.first, obj.second) {}
59 inline Tuple get_tuple() const { return std::make_pair(beta, lambda); }
60
64 double beta;
65
69 double lambda;
70};
71
75template <> struct UpdaterParameter<system::transverse_field_system> {
76 using Tuple = std::pair<double, double>;
77 UpdaterParameter() = default;
78 UpdaterParameter(double beta, double s) : beta{beta}, s{s} {}
80 : UpdaterParameter(obj.first, obj.second) {}
81 inline Tuple get_tuple() const { return std::make_pair(beta, s); }
82
86 double beta;
87
92 double s;
93};
94
95// TODO: add UpdaterParameter here if needed.
96//
97
102
108
114
115// TODO: the above class is only for monte carlo system, add enable_if.
116
122template <typename SystemType> struct Schedule {
123 Schedule() = default;
124 Schedule(const std::pair<UpdaterParameter<SystemType>, std::size_t> &obj)
125 : updater_parameter(obj.first), one_mc_step(obj.second) {}
127 std::size_t one_mc_step;
128};
129
135template <typename SystemType>
136using ScheduleList = std::vector<Schedule<SystemType>>;
137
142
148
154
167make_classical_schedule_list(double beta_min, double beta_max,
168 std::size_t one_mc_step,
169 std::size_t num_call_updater) {
170 auto schedule_list = ClassicalScheduleList(num_call_updater);
171 const double ratio = beta_min == beta_max ? 1.0 : beta_max / beta_min;
172 if (num_call_updater > 1 && beta_min > 0.0 && beta_max > 0.0 &&
173 std::isfinite(beta_min) && std::isfinite(beta_max) &&
174 !std::isnormal(ratio)) {
175 // A ratio can overflow or lose precision even with finite endpoints.
176 const std::size_t last = num_call_updater - 1;
177 const double log_min = std::log(beta_min);
178 const double log_span = std::log(beta_max) - log_min;
179 for (std::size_t index = 0; index < num_call_updater; ++index) {
180 const double fraction =
181 static_cast<double>(index) / static_cast<double>(last);
182 auto &schedule = schedule_list[index];
183 schedule.one_mc_step = one_mc_step;
184 schedule.updater_parameter =
185 ClassicalUpdaterParameter(std::exp(log_min + fraction * log_span));
186 }
187 schedule_list.front().updater_parameter.beta = beta_min;
188 schedule_list.back().updater_parameter.beta = beta_max;
189 return schedule_list;
190 }
191
192 const double r_beta =
193 std::pow(ratio, 1.0 / static_cast<double>(num_call_updater - 1));
194 const auto [lower, upper] = std::minmax(beta_min, beta_max);
195 double beta = beta_min;
196 for (auto &schedule : schedule_list) {
197 schedule.one_mc_step = one_mc_step;
198 schedule.updater_parameter = ClassicalUpdaterParameter(beta);
199 // Rounding in the step ratio can carry the accumulated beta past an endpoint.
200 beta = std::clamp(beta * r_beta, lower, upper);
201 }
202 if (num_call_updater > 1) {
203 schedule_list.back().updater_parameter.beta = beta_max;
204 }
205
206 return schedule_list;
207}
208
220make_transverse_field_schedule_list(double beta, std::size_t one_mc_step,
221 std::size_t num_call_updater) {
222 double ds = 1.0 / static_cast<double>(num_call_updater - 1);
223 double s = 0;
224
225 auto schedule_list = TransverseFieldScheduleList(num_call_updater);
226 for (auto &schedule : schedule_list) {
227 schedule.one_mc_step = one_mc_step;
228 schedule.updater_parameter = TransverseFieldUpdaterParameter(beta, s);
229 s += ds;
230 }
231
232 return schedule_list;
233}
234
248 double lambda, double beta_min, double beta_max, std::size_t one_mc_step,
249 std::size_t num_call_updater) {
250 double r_beta = std::pow(beta_max / beta_min,
251 1.0 / static_cast<double>(num_call_updater - 1));
252 double beta = beta_min;
253
254 auto schedule_list = ClassicalConstraintScheduleList(num_call_updater);
255 for (auto &schedule : schedule_list) {
256 schedule.one_mc_step = one_mc_step;
257 schedule.updater_parameter =
259 beta *= r_beta;
260 }
261
262 return schedule_list;
263}
264
273template <typename SystemType>
275 const std::vector<std::pair<typename UpdaterParameter<SystemType>::Tuple,
276 std::size_t>> &tuplelist) {
277 ScheduleList<SystemType> return_list;
278 return_list.reserve(tuplelist.size());
279 for (auto &elem : tuplelist) {
280 return_list.emplace_back(std::make_pair(elem.first, elem.second));
281 }
282 return return_list;
283}
284
286
289
292
293};
294
301template<typename FloatType>
302std::vector<FloatType> GenerateLinearBetaSchedule(const FloatType beta_min, const FloatType beta_max, const std::int32_t num_sweeps) {
303 if (num_sweeps == 1) {
304 return std::vector<FloatType>{beta_min};
305 }
306 std::vector<FloatType> beta_list(num_sweeps);
307 for (std::int32_t i = 0; i < num_sweeps; ++i) {
308 beta_list[i] = beta_min + i*(beta_max - beta_min)/(num_sweeps - 1);
309 }
310 return beta_list;
311}
312
319template<typename FloatType>
320std::vector<FloatType> GenerateGeometricBetaSchedule(const FloatType beta_min, const FloatType beta_max, const std::int32_t num_sweeps) {
321 if (num_sweeps == 1) {
322 return std::vector<FloatType>{beta_min};
323 }
324 std::vector<FloatType> beta_list(num_sweeps);
325 const FloatType alpha = std::pow(beta_max/beta_min, 1/static_cast<FloatType>(num_sweeps - 1));
326 FloatType beta = beta_min;
327 for (std::int32_t i = 0; i < num_sweeps; ++i) {
328 beta_list[i] = beta;
329 beta = beta*alpha;
330 }
331 return beta_list;
332}
333
341template<typename FloatType>
342std::vector<FloatType> GenerateBetaList(const TemperatureSchedule schedule_type,
343 const FloatType beta_min,
344 const FloatType beta_max,
345 const std::int32_t num_sweeps) {
346 std::vector<FloatType> beta_list;
347 if (schedule_type == TemperatureSchedule::LINEAR) {
348 beta_list = GenerateLinearBetaSchedule(beta_min, beta_max, num_sweeps);
349 }
350 else if (schedule_type == TemperatureSchedule::GEOMETRIC) {
351 beta_list = GenerateGeometricBetaSchedule(beta_min, beta_max, num_sweeps);
352 }
353 else {
354 throw std::runtime_error("Unknwon beta schedule list");
355 }
356 return beta_list;
357}
358
359
360} // namespace utility
361} // namespace openjij
Definition binary_polynomial_sa_system.hpp:20
Definition create_geometric_progression.hpp:18
UpdaterParameter< system::classical_system > ClassicalUpdaterParameter
ClassicalUpdaterParameter alias.
Definition schedule_list.hpp:101
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:167
TemperatureSchedule
Definition schedule_list.hpp:285
@ GEOMETRIC
Geometric cooling.
Definition schedule_list.hpp:291
@ LINEAR
Linear cooling.
Definition schedule_list.hpp:288
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:302
ScheduleList< system::transverse_field_system > TransverseFieldScheduleList
TransverseFieldScheduleList alias.
Definition schedule_list.hpp:146
UpdaterParameter< system::transverse_field_system > TransverseFieldUpdaterParameter
TransverseFieldUpdaterParameter alias.
Definition schedule_list.hpp:112
UpdaterParameter< system::classical_constraint_system > ClassicalConstraintUpdaterParameter
ClassicalUpdaterParameter alias.
Definition schedule_list.hpp:106
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:274
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:320
ScheduleList< system::classical_constraint_system > ClassicalConstraintScheduleList
ClassicalConstraintScheduleList alias.
Definition schedule_list.hpp:152
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:220
ScheduleList< system::classical_system > ClassicalScheduleList
ClassicalScheduleList alias.
Definition schedule_list.hpp:141
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:342
std::vector< Schedule< SystemType > > ScheduleList
schedule list alias
Definition schedule_list.hpp:136
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:247
Definition algorithm.hpp:24
double FloatType
Note:
Definition compile_config.hpp:37
Schedule(const std::pair< UpdaterParameter< SystemType >, std::size_t > &obj)
Definition schedule_list.hpp:124
std::size_t one_mc_step
Definition schedule_list.hpp:127
UpdaterParameter< SystemType > updater_parameter
Definition schedule_list.hpp:126
UpdaterParameter(const Tuple &obj)
Definition schedule_list.hpp:57
UpdaterParameter(double beta, double lambda)
Definition schedule_list.hpp:56
std::pair< double, double > Tuple
Definition schedule_list.hpp:54
double lambda
constraint coefficient
Definition schedule_list.hpp:69
double beta
inverse temperature
Definition schedule_list.hpp:64
double beta
inverse temperature
Definition schedule_list.hpp:47
UpdaterParameter(Tuple beta)
Definition schedule_list.hpp:41
Tuple get_tuple() const
Definition schedule_list.hpp:42
double beta
inverse temperature
Definition schedule_list.hpp:86
UpdaterParameter(double beta, double s)
Definition schedule_list.hpp:78
std::pair< double, double > Tuple
Definition schedule_list.hpp:76
double s
annealing schedule (from 0 (only transverse field) to 1 (only classical Hamiltonian))
Definition schedule_list.hpp:92
UpdaterParameter(const Tuple &obj)
Definition schedule_list.hpp:79
Tuple get_tuple() const
Definition schedule_list.hpp:81
updater parameter for monte carlo simulation
Definition schedule_list.hpp:33