cimod
C++ library for a binary (and polynomial) quadratic model.
Loading...
Searching...
No Matches
utilities.hpp
Go to the documentation of this file.
1// Copyright 2020-2025 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 <unordered_map>
18#include <unordered_set>
19
20#include "cimod/vartypes.hpp"
21
22namespace cimod {
33 template<class C_key, class C_value, class Hash>
34 void insert_or_assign( std::unordered_map<C_key, C_value, Hash> &um, const C_key &key, const C_value &val ) {
35 // insert
36 if ( um.count( key ) == 0 ) {
37 um.insert( { { key, val } } );
38 }
39 // assign
40 else {
41 um[ key ] = val;
42 }
43 }
44
49 template<typename IndexType>
50 void FormatPolynomialKey( std::vector<IndexType> *key, const Vartype &vartype ) {
51 if ( ( *key ).size() <= 1 ) {
52 return;
53 } else if ( ( *key ).size() == 2 ) {
54 if ( ( *key )[ 0 ] == ( *key )[ 1 ] ) {
55 if ( vartype == Vartype::SPIN ) {
56 ( *key ).clear();
57 return;
58 } else if ( vartype == Vartype::BINARY ) {
59 ( *key ).pop_back();
60 return;
61 } else {
62 throw std::runtime_error( "Unknown vartype detected" );
63 }
64 } else if ( ( *key )[ 0 ] < ( *key )[ 1 ] ) {
65 return;
66 } else {
67 std::swap( ( *key )[ 0 ], ( *key )[ 1 ] );
68 return;
69 }
70 } else {
71 std::sort( ( *key ).begin(), ( *key ).end() );
72 if ( vartype == Vartype::SPIN ) {
73 for ( int64_t i = static_cast<int64_t>( ( *key ).size() ) - 1; i > 0; --i ) {
74 if ( ( *key )[ i ] == ( *key )[ i - 1 ] ) {
75 std::swap( ( *key )[ i ], ( *key ).back() );
76 ( *key ).pop_back();
77 --i;
78 std::swap( ( *key )[ i ], ( *key ).back() );
79 ( *key ).pop_back();
80 }
81 }
82 return;
83 } else if ( vartype == Vartype::BINARY ) {
84 ( *key ).erase( std::unique( ( *key ).begin(), ( *key ).end() ), ( *key ).end() );
85 return;
86 } else {
87 throw std::runtime_error( "Unknown vartype detected" );
88 }
89 }
90 }
91
96 if ( vartype_str == "SPIN" ) {
97 return Vartype::SPIN;
98 } else if ( vartype_str == "BINARY" ) {
99 return Vartype::BINARY;
100 } else {
101 throw std::runtime_error( "Unknown vartype detected" );
102 }
103 }
104
109 template<typename IntegerType>
110 void CheckVariables( const std::vector<IntegerType> &configurations, const Vartype &vartype ) {
111 if ( vartype == Vartype::SPIN ) {
112 for ( const auto &v : configurations ) {
113 if ( !( v == -1 || v == +1 ) ) {
114 throw std::runtime_error( "The initial variables must be -1 or +1" );
115 }
116 }
117 } else if ( vartype == Vartype::BINARY ) {
118 for ( const auto &v : configurations ) {
119 if ( !( v == 0 || v == 1 ) ) {
120 throw std::runtime_error( "The initial variables must be 0 or 1" );
121 }
122 }
123 } else {
124 throw std::runtime_error( "Unknown vartype detected" );
125 }
126 }
127
130 void CheckVartypeNotNONE( const Vartype &vartype ) {
131 if ( vartype == cimod::Vartype::NONE ) {
132 throw std::runtime_error( "Unknow vartype detected" );
133 }
134 }
135
136} // namespace cimod
void declare_BQM(py::module &m, const std::string &name)
Definition main.hpp:41
Definition binary_polynomial_model.hpp:139
void insert_or_assign(std::unordered_map< C_key, C_value, Hash > &um, const C_key &key, const C_value &val)
Insert or assign a element of unordered_map (for C++14 or C++11)
Definition utilities.hpp:34
void CheckVartypeNotNONE(const Vartype &vartype)
Check if the input vartype is not Vartype::NONE.
Definition utilities.hpp:130
void CheckVariables(const std::vector< IntegerType > &configurations, const Vartype &vartype)
Convert vartype from string to cimod::Vartype.
Definition utilities.hpp:110
void FormatPolynomialKey(std::vector< IndexType > *key, const Vartype &vartype)
Format the input key: for example, {2,1,1}-->{1,2} for BINARY variable and {2,1,1}-->{2} for SPIN var...
Definition utilities.hpp:50
cimod::Vartype ToCimodVartype(const std::string &vartype_str)
Convert vartype from string to cimod::Vartype.
Definition utilities.hpp:95
Vartype
Enum class for representing problem type.
Definition vartypes.hpp:24