openjij
Framework for the Ising model and QUBO.
Loading...
Searching...
No Matches
trotter_cluster_flip.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 <cmath>
18#include <cstddef>
19#include <random>
20#include <vector>
21
24
25namespace openjij {
26namespace updater {
27
33template <typename System> struct TrotterClusterFlip;
34
57template <typename GraphType>
58struct TrotterClusterFlip<system::TransverseIsing<GraphType>> {
60 using FloatType = typename GraphType::value_type;
61
62 template <typename RandomNumberEngine>
63 inline static void
64 update(QIsing &system, RandomNumberEngine &random_number_engine,
66 const std::size_t num_classical_spins = system.num_classical_spins;
67 const std::size_t num_trotter_slices = system.trotter_spins.cols();
68 const double M = static_cast<double>(num_trotter_slices);
69 const double classical_beta = parameter.s * parameter.beta / M;
70 // probability that a bond between equal neighbouring copies is cut.
71 const double p_cut =
72 std::tanh(parameter.beta * system.gamma * (1.0 - parameter.s) / M);
73
74 std::uniform_real_distribution<double> urd(0.0, 1.0);
75 auto &spins = system.trotter_spins;
76 std::vector<char> bond(num_trotter_slices);
77
78 for (std::size_t i = 0; i < num_classical_spins; ++i) {
79 // bond[t]: copies t and t+1 (mod M) belong to the same segment.
80 std::size_t first_cut = num_trotter_slices;
81 for (std::size_t t = 0; t < num_trotter_slices; ++t) {
82 const std::size_t u = t + 1 == num_trotter_slices ? 0 : t + 1;
83 bond[t] = spins(i, t) == spins(i, u) && urd(random_number_engine) >= p_cut;
84 if (!bond[t] && first_cut == num_trotter_slices) {
85 first_cut = t;
86 }
87 }
88
89 if (first_cut == num_trotter_slices) {
90 // The ring is a single segment.
91 FloatType dE = 0;
92 for (std::size_t t = 0; t < num_trotter_slices; ++t) {
93 dE += system.dE(i, t);
94 }
95 if (accept(dE, classical_beta, urd, random_number_engine)) {
96 for (std::size_t t = 0; t < num_trotter_slices; ++t) {
97 flip(system, i, t);
98 }
99 }
100 continue;
101 }
102
103 // Walk the segments, starting right after the first cut.
104 std::size_t start = first_cut + 1 == num_trotter_slices ? 0 : first_cut + 1;
105 std::size_t visited = 0;
106 while (visited < num_trotter_slices) {
107 std::size_t t = start;
108 std::size_t length = 1;
109 FloatType dE = system.dE(i, t);
110 while (bond[t]) {
111 t = t + 1 == num_trotter_slices ? 0 : t + 1;
112 dE += system.dE(i, t);
113 ++length;
114 }
115 if (accept(dE, classical_beta, urd, random_number_engine)) {
116 std::size_t k = start;
117 for (std::size_t l = 0; l < length; ++l) {
118 flip(system, i, k);
119 k = k + 1 == num_trotter_slices ? 0 : k + 1;
120 }
121 }
122 visited += length;
123 start = t + 1 == num_trotter_slices ? 0 : t + 1;
124 }
125 }
126 }
127
128private:
129 template <typename Dist, typename RandomNumberEngine>
130 inline static bool accept(const FloatType dE, const double classical_beta,
131 Dist &urd, RandomNumberEngine &rng) {
132 return dE <= 0 || std::exp(-classical_beta * dE) > urd(rng);
133 }
134
135 inline static void flip(QIsing &system, const std::size_t i,
136 const std::size_t t) {
137 auto &spins = system.trotter_spins;
138 system.dE.col(t) +=
139 4 * spins(i, t) *
140 (system.interaction.row(i).transpose().cwiseProduct(spins.col(t)));
141 system.dE(i, t) *= -1;
142 spins(i, t) *= -1;
143 }
144};
145
146} // namespace updater
147} // namespace openjij
Definition binary_polynomial_sa_system.hpp:20
Definition continuous_time_swendsen_wang.hpp:30
UpdaterParameter< system::transverse_field_system > TransverseFieldUpdaterParameter
TransverseFieldUpdaterParameter alias.
Definition schedule_list.hpp:112
Definition algorithm.hpp:24
TransverseIsing structure with discrete-time trotter spins.
Definition transverse_ising.hpp:39
static bool accept(const FloatType dE, const double classical_beta, Dist &urd, RandomNumberEngine &rng)
Definition trotter_cluster_flip.hpp:130
static void update(QIsing &system, RandomNumberEngine &random_number_engine, const utility::TransverseFieldUpdaterParameter &parameter)
Definition trotter_cluster_flip.hpp:64
typename GraphType::value_type FloatType
Definition trotter_cluster_flip.hpp:60
static void flip(QIsing &system, const std::size_t i, const std::size_t t)
Definition trotter_cluster_flip.hpp:135
system::TransverseIsing< GraphType > QIsing
Definition trotter_cluster_flip.hpp:59
Imaginary-time (Trotter-direction) cluster updater.
Definition trotter_cluster_flip.hpp:33
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