88 #include <type_traits>
90 #include <unordered_map>
91 #include <unordered_set>
95 #include <Eigen/Dense>
96 #include <Eigen/Sparse>
110 template<
typename IndexType,
typename FloatType>
111 using Linear = std::unordered_map<IndexType, FloatType>;
118 template<
typename IndexType,
typename FloatType>
126 template<
typename IndexType,
typename FloatType>
127 using Adjacency = std::unordered_map<IndexType, std::unordered_map<IndexType, FloatType>>;
134 template<
typename IndexType>
135 using Sample = std::unordered_map<IndexType, int32_t>;
147 template<
typename IndexType,
typename FloatType,
typename DataType>
157 template<
typename T,
typename U>
158 using dispatch_t = std::enable_if_t<std::is_same_v<T, U>, std::nullptr_t>;
167 using DenseMatrix = Eigen::Matrix<FloatType, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
169 using SpIter =
typename SparseMatrix::InnerIterator;
173 using Vector = Eigen::Matrix<FloatType, Eigen::Dynamic, 1>;
237 template<
typename T = DataType>
252 template<
typename T = DataType>
267 template<
typename T = DataType>
282 template<
typename T = DataType>
295 inline FloatType &
_mat( IndexType label_i, IndexType label_j ) {
299 return _quadmat_get( std::min( i, j ), std::max( i, j ) );
301 throw std::runtime_error(
"No self-loop (mat(i,i)) allowed" );
311 inline FloatType &
_mat( IndexType label_i ) {
324 inline FloatType
_mat( IndexType label_i, IndexType label_j )
const {
329 return _quadmat_get( std::min( i, j ), std::max( i, j ) );
331 throw std::runtime_error(
"No self-loop (mat(i,i)) allowed" );
341 inline FloatType
_mat( IndexType label_i )
const {
351 template<
typename T = DataType>
354 return _quadmat.block( 0, N - 1, N - 1, 1 ).maxCoeff();
362 template<
typename T = DataType>
365 return _quadmat.block( 0, 0, N - 1, N - 1 ).maxCoeff();
373 template<
typename T = DataType>
376 return _quadmat.block( 0, N - 1, N - 1, 1 ).minCoeff();
384 template<
typename T = DataType>
387 return _quadmat.block( 0, 0, N - 1, N - 1 ).minCoeff();
395 template<
typename T = DataType>
399 mat =
_quadmat.block( 0, N - 1, N - 1, 1 );
400 return mat.coeffs().maxCoeff();
408 template<
typename T = DataType>
412 mat =
_quadmat.block( 0, 0, N - 1, N - 1 );
413 return mat.coeffs().maxCoeff();
421 template<
typename T = DataType>
425 mat =
_quadmat.block( 0, N - 1, N - 1, 1 );
426 return mat.coeffs().minCoeff();
434 template<
typename T = DataType>
438 mat =
_quadmat.block( 0, 0, N - 1, N - 1 );
439 return mat.coeffs().minCoeff();
447 template<
typename T = DataType>
455 tempmat.block( 0, 0, i, i ) =
_quadmat.block( 0, 0, i, i );
456 tempmat.block( 0, i + 1, i, N - i - 1 ) =
_quadmat.block( 0, i, i, N - i - 1 );
457 tempmat.block( i + 1, i + 1, N - i - 1, N - i - 1 ) =
_quadmat.block( i, i, N - i - 1, N - i - 1 );
467 template<
typename T = DataType>
478 std::vector<Eigen::Triplet<FloatType>> triplets;
479 triplets.reserve(
_quadmat.nonZeros() );
481 for (
int k = 0; k <
_quadmat.outerSize(); k++ ) {
485 FloatType val = it.value();
487 if ( r >= i && c >= i ) {
488 triplets.emplace_back( r + 1, c + 1, val );
489 }
else if ( r >= i ) {
490 triplets.emplace_back( r + 1, c, val );
491 }
else if ( c >= i ) {
492 triplets.emplace_back( r, c + 1, val );
494 triplets.emplace_back( r, c, val );
500 _quadmat.setFromTriplets( triplets.begin(), triplets.end() );
508 template<
typename T = DataType>
516 tempmat.block( 0, 0, i, i ) =
_quadmat.block( 0, 0, i, i );
517 tempmat.block( 0, i, i, N - i - 1 ) =
_quadmat.block( 0, i + 1, i, N - i - 1 );
518 tempmat.block( i, i, N - i - 1, N - i - 1 ) =
_quadmat.block( i + 1, i + 1, N - i - 1, N - i - 1 );
528 template<
typename T = DataType>
538 std::vector<Eigen::Triplet<FloatType>> triplets;
539 triplets.reserve(
_quadmat.nonZeros() );
541 for (
int k = 0; k <
_quadmat.outerSize(); k++ ) {
545 FloatType val = it.value();
547 if ( r == i || c == i )
550 if ( r > i && c > i ) {
551 triplets.emplace_back( r - 1, c - 1, val );
552 }
else if ( r > i ) {
553 triplets.emplace_back( r - 1, c, val );
554 }
else if ( c > i ) {
555 triplets.emplace_back( r, c - 1, val );
557 triplets.emplace_back( r, c, val );
563 _quadmat.setFromTriplets( triplets.begin(), triplets.end() );
594 if ( force_delete ==
false ) {
597 if (
_quadmat.col( i ).squaredNorm() > std::numeric_limits<FloatType>::epsilon()
598 ||
_quadmat.row( i ).squaredNorm() > std::numeric_limits<FloatType>::epsilon() ) {
619 template<
typename T = DataType>
625 std::unordered_set<IndexType> labels;
627 for (
const auto &kv : linear ) {
628 labels.insert( kv.first );
631 for (
const auto &kv : quadratic ) {
632 labels.insert( kv.first.first );
633 labels.insert( kv.first.second );
637 _idx_to_label = std::vector<IndexType>( labels.begin(), labels.end() );
648 for (
const auto &kv : linear ) {
649 IndexType key = kv.first;
650 FloatType val = kv.second;
654 for (
const auto &kv : quadratic ) {
655 std::pair<IndexType, IndexType> key = kv.first;
656 FloatType val = kv.second;
657 _mat( key.first, key.second ) += val;
667 template<
typename T = DataType>
673 std::unordered_set<IndexType> labels;
675 for (
const auto &kv : linear ) {
676 labels.insert( kv.first );
679 for (
const auto &kv : quadratic ) {
680 labels.insert( kv.first.first );
681 labels.insert( kv.first.second );
685 _idx_to_label = std::vector<IndexType>( labels.begin(), labels.end() );
693 std::vector<Eigen::Triplet<FloatType>> triplets;
694 const size_t buffer = 5;
695 triplets.reserve( linear.size() + quadratic.size() + buffer );
698 for (
const auto &kv : linear ) {
700 size_t idx2 = mat_size - 1;
701 FloatType val = kv.second;
704 triplets.emplace_back( std::min( idx1, idx2 ), std::max( idx1, idx2 ), val );
707 for (
const auto &kv : quadratic ) {
710 FloatType val = kv.second;
713 triplets.emplace_back( std::min( idx1, idx2 ), std::max( idx1, idx2 ), val );
716 triplets.emplace_back( mat_size - 1, mat_size - 1, 1 );
718 _quadmat.setFromTriplets( triplets.begin(), triplets.end() );
728 template<
typename T = DataType>
734 if ( fix_format ==
false ) {
737 _quadmat += mat.template triangularView<Eigen::StrictlyUpper>();
738 _quadmat += mat.template triangularView<Eigen::StrictlyLower>().transpose();
741 _quadmat.block( 0, 0, mat_size - 1, mat_size - 1 ) += mat.template triangularView<Eigen::StrictlyUpper>();
742 _quadmat.block( 0, 0, mat_size - 1, mat_size - 1 )
743 += mat.template triangularView<Eigen::StrictlyLower>().transpose();
745 Vector loc = mat.diagonal();
746 _quadmat.block( 0, mat_size - 1, mat_size - 1, 1 ) += loc;
748 throw std::runtime_error(
"the number of variables and dimension do not match." );
759 template<
typename T = DataType>
768 if ( fix_format ==
false ) {
771 sparse_mat = mat.sparseView();
772 _quadmat += sparse_mat.template triangularView<Eigen::StrictlyUpper>();
773 sparse_mat = mat.sparseView().transpose();
774 _quadmat += sparse_mat.template triangularView<Eigen::StrictlyUpper>();
778 DenseMatrix temp_mat = DenseMatrix::Zero( mat_size, mat_size );
780 temp_mat.block( 0, 0, mat_size - 1, mat_size - 1 ) += mat.template triangularView<Eigen::StrictlyUpper>();
781 temp_mat.block( 0, 0, mat_size - 1, mat_size - 1 )
782 += mat.template triangularView<Eigen::StrictlyLower>().transpose();
784 Vector loc = mat.diagonal();
785 temp_mat.block( 0, mat_size - 1, mat_size - 1, 1 ) += loc;
790 throw std::runtime_error(
"the number of variables and dimension do not match." );
837 std::unordered_set<IndexType> labels( labels_vec.begin(), labels_vec.end() );
838 _idx_to_label = std::vector<IndexType>( labels.begin(), labels.end() );
842 if ( mat.rows() != mat.cols() ) {
843 throw std::runtime_error(
"matrix must be a square matrix" );
864 template<
typename T = DataType>
875 return ret_quadratic;
878 template<
typename T = DataType>
882 for (
int k = 0; k <
_quadmat.outerSize(); k++ ) {
887 FloatType val = it.value();
894 return ret_quadratic;
915 this->_quadmat = mat;
916 std::unordered_set<IndexType> labels( labels_vec.begin(), labels_vec.end() );
917 _idx_to_label = std::vector<IndexType>( labels.begin(), labels.end() );
936 template<
typename T = DataType>
942 Vector colwise_sum( num_variables );
943 for (
size_t i = 0; i < num_variables; i++ ) {
944 colwise_sum( i ) =
_quadmat.block( 0, 0, i, num_variables ).col( i ).sum();
947 Vector rowwise_sum =
_quadmat.block( 0, 0, num_variables, num_variables ).rowwise().sum();
949 Vector local_field =
_quadmat.block( 0, num_variables, num_variables, 1 );
952 m_offset += colwise_sum.sum() - local_field.sum();
955 _quadmat.block( 0, num_variables, num_variables, 1 ) = 2 * local_field - 2 * ( colwise_sum + rowwise_sum );
958 _quadmat.block( 0, 0, num_variables, num_variables ) *= 4;
975 template<
typename T = DataType>
983 Vector colwise_sum( num_variables );
984 Vector rowwise_sum( num_variables );
985 colwise_sum.setZero();
986 rowwise_sum.setZero();
988 for (
int k = 0; k <
_quadmat.outerSize(); k++ ) {
993 FloatType val = it.value();
995 if ( ( r < num_variables ) && ( c < num_variables ) ) {
996 colwise_sum( c ) += val;
997 rowwise_sum( r ) += val;
1002 Vector local_field =
_quadmat.block( 0, num_variables, num_variables, 1 );
1005 m_offset += colwise_sum.sum() - local_field.sum();
1014 Vector new_local_field = 2 * local_field - 2 * ( colwise_sum + rowwise_sum );
1016 std::vector<Eigen::Triplet<FloatType>> triplets;
1017 triplets.reserve(
_quadmat.nonZeros() );
1018 for (
size_t r = 0; r < num_variables; r++ ) {
1019 triplets.emplace_back( r, num_variables, new_local_field( r ) );
1022 for (
int k = 0; k <
_quadmat.outerSize(); k++ ) {
1025 size_t r = it.row();
1026 size_t c = it.col();
1027 FloatType val = it.value();
1029 if ( ( r < num_variables ) && ( c < num_variables ) ) {
1030 triplets.emplace_back( r, c, 4 * val );
1035 triplets.emplace_back( num_variables, num_variables, 1 );
1038 _quadmat.setFromTriplets( triplets.begin(), triplets.end() );
1055 template<
typename T = DataType>
1061 Vector colwise_sum( num_variables );
1062 for (
size_t i = 0; i < num_variables; i++ ) {
1063 colwise_sum( i ) =
_quadmat.block( 0, 0, i, num_variables ).col( i ).sum();
1065 Vector rowwise_sum =
_quadmat.block( 0, 0, num_variables, num_variables ).rowwise().sum();
1067 Vector local_field =
_quadmat.block( 0, num_variables, num_variables, 1 );
1070 m_offset += 0.25 * colwise_sum.sum() + 0.5 * local_field.sum();
1073 _quadmat.block( 0, num_variables, num_variables, 1 ) = 0.5 * local_field + 0.25 * ( colwise_sum + rowwise_sum );
1076 _quadmat.block( 0, 0, num_variables, num_variables ) *= 0.25;
1093 template<
typename T = DataType>
1101 Vector colwise_sum( num_variables );
1102 Vector rowwise_sum( num_variables );
1103 colwise_sum.setZero();
1104 rowwise_sum.setZero();
1106 for (
int k = 0; k <
_quadmat.outerSize(); k++ ) {
1109 size_t r = it.row();
1110 size_t c = it.col();
1111 FloatType val = it.value();
1113 if ( ( r < num_variables ) && ( c < num_variables ) ) {
1114 colwise_sum( c ) += val;
1115 rowwise_sum( r ) += val;
1120 Vector local_field =
_quadmat.block( 0, num_variables, num_variables, 1 );
1123 m_offset += 0.25 * colwise_sum.sum() + 0.5 * local_field.sum();
1132 Vector new_local_field = 0.5 * local_field + 0.25 * ( colwise_sum + rowwise_sum );
1134 std::vector<Eigen::Triplet<FloatType>> triplets;
1135 triplets.reserve(
_quadmat.nonZeros() );
1136 for (
size_t r = 0; r < num_variables; r++ ) {
1137 triplets.emplace_back( r, num_variables, new_local_field( r ) );
1140 for (
int k = 0; k <
_quadmat.outerSize(); k++ ) {
1143 size_t r = it.row();
1144 size_t c = it.col();
1145 FloatType val = it.value();
1147 if ( ( r < num_variables ) && ( c < num_variables ) ) {
1148 triplets.emplace_back( r, c, 0.25 * val );
1153 triplets.emplace_back( num_variables, num_variables, 1 );
1155 _quadmat.setFromTriplets( triplets.begin(), triplets.end() );
1170 const FloatType &offset,
1202 const Eigen::Ref<const DenseMatrix> &mat,
1203 const std::vector<IndexType> &labels_vec,
1204 const FloatType &offset,
1206 bool fix_format =
true ) :
1221 const Eigen::Ref<const DenseMatrix> &mat,
1222 const std::vector<IndexType> &labels_vec,
1224 bool fix_format =
true ) :
1240 const std::vector<IndexType> &labels_vec,
1241 const FloatType &offset,
1302 return _mat( label_i );
1320 return _mat( label_i, label_j );
1389 for (
auto &it : linear ) {
1405 _mat( u, v ) += bias;
1414 for (
auto &it : quadratic ) {
1434 for (
auto &it : variables ) {
1457 for (
auto &it : interactions ) {
1487 const FloatType &scalar,
1488 const std::vector<IndexType> &ignored_variables = {},
1489 const std::vector<std::pair<IndexType, IndexType>> &ignored_interactions = {},
1490 const bool ignored_offset = false ) {
1491 if ( scalar == 0.0 )
1492 throw std::runtime_error(
"scalar must not be zero" );
1498 for (
const auto &it : ignored_variables ) {
1499 _mat( it ) *= 1.0 / scalar;
1503 for (
const auto &it : ignored_interactions ) {
1504 _mat( it.first, it.second ) *= 1.0 / scalar;
1508 if ( !ignored_offset ) {
1526 const std::pair<FloatType, FloatType> &bias_range = { 1.0, 1.0 },
1527 const bool use_quadratic_range =
false,
1528 const std::pair<FloatType, FloatType> &quadratic_range = { 1.0, 1.0 },
1529 const std::vector<IndexType> &ignored_variables = {},
1530 const std::vector<std::pair<IndexType, IndexType>> &ignored_interactions = {},
1531 const bool ignored_offset = false ) {
1533 std::pair<FloatType, FloatType> l_range = bias_range;
1534 std::pair<FloatType, FloatType> q_range;
1535 if ( !use_quadratic_range ) {
1536 q_range = bias_range;
1538 q_range = quadratic_range;
1547 std::vector<FloatType> v_scale
1548 = { lin_min / l_range.first, lin_max / l_range.second, quad_min / q_range.first, quad_max / q_range.second };
1550 FloatType inv_scale = *std::max_element( v_scale.begin(), v_scale.end() );
1553 if ( inv_scale != 0.0 ) {
1554 scale( 1.0 / inv_scale, ignored_variables, ignored_interactions, ignored_offset );
1565 std::vector<std::pair<IndexType, IndexType>> interactions;
1567 for (
const auto &it : quadratic ) {
1568 if ( it.first.first == v ) {
1570 interactions.push_back( it.first );
1571 }
else if ( it.first.second == v ) {
1573 interactions.push_back( it.first );
1587 for (
auto &it : fixed ) {
1670 if ( inplace ==
true ) {
1690 for (
const auto &elem : sample ) {
1695 return en + ( s.transpose() *
_quadmat * s ) - 1;
1705 std::vector<FloatType> en_vec;
1706 for (
auto &it : samples_like ) {
1707 en_vec.push_back(
energy( it ) );
1718 std::tuple<Quadratic<IndexType, FloatType>, FloatType>
to_qubo() {
1725 for (
const auto &it : linear ) {
1726 Q[ std::make_pair( it.first, it.first ) ] = it.second;
1728 return std::make_tuple( Q, offset );
1744 for (
auto &&elem : Q ) {
1745 const auto &key = elem.first;
1746 const auto &value = elem.second;
1747 if ( key.first == key.second ) {
1748 linear[ key.first ] = value;
1750 quadratic[ std::make_pair( key.first, key.second ) ] = value;
1769 return std::make_tuple( linear, quadratic, offset );
1784 FloatType offset = 0.0 ) {
1816 template<
typename T = DataType>
1818 std::string schema_version =
"3.0.0-dense";
1831 std::string index_dtype = this->
get_num_variables() <= 65536UL ?
"uint16" :
"uint32";
1834 std::string bias_type;
1835 if (
typeid(
m_offset ) ==
typeid(
float ) ) {
1836 bias_type =
"float32";
1837 }
else if (
typeid(
m_offset ) ==
typeid(
double ) ) {
1838 bias_type =
"float64";
1840 throw std::runtime_error(
"FloatType must be float or double." );
1844 std::string variable_type;
1846 variable_type =
"SPIN";
1848 variable_type =
"BINARY";
1850 throw std::runtime_error(
"Variable type must be SPIN or BINARY." );
1857 output[
"type" ] =
"BinaryQuadraticModel";
1858 output[
"version" ] = { {
"bqm_schema", schema_version } };
1860 output[
"use_bytes" ] =
false;
1861 output[
"index_type" ] = index_dtype;
1862 output[
"bias_type" ] = bias_type;
1864 output[
"variable_type" ] = variable_type;
1866 output[
"info" ] = {};
1867 output[
"biases" ] = biases;
1877 template<
typename T = DataType>
1879 std::string schema_version =
"3.0.0";
1893 std::string index_dtype = this->
get_num_variables() <= 65536UL ?
"uint16" :
"uint32";
1896 std::string bias_type;
1897 if (
typeid(
m_offset ) ==
typeid(
float ) ) {
1898 bias_type =
"float32";
1899 }
else if (
typeid(
m_offset ) ==
typeid(
double ) ) {
1900 bias_type =
"float64";
1902 throw std::runtime_error(
"FloatType must be float or double." );
1906 std::string variable_type;
1908 variable_type =
"SPIN";
1910 variable_type =
"BINARY";
1912 throw std::runtime_error(
"Variable type must be SPIN or BINARY." );
1916 output[
"type" ] =
"BinaryQuadraticModel";
1917 output[
"version" ] = { {
"bqm_schema", schema_version } };
1919 output[
"use_bytes" ] =
false;
1920 output[
"index_type" ] = index_dtype;
1921 output[
"bias_type" ] = bias_type;
1923 output[
"variable_type" ] = variable_type;
1925 output[
"info" ] =
"";
1929 Vector l_bias_vec =
_quadmat.block( 0, mat_size - 1, mat_size - 1, 1 );
1930 std::vector<FloatType> l_bias( l_bias_vec.size() );
1931 for (
int i = 0; i < l_bias_vec.size(); i++ ) {
1932 l_bias[ i ] = l_bias_vec( i );
1935 std::vector<FloatType> q_bias;
1936 std::vector<size_t> q_head;
1937 std::vector<size_t> q_tail;
1939 q_bias.reserve(
_quadmat.nonZeros() );
1940 q_head.reserve(
_quadmat.nonZeros() );
1941 q_tail.reserve(
_quadmat.nonZeros() );
1943 for (
int k = 0; k <
_quadmat.outerSize(); k++ ) {
1945 size_t r = it.row();
1946 size_t c = it.col();
1947 FloatType val = it.value();
1949 if ( ( r < mat_size - 1 ) && ( c < mat_size - 1 ) ) {
1950 q_bias.push_back( val );
1951 q_head.push_back( r );
1952 q_tail.push_back( c );
1957 output[
"linear_biases" ] = l_bias;
1958 output[
"quadratic_biases" ] = q_bias;
1959 output[
"quadratic_head" ] = q_head;
1960 output[
"quadratic_tail" ] = q_tail;
1961 output[
"num_interactions" ] = q_bias.size();
1974 template<
typename IndexType_serial = IndexType,
typename FloatType_serial = FloatType,
typename T = DataType>
1978 std::string type = input[
"type" ];
1979 if ( type !=
"BinaryQuadraticModel" ) {
1980 throw std::runtime_error(
"Type must be \"BinaryQuadraticModel\".\n" );
1982 std::string version = input[
"version" ][
"bqm_schema" ];
1983 if ( version !=
"3.0.0-dense" ) {
1984 throw std::runtime_error(
"bqm_schema must be 3.0.0-dense.\n" );
1989 std::string variable_type = input[
"variable_type" ];
1990 if ( variable_type ==
"SPIN" ) {
1992 }
else if ( variable_type ==
"BINARY" ) {
1995 throw std::runtime_error(
"variable_type must be SPIN or BINARY." );
1999 std::vector<IndexType_serial> variables = input[
"variable_labels" ];
2000 std::vector<FloatType_serial> biases = input[
"biases" ];
2001 FloatType offset = input[
"offset" ];
2003 size_t mat_size = variables.size() + 1;
2004 Eigen::Map<Matrix> mat( biases.data(), mat_size, mat_size );
2018 template<
typename IndexType_serial = IndexType,
typename FloatType_serial = FloatType,
typename T = DataType>
2022 std::string type = input[
"type" ];
2023 if ( type !=
"BinaryQuadraticModel" ) {
2024 throw std::runtime_error(
"Type must be \"BinaryQuadraticModel\".\n" );
2026 std::string version = input[
"version" ][
"bqm_schema" ];
2027 if ( version !=
"3.0.0" ) {
2028 throw std::runtime_error(
"bqm_schema must be 3.0.0.\n" );
2033 std::string variable_type = input[
"variable_type" ];
2034 if ( variable_type ==
"SPIN" ) {
2036 }
else if ( variable_type ==
"BINARY" ) {
2039 throw std::runtime_error(
"variable_type must be SPIN or BINARY." );
2043 std::vector<IndexType_serial> variables = input[
"variable_labels" ];
2044 FloatType offset = input[
"offset" ];
2046 std::vector<FloatType_serial> l_bias = input[
"linear_biases" ];
2047 std::vector<size_t> q_head = input[
"quadratic_head" ];
2048 std::vector<size_t> q_tail = input[
"quadratic_tail" ];
2049 std::vector<FloatType_serial> q_bias = input[
"quadratic_biases" ];
2052 std::vector<Eigen::Triplet<FloatType_serial>> triplets;
2053 triplets.reserve( q_bias.size() + l_bias.size() );
2055 size_t mat_size = variables.size() + 1;
2057 for (
size_t i = 0; i < l_bias.size(); i++ ) {
2058 if ( l_bias[ i ] != 0 )
2059 triplets.emplace_back( i, mat_size - 1, l_bias[ i ] );
2062 for (
size_t i = 0; i < q_bias.size(); i++ ) {
2063 triplets.emplace_back( q_head[ i ], q_tail[ i ], q_bias[ i ] );
2066 triplets.emplace_back( mat_size - 1, mat_size - 1, 1 );
2070 mat.setFromTriplets( triplets.begin(), triplets.end() );
Class for dense binary quadratic model.
Definition: binary_quadratic_model.hpp:148
FloatType _max_quadratic(dispatch_t< T, Sparse >=nullptr) const
calculate maximum element in quadratic term for dense graph
Definition: binary_quadratic_model.hpp:409
size_t get_num_variables() const
get the number of variables
Definition: binary_quadratic_model.hpp:1268
std::tuple< Linear< IndexType, FloatType >, Quadratic< IndexType, FloatType >, FloatType > to_ising()
Convert a binary quadratic model to Ising format.
Definition: binary_quadratic_model.hpp:1762
Quadratic< IndexType, FloatType > _generate_quadratic(dispatch_t< T, Sparse >=nullptr) const
Definition: binary_quadratic_model.hpp:879
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
bool contains(const IndexType &v) const
Return true if the variable contains v.
Definition: binary_quadratic_model.hpp:1288
FloatType & _mat(IndexType label_i, IndexType label_j)
get reference of _quadmat(i,j)
Definition: binary_quadratic_model.hpp:295
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
void _set_label_to_idx()
set _label_to_idx from _idx_to_label
Definition: binary_quadratic_model.hpp:218
BinaryQuadraticModel< IndexType, FloatType, DataType > change_vartype(const Vartype &vartype, bool inplace)
Create a binary quadratic model with the specified vartype.
Definition: binary_quadratic_model.hpp:1668
Quadratic< IndexType, FloatType > get_quadratic() const
Get uadratic object.
Definition: binary_quadratic_model.hpp:1328
void _binary_to_spin(dispatch_t< T, Dense >=nullptr)
change internal variable from QUBO to Ising ones for dense matrix The following conversion is applied...
Definition: binary_quadratic_model.hpp:1056
std::vector< IndexType > _idx_to_label
vector for converting index to label the list is asssumed to be sorted
Definition: binary_quadratic_model.hpp:196
FloatType _quadmat_get(size_t i, size_t j, dispatch_t< T, Dense >=nullptr) const
access elements for dense matrix
Definition: binary_quadratic_model.hpp:253
FloatType _min_quadratic(dispatch_t< T, Sparse >=nullptr) const
calculate minimum element in quadratic term for dense graph
Definition: binary_quadratic_model.hpp:435
std::tuple< Quadratic< IndexType, FloatType >, FloatType > to_qubo()
Convert a binary quadratic model to QUBO format.
Definition: binary_quadratic_model.hpp:1718
void _initialize_quadmat(const Linear< IndexType, FloatType > &linear, const Quadratic< IndexType, FloatType > &quadratic, dispatch_t< T, Dense >=nullptr)
initialize matrix with linear and quadratic dicts (for dense matrix)
Definition: binary_quadratic_model.hpp:620
void _spin_to_binary(dispatch_t< T, Dense >=nullptr)
change internal variable from Ising to QUBO ones for dense matrix The following conversion is applied...
Definition: binary_quadratic_model.hpp:937
void _delete_label_from_mat(IndexType label_i, dispatch_t< T, Sparse >=nullptr)
delete row and column that corresponds to existing label from _quadmat for sparse matrix
Definition: binary_quadratic_model.hpp:529
FloatType _min_quadratic(dispatch_t< T, Dense >=nullptr) const
calculate minimum element in quadratic term for dense graph
Definition: binary_quadratic_model.hpp:385
FloatType _min_linear(dispatch_t< T, Dense >=nullptr) const
calculate minimum element in linear term for dense graph
Definition: binary_quadratic_model.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.hpp:1433
void _initialize_quadmat(const DenseMatrix &mat, const std::vector< IndexType > &labels_vec, bool fix_format)
initialize matrix with matrix and labels the form of matrix is assumed to be the following two forms:
Definition: binary_quadratic_model.hpp:834
Eigen::Matrix< FloatType, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > DenseMatrix
Eigen Matrix if DataType is Dense , Matrix is equal to Eigen::Matrix if DataType is Sparse,...
Definition: binary_quadratic_model.hpp:167
FloatType _mat(IndexType label_i, IndexType label_j) const
get reference of _quadmat(i,j)
Definition: binary_quadratic_model.hpp:324
BinaryQuadraticModel< IndexType, FloatType, DataType > empty(Vartype vartype)
Create an empty BinaryQuadraticModel.
Definition: binary_quadratic_model.hpp:1364
Matrix interaction_matrix() const
generate (Dense or Sparse) interaction matrix with given list of indices The generated matrix will be...
Definition: binary_quadratic_model.hpp:1805
FloatType _min_linear(dispatch_t< T, Sparse >=nullptr) const
calculate minimum element in linear term for dense graph
Definition: binary_quadratic_model.hpp:422
FloatType & _mat(IndexType label_i)
get reference of _quadmat(i,i)
Definition: binary_quadratic_model.hpp:311
void _binary_to_spin(dispatch_t< T, Sparse >=nullptr)
change internal variable from QUBO to Ising ones for sparse matrix The following conversion is applie...
Definition: binary_quadratic_model.hpp:1094
void _spin_to_binary(dispatch_t< T, Sparse >=nullptr)
change internal variable from Ising to QUBO ones for sparse matrix The following conversion is applie...
Definition: binary_quadratic_model.hpp:976
FloatType & _quadmat_get(size_t i, size_t j, dispatch_t< T, Sparse >=nullptr)
access elements for sparse matrix
Definition: binary_quadratic_model.hpp:268
nlohmann::json json
Definition: binary_quadratic_model.hpp:1809
Vartype get_vartype() const
Get the vartype object.
Definition: binary_quadratic_model.hpp:1346
static BinaryQuadraticModel< IndexType_serial, FloatType_serial, DataType > from_serializable(const json &input, dispatch_t< T, Dense >=nullptr)
Create a BinaryQuadraticModel instance from a serializable object.
Definition: binary_quadratic_model.hpp:1976
std::unordered_map< IndexType, size_t > _label_to_idx
dict for converting label to index
Definition: binary_quadratic_model.hpp:201
std::conditional_t< std::is_same_v< DataType, Dense >, DenseMatrix, SparseMatrix > Matrix
Definition: binary_quadratic_model.hpp:171
FloatType _max_linear(dispatch_t< T, Dense >=nullptr) const
calculate maximum element in linear term for dense graph
Definition: binary_quadratic_model.hpp:352
size_t length() const
Return the number of variables.
Definition: binary_quadratic_model.hpp:1278
void _delete_label(IndexType label_i, bool force_delete=true)
delete label if label_i does not exist, this process is skipped.
Definition: binary_quadratic_model.hpp:591
json to_serializable(dispatch_t< T, Dense >=nullptr) const
Convert the binary quadratic model to a dense-version serializable object.
Definition: binary_quadratic_model.hpp:1817
const std::vector< IndexType > & get_variables() const
Get variables.
Definition: binary_quadratic_model.hpp:1355
static BinaryQuadraticModel< IndexType, FloatType, DataType > from_qubo(const Quadratic< IndexType, FloatType > &Q, FloatType offset=0.0)
Create a binary quadratic model from a QUBO model.
Definition: binary_quadratic_model.hpp:1740
FloatType m_offset
The energy offset associated with the model.
Definition: binary_quadratic_model.hpp:207
FloatType _mat(IndexType label_i) const
get reference of _quadmat(i,i)
Definition: binary_quadratic_model.hpp:341
Linear< IndexType, FloatType > get_linear() const
Get linear object.
Definition: binary_quadratic_model.hpp:1310
static BinaryQuadraticModel< IndexType, FloatType, DataType > 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.hpp:1781
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
std::vector< FloatType > energies(const std::vector< Sample< IndexType >> &samples_like) const
Determine the energies of the given samples.
Definition: binary_quadratic_model.hpp:1704
Eigen::Matrix< FloatType, Eigen::Dynamic, 1 > Vector
Definition: binary_quadratic_model.hpp:173
BinaryQuadraticModel(const BinaryQuadraticModel &)=default
Linear< IndexType, FloatType > _generate_linear() const
Definition: binary_quadratic_model.hpp:853
BinaryQuadraticModel(const Eigen::Ref< const DenseMatrix > &mat, const std::vector< IndexType > &labels_vec, const FloatType &offset, const Vartype vartype, bool fix_format=true)
BinaryQuadraticModel constructor (with matrix);.
Definition: binary_quadratic_model.hpp:1201
FloatType _max_quadratic(dispatch_t< T, Dense >=nullptr) const
calculate maximum element in quadratic term for dense graph
Definition: binary_quadratic_model.hpp:363
Eigen::SparseMatrix< FloatType, Eigen::RowMajor > SparseMatrix
Definition: binary_quadratic_model.hpp:168
std::enable_if_t< std::is_same_v< T, U >, std::nullptr_t > dispatch_t
template type for dispatch used for SFINAE
Definition: binary_quadratic_model.hpp:158
BinaryQuadraticModel(const SparseMatrix &mat, const std::vector< IndexType > &labels_vec, const Vartype vartype)
BinaryQuadraticModel constructor (with sparse matrix); this constructor is for developers.
Definition: binary_quadratic_model.hpp:1257
void _insert_label_into_mat(IndexType label_i, dispatch_t< T, Dense >=nullptr)
insert row and column that corresponds to added label into _quadmat for dense matrix
Definition: binary_quadratic_model.hpp:448
BinaryQuadraticModel(const Eigen::Ref< const DenseMatrix > &mat, const std::vector< IndexType > &labels_vec, const Vartype vartype, bool fix_format=true)
BinaryQuadraticModel constructor (with matrix);.
Definition: binary_quadratic_model.hpp:1220
void add_offset(const FloatType &offset)
Add specified value to the offset of a binary quadratic model.
Definition: binary_quadratic_model.hpp:1467
BinaryQuadraticModel(const SparseMatrix &mat, const std::vector< IndexType > &labels_vec, const FloatType &offset, const Vartype vartype)
BinaryQuadraticModel constructor (with sparse matrix); this constructor is for developers.
Definition: binary_quadratic_model.hpp:1238
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
Matrix _quadmat
quadratic dense-type matrix The stored matrix has the following triangular form:
Definition: binary_quadratic_model.hpp:190
FloatType _quadmat_get(size_t i, size_t j, dispatch_t< T, Sparse >=nullptr) const
access elements for sparse matrix
Definition: binary_quadratic_model.hpp:283
void _initialize_quadmat(const Linear< IndexType, FloatType > &linear, const Quadratic< IndexType, FloatType > &quadratic, dispatch_t< T, Sparse >=nullptr)
initialize matrix with linear and quadratic dicts (for sparse matrix)
Definition: binary_quadratic_model.hpp:668
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 & _quadmat_get(size_t i, size_t j, dispatch_t< T, Dense >=nullptr)
access elements for dense matrix
Definition: binary_quadratic_model.hpp:238
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.hpp:1586
json to_serializable(dispatch_t< T, Sparse >=nullptr) const
Convert the binary quadratic model to a serializable object.
Definition: binary_quadratic_model.hpp:1878
FloatType get_offset() const
Get the offset.
Definition: binary_quadratic_model.hpp:1337
void _delete_label_from_mat(IndexType label_i, dispatch_t< T, Dense >=nullptr)
delete row and column that corresponds to existing label from _quadmat for dense matrix
Definition: binary_quadratic_model.hpp:509
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.hpp:1525
void _insert_label_into_mat(IndexType label_i, dispatch_t< T, Sparse >=nullptr)
insert row and column that corresponds to added label into _quadmat for sparse matrix
Definition: binary_quadratic_model.hpp:468
typename SparseMatrix::InnerIterator SpIter
Definition: binary_quadratic_model.hpp:169
Quadratic< IndexType, FloatType > _generate_quadratic(dispatch_t< T, Dense >=nullptr) const
Definition: binary_quadratic_model.hpp:865
FloatType _max_linear(dispatch_t< T, Sparse >=nullptr) const
calculate maximum element in linear term for dense graph
Definition: binary_quadratic_model.hpp:396
BinaryQuadraticModel(const Linear< IndexType, FloatType > &linear, const Quadratic< IndexType, FloatType > &quadratic, const Vartype vartype)
BinaryQuadraticModel constructor.
Definition: binary_quadratic_model.hpp:1185
Vartype m_vartype
The model's type.
Definition: binary_quadratic_model.hpp:213
void remove_offset()
Set the binary quadratic model's offset to zero.
Definition: binary_quadratic_model.hpp:1474
void _add_new_label(IndexType label_i)
add new label if label_i already exists, this process is skipped.
Definition: binary_quadratic_model.hpp:572
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 _initialize_quadmat(const SparseMatrix &mat, const std::vector< IndexType > &labels_vec)
initialize matrix with matrix and labels the form of matrix is assumed to be the following form:
Definition: binary_quadratic_model.hpp:914
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
void flip_variable(const IndexType &v)
Flip variable v in a binary quadratic model.
Definition: binary_quadratic_model.hpp:1597
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
static BinaryQuadraticModel< IndexType_serial, FloatType_serial, DataType > from_serializable(const json &input, dispatch_t< T, Sparse >=nullptr)
Create a BinaryQuadraticModel instance from a serializable object.
Definition: binary_quadratic_model.hpp:2020
void _add_triangular_elements(const DenseMatrix &mat, bool fix_format, dispatch_t< T, Sparse >=nullptr)
add non-diagonal elements to upper triangular components for sparse matrix
Definition: binary_quadratic_model.hpp:760
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
BinaryQuadraticModel(const Linear< IndexType, FloatType > &linear, const Quadratic< IndexType, FloatType > &quadratic, const FloatType &offset, const Vartype vartype)
BinaryQuadraticModel constructor.
Definition: binary_quadratic_model.hpp:1167
void _add_triangular_elements(const DenseMatrix &mat, bool fix_format, dispatch_t< T, Dense >=nullptr)
add non-diagonal elements to upper triangular components for dense matrix
Definition: binary_quadratic_model.hpp:729
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
Definition: binary_polynomial_model.hpp:139
std::unordered_map< IndexType, std::unordered_map< IndexType, FloatType > > Adjacency
Type alias for adjacency list.
Definition: binary_quadratic_model.hpp:127
std::unordered_map< IndexType, int32_t > Sample
Type alias for sample, which represents the spin or binary configurations.
Definition: binary_polynomial_model.hpp:162
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.hpp:137
Definition: binary_quadratic_model.hpp:138
Hash function for std::unordered_map.
Definition: hash.hpp:65