openjij
Framework for the Ising model and QUBO.
Loading...
Searching...
No Matches
csr_sparse.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 <algorithm>
18#include <cassert>
19#include <cstddef>
20#include <type_traits>
21#include <unordered_map>
22#include <utility>
23
25
26#include <Eigen/Sparse>
27
29
30namespace openjij {
31namespace graph {
32
42template <typename FloatType> class CSRSparse : public Graph {
43 static_assert(std::is_floating_point<FloatType>::value,
44 "FloatType must be floating-point type.");
45
46public:
51 Eigen::SparseMatrix<FloatType, Eigen::RowMajor>;
52
57
58private:
76
77public:
78
96 explicit CSRSparse(const Interactions &interaction): Graph(interaction.rows()-1) {
97 if (interaction.rows() != interaction.cols()) {
98 std::runtime_error("interaction.rows() != interaction.cols()");
99 }
100 _J = interaction.template selfadjointView<Eigen::Upper>();
101 }
102
107 CSRSparse(const CSRSparse<FloatType> &) = default;
108
114
124 return this->energy(spins);
125 }
126
127 FloatType calc_energy(const Eigen::Matrix<FloatType, Eigen::Dynamic, 1,
128 Eigen::ColMajor> &spins) const {
129 return this->energy(spins);
130 }
131
139 FloatType energy(const Spins &spins) const {
140 if (spins.size() != this->get_num_spins()) {
141 throw std::out_of_range("Out of range in energy in CSRSparse graph.");
142 }
143
144 using Vec = Eigen::Matrix<FloatType, Eigen::Dynamic, 1, Eigen::ColMajor>;
145 Vec s(get_num_spins() + 1);
146 for (size_t i = 0; i < spins.size(); i++) {
147 s(i) = spins[i];
148 }
149 s(get_num_spins()) = 1;
150
151 // the energy must be consistent with BinaryQuadraticModel.
152 return (s.transpose() *
153 (_J.template triangularView<Eigen::Upper>() * s))(0, 0) -
154 1;
155 }
156
157 FloatType energy(const Eigen::Matrix<FloatType, Eigen::Dynamic, 1,
158 Eigen::ColMajor> &spins) const {
160 for (size_t i = 0; i < temp_spins.size(); i++) {
161 temp_spins[i] = spins(i);
162 }
163 return energy(temp_spins);
164 }
165
184 return this->_J.template selfadjointView<Eigen::Upper>();
185 }
186};
187} // namespace graph
188} // namespace openjij
CSRSparse graph: just store CSR Sparse Matrix (Eigen::Sparse) The Hamiltonian is like.
Definition csr_sparse.hpp:42
const Interactions get_interactions() const
get interactions (Eigen Matrix)
Definition csr_sparse.hpp:183
FloatType calc_energy(const Spins &spins) const
calculate total energy
Definition csr_sparse.hpp:123
FloatType energy(const Spins &spins) const
calculate total energy
Definition csr_sparse.hpp:139
FloatType energy(const Eigen::Matrix< FloatType, Eigen::Dynamic, 1, Eigen::ColMajor > &spins) const
Definition csr_sparse.hpp:157
CSRSparse(const Interactions &interaction)
CSRSparse constructor.
Definition csr_sparse.hpp:96
CSRSparse(const CSRSparse< FloatType > &)=default
CSRSparse copy constructor.
CSRSparse(CSRSparse< FloatType > &&)=default
CSRSparse move constructor.
Interactions _J
interactions (the number of intereactions is num_spins*(num_spins+1)/2).
Definition csr_sparse.hpp:75
Eigen::SparseMatrix< FloatType, Eigen::RowMajor > Interactions
interaction type
Definition csr_sparse.hpp:51
FloatType calc_energy(const Eigen::Matrix< FloatType, Eigen::Dynamic, 1, Eigen::ColMajor > &spins) const
Definition csr_sparse.hpp:127
FloatType value_type
float type
Definition csr_sparse.hpp:56
Abstract graph class.
Definition graph.hpp:37
std::size_t get_num_spins() const noexcept
get number of spins
Definition graph.hpp:89
auto json_parse(const json &obj, bool relabel=true)
parse json object from bqm.to_serializable
Definition parse.hpp:50
std::vector< Spin > Spins
Definition graph.hpp:27
Definition algorithm.hpp:24
double FloatType
Note:
Definition compile_config.hpp:37