PyQrackIsing 5.4.6__tar.gz → 5.5.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 (29) hide show
  1. {pyqrackising-5.4.6/pyqrackising.egg-info → pyqrackising-5.5.0}/PKG-INFO +1 -1
  2. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/README.md +7 -3
  3. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/pyproject.toml +1 -1
  4. pyqrackising-5.5.0/pyqrackising/kernels.cl +572 -0
  5. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/pyqrackising/maxcut_tfim.py +21 -8
  6. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/pyqrackising/maxcut_tfim_sparse.py +106 -15
  7. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/pyqrackising/maxcut_tfim_streaming.py +3 -3
  8. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/pyqrackising/maxcut_tfim_util.py +17 -3
  9. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/pyqrackising/spin_glass_solver.py +15 -5
  10. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/pyqrackising/spin_glass_solver_sparse.py +18 -7
  11. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/pyqrackising/tsp.py +8 -8
  12. {pyqrackising-5.4.6 → pyqrackising-5.5.0/pyqrackising.egg-info}/PKG-INFO +1 -1
  13. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/setup.py +1 -1
  14. pyqrackising-5.4.6/pyqrackising/kernels.cl +0 -246
  15. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/CMakeLists.txt +0 -0
  16. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/LICENSE.md +0 -0
  17. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/MANIFEST.in +0 -0
  18. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/pyqrackising/__init__.py +0 -0
  19. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/pyqrackising/_pyqrack_ising.cpp +0 -0
  20. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/pyqrackising/generate_tfim_samples.py +0 -0
  21. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/pyqrackising/spin_glass_solver_streaming.py +0 -0
  22. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/pyqrackising/spin_glass_solver_util.py +0 -0
  23. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/pyqrackising/tfim_magnetization.py +0 -0
  24. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/pyqrackising/tfim_square_magnetization.py +0 -0
  25. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/pyqrackising.egg-info/SOURCES.txt +0 -0
  26. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
  27. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/pyqrackising.egg-info/not-zip-safe +0 -0
  28. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/pyqrackising.egg-info/top_level.txt +0 -0
  29. {pyqrackising-5.4.6 → pyqrackising-5.5.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 5.4.6
3
+ Version: 5.5.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
@@ -50,10 +50,10 @@ from pyqrackising import maxcut_tfim
50
50
  import networkx as nx
51
51
 
52
52
  G = nx.petersen_graph()
53
- best_solution_bit_string, best_cut_value, best_node_groups = maxcut_tfim(G, quality=2)
53
+ best_solution_bit_string, best_cut_value, best_node_groups = maxcut_tfim(G, quality=2, is_alt_gpu_sampling=True)
54
54
  ```
55
55
 
56
- We also provide `maxcut_tfim_sparse(G)`, for `scipy` CSR sparse arrays (or `networkx` graphs). The (integer) `quality` setting is optional, with a default value of `3`, but you can turn it up for higher-quality results, or turn it down to save time. (You can also optionally specify the number of measurement `shots` as an argument, if you want specific fine-grained control over resource usage.) If you want to run MAXCUT on a graph with non-uniform edge weights, specify them as the `weight` attribute of each edge, with `networkx`. (If any `weight` attribute is not defined, the solver assumes it's `1.0` for that edge.)
56
+ We also provide `maxcut_tfim_sparse(G)`, for `scipy` CSR sparse arrays (or `networkx` graphs). The (integer) `quality` setting is optional, with a default value of `3`, but you can turn it up for higher-quality results, or turn it down to save time. (You can also optionally specify the number of measurement `shots` as an argument, if you want specific fine-grained control over resource usage.) `is_alt_gpu_sampling` is `False` by default and, if set to `True`, will dispatch almost the entire algorithm on GPU, instead of just the TFIM-heuristic component (although solution quality is often lower). If you want to run MAXCUT on a graph with non-uniform edge weights, specify them as the `weight` attribute of each edge, with `networkx`. (If any `weight` attribute is not defined, the solver assumes it's `1.0` for that edge.)
57
57
 
58
58
  Based on a combination of the TFIM-inspired MAXCUT solver and another technique for finding ground-state energy in quantum chemistry that we call the _"binary Clifford eigensolver,"_ we also provide an (approximate) spin glass ground-state solver:
59
59
  ```py
@@ -73,7 +73,7 @@ def generate_spin_glass_graph(n_nodes=16, degree=3, seed=None):
73
73
 
74
74
 
75
75
  G = generate_spin_glass_graph(n_nodes=64, seed=42)
76
- solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=2, best_guess=None)
76
+ solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=2, best_guess=None, is_alt_gpu_sampling=True)
77
77
  # solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, best_guess=maxcut_tfim(G, quality=6)[0])
78
78
  ```
79
79
  We also provide `spin_glass_solver_sparse(G)`, for `scipy` **upper-triangular** CSR sparse arrays (or `networkx` graphs). The (integer) default `quality` setting is `3`. `best_guess` gives the option to seed the algorithm with a best guess as to the maximal cut (as an integer, binary string, or list of booleans). By default, `spin_glass_solver()` uses `maxcut_tfim(G)` with passed-through `quality` as `best_guess`, which typically works well, but it could be seeded with higher `maxcut_tfim()` `quality` or Goemans-Williamson, for example. This function is designed with a sign convention for weights such that it can immediately be used as a MAXCUT solver itself: you might need to reverse the sign convention on your weights for spin glass graphs, but this is only convention.
@@ -139,6 +139,10 @@ solution_bit_string, cut_value, node_groups, energy = spin_glass_solver_streamin
139
139
  # solution_bit_string, cut_value, node_groups = maxcut_tfim_streaming(G_func, nodes, G_func_args_tuple=args_tuple)
140
140
  ```
141
141
 
142
+ ## Environment Variables
143
+
144
+ We expose an environment variable, "`PYQRACKISING_MAX_GPU_PROC_ELEM`", for when `is_alt_gpu_sampling=True`. 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).
145
+
142
146
  ## About
143
147
  Transverse field Ising model (TFIM) is the basis of most claimed algorithmic "quantum advantage," circa 2025, with the notable exception of Shor's integer factoring algorithm.
144
148
 
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
12
12
 
13
13
  [project]
14
14
  name = "pyqrackising"
15
- version = "5.4.6"
15
+ version = "5.5.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,572 @@
1
+ __kernel void init_theta(
2
+ __constant float* fargs,
3
+ const int n_qubits,
4
+ __constant float* J_eff,
5
+ __constant float* degrees,
6
+ __global float* theta
7
+ ) {
8
+ const int q = get_global_id(0);
9
+ if (q >= n_qubits) {
10
+ return;
11
+ }
12
+
13
+ const float h_mult = fabs(fargs[2]);
14
+
15
+ const float J = J_eff[q];
16
+ const float z = degrees[q];
17
+ const float abs_zJ = fabs(z * J);
18
+
19
+ float val;
20
+ if (abs_zJ < 2e-40f) {
21
+ val = (J > 0.0f) ? M_PI_F : -M_PI_F;
22
+ } else {
23
+ val = asin(fmax(-1.0f, fmin(1.0f, h_mult / (z * J))));
24
+ }
25
+
26
+ theta[q] = val;
27
+ }
28
+
29
+ // By Google Search AI
30
+ inline void AtomicAdd_g_f(volatile __global float *source, const float operand) {
31
+ union {
32
+ unsigned int intVal;
33
+ float floatVal;
34
+ } oldVal, newVal;
35
+
36
+ do {
37
+ oldVal.floatVal = *source;
38
+ newVal.floatVal = oldVal.floatVal + operand;
39
+ // Use atomic_cmpxchg to atomically update the value
40
+ } while (atomic_cmpxchg((volatile __global unsigned int*)source, oldVal.intVal, newVal.intVal) != oldVal.intVal);
41
+ }
42
+
43
+ // By Elara (custom OpenAI GPT)
44
+ inline float probability_by_hamming_weight(
45
+ int q, float J, float h, float z, float theta, float t, int n_qubits
46
+ ) {
47
+ float ratio = fabs(h) / (z * J);
48
+ if (ratio > 1.0f) ratio = 1.0f;
49
+ float theta_c = asin(ratio);
50
+
51
+ float p = pow(2.0f, fabs(J / h) - 1.0f)
52
+ * (1.0f + sin(theta - theta_c) * cos(1.5f * M_PI_F * J * t + theta) / (1.0f + sqrt(t)))
53
+ - 0.5f;
54
+
55
+ if ((p * (n_qubits + 2.0f)) >= 1024.0f) return 0.0f;
56
+
57
+ float numerator = pow(2.0f, (n_qubits + 2.0f) * p) - 1.0f;
58
+ float denominator = pow(2.0f, p) - 1.0f;
59
+ float result = numerator * pow(2.0f, -((n_qubits + 1.0f) * p) - p * q) / denominator;
60
+
61
+ if (isnan(result) || isinf(result)) return 0.0f;
62
+ return result;
63
+ }
64
+
65
+ __kernel void maxcut_hamming_cdf(
66
+ int n_qubits,
67
+ __constant int* degrees,
68
+ __constant float* args,
69
+ __constant float* J_func,
70
+ __constant float* theta,
71
+ __global float* hamming_prob
72
+ ) {
73
+ const float delta_t = args[0U];
74
+ const float tot_t = args[1U];
75
+ const float h_mult = args[2U];
76
+ const int step = get_group_id(0) / n_qubits;
77
+ const int qi = get_group_id(0) % n_qubits;
78
+ const float J_eff = J_func[qi];
79
+ const float z = degrees[qi];
80
+ if (fabs(z * J_eff) <= (pow(2.0f, -52))) return;
81
+
82
+ const float theta_eff = theta[qi];
83
+ const float t = step * delta_t;
84
+ const float tm1 = (step - 1) * delta_t;
85
+ const float h_t = h_mult * (tot_t - t);
86
+
87
+ const int n_threads = get_local_size(0);
88
+
89
+ for (int qo = get_local_id(0); qo < n_qubits; qo += n_threads) {
90
+ int _qo = (J_eff > 0.0f) ? (n_qubits - (1 + qo)) : qo;
91
+ float diff = probability_by_hamming_weight(_qo, J_eff, h_t, z, theta_eff, t, n_qubits);
92
+ diff -= probability_by_hamming_weight(_qo, J_eff, h_t, z, theta_eff, tm1, n_qubits);
93
+ AtomicAdd_g_f(&(hamming_prob[_qo]), diff);
94
+ }
95
+ }
96
+
97
+ float bootstrap_worker(__constant char* theta, __global double* G_m, __constant int* indices, const int k, const int n) {
98
+ double energy = 0.0;
99
+ const size_t n_st = (size_t)n;
100
+ for (int u = 0; u < n; ++u) {
101
+ const size_t u_offset = u * n_st;
102
+ bool u_bit = theta[u];
103
+ for (int x = 0; x < k; ++x) {
104
+ if (indices[x] == u) {
105
+ u_bit = !u_bit;
106
+ break;
107
+ }
108
+ }
109
+ for (int v = u + 1; v < n; ++v) {
110
+ const double val = G_m[u_offset + v];
111
+ bool v_bit = theta[v];
112
+ for (int x = 0; x < k; ++x) {
113
+ if (indices[x] == v) {
114
+ v_bit = !v_bit;
115
+ break;
116
+ }
117
+ }
118
+ energy += (u_bit == v_bit) ? val : -val;
119
+ }
120
+ }
121
+
122
+ return (float)energy;
123
+ }
124
+
125
+ __kernel void bootstrap(
126
+ __global double* G_m,
127
+ __constant char* best_theta,
128
+ __constant int* indices_array,
129
+ __constant int* args, // args[0] = n, args[1] = k
130
+ __global float* min_energy_ptr, // output: per-group min energy
131
+ __global int* min_index_ptr, // output: per-group best index (i)
132
+ __local float* loc_energy, // local memory buffer
133
+ __local int* loc_index // local memory buffer
134
+ ) {
135
+ const int n = args[0];
136
+ const int k = args[1];
137
+ const int combo_count = args[2];
138
+ const int i = get_global_id(0);
139
+
140
+ float energy = INFINITY;
141
+
142
+ if (i < combo_count) {
143
+ const int j = i * k;
144
+ // Compute energy for this combination
145
+ energy = bootstrap_worker(best_theta, G_m, indices_array + j, k, n);
146
+ }
147
+
148
+ const int lt_id = get_local_id(0);
149
+ const int lt_size = get_local_size(0);
150
+
151
+ // Initialize local memory
152
+ loc_energy[lt_id] = energy;
153
+ loc_index[lt_id] = i;
154
+
155
+ // Reduce within workgroup
156
+ for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
157
+ barrier(CLK_LOCAL_MEM_FENCE);
158
+ if ((lt_id < offset) && (loc_energy[lt_id + offset] < loc_energy[lt_id])) {
159
+ loc_energy[lt_id] = loc_energy[lt_id + offset];
160
+ loc_index[lt_id] = loc_index[lt_id + offset];
161
+ }
162
+ }
163
+
164
+ // Write out per-group result
165
+ if (lt_id == 0) {
166
+ min_energy_ptr[get_group_id(0)] = loc_energy[0];
167
+ min_index_ptr[get_group_id(0)] = loc_index[0];
168
+ }
169
+ }
170
+
171
+ float bootstrap_worker_sparse(__constant char* theta, __global double* G_data, __global unsigned* G_rows, __global unsigned* G_cols, __constant int* indices, const int k, const int n) {
172
+ double energy = 0.0;
173
+ for (int u = 0; u < n; ++u) {
174
+ bool u_bit = theta[u];
175
+ for (int x = 0; x < k; ++x) {
176
+ if (indices[x] == u) {
177
+ u_bit = !u_bit;
178
+ break;
179
+ }
180
+ }
181
+ const size_t mCol = G_rows[u + 1];
182
+ for (int col = G_rows[u]; col < mCol; ++col) {
183
+ const int v = G_cols[col];
184
+ const double val = G_data[col];
185
+ bool v_bit = theta[v];
186
+ for (int x = 0; x < k; ++x) {
187
+ if (indices[x] == v) {
188
+ v_bit = !v_bit;
189
+ break;
190
+ }
191
+ }
192
+ energy += (u_bit == v_bit) ? val : -val;
193
+ }
194
+ }
195
+
196
+ return (float)energy;
197
+ }
198
+
199
+ __kernel void bootstrap_sparse(
200
+ __global double* G_data,
201
+ __global unsigned* G_rows,
202
+ __global unsigned* G_cols,
203
+ __constant char* best_theta,
204
+ __constant int* indices_array,
205
+ __constant int* args, // args[0] = n, args[1] = k
206
+ __global float* min_energy_ptr, // output: per-group min energy
207
+ __global int* min_index_ptr, // output: per-group best index (i)
208
+ __local float* loc_energy, // local memory buffer
209
+ __local int* loc_index // local memory buffer
210
+ ) {
211
+ const int n = args[0];
212
+ const int k = args[1];
213
+ const int combo_count = args[2];
214
+ const int i = get_global_id(0);
215
+
216
+ float energy = INFINITY;
217
+
218
+ if (i < combo_count) {
219
+ const int j = i * k;
220
+ // Compute energy for this combination
221
+ energy = bootstrap_worker_sparse(best_theta, G_data, G_rows, G_cols, indices_array + j, k, n);
222
+ }
223
+
224
+ const int lt_id = get_local_id(0);
225
+ const int lt_size = get_local_size(0);
226
+
227
+ // Initialize local memory
228
+ loc_energy[lt_id] = energy;
229
+ loc_index[lt_id] = i;
230
+
231
+ // Reduce within workgroup
232
+ for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
233
+ barrier(CLK_LOCAL_MEM_FENCE);
234
+ if ((lt_id < offset) && (loc_energy[lt_id + offset] < loc_energy[lt_id])) {
235
+ loc_energy[lt_id] = loc_energy[lt_id + offset];
236
+ loc_index[lt_id] = loc_index[lt_id + offset];
237
+ }
238
+ }
239
+
240
+ // Write out per-group result
241
+ if (lt_id == 0) {
242
+ min_energy_ptr[get_group_id(0)] = loc_energy[0];
243
+ min_index_ptr[get_group_id(0)] = loc_index[0];
244
+ }
245
+ }
246
+
247
+ /// ------ Alternative random sampling rejection kernel by Elara (the OpenAI custom GPT) is below ------ ///
248
+
249
+ inline uint xorshift32(uint *state) {
250
+ uint x = *state;
251
+ x ^= x << 13;
252
+ x ^= x >> 17;
253
+ x ^= x << 5;
254
+ *state = x;
255
+ return x;
256
+ }
257
+
258
+ inline float rand_uniform(uint *state) {
259
+ return (float)xorshift32(state) / (float)0xFFFFFFFF;
260
+ }
261
+
262
+ // Compute cut value from bitset solution
263
+ double compute_cut_bitset(__global const double* G_m, const uint* sol_bits, int n) {
264
+ double cut_val = 0.0;
265
+ for (int u = 0; u < n; u++) {
266
+ int u_word = u >> 5; // divide by 32
267
+ int u_bit = u & 31;
268
+ int u_val = (sol_bits[u_word] >> u_bit) & 1;
269
+
270
+ for (int v = u + 1; v < n; v++) {
271
+ int v_word = v >> 5;
272
+ int v_bit = v & 31;
273
+ int v_val = (sol_bits[v_word] >> v_bit) & 1;
274
+
275
+ if (u_val != v_val) {
276
+ cut_val += G_m[u * n + v];
277
+ }
278
+ }
279
+ }
280
+ return cut_val;
281
+ }
282
+
283
+ #define MAX_WORDS 4096
284
+ #define MAX_WORDS_MASK 4095
285
+
286
+ __kernel void sample_for_solution_best_bitset(
287
+ __global const double* G_m,
288
+ __global const float* thresholds,
289
+ const int n,
290
+ const int shots,
291
+ const double max_weight,
292
+ __global float* rng_seeds,
293
+ __global uint* best_solutions, // [num_groups × ceil(n/32)]
294
+ __global float* best_energies, // [num_groups]
295
+ __local float* loc_energy,
296
+ __local int* loc_index
297
+ ) {
298
+
299
+ const int gid_orig = get_global_id(0);
300
+ const int lid = get_local_id(0);
301
+ const int group = get_group_id(0);
302
+ const int lsize = get_local_size(0);
303
+
304
+ const int words = (n + 31) / 32; // how many uint32s per solution
305
+
306
+ uint state = rng_seeds[gid_orig];
307
+
308
+ uint sol_bits[MAX_WORDS];
309
+ for (int w = 0; w < words; w++) sol_bits[w] = 0;
310
+ uint temp_sol[MAX_WORDS];
311
+
312
+ double cut_val = -INFINITY;
313
+ for (int gid = gid_orig; gid < shots; gid += MAX_PROC_ELEM) {
314
+
315
+ // --- 1. Choose Hamming weight
316
+ float mag_prob = rand_uniform(&state);
317
+ int m = 0;
318
+ while (m < n && thresholds[m] < mag_prob) m++;
319
+ m++;
320
+
321
+ // --- 2. Build solution bitset
322
+ for (int w = 0; w < words; w++) temp_sol[w] = 0;
323
+ temp_sol[(gid >> 5) & MAX_WORDS_MASK] |= 1U << (gid & 31);
324
+
325
+ for (int count = 1; count < m; ++count) {
326
+ double highest_weight = -INFINITY;
327
+ int best_bit = -1;
328
+
329
+ for (int i = 0; i < n; i += 32) {
330
+ for (int j = 0; j < 32; ++j) {
331
+ const int u = i + j;
332
+ if (u >= n) {
333
+ break;
334
+ }
335
+ if ((temp_sol[i >> 5] >> j) & 1) {
336
+ continue;
337
+ }
338
+ const int u_offset = u * n;
339
+
340
+ double weight = 1.0;
341
+ for (int k = 0; k < n; k += 32) {
342
+ for (int l = 0; l < 32; ++l) {
343
+ const int v = k + l;
344
+ if (v >= n) {
345
+ break;
346
+ }
347
+ if ((temp_sol[k >> 5] >> l) & 1) {
348
+ weight *= max(2e-7, 1.0 - G_m[u_offset + v] / max_weight);
349
+ }
350
+ }
351
+ }
352
+ if (weight > highest_weight) {
353
+ highest_weight = weight;
354
+ best_bit = u;
355
+ }
356
+ }
357
+ }
358
+
359
+ const int w = best_bit >> 5;
360
+ const int b = best_bit & 31;
361
+ temp_sol[w] |= 1U << b;
362
+ }
363
+
364
+ // --- 3. Compute cut value
365
+ double temp_cut = compute_cut_bitset(G_m, temp_sol, n);
366
+ if (temp_cut > cut_val) {
367
+ cut_val = temp_cut;
368
+ for (int i = 0; i < words; ++i) {
369
+ sol_bits[i] = temp_sol[i];
370
+ }
371
+ }
372
+ }
373
+
374
+ loc_energy[lid] = cut_val;
375
+ loc_index[lid] = gid_orig;
376
+ barrier(CLK_LOCAL_MEM_FENCE);
377
+
378
+ // --- 4. Reduction for best in workgroup
379
+ for (int offset = lsize >> 1; offset > 0; offset >>= 1) {
380
+ barrier(CLK_LOCAL_MEM_FENCE);
381
+ if (lid < offset) {
382
+ if (loc_energy[lid + offset] > loc_energy[lid]) {
383
+ loc_energy[lid] = loc_energy[lid + offset];
384
+ loc_index[lid] = loc_index[lid + offset];
385
+ }
386
+ }
387
+ }
388
+
389
+ // --- 5. Write best per group
390
+ int winner_gid = loc_index[0];
391
+ if (lid == (winner_gid % get_local_size(0))) {
392
+ best_energies[group] = loc_energy[0];
393
+
394
+ // Copy winning bitset solution into best_solutions[group]
395
+ __global uint* dst = best_solutions + group * words;
396
+ for (int w = 0; w < words; w++) {
397
+ dst[w] = sol_bits[w];
398
+ }
399
+ }
400
+ }
401
+
402
+ // Compute cut value from bitset solution
403
+ double compute_cut_bitset_sparse(__global const double* G_data, __global const unsigned* G_rows, __global const unsigned* G_cols, const uint* sol_bits, int n) {
404
+ double cut_val = 0.0;
405
+ for (unsigned u = 0; u < n; u++) {
406
+ int u_word = u >> 5; // divide by 32
407
+ int u_bit = u & 31;
408
+ int u_val = (sol_bits[u_word] >> u_bit) & 1;
409
+
410
+ unsigned max_col = G_rows[u + 1];
411
+ for (unsigned col = G_rows[u]; col < max_col; ++col) {
412
+ const int v = G_cols[col];
413
+ int v_word = v >> 5;
414
+ int v_bit = v & 31;
415
+ int v_val = (sol_bits[v_word] >> v_bit) & 1;
416
+
417
+ if (u_val != v_val) {
418
+ cut_val += G_data[col];
419
+ }
420
+ }
421
+ }
422
+ return cut_val;
423
+ }
424
+
425
+ int binary_search(__global const unsigned* l, const unsigned t, const unsigned len) {
426
+ int left = 0;
427
+ int right = len - 1;
428
+
429
+ while (left <= right) {
430
+ int mid = (left + right) >> 1;
431
+
432
+ if (l[mid] == t) {
433
+ return mid;
434
+ }
435
+
436
+ if (l[mid] < t) {
437
+ left = mid + 1;
438
+ } else {
439
+ right = mid - 1;
440
+ }
441
+ }
442
+
443
+ return len;
444
+ }
445
+
446
+ __kernel void sample_for_solution_best_bitset_sparse(
447
+ __global const double* G_data,
448
+ __global const unsigned* G_rows,
449
+ __global const unsigned* G_cols,
450
+ __global const float* thresholds,
451
+ const int n,
452
+ const int shots,
453
+ const double max_weight,
454
+ __global float* rng_seeds,
455
+ __global uint* best_solutions, // [num_groups × ceil(n/32)]
456
+ __global float* best_energies, // [num_groups]
457
+ __local float* loc_energy,
458
+ __local int* loc_index
459
+ ) {
460
+
461
+ const int gid_orig = get_global_id(0);
462
+ const int lid = get_local_id(0);
463
+ const int group = get_group_id(0);
464
+ const int lsize = get_local_size(0);
465
+
466
+ const int words = (n + 31) / 32; // how many uint32s per solution
467
+
468
+ uint state = rng_seeds[gid_orig];
469
+
470
+ uint sol_bits[MAX_WORDS];
471
+ for (int w = 0; w < words; w++) sol_bits[w] = 0;
472
+ uint temp_sol[MAX_WORDS];
473
+
474
+ double cut_val = -INFINITY;
475
+ for (int gid = gid_orig; gid < shots; gid += MAX_PROC_ELEM) {
476
+
477
+ // --- 1. Choose Hamming weight
478
+ float mag_prob = rand_uniform(&state);
479
+ int m = 0;
480
+ while (m < n && thresholds[m] < mag_prob) m++;
481
+ m++;
482
+
483
+ // --- 2. Build solution bitset
484
+ for (int w = 0; w < words; w++) temp_sol[w] = 0;
485
+ temp_sol[(gid >> 5) & MAX_WORDS_MASK] |= 1U << (gid & 31);
486
+
487
+ for (int count = 1; count < m; ++count) {
488
+ double highest_weight = -INFINITY;
489
+ int best_bit = -1;
490
+
491
+ for (int i = 0; i < n; i += 32) {
492
+ for (int j = 0; j < 32; ++j) {
493
+ const int u = i + j;
494
+ if (u >= n) {
495
+ break;
496
+ }
497
+ if ((temp_sol[i >> 5] >> j) & 1) {
498
+ continue;
499
+ }
500
+
501
+ double weight = 1.0;
502
+
503
+ unsigned max_col = G_rows[u + 1];
504
+ for (int col = G_rows[u]; col < max_col; ++col) {
505
+ const int v = G_cols[col];
506
+
507
+ if ((temp_sol[v >> 5] >> (v & 31)) & 1) {
508
+ weight *= max(2e-7, 1.0 - G_data[col] / max_weight);
509
+ }
510
+ }
511
+
512
+ for (int v = 0; v < u; ++v) {
513
+ if (!((temp_sol[v >> 5] >> (v & 31)) & 1)) {
514
+ continue;
515
+ }
516
+ int start = G_rows[v];
517
+ int end = G_rows[v + 1];
518
+ int j = binary_search(&(G_cols[start]), u, end - start) + start;
519
+ if (j < end) {
520
+ weight *= max(2e-7, 1.0 - G_data[j] / max_weight);
521
+ }
522
+ }
523
+
524
+ if (weight > highest_weight) {
525
+ highest_weight = weight;
526
+ best_bit = u;
527
+ }
528
+ }
529
+ }
530
+
531
+ const int w = best_bit >> 5;
532
+ const int b = best_bit & 31;
533
+ temp_sol[w] |= 1U << b;
534
+ }
535
+
536
+ // --- 3. Compute cut value
537
+ double temp_cut = compute_cut_bitset_sparse(G_data, G_rows, G_cols, temp_sol, n);
538
+ if (temp_cut > cut_val) {
539
+ cut_val = temp_cut;
540
+ for (int i = 0; i < words; ++i) {
541
+ sol_bits[i] = temp_sol[i];
542
+ }
543
+ }
544
+ }
545
+
546
+ loc_energy[lid] = cut_val;
547
+ loc_index[lid] = gid_orig;
548
+ barrier(CLK_LOCAL_MEM_FENCE);
549
+
550
+ // --- 4. Reduction for best in workgroup
551
+ for (int offset = lsize >> 1; offset > 0; offset >>= 1) {
552
+ barrier(CLK_LOCAL_MEM_FENCE);
553
+ if (lid < offset) {
554
+ if (loc_energy[lid + offset] > loc_energy[lid]) {
555
+ loc_energy[lid] = loc_energy[lid + offset];
556
+ loc_index[lid] = loc_index[lid + offset];
557
+ }
558
+ }
559
+ }
560
+
561
+ // --- 5. Write best per group
562
+ int winner_gid = loc_index[0];
563
+ if (lid == (winner_gid % get_local_size(0))) {
564
+ best_energies[group] = loc_energy[0];
565
+
566
+ // Copy winning bitset solution into best_solutions[group]
567
+ __global uint* dst = best_solutions + group * words;
568
+ for (int w = 0; w < words; w++) {
569
+ dst[w] = sol_bits[w];
570
+ }
571
+ }
572
+ }