17 #include <unordered_map>
18 #include <unordered_set>
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 ) {
36 if ( um.count( key ) == 0 ) {
37 um.insert( { { key, val } } );
49 template<
typename IndexType>
51 if ( ( *key ).size() <= 1 ) {
53 }
else if ( ( *key ).size() == 2 ) {
54 if ( ( *key )[ 0 ] == ( *key )[ 1 ] ) {
62 throw std::runtime_error(
"Unknown vartype detected" );
64 }
else if ( ( *key )[ 0 ] < ( *key )[ 1 ] ) {
67 std::swap( ( *key )[ 0 ], ( *key )[ 1 ] );
71 std::sort( ( *key ).begin(), ( *key ).end() );
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() );
78 std::swap( ( *key )[ i ], ( *key ).back() );
84 ( *key ).erase( std::unique( ( *key ).begin(), ( *key ).end() ), ( *key ).end() );
87 throw std::runtime_error(
"Unknown vartype detected" );
96 if ( vartype_str ==
"SPIN" ) {
98 }
else if ( vartype_str ==
"BINARY" ) {
101 throw std::runtime_error(
"Unknown vartype detected" );
109 template<
typename IntegerType>
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" );
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" );
124 throw std::runtime_error(
"Unknown vartype detected" );
132 throw std::runtime_error(
"Unknow vartype detected" );
vartype
Definition: legacy/binary_quadratic_model.py:182
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