openjij
Framework for the Ising model and QUBO.
Loading...
Searching...
No Matches
ising_polynomial_sa_system.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 <cstdint>
18#include <vector>
19#include <random>
20#include "./sa_system.hpp"
21
22namespace openjij {
23namespace system {
24
25template<typename FloatType, typename RandType>
26class SASystem<graph::IsingPolynomialModel<FloatType>, RandType> {
27
30
33
35 using SeedType = typename RandType::result_type;
36
37public:
40
44 SASystem(const ModelType &model, const SeedType seed):
45 system_size_(model.GetSystemSize()),
46 key_value_list_(model.GetKeyValueList()),
47 adjacency_list_(model.GetAdjacencyList()) {
48 SetRandomConfiguration(seed);
49 SetTermProd();
50 SetBaseEnergyDifference();
51 }
52
53 void SetSample(const std::vector<VariableType> &sample) {
54 if (static_cast<std::int32_t>(sample.size()) != system_size_) {
55 throw std::runtime_error("The size of initial variables is not equal to the system size.");
56 }
57 for (std::int32_t i = 0; i < system_size_; ++i) {
58 if (!(sample[i] == -1 || sample[i] == 1)) {
59 throw std::runtime_error("The initial variables must be -1 or 1.");
60 }
61 }
62 sample_ = sample;
63 SetTermProd();
64 SetBaseEnergyDifference();
65 }
66
69 void Flip(const std::int32_t index) {
70 sample_[index] *= -1;
71 for (const auto &index_key: adjacency_list_[index]) {
72 const ValueType val = -2*key_value_list_[index_key].second*term_prod_[index_key];
73 term_prod_[index_key] *= -1;
74 for (const auto &v_index: key_value_list_[index_key].first) {
75 if (v_index != index) {
76 base_energy_difference_[v_index] += val*sample_[v_index];
77 }
78 }
79 }
80 }
81
84 std::int32_t GetSystemSize() const {
85 return system_size_;
86 }
87
90 const std::vector<VariableType> &ExtractSample() const {
91 return sample_;
92 }
93
96 const std::vector<ValueType> &GetBaseEnergyDifference() const {
97 return base_energy_difference_;
98 }
99
103 ValueType GetEnergyDifference(const std::int32_t index) const {
104 return -2*sample_[index]*base_energy_difference_[index];
105 }
106
107private:
108 const std::int32_t system_size_;
109 const std::vector<std::pair<std::vector<std::int32_t>, ValueType>> &key_value_list_;
110 const std::vector<std::vector<std::size_t>> &adjacency_list_;
111
112 std::vector<VariableType> sample_;
113 std::vector<ValueType> base_energy_difference_;
114 std::vector<short> term_prod_;
115
119 sample_.resize(system_size_);
120 std::uniform_int_distribution<short> dist(0, 1);
121 RandType random_number_engine(seed);
122 for (std::int32_t i = 0; i < system_size_; i++) {
123 sample_[i] = 2*dist(random_number_engine) - 1;
124 }
125 }
126
127 void SetTermProd() {
128 term_prod_.resize(key_value_list_.size());
129 for (std::size_t i = 0; i < key_value_list_.size(); ++i) {
130 short prod = 1;
131 for (const auto &index: key_value_list_[i].first) {
132 prod *= sample_[index];
133 }
134 term_prod_[i] = prod;
135 }
136 }
137
139 base_energy_difference_.clear();
140 base_energy_difference_.resize(system_size_);
141 for (std::size_t i = 0; i < key_value_list_.size(); ++i) {
142 const ValueType value = key_value_list_[i].second;
143 for (const auto &index: key_value_list_[i].first) {
144 base_energy_difference_[index] += value*term_prod_[i]*sample_[index];
145 }
146 }
147 }
148
149
150};
151
152
153} // namespace system
154} // namespace openjij
Definition ising_polynomial_model.hpp:23
std::int8_t VariableType
The variable type, which here represents binary variables .
Definition ising_polynomial_model.hpp:38
FloatType ValueType
The value type.
Definition ising_polynomial_model.hpp:29
typename ModelType::VariableType VariableType
The variable type, which here represents binary variables .
Definition ising_polynomial_sa_system.hpp:32
std::int32_t GetSystemSize() const
Get the system size.
Definition ising_polynomial_sa_system.hpp:84
const std::vector< ValueType > & GetBaseEnergyDifference() const
Get the energy difference when flipped and sample as list.
Definition ising_polynomial_sa_system.hpp:96
void SetSample(const std::vector< VariableType > &sample)
Definition ising_polynomial_sa_system.hpp:53
const std::int32_t system_size_
Definition ising_polynomial_sa_system.hpp:108
typename ModelType::ValueType ValueType
The value type.
Definition ising_polynomial_sa_system.hpp:39
const std::vector< VariableType > & ExtractSample() const
Extract the sample.
Definition ising_polynomial_sa_system.hpp:90
typename RandType::result_type SeedType
The type of seed in random number engine.
Definition ising_polynomial_sa_system.hpp:35
void SetBaseEnergyDifference()
Definition ising_polynomial_sa_system.hpp:138
void SetRandomConfiguration(const SeedType seed)
Set initial binary variables.
Definition ising_polynomial_sa_system.hpp:118
void Flip(const std::int32_t index)
Flip a variable.
Definition ising_polynomial_sa_system.hpp:69
const std::vector< std::vector< std::size_t > > & adjacency_list_
Definition ising_polynomial_sa_system.hpp:110
std::vector< VariableType > sample_
Definition ising_polynomial_sa_system.hpp:112
std::vector< short > term_prod_
Definition ising_polynomial_sa_system.hpp:114
const std::vector< std::pair< std::vector< std::int32_t >, ValueType > > & key_value_list_
Definition ising_polynomial_sa_system.hpp:109
SASystem(const ModelType &model, const SeedType seed)
Constructor of SASystem for IsingPolynomialModel.
Definition ising_polynomial_sa_system.hpp:44
std::vector< ValueType > base_energy_difference_
Definition ising_polynomial_sa_system.hpp:113
void SetTermProd()
Definition ising_polynomial_sa_system.hpp:127
ValueType GetEnergyDifference(const std::int32_t index) const
Get the energy difference when flipped.
Definition ising_polynomial_sa_system.hpp:103
Definition sa_system.hpp:21
Definition algorithm.hpp:24