25#include <unordered_map>
26#include <unordered_set>
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>;
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" );
192 for (
auto elem : m_linear ) {
196 for (
auto elem : m_quadratic ) {
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;
275 return this->m_quadratic;
304 for (
auto &&
elem : m_linear ) {
344 throw std::runtime_error(
"Unknown vartype" );
350 if ( m_linear.count(
v ) != 0 ) {
362 for (
auto &
it : linear ) {
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 ) {
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 ) {
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 ) ) {
546 for (
auto &
it : m_linear ) {
554 for (
auto &
it : m_quadratic ) {
580 const std::pair<FloatType, FloatType> &
bias_range = { 1.0, 1.0 },
586 if ( m_linear.empty() ) {
591 std::pair<FloatType, FloatType>
q_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 );
625 std::vector<std::pair<IndexType, IndexType>>
interactions;
626 for (
auto &
it : m_quadratic ) {
627 if (
it.first.first ==
v ) {
630 }
else if (
it.first.second ==
v ) {
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 ) {
724 }
else if (
it.first.second ==
v ) {
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;
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 );
791 m_linear =
bqm.get_linear();
792 m_quadratic =
bqm.get_quadratic();
821 for (
auto &
it : linear ) {
826 for (
auto &
it : quadratic ) {
858 for (
auto &
it : linear ) {
863 for (
auto &
it : quadratic ) {
865 h[
it.first.first ] += 0.25 *
it.second;
866 h[
it.first.second ] += 0.25 *
it.second;
885 for (
auto &&
it : m_linear ) {
890 for (
auto &
it : m_quadratic ) {
907 std::vector<FloatType>
en_vec;
927 for (
auto &
it : linear ) {
930 return std::make_tuple( Q, offset );
945 for (
auto &&
elem : Q ) {
948 if (
key.first ==
key.second ) {
951 quadratic[ std::make_pair(
key.first,
key.second ) ] =
value;
970 return std::make_tuple( linear, quadratic, offset );
1033 for (
size_t i = 0;
i <
indices.size();
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++ ) {
1040 if ( quadratic.find( std::make_pair(
i_index,
j_index ) ) != quadratic.end() ) {
1043 if ( quadratic.find( std::make_pair(
j_index,
i_index ) ) != quadratic.end() ) {
1047 _interaction_matrix(
i,
j ) =
jval;
1048 _interaction_matrix(
j,
i ) =
jval;
1052 return _interaction_matrix;
1077 for (
size_t i = 0;
i <
indices.size();
i++ ) {
1080 for (
size_t j =
i + 1;
j <
indices.size();
j++ ) {
1084 if ( quadratic.find( std::make_pair(
i_index,
j_index ) ) != quadratic.end() ) {
1087 if ( quadratic.find( std::make_pair(
j_index,
i_index ) ) != quadratic.end() ) {
1091 _interaction_matrix(
i,
j ) =
jval;
1095 return _interaction_matrix;
1125 for (
auto &&
elem : m_linear ) {
1134 std::vector<FloatType>
l_bias;
1141 std::vector<FloatType>
q_bias;
1142 for (
auto &&
elem : m_quadratic ) {
1159 if (
typeid(
m_offset ) ==
typeid(
float ) ) {
1161 }
else if (
typeid(
m_offset ) ==
typeid(
double ) ) {
1164 throw std::runtime_error(
"FloatType must be float or double." );
1174 throw std::runtime_error(
"Variable type must be SPIN or BINARY." );
1178 output[
"type" ] =
"BinaryQuadraticModel";
1179 output[
"version" ] = { {
"bqm_schema",
"3.0.0" } };
1181 output[
"use_bytes" ] =
false;
1206 template<
typename IndexType_serial = IndexType,
typename FloatType_serial = FloatType>
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" );
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" ];
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 ) {
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
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
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
const FloatType & get_offset() const
Get the offset.
Definition binary_quadratic_model_dict.hpp:283
FloatType m_offset
The energy offset associated with the model.
Definition binary_quadratic_model_dict.hpp:77
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
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
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 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
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 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.
const Linear< IndexType, FloatType > & get_linear() const
Get the linear object.
Definition binary_quadratic_model_dict.hpp:256
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
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
size_t length() const
Return the number of variables.
Definition binary_quadratic_model_dict.hpp:212
std::vector< IndexType > _generate_indices() const
generate indices
Definition binary_quadratic_model_dict.hpp:190
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
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
std::tuple< Quadratic< IndexType, FloatType >, FloatType > to_qubo()
Convert a binary quadratic model to QUBO format.
Definition binary_quadratic_model_dict.hpp:920
void change_vartype(const Vartype &vartype)
Create a binary quadratic model with the specified vartype.
Definition binary_quadratic_model_dict.hpp:743
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
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
BinaryQuadraticModel(const Linear< IndexType, FloatType > &linear, const Quadratic< IndexType, FloatType > &quadratic, const Vartype vartype)
BinaryQuadraticModel constructor.
Definition binary_quadratic_model_dict.hpp:112
const std::vector< IndexType > get_variables() const
Get variables.
Definition binary_quadratic_model_dict.hpp:301
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
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
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
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
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
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
const Vartype & get_vartype() const
Get the vartype object.
Definition binary_quadratic_model_dict.hpp:292
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< IndexType, FloatType, DataType > empty(Vartype vartype)
Create an empty BinaryQuadraticModel.
Definition binary_quadratic_model_dict.hpp:317
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
const Quadratic< IndexType, FloatType > & get_quadratic() const
Get the quadratic object.
Definition binary_quadratic_model_dict.hpp:274
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 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
void add_offset(const FloatType &offset)
Add specified value to the offset of a binary quadratic model.
Definition binary_quadratic_model.hpp:1467
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
Vartype m_vartype
The model's type.
Definition binary_quadratic_model.hpp:213
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
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 declare_BQM(py::module &m, const std::string &name)
Definition main.hpp:41
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