25 #include <unordered_map>
26 #include <unordered_set>
30 #include <Eigen/Dense>
47 template<
typename IndexType,
typename FloatType>
52 using DenseMatrix = Eigen::Matrix<FloatType, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
53 using SparseMatrix = Eigen::SparseMatrix<FloatType, Eigen::RowMajor>;
58 using Matrix = Eigen::Matrix<FloatType, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
97 const FloatType &offset,
130 const Eigen::Ref<const DenseMatrix> &,
131 const std::vector<IndexType> &,
135 throw std::runtime_error(
"Initialization from matrix is not implemented on dict-type BQM" );
148 throw std::runtime_error(
"Initialization from matrix is not implemented on dict-type BQM" );
162 throw std::runtime_error(
"Initialization from matrix is not implemented on dict-type BQM" );
175 throw std::runtime_error(
"Initialization from matrix is not implemented on dict-type BQM" );
191 std::unordered_set<IndexType> index_set;
192 for (
auto elem : m_linear ) {
193 index_set.insert( elem.first );
196 for (
auto elem : m_quadratic ) {
197 index_set.insert( elem.first.first );
198 index_set.insert( elem.first.second );
201 auto ret = std::vector<IndexType>( index_set.begin(), index_set.end() );
202 std::sort( ret.begin(), ret.end() );
223 return m_linear.size();
233 if ( m_linear.count( v ) != 0 ) {
248 return this->m_linear.at( label_i );
257 return this->m_linear;
266 return this->m_quadratic.at( std::make_pair( std::min( label_i, label_j ), std::max( label_i, label_j ) ) );
275 return this->m_quadratic;
302 std::vector<IndexType> variables;
303 variables.reserve( m_linear.size() );
304 for (
auto &&elem : m_linear ) {
305 variables.push_back( elem.first );
308 std::sort( variables.begin(), variables.end() );
344 throw std::runtime_error(
"Unknown vartype" );
350 if ( m_linear.count( v ) != 0 ) {
351 value = m_linear[ v ];
362 for (
auto &it : linear ) {
374 void add_interaction(
const IndexType &arg_u,
const IndexType &arg_v,
const FloatType &bias ) {
375 IndexType u = std::min( arg_u, arg_v );
376 IndexType v = std::max( arg_u, arg_v );
381 throw std::runtime_error(
"No self-loops allowed" );
388 throw std::runtime_error(
389 "Binary quadratic model is empty. Please set vartype to Vartype::SPIN or Vartype::BINARY" );
408 throw std::runtime_error(
"Unknown vartype" );
411 if ( m_linear.count( u ) == 0 ) {
414 if ( m_linear.count( v ) == 0 ) {
420 std::pair<IndexType, IndexType> p1 = std::make_pair( u, v );
421 if ( m_quadratic.count( p1 ) != 0 ) {
422 value = m_quadratic[ p1 ];
434 for (
auto &it : quadratic ) {
445 std::vector<std::pair<IndexType, IndexType>> interactions;
446 for (
auto &it : m_quadratic ) {
447 if ( it.first.first == v || it.first.second == v ) {
448 interactions.push_back( it.first );
461 for (
auto &it : variables ) {
473 IndexType u = std::min( arg_u, arg_v );
474 IndexType v = std::max( arg_u, arg_v );
475 auto p = std::make_pair( u, v );
476 if ( m_quadratic.count( p ) != 0 ) {
477 m_quadratic.erase( p );
481 for (
auto &it : m_quadratic ) {
482 if ( ( it.first.first == u ) || ( it.first.second == u ) ) {
488 if ( u_flag && ( m_linear[ u ] == 0 ) ) {
493 for (
auto &it : m_quadratic ) {
494 if ( ( it.first.first == v ) || ( it.first.second == v ) ) {
500 if ( v_flag && ( m_linear[ v ] == 0 ) ) {
511 for (
auto &it : interactions ) {
541 const FloatType &scalar,
542 const std::vector<IndexType> &ignored_variables = {},
543 const std::vector<std::pair<IndexType, IndexType>> &ignored_interactions = {},
544 const bool ignored_offset = false ) {
546 for (
auto &it : m_linear ) {
547 if ( std::find( ignored_variables.begin(), ignored_variables.end(), it.first ) != ignored_variables.end()
548 || ignored_variables.empty() ) {
554 for (
auto &it : m_quadratic ) {
555 if ( std::find( ignored_interactions.begin(), ignored_interactions.end(), it.first ) != ignored_interactions.end()
556 || ignored_variables.empty() ) {
562 if ( !ignored_offset ) {
580 const std::pair<FloatType, FloatType> &bias_range = { 1.0, 1.0 },
581 const bool use_quadratic_range =
false,
582 const std::pair<FloatType, FloatType> &quadratic_range = { 1.0, 1.0 },
583 const std::vector<IndexType> &ignored_variables = {},
584 const std::vector<std::pair<IndexType, IndexType>> &ignored_interactions = {},
585 const bool ignored_offset = false ) {
586 if ( m_linear.empty() ) {
590 std::pair<FloatType, FloatType> l_range = bias_range;
591 std::pair<FloatType, FloatType> q_range;
592 if ( !use_quadratic_range ) {
593 q_range = bias_range;
595 q_range = quadratic_range;
599 auto comp = [](
const auto &a,
const auto &b ) {
return a.second < b.second; };
600 auto it_lin_min = std::min_element( m_linear.begin(), m_linear.end(), comp );
601 auto it_lin_max = std::max_element( m_linear.begin(), m_linear.end(), comp );
602 auto it_quad_min = std::min_element( m_quadratic.begin(), m_quadratic.end(), comp );
603 auto it_quad_max = std::max_element( m_quadratic.begin(), m_quadratic.end(), comp );
605 std::vector<FloatType> v_scale
606 = { it_lin_min->second / l_range.first,
607 it_lin_max->second / l_range.second,
608 it_quad_min->second / q_range.first,
609 it_quad_max->second / q_range.second };
610 FloatType inv_scale = *std::max_element( v_scale.begin(), v_scale.end() );
613 if ( inv_scale != 0.0 ) {
614 scale( 1.0 / inv_scale, ignored_variables, ignored_interactions, ignored_offset );
625 std::vector<std::pair<IndexType, IndexType>> interactions;
626 for (
auto &it : m_quadratic ) {
627 if ( it.first.first == v ) {
629 interactions.push_back( it.first );
630 }
else if ( it.first.second == v ) {
632 interactions.push_back( it.first );
645 void fix_variables(
const std::vector<std::pair<IndexType, int32_t>> &fixed ) {
646 for (
auto &it : fixed ) {
658 if ( m_linear.count( v ) == 0 ) {
659 throw std::runtime_error(
"not a variable in the binary quadratic model." );
663 m_linear[ v ] *= -1.0;
664 for (
auto &it : m_quadratic ) {
665 if ( it.first.first == v || it.first.second == v ) {
671 m_linear[ v ] *= -1.0;
673 for (
auto &it : m_quadratic ) {
674 if ( it.first.first == v ) {
675 m_linear[ it.first.second ] += it.second;
677 }
else if ( it.first.second == v ) {
678 m_linear[ it.first.first ] += it.second;
693 if ( m_linear.count( v ) == 0 ) {
694 throw std::runtime_error(
"not a variable in the binary quadratic model." );
696 if ( m_linear.count( u ) == 0 ) {
697 throw std::runtime_error(
"not a variable in the binary quadratic model." );
700 auto p1 = std::make_pair( u, v );
701 auto p2 = std::make_pair( v, u );
702 if ( m_quadratic.count( p1 ) != 0 ) {
710 if ( m_quadratic.count( p2 ) != 0 ) {
719 std::vector<std::pair<IndexType, IndexType>> interactions;
720 for (
auto &it : m_quadratic ) {
721 if ( it.first.first == v ) {
723 interactions.push_back( it.first );
724 }
else if ( it.first.second == v ) {
726 interactions.push_back( it.first );
746 FloatType offset = 0.0;
750 std::tie( linear, quadratic, offset ) = binary_to_spin( m_linear, m_quadratic,
m_offset );
753 std::tie( linear, quadratic, offset ) = spin_to_binary( m_linear, m_quadratic,
m_offset );
755 std::tie( linear, quadratic, offset ) = std::tie( m_linear, m_quadratic,
m_offset );
760 m_quadratic = quadratic;
775 FloatType offset = 0.0;
779 std::tie( linear, quadratic, offset ) = binary_to_spin( m_linear, m_quadratic,
m_offset );
782 std::tie( linear, quadratic, offset ) = spin_to_binary( m_linear, m_quadratic,
m_offset );
784 std::tie( linear, quadratic, offset ) = std::tie( m_linear, m_quadratic,
m_offset );
789 if ( inplace ==
true ) {
814 const FloatType &offset ) {
817 FloatType new_offset = offset;
818 FloatType linear_offset = 0.0;
819 FloatType quadratic_offset = 0.0;
821 for (
auto &it : linear ) {
822 insert_or_assign( new_linear, it.first,
static_cast<FloatType
>( 2.0 * it.second ) );
823 linear_offset += it.second;
826 for (
auto &it : quadratic ) {
827 insert_or_assign( new_quadratic, it.first,
static_cast<FloatType
>( 4.0 * it.second ) );
828 new_linear[ it.first.first ] -= 2.0 * it.second;
829 new_linear[ it.first.second ] -= 2.0 * it.second;
830 quadratic_offset += it.second;
833 new_offset += quadratic_offset - linear_offset;
835 return std::make_tuple( new_linear, new_quadratic, new_offset );
851 const FloatType &offset ) {
854 FloatType new_offset = offset;
855 FloatType linear_offset = 0.0;
856 FloatType quadratic_offset = 0.0;
858 for (
auto &it : linear ) {
860 linear_offset += it.second;
863 for (
auto &it : quadratic ) {
864 insert_or_assign( J, it.first,
static_cast<FloatType
>( 0.25 * it.second ) );
865 h[ it.first.first ] += 0.25 * it.second;
866 h[ it.first.second ] += 0.25 * it.second;
867 quadratic_offset += it.second;
870 new_offset += 0.5 * linear_offset + 0.25 * quadratic_offset;
872 return std::make_tuple( h, J, new_offset );
885 for (
auto &&it : m_linear ) {
887 en +=
static_cast<FloatType
>( sample.at( it.first ) ) * it.second;
890 for (
auto &it : m_quadratic ) {
893 en +=
static_cast<FloatType
>( sample.at( it.first.first ) )
894 *
static_cast<FloatType
>( sample.at( it.first.second ) ) * it.second;
907 std::vector<FloatType> en_vec;
908 for (
auto &it : samples_like ) {
909 en_vec.push_back(
energy( it ) );
920 std::tuple<Quadratic<IndexType, FloatType>, FloatType>
to_qubo() {
927 for (
auto &it : linear ) {
930 return std::make_tuple( Q, offset );
945 for (
auto &&elem : Q ) {
946 const auto &key = elem.first;
947 const auto &value = elem.second;
948 if ( key.first == key.second ) {
949 linear[ key.first ] = value;
951 quadratic[ std::make_pair( key.first, key.second ) ] = value;
970 return std::make_tuple( linear, quadratic, offset );
985 FloatType offset = 0.0 ) {
1028 size_t system_size = indices.size();
1033 for (
size_t i = 0; i < indices.size(); i++ ) {
1034 const IndexType &i_index = indices[ i ];
1035 _interaction_matrix( i, i ) = ( linear.find( i_index ) != linear.end() ) ? linear.at( i_index ) : 0;
1036 for (
size_t j = i + 1; j < indices.size(); j++ ) {
1037 const IndexType &j_index = indices[ j ];
1038 FloatType jval = 0.0;
1040 if ( quadratic.find( std::make_pair( i_index, j_index ) ) != quadratic.end() ) {
1041 jval += quadratic.at( std::make_pair( i_index, j_index ) );
1043 if ( quadratic.find( std::make_pair( j_index, i_index ) ) != quadratic.end() ) {
1044 jval += quadratic.at( std::make_pair( j_index, i_index ) );
1077 for (
size_t i = 0; i < indices.size(); i++ ) {
1078 const IndexType &i_index = indices[ i ];
1079 _interaction_matrix( i, system_size ) = ( linear.find( i_index ) != linear.end() ) ? linear.at( i_index ) : 0;
1080 for (
size_t j = i + 1; j < indices.size(); j++ ) {
1081 const IndexType &j_index = indices[ j ];
1082 FloatType jval = 0.0;
1084 if ( quadratic.find( std::make_pair( i_index, j_index ) ) != quadratic.end() ) {
1085 jval += quadratic.at( std::make_pair( i_index, j_index ) );
1087 if ( quadratic.find( std::make_pair( j_index, i_index ) ) != quadratic.end() ) {
1088 jval += quadratic.at( std::make_pair( j_index, i_index ) );
1107 std::string schema_version =
"3.0.0";
1123 std::vector<IndexType> variables;
1124 variables.reserve( m_linear.size() );
1125 for (
auto &&elem : m_linear ) {
1126 variables.push_back( elem.first );
1129 std::sort( variables.begin(), variables.end() );
1131 size_t num_variables = variables.size();
1134 std::vector<FloatType> l_bias;
1135 for (
auto &&key : variables ) {
1136 l_bias.push_back( m_linear.at( key ) );
1140 std::vector<size_t> q_head, q_tail;
1141 std::vector<FloatType> q_bias;
1142 for (
auto &&elem : m_quadratic ) {
1143 auto it_head = std::find( variables.begin(), variables.end(), elem.first.first );
1144 auto it_tail = std::find( variables.begin(), variables.end(), elem.first.second );
1145 size_t idx_head = std::distance( variables.begin(), it_head );
1146 size_t idx_tail = std::distance( variables.begin(), it_tail );
1147 q_head.push_back( idx_head );
1148 q_tail.push_back( idx_tail );
1149 q_bias.push_back( elem.second );
1152 size_t num_interactions = m_quadratic.size();
1155 std::string index_dtype = num_variables <= 65536UL ?
"uint16" :
"uint32";
1158 std::string bias_type;
1159 if (
typeid(
m_offset ) ==
typeid(
float ) ) {
1160 bias_type =
"float32";
1161 }
else if (
typeid(
m_offset ) ==
typeid(
double ) ) {
1162 bias_type =
"float64";
1164 throw std::runtime_error(
"FloatType must be float or double." );
1168 std::string variable_type;
1170 variable_type =
"SPIN";
1172 variable_type =
"BINARY";
1174 throw std::runtime_error(
"Variable type must be SPIN or BINARY." );
1178 output[
"type" ] =
"BinaryQuadraticModel";
1179 output[
"version" ] = { {
"bqm_schema",
"3.0.0" } };
1180 output[
"variable_labels" ] = variables;
1181 output[
"use_bytes" ] =
false;
1182 output[
"index_type" ] = index_dtype;
1183 output[
"bias_type" ] = bias_type;
1184 output[
"num_variables" ] = num_variables;
1185 output[
"num_interactions" ] = num_interactions;
1186 output[
"variable_labels" ] = variables;
1187 output[
"variable_type" ] = variable_type;
1189 output[
"info" ] =
"";
1190 output[
"linear_biases" ] = l_bias;
1191 output[
"quadratic_biases" ] = q_bias;
1192 output[
"quadratic_head" ] = q_head;
1193 output[
"quadratic_tail" ] = q_tail;
1206 template<
typename IndexType_serial = IndexType,
typename FloatType_serial = FloatType>
1209 std::string type = input[
"type" ];
1210 if ( type !=
"BinaryQuadraticModel" ) {
1211 throw std::runtime_error(
"Type must be \"BinaryQuadraticModel\".\n" );
1213 std::string version = input[
"version" ][
"bqm_schema" ];
1214 if ( version !=
"3.0.0" ) {
1215 throw std::runtime_error(
"bqm_schema must be 3.0.0.\n" );
1220 std::string variable_type = input[
"variable_type" ];
1221 if ( variable_type ==
"SPIN" ) {
1223 }
else if ( variable_type ==
"BINARY" ) {
1226 throw std::runtime_error(
"variable_type must be SPIN or BINARY." );
1231 std::vector<IndexType_serial> variables = input[
"variable_labels" ];
1232 std::vector<FloatType_serial> l_bias = input[
"linear_biases" ];
1233 for (
size_t i = 0; i < variables.size(); ++i ) {
1239 std::vector<size_t> q_head = input[
"quadratic_head" ];
1240 std::vector<size_t> q_tail = input[
"quadratic_tail" ];
1241 std::vector<FloatType_serial> q_bias = input[
"quadratic_biases" ];
1242 for (
size_t i = 0; i < q_head.size(); ++i ) {
1243 insert_or_assign( quadratic, std::make_pair( variables[ q_head[ i ] ], variables[ q_tail[ i ] ] ), q_bias[ i ] );
1247 FloatType_serial offset = input[
"offset" ];
Linear< IndexType, FloatType > m_linear
Linear biases as a dictionary.
Definition: binary_quadratic_model_dict.hpp:65
void add_interaction(const IndexType &arg_u, const IndexType &arg_v, const FloatType &bias)
Add an interaction and/or quadratic bias to a binary quadratic model.
Definition: binary_quadratic_model_dict.hpp:374
void remove_variables_from(const std::vector< IndexType > &variables)
Remove specified variables and all of their interactions from a binary quadratic model.
Definition: binary_quadratic_model_dict.hpp:460
BinaryQuadraticModel(const Linear< IndexType, FloatType > &linear, const Quadratic< IndexType, FloatType > &quadratic, const FloatType &offset, const Vartype vartype)
BinaryQuadraticModel constructor.
Definition: binary_quadratic_model_dict.hpp:94
const Vartype & get_vartype() const
Get the vartype object.
Definition: binary_quadratic_model_dict.hpp:292
FloatType energy(const Sample< IndexType > &sample) const
Determine the energy of the specified sample of a binary quadratic model.
Definition: binary_quadratic_model_dict.hpp:883
Quadratic< IndexType, FloatType > m_quadratic
Quadratic biases as a dictionary.
Definition: binary_quadratic_model_dict.hpp:71
FloatType m_offset
The energy offset associated with the model.
Definition: binary_quadratic_model_dict.hpp:77
const Linear< IndexType, FloatType > & get_linear() const
Get the linear object.
Definition: binary_quadratic_model_dict.hpp:256
BinaryQuadraticModel(const Eigen::Ref< const DenseMatrix > &, const std::vector< IndexType > &, const Vartype, bool)
BinaryQuadraticModel constructor (with matrix); This constructor is not be implemented.
Definition: binary_quadratic_model_dict.hpp:147
Matrix interaction_matrix(const std::vector< IndexType > &indices) const
generate interaction matrix with given list of indices The generated matrix will be the following sym...
Definition: binary_quadratic_model_dict.hpp:1026
FloatType get_quadratic(IndexType label_i, IndexType label_j) const
Get the element of quadratic object.
Definition: binary_quadratic_model_dict.hpp:265
std::vector< IndexType > _generate_indices() const
generate indices
Definition: binary_quadratic_model_dict.hpp:190
static BinaryQuadraticModel< IndexType_serial, FloatType_serial, DataType > from_serializable(const json &input)
Create a BinaryQuadraticModel instance from a serializable object.
Definition: binary_quadratic_model_dict.hpp:1207
void fix_variable(const IndexType &v, const int32_t &value)
Fix the value of a variable and remove it from a binary quadratic model.
Definition: binary_quadratic_model_dict.hpp:624
BinaryQuadraticModel< IndexType, FloatType, DataType > empty(Vartype vartype)
Create an empty BinaryQuadraticModel.
Definition: binary_quadratic_model_dict.hpp:317
void remove_variable(const IndexType &v)
Remove variable v and all its interactions from a binary quadratic model.
Definition: binary_quadratic_model_dict.hpp:444
void remove_interactions_from(const std::vector< std::pair< IndexType, IndexType >> &interactions)
Remove all specified interactions from the binary quadratic model.
Definition: binary_quadratic_model_dict.hpp:510
void flip_variable(const IndexType &v)
Flip variable v in a binary quadratic model.
Definition: binary_quadratic_model_dict.hpp:656
BinaryQuadraticModel(const BinaryQuadraticModel &)=default
Copy constructor of BinaryQuadraticModel.
static std::tuple< Linear< IndexType, FloatType >, Quadratic< IndexType, FloatType >, FloatType > spin_to_binary(const Linear< IndexType, FloatType > &linear, const Quadratic< IndexType, FloatType > &quadratic, const FloatType &offset)
Convert linear, quadratic, and offset from spin to binary.
Definition: binary_quadratic_model_dict.hpp:811
std::tuple< Linear< IndexType, FloatType >, Quadratic< IndexType, FloatType >, FloatType > to_ising()
Convert a binary quadratic model to Ising format.
Definition: binary_quadratic_model_dict.hpp:963
const FloatType & get_offset() const
Get the offset.
Definition: binary_quadratic_model_dict.hpp:283
size_t length() const
Return the number of variables.
Definition: binary_quadratic_model_dict.hpp:212
std::tuple< Quadratic< IndexType, FloatType >, FloatType > to_qubo()
Convert a binary quadratic model to QUBO format.
Definition: binary_quadratic_model_dict.hpp:920
static BinaryQuadraticModel from_ising(const Linear< IndexType, FloatType > &linear, const Quadratic< IndexType, FloatType > &quadratic, FloatType offset=0.0)
Create a binary quadratic model from an Ising problem.
Definition: binary_quadratic_model_dict.hpp:982
void add_offset(const FloatType &offset)
Add specified value to the offset of a binary quadratic model.
Definition: binary_quadratic_model_dict.hpp:521
Matrix interaction_matrix() const
generate interaction matrix with given list of indices The generated matrix will be the following sym...
Definition: binary_quadratic_model_dict.hpp:1069
BinaryQuadraticModel(const Eigen::Ref< const DenseMatrix > &, const std::vector< IndexType > &, const FloatType &, const Vartype, bool)
BinaryQuadraticModel constructor (with matrix); This constructor is not be implemented.
Definition: binary_quadratic_model_dict.hpp:129
Eigen::Matrix< FloatType, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > Matrix
Eigen Matrix.
Definition: binary_quadratic_model_dict.hpp:58
void change_vartype(const Vartype &vartype)
Create a binary quadratic model with the specified vartype.
Definition: binary_quadratic_model_dict.hpp:743
static std::tuple< Linear< IndexType, FloatType >, Quadratic< IndexType, FloatType >, FloatType > binary_to_spin(const Linear< IndexType, FloatType > &linear, const Quadratic< IndexType, FloatType > &quadratic, const FloatType &offset)
Convert linear, quadratic and offset from binary to spin.
Definition: binary_quadratic_model_dict.hpp:848
nlohmann::json json
Definition: binary_quadratic_model_dict.hpp:1098
BinaryQuadraticModel(const SparseMatrix &, const std::vector< IndexType > &, const Vartype)
BinaryQuadraticModel constructor (with sparse matrix); this constructor is for developers.
Definition: binary_quadratic_model_dict.hpp:174
BinaryQuadraticModel(const Linear< IndexType, FloatType > &linear, const Quadratic< IndexType, FloatType > &quadratic, const Vartype vartype)
BinaryQuadraticModel constructor.
Definition: binary_quadratic_model_dict.hpp:112
void normalize(const std::pair< FloatType, FloatType > &bias_range={ 1.0, 1.0 }, const bool use_quadratic_range=false, const std::pair< FloatType, FloatType > &quadratic_range={ 1.0, 1.0 }, const std::vector< IndexType > &ignored_variables={}, const std::vector< std::pair< IndexType, IndexType >> &ignored_interactions={}, const bool ignored_offset=false)
Normalizes the biases of the binary quadratic model such that they fall in the provided range(s),...
Definition: binary_quadratic_model_dict.hpp:579
json to_serializable() const
Convert the binary quadratic model to a serializable object user_bytes is assume to be set to False.
Definition: binary_quadratic_model_dict.hpp:1106
void add_variable(const IndexType &v, const FloatType &bias)
Add variable v and/or its bias to a binary quadratic model.
Definition: binary_quadratic_model_dict.hpp:330
void remove_interaction(const IndexType &arg_u, const IndexType &arg_v)
Remove interaction of variables u, v from a binary quadratic model.
Definition: binary_quadratic_model_dict.hpp:472
const Quadratic< IndexType, FloatType > & get_quadratic() const
Get the quadratic object.
Definition: binary_quadratic_model_dict.hpp:274
size_t get_num_variables() const
Return the number of variables.
Definition: binary_quadratic_model_dict.hpp:222
void add_interactions_from(const Quadratic< IndexType, FloatType > &quadratic)
Add interactions and/or quadratic biases to a binary quadratic model.
Definition: binary_quadratic_model_dict.hpp:433
void scale(const FloatType &scalar, const std::vector< IndexType > &ignored_variables={}, const std::vector< std::pair< IndexType, IndexType >> &ignored_interactions={}, const bool ignored_offset=false)
Multiply by the specified scalar all the biases and offset of a binary quadratic model.
Definition: binary_quadratic_model_dict.hpp:540
const std::vector< IndexType > get_variables() const
Get variables.
Definition: binary_quadratic_model_dict.hpp:301
void fix_variables(const std::vector< std::pair< IndexType, int32_t >> &fixed)
Fix the value of the variables and remove it from a binary quadratic model.
Definition: binary_quadratic_model_dict.hpp:645
bool contains(const IndexType &v) const
Return true if the variable contains v.
Definition: binary_quadratic_model_dict.hpp:232
BinaryQuadraticModel(const SparseMatrix &, const std::vector< IndexType > &, const FloatType &, const Vartype)
BinaryQuadraticModel constructor (with sparse matrix); this constructor is for developers.
Definition: binary_quadratic_model_dict.hpp:161
std::vector< FloatType > energies(const std::vector< Sample< IndexType >> &samples_like) const
Determine the energies of the given samples.
Definition: binary_quadratic_model_dict.hpp:906
Eigen::SparseMatrix< FloatType, Eigen::RowMajor > SparseMatrix
Definition: binary_quadratic_model_dict.hpp:53
FloatType get_linear(IndexType label_i) const
Get the element of linear object.
Definition: binary_quadratic_model_dict.hpp:247
void remove_offset()
Set the binary quadratic model's offset to zero.
Definition: binary_quadratic_model_dict.hpp:528
void contract_variables(const IndexType &u, const IndexType &v)
Enforce u, v being the same variable in a binary quadratic model.
Definition: binary_quadratic_model_dict.hpp:691
void add_variables_from(const Linear< IndexType, FloatType > &linear)
Add variables and/or linear biases to a binary quadratic model.
Definition: binary_quadratic_model_dict.hpp:361
Eigen::Matrix< FloatType, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > DenseMatrix
Definition: binary_quadratic_model_dict.hpp:52
BinaryQuadraticModel change_vartype(const Vartype &vartype, bool inplace)
Create a binary quadratic model with the specified vartype.
Definition: binary_quadratic_model_dict.hpp:772
static BinaryQuadraticModel from_qubo(const Quadratic< IndexType, FloatType > &Q, FloatType offset=0.0)
Create a binary quadratic model from a QUBO model.
Definition: binary_quadratic_model_dict.hpp:941
Class for dense binary quadratic model.
Definition: binary_quadratic_model.hpp:148
size_t get_num_variables() const
get the number of variables
Definition: binary_quadratic_model.hpp:1268
void add_variables_from(const Linear< IndexType, FloatType > &linear)
Add variables and/or linear biases to a binary quadratic model.
Definition: binary_quadratic_model.hpp:1388
void add_interaction(const IndexType &u, const IndexType &v, const FloatType &bias)
Add an interaction and/or quadratic bias to a binary quadratic model.
Definition: binary_quadratic_model.hpp:1401
Vartype get_vartype() const
Get the vartype object.
Definition: binary_quadratic_model.hpp:1346
const std::vector< IndexType > & get_variables() const
Get variables.
Definition: binary_quadratic_model.hpp:1355
FloatType m_offset
The energy offset associated with the model.
Definition: binary_quadratic_model.hpp:207
void remove_interaction(const IndexType &u, const IndexType &v)
Remove interaction of variables u, v from a binary quadratic model.
Definition: binary_quadratic_model.hpp:1445
void add_offset(const FloatType &offset)
Add specified value to the offset of a binary quadratic model.
Definition: binary_quadratic_model.hpp:1467
FloatType get_linear(IndexType label_i) const
Get the element of linear object.
Definition: binary_quadratic_model.hpp:1301
void add_variable(const IndexType &v, const FloatType &bias)
Add variable v and/or its bias to a binary quadratic model.
Definition: binary_quadratic_model.hpp:1377
void scale(const FloatType &scalar, const std::vector< IndexType > &ignored_variables={}, const std::vector< std::pair< IndexType, IndexType >> &ignored_interactions={}, const bool ignored_offset=false)
Multiply by the specified scalar all the biases and offset of a binary quadratic model.
Definition: binary_quadratic_model.hpp:1486
FloatType get_offset() const
Get the offset.
Definition: binary_quadratic_model.hpp:1337
Vartype m_vartype
The model's type.
Definition: binary_quadratic_model.hpp:213
void remove_interactions_from(const std::vector< std::pair< IndexType, IndexType >> &interactions)
Remove all specified interactions from the binary quadratic model.
Definition: binary_quadratic_model.hpp:1456
void remove_variable(const IndexType &v)
Remove variable v and all its interactions from a binary quadratic model.
Definition: binary_quadratic_model.hpp:1424
void fix_variable(const IndexType &v, const int32_t &value)
Fix the value of a variable and remove it from a binary quadratic model.
Definition: binary_quadratic_model.hpp:1564
FloatType energy(const Sample< IndexType > &sample) const
Determine the energy of the specified sample of a binary quadratic model.
Definition: binary_quadratic_model.hpp:1686
void add_interactions_from(const Quadratic< IndexType, FloatType > &quadratic)
Add interactions and/or quadratic biases to a binary quadratic model.
Definition: binary_quadratic_model.hpp:1413
void change_vartype(const Vartype &vartype)
**
Definition: binary_quadratic_model.hpp:1649
FloatType get_quadratic(IndexType label_i, IndexType label_j) const
Get the element of quadratic object.
Definition: binary_quadratic_model.hpp:1319
vartype
Definition: legacy/binary_quadratic_model.py:182
_interaction_matrix
Definition: legacy/binary_quadratic_model.py:92
Definition: binary_polynomial_model.hpp:139
bool check_vartype(const int32_t &var, const Vartype &vartype)
Check that the variable has appropriate value.
Definition: vartypes.hpp:37
std::unordered_map< IndexType, int32_t > Sample
Type alias for sample, which represents the spin or binary configurations.
Definition: binary_polynomial_model.hpp:162
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
std::unordered_map< IndexType, FloatType > Linear
Type alias for linear bias.
Definition: binary_quadratic_model.hpp:111
std::unordered_map< std::pair< IndexType, IndexType >, FloatType, pair_hash > Quadratic
Type alias for quadratic bias.
Definition: binary_quadratic_model.hpp:119
Vartype
Enum class for representing problem type.
Definition: vartypes.hpp:24
Definition: binary_quadratic_model_dict.hpp:41