PyQrackIsing 7.5.2__tar.gz → 8.0.0__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.
Files changed (31) hide show
  1. {pyqrackising-7.5.2/pyqrackising.egg-info → pyqrackising-8.0.0}/PKG-INFO +1 -1
  2. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/pyproject.toml +1 -1
  3. pyqrackising-8.0.0/pyqrackising/kernels.cl +417 -0
  4. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/pyqrackising/maxcut_tfim.py +56 -97
  5. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/pyqrackising/maxcut_tfim_sparse.py +59 -100
  6. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/pyqrackising/maxcut_tfim_streaming.py +59 -94
  7. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/pyqrackising/maxcut_tfim_util.py +2 -14
  8. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/pyqrackising/spin_glass_solver.py +3 -4
  9. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/pyqrackising/spin_glass_solver_sparse.py +3 -4
  10. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/pyqrackising/spin_glass_solver_streaming.py +6 -4
  11. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/pyqrackising/tsp.py +6 -18
  12. {pyqrackising-7.5.2 → pyqrackising-8.0.0/pyqrackising.egg-info}/PKG-INFO +1 -1
  13. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/setup.py +1 -1
  14. pyqrackising-7.5.2/pyqrackising/kernels.cl +0 -946
  15. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/CMakeLists.txt +0 -0
  16. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/LICENSE.md +0 -0
  17. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/MANIFEST.in +0 -0
  18. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/README.md +0 -0
  19. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/pyqrackising/__init__.py +0 -0
  20. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/pyqrackising/_pyqrack_ising.cpp +0 -0
  21. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
  22. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/pyqrackising/generate_tfim_samples.py +0 -0
  23. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/pyqrackising/spin_glass_solver_util.py +0 -0
  24. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/pyqrackising/tfim_magnetization.py +0 -0
  25. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/pyqrackising/tfim_square_magnetization.py +0 -0
  26. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/pyqrackising/tsp_maxcut.py +0 -0
  27. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/pyqrackising.egg-info/SOURCES.txt +0 -0
  28. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
  29. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/pyqrackising.egg-info/not-zip-safe +0 -0
  30. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/pyqrackising.egg-info/top_level.txt +0 -0
  31. {pyqrackising-7.5.2 → pyqrackising-8.0.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 7.5.2
3
+ Version: 8.0.0
4
4
  Summary: Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)
5
5
  Home-page: https://github.com/vm6502q/PyQrackIsing
6
6
  Author: Dan Strano
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
12
12
 
13
13
  [project]
14
14
  name = "pyqrackising"
15
- version = "7.5.2"
15
+ version = "8.0.0"
16
16
  requires-python = ">=3.8"
17
17
  description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
18
18
  readme = {file = "README.txt", content-type = "text/markdown"}
@@ -0,0 +1,417 @@
1
+ #if FP16
2
+ #define fwrapper(f, a) (half)f((float)(a))
3
+ #define fwrapper2(f, a, b) (half)f((float)(a), (float)(b))
4
+ #else
5
+ #define fwrapper(f, a) f(a)
6
+ #define fwrapper2(f, a, b) f(a, b)
7
+ #endif
8
+
9
+ inline uint xorshift32(uint *state) {
10
+ uint x = *state;
11
+ x ^= x << 13;
12
+ x ^= x >> 17;
13
+ x ^= x << 5;
14
+ *state = x;
15
+ return x;
16
+ }
17
+
18
+ real1 bootstrap_worker(__constant char* theta, __global const real1* G_m, __constant int* indices, const int k, const int n) {
19
+ real1 energy = ZERO_R1;
20
+ const size_t n_st = (size_t)n;
21
+ for (int u = 0; u < n; ++u) {
22
+ const size_t u_offset = u * n_st;
23
+ bool u_bit = theta[u];
24
+ for (int x = 0; x < k; ++x) {
25
+ if (indices[x] == u) {
26
+ u_bit = !u_bit;
27
+ break;
28
+ }
29
+ }
30
+ for (int v = u + 1; v < n; ++v) {
31
+ const real1 val = G_m[u_offset + v];
32
+ bool v_bit = theta[v];
33
+ for (int x = 0; x < k; ++x) {
34
+ if (indices[x] == v) {
35
+ v_bit = !v_bit;
36
+ break;
37
+ }
38
+ }
39
+ energy += (u_bit == v_bit) ? val : -val;
40
+ }
41
+ }
42
+
43
+ return energy;
44
+ }
45
+
46
+ __kernel void bootstrap(
47
+ uint prng_seed,
48
+ __global const real1* G_m,
49
+ __constant char* best_theta,
50
+ __constant int* indices_array,
51
+ __constant int* args, // args[0] = n, args[1] = k
52
+ __global real1* min_energy_ptr, // output: per-group min energy
53
+ __global int* min_index_ptr, // output: per-group best index (i)
54
+ __local real1* loc_energy, // local memory buffer
55
+ __local int* loc_index // local memory buffer
56
+ ) {
57
+ const int n = args[0];
58
+ const int k = args[1];
59
+ const int combo_count = args[2];
60
+ int i = get_global_id(0);
61
+
62
+ // The inputs are chaotic, and this doesn't need to be high-quality, just uniform.
63
+ prng_seed ^= (uint)i;
64
+
65
+ real1 best_energy = INFINITY;
66
+ int best_i = i;
67
+
68
+ for (; i < combo_count; i += MAX_PROC_ELEM) {
69
+ const int j = i * k;
70
+ const real1 energy = bootstrap_worker(best_theta, G_m, indices_array + j, k, n);
71
+ if (energy < best_energy) {
72
+ best_energy = energy;
73
+ best_i = i;
74
+ } else if (((energy - best_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
75
+ best_i = i;
76
+ }
77
+ }
78
+
79
+ const int lt_id = get_local_id(0);
80
+ const int lt_size = get_local_size(0);
81
+
82
+ loc_energy[lt_id] = best_energy;
83
+ loc_index[lt_id] = best_i;
84
+
85
+ // Reduce within workgroup
86
+ for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
87
+ barrier(CLK_LOCAL_MEM_FENCE);
88
+ if (lt_id < offset) {
89
+ real1 hid_energy = loc_energy[lt_id + offset];
90
+ real1 lid_energy = loc_energy[lt_id];
91
+ if (hid_energy < lid_energy) {
92
+ loc_energy[lt_id] = hid_energy;
93
+ loc_index[lt_id] = loc_index[lt_id + offset];
94
+ } else if (((hid_energy - lid_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
95
+ loc_index[lt_id] = loc_index[lt_id + offset];
96
+ }
97
+ }
98
+ }
99
+
100
+ // Write out per-group result
101
+ if (lt_id == 0) {
102
+ min_energy_ptr[get_group_id(0)] = loc_energy[0];
103
+ min_index_ptr[get_group_id(0)] = loc_index[0];
104
+ }
105
+ }
106
+
107
+ real1 bootstrap_worker_sparse(__constant char* theta, __global const real1* G_data, __global const uint* G_rows, __global const uint* G_cols, __constant int* indices, const int k, const int n) {
108
+ real1 energy = ZERO_R1;
109
+ for (int u = 0; u < n; ++u) {
110
+ bool u_bit = theta[u];
111
+ for (int x = 0; x < k; ++x) {
112
+ if (indices[x] == u) {
113
+ u_bit = !u_bit;
114
+ break;
115
+ }
116
+ }
117
+ const size_t mCol = G_rows[u + 1];
118
+ for (int col = G_rows[u]; col < mCol; ++col) {
119
+ const int v = G_cols[col];
120
+ const real1 val = G_data[col];
121
+ bool v_bit = theta[v];
122
+ for (int x = 0; x < k; ++x) {
123
+ if (indices[x] == v) {
124
+ v_bit = !v_bit;
125
+ break;
126
+ }
127
+ }
128
+ energy += (u_bit == v_bit) ? val : -val;
129
+ }
130
+ }
131
+
132
+ return energy;
133
+ }
134
+
135
+ __kernel void bootstrap_sparse(
136
+ uint prng_seed,
137
+ __global const real1* G_data,
138
+ __global const uint* G_rows,
139
+ __global const uint* G_cols,
140
+ __constant char* best_theta,
141
+ __constant int* indices_array,
142
+ __constant int* args, // args[0] = n, args[1] = k
143
+ __global real1* min_energy_ptr, // output: per-group min energy
144
+ __global int* min_index_ptr, // output: per-group best index (i)
145
+ __local real1* loc_energy, // local memory buffer
146
+ __local int* loc_index // local memory buffer
147
+ ) {
148
+ const int n = args[0];
149
+ const int k = args[1];
150
+ const int combo_count = args[2];
151
+ int i = get_global_id(0);
152
+
153
+ prng_seed ^= (uint)i;
154
+
155
+ real1 best_energy = INFINITY;
156
+ int best_i = i;
157
+
158
+ for (; i < combo_count; i += MAX_PROC_ELEM) {
159
+ const int j = i * k;
160
+ const real1 energy = bootstrap_worker_sparse(best_theta, G_data, G_rows, G_cols, indices_array + j, k, n);
161
+ if (energy < best_energy) {
162
+ best_energy = energy;
163
+ best_i = i;
164
+ } else if (((energy - best_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
165
+ best_i = i;
166
+ }
167
+ }
168
+
169
+ const int lt_id = get_local_id(0);
170
+ const int lt_size = get_local_size(0);
171
+
172
+ loc_energy[lt_id] = best_energy;
173
+ loc_index[lt_id] = best_i;
174
+
175
+ // Reduce within workgroup
176
+ for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
177
+ barrier(CLK_LOCAL_MEM_FENCE);
178
+ if (lt_id < offset) {
179
+ real1 hid_energy = loc_energy[lt_id + offset];
180
+ real1 lid_energy = loc_energy[lt_id];
181
+ if (hid_energy < lid_energy) {
182
+ loc_energy[lt_id] = hid_energy;
183
+ loc_index[lt_id] = loc_index[lt_id + offset];
184
+ } else if (((hid_energy - lid_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
185
+ loc_index[lt_id] = loc_index[lt_id + offset];
186
+ }
187
+ }
188
+ }
189
+
190
+ // Write out per-group result
191
+ if (lt_id == 0) {
192
+ min_energy_ptr[get_group_id(0)] = loc_energy[0];
193
+ min_index_ptr[get_group_id(0)] = loc_index[0];
194
+ }
195
+ }
196
+
197
+ // Helper to read from segmented G_m
198
+ inline real1 get_G_m(
199
+ __global const real1** G_m,
200
+ size_t flat_idx,
201
+ int segment_size
202
+ ) {
203
+ return G_m[flat_idx / segment_size][flat_idx % segment_size];
204
+ }
205
+
206
+ real1 bootstrap_worker_segmented(
207
+ __constant char* theta,
208
+ __global const real1** G_m,
209
+ __constant int* indices,
210
+ const int k,
211
+ const int n,
212
+ const int segment_size
213
+ ) {
214
+ real1 energy = ZERO_R1;
215
+ const size_t n_st = (size_t)n;
216
+
217
+ for (int u = 0; u < n; ++u) {
218
+ const size_t u_offset = u * n_st;
219
+ bool u_bit = theta[u];
220
+ for (int x = 0; x < k; ++x) {
221
+ if (indices[x] == u) {
222
+ u_bit = !u_bit;
223
+ break;
224
+ }
225
+ }
226
+ for (int v = u + 1; v < n; ++v) {
227
+ size_t flat_idx = u_offset + v;
228
+ real1 val = get_G_m(G_m, flat_idx, segment_size);
229
+
230
+ bool v_bit = theta[v];
231
+ for (int x = 0; x < k; ++x) {
232
+ if (indices[x] == v) {
233
+ v_bit = !v_bit;
234
+ break;
235
+ }
236
+ }
237
+ energy += (u_bit == v_bit) ? val : -val;
238
+ }
239
+ }
240
+
241
+ return energy;
242
+ }
243
+
244
+
245
+ __kernel void bootstrap_segmented(
246
+ uint prng_seed,
247
+ __global const real1* G_m0,
248
+ __global const real1* G_m1,
249
+ __global const real1* G_m2,
250
+ __global const real1* G_m3,
251
+ __constant char* best_theta,
252
+ __constant int* indices_array,
253
+ __constant int* args, // args[0]=n, args[1]=k, args[2]=combo_count, args[3]=segment_size
254
+ __global real1* min_energy_ptr,
255
+ __global int* min_index_ptr,
256
+ __local real1* loc_energy,
257
+ __local int* loc_index
258
+ ) {
259
+ __global const real1* G_m[4] = { G_m0, G_m1, G_m2, G_m3 };
260
+
261
+ const int n = args[0];
262
+ const int k = args[1];
263
+ const int combo_count = args[2];
264
+ const int segment_size = args[3];
265
+ int i = get_global_id(0);
266
+
267
+ prng_seed ^= (uint)i;
268
+
269
+ real1 best_energy = INFINITY;
270
+ int best_i = i;
271
+
272
+ for (; i < combo_count; i += MAX_PROC_ELEM) {
273
+ const int j = i * k;
274
+ const real1 energy = bootstrap_worker_segmented(best_theta, G_m, indices_array + j, k, n, segment_size);
275
+ if (energy < best_energy) {
276
+ best_energy = energy;
277
+ best_i = i;
278
+ } else if (((energy - best_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
279
+ best_i = i;
280
+ }
281
+ }
282
+
283
+ const int lt_id = get_local_id(0);
284
+ const int lt_size = get_local_size(0);
285
+
286
+ loc_energy[lt_id] = best_energy;
287
+ loc_index[lt_id] = best_i;
288
+
289
+ for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
290
+ barrier(CLK_LOCAL_MEM_FENCE);
291
+ if (lt_id < offset) {
292
+ real1 hid_energy = loc_energy[lt_id + offset];
293
+ real1 lid_energy = loc_energy[lt_id];
294
+ if (hid_energy < lid_energy) {
295
+ loc_energy[lt_id] = hid_energy;
296
+ loc_index[lt_id] = loc_index[lt_id + offset];
297
+ } else if (((hid_energy - lid_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
298
+ loc_index[lt_id] = loc_index[lt_id + offset];
299
+ }
300
+ }
301
+ }
302
+
303
+ if (lt_id == 0) {
304
+ min_energy_ptr[get_group_id(0)] = loc_energy[0];
305
+ min_index_ptr[get_group_id(0)] = loc_index[0];
306
+ }
307
+ }
308
+
309
+ real1 bootstrap_worker_sparse_segmented(
310
+ __constant char* theta,
311
+ __global const real1** G_data,
312
+ __global const uint* G_rows,
313
+ __global const uint* G_cols,
314
+ __constant int* indices,
315
+ const int k,
316
+ const int n,
317
+ const int segment_size
318
+ ) {
319
+ real1 energy = ZERO_R1;
320
+
321
+ for (int u = 0; u < n; ++u) {
322
+ bool u_bit = theta[u];
323
+ for (int x = 0; x < k; ++x) {
324
+ if (indices[x] == u) {
325
+ u_bit = !u_bit;
326
+ break;
327
+ }
328
+ }
329
+
330
+ const uint row_start = G_rows[u];
331
+ const uint row_end = G_rows[u + 1];
332
+
333
+ for (uint col = row_start; col < row_end; ++col) {
334
+ const int v = G_cols[col];
335
+ real1 val = get_G_m(G_data, col, segment_size);
336
+
337
+ bool v_bit = theta[v];
338
+ for (int x = 0; x < k; ++x) {
339
+ if (indices[x] == v) {
340
+ v_bit = !v_bit;
341
+ break;
342
+ }
343
+ }
344
+
345
+ energy += (u_bit == v_bit) ? val : -val;
346
+ }
347
+ }
348
+
349
+ return energy;
350
+ }
351
+
352
+ __kernel void bootstrap_sparse_segmented(
353
+ uint prng_seed,
354
+ __global const real1* G_data0,
355
+ __global const real1* G_data1,
356
+ __global const real1* G_data2,
357
+ __global const real1* G_data3,
358
+ __global const uint* G_rows,
359
+ __global const uint* G_cols,
360
+ __constant char* best_theta,
361
+ __constant int* indices_array,
362
+ __constant int* args, // args[0] = n, args[1] = k, args[2] = combo_count, args[3] = segment_size
363
+ __global real1* min_energy_ptr,
364
+ __global int* min_index_ptr,
365
+ __local real1* loc_energy,
366
+ __local int* loc_index
367
+ ) {
368
+ __global const real1* G_data[4] = { G_data0, G_data1, G_data2, G_data3 };
369
+
370
+ const int n = args[0];
371
+ const int k = args[1];
372
+ const int combo_count = args[2];
373
+ const int segment_size = args[3];
374
+ int i = get_global_id(0);
375
+
376
+ prng_seed ^= (uint)i;
377
+
378
+ real1 best_energy = INFINITY;
379
+ int best_i = i;
380
+
381
+ for (; i < combo_count; i += MAX_PROC_ELEM) {
382
+ const int j = i * k;
383
+ const real1 energy = bootstrap_worker_sparse_segmented(best_theta, G_data, G_rows, G_cols, indices_array + j, k, n, segment_size);
384
+ if (energy < best_energy) {
385
+ best_energy = energy;
386
+ best_i = i;
387
+ } else if (((energy - best_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
388
+ best_i = i;
389
+ }
390
+ }
391
+
392
+ const int lt_id = get_local_id(0);
393
+ const int lt_size = get_local_size(0);
394
+
395
+ loc_energy[lt_id] = best_energy;
396
+ loc_index[lt_id] = best_i;
397
+
398
+ for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
399
+ barrier(CLK_LOCAL_MEM_FENCE);
400
+ if (lt_id < offset) {
401
+ real1 hid_energy = loc_energy[lt_id + offset];
402
+ real1 lid_energy = loc_energy[lt_id];
403
+ if (hid_energy < lid_energy) {
404
+ loc_energy[lt_id] = hid_energy;
405
+ loc_index[lt_id] = loc_index[lt_id + offset];
406
+ } else if (((hid_energy - lid_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
407
+ loc_index[lt_id] = loc_index[lt_id + offset];
408
+ }
409
+ }
410
+ }
411
+
412
+ if (lt_id == 0) {
413
+ min_energy_ptr[get_group_id(0)] = loc_energy[0];
414
+ min_index_ptr[get_group_id(0)] = loc_index[0];
415
+ }
416
+ }
417
+
@@ -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 fix_cdf, get_cut, init_thresholds, maxcut_hamming_cdf, opencl_context
7
+ from .maxcut_tfim_util import fix_cdf, get_cut, init_theta, init_thresholds, maxcut_hamming_cdf, opencl_context
8
8
 
9
9
  IS_OPENCL_AVAILABLE = True
10
10
  try:
@@ -13,8 +13,11 @@ except ImportError:
13
13
  IS_OPENCL_AVAILABLE = False
14
14
 
15
15
 
16
+ epsilon = opencl_context.epsilon
17
+
18
+
16
19
  @njit
17
- def update_repulsion_choice(G_m, max_weight, weights, n, used, node):
20
+ def update_repulsion_choice(G_m, max_edge, weights, n, used, node):
18
21
  # Select node
19
22
  used[node] = True
20
23
 
@@ -22,12 +25,12 @@ def update_repulsion_choice(G_m, max_weight, weights, n, used, node):
22
25
  for nbr in range(n):
23
26
  if used[nbr]:
24
27
  continue
25
- weights[nbr] *= max(1.1920928955078125e-7, 1 - G_m[node, nbr] / max_weight)
28
+ weights[nbr] *= max(epsilon, 1 - G_m[node, nbr] / max_edge)
26
29
 
27
30
 
28
31
  # Written by Elara (OpenAI custom GPT) and improved by Dan Strano
29
32
  @njit
30
- def local_repulsion_choice(G_m, max_weight, weights, n, m, shot):
33
+ def local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, n, m):
31
34
  """
32
35
  Pick m nodes out of n with repulsion bias:
33
36
  - High-degree nodes are already less likely
@@ -39,10 +42,23 @@ def local_repulsion_choice(G_m, max_weight, weights, n, m, shot):
39
42
  weights = weights.copy()
40
43
  used = np.zeros(n, dtype=np.bool_) # False = available, True = used
41
44
 
42
- # Update answer and weights
43
- update_repulsion_choice(G_m, max_weight, weights, n, used, shot % n)
45
+ # First bit:
46
+ r = np.random.rand()
47
+ cum = 0.0
48
+ node = 0
49
+ for i in range(n):
50
+ cum += weights[i]
51
+ if (tot_init_weight * r) < cum:
52
+ node = i
53
+ break
54
+
55
+ if m == 1:
56
+ used[node] = True
57
+ return used
44
58
 
45
- for _ in range(1, m):
59
+ update_repulsion_choice(G_m, max_edge, weights, n, used, node)
60
+
61
+ for _ in range(1, m - 1):
46
62
  # Count available
47
63
  total_w = 0.0
48
64
  for i in range(n):
@@ -68,7 +84,33 @@ def local_repulsion_choice(G_m, max_weight, weights, n, m, shot):
68
84
  node += 1
69
85
 
70
86
  # Update answer and weights
71
- update_repulsion_choice(G_m, max_weight, weights, n, used, node)
87
+ update_repulsion_choice(G_m, max_edge, weights, n, used, node)
88
+
89
+ # Count available
90
+ total_w = 0.0
91
+ for i in range(n):
92
+ if used[i]:
93
+ continue
94
+ total_w += weights[i]
95
+
96
+ # Normalize & sample
97
+ r = np.random.rand()
98
+ cum = 0.0
99
+ node = -1
100
+ for i in range(n):
101
+ if used[i]:
102
+ continue
103
+ cum += weights[i]
104
+ if (total_w * r) < cum:
105
+ node = i
106
+ break
107
+
108
+ if node == -1:
109
+ node = 0
110
+ while used[node]:
111
+ node += 1
112
+
113
+ used[node] = True
72
114
 
73
115
  return used
74
116
 
@@ -87,7 +129,8 @@ def compute_energy(sample, G_m, n_qubits):
87
129
  def sample_for_solution(G_m, shots, thresholds, weights, dtype):
88
130
  shots = max(1, shots >> 1)
89
131
  n = len(G_m)
90
- max_weight = G_m.max()
132
+ max_edge = G_m.max()
133
+ tot_init_weight = weights.sum()
91
134
 
92
135
  solutions = np.empty((shots, n), dtype=np.bool_)
93
136
  energies = np.empty(shots, dtype=dtype)
@@ -107,7 +150,7 @@ def sample_for_solution(G_m, shots, thresholds, weights, dtype):
107
150
  m += 1
108
151
 
109
152
  # Second dimension: permutation within Hamming weight
110
- sample = local_repulsion_choice(G_m, max_weight, weights, n, m, s)
153
+ sample = local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, n, m)
111
154
  solutions[s] = sample
112
155
  energies[s] = compute_energy(sample, G_m, n)
113
156
 
@@ -223,7 +266,7 @@ def run_sampling_opencl(G_m_np, thresholds_np, shots, n, is_g_buf_reused):
223
266
 
224
267
 
225
268
  @njit
226
- def cpu_footer(shots, quality, n_qubits, G_m, nodes, dtype, epsilon):
269
+ def cpu_footer(shots, quality, n_qubits, G_m, nodes, dtype):
227
270
  J_eff, degrees = init_J_and_z(G_m, dtype)
228
271
  hamming_prob = init_thresholds(n_qubits, dtype)
229
272
 
@@ -239,27 +282,12 @@ def cpu_footer(shots, quality, n_qubits, G_m, nodes, dtype, epsilon):
239
282
  return bit_string, best_value, (l, r)
240
283
 
241
284
 
242
- @njit
243
- def gpu_footer(shots, n_qubits, G_m, weights, hamming_prob, nodes, dtype):
244
- fix_cdf(hamming_prob)
245
-
246
- best_solution, best_value = sample_for_solution(G_m, shots, hamming_prob, weights, dtype)
247
-
248
- bit_string, l, r = get_cut(best_solution, nodes)
249
-
250
- return bit_string, best_value, (l, r)
251
-
252
-
253
285
  def maxcut_tfim(
254
286
  G,
255
287
  quality=None,
256
- shots=None,
257
- is_alt_gpu_sampling=False,
258
- is_g_buf_reused=False,
259
- is_base_maxcut_gpu=True
288
+ shots=None
260
289
  ):
261
290
  dtype = opencl_context.dtype
262
- epsilon = opencl_context.epsilon
263
291
  wgs = opencl_context.work_group_size
264
292
  nodes = None
265
293
  n_qubits = 0
@@ -302,73 +330,4 @@ def maxcut_tfim(
302
330
  n_steps = n_qubits << quality
303
331
  grid_size = n_steps * n_qubits
304
332
 
305
- if (not is_base_maxcut_gpu) or (not IS_OPENCL_AVAILABLE):
306
- return cpu_footer(shots, quality, n_qubits, G_m, nodes, dtype, epsilon)
307
-
308
- J_eff, degrees = init_J_and_z(G_m, dtype)
309
-
310
- delta_t = 1.0 / n_steps
311
- tot_t = 2.0 * n_steps * delta_t
312
- h_mult = 2.0 / tot_t
313
-
314
- args = np.empty(3, dtype=dtype)
315
- args[0] = delta_t
316
- args[1] = tot_t
317
- args[2] = h_mult
318
-
319
- # Move to GPU
320
- mf = cl.mem_flags
321
- args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
322
- J_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
323
- deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
324
- theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE, size=(n_qubits * dtype().nbytes))
325
-
326
- # Warp size is 32:
327
- group_size = min(wgs, n_qubits)
328
- global_size = ((n_qubits + group_size - 1) // group_size) * group_size
329
-
330
- opencl_context.init_theta_kernel(
331
- opencl_context.queue, (global_size,), (group_size,),
332
- args_buf, np.int32(n_qubits), J_buf, deg_buf, theta_buf
333
- )
334
-
335
- hamming_prob = init_thresholds(n_qubits, dtype)
336
-
337
- # Warp size is 32:
338
- group_size = min(wgs, n_qubits - 1)
339
- grid_dim = n_steps * n_qubits * group_size
340
-
341
- # Move to GPU
342
- ham_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=hamming_prob)
343
-
344
- # Kernel execution
345
- opencl_context.maxcut_hamming_cdf_kernel(
346
- opencl_context.queue, (grid_dim,), (group_size,),
347
- np.int32(n_qubits), deg_buf, args_buf, J_buf, theta_buf, ham_buf
348
- )
349
-
350
- # Fetch results
351
- cl.enqueue_copy(opencl_context.queue, hamming_prob, ham_buf)
352
- opencl_context.queue.finish()
353
-
354
- args_buf.release()
355
- J_buf.release()
356
- deg_buf.release()
357
- theta_buf.release()
358
-
359
- args_buf = None
360
- J_buf = None
361
- deg_buf = None
362
- theta_buf = None
363
-
364
- if not is_alt_gpu_sampling:
365
- degrees = None
366
- J_eff = 1.0 / (1.0 + epsilon - J_eff)
367
-
368
- return gpu_footer(shots, n_qubits, G_m, J_eff, hamming_prob, nodes, dtype)
369
-
370
- fix_cdf(hamming_prob)
371
- best_solution, best_value = run_sampling_opencl(G_m, hamming_prob, shots, n_qubits, is_g_buf_reused)
372
- bit_string, l, r = get_cut(best_solution, nodes)
373
-
374
- return bit_string, best_value, (l, r)
333
+ return cpu_footer(shots, quality, n_qubits, G_m, nodes, dtype)