PyQrackIsing 6.0.0__tar.gz → 6.1.1__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 (28) hide show
  1. {pyqrackising-6.0.0/pyqrackising.egg-info → pyqrackising-6.1.1}/PKG-INFO +1 -1
  2. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/README.md +2 -0
  3. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/pyproject.toml +1 -1
  4. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/pyqrackising/kernels.cl +173 -137
  5. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/pyqrackising/maxcut_tfim.py +24 -22
  6. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/pyqrackising/maxcut_tfim_sparse.py +26 -24
  7. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/pyqrackising/maxcut_tfim_streaming.py +16 -15
  8. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/pyqrackising/maxcut_tfim_util.py +50 -14
  9. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/pyqrackising/spin_glass_solver.py +8 -6
  10. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/pyqrackising/spin_glass_solver_sparse.py +8 -7
  11. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/pyqrackising/spin_glass_solver_streaming.py +5 -3
  12. {pyqrackising-6.0.0 → pyqrackising-6.1.1/pyqrackising.egg-info}/PKG-INFO +1 -1
  13. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/setup.py +1 -1
  14. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/CMakeLists.txt +0 -0
  15. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/LICENSE.md +0 -0
  16. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/MANIFEST.in +0 -0
  17. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/pyqrackising/__init__.py +0 -0
  18. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/pyqrackising/_pyqrack_ising.cpp +0 -0
  19. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/pyqrackising/generate_tfim_samples.py +0 -0
  20. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/pyqrackising/spin_glass_solver_util.py +0 -0
  21. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/pyqrackising/tfim_magnetization.py +0 -0
  22. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/pyqrackising/tfim_square_magnetization.py +0 -0
  23. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/pyqrackising/tsp.py +0 -0
  24. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/pyqrackising.egg-info/SOURCES.txt +0 -0
  25. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/pyqrackising.egg-info/dependency_links.txt +0 -0
  26. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/pyqrackising.egg-info/not-zip-safe +0 -0
  27. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/pyqrackising.egg-info/top_level.txt +0 -0
  28. {pyqrackising-6.0.0 → pyqrackising-6.1.1}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 6.0.0
3
+ Version: 6.1.1
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
@@ -145,6 +145,8 @@ We expose an environment variable, "`PYQRACKISING_MAX_GPU_PROC_ELEM`", for when
145
145
 
146
146
  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.
147
147
 
148
+ 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.
149
+
148
150
  ## About
149
151
  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.
150
152
 
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
12
12
 
13
13
  [project]
14
14
  name = "pyqrackising"
15
- version = "6.0.0"
15
+ version = "6.1.1"
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"}
@@ -1,61 +1,92 @@
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
+ #define INFINITY_R1 INFINITY
8
+ #endif
9
+ #define INFINITY_R1 ((real1)INFINITY)
10
+
1
11
  __kernel void init_theta(
2
- __constant float* fargs,
12
+ __constant real1* fargs,
3
13
  const int n_qubits,
4
- __constant float* J_eff,
5
- __constant unsigned* degrees,
6
- __global float* theta
14
+ __constant real1* J_eff,
15
+ __constant uint* degrees,
16
+ __global real1* theta
7
17
  ) {
8
18
  const int q = get_global_id(0);
9
19
  if (q >= n_qubits) {
10
20
  return;
11
21
  }
12
22
 
13
- const float h_mult = fabs(fargs[2]);
14
- const float J = J_eff[q];
15
- const unsigned z = degrees[q];
16
- const float abs_zJ = fabs(z * J);
23
+ const real1 h_mult = fwrapper(fabs, fargs[2]);
24
+ const real1 J = J_eff[q];
25
+ const uint z = degrees[q];
26
+ const real1 abs_zJ = fwrapper(fabs, z * J);
17
27
 
18
- theta[q] = (abs_zJ <= FLT_EPSILON) ? ((J > 0.0f) ? M_PI_F : -M_PI_F) : (asin(fmax(-1.0f, fmin(1.0f, h_mult / (z * J)))));
28
+ #if FP16
29
+ theta[q] = (abs_zJ <= EPSILON) ? ((J > ZERO_R1) ? M_PI_F : -M_PI_F) : (real1)asin(fmax(-1.0f, fmin(1.0f, (float)(h_mult / (z * J)))));
30
+ #else
31
+ theta[q] = (abs_zJ <= EPSILON) ? ((J > ZERO_R1) ? M_PI_F : -M_PI_F) : asin(fmax(-ONE_R1, fmin(ONE_R1, h_mult / (z * J))));
32
+ #endif
19
33
  }
20
34
 
21
35
  // By Google Search AI
22
- inline void atomic_add_float(__global float* address, float val) {
23
- int old_val_ll;
24
- int new_val_ll;
25
- float old_val_d;
36
+ #if FP16
37
+ inline void atomic_add_real1(__global real1* address, real1 val, bool highWord) {
38
+ union {
39
+ uint intVal;
40
+ real1 floatVal[2];
41
+ } oldVal, newVal;
26
42
 
27
43
  do {
28
- old_val_d = *address;
29
- old_val_ll = as_int(old_val_d); // Reinterpret float as int
30
- new_val_ll = as_int(old_val_d + val); // Perform addition, then reinterpret
44
+ oldVal.floatVal[0] = address[0];
45
+ oldVal.floatVal[1] = address[1];
46
+ if (highWord) {
47
+ newVal.floatVal[0] = oldVal.floatVal[0];
48
+ newVal.floatVal[1] = oldVal.floatVal[1] + val;
49
+ } else {
50
+ newVal.floatVal[0] = oldVal.floatVal[0] + val;
51
+ newVal.floatVal[1] = oldVal.floatVal[1];
52
+ }
53
+ } while (atomic_cmpxchg((__global uint*)address, oldVal.intVal, newVal.intVal) != oldVal.intVal);
54
+ }
55
+ #else
56
+ inline void atomic_add_real1(__global real1* address, real1 val) {
57
+ union {
58
+ qint intVal;
59
+ real1 floatVal;
60
+ } oldVal, newVal;
31
61
 
32
- } while (atom_cmpxchg((__global int*)address, old_val_ll, new_val_ll) != old_val_ll);
62
+ do {
63
+ oldVal.floatVal = *address;
64
+ newVal.floatVal = oldVal.floatVal + val; // Perform addition, then reinterpret
65
+
66
+ } while (atom_cmpxchg((__global qint*)address, oldVal.intVal, newVal.intVal) != oldVal.intVal);
33
67
  }
68
+ #endif
34
69
 
35
70
  // By Elara (custom OpenAI GPT)
36
- inline float probability_by_hamming_weight(
37
- int q, float J, float h, unsigned z, float theta, float t, int n_qubits
71
+ inline real1 probability_by_hamming_weight(
72
+ int q, real1 J, real1 h, uint z, real1 theta, real1 t, int n_qubits
38
73
  ) {
39
- float ratio = fabs(h) / (z * J);
40
- if (ratio > 1.0f) {
41
- ratio = 1.0f;
74
+ real1 ratio = fwrapper(fabs, h) / (z * J);
75
+ if (ratio > ONE_R1) {
76
+ ratio = ONE_R1;
42
77
  }
43
- float theta_c = asin(ratio);
78
+ real1 theta_c = fwrapper(asin, ratio);
44
79
 
45
- float p = pow(2.0f, fabs(J / h) - 1.0f)
46
- * (1.0f + sin(theta - theta_c) * cos(1.5f * M_PI_F * J * t + theta) / (1.0f + sqrt(t)))
80
+ real1 p = fwrapper2(pow, TWO_R1, fwrapper(fabs, J / h) - ONE_R1)
81
+ * (ONE_R1 + fwrapper(sin,theta - theta_c) * fwrapper(cos, 1.5f * M_PI_F * J * t + theta) / (ONE_R1 + fwrapper(sqrt, t)))
47
82
  - 0.5f;
48
83
 
49
- if ((p * (n_qubits + 2.0f)) >= 128.0f) {
50
- return 0.0f;
51
- }
52
-
53
- float numerator = pow(2.0f, (n_qubits + 2.0f) * p) - 1.0f;
54
- float denominator = pow(2.0f, p) - 1.0f;
55
- float result = numerator * pow(2.0f, -((n_qubits + 1.0f) * p) - p * q) / denominator;
84
+ real1 numerator = fwrapper2(pow, TWO_R1, (n_qubits + TWO_R1) * p) - ONE_R1;
85
+ real1 denominator = fwrapper2(pow, TWO_R1, p) - ONE_R1;
86
+ real1 result = numerator * fwrapper2(pow, TWO_R1, -((n_qubits + ONE_R1) * p) - p * q) / denominator;
56
87
 
57
- if (isnan(result) || isinf(result)) {
58
- return 0.0f;
88
+ if (fwrapper(isnan, result) || fwrapper(isinf, result)) {
89
+ return ZERO_R1;
59
90
  }
60
91
 
61
92
  return result;
@@ -63,35 +94,39 @@ inline float probability_by_hamming_weight(
63
94
 
64
95
  __kernel void maxcut_hamming_cdf(
65
96
  int n_qubits,
66
- __constant unsigned* degrees,
67
- __constant float* args,
68
- __constant float* J_func,
69
- __constant float* theta,
70
- __global float* hamming_prob
97
+ __constant uint* degrees,
98
+ __constant real1* args,
99
+ __constant real1* J_func,
100
+ __constant real1* theta,
101
+ __global real1* hamming_prob
71
102
  ) {
72
- const float delta_t = args[0U];
73
- const float tot_t = args[1U];
74
- const float h_mult = args[2U];
103
+ const real1 delta_t = args[0U];
104
+ const real1 tot_t = args[1U];
105
+ const real1 h_mult = args[2U];
75
106
  const int step = get_group_id(0) / n_qubits;
76
107
  const int qi = get_group_id(0) % n_qubits;
77
- const float J_eff = J_func[qi];
78
- const unsigned z = degrees[qi];
79
- if (fabs(z * J_eff) <= FLT_EPSILON) {
108
+ const real1 J_eff = J_func[qi];
109
+ const uint z = degrees[qi];
110
+ if (fwrapper(fabs, z * J_eff) <= EPSILON) {
80
111
  return;
81
112
  }
82
113
 
83
- const float theta_eff = theta[qi];
84
- const float t = step * delta_t;
85
- const float tm1 = (step - 1) * delta_t;
86
- const float h_t = h_mult * (tot_t - t);
114
+ const real1 theta_eff = theta[qi];
115
+ const real1 t = step * delta_t;
116
+ const real1 tm1 = (step - 1) * delta_t;
117
+ const real1 h_t = h_mult * (tot_t - t);
87
118
 
88
119
  const int n_threads = get_local_size(0);
89
120
 
90
121
  for (int qo = get_local_id(0); qo < n_qubits; qo += n_threads) {
91
- int _qo = (J_eff > 0.0) ? (n_qubits - (1 + qo)) : qo;
92
- float diff = probability_by_hamming_weight(_qo, J_eff, h_t, z, theta_eff, t, n_qubits);
122
+ int _qo = (J_eff > ZERO_R1) ? (n_qubits - (1 + qo)) : qo;
123
+ real1 diff = probability_by_hamming_weight(_qo, J_eff, h_t, z, theta_eff, t, n_qubits);
93
124
  diff -= probability_by_hamming_weight(_qo, J_eff, h_t, z, theta_eff, tm1, n_qubits);
94
- atomic_add_float(&(hamming_prob[_qo]), diff);
125
+ #if FP16
126
+ atomic_add_real1(&(hamming_prob[_qo & ~1]), diff, _qo & 1U);
127
+ #else
128
+ atomic_add_real1(&(hamming_prob[_qo]), diff);
129
+ #endif
95
130
  }
96
131
  }
97
132
 
@@ -104,8 +139,8 @@ inline uint xorshift32(uint *state) {
104
139
  return x;
105
140
  }
106
141
 
107
- float bootstrap_worker(__constant char* theta, __global float* G_m, __constant int* indices, const int k, const int n) {
108
- float energy = 0.0;
142
+ real1 bootstrap_worker(__constant char* theta, __global const real1* G_m, __constant int* indices, const int k, const int n) {
143
+ real1 energy = ZERO_R1;
109
144
  const size_t n_st = (size_t)n;
110
145
  for (int u = 0; u < n; ++u) {
111
146
  const size_t u_offset = u * n_st;
@@ -117,7 +152,7 @@ float bootstrap_worker(__constant char* theta, __global float* G_m, __constant i
117
152
  }
118
153
  }
119
154
  for (int v = u + 1; v < n; ++v) {
120
- const float val = G_m[u_offset + v];
155
+ const real1 val = G_m[u_offset + v];
121
156
  bool v_bit = theta[v];
122
157
  for (int x = 0; x < k; ++x) {
123
158
  if (indices[x] == v) {
@@ -134,13 +169,13 @@ float bootstrap_worker(__constant char* theta, __global float* G_m, __constant i
134
169
 
135
170
  __kernel void bootstrap(
136
171
  uint prng_seed,
137
- __global float* G_m,
172
+ __global const real1* G_m,
138
173
  __constant char* best_theta,
139
174
  __constant int* indices_array,
140
175
  __constant int* args, // args[0] = n, args[1] = k
141
- __global float* min_energy_ptr, // output: per-group min energy
176
+ __global real1* min_energy_ptr, // output: per-group min energy
142
177
  __global int* min_index_ptr, // output: per-group best index (i)
143
- __local float* loc_energy, // local memory buffer
178
+ __local real1* loc_energy, // local memory buffer
144
179
  __local int* loc_index // local memory buffer
145
180
  ) {
146
181
  const int n = args[0];
@@ -151,7 +186,7 @@ __kernel void bootstrap(
151
186
  // The inputs are chaotic, and this doesn't need to be high-quality, just uniform.
152
187
  prng_seed ^= (uint)i;
153
188
 
154
- float energy = INFINITY;
189
+ real1 energy = INFINITY_R1;
155
190
 
156
191
  if (i < combo_count) {
157
192
  const int j = i * k;
@@ -170,12 +205,12 @@ __kernel void bootstrap(
170
205
  for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
171
206
  barrier(CLK_LOCAL_MEM_FENCE);
172
207
  if (lt_id < offset) {
173
- float hid_energy = loc_energy[lt_id + offset];
174
- float lid_energy = loc_energy[lt_id];
208
+ real1 hid_energy = loc_energy[lt_id + offset];
209
+ real1 lid_energy = loc_energy[lt_id];
175
210
  if (hid_energy < lid_energy) {
176
211
  loc_energy[lt_id] = hid_energy;
177
212
  loc_index[lt_id] = loc_index[lt_id + offset];
178
- } else if (((hid_energy - lid_energy) <= FLT_EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
213
+ } else if (((hid_energy - lid_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
179
214
  loc_index[lt_id] = loc_index[lt_id + offset];
180
215
  }
181
216
  }
@@ -188,8 +223,8 @@ __kernel void bootstrap(
188
223
  }
189
224
  }
190
225
 
191
- float bootstrap_worker_sparse(__constant char* theta, __global float* G_data, __global unsigned* G_rows, __global unsigned* G_cols, __constant int* indices, const int k, const int n) {
192
- float energy = 0.0f;
226
+ 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) {
227
+ real1 energy = ZERO_R1;
193
228
  for (int u = 0; u < n; ++u) {
194
229
  bool u_bit = theta[u];
195
230
  for (int x = 0; x < k; ++x) {
@@ -201,7 +236,7 @@ float bootstrap_worker_sparse(__constant char* theta, __global float* G_data, __
201
236
  const size_t mCol = G_rows[u + 1];
202
237
  for (int col = G_rows[u]; col < mCol; ++col) {
203
238
  const int v = G_cols[col];
204
- const float val = G_data[col];
239
+ const real1 val = G_data[col];
205
240
  bool v_bit = theta[v];
206
241
  for (int x = 0; x < k; ++x) {
207
242
  if (indices[x] == v) {
@@ -218,15 +253,15 @@ float bootstrap_worker_sparse(__constant char* theta, __global float* G_data, __
218
253
 
219
254
  __kernel void bootstrap_sparse(
220
255
  uint prng_seed,
221
- __global float* G_data,
222
- __global unsigned* G_rows,
223
- __global unsigned* G_cols,
256
+ __global const real1* G_data,
257
+ __global const uint* G_rows,
258
+ __global const uint* G_cols,
224
259
  __constant char* best_theta,
225
260
  __constant int* indices_array,
226
261
  __constant int* args, // args[0] = n, args[1] = k
227
- __global float* min_energy_ptr, // output: per-group min energy
262
+ __global real1* min_energy_ptr, // output: per-group min energy
228
263
  __global int* min_index_ptr, // output: per-group best index (i)
229
- __local float* loc_energy, // local memory buffer
264
+ __local real1* loc_energy, // local memory buffer
230
265
  __local int* loc_index // local memory buffer
231
266
  ) {
232
267
  const int n = args[0];
@@ -236,7 +271,7 @@ __kernel void bootstrap_sparse(
236
271
 
237
272
  prng_seed ^= (uint)i;
238
273
 
239
- float energy = INFINITY;
274
+ real1 energy = INFINITY_R1;
240
275
 
241
276
  if (i < combo_count) {
242
277
  const int j = i * k;
@@ -255,12 +290,12 @@ __kernel void bootstrap_sparse(
255
290
  for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
256
291
  barrier(CLK_LOCAL_MEM_FENCE);
257
292
  if (lt_id < offset) {
258
- float hid_energy = loc_energy[lt_id + offset];
259
- float lid_energy = loc_energy[lt_id];
293
+ real1 hid_energy = loc_energy[lt_id + offset];
294
+ real1 lid_energy = loc_energy[lt_id];
260
295
  if (hid_energy < lid_energy) {
261
296
  loc_energy[lt_id] = hid_energy;
262
297
  loc_index[lt_id] = loc_index[lt_id + offset];
263
- } else if (((hid_energy - lid_energy) <= FLT_EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
298
+ } else if (((hid_energy - lid_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
264
299
  loc_index[lt_id] = loc_index[lt_id + offset];
265
300
  }
266
301
  }
@@ -275,13 +310,13 @@ __kernel void bootstrap_sparse(
275
310
 
276
311
  /// ------ Alternative random sampling rejection kernel by Elara (the OpenAI custom GPT) is below ------ ///
277
312
 
278
- inline float rand_uniform(uint *state) {
279
- return (float)xorshift32(state) / (float)0xFFFFFFFF;
313
+ inline real1 rand_uniform(uint *state) {
314
+ return (real1)((double)xorshift32(state) / (double)0xFFFFFFFF);
280
315
  }
281
316
 
282
317
  // Compute cut value from bitset solution
283
- float compute_cut_bitset(__global const float* G_m, const uint* sol_bits, int n) {
284
- float cut_val = 0.0f;
318
+ real1 compute_cut_bitset(__global const real1* G_m, const uint* sol_bits, int n) {
319
+ real1 cut_val = ZERO_R1;
285
320
  for (int u = 0; u < n; u++) {
286
321
  int u_word = u >> 5; // divide by 32
287
322
  int u_bit = u & 31;
@@ -301,19 +336,20 @@ float compute_cut_bitset(__global const float* G_m, const uint* sol_bits, int n)
301
336
  return cut_val;
302
337
  }
303
338
 
339
+ #define MIN_WEIGHT ((real1)FLT_EPSILON)
304
340
  #define MAX_WORDS 4096
305
341
  #define MAX_WORDS_MASK 4095
306
342
 
307
343
  __kernel void sample_for_solution_best_bitset(
308
- __global const float* G_m,
309
- __global const float* thresholds,
344
+ __global const real1* G_m,
345
+ __constant real1* thresholds,
310
346
  const int n,
311
347
  const int shots,
312
- const float max_weight,
313
- __global float* rng_seeds,
348
+ const real1 max_weight,
349
+ __global real1* rng_seeds,
314
350
  __global uint* best_solutions, // [num_groups × ceil(n/32)]
315
- __global float* best_energies, // [num_groups]
316
- __local float* loc_energy,
351
+ __global real1* best_energies, // [num_groups]
352
+ __local real1* loc_energy,
317
353
  __local int* loc_index
318
354
  ) {
319
355
 
@@ -330,11 +366,11 @@ __kernel void sample_for_solution_best_bitset(
330
366
  for (int w = 0; w < words; w++) sol_bits[w] = 0;
331
367
  uint temp_sol[MAX_WORDS];
332
368
 
333
- float cut_val = -INFINITY;
369
+ real1 cut_val = -INFINITY_R1;
334
370
  for (int gid = gid_orig; gid < shots; gid += MAX_PROC_ELEM) {
335
371
 
336
372
  // --- 1. Choose Hamming weight
337
- float mag_prob = rand_uniform(&state);
373
+ real1 mag_prob = rand_uniform(&state);
338
374
  int m = 0;
339
375
  while (m < n && thresholds[m] < mag_prob) m++;
340
376
  m++;
@@ -344,10 +380,10 @@ __kernel void sample_for_solution_best_bitset(
344
380
  temp_sol[(gid >> 5) & MAX_WORDS_MASK] |= 1U << (gid & 31);
345
381
 
346
382
  for (int count = 1; count < m; ++count) {
347
- float highest_weights[TOP_N];
383
+ real1 highest_weights[TOP_N];
348
384
  int best_bits[TOP_N];
349
385
  for (int x = 0; x < TOP_N; ++x) {
350
- highest_weights[x] = -INFINITY;
386
+ highest_weights[x] = -INFINITY_R1;
351
387
  best_bits[x] = -1;
352
388
  }
353
389
 
@@ -362,7 +398,7 @@ __kernel void sample_for_solution_best_bitset(
362
398
  }
363
399
  const int u_offset = u * n;
364
400
 
365
- float weight = 1.0f;
401
+ real1 weight = ONE_R1;
366
402
  for (int k = 0; k < n; k += 32) {
367
403
  for (int l = 0; l < 32; ++l) {
368
404
  const int v = k + l;
@@ -370,20 +406,20 @@ __kernel void sample_for_solution_best_bitset(
370
406
  break;
371
407
  }
372
408
  if ((temp_sol[k >> 5] >> l) & 1) {
373
- weight *= max(FLT_EPSILON, 1.0f - G_m[u_offset + v] / max_weight);
409
+ weight *= fwrapper2(max, MIN_WEIGHT, ONE_R1 - G_m[u_offset + v] / max_weight);
374
410
  }
375
411
  }
376
412
  }
377
413
 
378
414
  int lowest_option = 0;
379
- float lowest_weight = highest_weights[0];
380
- if (lowest_weight != -INFINITY) {
381
- for (int x = 0; x < TOP_N; ++x) {
382
- float val = highest_weights[x];
415
+ real1 lowest_weight = highest_weights[0];
416
+ if (-lowest_weight != INFINITY_R1) {
417
+ for (int x = 1; x < TOP_N; ++x) {
418
+ real1 val = highest_weights[x];
383
419
  if (val < lowest_weight) {
384
420
  lowest_option = x;
385
421
  lowest_weight = highest_weights[x];
386
- if (val == -INFINITY) {
422
+ if (-val == INFINITY_R1) {
387
423
  break;
388
424
  }
389
425
  }
@@ -397,21 +433,21 @@ __kernel void sample_for_solution_best_bitset(
397
433
  }
398
434
  }
399
435
 
400
- float total_weight = 0.0f;
436
+ real1 total_weight = ZERO_R1;
401
437
  for (int x = 0; x < TOP_N; ++x) {
402
- const float val = highest_weights[x];
403
- if (val == -INFINITY) {
438
+ const real1 val = highest_weights[x];
439
+ if (-val == INFINITY_R1) {
404
440
  continue;
405
441
  }
406
442
  total_weight += val;
407
443
  }
408
444
 
409
- const float bit_prob = rand_uniform(&state);
410
- float tot_prob = 0.0f;
445
+ const real1 bit_prob = rand_uniform(&state);
446
+ real1 tot_prob = ZERO_R1;
411
447
  int best_bit = 0;
412
448
  for (int x = 0; x < TOP_N; ++x) {
413
- const float val = highest_weights[x];
414
- if (val == -INFINITY) {
449
+ const real1 val = highest_weights[x];
450
+ if (-val == INFINITY_R1) {
415
451
  continue;
416
452
  }
417
453
  tot_prob += val;
@@ -427,7 +463,7 @@ __kernel void sample_for_solution_best_bitset(
427
463
  }
428
464
 
429
465
  // --- 3. Compute cut value
430
- float temp_cut = compute_cut_bitset(G_m, temp_sol, n);
466
+ real1 temp_cut = compute_cut_bitset(G_m, temp_sol, n);
431
467
  if (temp_cut > cut_val) {
432
468
  cut_val = temp_cut;
433
469
  for (int i = 0; i < words; ++i) {
@@ -465,15 +501,15 @@ __kernel void sample_for_solution_best_bitset(
465
501
  }
466
502
 
467
503
  // Compute cut value from bitset solution
468
- float compute_cut_bitset_sparse(__global const float* G_data, __global const unsigned* G_rows, __global const unsigned* G_cols, const uint* sol_bits, int n) {
469
- float cut_val = 0.0f;
470
- for (unsigned u = 0; u < n; u++) {
504
+ real1 compute_cut_bitset_sparse(__global const real1* G_data, __global const uint* G_rows, __global const uint* G_cols, const uint* sol_bits, int n) {
505
+ real1 cut_val = ZERO_R1;
506
+ for (uint u = 0; u < n; u++) {
471
507
  int u_word = u >> 5; // divide by 32
472
508
  int u_bit = u & 31;
473
509
  int u_val = (sol_bits[u_word] >> u_bit) & 1;
474
510
 
475
- unsigned max_col = G_rows[u + 1];
476
- for (unsigned col = G_rows[u]; col < max_col; ++col) {
511
+ uint max_col = G_rows[u + 1];
512
+ for (uint col = G_rows[u]; col < max_col; ++col) {
477
513
  const int v = G_cols[col];
478
514
  int v_word = v >> 5;
479
515
  int v_bit = v & 31;
@@ -488,7 +524,7 @@ float compute_cut_bitset_sparse(__global const float* G_data, __global const uns
488
524
  return cut_val;
489
525
  }
490
526
 
491
- int binary_search(__global const unsigned* l, const unsigned t, const unsigned len) {
527
+ int binary_search(__global const uint* l, const uint t, const uint len) {
492
528
  int left = 0;
493
529
  int right = len - 1;
494
530
 
@@ -510,17 +546,17 @@ int binary_search(__global const unsigned* l, const unsigned t, const unsigned l
510
546
  }
511
547
 
512
548
  __kernel void sample_for_solution_best_bitset_sparse(
513
- __global const float* G_data,
514
- __global const unsigned* G_rows,
515
- __global const unsigned* G_cols,
516
- __global const float* thresholds,
549
+ __global const real1* G_data,
550
+ __global const uint* G_rows,
551
+ __global const uint* G_cols,
552
+ __constant real1* thresholds,
517
553
  const int n,
518
554
  const int shots,
519
- const float max_weight,
520
- __global float* rng_seeds,
555
+ const real1 max_weight,
556
+ __global real1* rng_seeds,
521
557
  __global uint* best_solutions, // [num_groups × ceil(n/32)]
522
- __global float* best_energies, // [num_groups]
523
- __local float* loc_energy,
558
+ __global real1* best_energies, // [num_groups]
559
+ __local real1* loc_energy,
524
560
  __local int* loc_index
525
561
  ) {
526
562
 
@@ -537,11 +573,11 @@ __kernel void sample_for_solution_best_bitset_sparse(
537
573
  for (int w = 0; w < words; w++) sol_bits[w] = 0;
538
574
  uint temp_sol[MAX_WORDS];
539
575
 
540
- float cut_val = -INFINITY;
576
+ real1 cut_val = -INFINITY_R1;
541
577
  for (int gid = gid_orig; gid < shots; gid += MAX_PROC_ELEM) {
542
578
 
543
579
  // --- 1. Choose Hamming weight
544
- const float mag_prob = rand_uniform(&state);
580
+ const real1 mag_prob = rand_uniform(&state);
545
581
  int m = 0;
546
582
  while (m < n && thresholds[m] < mag_prob) m++;
547
583
  m++;
@@ -551,10 +587,10 @@ __kernel void sample_for_solution_best_bitset_sparse(
551
587
  temp_sol[(gid >> 5) & MAX_WORDS_MASK] |= 1U << (gid & 31);
552
588
 
553
589
  for (int count = 1; count < m; ++count) {
554
- float highest_weights[TOP_N];
590
+ real1 highest_weights[TOP_N];
555
591
  int best_bits[TOP_N];
556
592
  for (int x = 0; x < TOP_N; ++x) {
557
- highest_weights[x] = -INFINITY;
593
+ highest_weights[x] = -INFINITY_R1;
558
594
  best_bits[x] = -1;
559
595
  }
560
596
 
@@ -568,14 +604,14 @@ __kernel void sample_for_solution_best_bitset_sparse(
568
604
  continue;
569
605
  }
570
606
 
571
- float weight = 1.0f;
607
+ real1 weight = ONE_R1;
572
608
 
573
- unsigned max_col = G_rows[u + 1];
609
+ uint max_col = G_rows[u + 1];
574
610
  for (int col = G_rows[u]; col < max_col; ++col) {
575
611
  const int v = G_cols[col];
576
612
 
577
613
  if ((temp_sol[v >> 5] >> (v & 31)) & 1) {
578
- weight *= max(FLT_EPSILON, 1.0f - G_data[col] / max_weight);
614
+ weight *= fwrapper2(max, MIN_WEIGHT, ONE_R1 - G_data[col] / max_weight);
579
615
  }
580
616
  }
581
617
 
@@ -587,19 +623,19 @@ __kernel void sample_for_solution_best_bitset_sparse(
587
623
  int end = G_rows[v + 1];
588
624
  int j = binary_search(&(G_cols[start]), u, end - start) + start;
589
625
  if (j < end) {
590
- weight *= max(FLT_EPSILON, 1.0f - G_data[j] / max_weight);
626
+ weight *= fwrapper2(max, MIN_WEIGHT, ONE_R1 - G_data[j] / max_weight);
591
627
  }
592
628
  }
593
629
 
594
630
  int lowest_option = 0;
595
- float lowest_weight = highest_weights[0];
596
- if (lowest_weight != -INFINITY) {
631
+ real1 lowest_weight = highest_weights[0];
632
+ if (-lowest_weight != INFINITY_R1) {
597
633
  for (int x = 0; x < TOP_N; ++x) {
598
- float val = highest_weights[x];
634
+ real1 val = highest_weights[x];
599
635
  if (val < lowest_weight) {
600
636
  lowest_option = x;
601
637
  lowest_weight = highest_weights[x];
602
- if (val == -INFINITY) {
638
+ if (-val == INFINITY_R1) {
603
639
  break;
604
640
  }
605
641
  }
@@ -613,21 +649,21 @@ __kernel void sample_for_solution_best_bitset_sparse(
613
649
  }
614
650
  }
615
651
 
616
- float total_weight = 0.0f;
652
+ real1 total_weight = ZERO_R1;
617
653
  for (int x = 0; x < TOP_N; ++x) {
618
- const float val = highest_weights[x];
619
- if (val == -INFINITY) {
654
+ const real1 val = highest_weights[x];
655
+ if (-val == INFINITY_R1) {
620
656
  continue;
621
657
  }
622
658
  total_weight += val;
623
659
  }
624
660
 
625
- const float bit_prob = rand_uniform(&state);
626
- float tot_prob = 0.0f;
661
+ const real1 bit_prob = rand_uniform(&state);
662
+ real1 tot_prob = ZERO_R1;
627
663
  int best_bit = 0;
628
664
  for (int x = 0; x < TOP_N; ++x) {
629
- const float val = highest_weights[x];
630
- if (val == -INFINITY) {
665
+ const real1 val = highest_weights[x];
666
+ if (-val == INFINITY_R1) {
631
667
  continue;
632
668
  }
633
669
  tot_prob += val;
@@ -643,7 +679,7 @@ __kernel void sample_for_solution_best_bitset_sparse(
643
679
  }
644
680
 
645
681
  // --- 3. Compute cut value
646
- float temp_cut = compute_cut_bitset_sparse(G_data, G_rows, G_cols, temp_sol, n);
682
+ real1 temp_cut = compute_cut_bitset_sparse(G_data, G_rows, G_cols, temp_sol, n);
647
683
  if (temp_cut > cut_val) {
648
684
  cut_val = temp_cut;
649
685
  for (int i = 0; i < words; ++i) {