openjij
Framework for the Ising model and QUBO.
Loading...
Searching...
No Matches
graph.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 <cstddef>
18#include <random>
19#include <vector>
20
21#include <nlohmann/json.hpp>
22
23namespace openjij {
24namespace graph {
25
26using Spin = int;
27using Spins = std::vector<Spin>;
28using Binary = int;
29using Binaries = std::vector<Binary>;
30using Index = std::size_t;
31
32using Nodes = std::vector<Index>;
33
37class Graph {
38private:
39 const std::size_t _num_spins;
40public:
46 explicit Graph(std::size_t num_spins) : _num_spins(num_spins) {}
47
53 // const Spins gen_spin() const{
54 template <typename RandomNumberEngine>
55 const Spins gen_spin(RandomNumberEngine &random_numder_engine) const {
56 // generate spin array
58
59 std::uniform_int_distribution<> uid(0, 1);
60 for (auto &elem : ret_spin) {
61 elem = 2 * uid(random_numder_engine) - 1;
62 }
63
64 return ret_spin;
65 }
66
72 template <typename RandomNumberEngine>
73 const Binaries gen_binary(RandomNumberEngine &random_numder_engine) const {
74 // generate binary array
76 std::uniform_int_distribution<> uid(0, 1);
77 for (auto &elem : ret_binary) {
79 }
80 return ret_binary;
81 }
82
89 std::size_t get_num_spins() const noexcept { return _num_spins; }
90
96 std::size_t size() const noexcept { return _num_spins; }
97};
98} // namespace graph
99} // namespace openjij
Abstract graph class.
Definition graph.hpp:37
Graph(std::size_t num_spins)
Graph constructor.
Definition graph.hpp:46
const Spins gen_spin(RandomNumberEngine &random_numder_engine) const
generate spins randomly.
Definition graph.hpp:55
const std::size_t _num_spins
total number of spins
Definition graph.hpp:39
std::size_t get_num_spins() const noexcept
get number of spins
Definition graph.hpp:89
const Binaries gen_binary(RandomNumberEngine &random_numder_engine) const
generate spins randomly.
Definition graph.hpp:73
std::size_t size() const noexcept
get number of spins
Definition graph.hpp:96
std::vector< Binary > Binaries
Definition graph.hpp:29
auto json_parse(const json &obj, bool relabel=true)
parse json object from bqm.to_serializable
Definition parse.hpp:50
std::vector< Index > Nodes
Definition graph.hpp:32
int Binary
Definition graph.hpp:28
std::vector< Spin > Spins
Definition graph.hpp:27
int Spin
Definition graph.hpp:26
std::size_t Index
Definition graph.hpp:30
Definition algorithm.hpp:24