openjij
Framework for the Ising model and QUBO.
Loading...
Searching...
No Matches
parse.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 <exception>
18#include <tuple>
19#include <vector>
20#include <numeric>
21
22#include <nlohmann/json.hpp>
23
24#include <cimod/binary_polynomial_model.hpp>
25#include <cimod/binary_quadratic_model.hpp>
26#include <cimod/binary_quadratic_model_dict.hpp>
27
29
30namespace openjij {
31namespace graph {
32
33using json = nlohmann::json;
34
49template <typename FloatType, typename CimodDataType>
50inline auto json_parse(const json &obj, bool relabel = true) {
51
52 using namespace cimod;
53 // convert variable_labels
54 json temp = obj;
55 temp["type"] = "BinaryQuadraticModel";
56 if (relabel) {
57 // re-labeling
58 std::size_t num_variables = temp["num_variables"];
59 std::vector<size_t> variables(num_variables);
60 // generate sequence numbers
61 std::iota(variables.begin(), variables.end(), 0);
62 temp["variable_labels"] = variables;
63 }
64 // make cimod object and apply to_serializable function
65 auto bqm =
66 BinaryQuadraticModel<size_t, FloatType, CimodDataType>::from_serializable(
67 temp);
68 return bqm.change_vartype(Vartype::SPIN, false);
69}
70
71template <typename FloatType>
72inline auto json_parse_polynomial(const nlohmann::json &obj,
73 const bool relabel = true) {
74
75 if (obj.at("type") != "BinaryPolynomialModel") {
76 throw std::runtime_error("Type must be \"BinaryPolynomialModel\".\n");
77 }
78
79 if (obj.at("poly_key_distance_list").size() !=
80 obj.at("poly_value_list").size()) {
81 throw std::runtime_error(
82 "The sizes of key_list and value_list must match each other");
83 }
84
85 const std::size_t num_variables = obj["variables"].size();
87 static_cast<int64_t>(obj["poly_value_list"].size());
88
89 if (num_interactions == 0) {
90 throw std::runtime_error("The interaction is empty.");
91 }
92
93 const cimod::PolynomialKeyList<std::size_t> &poly_key_distance_list =
94 obj["poly_key_distance_list"];
95 const cimod::PolynomialValueList<FloatType> &poly_value_list =
96 obj["poly_value_list"];
97 cimod::PolynomialKeyList<Index> poly_key_list(num_interactions);
98
99 if (relabel) {
100 std::vector<std::size_t> sorted_variables;
101 sorted_variables.resize(num_variables);
102 std::iota(sorted_variables.begin(), sorted_variables.end(), 0);
103#pragma omp parallel for
104 for (int64_t i = 0; i < num_interactions; ++i) {
105 std::vector<Index> temp;
106 for (const auto &it : poly_key_distance_list[i]) {
107 temp.push_back(sorted_variables[it]);
108 }
109 std::sort(temp.begin(), temp.end());
110 poly_key_list[i] = temp;
111 }
112 } else {
113 const std::vector<Index> &variables = obj["variables"];
114#pragma omp parallel for
115 for (int64_t i = 0; i < num_interactions; ++i) {
116 std::vector<Index> temp;
117 for (const auto &it : poly_key_distance_list[i]) {
118 temp.push_back(variables[it]);
119 }
120 std::sort(temp.begin(), temp.end());
121 poly_key_list[i] = temp;
122 }
123 }
124 return std::tuple<cimod::PolynomialKeyList<Index>,
125 cimod::PolynomialValueList<FloatType>>(poly_key_list,
127}
128
129} // namespace graph
130} // namespace openjij
auto json_parse(const json &obj, bool relabel=true)
parse json object from bqm.to_serializable
Definition parse.hpp:50
nlohmann::json json
Definition parse.hpp:33
auto json_parse_polynomial(const nlohmann::json &obj, const bool relabel=true)
Definition parse.hpp:72
Definition algorithm.hpp:24