PyQrackIsing 5.7.3__tar.gz → 6.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 (28) hide show
  1. {pyqrackising-5.7.3/pyqrackising.egg-info → pyqrackising-6.0.0}/PKG-INFO +1 -1
  2. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/README.md +2 -2
  3. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/pyproject.toml +1 -1
  4. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/pyqrackising/kernels.cl +75 -74
  5. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/pyqrackising/maxcut_tfim.py +10 -14
  6. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/pyqrackising/maxcut_tfim_sparse.py +10 -14
  7. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/pyqrackising/maxcut_tfim_streaming.py +9 -13
  8. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/pyqrackising/spin_glass_solver.py +3 -4
  9. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/pyqrackising/spin_glass_solver_sparse.py +3 -4
  10. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/pyqrackising/spin_glass_solver_streaming.py +1 -1
  11. {pyqrackising-5.7.3 → pyqrackising-6.0.0/pyqrackising.egg-info}/PKG-INFO +1 -1
  12. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/setup.py +1 -1
  13. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/CMakeLists.txt +0 -0
  14. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/LICENSE.md +0 -0
  15. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/MANIFEST.in +0 -0
  16. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/pyqrackising/__init__.py +0 -0
  17. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/pyqrackising/_pyqrack_ising.cpp +0 -0
  18. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/pyqrackising/generate_tfim_samples.py +0 -0
  19. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/pyqrackising/maxcut_tfim_util.py +0 -0
  20. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/pyqrackising/spin_glass_solver_util.py +0 -0
  21. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/pyqrackising/tfim_magnetization.py +0 -0
  22. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/pyqrackising/tfim_square_magnetization.py +0 -0
  23. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/pyqrackising/tsp.py +0 -0
  24. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/pyqrackising.egg-info/SOURCES.txt +0 -0
  25. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
  26. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/pyqrackising.egg-info/not-zip-safe +0 -0
  27. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/pyqrackising.egg-info/top_level.txt +0 -0
  28. {pyqrackising-5.7.3 → pyqrackising-6.0.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 5.7.3
3
+ Version: 6.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
@@ -44,7 +44,7 @@ samples = generate_tfim_samples(
44
44
 
45
45
  There are two other functions, `tfim_magnetization()` and `tfim_square_magnetization()`, that follow the same function signature except without the `shots` argument.
46
46
 
47
- The library also provides a TFIM-inspired (approximate) MAXCUT solver:
47
+ The library also provides a TFIM-inspired (approximate) MAXCUT solver (which accepts a `networkx` graph or a 32-bit adjacency matrix):
48
48
  ```py
49
49
  from pyqrackising import maxcut_tfim
50
50
  import networkx as nx
@@ -76,7 +76,7 @@ G = generate_spin_glass_graph(n_nodes=64, seed=42)
76
76
  solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=2, best_guess=None, is_base_maxcut_gpu=True, is_alt_gpu_sampling=True, is_combo_maxcut_gpu=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
- We also provide `spin_glass_solver_sparse(G)`, for `scipy` **upper-triangular** CSR sparse arrays (or `networkx` graphs). The (integer) default `quality` setting is `2`. `is_combo_maxcut_gpu` controls whether gradient descent optimization is done on GPU (which is the most costly GPU-based feature), while `is_base_maxcut_gpu` is passed through the underlying `maxcut_tfim(G)` solver. `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.
79
+ We also provide `spin_glass_solver_sparse(G)`, for `scipy` (32-bit) **upper-triangular** CSR sparse arrays (or `networkx` graphs). The (integer) default `quality` setting is `2`. `is_combo_maxcut_gpu` controls whether gradient descent optimization is done on GPU (which is the most costly GPU-based feature), while `is_base_maxcut_gpu` is passed through the underlying `maxcut_tfim(G)` solver. `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.
80
80
 
81
81
  From the `spin_glass_solver()`, we provide a (recursive) Traveling Salesman Problem (TSP) solver:
82
82
  ```py
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
12
12
 
13
13
  [project]
14
14
  name = "pyqrackising"
15
- version = "5.7.3"
15
+ version = "6.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"}
@@ -2,7 +2,7 @@ __kernel void init_theta(
2
2
  __constant float* fargs,
3
3
  const int n_qubits,
4
4
  __constant float* J_eff,
5
- __constant float* degrees,
5
+ __constant unsigned* degrees,
6
6
  __global float* theta
7
7
  ) {
8
8
  const int q = get_global_id(0);
@@ -11,60 +11,59 @@ __kernel void init_theta(
11
11
  }
12
12
 
13
13
  const float h_mult = fabs(fargs[2]);
14
-
15
14
  const float J = J_eff[q];
16
- const float z = degrees[q];
15
+ const unsigned z = degrees[q];
17
16
  const float abs_zJ = fabs(z * J);
18
17
 
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;
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)))));
27
19
  }
28
20
 
29
21
  // 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;
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;
35
26
 
36
27
  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);
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
31
+
32
+ } while (atom_cmpxchg((__global int*)address, old_val_ll, new_val_ll) != old_val_ll);
41
33
  }
42
34
 
43
35
  // By Elara (custom OpenAI GPT)
44
36
  inline float probability_by_hamming_weight(
45
- int q, float J, float h, float z, float theta, float t, int n_qubits
37
+ int q, float J, float h, unsigned z, float theta, float t, int n_qubits
46
38
  ) {
47
39
  float ratio = fabs(h) / (z * J);
48
- if (ratio > 1.0f) ratio = 1.0f;
40
+ if (ratio > 1.0f) {
41
+ ratio = 1.0f;
42
+ }
49
43
  float theta_c = asin(ratio);
50
44
 
51
45
  float p = pow(2.0f, fabs(J / h) - 1.0f)
52
46
  * (1.0f + sin(theta - theta_c) * cos(1.5f * M_PI_F * J * t + theta) / (1.0f + sqrt(t)))
53
47
  - 0.5f;
54
48
 
55
- if ((p * (n_qubits + 2.0f)) >= 1024.0f) return 0.0f;
49
+ if ((p * (n_qubits + 2.0f)) >= 128.0f) {
50
+ return 0.0f;
51
+ }
56
52
 
57
53
  float numerator = pow(2.0f, (n_qubits + 2.0f) * p) - 1.0f;
58
54
  float denominator = pow(2.0f, p) - 1.0f;
59
55
  float result = numerator * pow(2.0f, -((n_qubits + 1.0f) * p) - p * q) / denominator;
60
56
 
61
- if (isnan(result) || isinf(result)) return 0.0f;
57
+ if (isnan(result) || isinf(result)) {
58
+ return 0.0f;
59
+ }
60
+
62
61
  return result;
63
62
  }
64
63
 
65
64
  __kernel void maxcut_hamming_cdf(
66
65
  int n_qubits,
67
- __constant int* degrees,
66
+ __constant unsigned* degrees,
68
67
  __constant float* args,
69
68
  __constant float* J_func,
70
69
  __constant float* theta,
@@ -76,8 +75,10 @@ __kernel void maxcut_hamming_cdf(
76
75
  const int step = get_group_id(0) / n_qubits;
77
76
  const int qi = get_group_id(0) % n_qubits;
78
77
  const float J_eff = J_func[qi];
79
- const float z = degrees[qi];
80
- if (fabs(z * J_eff) <= (pow(2.0f, -52))) return;
78
+ const unsigned z = degrees[qi];
79
+ if (fabs(z * J_eff) <= FLT_EPSILON) {
80
+ return;
81
+ }
81
82
 
82
83
  const float theta_eff = theta[qi];
83
84
  const float t = step * delta_t;
@@ -87,10 +88,10 @@ __kernel void maxcut_hamming_cdf(
87
88
  const int n_threads = get_local_size(0);
88
89
 
89
90
  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
+ int _qo = (J_eff > 0.0) ? (n_qubits - (1 + qo)) : qo;
91
92
  float diff = probability_by_hamming_weight(_qo, J_eff, h_t, z, theta_eff, t, n_qubits);
92
93
  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
+ atomic_add_float(&(hamming_prob[_qo]), diff);
94
95
  }
95
96
  }
96
97
 
@@ -103,8 +104,8 @@ inline uint xorshift32(uint *state) {
103
104
  return x;
104
105
  }
105
106
 
106
- float bootstrap_worker(__constant char* theta, __global double* G_m, __constant int* indices, const int k, const int n) {
107
- double energy = 0.0;
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;
108
109
  const size_t n_st = (size_t)n;
109
110
  for (int u = 0; u < n; ++u) {
110
111
  const size_t u_offset = u * n_st;
@@ -116,7 +117,7 @@ float bootstrap_worker(__constant char* theta, __global double* G_m, __constant
116
117
  }
117
118
  }
118
119
  for (int v = u + 1; v < n; ++v) {
119
- const double val = G_m[u_offset + v];
120
+ const float val = G_m[u_offset + v];
120
121
  bool v_bit = theta[v];
121
122
  for (int x = 0; x < k; ++x) {
122
123
  if (indices[x] == v) {
@@ -128,14 +129,12 @@ float bootstrap_worker(__constant char* theta, __global double* G_m, __constant
128
129
  }
129
130
  }
130
131
 
131
- return (float)energy;
132
+ return energy;
132
133
  }
133
134
 
134
- #define ENERGY_EPSILON 2e-23
135
-
136
135
  __kernel void bootstrap(
137
136
  uint prng_seed,
138
- __global double* G_m,
137
+ __global float* G_m,
139
138
  __constant char* best_theta,
140
139
  __constant int* indices_array,
141
140
  __constant int* args, // args[0] = n, args[1] = k
@@ -176,7 +175,7 @@ __kernel void bootstrap(
176
175
  if (hid_energy < lid_energy) {
177
176
  loc_energy[lt_id] = hid_energy;
178
177
  loc_index[lt_id] = loc_index[lt_id + offset];
179
- } else if (((hid_energy - lid_energy) <= ENERGY_EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
178
+ } else if (((hid_energy - lid_energy) <= FLT_EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
180
179
  loc_index[lt_id] = loc_index[lt_id + offset];
181
180
  }
182
181
  }
@@ -189,8 +188,8 @@ __kernel void bootstrap(
189
188
  }
190
189
  }
191
190
 
192
- 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) {
193
- double energy = 0.0;
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;
194
193
  for (int u = 0; u < n; ++u) {
195
194
  bool u_bit = theta[u];
196
195
  for (int x = 0; x < k; ++x) {
@@ -202,7 +201,7 @@ float bootstrap_worker_sparse(__constant char* theta, __global double* G_data, _
202
201
  const size_t mCol = G_rows[u + 1];
203
202
  for (int col = G_rows[u]; col < mCol; ++col) {
204
203
  const int v = G_cols[col];
205
- const double val = G_data[col];
204
+ const float val = G_data[col];
206
205
  bool v_bit = theta[v];
207
206
  for (int x = 0; x < k; ++x) {
208
207
  if (indices[x] == v) {
@@ -214,12 +213,12 @@ float bootstrap_worker_sparse(__constant char* theta, __global double* G_data, _
214
213
  }
215
214
  }
216
215
 
217
- return (float)energy;
216
+ return energy;
218
217
  }
219
218
 
220
219
  __kernel void bootstrap_sparse(
221
220
  uint prng_seed,
222
- __global double* G_data,
221
+ __global float* G_data,
223
222
  __global unsigned* G_rows,
224
223
  __global unsigned* G_cols,
225
224
  __constant char* best_theta,
@@ -261,7 +260,7 @@ __kernel void bootstrap_sparse(
261
260
  if (hid_energy < lid_energy) {
262
261
  loc_energy[lt_id] = hid_energy;
263
262
  loc_index[lt_id] = loc_index[lt_id + offset];
264
- } else if (((hid_energy - lid_energy) <= ENERGY_EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
263
+ } else if (((hid_energy - lid_energy) <= FLT_EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
265
264
  loc_index[lt_id] = loc_index[lt_id + offset];
266
265
  }
267
266
  }
@@ -281,8 +280,8 @@ inline float rand_uniform(uint *state) {
281
280
  }
282
281
 
283
282
  // Compute cut value from bitset solution
284
- double compute_cut_bitset(__global const double* G_m, const uint* sol_bits, int n) {
285
- double cut_val = 0.0;
283
+ float compute_cut_bitset(__global const float* G_m, const uint* sol_bits, int n) {
284
+ float cut_val = 0.0f;
286
285
  for (int u = 0; u < n; u++) {
287
286
  int u_word = u >> 5; // divide by 32
288
287
  int u_bit = u & 31;
@@ -298,6 +297,7 @@ double compute_cut_bitset(__global const double* G_m, const uint* sol_bits, int
298
297
  }
299
298
  }
300
299
  }
300
+
301
301
  return cut_val;
302
302
  }
303
303
 
@@ -305,11 +305,11 @@ double compute_cut_bitset(__global const double* G_m, const uint* sol_bits, int
305
305
  #define MAX_WORDS_MASK 4095
306
306
 
307
307
  __kernel void sample_for_solution_best_bitset(
308
- __global const double* G_m,
308
+ __global const float* G_m,
309
309
  __global const float* thresholds,
310
310
  const int n,
311
311
  const int shots,
312
- const double max_weight,
312
+ const float max_weight,
313
313
  __global float* rng_seeds,
314
314
  __global uint* best_solutions, // [num_groups × ceil(n/32)]
315
315
  __global float* best_energies, // [num_groups]
@@ -330,7 +330,7 @@ __kernel void sample_for_solution_best_bitset(
330
330
  for (int w = 0; w < words; w++) sol_bits[w] = 0;
331
331
  uint temp_sol[MAX_WORDS];
332
332
 
333
- double cut_val = -INFINITY;
333
+ float cut_val = -INFINITY;
334
334
  for (int gid = gid_orig; gid < shots; gid += MAX_PROC_ELEM) {
335
335
 
336
336
  // --- 1. Choose Hamming weight
@@ -344,7 +344,7 @@ __kernel void sample_for_solution_best_bitset(
344
344
  temp_sol[(gid >> 5) & MAX_WORDS_MASK] |= 1U << (gid & 31);
345
345
 
346
346
  for (int count = 1; count < m; ++count) {
347
- double highest_weights[TOP_N];
347
+ float highest_weights[TOP_N];
348
348
  int best_bits[TOP_N];
349
349
  for (int x = 0; x < TOP_N; ++x) {
350
350
  highest_weights[x] = -INFINITY;
@@ -362,7 +362,7 @@ __kernel void sample_for_solution_best_bitset(
362
362
  }
363
363
  const int u_offset = u * n;
364
364
 
365
- double weight = 1.0;
365
+ float weight = 1.0f;
366
366
  for (int k = 0; k < n; k += 32) {
367
367
  for (int l = 0; l < 32; ++l) {
368
368
  const int v = k + l;
@@ -370,16 +370,16 @@ __kernel void sample_for_solution_best_bitset(
370
370
  break;
371
371
  }
372
372
  if ((temp_sol[k >> 5] >> l) & 1) {
373
- weight *= max(2e-7, 1.0 - G_m[u_offset + v] / max_weight);
373
+ weight *= max(FLT_EPSILON, 1.0f - G_m[u_offset + v] / max_weight);
374
374
  }
375
375
  }
376
376
  }
377
377
 
378
378
  int lowest_option = 0;
379
- double lowest_weight = highest_weights[0];
379
+ float lowest_weight = highest_weights[0];
380
380
  if (lowest_weight != -INFINITY) {
381
381
  for (int x = 0; x < TOP_N; ++x) {
382
- double val = highest_weights[x];
382
+ float val = highest_weights[x];
383
383
  if (val < lowest_weight) {
384
384
  lowest_option = x;
385
385
  lowest_weight = highest_weights[x];
@@ -397,9 +397,9 @@ __kernel void sample_for_solution_best_bitset(
397
397
  }
398
398
  }
399
399
 
400
- double total_weight = 0.0;
400
+ float total_weight = 0.0f;
401
401
  for (int x = 0; x < TOP_N; ++x) {
402
- const double val = highest_weights[x];
402
+ const float val = highest_weights[x];
403
403
  if (val == -INFINITY) {
404
404
  continue;
405
405
  }
@@ -407,10 +407,10 @@ __kernel void sample_for_solution_best_bitset(
407
407
  }
408
408
 
409
409
  const float bit_prob = rand_uniform(&state);
410
- double tot_prob = 0.0;
410
+ float tot_prob = 0.0f;
411
411
  int best_bit = 0;
412
412
  for (int x = 0; x < TOP_N; ++x) {
413
- const double val = highest_weights[x];
413
+ const float val = highest_weights[x];
414
414
  if (val == -INFINITY) {
415
415
  continue;
416
416
  }
@@ -427,7 +427,7 @@ __kernel void sample_for_solution_best_bitset(
427
427
  }
428
428
 
429
429
  // --- 3. Compute cut value
430
- double temp_cut = compute_cut_bitset(G_m, temp_sol, n);
430
+ float temp_cut = compute_cut_bitset(G_m, temp_sol, n);
431
431
  if (temp_cut > cut_val) {
432
432
  cut_val = temp_cut;
433
433
  for (int i = 0; i < words; ++i) {
@@ -465,8 +465,8 @@ __kernel void sample_for_solution_best_bitset(
465
465
  }
466
466
 
467
467
  // Compute cut value from bitset solution
468
- 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) {
469
- double cut_val = 0.0;
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
470
  for (unsigned u = 0; u < n; u++) {
471
471
  int u_word = u >> 5; // divide by 32
472
472
  int u_bit = u & 31;
@@ -484,6 +484,7 @@ double compute_cut_bitset_sparse(__global const double* G_data, __global const u
484
484
  }
485
485
  }
486
486
  }
487
+
487
488
  return cut_val;
488
489
  }
489
490
 
@@ -509,13 +510,13 @@ int binary_search(__global const unsigned* l, const unsigned t, const unsigned l
509
510
  }
510
511
 
511
512
  __kernel void sample_for_solution_best_bitset_sparse(
512
- __global const double* G_data,
513
+ __global const float* G_data,
513
514
  __global const unsigned* G_rows,
514
515
  __global const unsigned* G_cols,
515
516
  __global const float* thresholds,
516
517
  const int n,
517
518
  const int shots,
518
- const double max_weight,
519
+ const float max_weight,
519
520
  __global float* rng_seeds,
520
521
  __global uint* best_solutions, // [num_groups × ceil(n/32)]
521
522
  __global float* best_energies, // [num_groups]
@@ -536,7 +537,7 @@ __kernel void sample_for_solution_best_bitset_sparse(
536
537
  for (int w = 0; w < words; w++) sol_bits[w] = 0;
537
538
  uint temp_sol[MAX_WORDS];
538
539
 
539
- double cut_val = -INFINITY;
540
+ float cut_val = -INFINITY;
540
541
  for (int gid = gid_orig; gid < shots; gid += MAX_PROC_ELEM) {
541
542
 
542
543
  // --- 1. Choose Hamming weight
@@ -550,7 +551,7 @@ __kernel void sample_for_solution_best_bitset_sparse(
550
551
  temp_sol[(gid >> 5) & MAX_WORDS_MASK] |= 1U << (gid & 31);
551
552
 
552
553
  for (int count = 1; count < m; ++count) {
553
- double highest_weights[TOP_N];
554
+ float highest_weights[TOP_N];
554
555
  int best_bits[TOP_N];
555
556
  for (int x = 0; x < TOP_N; ++x) {
556
557
  highest_weights[x] = -INFINITY;
@@ -567,14 +568,14 @@ __kernel void sample_for_solution_best_bitset_sparse(
567
568
  continue;
568
569
  }
569
570
 
570
- double weight = 1.0;
571
+ float weight = 1.0f;
571
572
 
572
573
  unsigned max_col = G_rows[u + 1];
573
574
  for (int col = G_rows[u]; col < max_col; ++col) {
574
575
  const int v = G_cols[col];
575
576
 
576
577
  if ((temp_sol[v >> 5] >> (v & 31)) & 1) {
577
- weight *= max(2e-7, 1.0 - G_data[col] / max_weight);
578
+ weight *= max(FLT_EPSILON, 1.0f - G_data[col] / max_weight);
578
579
  }
579
580
  }
580
581
 
@@ -586,15 +587,15 @@ __kernel void sample_for_solution_best_bitset_sparse(
586
587
  int end = G_rows[v + 1];
587
588
  int j = binary_search(&(G_cols[start]), u, end - start) + start;
588
589
  if (j < end) {
589
- weight *= max(2e-7, 1.0 - G_data[j] / max_weight);
590
+ weight *= max(FLT_EPSILON, 1.0f - G_data[j] / max_weight);
590
591
  }
591
592
  }
592
593
 
593
594
  int lowest_option = 0;
594
- double lowest_weight = highest_weights[0];
595
+ float lowest_weight = highest_weights[0];
595
596
  if (lowest_weight != -INFINITY) {
596
597
  for (int x = 0; x < TOP_N; ++x) {
597
- double val = highest_weights[x];
598
+ float val = highest_weights[x];
598
599
  if (val < lowest_weight) {
599
600
  lowest_option = x;
600
601
  lowest_weight = highest_weights[x];
@@ -612,9 +613,9 @@ __kernel void sample_for_solution_best_bitset_sparse(
612
613
  }
613
614
  }
614
615
 
615
- double total_weight = 0.0;
616
+ float total_weight = 0.0f;
616
617
  for (int x = 0; x < TOP_N; ++x) {
617
- const double val = highest_weights[x];
618
+ const float val = highest_weights[x];
618
619
  if (val == -INFINITY) {
619
620
  continue;
620
621
  }
@@ -622,10 +623,10 @@ __kernel void sample_for_solution_best_bitset_sparse(
622
623
  }
623
624
 
624
625
  const float bit_prob = rand_uniform(&state);
625
- double tot_prob = 0.0;
626
+ float tot_prob = 0.0f;
626
627
  int best_bit = 0;
627
628
  for (int x = 0; x < TOP_N; ++x) {
628
- const double val = highest_weights[x];
629
+ const float val = highest_weights[x];
629
630
  if (val == -INFINITY) {
630
631
  continue;
631
632
  }
@@ -642,7 +643,7 @@ __kernel void sample_for_solution_best_bitset_sparse(
642
643
  }
643
644
 
644
645
  // --- 3. Compute cut value
645
- double temp_cut = compute_cut_bitset_sparse(G_data, G_rows, G_cols, temp_sol, n);
646
+ float temp_cut = compute_cut_bitset_sparse(G_data, G_rows, G_cols, temp_sol, n);
646
647
  if (temp_cut > cut_val) {
647
648
  cut_val = temp_cut;
648
649
  for (int i = 0; i < words; ++i) {
@@ -22,7 +22,7 @@ def update_repulsion_choice(G_m, max_weight, weights, n, used, node):
22
22
  for nbr in range(n):
23
23
  if used[nbr]:
24
24
  continue
25
- weights[nbr] *= max(2e-8, 1 - G_m[node, nbr] / max_weight)
25
+ weights[nbr] *= max(1.1920928955078125e-7, 1 - G_m[node, nbr] / max_weight)
26
26
 
27
27
 
28
28
  # Written by Elara (OpenAI custom GPT) and improved by Dan Strano
@@ -33,7 +33,7 @@ def local_repulsion_choice(G_m, max_weight, weights, n, m, shot):
33
33
  - High-degree nodes are already less likely
34
34
  - After choosing a node, its neighbors' probabilities are further reduced
35
35
  adjacency_data, adjacency_rows: CSR-format sparse adjacency data
36
- weights: float64 array of shape (n,)
36
+ weights: float32 array of shape (n,)
37
37
  """
38
38
 
39
39
  weights = weights.copy()
@@ -175,7 +175,7 @@ def run_sampling_opencl(G_m_np, thresholds_np, shots, n, is_g_buf_reused):
175
175
  thresholds_buf,
176
176
  np.int32(n),
177
177
  np.int32(shots),
178
- np.float64(G_m_np.max()),
178
+ np.float32(G_m_np.max()),
179
179
  rng_buf,
180
180
  solutions_buf,
181
181
  best_energies_buf,
@@ -218,11 +218,9 @@ def cpu_footer(shots, quality, n_qubits, G_m, nodes):
218
218
  maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, hamming_prob)
219
219
 
220
220
  degrees = None
221
- J_eff = 1.0 / (1.0 + (2e-52) - J_eff)
222
- weights = J_eff.astype(np.float64)
223
- J_eff = None
221
+ J_eff = 1.0 / (1.0 + (2 ** -52) - J_eff)
224
222
 
225
- best_solution, best_value = sample_for_solution(G_m, shots, hamming_prob, weights)
223
+ best_solution, best_value = sample_for_solution(G_m, shots, hamming_prob, J_eff)
226
224
 
227
225
  bit_string, l, r = get_cut(best_solution, nodes)
228
226
 
@@ -254,7 +252,7 @@ def maxcut_tfim(
254
252
  if isinstance(G, nx.Graph):
255
253
  nodes = list(G.nodes())
256
254
  n_qubits = len(nodes)
257
- G_m = nx.to_numpy_array(G, weight='weight', nonedge=0.0)
255
+ G_m = nx.to_numpy_array(G, weight='weight', nonedge=0.0, dtype=np.float32)
258
256
  else:
259
257
  n_qubits = len(G)
260
258
  nodes = list(range(n_qubits))
@@ -275,13 +273,13 @@ def maxcut_tfim(
275
273
  return "01", weight, ([nodes[0]], [nodes[1]])
276
274
 
277
275
  if quality is None:
278
- quality = 2
276
+ quality = 3
279
277
 
280
278
  if shots is None:
281
279
  # Number of measurement shots
282
280
  shots = n_qubits << quality
283
281
 
284
- n_steps = 2 << quality
282
+ n_steps = n_qubits << quality
285
283
  grid_size = n_steps * n_qubits
286
284
 
287
285
  if (not is_base_maxcut_gpu) or not (IS_OPENCL_AVAILABLE and grid_size >= 128):
@@ -347,11 +345,9 @@ def maxcut_tfim(
347
345
 
348
346
  if not is_alt_gpu_sampling:
349
347
  degrees = None
350
- J_eff = 1.0 / (1.0 + (2e-52) - J_eff)
351
- weights = J_eff.astype(np.float64)
352
- J_eff = None
348
+ J_eff = 1.0 / (1.0 + (2 ** -52) - J_eff)
353
349
 
354
- return gpu_footer(shots, n_qubits, G_m, weights, hamming_prob, nodes)
350
+ return gpu_footer(shots, n_qubits, G_m, J_eff, hamming_prob, nodes)
355
351
 
356
352
  fix_cdf(hamming_prob)
357
353
  best_solution, best_value = run_sampling_opencl(G_m, hamming_prob, shots, n_qubits, is_g_buf_reused)
@@ -43,7 +43,7 @@ def update_repulsion_choice(G_cols, G_data, G_rows, max_weight, weights, n, used
43
43
  nbr = G_cols[j]
44
44
  if used[nbr]:
45
45
  continue
46
- weights[nbr] *= max(2e-7, 1 - G_data[j] / max_weight)
46
+ weights[nbr] *= max(1.1920928955078125e-7, 1 - G_data[j] / max_weight)
47
47
 
48
48
  for nbr in range(node):
49
49
  if used[nbr]:
@@ -52,7 +52,7 @@ def update_repulsion_choice(G_cols, G_data, G_rows, max_weight, weights, n, used
52
52
  end = G_rows[nbr + 1]
53
53
  j = binary_search(G_cols[start:end], node) + start
54
54
  if j < end:
55
- weights[nbr] *= max(2e-8, 1 - G_data[j] / max_weight)
55
+ weights[nbr] *= max(1.1920928955078125e-7, 1 - G_data[j] / max_weight)
56
56
 
57
57
 
58
58
  # Written by Elara (OpenAI custom GPT) and improved by Dan Strano
@@ -63,7 +63,7 @@ def local_repulsion_choice(G_cols, G_data, G_rows, max_weight, weights, n, m, sh
63
63
  - High-degree nodes are already less likely
64
64
  - After choosing a node, its neighbors' probabilities are further reduced
65
65
  adjacency_data, adjacency_rows: CSR-format sparse adjacency data
66
- weights: float64 array of shape (n,)
66
+ weights: float32 array of shape (n,)
67
67
  """
68
68
 
69
69
  weights = weights.copy()
@@ -226,7 +226,7 @@ def run_sampling_opencl(G_m_csr, thresholds_np, shots, n, is_g_buf_reused):
226
226
  thresholds_buf,
227
227
  np.int32(n),
228
228
  np.int32(shots),
229
- np.float64(G_m_csr.data.max()),
229
+ np.float32(G_m_csr.data.max()),
230
230
  rng_buf,
231
231
  solutions_buf,
232
232
  best_energies_buf,
@@ -271,11 +271,9 @@ def cpu_footer(shots, quality, n_qubits, G_data, G_rows, G_cols, nodes):
271
271
  maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, hamming_prob)
272
272
 
273
273
  degrees = None
274
- J_eff = 1.0 / (1.0 + (2e-52) - J_eff)
275
- weights = J_eff.astype(np.float64)
276
- J_eff = None
274
+ J_eff = 1.0 / (1.0 + (2 ** -52) - J_eff)
277
275
 
278
- best_solution, best_value = sample_for_solution(G_data, G_rows, G_cols, shots, hamming_prob, weights)
276
+ best_solution, best_value = sample_for_solution(G_data, G_rows, G_cols, shots, hamming_prob, J_eff)
279
277
 
280
278
  bit_string, l, r = get_cut(best_solution, nodes)
281
279
 
@@ -340,13 +338,13 @@ def maxcut_tfim_sparse(
340
338
  return "01", weight, ([nodes[0]], [nodes[1]])
341
339
 
342
340
  if quality is None:
343
- quality = 2
341
+ quality = 3
344
342
 
345
343
  if shots is None:
346
344
  # Number of measurement shots
347
345
  shots = n_qubits << quality
348
346
 
349
- n_steps = 2 << quality
347
+ n_steps = n_qubits << quality
350
348
  grid_size = n_steps * n_qubits
351
349
 
352
350
  if (not is_base_maxcut_gpu) or not (IS_OPENCL_AVAILABLE and grid_size >= 128):
@@ -412,11 +410,9 @@ def maxcut_tfim_sparse(
412
410
 
413
411
  if not is_alt_gpu_sampling:
414
412
  degrees = None
415
- J_eff = 1.0 / (1.0 + (2e-52) - J_eff)
416
- weights = J_eff.astype(np.float64)
417
- J_eff = None
413
+ J_eff = 1.0 / (1.0 + (2 ** -52) - J_eff)
418
414
 
419
- return gpu_footer(shots, n_qubits, G_m.data, G_m.indptr, G_m.indices, weights, hamming_prob, nodes)
415
+ return gpu_footer(shots, n_qubits, G_m.data, G_m.indptr, G_m.indices, J_eff, hamming_prob, nodes)
420
416
 
421
417
  fix_cdf(hamming_prob)
422
418
  best_solution, best_value = run_sampling_opencl(G_m, hamming_prob, shots, n_qubits, is_g_buf_reused)
@@ -22,7 +22,7 @@ def update_repulsion_choice(G_func, G_func_args_tuple, nodes, max_weight, weight
22
22
  for nbr in range(n):
23
23
  if used[nbr]:
24
24
  continue
25
- weights[nbr] *= max(2e-8, 1 - G_func((nodes[node], nodes[nbr]), G_func_args_tuple) / max_weight)
25
+ weights[nbr] *= max(1.1920928955078125e-7, 1 - G_func((nodes[node], nodes[nbr]), G_func_args_tuple) / max_weight)
26
26
 
27
27
 
28
28
  # Written by Elara (OpenAI custom GPT) and improved by Dan Strano
@@ -120,7 +120,7 @@ def sample_for_solution(G_func, G_func_args_tuple, nodes, max_weight, shots, thr
120
120
  def init_J_and_z(G_func, G_func_args_tuple, nodes):
121
121
  n_qubits = len(nodes)
122
122
  degrees = np.empty(n_qubits, dtype=np.uint32)
123
- J_eff = np.empty(n_qubits, dtype=np.float32)
123
+ J_eff = np.empty(n_qubits, dtype=np.float64)
124
124
  J_max = -float("inf")
125
125
  G_max = -float("inf")
126
126
  for n in prange(n_qubits):
@@ -151,11 +151,9 @@ def cpu_footer(shots, quality, n_qubits, G_func, G_func_args_tuple, nodes):
151
151
 
152
152
  max_weight = degrees.sum()
153
153
  degrees = None
154
- J_eff = 1.0 / (1.0 + (2e-52) - J_eff)
155
- weights = J_eff.astype(np.float64)
156
- J_eff = None
154
+ J_eff = 1.0 / (1.0 + (2 ** -52) - J_eff)
157
155
 
158
- best_solution, best_value = sample_for_solution(G_func, G_func_args_tuple, nodes, G_max, shots, hamming_prob, max_weight, weights, n_qubits)
156
+ best_solution, best_value = sample_for_solution(G_func, G_func_args_tuple, nodes, G_max, shots, hamming_prob, max_weight, J_eff, n_qubits)
159
157
 
160
158
  bit_string, l, r = get_cut(best_solution, nodes)
161
159
 
@@ -198,13 +196,13 @@ def maxcut_tfim_streaming(
198
196
  return "01", weight, ([nodes[0]], [nodes[1]])
199
197
 
200
198
  if quality is None:
201
- quality = 2
199
+ quality = 3
202
200
 
203
201
  if shots is None:
204
202
  # Number of measurement shots
205
203
  shots = n_qubits << quality
206
204
 
207
- n_steps = 2 << quality
205
+ n_steps = n_qubits << quality
208
206
  grid_size = n_steps * n_qubits
209
207
 
210
208
  if (not is_base_maxcut_gpu) or not (IS_OPENCL_AVAILABLE and grid_size >= 128):
@@ -216,7 +214,7 @@ def maxcut_tfim_streaming(
216
214
  tot_t = 2.0 * n_steps * delta_t
217
215
  h_mult = 2.0 / tot_t
218
216
 
219
- args = np.empty(3, dtype=np.float32)
217
+ args = np.empty(3, dtype=np.float64)
220
218
  args[0] = delta_t
221
219
  args[1] = tot_t
222
220
  args[2] = h_mult
@@ -270,8 +268,6 @@ def maxcut_tfim_streaming(
270
268
 
271
269
  max_weight = degrees.sum()
272
270
  degrees = None
273
- J_eff = 1.0 / (1.0 + (2e-52) - J_eff)
274
- weights = J_eff.astype(np.float64)
275
- J_eff = None
271
+ J_eff = 1.0 / (1.0 + (2 ** -52) - J_eff)
276
272
 
277
- return gpu_footer(shots, n_qubits, G_func, G_func_args_tuple, nodes, G_max, weights, degrees, hamming_prob, max_weight)
273
+ return gpu_footer(shots, n_qubits, G_func, G_func_args_tuple, nodes, G_max, J_eff, degrees, hamming_prob, max_weight)
@@ -124,8 +124,7 @@ def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy):
124
124
  if min_energy < energy:
125
125
  return min_energy
126
126
 
127
- ENERGY_EPSILON = 2e-23
128
- choices = np.where(abs(min_energy_host - energy) <= ENERGY_EPSILON)[0]
127
+ choices = np.where(np.isclose(min_energy_host, energy, atol=2e-23, rtol=0))[0]
129
128
  best_i = np.random.choice(choices) if len(choices) else np.argmin(min_energy_host)
130
129
 
131
130
  flip_index_start = best_i * k
@@ -152,7 +151,7 @@ def spin_glass_solver(
152
151
  if isinstance(G, nx.Graph):
153
152
  nodes = list(G.nodes())
154
153
  n_qubits = len(nodes)
155
- G_m = nx.to_numpy_array(G, weight='weight', nonedge=0.0)
154
+ G_m = nx.to_numpy_array(G, weight='weight', nonedge=0.0, dtype=np.float32)
156
155
  else:
157
156
  n_qubits = len(G)
158
157
  nodes = list(range(n_qubits))
@@ -173,7 +172,7 @@ def spin_glass_solver(
173
172
  return "01", weight, ([nodes[0]], [nodes[1]]), -weight
174
173
 
175
174
  if quality is None:
176
- quality = 2
175
+ quality = 3
177
176
 
178
177
  bitstring = ""
179
178
  if isinstance(best_guess, str):
@@ -129,8 +129,7 @@ def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices
129
129
  if min_energy < energy:
130
130
  return min_energy
131
131
 
132
- ENERGY_EPSILON = 2e-23
133
- choices = np.where(abs(min_energy_host - energy) <= ENERGY_EPSILON)[0]
132
+ choices = np.where(np.isclose(min_energy_host, energy, atol=2e-23, rtol=0))[0]
134
133
  best_i = np.random.choice(choices) if len(choices) else np.argmin(min_energy_host)
135
134
 
136
135
  flip_index_start = best_i * k
@@ -143,7 +142,7 @@ def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices
143
142
 
144
143
 
145
144
  def to_scipy_sparse_upper_triangular(G, nodes, n_nodes):
146
- lil = lil_matrix((n_nodes, n_nodes), dtype=np.float64)
145
+ lil = lil_matrix((n_nodes, n_nodes), dtype=np.float32)
147
146
  for u in range(n_nodes):
148
147
  u_node = nodes[u]
149
148
  for v in range(u + 1, n_nodes):
@@ -190,7 +189,7 @@ def spin_glass_solver_sparse(
190
189
  return "01", weight, ([nodes[0]], [nodes[1]]), -weight
191
190
 
192
191
  if quality is None:
193
- quuality = 2
192
+ quuality = 3
194
193
 
195
194
  bitstring = ""
196
195
  if isinstance(best_guess, str):
@@ -86,7 +86,7 @@ def spin_glass_solver_streaming(
86
86
  return "01", weight, ([nodes[0]], [nodes[1]]), -weight
87
87
 
88
88
  if quality is None:
89
- quality = 2
89
+ quality = 3
90
90
 
91
91
  bitstring = ""
92
92
  if isinstance(best_guess, str):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 5.7.3
3
+ Version: 6.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
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
51
51
 
52
52
  setup(
53
53
  name='pyqrackising',
54
- version='5.7.3',
54
+ version='6.0.0',
55
55
  author='Dan Strano',
56
56
  author_email='stranoj@gmail.com',
57
57
  description='Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)',
File without changes
File without changes
File without changes