PyQrackIsing 8.14.4__tar.gz → 8.14.5__tar.gz
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.
- {pyqrackising-8.14.4/pyqrackising.egg-info → pyqrackising-8.14.5}/PKG-INFO +1 -1
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/README.md +1 -3
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/pyproject.toml +1 -1
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/pyqrackising/kernels.cl +105 -278
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/pyqrackising/maxcut_tfim.py +15 -16
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/pyqrackising/maxcut_tfim_sparse.py +15 -16
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/pyqrackising/maxcut_tfim_util.py +16 -24
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/pyqrackising/spin_glass_solver.py +23 -47
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/pyqrackising/spin_glass_solver_sparse.py +22 -67
- {pyqrackising-8.14.4 → pyqrackising-8.14.5/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/setup.py +1 -1
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/LICENSE.md +0 -0
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/MANIFEST.in +0 -0
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/pyqrackising/__init__.py +0 -0
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/pyqrackising/maxcut_tfim_streaming.py +0 -0
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/pyqrackising/spin_glass_solver_streaming.py +0 -0
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/pyqrackising/spin_glass_solver_util.py +0 -0
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/pyqrackising/tsp.py +0 -0
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/pyqrackising/tsp_maxcut.py +0 -0
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-8.14.4 → pyqrackising-8.14.5}/setup.cfg +0 -0
|
@@ -153,9 +153,7 @@ When `is_optimized=True`, the `spin_glass_solver(G)` is used as a final optimiza
|
|
|
153
153
|
|
|
154
154
|
## Environment Variables
|
|
155
155
|
|
|
156
|
-
We expose an environment variable, "`PYQRACKISING_MAX_GPU_PROC_ELEM`", for
|
|
157
|
-
|
|
158
|
-
Similarly, for `is_alt_gpu_sampling=True`, define the "top-`n`" count of highest-weight direct neighbors to retain during sampling with environment variable "`PYQRACKISING_GPU_TOP_N`". The default is `32`. Increasing this might increase solution quality, but it will also increase time-to-solution and private memory usage.
|
|
156
|
+
We expose an environment variable, "`PYQRACKISING_MAX_GPU_PROC_ELEM`", for OpenCL-based solvers. The default value (when the variable is not set) is queried from the OpenCL device properties. You might see performance benefit from tuning this manually to several times your device's number of "compute units" (or tune it down to reduce private memory usage).
|
|
159
157
|
|
|
160
158
|
By default, PyQrackIsing expects all `numpy` floating-point array inputs to be 32-bit. If you'd like to use 64-bit, you can set environment variable `PYQRACKISING_FPPOW=6` (meaning, 2^6=64, for the "floating-point (precision) power"). The default is `5`, for 32-bit. 16-bit is stubbed out and compiles for OpenCL, but the bigger hurdle is that `numpy` on `x86_64` doesn't provide a 16-bit floating point implementation. (As author of Qrack, I could suggest to the `numpy` maintainers that open-source, IEEE-compliant software-based implementations exist for `x86_64` and other architectures, but I'm sure they're aware and likely waiting for in-compiler support.) If you're on an ARM-based architecture, there's a good chance 16-bit floating-point will work, if `numpy` uses the native hardware support.
|
|
161
159
|
|
|
@@ -10,7 +10,7 @@ build-backend = "setuptools.build_meta"
|
|
|
10
10
|
|
|
11
11
|
[project]
|
|
12
12
|
name = "pyqrackising"
|
|
13
|
-
version = "8.14.
|
|
13
|
+
version = "8.14.5"
|
|
14
14
|
requires-python = ">=3.8"
|
|
15
15
|
description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
|
|
16
16
|
readme = {file = "README.txt", content-type = "text/markdown"}
|
|
@@ -1,21 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
void reduce_energy_index(real1 energy, int i, __local real1* loc_energy, __local int* loc_index, __global real1* max_energy_ptr, __global int* max_index_ptr) {
|
|
2
|
+
const int lt_id = get_local_id(0);
|
|
3
|
+
const int lt_size = get_local_size(0);
|
|
4
|
+
|
|
5
|
+
loc_energy[lt_id] = energy;
|
|
6
|
+
loc_index[lt_id] = i;
|
|
7
|
+
|
|
8
|
+
// Reduce within workgroup
|
|
9
|
+
for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
|
|
10
|
+
barrier(CLK_LOCAL_MEM_FENCE);
|
|
11
|
+
real1 hid_energy, lid_energy;
|
|
12
|
+
if (lt_id < offset) {
|
|
13
|
+
hid_energy = loc_energy[lt_id + offset];
|
|
14
|
+
lid_energy = loc_energy[lt_id];
|
|
15
|
+
if (hid_energy > lid_energy) {
|
|
16
|
+
loc_energy[lt_id] = hid_energy;
|
|
17
|
+
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
barrier(CLK_LOCAL_MEM_FENCE);
|
|
23
|
+
|
|
24
|
+
// Write out per-group result
|
|
25
|
+
if (lt_id == 0) {
|
|
26
|
+
max_energy_ptr[get_group_id(0)] = loc_energy[0];
|
|
27
|
+
max_index_ptr[get_group_id(0)] = loc_index[0];
|
|
28
|
+
}
|
|
16
29
|
}
|
|
17
30
|
|
|
18
|
-
real1 bootstrap_worker(__constant char* theta, __global const real1* G_m,
|
|
31
|
+
real1 bootstrap_worker(__constant char* theta, __global const real1* G_m, __global int* indices, const int k, const int n, const bool is_spin_glass) {
|
|
19
32
|
real1 energy = ZERO_R1;
|
|
20
33
|
const size_t n_st = (size_t)n;
|
|
21
34
|
for (int u = 0; u < n; ++u) {
|
|
@@ -37,9 +50,9 @@ real1 bootstrap_worker(__constant char* theta, __global const real1* G_m, __cons
|
|
|
37
50
|
}
|
|
38
51
|
}
|
|
39
52
|
if (u_bit != v_bit) {
|
|
40
|
-
energy -= val;
|
|
41
|
-
} else if (is_spin_glass) {
|
|
42
53
|
energy += val;
|
|
54
|
+
} else if (is_spin_glass) {
|
|
55
|
+
energy -= val;
|
|
43
56
|
}
|
|
44
57
|
}
|
|
45
58
|
}
|
|
@@ -50,10 +63,10 @@ real1 bootstrap_worker(__constant char* theta, __global const real1* G_m, __cons
|
|
|
50
63
|
__kernel void bootstrap(
|
|
51
64
|
__global const real1* G_m,
|
|
52
65
|
__constant char* best_theta,
|
|
53
|
-
|
|
66
|
+
__global int* indices,
|
|
54
67
|
__constant int* args, // args[0] = n, args[1] = k
|
|
55
|
-
__global real1*
|
|
56
|
-
__global int*
|
|
68
|
+
__global real1* max_energy_ptr, // output: per-group min energy
|
|
69
|
+
__global int* max_index_ptr, // output: per-group best index (i)
|
|
57
70
|
__local real1* loc_energy, // local memory buffer
|
|
58
71
|
__local int* loc_index // local memory buffer
|
|
59
72
|
) {
|
|
@@ -61,55 +74,26 @@ __kernel void bootstrap(
|
|
|
61
74
|
const int k = args[1];
|
|
62
75
|
const int combo_count = args[2];
|
|
63
76
|
const bool is_spin_glass = args[3];
|
|
64
|
-
uint prng_seed = (uint)args[4];
|
|
65
|
-
int i = get_global_id(0);
|
|
66
77
|
|
|
67
|
-
|
|
68
|
-
|
|
78
|
+
int i = get_global_id(0);
|
|
79
|
+
const int max_i = get_global_size(0);
|
|
69
80
|
|
|
70
|
-
real1 best_energy = INFINITY;
|
|
81
|
+
real1 best_energy = -INFINITY;
|
|
71
82
|
int best_i = i;
|
|
72
83
|
|
|
73
|
-
for (; i < combo_count; i +=
|
|
84
|
+
for (; i < combo_count; i += max_i) {
|
|
74
85
|
const int j = i * k;
|
|
75
|
-
const real1 energy = bootstrap_worker(best_theta, G_m,
|
|
76
|
-
if (energy
|
|
86
|
+
const real1 energy = bootstrap_worker(best_theta, G_m, indices + j, k, n, is_spin_glass);
|
|
87
|
+
if (energy > best_energy) {
|
|
77
88
|
best_energy = energy;
|
|
78
89
|
best_i = i;
|
|
79
|
-
} else if (((energy - best_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
80
|
-
best_i = i;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const int lt_id = get_local_id(0);
|
|
85
|
-
const int lt_size = get_local_size(0);
|
|
86
|
-
|
|
87
|
-
loc_energy[lt_id] = best_energy;
|
|
88
|
-
loc_index[lt_id] = best_i;
|
|
89
|
-
|
|
90
|
-
// Reduce within workgroup
|
|
91
|
-
for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
|
|
92
|
-
barrier(CLK_LOCAL_MEM_FENCE);
|
|
93
|
-
if (lt_id < offset) {
|
|
94
|
-
real1 hid_energy = loc_energy[lt_id + offset];
|
|
95
|
-
real1 lid_energy = loc_energy[lt_id];
|
|
96
|
-
if (hid_energy < lid_energy) {
|
|
97
|
-
loc_energy[lt_id] = hid_energy;
|
|
98
|
-
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
99
|
-
} else if (((hid_energy - lid_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
100
|
-
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
101
|
-
}
|
|
102
90
|
}
|
|
103
91
|
}
|
|
104
92
|
|
|
105
|
-
|
|
106
|
-
if (lt_id == 0) {
|
|
107
|
-
min_energy_ptr[get_group_id(0)] = loc_energy[0];
|
|
108
|
-
min_index_ptr[get_group_id(0)] = loc_index[0];
|
|
109
|
-
}
|
|
93
|
+
reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
|
|
110
94
|
}
|
|
111
95
|
|
|
112
|
-
real1 bootstrap_worker_sparse(__constant char* theta, __global const real1* G_data, __global const uint* G_rows, __global const uint* G_cols,
|
|
96
|
+
real1 bootstrap_worker_sparse(__constant char* theta, __global const real1* G_data, __global const uint* G_rows, __global const uint* G_cols, __global int* indices, const int k, const int n, const bool is_spin_glass) {
|
|
113
97
|
real1 energy = ZERO_R1;
|
|
114
98
|
for (int u = 0; u < n; ++u) {
|
|
115
99
|
bool u_bit = theta[u];
|
|
@@ -131,9 +115,9 @@ real1 bootstrap_worker_sparse(__constant char* theta, __global const real1* G_da
|
|
|
131
115
|
}
|
|
132
116
|
}
|
|
133
117
|
if (u_bit != v_bit) {
|
|
134
|
-
energy -= val;
|
|
135
|
-
} else if (is_spin_glass) {
|
|
136
118
|
energy += val;
|
|
119
|
+
} else if (is_spin_glass) {
|
|
120
|
+
energy -= val;
|
|
137
121
|
}
|
|
138
122
|
}
|
|
139
123
|
}
|
|
@@ -146,10 +130,10 @@ __kernel void bootstrap_sparse(
|
|
|
146
130
|
__global const uint* G_rows,
|
|
147
131
|
__global const uint* G_cols,
|
|
148
132
|
__constant char* best_theta,
|
|
149
|
-
|
|
133
|
+
__global int* indices,
|
|
150
134
|
__constant int* args, // args[0] = n, args[1] = k
|
|
151
|
-
__global real1*
|
|
152
|
-
__global int*
|
|
135
|
+
__global real1* max_energy_ptr, // output: per-group min energy
|
|
136
|
+
__global int* max_index_ptr, // output: per-group best index (i)
|
|
153
137
|
__local real1* loc_energy, // local memory buffer
|
|
154
138
|
__local int* loc_index // local memory buffer
|
|
155
139
|
) {
|
|
@@ -157,51 +141,23 @@ __kernel void bootstrap_sparse(
|
|
|
157
141
|
const int k = args[1];
|
|
158
142
|
const int combo_count = args[2];
|
|
159
143
|
const bool is_spin_glass = args[3];
|
|
160
|
-
uint prng_seed = (uint)args[4];
|
|
161
|
-
int i = get_global_id(0);
|
|
162
144
|
|
|
163
|
-
|
|
145
|
+
int i = get_global_id(0);
|
|
146
|
+
const int max_i = get_global_size(0);
|
|
164
147
|
|
|
165
|
-
real1 best_energy = INFINITY;
|
|
148
|
+
real1 best_energy = -INFINITY;
|
|
166
149
|
int best_i = i;
|
|
167
150
|
|
|
168
|
-
for (; i < combo_count; i +=
|
|
151
|
+
for (; i < combo_count; i += max_i) {
|
|
169
152
|
const int j = i * k;
|
|
170
|
-
const real1 energy = bootstrap_worker_sparse(best_theta, G_data, G_rows, G_cols,
|
|
171
|
-
if (energy
|
|
153
|
+
const real1 energy = bootstrap_worker_sparse(best_theta, G_data, G_rows, G_cols, indices + j, k, n, is_spin_glass);
|
|
154
|
+
if (energy > best_energy) {
|
|
172
155
|
best_energy = energy;
|
|
173
156
|
best_i = i;
|
|
174
|
-
} else if (((energy - best_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
175
|
-
best_i = i;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
const int lt_id = get_local_id(0);
|
|
180
|
-
const int lt_size = get_local_size(0);
|
|
181
|
-
|
|
182
|
-
loc_energy[lt_id] = best_energy;
|
|
183
|
-
loc_index[lt_id] = best_i;
|
|
184
|
-
|
|
185
|
-
// Reduce within workgroup
|
|
186
|
-
for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
|
|
187
|
-
barrier(CLK_LOCAL_MEM_FENCE);
|
|
188
|
-
if (lt_id < offset) {
|
|
189
|
-
real1 hid_energy = loc_energy[lt_id + offset];
|
|
190
|
-
real1 lid_energy = loc_energy[lt_id];
|
|
191
|
-
if (hid_energy < lid_energy) {
|
|
192
|
-
loc_energy[lt_id] = hid_energy;
|
|
193
|
-
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
194
|
-
} else if (((hid_energy - lid_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
195
|
-
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
196
|
-
}
|
|
197
157
|
}
|
|
198
158
|
}
|
|
199
159
|
|
|
200
|
-
|
|
201
|
-
if (lt_id == 0) {
|
|
202
|
-
min_energy_ptr[get_group_id(0)] = loc_energy[0];
|
|
203
|
-
min_index_ptr[get_group_id(0)] = loc_index[0];
|
|
204
|
-
}
|
|
160
|
+
reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
|
|
205
161
|
}
|
|
206
162
|
|
|
207
163
|
// Helper to read from segmented G_m
|
|
@@ -216,7 +172,7 @@ inline real1 get_G_m(
|
|
|
216
172
|
real1 bootstrap_worker_segmented(
|
|
217
173
|
__constant char* theta,
|
|
218
174
|
__global const real1** G_m,
|
|
219
|
-
|
|
175
|
+
__global int* indices,
|
|
220
176
|
const int k,
|
|
221
177
|
const int n,
|
|
222
178
|
const int segment_size,
|
|
@@ -247,9 +203,9 @@ real1 bootstrap_worker_segmented(
|
|
|
247
203
|
}
|
|
248
204
|
|
|
249
205
|
if (u_bit != v_bit) {
|
|
250
|
-
energy -= val;
|
|
251
|
-
} else if (is_spin_glass) {
|
|
252
206
|
energy += val;
|
|
207
|
+
} else if (is_spin_glass) {
|
|
208
|
+
energy -= val;
|
|
253
209
|
}
|
|
254
210
|
}
|
|
255
211
|
}
|
|
@@ -264,10 +220,10 @@ __kernel void bootstrap_segmented(
|
|
|
264
220
|
__global const real1* G_m2,
|
|
265
221
|
__global const real1* G_m3,
|
|
266
222
|
__constant char* best_theta,
|
|
267
|
-
|
|
223
|
+
__global int* indices,
|
|
268
224
|
__constant int* args, // args[0]=n, args[1]=k, args[2]=combo_count, args[3]=segment_size
|
|
269
|
-
__global real1*
|
|
270
|
-
__global int*
|
|
225
|
+
__global real1* max_energy_ptr,
|
|
226
|
+
__global int* max_index_ptr,
|
|
271
227
|
__local real1* loc_energy,
|
|
272
228
|
__local int* loc_index
|
|
273
229
|
) {
|
|
@@ -277,50 +233,24 @@ __kernel void bootstrap_segmented(
|
|
|
277
233
|
const int k = args[1];
|
|
278
234
|
const int combo_count = args[2];
|
|
279
235
|
const bool is_spin_glass = args[3];
|
|
280
|
-
|
|
281
|
-
const int segment_size = args[5];
|
|
282
|
-
int i = get_global_id(0);
|
|
236
|
+
const int segment_size = args[4];
|
|
283
237
|
|
|
284
|
-
|
|
238
|
+
int i = get_global_id(0);
|
|
239
|
+
const int max_i = get_global_size(0);
|
|
285
240
|
|
|
286
|
-
real1 best_energy = INFINITY;
|
|
241
|
+
real1 best_energy = -INFINITY;
|
|
287
242
|
int best_i = i;
|
|
288
243
|
|
|
289
|
-
for (; i < combo_count; i +=
|
|
244
|
+
for (; i < combo_count; i += max_i) {
|
|
290
245
|
const int j = i * k;
|
|
291
|
-
const real1 energy = bootstrap_worker_segmented(best_theta, G_m,
|
|
292
|
-
if (energy
|
|
246
|
+
const real1 energy = bootstrap_worker_segmented(best_theta, G_m, indices + j, k, n, segment_size, is_spin_glass);
|
|
247
|
+
if (energy > best_energy) {
|
|
293
248
|
best_energy = energy;
|
|
294
249
|
best_i = i;
|
|
295
|
-
} else if (((energy - best_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
296
|
-
best_i = i;
|
|
297
250
|
}
|
|
298
251
|
}
|
|
299
252
|
|
|
300
|
-
|
|
301
|
-
const int lt_size = get_local_size(0);
|
|
302
|
-
|
|
303
|
-
loc_energy[lt_id] = best_energy;
|
|
304
|
-
loc_index[lt_id] = best_i;
|
|
305
|
-
|
|
306
|
-
for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
|
|
307
|
-
barrier(CLK_LOCAL_MEM_FENCE);
|
|
308
|
-
if (lt_id < offset) {
|
|
309
|
-
real1 hid_energy = loc_energy[lt_id + offset];
|
|
310
|
-
real1 lid_energy = loc_energy[lt_id];
|
|
311
|
-
if (hid_energy < lid_energy) {
|
|
312
|
-
loc_energy[lt_id] = hid_energy;
|
|
313
|
-
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
314
|
-
} else if (((hid_energy - lid_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
315
|
-
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
if (lt_id == 0) {
|
|
321
|
-
min_energy_ptr[get_group_id(0)] = loc_energy[0];
|
|
322
|
-
min_index_ptr[get_group_id(0)] = loc_index[0];
|
|
323
|
-
}
|
|
253
|
+
reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
|
|
324
254
|
}
|
|
325
255
|
|
|
326
256
|
real1 bootstrap_worker_sparse_segmented(
|
|
@@ -328,7 +258,7 @@ real1 bootstrap_worker_sparse_segmented(
|
|
|
328
258
|
__global const real1** G_data,
|
|
329
259
|
__global const uint* G_rows,
|
|
330
260
|
__global const uint* G_cols,
|
|
331
|
-
|
|
261
|
+
__global int* indices,
|
|
332
262
|
const int k,
|
|
333
263
|
const int n,
|
|
334
264
|
const int segment_size,
|
|
@@ -361,9 +291,9 @@ real1 bootstrap_worker_sparse_segmented(
|
|
|
361
291
|
}
|
|
362
292
|
|
|
363
293
|
if (u_bit != v_bit) {
|
|
364
|
-
energy -= val;
|
|
365
|
-
} else if (is_spin_glass) {
|
|
366
294
|
energy += val;
|
|
295
|
+
} else if (is_spin_glass) {
|
|
296
|
+
energy -= val;
|
|
367
297
|
}
|
|
368
298
|
}
|
|
369
299
|
}
|
|
@@ -379,10 +309,10 @@ __kernel void bootstrap_sparse_segmented(
|
|
|
379
309
|
__global const uint* G_rows,
|
|
380
310
|
__global const uint* G_cols,
|
|
381
311
|
__constant char* best_theta,
|
|
382
|
-
|
|
312
|
+
__global int* indices,
|
|
383
313
|
__constant int* args, // args[0] = n, args[1] = k, args[2] = combo_count, args[3] = segment_size
|
|
384
|
-
__global real1*
|
|
385
|
-
__global int*
|
|
314
|
+
__global real1* max_energy_ptr,
|
|
315
|
+
__global int* max_index_ptr,
|
|
386
316
|
__local real1* loc_energy,
|
|
387
317
|
__local int* loc_index
|
|
388
318
|
) {
|
|
@@ -392,50 +322,24 @@ __kernel void bootstrap_sparse_segmented(
|
|
|
392
322
|
const int k = args[1];
|
|
393
323
|
const int combo_count = args[2];
|
|
394
324
|
const bool is_spin_glass = args[3];
|
|
395
|
-
|
|
396
|
-
const int segment_size = args[5];
|
|
397
|
-
int i = get_global_id(0);
|
|
325
|
+
const int segment_size = args[4];
|
|
398
326
|
|
|
399
|
-
|
|
327
|
+
int i = get_global_id(0);
|
|
328
|
+
const int max_i = get_global_size(0);
|
|
400
329
|
|
|
401
|
-
real1 best_energy = INFINITY;
|
|
330
|
+
real1 best_energy = -INFINITY;
|
|
402
331
|
int best_i = i;
|
|
403
332
|
|
|
404
|
-
for (; i < combo_count; i +=
|
|
333
|
+
for (; i < combo_count; i += max_i) {
|
|
405
334
|
const int j = i * k;
|
|
406
|
-
const real1 energy = bootstrap_worker_sparse_segmented(best_theta, G_data, G_rows, G_cols,
|
|
407
|
-
if (energy
|
|
335
|
+
const real1 energy = bootstrap_worker_sparse_segmented(best_theta, G_data, G_rows, G_cols, indices + j, k, n, segment_size, is_spin_glass);
|
|
336
|
+
if (energy > best_energy) {
|
|
408
337
|
best_energy = energy;
|
|
409
338
|
best_i = i;
|
|
410
|
-
} else if (((energy - best_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
411
|
-
best_i = i;
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
const int lt_id = get_local_id(0);
|
|
416
|
-
const int lt_size = get_local_size(0);
|
|
417
|
-
|
|
418
|
-
loc_energy[lt_id] = best_energy;
|
|
419
|
-
loc_index[lt_id] = best_i;
|
|
420
|
-
|
|
421
|
-
for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
|
|
422
|
-
barrier(CLK_LOCAL_MEM_FENCE);
|
|
423
|
-
if (lt_id < offset) {
|
|
424
|
-
real1 hid_energy = loc_energy[lt_id + offset];
|
|
425
|
-
real1 lid_energy = loc_energy[lt_id];
|
|
426
|
-
if (hid_energy < lid_energy) {
|
|
427
|
-
loc_energy[lt_id] = hid_energy;
|
|
428
|
-
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
429
|
-
} else if (((hid_energy - lid_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
430
|
-
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
431
|
-
}
|
|
432
339
|
}
|
|
433
340
|
}
|
|
434
341
|
|
|
435
|
-
|
|
436
|
-
min_energy_ptr[get_group_id(0)] = loc_energy[0];
|
|
437
|
-
min_index_ptr[get_group_id(0)] = loc_index[0];
|
|
438
|
-
}
|
|
342
|
+
reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
|
|
439
343
|
}
|
|
440
344
|
|
|
441
345
|
inline bool get_bit(__global const uint* theta, const size_t u) {
|
|
@@ -472,15 +376,17 @@ __kernel void calculate_cut(
|
|
|
472
376
|
__local int* loc_index // local memory buffer
|
|
473
377
|
) {
|
|
474
378
|
const int n = args[0];
|
|
475
|
-
const int n32 = (n + 31) >> 5U;
|
|
476
379
|
const int shots = args[1];
|
|
477
380
|
const bool is_spin_glass = args[2];
|
|
381
|
+
const int n32 = (n + 31) >> 5U;
|
|
382
|
+
|
|
478
383
|
int i = get_global_id(0);
|
|
384
|
+
const int max_i = get_global_size(0);
|
|
479
385
|
|
|
480
386
|
real1 best_energy = -INFINITY;
|
|
481
387
|
int best_i = i;
|
|
482
388
|
|
|
483
|
-
for (; i < shots; i +=
|
|
389
|
+
for (; i < shots; i += max_i) {
|
|
484
390
|
const int j = i * n32;
|
|
485
391
|
const real1 energy = cut_worker(theta + j, G_m, n, is_spin_glass);
|
|
486
392
|
if (energy > best_energy) {
|
|
@@ -489,30 +395,7 @@ __kernel void calculate_cut(
|
|
|
489
395
|
}
|
|
490
396
|
}
|
|
491
397
|
|
|
492
|
-
|
|
493
|
-
const int lt_size = get_local_size(0);
|
|
494
|
-
|
|
495
|
-
loc_energy[lt_id] = best_energy;
|
|
496
|
-
loc_index[lt_id] = best_i;
|
|
497
|
-
|
|
498
|
-
// Reduce within workgroup
|
|
499
|
-
for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
|
|
500
|
-
barrier(CLK_LOCAL_MEM_FENCE);
|
|
501
|
-
if (lt_id < offset) {
|
|
502
|
-
real1 hid_energy = loc_energy[lt_id + offset];
|
|
503
|
-
real1 lid_energy = loc_energy[lt_id];
|
|
504
|
-
if (hid_energy > lid_energy) {
|
|
505
|
-
loc_energy[lt_id] = hid_energy;
|
|
506
|
-
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
// Write out per-group result
|
|
512
|
-
if (lt_id == 0) {
|
|
513
|
-
max_energy_ptr[get_group_id(0)] = loc_energy[0];
|
|
514
|
-
max_index_ptr[get_group_id(0)] = loc_index[0];
|
|
515
|
-
}
|
|
398
|
+
reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
|
|
516
399
|
}
|
|
517
400
|
|
|
518
401
|
real1 cut_worker_sparse(__global const uint* theta, __global const real1* G_data, __global const uint* G_rows, __global const uint* G_cols, const int n, const bool is_spin_glass) {
|
|
@@ -549,13 +432,16 @@ __kernel void calculate_cut_sparse(
|
|
|
549
432
|
const int n = args[0];
|
|
550
433
|
const int shots = args[1];
|
|
551
434
|
const bool is_spin_glass = args[2];
|
|
435
|
+
const int n32 = (n + 31) >> 5U;
|
|
436
|
+
|
|
552
437
|
int i = get_global_id(0);
|
|
438
|
+
const int max_i = get_global_size(0);
|
|
553
439
|
|
|
554
440
|
real1 best_energy = -INFINITY;
|
|
555
441
|
int best_i = i;
|
|
556
442
|
|
|
557
|
-
for (; i < shots; i +=
|
|
558
|
-
const int j =
|
|
443
|
+
for (; i < shots; i += max_i) {
|
|
444
|
+
const int j = i * n32;
|
|
559
445
|
const real1 energy = cut_worker_sparse(theta + j, G_data, G_rows, G_cols, n, is_spin_glass);
|
|
560
446
|
if (energy > best_energy) {
|
|
561
447
|
best_energy = energy;
|
|
@@ -563,30 +449,7 @@ __kernel void calculate_cut_sparse(
|
|
|
563
449
|
}
|
|
564
450
|
}
|
|
565
451
|
|
|
566
|
-
|
|
567
|
-
const int lt_size = get_local_size(0);
|
|
568
|
-
|
|
569
|
-
loc_energy[lt_id] = best_energy;
|
|
570
|
-
loc_index[lt_id] = best_i;
|
|
571
|
-
|
|
572
|
-
// Reduce within workgroup
|
|
573
|
-
for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
|
|
574
|
-
barrier(CLK_LOCAL_MEM_FENCE);
|
|
575
|
-
if (lt_id < offset) {
|
|
576
|
-
real1 hid_energy = loc_energy[lt_id + offset];
|
|
577
|
-
real1 lid_energy = loc_energy[lt_id];
|
|
578
|
-
if (hid_energy > lid_energy) {
|
|
579
|
-
loc_energy[lt_id] = hid_energy;
|
|
580
|
-
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
// Write out per-group result
|
|
586
|
-
if (lt_id == 0) {
|
|
587
|
-
max_energy_ptr[get_group_id(0)] = loc_energy[0];
|
|
588
|
-
max_index_ptr[get_group_id(0)] = loc_index[0];
|
|
589
|
-
}
|
|
452
|
+
reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
|
|
590
453
|
}
|
|
591
454
|
|
|
592
455
|
real1 cut_worker_segmented(
|
|
@@ -641,13 +504,16 @@ __kernel void calculate_cut_segmented(
|
|
|
641
504
|
const bool is_spin_glass = args[2];
|
|
642
505
|
const int segment_size = args[3];
|
|
643
506
|
const int theta_segment_size = args[4];
|
|
507
|
+
const int n32 = (n + 31) >> 5U;
|
|
508
|
+
|
|
644
509
|
int i = get_global_id(0);
|
|
510
|
+
const int max_i = get_global_size(0);
|
|
645
511
|
|
|
646
512
|
real1 best_energy = -INFINITY;
|
|
647
513
|
int best_i = i;
|
|
648
514
|
|
|
649
|
-
for (; i < shots; i +=
|
|
650
|
-
const int j =
|
|
515
|
+
for (; i < shots; i += max_i) {
|
|
516
|
+
const int j = i * n32;
|
|
651
517
|
const real1 energy = cut_worker_segmented(theta[j / theta_segment_size] + (j % theta_segment_size), G_m, n, segment_size, is_spin_glass);
|
|
652
518
|
if (energy > best_energy) {
|
|
653
519
|
best_energy = energy;
|
|
@@ -655,28 +521,7 @@ __kernel void calculate_cut_segmented(
|
|
|
655
521
|
}
|
|
656
522
|
}
|
|
657
523
|
|
|
658
|
-
|
|
659
|
-
const int lt_size = get_local_size(0);
|
|
660
|
-
|
|
661
|
-
loc_energy[lt_id] = best_energy;
|
|
662
|
-
loc_index[lt_id] = best_i;
|
|
663
|
-
|
|
664
|
-
for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
|
|
665
|
-
barrier(CLK_LOCAL_MEM_FENCE);
|
|
666
|
-
if (lt_id < offset) {
|
|
667
|
-
real1 hid_energy = loc_energy[lt_id + offset];
|
|
668
|
-
real1 lid_energy = loc_energy[lt_id];
|
|
669
|
-
if (hid_energy > lid_energy) {
|
|
670
|
-
loc_energy[lt_id] = hid_energy;
|
|
671
|
-
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
if (lt_id == 0) {
|
|
677
|
-
max_energy_ptr[get_group_id(0)] = loc_energy[0];
|
|
678
|
-
max_index_ptr[get_group_id(0)] = loc_index[0];
|
|
679
|
-
}
|
|
524
|
+
reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
|
|
680
525
|
}
|
|
681
526
|
|
|
682
527
|
real1 cut_worker_sparse_segmented(
|
|
@@ -735,13 +580,16 @@ __kernel void calculate_cut_sparse_segmented(
|
|
|
735
580
|
const bool is_spin_glass = args[2];
|
|
736
581
|
const int segment_size = args[3];
|
|
737
582
|
const int theta_segment_size = args[4];
|
|
583
|
+
const int n32 = (n + 31) >> 5U;
|
|
584
|
+
|
|
738
585
|
int i = get_global_id(0);
|
|
586
|
+
const int max_i = get_global_size(0);
|
|
739
587
|
|
|
740
588
|
real1 best_energy = -INFINITY;
|
|
741
589
|
int best_i = i;
|
|
742
590
|
|
|
743
|
-
for (; i < shots; i +=
|
|
744
|
-
const int j =
|
|
591
|
+
for (; i < shots; i += max_i) {
|
|
592
|
+
const int j = i * n32;
|
|
745
593
|
const real1 energy = cut_worker_sparse_segmented(theta[j / theta_segment_size] + (j % theta_segment_size), G_data, G_rows, G_cols, n, segment_size, is_spin_glass);
|
|
746
594
|
if (energy > best_energy) {
|
|
747
595
|
best_energy = energy;
|
|
@@ -749,26 +597,5 @@ __kernel void calculate_cut_sparse_segmented(
|
|
|
749
597
|
}
|
|
750
598
|
}
|
|
751
599
|
|
|
752
|
-
|
|
753
|
-
const int lt_size = get_local_size(0);
|
|
754
|
-
|
|
755
|
-
loc_energy[lt_id] = best_energy;
|
|
756
|
-
loc_index[lt_id] = best_i;
|
|
757
|
-
|
|
758
|
-
for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
|
|
759
|
-
barrier(CLK_LOCAL_MEM_FENCE);
|
|
760
|
-
if (lt_id < offset) {
|
|
761
|
-
real1 hid_energy = loc_energy[lt_id + offset];
|
|
762
|
-
real1 lid_energy = loc_energy[lt_id];
|
|
763
|
-
if (hid_energy > lid_energy) {
|
|
764
|
-
loc_energy[lt_id] = hid_energy;
|
|
765
|
-
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
766
|
-
}
|
|
767
|
-
}
|
|
768
|
-
}
|
|
769
|
-
|
|
770
|
-
if (lt_id == 0) {
|
|
771
|
-
max_energy_ptr[get_group_id(0)] = loc_energy[0];
|
|
772
|
-
max_index_ptr[get_group_id(0)] = loc_index[0];
|
|
773
|
-
}
|
|
600
|
+
reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
|
|
774
601
|
}
|
|
@@ -4,7 +4,7 @@ import numpy as np
|
|
|
4
4
|
import os
|
|
5
5
|
from numba import njit, prange
|
|
6
6
|
|
|
7
|
-
from .maxcut_tfim_util import convert_bool_to_uint, get_cut, get_cut_base, make_G_m_buf, make_theta_buf, maxcut_hamming_cdf, opencl_context, sample_mag,
|
|
7
|
+
from .maxcut_tfim_util import convert_bool_to_uint, get_cut, get_cut_base, make_G_m_buf, make_theta_buf, maxcut_hamming_cdf, opencl_context, sample_mag, setup_opencl, bit_pick, init_bit_pick
|
|
8
8
|
|
|
9
9
|
IS_OPENCL_AVAILABLE = True
|
|
10
10
|
try:
|
|
@@ -15,6 +15,7 @@ except ImportError:
|
|
|
15
15
|
|
|
16
16
|
epsilon = opencl_context.epsilon
|
|
17
17
|
dtype = opencl_context.dtype
|
|
18
|
+
wgs = opencl_context.work_group_size
|
|
18
19
|
|
|
19
20
|
|
|
20
21
|
@njit
|
|
@@ -144,8 +145,8 @@ def shot_loop(G_m, max_edge, thresholds, weights, tot_init_weight, repulsion_bas
|
|
|
144
145
|
solutions[s] = local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, repulsion_base, n, m)
|
|
145
146
|
|
|
146
147
|
|
|
147
|
-
def sample_for_opencl(G_m, G_m_buf, max_edge, shots, thresholds, weights, repulsion_base, is_spin_glass, is_segmented, segment_size):
|
|
148
|
-
shots = ((max(1, shots >> 1) +
|
|
148
|
+
def sample_for_opencl(G_m, G_m_buf, max_edge, shots, thresholds, weights, repulsion_base, is_spin_glass, is_segmented, segment_size, theta_segment_size):
|
|
149
|
+
shots = ((max(1, shots >> 1) + 31) >> 5) << 5
|
|
149
150
|
n = len(G_m)
|
|
150
151
|
tot_init_weight = weights.sum()
|
|
151
152
|
|
|
@@ -154,7 +155,7 @@ def sample_for_opencl(G_m, G_m_buf, max_edge, shots, thresholds, weights, repuls
|
|
|
154
155
|
best_solution = solutions[0]
|
|
155
156
|
best_energy = -float("inf")
|
|
156
157
|
|
|
157
|
-
opencl_args =
|
|
158
|
+
opencl_args = setup_opencl(shots, shots, np.array([n, shots, is_spin_glass, segment_size, theta_segment_size], dtype=np.int32))
|
|
158
159
|
|
|
159
160
|
improved = True
|
|
160
161
|
while improved:
|
|
@@ -163,11 +164,11 @@ def sample_for_opencl(G_m, G_m_buf, max_edge, shots, thresholds, weights, repuls
|
|
|
163
164
|
solution, energy = run_cut_opencl(solutions, G_m_buf, is_segmented, segment_size, is_spin_glass, *opencl_args)
|
|
164
165
|
if energy > best_energy:
|
|
165
166
|
best_energy = energy
|
|
166
|
-
best_solution = solution
|
|
167
|
+
best_solution = solution.copy()
|
|
167
168
|
improved = True
|
|
168
169
|
|
|
169
170
|
if is_spin_glass:
|
|
170
|
-
best_energy = compute_cut(best_solution, G_m, n)
|
|
171
|
+
best_energy = compute_cut(best_solution, G_m, n)
|
|
171
172
|
|
|
172
173
|
return best_solution, float(best_energy)
|
|
173
174
|
|
|
@@ -264,10 +265,11 @@ def run_cut_opencl(samples, G_m_buf, is_segmented, segment_size, is_spin_glass,
|
|
|
264
265
|
cl.enqueue_copy(queue, max_index_host, max_index_buf)
|
|
265
266
|
queue.finish()
|
|
266
267
|
|
|
267
|
-
# Find global
|
|
268
|
-
|
|
268
|
+
# Find global minimum
|
|
269
|
+
best_x = np.argmax(max_energy_host)
|
|
270
|
+
best_i = max_index_host[best_x]
|
|
269
271
|
|
|
270
|
-
return samples[best_i], max_energy_host[
|
|
272
|
+
return samples[best_i], max_energy_host[best_x]
|
|
271
273
|
|
|
272
274
|
def maxcut_tfim(
|
|
273
275
|
G,
|
|
@@ -324,7 +326,6 @@ def maxcut_tfim(
|
|
|
324
326
|
if repulsion_base is None:
|
|
325
327
|
repulsion_base = 8.0
|
|
326
328
|
|
|
327
|
-
wgs = max(32, opencl_context.work_group_size)
|
|
328
329
|
is_opencl = is_maxcut_gpu and IS_OPENCL_AVAILABLE and (n_qubits >= wgs) and (shots >= wgs)
|
|
329
330
|
|
|
330
331
|
if not is_opencl:
|
|
@@ -336,12 +337,10 @@ def maxcut_tfim(
|
|
|
336
337
|
|
|
337
338
|
return bit_string, best_value, partition
|
|
338
339
|
|
|
339
|
-
segment_size = G_m.shape[0]
|
|
340
|
-
|
|
341
|
-
|
|
340
|
+
segment_size = (G_m.shape[0] * G_m.shape[1] + 3) >> 2
|
|
341
|
+
theta_segment_size = (((n_qubits + 31) >> 5) * (((shots + wgs - 1) // wgs) * wgs) + 3) >> 2
|
|
342
|
+
is_segmented = ((G_m.nbytes << 1) > opencl_context.max_alloc) or ((theta_segment_size << 3) > opencl_context.max_alloc)
|
|
342
343
|
G_m_buf = make_G_m_buf(G_m, is_segmented, segment_size)
|
|
343
|
-
if is_nested:
|
|
344
|
-
opencl_context.G_m_buf = G_m_buf
|
|
345
344
|
|
|
346
345
|
J_eff, degrees, max_edge = init_J_and_z(G_m)
|
|
347
346
|
hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
|
|
@@ -349,7 +348,7 @@ def maxcut_tfim(
|
|
|
349
348
|
degrees = None
|
|
350
349
|
J_eff = 1.0 / (1.0 + epsilon - J_eff)
|
|
351
350
|
|
|
352
|
-
best_solution, best_value = sample_for_opencl(G_m, G_m_buf, max_edge, shots, hamming_prob, J_eff, repulsion_base, is_spin_glass, is_segmented, segment_size)
|
|
351
|
+
best_solution, best_value = sample_for_opencl(G_m, G_m_buf, max_edge, shots, hamming_prob, J_eff, repulsion_base, is_spin_glass, is_segmented, segment_size, theta_segment_size)
|
|
353
352
|
|
|
354
353
|
bit_string, l, r = get_cut(best_solution, nodes)
|
|
355
354
|
|
|
@@ -4,7 +4,7 @@ import numpy as np
|
|
|
4
4
|
import os
|
|
5
5
|
from numba import njit, prange
|
|
6
6
|
|
|
7
|
-
from .maxcut_tfim_util import binary_search, convert_bool_to_uint, get_cut, get_cut_base, make_G_m_csr_buf, make_theta_buf, maxcut_hamming_cdf, opencl_context, sample_mag,
|
|
7
|
+
from .maxcut_tfim_util import binary_search, convert_bool_to_uint, get_cut, get_cut_base, make_G_m_csr_buf, make_theta_buf, maxcut_hamming_cdf, opencl_context, sample_mag, setup_opencl, bit_pick, init_bit_pick, to_scipy_sparse_upper_triangular
|
|
8
8
|
|
|
9
9
|
IS_OPENCL_AVAILABLE = True
|
|
10
10
|
try:
|
|
@@ -15,6 +15,7 @@ except ImportError:
|
|
|
15
15
|
|
|
16
16
|
epsilon = opencl_context.epsilon
|
|
17
17
|
dtype = opencl_context.dtype
|
|
18
|
+
wgs = opencl_context.work_group_size
|
|
18
19
|
|
|
19
20
|
|
|
20
21
|
@njit
|
|
@@ -159,8 +160,8 @@ def shot_loop(G_data, G_rows, G_cols, max_edge, thresholds, weights, tot_init_we
|
|
|
159
160
|
solutions[s] = local_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, tot_init_weight, repulsion_base, n, m)
|
|
160
161
|
|
|
161
162
|
|
|
162
|
-
def sample_for_opencl(G_data, G_rows, G_cols, G_data_buf, G_rows_buf, G_cols_buf, max_edge, shots, thresholds, weights, repulsion_base, is_spin_glass, is_segmented, segment_size):
|
|
163
|
-
shots = ((max(1, shots >> 1) +
|
|
163
|
+
def sample_for_opencl(G_data, G_rows, G_cols, G_data_buf, G_rows_buf, G_cols_buf, max_edge, shots, thresholds, weights, repulsion_base, is_spin_glass, is_segmented, segment_size, theta_segment_size):
|
|
164
|
+
shots = ((max(1, shots >> 1) + 31) >> 5) << 5
|
|
164
165
|
n = G_rows.shape[0] - 1
|
|
165
166
|
tot_init_weight = weights.sum()
|
|
166
167
|
|
|
@@ -169,7 +170,7 @@ def sample_for_opencl(G_data, G_rows, G_cols, G_data_buf, G_rows_buf, G_cols_buf
|
|
|
169
170
|
best_solution = solutions[0]
|
|
170
171
|
best_energy = -float("inf")
|
|
171
172
|
|
|
172
|
-
opencl_args =
|
|
173
|
+
opencl_args = setup_opencl(shots, shots, np.array([n, shots, is_spin_glass, segment_size, theta_segment_size], dtype=np.int32))
|
|
173
174
|
|
|
174
175
|
improved = True
|
|
175
176
|
while improved:
|
|
@@ -178,7 +179,7 @@ def sample_for_opencl(G_data, G_rows, G_cols, G_data_buf, G_rows_buf, G_cols_buf
|
|
|
178
179
|
solution, energy = run_cut_opencl(solutions, G_data_buf, G_rows_buf, G_cols_buf, is_segmented, segment_size, is_spin_glass, *opencl_args)
|
|
179
180
|
if energy > best_energy:
|
|
180
181
|
best_energy = energy
|
|
181
|
-
best_solution = solution
|
|
182
|
+
best_solution = solution.copy()
|
|
182
183
|
improved = True
|
|
183
184
|
|
|
184
185
|
if is_spin_glass:
|
|
@@ -326,10 +327,12 @@ def run_cut_opencl(samples, G_data_buf, G_rows_buf, G_cols_buf, is_segmented, se
|
|
|
326
327
|
cl.enqueue_copy(queue, max_index_host, max_index_buf)
|
|
327
328
|
queue.finish()
|
|
328
329
|
|
|
329
|
-
# Find global
|
|
330
|
-
|
|
330
|
+
# Find global minimum
|
|
331
|
+
best_x = np.argmax(max_energy_host)
|
|
332
|
+
best_i = max_index_host[best_x]
|
|
333
|
+
|
|
334
|
+
return samples[best_i], max_energy_host[best_x]
|
|
331
335
|
|
|
332
|
-
return samples[best_i], max_energy_host[best_i]
|
|
333
336
|
|
|
334
337
|
|
|
335
338
|
def maxcut_tfim_sparse(
|
|
@@ -390,7 +393,6 @@ def maxcut_tfim_sparse(
|
|
|
390
393
|
|
|
391
394
|
n_qubits = G_m.shape[0]
|
|
392
395
|
|
|
393
|
-
wgs = max(32, opencl_context.work_group_size)
|
|
394
396
|
is_opencl = is_maxcut_gpu and IS_OPENCL_AVAILABLE and (n_qubits >= wgs) and (shots >= wgs)
|
|
395
397
|
|
|
396
398
|
if not is_opencl:
|
|
@@ -402,21 +404,18 @@ def maxcut_tfim_sparse(
|
|
|
402
404
|
|
|
403
405
|
return bit_string, best_value, partition
|
|
404
406
|
|
|
405
|
-
segment_size = G_m.data.shape[0]
|
|
406
|
-
|
|
407
|
+
segment_size = (G_m.data.shape[0] + 3) >> 2
|
|
408
|
+
theta_segment_size = (((n_qubits + 31) >> 5) * (((shots + wgs - 1) // wgs) * wgs) + 3) >> 2
|
|
409
|
+
is_segmented = (G_m.data.nbytes << 1) > opencl_context.max_alloc or ((theta_segment_size << 3) > opencl_context.max_alloc)
|
|
407
410
|
|
|
408
411
|
G_data_buf, G_rows_buf, G_cols_buf = make_G_m_csr_buf(G_m, is_segmented, segment_size)
|
|
409
|
-
if is_nested:
|
|
410
|
-
opencl_context.G_data_buf = G_data_buf
|
|
411
|
-
opencl_context.G_rows_buf = G_rows_buf
|
|
412
|
-
opencl_context.G_cols_buf = G_cols_buf
|
|
413
412
|
|
|
414
413
|
hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
|
|
415
414
|
|
|
416
415
|
degrees = None
|
|
417
416
|
J_eff = 1.0 / (1.0 + epsilon - J_eff)
|
|
418
417
|
|
|
419
|
-
best_solution, best_value = sample_for_opencl(G_m.data, G_m.indptr, G_m.indices, G_data_buf, G_rows_buf, G_cols_buf, G_max, shots, hamming_prob, J_eff, repulsion_base, is_spin_glass, is_segmented, segment_size)
|
|
418
|
+
best_solution, best_value = sample_for_opencl(G_m.data, G_m.indptr, G_m.indices, G_data_buf, G_rows_buf, G_cols_buf, G_max, shots, hamming_prob, J_eff, repulsion_base, is_spin_glass, is_segmented, segment_size, theta_segment_size)
|
|
420
419
|
|
|
421
420
|
bit_string, l, r = get_cut(best_solution, nodes)
|
|
422
421
|
|
|
@@ -24,10 +24,6 @@ class OpenCLContext:
|
|
|
24
24
|
self.calculate_cut_sparse_kernel = j
|
|
25
25
|
self.calculate_cut_segmented_kernel = k
|
|
26
26
|
self.calculate_cut_sparse_segmented_kernel = l
|
|
27
|
-
self.G_m_buf = None
|
|
28
|
-
self.G_data_buf = None
|
|
29
|
-
self.G_rows_buf = None
|
|
30
|
-
self.G_cols_buf = None
|
|
31
27
|
|
|
32
28
|
IS_OPENCL_AVAILABLE = True
|
|
33
29
|
ctx = None
|
|
@@ -91,8 +87,6 @@ try:
|
|
|
91
87
|
compute_units = int(os.getenv('PYQRACKISING_MAX_GPU_PROC_ELEM', str(ctx.devices[0].get_info(cl.device_info.MAX_COMPUTE_UNITS))))
|
|
92
88
|
|
|
93
89
|
# Load and build OpenCL kernels
|
|
94
|
-
kernel_src += f"#define MAX_PROC_ELEM {compute_units}\n"
|
|
95
|
-
kernel_src += f"#define TOP_N {os.getenv('PYQRACKISING_GPU_TOP_N', '32')}\n"
|
|
96
90
|
kernel_src += open(os.path.dirname(os.path.abspath(__file__)) + "/kernels.cl").read()
|
|
97
91
|
program = cl.Program(ctx, kernel_src).build()
|
|
98
92
|
bootstrap_kernel = program.bootstrap
|
|
@@ -117,29 +111,29 @@ except ImportError:
|
|
|
117
111
|
opencl_context = OpenCLContext(compute_units, IS_OPENCL_AVAILABLE, work_group_size, dtype, epsilon, max_alloc, ctx, queue, bootstrap_kernel, bootstrap_sparse_kernel, bootstrap_segmented_kernel, bootstrap_sparse_segmented_kernel, calculate_cut_kernel, calculate_cut_sparse_kernel, calculate_cut_segmented_kernel, calculate_cut_sparse_segmented_kernel)
|
|
118
112
|
|
|
119
113
|
|
|
120
|
-
|
|
114
|
+
|
|
115
|
+
def setup_opencl(l, g, args_np):
|
|
121
116
|
ctx = opencl_context.ctx
|
|
122
117
|
queue = opencl_context.queue
|
|
123
118
|
dtype = opencl_context.dtype
|
|
124
119
|
wgs = opencl_context.work_group_size
|
|
125
120
|
|
|
126
|
-
# Args: [n, shots, is_spin_glass, prng_seed, segment_size]
|
|
127
|
-
args_np = np.array([n, shots, is_spin_glass, segment_size], dtype=np.int32)
|
|
128
|
-
|
|
129
121
|
# Buffers
|
|
130
122
|
mf = cl.mem_flags
|
|
131
123
|
args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args_np)
|
|
132
124
|
|
|
133
|
-
#
|
|
134
|
-
local_size = min(wgs,
|
|
125
|
+
# Group sizes
|
|
126
|
+
local_size = min(wgs, l)
|
|
135
127
|
max_global_size = ((opencl_context.MAX_GPU_PROC_ELEM + local_size - 1) // local_size) * local_size # corresponds to MAX_PROC_ELEM macro in OpenCL kernel program
|
|
136
|
-
global_size = min(((
|
|
128
|
+
global_size = min(((g + local_size - 1) // local_size) * local_size, max_global_size)
|
|
129
|
+
|
|
130
|
+
# Local memory allocation (1 float per work item)
|
|
137
131
|
local_energy_buf = cl.LocalMemory(np.dtype(dtype).itemsize * local_size)
|
|
138
132
|
local_index_buf = cl.LocalMemory(np.dtype(np.int32).itemsize * local_size)
|
|
139
133
|
|
|
140
134
|
# Allocate max_energy and max_index result buffers per workgroup
|
|
141
|
-
max_energy_host = np.empty(global_size, dtype=dtype)
|
|
142
|
-
max_index_host = np.empty(global_size, dtype=np.int32)
|
|
135
|
+
max_energy_host = np.empty(global_size // local_size, dtype=dtype)
|
|
136
|
+
max_index_host = np.empty(global_size // local_size, dtype=np.int32)
|
|
143
137
|
|
|
144
138
|
max_energy_buf = cl.Buffer(ctx, mf.WRITE_ONLY, max_energy_host.nbytes)
|
|
145
139
|
max_index_buf = cl.Buffer(ctx, mf.WRITE_ONLY, max_index_host.nbytes)
|
|
@@ -151,8 +145,7 @@ def make_G_m_buf(G_m, is_segmented, segment_size):
|
|
|
151
145
|
mf = cl.mem_flags
|
|
152
146
|
ctx = opencl_context.ctx
|
|
153
147
|
if is_segmented:
|
|
154
|
-
o_shape =
|
|
155
|
-
segment_size = (segment_size + 3) >> 2
|
|
148
|
+
o_shape = G_m.shape[0] * G_m.shape[1]
|
|
156
149
|
n_shape = segment_size << 2
|
|
157
150
|
_G_m = np.reshape(G_m, (o_shape,))
|
|
158
151
|
if n_shape != o_shape:
|
|
@@ -174,8 +167,7 @@ def make_G_m_csr_buf(G_m, is_segmented, segment_size):
|
|
|
174
167
|
G_rows_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m.indptr)
|
|
175
168
|
G_cols_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m.indices)
|
|
176
169
|
if is_segmented:
|
|
177
|
-
o_shape =
|
|
178
|
-
segment_size = (segment_size + 3) >> 2
|
|
170
|
+
o_shape = G_m.data.shape[0]
|
|
179
171
|
n_shape = segment_size << 2
|
|
180
172
|
_G_data = np.reshape(G_m.data, (o_shape,))
|
|
181
173
|
if n_shape != o_shape:
|
|
@@ -213,14 +205,14 @@ def make_theta_buf(theta, is_segmented, shots, n):
|
|
|
213
205
|
@njit(parallel=True)
|
|
214
206
|
def convert_bool_to_uint(samples):
|
|
215
207
|
shots = samples.shape[0]
|
|
216
|
-
|
|
217
|
-
|
|
208
|
+
n = samples.shape[1]
|
|
209
|
+
n32 = (n + 31) >> 5
|
|
210
|
+
theta = np.zeros(shots * n32, dtype=np.uint32)
|
|
218
211
|
for i in prange(shots):
|
|
219
212
|
i_offset = i * n32
|
|
220
|
-
for j in range(
|
|
213
|
+
for j in range(n):
|
|
221
214
|
if samples[i, j]:
|
|
222
|
-
|
|
223
|
-
theta[b_index >> 5] |= 1 << (b_index & 31)
|
|
215
|
+
theta[i_offset + (j >> 5)] |= 1 << (j & 31)
|
|
224
216
|
|
|
225
217
|
return theta
|
|
226
218
|
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
from .maxcut_tfim import maxcut_tfim
|
|
2
|
-
from .maxcut_tfim_util import get_cut_base, make_G_m_buf, opencl_context
|
|
2
|
+
from .maxcut_tfim_util import get_cut_base, make_G_m_buf, opencl_context, setup_opencl
|
|
3
3
|
from .spin_glass_solver_util import get_cut_from_bit_array, int_to_bitstring
|
|
4
4
|
import itertools
|
|
5
|
+
import math
|
|
5
6
|
import networkx as nx
|
|
6
7
|
import numpy as np
|
|
7
8
|
from numba import njit, prange
|
|
@@ -68,41 +69,19 @@ def bootstrap(best_theta, G_m, indices_array, k, min_energy, dtype, is_spin_glas
|
|
|
68
69
|
return min_energy
|
|
69
70
|
|
|
70
71
|
|
|
71
|
-
def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy, is_segmented,
|
|
72
|
+
def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy, is_segmented, local_size, global_size, args_buf, local_energy_buf, local_index_buf, max_energy_host, max_index_host, max_energy_buf, max_index_buf):
|
|
72
73
|
ctx = opencl_context.ctx
|
|
73
74
|
queue = opencl_context.queue
|
|
74
75
|
bootstrap_kernel = opencl_context.bootstrap_segmented_kernel if is_segmented else opencl_context.bootstrap_kernel
|
|
75
76
|
dtype = opencl_context.dtype
|
|
76
77
|
epsilon = opencl_context.epsilon
|
|
77
|
-
wgs = opencl_context.work_group_size
|
|
78
|
-
|
|
79
|
-
n = best_theta.shape[0]
|
|
80
|
-
combo_count = len(indices_array_np) // k
|
|
81
78
|
|
|
82
79
|
best_theta_np = np.array([(1 if b else 0) for b in best_theta], dtype=np.int8)
|
|
83
80
|
|
|
84
|
-
# Args: [n, k, combo_count, is_spin_glass, prng_seed, segment_size]
|
|
85
|
-
args_np = np.array([n, k, combo_count, is_spin_glass, np.random.randint(-(1<<31), (1<<31) - 1), segment_size], dtype=np.int32)
|
|
86
|
-
|
|
87
81
|
# Buffers
|
|
88
82
|
mf = cl.mem_flags
|
|
89
83
|
best_theta_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=best_theta_np)
|
|
90
84
|
indices_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=indices_array_np)
|
|
91
|
-
args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args_np)
|
|
92
|
-
|
|
93
|
-
# Local memory allocation (1 float per work item)
|
|
94
|
-
local_size = min(wgs, n)
|
|
95
|
-
max_global_size = ((opencl_context.MAX_GPU_PROC_ELEM + local_size - 1) // local_size) * local_size # corresponds to MAX_PROC_ELEM macro in OpenCL kernel program
|
|
96
|
-
global_size = min(((combo_count + local_size - 1) // local_size) * local_size, max_global_size)
|
|
97
|
-
local_energy_buf = cl.LocalMemory(np.dtype(dtype).itemsize * local_size)
|
|
98
|
-
local_index_buf = cl.LocalMemory(np.dtype(np.int32).itemsize * local_size)
|
|
99
|
-
|
|
100
|
-
# Allocate min_energy and min_index result buffers per workgroup
|
|
101
|
-
min_energy_host = np.empty(global_size, dtype=dtype)
|
|
102
|
-
min_index_host = np.empty(global_size, dtype=np.int32)
|
|
103
|
-
|
|
104
|
-
min_energy_buf = cl.Buffer(ctx, mf.WRITE_ONLY, min_energy_host.nbytes)
|
|
105
|
-
min_index_buf = cl.Buffer(ctx, mf.WRITE_ONLY, min_index_host.nbytes)
|
|
106
85
|
|
|
107
86
|
# Set kernel args
|
|
108
87
|
if is_segmented:
|
|
@@ -114,8 +93,8 @@ def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy, i
|
|
|
114
93
|
best_theta_buf,
|
|
115
94
|
indices_buf,
|
|
116
95
|
args_buf,
|
|
117
|
-
|
|
118
|
-
|
|
96
|
+
max_energy_buf,
|
|
97
|
+
max_index_buf,
|
|
119
98
|
local_energy_buf,
|
|
120
99
|
local_index_buf
|
|
121
100
|
)
|
|
@@ -125,8 +104,8 @@ def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy, i
|
|
|
125
104
|
best_theta_buf,
|
|
126
105
|
indices_buf,
|
|
127
106
|
args_buf,
|
|
128
|
-
|
|
129
|
-
|
|
107
|
+
max_energy_buf,
|
|
108
|
+
max_index_buf,
|
|
130
109
|
local_energy_buf,
|
|
131
110
|
local_index_buf
|
|
132
111
|
)
|
|
@@ -134,19 +113,16 @@ def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy, i
|
|
|
134
113
|
cl.enqueue_nd_range_kernel(queue, bootstrap_kernel, (global_size,), (local_size,))
|
|
135
114
|
|
|
136
115
|
# Read results
|
|
137
|
-
cl.enqueue_copy(queue,
|
|
138
|
-
cl.enqueue_copy(queue,
|
|
116
|
+
cl.enqueue_copy(queue, max_energy_host, max_energy_buf)
|
|
117
|
+
cl.enqueue_copy(queue, max_index_host, max_index_buf)
|
|
139
118
|
queue.finish()
|
|
140
119
|
|
|
141
120
|
# Find global minimum
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
return min_energy
|
|
121
|
+
best_i = np.argmax(max_energy_host)
|
|
122
|
+
best_energy = -max_energy_host[best_i]
|
|
145
123
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
choices = np.where(np.isclose(min_energy_host, energy, atol=atol, rtol=rtol))[0]
|
|
149
|
-
best_i = np.random.choice(choices) if len(choices) else np.argmin(min_energy_host)
|
|
124
|
+
if min_energy <= best_energy:
|
|
125
|
+
return min_energy
|
|
150
126
|
|
|
151
127
|
flip_index_start = best_i * k
|
|
152
128
|
indices_to_flip = indices_array_np[flip_index_start : flip_index_start + k]
|
|
@@ -154,7 +130,7 @@ def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy, i
|
|
|
154
130
|
for i in indices_to_flip:
|
|
155
131
|
best_theta[i] = not best_theta[i]
|
|
156
132
|
|
|
157
|
-
return
|
|
133
|
+
return best_energy
|
|
158
134
|
|
|
159
135
|
|
|
160
136
|
def log_intermediate(theta, G_m, nodes, min_energy, is_spin_glass):
|
|
@@ -220,15 +196,13 @@ def spin_glass_solver(
|
|
|
220
196
|
bitstring, _, _ = maxcut_tfim(G_m, quality=quality, shots=shots, is_spin_glass=is_spin_glass, anneal_t=anneal_t, anneal_h=anneal_h, repulsion_base=repulsion_base, is_maxcut_gpu=is_combo_maxcut_gpu, is_nested=True)
|
|
221
197
|
best_theta = np.array([b == "1" for b in list(bitstring)], dtype=np.bool_)
|
|
222
198
|
|
|
223
|
-
segment_size = G_m.shape[0]
|
|
199
|
+
segment_size = G_m.shape[0] * G_m.shape[1]
|
|
224
200
|
is_segmented = (G_m.nbytes << 1) > opencl_context.max_alloc
|
|
225
201
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
else:
|
|
231
|
-
G_m_buf = make_G_m_buf(G_m, is_segmented, segment_size)
|
|
202
|
+
is_opencl = is_combo_maxcut_gpu and IS_OPENCL_AVAILABLE
|
|
203
|
+
|
|
204
|
+
if is_opencl:
|
|
205
|
+
G_m_buf = make_G_m_buf(G_m, is_segmented, segment_size)
|
|
232
206
|
|
|
233
207
|
if max_order is None:
|
|
234
208
|
max_order = n_qubits
|
|
@@ -259,8 +233,10 @@ def spin_glass_solver(
|
|
|
259
233
|
else:
|
|
260
234
|
combos = combos_list[k - 1]
|
|
261
235
|
|
|
262
|
-
if
|
|
263
|
-
|
|
236
|
+
if is_opencl:
|
|
237
|
+
combo_count = len(combos) // k
|
|
238
|
+
opencl_args = setup_opencl(n_qubits, combo_count, np.array([n_qubits, k, combo_count, is_spin_glass, segment_size], dtype=np.int32))
|
|
239
|
+
energy = run_bootstrap_opencl(reheat_theta, G_m_buf, combos, k, reheat_min_energy, is_segmented, *opencl_args)
|
|
264
240
|
else:
|
|
265
241
|
energy = bootstrap(reheat_theta, G_m, combos, k, reheat_min_energy, dtype, is_spin_glass)
|
|
266
242
|
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
from .maxcut_tfim_sparse import maxcut_tfim_sparse
|
|
2
|
-
from .maxcut_tfim_util import get_cut_base, opencl_context
|
|
2
|
+
from .maxcut_tfim_util import get_cut_base, make_G_m_csr_buf, opencl_context, setup_opencl
|
|
3
3
|
from .spin_glass_solver_util import get_cut_from_bit_array, int_to_bitstring
|
|
4
4
|
import itertools
|
|
5
|
+
import math
|
|
5
6
|
import networkx as nx
|
|
6
7
|
import numpy as np
|
|
7
8
|
from numba import njit, prange
|
|
@@ -73,41 +74,19 @@ def bootstrap(best_theta, G_data, G_rows, G_cols, indices_array, k, min_energy,
|
|
|
73
74
|
return min_energy
|
|
74
75
|
|
|
75
76
|
|
|
76
|
-
def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices_array_np, k, min_energy, is_segmented,
|
|
77
|
+
def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices_array_np, k, min_energy, is_segmented, local_size, global_size, args_buf, local_energy_buf, local_index_buf, max_energy_host, max_index_host, max_energy_buf, max_index_buf):
|
|
77
78
|
ctx = opencl_context.ctx
|
|
78
79
|
queue = opencl_context.queue
|
|
79
80
|
bootstrap_kernel = opencl_context.bootstrap_sparse_segmented_kernel if is_segmented else opencl_context.bootstrap_sparse_kernel
|
|
80
81
|
dtype = opencl_context.dtype
|
|
81
82
|
epsilon = opencl_context.epsilon
|
|
82
|
-
wgs = opencl_context.work_group_size
|
|
83
|
-
|
|
84
|
-
n = best_theta.shape[0]
|
|
85
|
-
combo_count = len(indices_array_np) // k
|
|
86
83
|
|
|
87
84
|
best_theta_np = np.array([(1 if b else 0) for b in best_theta], dtype=np.int8)
|
|
88
85
|
|
|
89
|
-
# Args: [n, k, combo_count, is_spin_glass, prng_seed, segment_size]
|
|
90
|
-
args_np = np.array([n, k, combo_count, is_spin_glass, np.random.randint(-(1<<31), (1<<31) - 1), segment_size], dtype=np.int32)
|
|
91
|
-
|
|
92
86
|
# Buffers
|
|
93
87
|
mf = cl.mem_flags
|
|
94
88
|
best_theta_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=best_theta_np)
|
|
95
89
|
indices_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=indices_array_np)
|
|
96
|
-
args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args_np)
|
|
97
|
-
|
|
98
|
-
# Local memory allocation (1 float per work item)
|
|
99
|
-
local_size = min(wgs, n)
|
|
100
|
-
max_global_size = ((opencl_context.MAX_GPU_PROC_ELEM + local_size - 1) // local_size) * local_size # corresponds to MAX_PROC_ELEM macro in OpenCL kernel program
|
|
101
|
-
global_size = min(((combo_count + local_size - 1) // local_size) * local_size, max_global_size)
|
|
102
|
-
local_energy_buf = cl.LocalMemory(np.dtype(dtype).itemsize * local_size)
|
|
103
|
-
local_index_buf = cl.LocalMemory(np.dtype(np.int32).itemsize * local_size)
|
|
104
|
-
|
|
105
|
-
# Allocate min_energy and min_index result buffers per workgroup
|
|
106
|
-
min_energy_host = np.empty(global_size, dtype=dtype)
|
|
107
|
-
min_index_host = np.empty(global_size, dtype=np.int32)
|
|
108
|
-
|
|
109
|
-
min_energy_buf = cl.Buffer(ctx, mf.WRITE_ONLY, min_energy_host.nbytes)
|
|
110
|
-
min_index_buf = cl.Buffer(ctx, mf.WRITE_ONLY, min_index_host.nbytes)
|
|
111
90
|
|
|
112
91
|
# Set kernel args
|
|
113
92
|
if is_segmented:
|
|
@@ -121,8 +100,8 @@ def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices
|
|
|
121
100
|
best_theta_buf,
|
|
122
101
|
indices_buf,
|
|
123
102
|
args_buf,
|
|
124
|
-
|
|
125
|
-
|
|
103
|
+
max_energy_buf,
|
|
104
|
+
max_index_buf,
|
|
126
105
|
local_energy_buf,
|
|
127
106
|
local_index_buf
|
|
128
107
|
)
|
|
@@ -134,8 +113,8 @@ def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices
|
|
|
134
113
|
best_theta_buf,
|
|
135
114
|
indices_buf,
|
|
136
115
|
args_buf,
|
|
137
|
-
|
|
138
|
-
|
|
116
|
+
max_energy_buf,
|
|
117
|
+
max_index_buf,
|
|
139
118
|
local_energy_buf,
|
|
140
119
|
local_index_buf
|
|
141
120
|
)
|
|
@@ -143,19 +122,16 @@ def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices
|
|
|
143
122
|
cl.enqueue_nd_range_kernel(queue, bootstrap_kernel, (global_size,), (local_size,))
|
|
144
123
|
|
|
145
124
|
# Read results
|
|
146
|
-
cl.enqueue_copy(queue,
|
|
147
|
-
cl.enqueue_copy(queue,
|
|
125
|
+
cl.enqueue_copy(queue, max_energy_host, max_energy_buf)
|
|
126
|
+
cl.enqueue_copy(queue, max_index_host, max_index_buf)
|
|
148
127
|
queue.finish()
|
|
149
128
|
|
|
150
129
|
# Find global minimum
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
return min_energy
|
|
130
|
+
best_i = np.argmax(max_energy_host)
|
|
131
|
+
best_energy = -max_energy_host[best_i]
|
|
154
132
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
choices = np.where(np.isclose(min_energy_host, energy, atol=atol, rtol=rtol))[0]
|
|
158
|
-
best_i = np.random.choice(choices) if len(choices) else np.argmin(min_energy_host)
|
|
133
|
+
if min_energy < best_energy:
|
|
134
|
+
return min_energy
|
|
159
135
|
|
|
160
136
|
flip_index_start = best_i * k
|
|
161
137
|
indices_to_flip = indices_array_np[flip_index_start : flip_index_start + k]
|
|
@@ -163,7 +139,7 @@ def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices
|
|
|
163
139
|
for i in indices_to_flip:
|
|
164
140
|
best_theta[i] = not best_theta[i]
|
|
165
141
|
|
|
166
|
-
return
|
|
142
|
+
return best_energy
|
|
167
143
|
|
|
168
144
|
|
|
169
145
|
def to_scipy_sparse_upper_triangular(G, nodes, n_nodes, dtype):
|
|
@@ -244,33 +220,10 @@ def spin_glass_solver_sparse(
|
|
|
244
220
|
segment_size = G_m.data.shape[0]
|
|
245
221
|
is_segmented = (G_m.data.nbytes << 1) > opencl_context.max_alloc
|
|
246
222
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
G_cols_buf = opencl_context.G_cols_buf
|
|
252
|
-
is_segmented = isinstance(G_data_buf, list)
|
|
253
|
-
else:
|
|
254
|
-
mf = cl.mem_flags
|
|
255
|
-
ctx = opencl_context.ctx
|
|
256
|
-
G_rows_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m.indptr)
|
|
257
|
-
G_cols_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m.indices)
|
|
258
|
-
if is_segmented:
|
|
259
|
-
o_shape = segment_size
|
|
260
|
-
segment_size = (segment_size + 3) >> 2
|
|
261
|
-
n_shape = segment_size << 2
|
|
262
|
-
_G_data = np.reshape(G_m.data, (o_shape,))
|
|
263
|
-
if n_shape != o_shape:
|
|
264
|
-
np.resize(_G_data, (n_shape,))
|
|
265
|
-
_G_data_segments = np.split(_G_data, 4)
|
|
266
|
-
G_data_buf = [
|
|
267
|
-
cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=seg)
|
|
268
|
-
for seg in _G_data_segments
|
|
269
|
-
]
|
|
270
|
-
_G_data = None
|
|
271
|
-
_G_data_segments = None
|
|
272
|
-
else:
|
|
273
|
-
G_data_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m.data)
|
|
223
|
+
is_opencl = is_combo_maxcut_gpu and IS_OPENCL_AVAILABLE
|
|
224
|
+
|
|
225
|
+
if is_opencl:
|
|
226
|
+
G_data_buf, G_rows_buf, G_cols_buf = make_G_m_csr_buf(G_m, is_segmented, segment_size)
|
|
274
227
|
|
|
275
228
|
if max_order is None:
|
|
276
229
|
max_order = n_qubits
|
|
@@ -301,8 +254,10 @@ def spin_glass_solver_sparse(
|
|
|
301
254
|
else:
|
|
302
255
|
combos = combos_list[k - 1]
|
|
303
256
|
|
|
304
|
-
if
|
|
305
|
-
|
|
257
|
+
if is_opencl:
|
|
258
|
+
combo_count = len(combos) // k
|
|
259
|
+
opencl_args = setup_opencl(n_qubits, combo_count, np.array([n_qubits, k, combo_count, is_spin_glass, segment_size], dtype=np.int32))
|
|
260
|
+
energy = run_bootstrap_opencl(reheat_theta, G_data_buf, G_rows_buf, G_cols_buf, combos, k, reheat_min_energy, is_segmented, *opencl_args)
|
|
306
261
|
else:
|
|
307
262
|
energy = bootstrap(reheat_theta, G_m.data, G_m.indptr, G_m.indices, combos, k, reheat_min_energy, dtype, is_spin_glass)
|
|
308
263
|
|
|
@@ -7,7 +7,7 @@ with open(README_PATH) as readme_file:
|
|
|
7
7
|
|
|
8
8
|
setup(
|
|
9
9
|
name='pyqrackising',
|
|
10
|
-
version='8.14.
|
|
10
|
+
version='8.14.5',
|
|
11
11
|
author='Dan Strano',
|
|
12
12
|
author_email='stranoj@gmail.com',
|
|
13
13
|
description='Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)',
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|