openjij
Framework for the Ising model and QUBO.
Loading...
Searching...
No Matches
eigen.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 <Eigen/Dense>
18#include <Eigen/Sparse>
19
20#include "openjij/graph/all.hpp"
21
22namespace openjij {
23namespace utility {
24
31template <typename GraphType, int Options = Eigen::ColMajor>
33
40template <typename FloatType, int Options>
41struct get_eigen_matrix_type<graph::Dense<FloatType>, Options> {
42 using type =
43 Eigen::Matrix<FloatType, Eigen::Dynamic, Eigen::Dynamic, Options>;
44};
45
52template <typename FloatType, int Options>
53struct get_eigen_matrix_type<graph::Sparse<FloatType>, Options> {
54 using type = Eigen::SparseMatrix<FloatType, Options>;
55};
56
66template <typename FloatType, int Options = Eigen::ColMajor>
67inline static Eigen::Matrix<FloatType, Eigen::Dynamic, 1, Options>
69 Eigen::Matrix<FloatType, Eigen::Dynamic, 1, Options> ret_vec(
70 init_spin.size() + 1);
71
72 // initialize spin
73 for (size_t i = 0; i < init_spin.size(); i++) {
74 ret_vec(i) = init_spin[i];
75 }
76
77 // for local field
78 ret_vec[init_spin.size()] = 1;
79
80 return ret_vec;
81}
82
93template <typename FloatType, int Options = Eigen::ColMajor>
94inline static Eigen::Matrix<FloatType, Eigen::Dynamic, Eigen::Dynamic, Options>
95gen_matrix_from_trotter_spins(const std::vector<graph::Spins> &trotter_spins) {
96 Eigen::Matrix<FloatType, Eigen::Dynamic, Eigen::Dynamic, Options> ret_mat(
97 trotter_spins[0].size() + 1, trotter_spins.size());
98
99 // initialize spin
100 for (size_t j = 0; j < trotter_spins.size(); j++) {
101 for (size_t i = 0; i < trotter_spins[j].size(); i++) {
102 ret_mat(i, j) = trotter_spins[j][i];
103 }
104 }
105
106 // dummy spins
107 for (size_t j = 0; j < trotter_spins.size(); j++) {
108 ret_mat(trotter_spins[0].size(), j) = 1;
109 }
110
111 return ret_mat;
112}
113
124template <int Options = Eigen::ColMajor, typename FloatType>
125inline static Eigen::Matrix<FloatType, Eigen::Dynamic, Eigen::Dynamic, Options>
127 // initialize interaction
128 Eigen::Matrix<FloatType, Eigen::Dynamic, Eigen::Dynamic, Options> ret_mat(
129 graph.get_num_spins() + 1, graph.get_num_spins() + 1);
130
131 ret_mat.setZero();
132
133 for (size_t i = 0; i < graph.get_num_spins(); i++) {
134 for (size_t j = i + 1; j < graph.get_num_spins(); j++) {
135 ret_mat(i, j) = graph.J(i, j);
136 ret_mat(j, i) = graph.J(i, j);
137 }
138 }
139
140 // for local field
141 for (size_t i = 0; i < graph.get_num_spins(); i++) {
142 ret_mat(i, graph.get_num_spins()) = graph.h(i);
143 ret_mat(graph.get_num_spins(), i) = graph.h(i);
144 }
145
146 // for local field
147 ret_mat(graph.get_num_spins(), graph.get_num_spins()) = 1;
148
149 return ret_mat;
150}
151
162template <int Options = Eigen::ColMajor, typename FloatType>
163inline static Eigen::SparseMatrix<FloatType, Options>
165 // initialize interaction
166 Eigen::SparseMatrix<FloatType, Options> ret_mat(graph.get_num_spins() + 1,
167 graph.get_num_spins() + 1);
168
169 ret_mat.setZero();
170
171 // make triplet list
172 using T = std::vector<Eigen::Triplet<FloatType>>;
173 T t_list;
174
175 for (size_t ind = 0; ind < graph.get_num_spins(); ind++) {
176 for (size_t adj_ind : graph.adj_nodes(ind)) {
177 if (ind != adj_ind) {
178 t_list.emplace_back(ind, adj_ind, graph.J(ind, adj_ind));
179 } else {
180 t_list.emplace_back(ind, graph.get_num_spins(), graph.h(ind));
181 t_list.emplace_back(graph.get_num_spins(), ind, graph.h(ind));
182 }
183 }
184 }
185
186 t_list.emplace_back(graph.get_num_spins(), graph.get_num_spins(), 1);
187
188 ret_mat.setFromTriplets(t_list.begin(), t_list.end());
189
190 return ret_mat;
191}
192
193} // namespace utility
194} // namespace openjij
two-body all-to-all interactions The Hamiltonian is like
Definition dense.hpp:44
FloatType & J(Index i, Index j)
access J_{ij}
Definition dense.hpp:211
FloatType & h(Index i)
access h_{i} (local field)
Definition dense.hpp:246
std::size_t get_num_spins() const noexcept
get number of spins
Definition graph.hpp:89
Sparse graph: two-body intereactions with O(1) connectivity The Hamiltonian is like.
Definition sparse.hpp:40
FloatType & h(Index i)
access h_{i} (local field)
Definition sparse.hpp:300
FloatType & J(Index i, Index j)
access J_{ij}
Definition sparse.hpp:269
const Nodes & adj_nodes(Index ind) const
list of adjacent nodes
Definition sparse.hpp:201
std::vector< Spin > Spins
Definition graph.hpp:27
static Eigen::Matrix< FloatType, Eigen::Dynamic, Eigen::Dynamic, Options > gen_matrix_from_graph(const graph::Dense< FloatType > &graph)
generate Eigen Dense Matrix from Dense graph
Definition eigen.hpp:126
static Eigen::Matrix< FloatType, Eigen::Dynamic, 1, Options > gen_vector_from_std_vector(const graph::Spins &init_spin)
generate Eigen Vector from std::vector
Definition eigen.hpp:68
static Eigen::Matrix< FloatType, Eigen::Dynamic, Eigen::Dynamic, Options > gen_matrix_from_trotter_spins(const std::vector< graph::Spins > &trotter_spins)
generate Eigen Matrix from TrotterSpins
Definition eigen.hpp:95
Definition algorithm.hpp:24
Eigen::Matrix< FloatType, Eigen::Dynamic, Eigen::Dynamic, Options > type
Definition eigen.hpp:43
Eigen::SparseMatrix< FloatType, Options > type
Definition eigen.hpp:54
get Eigen Matrix type from Graph Type
Definition eigen.hpp:32