25template<
typename FloatType,
typename RandType>
26class SASystem<graph::IsingPolynomialModel<FloatType>, RandType> {
35 using SeedType =
typename RandType::result_type;
45 system_size_(model.GetSystemSize()),
46 key_value_list_(model.GetKeyValueList()),
47 adjacency_list_(model.GetAdjacencyList()) {
48 SetRandomConfiguration(seed);
50 SetBaseEnergyDifference();
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.");
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.");
64 SetBaseEnergyDifference();
69 void Flip(
const std::int32_t index) {
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];
97 return base_energy_difference_;
104 return -2*sample_[index]*base_energy_difference_[index];
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;
128 term_prod_.resize(key_value_list_.size());
129 for (std::size_t i = 0; i < key_value_list_.size(); ++i) {
131 for (
const auto &index: key_value_list_[i].first) {
132 prod *= sample_[index];
134 term_prod_[i] = prod;
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];
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