dwave-preprocessing 0.6.7__cp313-cp313-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dwave/__init__.py +16 -0
- dwave/preprocessing/__init__.py +42 -0
- dwave/preprocessing/composites/__init__.py +19 -0
- dwave/preprocessing/composites/clip.py +114 -0
- dwave/preprocessing/composites/connected_components.py +113 -0
- dwave/preprocessing/composites/fix_variables.py +153 -0
- dwave/preprocessing/composites/scale.py +144 -0
- dwave/preprocessing/composites/spin_reversal_transform.py +221 -0
- dwave/preprocessing/cyfix_variables.cp313-win_amd64.pyd +0 -0
- dwave/preprocessing/cyfix_variables.cpp +29498 -0
- dwave/preprocessing/cyfix_variables.pyx +58 -0
- dwave/preprocessing/include/dwave/exceptions.hpp +28 -0
- dwave/preprocessing/include/dwave/flags.hpp +63 -0
- dwave/preprocessing/include/dwave/presolve.hpp +91 -0
- dwave/preprocessing/include/dwave-preprocessing/fix_variables.hpp +119 -0
- dwave/preprocessing/include/dwave-preprocessing/helper_data_structures.hpp +152 -0
- dwave/preprocessing/include/dwave-preprocessing/helper_graph_algorithms.hpp +476 -0
- dwave/preprocessing/include/dwave-preprocessing/implication_network.hpp +690 -0
- dwave/preprocessing/include/dwave-preprocessing/mapping_policy.hpp +124 -0
- dwave/preprocessing/include/dwave-preprocessing/posiform_info.hpp +384 -0
- dwave/preprocessing/include/dwave-preprocessing/push_relabel.hpp +775 -0
- dwave/preprocessing/libcpp.pxd +59 -0
- dwave/preprocessing/lower_bounds.py +94 -0
- dwave/preprocessing/presolve/__init__.pxd +13 -0
- dwave/preprocessing/presolve/__init__.py +16 -0
- dwave/preprocessing/presolve/cypresolve.cp313-win_amd64.pyd +0 -0
- dwave/preprocessing/presolve/cypresolve.cpp +39255 -0
- dwave/preprocessing/presolve/cypresolve.pxd +45 -0
- dwave/preprocessing/presolve/cypresolve.pyi +50 -0
- dwave/preprocessing/presolve/cypresolve.pyx +340 -0
- dwave/preprocessing/presolve/exceptions.py +21 -0
- dwave/preprocessing/presolve/pypresolve.py +173 -0
- dwave/preprocessing/src/exceptions.cpp +21 -0
- dwave/preprocessing/src/presolve.cpp +105 -0
- dwave/preprocessing/src/presolveimpl.hpp +1060 -0
- dwave_preprocessing-0.6.7.dist-info/DELVEWHEEL +2 -0
- dwave_preprocessing-0.6.7.dist-info/LICENSE +203 -0
- dwave_preprocessing-0.6.7.dist-info/METADATA +114 -0
- dwave_preprocessing-0.6.7.dist-info/RECORD +42 -0
- dwave_preprocessing-0.6.7.dist-info/WHEEL +5 -0
- dwave_preprocessing-0.6.7.dist-info/top_level.txt +1 -0
- dwave_preprocessing.libs/msvcp140-c5f1b59771da57851103ca2011763553.dll +0 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// Copyright 2021 D-Wave Systems 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
|
+
#ifndef IMPLICATION_NETWORK_MAPPING_POLICY_HPP_INCLUDED
|
|
16
|
+
#define IMPLICATION_NETWORK_MAPPING_POLICY_HPP_INCLUDED
|
|
17
|
+
|
|
18
|
+
// PLEASE DO NOT MAKE AN INTERFACE FOR THE MAPPER CLASS. VIRTUAL DISPATCH WILL
|
|
19
|
+
// ADD TO THE ALREADY EXISTING PENALTY OF USING THIS METHODOLOGY. WE CREATED
|
|
20
|
+
// THIS CLASS SINCE WE CAN HAVE PERFORMANCE ADVANTAGE WITH ALTERNATE/EVEN_ODD
|
|
21
|
+
// MAPPING WHILE IT IS EASIER TO DEBUG WITH SEQUENTIAL MAPPING, BUT IT IS HARD
|
|
22
|
+
// TO SHIFT BETWEEN THEM WITHOUT A DEDICATED CLASS.
|
|
23
|
+
|
|
24
|
+
#define evenOddMapper mapper_t
|
|
25
|
+
|
|
26
|
+
// These mapper objects control variable to vertex mapping. Please do not
|
|
27
|
+
// manually map them even though it may seem easy, since we may change the
|
|
28
|
+
// mapping by these functions later. The assertions should be changed if the
|
|
29
|
+
// mapping is changed. If there are n variables in the Quobo, the posiform will
|
|
30
|
+
// have 2 * ( n + 1 ) variables, each variable with its complement and a root,
|
|
31
|
+
// X_0/X_source and its complement. Here we treat variable 0-n-1 as the original
|
|
32
|
+
// n Qubo variables and variable n as X_0/X_source.
|
|
33
|
+
|
|
34
|
+
// In sequential mapping the variables 0-n-1 map to vertices 0-n-1. The source
|
|
35
|
+
// variable maps to vertex n. Then the vertices n+1 to 2*n + 1 correspond to the
|
|
36
|
+
// complements of vertices 0 - n sequentially. This method of mapping is easy
|
|
37
|
+
// for debugging purpose but does not keep the ordering that may be in the qubo
|
|
38
|
+
// as regards to the biases which may be sorted based on the connecting
|
|
39
|
+
// variables.
|
|
40
|
+
class sequentialMapper {
|
|
41
|
+
public:
|
|
42
|
+
sequentialMapper(int num_variables) : _num_variables(num_variables) {}
|
|
43
|
+
|
|
44
|
+
sequentialMapper() {}
|
|
45
|
+
|
|
46
|
+
inline int source() { return _num_variables; }
|
|
47
|
+
|
|
48
|
+
inline int sink() { return 2 * _num_variables + 1; }
|
|
49
|
+
|
|
50
|
+
inline int num_vertices() { return 2 * (_num_variables + 1); }
|
|
51
|
+
|
|
52
|
+
inline int complement(int vertex) {
|
|
53
|
+
assert((vertex <= 2 * _num_variables + 1) && (vertex >= 0));
|
|
54
|
+
return (vertex <= _num_variables) ? (vertex + _num_variables + 1)
|
|
55
|
+
: (vertex - _num_variables - 1);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
inline int non_complemented_vertex(int vertex) {
|
|
59
|
+
assert((vertex <= 2 * _num_variables + 1) && (vertex >= 0));
|
|
60
|
+
return (vertex <= _num_variables) ? vertex : (vertex - _num_variables - 1);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Should convert only vertices which correspond to non-complemented variables
|
|
64
|
+
// and should not convert the source vertex.
|
|
65
|
+
inline int non_complemented_vertex_to_variable(int vertex) {
|
|
66
|
+
assert((vertex < _num_variables) && (vertex >= 0));
|
|
67
|
+
return vertex;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
inline int variable_to_vertex(int variable) {
|
|
71
|
+
assert((variable < _num_variables) && (variable >= 0));
|
|
72
|
+
return variable;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
inline bool complement_maintains_order() { return false; }
|
|
76
|
+
|
|
77
|
+
private:
|
|
78
|
+
int _num_variables;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// The evenOddMapper maps variable x to vertex 2 * x and its complement is 2 * x
|
|
82
|
+
// + 1. Same as before we consider variable n to be the source.
|
|
83
|
+
class evenOddMapper {
|
|
84
|
+
public:
|
|
85
|
+
evenOddMapper(int num_variables) : _num_variables(num_variables) {}
|
|
86
|
+
|
|
87
|
+
evenOddMapper() {}
|
|
88
|
+
|
|
89
|
+
inline int source() { return 2 * _num_variables; }
|
|
90
|
+
|
|
91
|
+
inline int sink() { return 2 * _num_variables + 1; }
|
|
92
|
+
|
|
93
|
+
inline int num_vertices() { return 2 * (_num_variables + 1); }
|
|
94
|
+
|
|
95
|
+
inline int complement(int vertex) {
|
|
96
|
+
assert((vertex <= 2 * _num_variables + 1) && (vertex >= 0));
|
|
97
|
+
return (vertex % 2) ? (vertex - 1) : (vertex + 1);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
inline int non_complemented_vertex(int vertex) {
|
|
101
|
+
assert((vertex <= 2 * _num_variables + 1) && (vertex >= 0));
|
|
102
|
+
return (vertex % 2) ? (vertex - 1) : (vertex);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Should convert only vertices which correspond to non-complemented variables
|
|
106
|
+
// and should not convert the source vertex.
|
|
107
|
+
inline int non_complemented_vertex_to_variable(int vertex) {
|
|
108
|
+
assert((vertex <= 2 * _num_variables - 1) && (vertex >= 0) &&
|
|
109
|
+
!(vertex % 2));
|
|
110
|
+
return vertex / 2;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
inline int variable_to_vertex(int variable) {
|
|
114
|
+
assert((variable < _num_variables) && (variable >= 0));
|
|
115
|
+
return variable * 2;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
inline bool complement_maintains_order() { return true; }
|
|
119
|
+
|
|
120
|
+
private:
|
|
121
|
+
int _num_variables;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
#endif
|
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
// Copyright 2021 D-Wave Systems 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
|
+
#ifndef POSIFORM_INFORMATION_HPP_INCLUDED
|
|
16
|
+
#define POSIFORM_INFORMATION_HPP_INCLUDED
|
|
17
|
+
|
|
18
|
+
#include <algorithm>
|
|
19
|
+
#include <cmath>
|
|
20
|
+
#include <iostream>
|
|
21
|
+
#include <limits>
|
|
22
|
+
#include <unordered_map>
|
|
23
|
+
#include <utility>
|
|
24
|
+
#include <vector>
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Contains all the information needed to recreate a posiform corresponding to
|
|
28
|
+
* a BQM. The intention is to reduce the memory footprint as much as possible,
|
|
29
|
+
* thus requiring some processing before the stored data can be used.
|
|
30
|
+
*
|
|
31
|
+
* For implementation details, see comments in the source code.
|
|
32
|
+
*
|
|
33
|
+
* For details on the algorithm, see : Boros, Endre & Hammer, Peter & Tavares, Gabriel.
|
|
34
|
+
* (2006). Preprocessing of unconstrained quadratic binary optimization. RUTCOR
|
|
35
|
+
* Research Report.
|
|
36
|
+
*/
|
|
37
|
+
template <class BQM, class coefficient_t> class PosiformInfo {
|
|
38
|
+
/*
|
|
39
|
+
If linear bias for variable X_i is negative with value -L it means in the
|
|
40
|
+
posiform we have the term L * X_source * X_i' (X_i' being the complement of
|
|
41
|
+
X_i). X_source is X_0 in the paper mentioned below. X_0 was a variable added
|
|
42
|
+
to the posiform at a later step. We do not store X_0 explicitly as that would
|
|
43
|
+
make the indexing complicated, shifting the index of each posiform variable
|
|
44
|
+
by 1.
|
|
45
|
+
|
|
46
|
+
If a quadratic bias between X_i, X_j is negative with value -L with i < j it
|
|
47
|
+
means in the posiform we have the term L*X_i*X_j' (X_i' being the complement
|
|
48
|
+
of X_i).
|
|
49
|
+
|
|
50
|
+
For each variable X_i the posiform keeps the iterators in the provided bqm
|
|
51
|
+
for biases starting from X_j, (j>i) to X_n.
|
|
52
|
+
|
|
53
|
+
The linear biases are stored in integral format and can be used directly.
|
|
54
|
+
But the quadratic biases are not stored, instead the iterators in the bqm are
|
|
55
|
+
stored, thus the convertTo** function must be applied first on the biases
|
|
56
|
+
before use.
|
|
57
|
+
|
|
58
|
+
Note the number of variables and biases exposed correspond to the integral
|
|
59
|
+
version of the bqm matrix and may differ from the numbers corresponding
|
|
60
|
+
to the floating point based numbers as many biases may be flushed to zeroes.
|
|
61
|
+
*/
|
|
62
|
+
public:
|
|
63
|
+
using coefficient_type = coefficient_t; // Must be a signed integral type.
|
|
64
|
+
using bias_type = typename BQM::bias_type;
|
|
65
|
+
using quadratic_iterator_type = typename BQM::const_neighborhood_iterator;
|
|
66
|
+
using variable_type = typename BQM::index_type;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Construct a PosiformInfo from a binary quadratic model.
|
|
70
|
+
*/
|
|
71
|
+
PosiformInfo(const BQM &bqm);
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Get number of posiform variables.
|
|
75
|
+
*/
|
|
76
|
+
inline int getNumVariables() { return _num_posiform_variables; }
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Get number of posiform variables with a non-zero linear bias.
|
|
80
|
+
*/
|
|
81
|
+
inline int getNumLinear() { return _num_linear_integral_biases; }
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Get the linear bias of a posiform variable.
|
|
86
|
+
*/
|
|
87
|
+
inline coefficient_type getLinear(int posiform_variable) {
|
|
88
|
+
return _linear_integral_biases
|
|
89
|
+
[_posiform_to_bqm_variable_map[posiform_variable]];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Get number of quadratic terms a posiform variable contributes in.
|
|
94
|
+
*/
|
|
95
|
+
inline int getNumQuadratic(int posiform_variable) {
|
|
96
|
+
return _num_quadratic_integral_biases
|
|
97
|
+
[_posiform_to_bqm_variable_map[posiform_variable]];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// For iterating over the quadratic biases, we need the
|
|
101
|
+
// convertToPosiformCoefficient and mapVariableQuboToPosiform functions, since
|
|
102
|
+
// the iterators belong to the bqm.
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Get the neighbors of a posiform variable in the corresponding BQM.
|
|
106
|
+
*/
|
|
107
|
+
inline std::pair<quadratic_iterator_type, quadratic_iterator_type>
|
|
108
|
+
getQuadratic(int posiform_variable) {
|
|
109
|
+
return _quadratic_iterators
|
|
110
|
+
[_posiform_to_bqm_variable_map[posiform_variable]];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Map a QUBO variable to a posiform variable. The number of QUBO variables
|
|
115
|
+
* and posiform variables may differ since the coefficient for a QUBO variable
|
|
116
|
+
* may turn out to be zero in a posiform. This may occur if the coefficient is
|
|
117
|
+
* flushed to zero during the conversion or if the variable did not have non-zero
|
|
118
|
+
* linear/quadratic biases in the QUBO.
|
|
119
|
+
*/
|
|
120
|
+
inline int mapVariableQuboToPosiform(int bqm_variable) {
|
|
121
|
+
if (_bqm_to_posiform_variable_map.count(bqm_variable) == 0) {
|
|
122
|
+
return -1;
|
|
123
|
+
} else {
|
|
124
|
+
return _bqm_to_posiform_variable_map[bqm_variable];
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Map a posiform variable to a QUBO variable. The number of QUBO variables
|
|
130
|
+
* and posiform variables may differ since the coefficient for a QUBO variable
|
|
131
|
+
* may turn out to be zero in a posiform. This may occur if the coefficient is
|
|
132
|
+
* flushed to zero during the conversion or if the variable did not have non-zero
|
|
133
|
+
* linear/quadratic biases in the QUBO.
|
|
134
|
+
*/
|
|
135
|
+
inline int mapVariablePosiformToQubo(int posiform_variable) {
|
|
136
|
+
return _posiform_to_bqm_variable_map[posiform_variable];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Convert a QUBO coefficient to a posiform coefficient.
|
|
141
|
+
*/
|
|
142
|
+
inline coefficient_type convertToPosiformCoefficient(bias_type bqm_bias) {
|
|
143
|
+
return static_cast<coefficient_type>(bqm_bias * _bias_conversion_ratio);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Return the value by which bqm biases are multiplied to get posiform coefficients.
|
|
148
|
+
*/
|
|
149
|
+
inline double getBiasConversionRatio() {
|
|
150
|
+
return _bias_conversion_ratio;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Return the value of the constant term of the posiform.
|
|
155
|
+
*/
|
|
156
|
+
inline double getConstant() {
|
|
157
|
+
return _constant_posiform;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Print out posiform details.
|
|
162
|
+
*/
|
|
163
|
+
void print();
|
|
164
|
+
|
|
165
|
+
private:
|
|
166
|
+
double _max_absolute_value;
|
|
167
|
+
double _bias_conversion_ratio;
|
|
168
|
+
coefficient_type _constant_posiform;
|
|
169
|
+
double _posiform_linear_sum_non_integral;
|
|
170
|
+
coefficient_type _posiform_linear_sum_integral;
|
|
171
|
+
int _num_bqm_variables;
|
|
172
|
+
int _num_posiform_variables;
|
|
173
|
+
int _num_linear_integral_biases;
|
|
174
|
+
std::vector<int> _num_quadratic_integral_biases;
|
|
175
|
+
std::vector<int> _posiform_to_bqm_variable_map;
|
|
176
|
+
std::unordered_map<int, int> _bqm_to_posiform_variable_map;
|
|
177
|
+
std::vector<double> _linear_double_biases;
|
|
178
|
+
std::vector<coefficient_type> _linear_integral_biases;
|
|
179
|
+
std::vector<std::pair<quadratic_iterator_type, quadratic_iterator_type>>
|
|
180
|
+
_quadratic_iterators;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
template <class BQM, class coefficient_t>
|
|
184
|
+
PosiformInfo<BQM, coefficient_t>::PosiformInfo(const BQM &bqm) {
|
|
185
|
+
assert(std::is_integral<coefficient_type>::value &&
|
|
186
|
+
std::is_signed<coefficient_type>::value &&
|
|
187
|
+
"Posiform must have signed, integral type coefficients");
|
|
188
|
+
_constant_posiform = 0;
|
|
189
|
+
_max_absolute_value = 0;
|
|
190
|
+
_num_linear_integral_biases = 0;
|
|
191
|
+
_num_bqm_variables = bqm.num_variables();
|
|
192
|
+
_quadratic_iterators.resize(_num_bqm_variables);
|
|
193
|
+
_linear_double_biases.resize(_num_bqm_variables, 0);
|
|
194
|
+
_linear_integral_biases.resize(_num_bqm_variables, 0);
|
|
195
|
+
_num_quadratic_integral_biases.resize(_num_bqm_variables, 0);
|
|
196
|
+
|
|
197
|
+
// Apart from finding the maximum absolute value in the bqm, we must
|
|
198
|
+
// consider the sum of the absolute values of linear values found by using
|
|
199
|
+
// non integral bqm-biases for calculating the conversion ratio. As that
|
|
200
|
+
// value converted to integral type is the upper bound for max flow. This is
|
|
201
|
+
// because the linear values correspond to the capacities of the edges in
|
|
202
|
+
// the implication network connecting to source/sink. Thus we calculate the
|
|
203
|
+
// linear values of posiform in double format. Note in the posiform all
|
|
204
|
+
// linear values are positive, here we keep the sign to indicate whether
|
|
205
|
+
// X_source is connected to X_i or X_i'.
|
|
206
|
+
for (int bqm_variable = 0; bqm_variable < _num_bqm_variables;
|
|
207
|
+
bqm_variable++) {
|
|
208
|
+
auto bqm_linear = bqm.linear(bqm_variable);
|
|
209
|
+
auto bqm_linear_abs = std::fabs(bqm_linear);
|
|
210
|
+
_linear_double_biases[bqm_variable] = bqm_linear;
|
|
211
|
+
if (_max_absolute_value < bqm_linear_abs) {
|
|
212
|
+
_max_absolute_value = bqm_linear_abs;
|
|
213
|
+
}
|
|
214
|
+
auto span =
|
|
215
|
+
std::make_pair(std::lower_bound(bqm.cbegin_neighborhood(bqm_variable),
|
|
216
|
+
bqm.cend_neighborhood(bqm_variable), bqm_variable + 1),
|
|
217
|
+
bqm.cend_neighborhood(bqm_variable));
|
|
218
|
+
_quadratic_iterators[bqm_variable] = span;
|
|
219
|
+
if (span.first != span.second) {
|
|
220
|
+
for (auto it_end = span.second; span.first != it_end; span.first++) {
|
|
221
|
+
auto bqm_quadratic = span.first->bias;
|
|
222
|
+
auto bqm_quadratic_abs = std::fabs(bqm_quadratic);
|
|
223
|
+
if (bqm_quadratic < 0) {
|
|
224
|
+
_linear_double_biases[bqm_variable] += bqm_quadratic;
|
|
225
|
+
}
|
|
226
|
+
if (_max_absolute_value < bqm_quadratic_abs) {
|
|
227
|
+
_max_absolute_value = bqm_quadratic_abs;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// See comment above regarding calculating conversion ratio. We do not know
|
|
234
|
+
// the conversion ratio yet so we consider all the linear values, including
|
|
235
|
+
// those which will be flushed to zero and will not be in the posiform. Hence
|
|
236
|
+
// we are summing up the linear coefficients for the posiform over all the bqm
|
|
237
|
+
// variables, not the posiform variables.
|
|
238
|
+
_posiform_linear_sum_non_integral = 0;
|
|
239
|
+
for (std::size_t bqm_variable = 0; bqm_variable < _linear_double_biases.size();
|
|
240
|
+
bqm_variable++) {
|
|
241
|
+
_posiform_linear_sum_non_integral +=
|
|
242
|
+
std::fabs(_linear_double_biases[bqm_variable]);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// Consider the upper limit of max-flow in implication graph for calculating
|
|
246
|
+
// conversion ratio.
|
|
247
|
+
if (_max_absolute_value < _posiform_linear_sum_non_integral) {
|
|
248
|
+
_max_absolute_value = _posiform_linear_sum_non_integral;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (_max_absolute_value != 0) {
|
|
252
|
+
_bias_conversion_ratio =
|
|
253
|
+
static_cast<double>(std::numeric_limits<coefficient_type>::max()) /
|
|
254
|
+
_max_absolute_value;
|
|
255
|
+
|
|
256
|
+
// We should divide the ratio by at least 2 to avoid overflow because we do
|
|
257
|
+
// not divide by 2 when we make the residual network symmetric. In that step
|
|
258
|
+
// the total flow is effectively doubled along with capacities. Divinding just
|
|
259
|
+
// by 2 introduced overflow, thus we divide by a number larger than 2.
|
|
260
|
+
// TODO : Find the theoretical optimal number for division, for now we divide
|
|
261
|
+
// by 4 to be safe.
|
|
262
|
+
_bias_conversion_ratio /= 4;
|
|
263
|
+
|
|
264
|
+
for (int bqm_variable = 0; bqm_variable < _num_bqm_variables;
|
|
265
|
+
bqm_variable++) {
|
|
266
|
+
_linear_integral_biases[bqm_variable] =
|
|
267
|
+
convertToPosiformCoefficient(bqm.linear(bqm_variable));
|
|
268
|
+
int num_nonZero_quadratic_biases_in_upper_triangular_row = 0;
|
|
269
|
+
auto it = _quadratic_iterators[bqm_variable].first;
|
|
270
|
+
auto it_end = _quadratic_iterators[bqm_variable].second;
|
|
271
|
+
for (; it != it_end; it++) {
|
|
272
|
+
// Note the checks for zero below must be done after conversion since some
|
|
273
|
+
// values may get flushed to zero, we want the linear values and quadratic
|
|
274
|
+
// values adhere to the same conversion protocal and be consistent.
|
|
275
|
+
// Otherwise in the implication graph, there may be paths with flow values
|
|
276
|
+
// of small capacities corresponding to numerical errors, affecting the
|
|
277
|
+
// final result. For this same reason we do not use the already computed
|
|
278
|
+
// linear values for the posiform in double format.
|
|
279
|
+
auto bias_quadratic_integral = convertToPosiformCoefficient(it->bias);
|
|
280
|
+
if (bias_quadratic_integral) {
|
|
281
|
+
_num_quadratic_integral_biases[it->v]++;
|
|
282
|
+
if (bias_quadratic_integral < 0) {
|
|
283
|
+
_linear_integral_biases[bqm_variable] += bias_quadratic_integral;
|
|
284
|
+
}
|
|
285
|
+
num_nonZero_quadratic_biases_in_upper_triangular_row++;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
_num_quadratic_integral_biases[bqm_variable] +=
|
|
289
|
+
num_nonZero_quadratic_biases_in_upper_triangular_row;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
_posiform_linear_sum_integral = 0; // For debugging purposes.
|
|
293
|
+
for (std::size_t bqm_variable = 0; bqm_variable < _linear_integral_biases.size();
|
|
294
|
+
bqm_variable++) {
|
|
295
|
+
if (_linear_integral_biases[bqm_variable]) {
|
|
296
|
+
_num_linear_integral_biases++;
|
|
297
|
+
_posiform_linear_sum_integral +=
|
|
298
|
+
std::llabs(_linear_integral_biases[bqm_variable]);
|
|
299
|
+
}
|
|
300
|
+
if (_linear_integral_biases[bqm_variable] < 0) {
|
|
301
|
+
_constant_posiform += _linear_integral_biases[bqm_variable];
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
_posiform_to_bqm_variable_map.reserve(_num_bqm_variables);
|
|
306
|
+
_num_posiform_variables = 0;
|
|
307
|
+
for (int bqm_variable = 0; bqm_variable < _num_bqm_variables;
|
|
308
|
+
bqm_variable++) {
|
|
309
|
+
if (_linear_integral_biases[bqm_variable] ||
|
|
310
|
+
_num_quadratic_integral_biases[bqm_variable]) {
|
|
311
|
+
_posiform_to_bqm_variable_map.push_back(bqm_variable);
|
|
312
|
+
_bqm_to_posiform_variable_map.insert(
|
|
313
|
+
{bqm_variable, _num_posiform_variables});
|
|
314
|
+
_num_posiform_variables++;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
else {
|
|
319
|
+
// All biases in bqm are 0, so the resulting posiform won't have any term in it.
|
|
320
|
+
_num_posiform_variables = 0;
|
|
321
|
+
_constant_posiform = 0;
|
|
322
|
+
_bias_conversion_ratio = 1;
|
|
323
|
+
_posiform_linear_sum_integral = 0;
|
|
324
|
+
_num_linear_integral_biases = 0;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
template <class BQM, class coefficient_t>
|
|
329
|
+
void PosiformInfo<BQM, coefficient_t>::print() {
|
|
330
|
+
std::cout << std::endl;
|
|
331
|
+
std::cout << "Posiform Information : " << std::endl << std::endl;
|
|
332
|
+
std::cout << "Number of BQM Variables : " << _num_bqm_variables << std::endl;
|
|
333
|
+
std::cout << "Constant : " << _constant_posiform << std::endl;
|
|
334
|
+
std::cout << "Maximum Absolute Value : " << _max_absolute_value << std::endl;
|
|
335
|
+
std::cout << "Numeric Limit of Coefficient Type "
|
|
336
|
+
<< std::numeric_limits<coefficient_type>::max() << std::endl;
|
|
337
|
+
std::cout << "Linear Sum in double Format : "
|
|
338
|
+
<< _posiform_linear_sum_non_integral << std::endl;
|
|
339
|
+
std::cout << "Linear Sum Converted to Integral Type : "
|
|
340
|
+
<< convertToPosiformCoefficient(_posiform_linear_sum_non_integral)
|
|
341
|
+
<< std::endl;
|
|
342
|
+
std::cout << "Linear Sum After Summing in Integral Type : "
|
|
343
|
+
<< _posiform_linear_sum_integral << std::endl;
|
|
344
|
+
std::cout << "Ratio Chosen : " << _bias_conversion_ratio << std::endl;
|
|
345
|
+
std::cout << std::endl;
|
|
346
|
+
|
|
347
|
+
// Printing out in convoluted manner, to verify the mappings.
|
|
348
|
+
// Posiform variables should be serially numbered, 0,1,2....
|
|
349
|
+
std::cout << "Used Variables (bqm --> posiform) : "
|
|
350
|
+
<< _posiform_to_bqm_variable_map.size() << std::endl;
|
|
351
|
+
for (int i = 0; i < _posiform_to_bqm_variable_map.size(); i++) {
|
|
352
|
+
std::cout << _posiform_to_bqm_variable_map[i] << " --> "
|
|
353
|
+
<< _bqm_to_posiform_variable_map[_posiform_to_bqm_variable_map[i]]
|
|
354
|
+
<< std::endl;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
std::cout << std::endl;
|
|
358
|
+
std::cout << "Linear (posiform, bqm, value) : " << std::endl;
|
|
359
|
+
for (int bqm_variable = 0; bqm_variable < _num_bqm_variables;
|
|
360
|
+
bqm_variable++) {
|
|
361
|
+
if (_linear_integral_biases[bqm_variable]) {
|
|
362
|
+
std::cout << _bqm_to_posiform_variable_map[bqm_variable] << ", "
|
|
363
|
+
<< bqm_variable << ", " << _linear_integral_biases[bqm_variable]
|
|
364
|
+
<< std::endl;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
std::cout << std::endl;
|
|
369
|
+
std::cout << "Quadratic (posiform-posiform, bqm-bqm, value): " << std::endl;
|
|
370
|
+
for (int bqm_variable = 0; bqm_variable < _num_bqm_variables;
|
|
371
|
+
bqm_variable++) {
|
|
372
|
+
auto it = _quadratic_iterators[bqm_variable].first;
|
|
373
|
+
auto it_end = _quadratic_iterators[bqm_variable].second;
|
|
374
|
+
for (; it != it_end; it++) {
|
|
375
|
+
std::cout << _bqm_to_posiform_variable_map[bqm_variable] << " "
|
|
376
|
+
<< _bqm_to_posiform_variable_map[it->v] << ", ";
|
|
377
|
+
std::cout << bqm_variable << " " << it->v << ", "
|
|
378
|
+
<< convertToPosiformCoefficient(it->bias) << std::endl;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
std::cout << std::endl;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
#endif // POSIFORM_INFORMATION_INCLUDED
|