PyQrackIsing 9.15.2__tar.gz → 9.15.4__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 (35) hide show
  1. {pyqrackising-9.15.2/pyqrackising.egg-info → pyqrackising-9.15.4}/PKG-INFO +1 -1
  2. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyproject.toml +1 -1
  3. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising/kernels.cl +116 -81
  4. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising/maxcut_tfim_util.py +49 -0
  5. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising/spin_glass_solver_sparse.py +52 -102
  6. {pyqrackising-9.15.2 → pyqrackising-9.15.4/pyqrackising.egg-info}/PKG-INFO +1 -1
  7. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/setup.py +1 -1
  8. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/LICENSE.md +0 -0
  9. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/MANIFEST.in +0 -0
  10. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/README.md +0 -0
  11. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising/__init__.py +0 -0
  12. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
  13. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising/generate_tfim_samples.py +0 -0
  14. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising/maxcut_tfim.py +0 -0
  15. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising/maxcut_tfim_sparse.py +0 -0
  16. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising/maxcut_tfim_streaming.py +0 -0
  17. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising/otoc.py +0 -0
  18. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising/solve_maxcut_bnb.py +0 -0
  19. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising/solve_maxcut_bnb_sparse.py +0 -0
  20. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising/solve_maxcut_bnb_streaming.py +0 -0
  21. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising/solve_maxcut_exact.py +0 -0
  22. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising/solve_maxcut_exact_sparse.py +0 -0
  23. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising/solve_maxcut_exact_streaming.py +0 -0
  24. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising/spin_glass_solver.py +0 -0
  25. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising/spin_glass_solver_streaming.py +0 -0
  26. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising/tfim_magnetization.py +0 -0
  27. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising/tfim_square_magnetization.py +0 -0
  28. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising/tsp.py +0 -0
  29. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising/tsp_maxcut.py +0 -0
  30. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising.egg-info/SOURCES.txt +0 -0
  31. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising.egg-info/dependency_links.txt +0 -0
  32. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising.egg-info/not-zip-safe +0 -0
  33. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising.egg-info/requires.txt +0 -0
  34. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/pyqrackising.egg-info/top_level.txt +0 -0
  35. {pyqrackising-9.15.2 → pyqrackising-9.15.4}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 9.15.2
3
+ Version: 9.15.4
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
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
7
7
 
8
8
  [project]
9
9
  name = "pyqrackising"
10
- version = "9.15.2"
10
+ version = "9.15.4"
11
11
  requires-python = ">=3.8"
12
12
  description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
13
13
  readme = {file = "README.txt", content-type = "text/markdown"}
@@ -285,29 +285,31 @@ __kernel void calculate_cut_sparse(
285
285
  }
286
286
 
287
287
  real1 single_bit_flip_worker_sparse(__constant uint* theta, __global const real1* G_data, __global const uint* G_rows, __global const uint* G_cols, const int n, const bool is_spin_glass, const int k) {
288
- real1 energy = ZERO_R1;
289
- for (int u = 0; u < n; ++u) {
290
- bool u_bit = get_const_bit(theta, u);
291
- if (u == k) {
292
- u_bit = !u_bit;
293
- }
294
- const size_t mCol = G_rows[u + 1];
295
- for (int col = G_rows[u]; col < mCol; ++col) {
296
- const int v = G_cols[col];
297
- const real1 val = G_data[col];
298
- bool v_bit = get_const_bit(theta, v);
299
- if (v == k) {
300
- v_bit = !v_bit;
301
- }
302
- if (u_bit != v_bit) {
303
- energy += val;
304
- } else if (is_spin_glass) {
305
- energy -= val;
306
- }
288
+ // Only edges incident to k change when k is flipped.
289
+ // Walk only the adjacency list of k: O(degree(k)) instead of O(n * degree).
290
+ // Returns the energy DELTA (new_energy - base_energy) for this flip.
291
+ // All threads share the same base state (best_theta), so deltas are
292
+ // directly comparable for finding the best single-bit flip.
293
+ const bool k_bit_old = get_const_bit(theta, k);
294
+ const bool k_bit_new = !k_bit_old;
295
+ real1 delta = ZERO_R1;
296
+ const uint row_end = G_rows[k + 1];
297
+ for (uint col = G_rows[k]; col < row_end; ++col) {
298
+ const int v = G_cols[col];
299
+ const real1 val = G_data[col];
300
+ const bool v_bit = get_const_bit(theta, v);
301
+ const bool old_cut = k_bit_old != v_bit;
302
+ const bool new_cut = k_bit_new != v_bit;
303
+ if (old_cut != new_cut) {
304
+ delta += new_cut ? val : -val;
307
305
  }
308
306
  }
309
-
310
- return energy;
307
+ // For spin glass, the same edge contributes to both cut and non-cut terms,
308
+ // so the delta magnitude doubles (unlike-becomes-like costs twice: lose val + gain -val).
309
+ if (is_spin_glass) {
310
+ delta *= 2.0;
311
+ }
312
+ return delta;
311
313
  }
312
314
 
313
315
  __kernel void single_bit_flips_sparse(
@@ -315,8 +317,8 @@ __kernel void single_bit_flips_sparse(
315
317
  __global const uint* G_rows,
316
318
  __global const uint* G_cols,
317
319
  __constant uint* best_theta,
318
- __constant int* args, // args[0] = n, args[1] = k
319
- __global real1* max_energy_ptr, // output: per-group min energy
320
+ __constant int* args, // args[0] = n, args[1] = is_spin_glass
321
+ __global real1* max_energy_ptr, // output: per-group best delta
320
322
  __global int* max_index_ptr, // output: per-group best index (i)
321
323
  __local real1* loc_energy, // local memory buffer
322
324
  __local int* loc_index // local memory buffer
@@ -331,6 +333,8 @@ __kernel void single_bit_flips_sparse(
331
333
  int best_i = i;
332
334
 
333
335
  for (; i < n; i += max_i) {
336
+ // Worker returns delta; sign determines accept/reject.
337
+ // Only the largest positive delta matters for selecting the best flip.
334
338
  const real1 energy = single_bit_flip_worker_sparse(best_theta, G_data, G_rows, G_cols, n, is_spin_glass, i);
335
339
  if (energy > best_energy) {
336
340
  best_energy = energy;
@@ -342,29 +346,53 @@ __kernel void single_bit_flips_sparse(
342
346
  }
343
347
 
344
348
  real1 double_bit_flip_worker_sparse(__constant uint* theta, __global const real1* G_data, __global const uint* G_rows, __global const uint* G_cols, const int n, const bool is_spin_glass, const int k, const int l) {
345
- real1 energy = ZERO_R1;
346
- for (int u = 0; u < n; ++u) {
347
- bool u_bit = get_const_bit(theta, u);
348
- if ((u == k) || (u == l)) {
349
- u_bit = !u_bit;
349
+ // Only edges incident to k or l change when both are flipped simultaneously.
350
+ // Walk adjacency lists of k and l: O(degree(k) + degree(l)) instead of O(n * degree).
351
+ // Returns the energy DELTA for this double flip.
352
+ const bool k_bit_old = get_const_bit(theta, k);
353
+ const bool k_bit_new = !k_bit_old;
354
+ const bool l_bit_old = get_const_bit(theta, l);
355
+ const bool l_bit_new = !l_bit_old;
356
+ real1 delta = ZERO_R1;
357
+
358
+ // Contribution from edges incident to k (excluding the k-l edge, handled below)
359
+ const uint k_row_end = G_rows[k + 1];
360
+ for (uint col = G_rows[k]; col < k_row_end; ++col) {
361
+ const int v = G_cols[col];
362
+ if (v == l) continue; // handle k-l edge separately
363
+ const real1 val = G_data[col];
364
+ // v is not flipped, so v_bit unchanged
365
+ const bool v_bit = get_const_bit(theta, v);
366
+ const bool old_cut = k_bit_old != v_bit;
367
+ const bool new_cut = k_bit_new != v_bit;
368
+ if (old_cut != new_cut) {
369
+ delta += new_cut ? val : -val;
350
370
  }
351
- const size_t mCol = G_rows[u + 1];
352
- for (int col = G_rows[u]; col < mCol; ++col) {
353
- const int v = G_cols[col];
354
- const real1 val = G_data[col];
355
- bool v_bit = get_const_bit(theta, v);
356
- if ((v == k) || (v == l)) {
357
- v_bit = !v_bit;
358
- }
359
- if (u_bit != v_bit) {
360
- energy += val;
361
- } else if (is_spin_glass) {
362
- energy -= val;
363
- }
371
+ }
372
+
373
+ // Contribution from edges incident to l (excluding the k-l edge)
374
+ const uint l_row_end = G_rows[l + 1];
375
+ for (uint col = G_rows[l]; col < l_row_end; ++col) {
376
+ const int v = G_cols[col];
377
+ if (v == k) continue; // handle k-l edge separately
378
+ const real1 val = G_data[col];
379
+ const bool v_bit = get_const_bit(theta, v);
380
+ const bool old_cut = l_bit_old != v_bit;
381
+ const bool new_cut = l_bit_new != v_bit;
382
+ if (old_cut != new_cut) {
383
+ delta += new_cut ? val : -val;
364
384
  }
365
385
  }
366
386
 
367
- return energy;
387
+ // Handle the k-l edge: both endpoints flip, so XOR status may or may not change.
388
+ // old_cut = (k_bit_old != l_bit_old); new_cut = (k_bit_new != l_bit_new).
389
+ // Since both bits flip, new_cut == old_cut — the k-l edge contribution is unchanged.
390
+ // No delta from the k-l edge. (Included here explicitly for clarity.)
391
+
392
+ if (is_spin_glass) {
393
+ delta *= 2.0;
394
+ }
395
+ return delta;
368
396
  }
369
397
 
370
398
  __kernel void double_bit_flips_sparse(
@@ -372,8 +400,8 @@ __kernel void double_bit_flips_sparse(
372
400
  __global const uint* G_rows,
373
401
  __global const uint* G_cols,
374
402
  __constant uint* best_theta,
375
- __constant int* args, // args[0] = n, args[1] = k
376
- __global real1* max_energy_ptr, // output: per-group min energy
403
+ __constant int* args, // args[0] = n, args[1] = is_spin_glass
404
+ __global real1* max_energy_ptr, // output: per-group best delta
377
405
  __global int* max_index_ptr, // output: per-group best index (i)
378
406
  __local real1* loc_energy, // local memory buffer
379
407
  __local int* loc_index // local memory buffer
@@ -403,6 +431,7 @@ __kernel void double_bit_flips_sparse(
403
431
  }
404
432
  const int l = c + k + 1;
405
433
 
434
+ // Worker returns delta; sign determines accept/reject.
406
435
  const real1 energy = double_bit_flip_worker_sparse(best_theta, G_data, G_rows, G_cols, n, is_spin_glass, k, l);
407
436
  if (energy > best_energy) {
408
437
  best_energy = energy;
@@ -879,12 +908,10 @@ inline size_t gray_code_next(ulong* theta, const size_t curr_idx, const size_t o
879
908
  size_t curr = curr_idx + 1U;
880
909
  prev = prev ^ (prev >> 1U);
881
910
  curr = curr ^ (curr >> 1U);
882
- size_t diff = prev ^ curr;
883
- size_t flip_bit = 0;
884
- while (!((diff >> flip_bit) & 1U)) {
885
- ++flip_bit;
886
- }
887
- flip_bit += offset;
911
+ const ulong diff = prev ^ curr;
912
+ // ctz gives the position of the lowest set bit in a single native instruction.
913
+ // diff is always non-zero (consecutive Gray codes differ in exactly one bit).
914
+ const size_t flip_bit = (size_t)ctz(diff) + offset;
888
915
  theta[flip_bit >> 6U] ^= 1UL << (flip_bit & 63U);
889
916
 
890
917
  return flip_bit;
@@ -1117,25 +1144,31 @@ __kernel void gray_sparse(
1117
1144
  for (; i < gray_iterations; i += max_i) {
1118
1145
  for (int block = 0; block < blocks; ++block) {
1119
1146
  const size_t flip_bit = gray_code_next(theta_local, i, block << 6U);
1120
- real1 energy = ZERO_R1;
1121
- for (uint u = 0; u < n; u++) {
1122
- const bool u_bit = get_local_bit(theta_local, u);
1123
- const size_t mCol = G_rows[u + 1];
1124
- for (int col = G_rows[u]; col < mCol; ++col) {
1125
- const int v = G_cols[col];
1126
- const real1 val = G_data[col];
1127
- const bool v_bit = get_local_bit(theta_local, v);
1128
- if (u_bit != v_bit) {
1129
- energy += val;
1130
- } else if (is_spin_glass) {
1131
- energy -= val;
1132
- }
1147
+ // Incremental update: only edges incident to flip_bit change.
1148
+ // After gray_code_next, theta_local already has the bit flipped.
1149
+ // Determine the new and old bit values.
1150
+ const bool new_bit = get_local_bit(theta_local, flip_bit);
1151
+ const bool old_bit = !new_bit;
1152
+ real1 delta = ZERO_R1;
1153
+ const uint row_end = G_rows[flip_bit + 1];
1154
+ for (uint col = G_rows[flip_bit]; col < row_end; ++col) {
1155
+ const uint v = G_cols[col];
1156
+ const real1 val = G_data[col];
1157
+ const bool v_bit = get_local_bit(theta_local, v);
1158
+ const bool old_cut = old_bit != v_bit;
1159
+ const bool new_cut = new_bit != v_bit;
1160
+ if (old_cut != new_cut) {
1161
+ delta += new_cut ? val : -val;
1133
1162
  }
1134
1163
  }
1164
+ if (is_spin_glass) {
1165
+ delta *= 2.0;
1166
+ }
1135
1167
 
1136
- if (energy > best_energy) {
1137
- best_energy = energy;
1168
+ if (delta > ZERO_R1) {
1169
+ best_energy += delta;
1138
1170
  } else {
1171
+ // Reject: undo the flip
1139
1172
  theta_local[flip_bit >> 6U] ^= 1UL << (flip_bit & 63U);
1140
1173
  }
1141
1174
  }
@@ -1204,25 +1237,27 @@ __kernel void gray_sparse_segmented(
1204
1237
  for (; i < gray_iterations; i += max_i) {
1205
1238
  for (int block = 0; block < blocks; ++block) {
1206
1239
  const size_t flip_bit = gray_code_next(theta_local, i, block << 6U);
1207
- real1 energy = ZERO_R1;
1208
- for (uint u = 0; u < n; u++) {
1209
- const size_t u_offset = u * n;
1210
- const bool u_bit = get_local_bit(theta_local, u);
1211
- const uint row_end = G_rows[u + 1];
1212
- for (uint col = G_rows[u]; col < row_end; ++col) {
1213
- const int v = G_cols[col];
1214
- const real1 val = get_G_m(G_data, col, segment_size);
1215
- const bool v_bit = get_local_bit(theta_local, v);
1216
- if (u_bit != v_bit) {
1217
- energy += val;
1218
- } else if (is_spin_glass) {
1219
- energy -= val;
1220
- }
1240
+ // Incremental update: only edges incident to flip_bit change.
1241
+ const bool new_bit = get_local_bit(theta_local, flip_bit);
1242
+ const bool old_bit = !new_bit;
1243
+ real1 delta = ZERO_R1;
1244
+ const uint row_end = G_rows[flip_bit + 1];
1245
+ for (uint col = G_rows[flip_bit]; col < row_end; ++col) {
1246
+ const int v = G_cols[col];
1247
+ const real1 val = get_G_m(G_data, col, segment_size);
1248
+ const bool v_bit = get_local_bit(theta_local, v);
1249
+ const bool old_cut = old_bit != v_bit;
1250
+ const bool new_cut = new_bit != v_bit;
1251
+ if (old_cut != new_cut) {
1252
+ delta += new_cut ? val : -val;
1221
1253
  }
1222
1254
  }
1255
+ if (is_spin_glass) {
1256
+ delta *= 2.0;
1257
+ }
1223
1258
 
1224
- if (energy > best_energy) {
1225
- best_energy = energy;
1259
+ if (delta > ZERO_R1) {
1260
+ best_energy += delta;
1226
1261
  } else {
1227
1262
  theta_local[flip_bit >> 6U] ^= 1UL << (flip_bit & 63U);
1228
1263
  }
@@ -416,6 +416,55 @@ def compute_cut_sparse(sample, G_data, G_rows, G_cols, n_qubits):
416
416
  return cut
417
417
 
418
418
 
419
+ @njit(cache=True)
420
+ def compute_cut_diff_sparse(u, sample, G_data, G_rows, G_cols, n_qubits):
421
+ energy = 0.0
422
+ u_bit = sample[u]
423
+ for col in range(G_rows[u], G_rows[u + 1]):
424
+ v = G_cols[col]
425
+ val = G_data[col]
426
+ energy += -val if u_bit == sample[v] else val
427
+
428
+ return energy
429
+
430
+
431
+ @njit(cache=True)
432
+ def compute_energy_diff_sparse(u, sample, G_data, G_rows, G_cols, n_qubits):
433
+ energy = 0.0
434
+ u_bit = sample[u]
435
+ for col in range(G_rows[u], G_rows[u + 1]):
436
+ v = G_cols[col]
437
+ val = G_data[col]
438
+ energy += -val if u_bit == sample[v] else val
439
+
440
+ return energy
441
+
442
+
443
+ @njit(cache=True)
444
+ def compute_cut_diff_2_sparse(k, l, sample, G_data, G_rows, G_cols, n_qubits):
445
+ if l < k:
446
+ t = k
447
+ k = l
448
+ l = t
449
+ energy = 0.0
450
+ k_bit = sample[k]
451
+ l_bit = sample[l]
452
+ for col in range(G_rows[k], G_rows[k + 1]):
453
+ v = G_cols[col]
454
+ if v == l:
455
+ continue
456
+ val = G_data[col]
457
+ energy += -val if k_bit == sample[v] else val
458
+ for col in range(G_rows[l], G_rows[l + 1]):
459
+ v = G_cols[col]
460
+ if v == k:
461
+ continue
462
+ val = G_data[col]
463
+ energy += -val if l_bit == sample[v] else val
464
+
465
+ return energy
466
+
467
+
419
468
  @njit(cache=True)
420
469
  def compute_energy_streaming(sample, G_func, nodes, n_qubits):
421
470
  energy = 0.0
@@ -2,6 +2,8 @@ from .maxcut_tfim_sparse import maxcut_tfim_sparse
2
2
  from .maxcut_tfim_util import (
3
3
  compute_cut_sparse,
4
4
  compute_energy_sparse,
5
+ compute_cut_diff_sparse,
6
+ compute_cut_diff_2_sparse,
5
7
  get_cut,
6
8
  gray_code_next,
7
9
  gray_mutation,
@@ -35,22 +37,15 @@ gnl = opencl_context.GRAY_NODE_LIMIT
35
37
  @njit(parallel=True, cache=True)
36
38
  def run_single_bit_flips(best_theta, is_spin_glass, G_data, G_rows, G_cols):
37
39
  n = len(best_theta)
38
-
39
40
  energies = np.empty(n, dtype=dtype)
40
-
41
- if is_spin_glass:
42
- for i in prange(n):
43
- state = best_theta.copy()
44
- state[i] = not state[i]
45
- energies[i] = compute_energy_sparse(state, G_data, G_rows, G_cols, n)
46
- else:
47
- for i in prange(n):
48
- state = best_theta.copy()
49
- state[i] = not state[i]
50
- energies[i] = compute_cut_sparse(state, G_data, G_rows, G_cols, n)
51
-
41
+ for i in prange(n):
42
+ state = best_theta.copy()
43
+ state[i] = not state[i]
44
+ energies[i] = compute_cut_diff_sparse(i, state, G_data, G_rows, G_cols, n)
52
45
  best_index = np.argmax(energies)
53
46
  best_energy = energies[best_index]
47
+ if is_spin_glass:
48
+ best_energy *= 2.0
54
49
  best_state = best_theta.copy()
55
50
  best_state[best_index] = not best_state[best_index]
56
51
 
@@ -66,53 +61,32 @@ def run_double_bit_flips(best_theta, is_spin_glass, G_data, G_rows, G_cols, thre
66
61
  states = np.empty((thread_count, n), dtype=np.bool_)
67
62
  energies = np.empty(thread_count, dtype=dtype)
68
63
 
69
- if is_spin_glass:
70
- for t in prange(thread_count):
71
- s = int(t)
72
- while s < combo_count:
73
- c = int(s)
74
- i = int(0)
75
- lcv = int(n - 1)
76
- while c >= lcv:
77
- c -= lcv
78
- i += 1
79
- lcv -= 1
80
- if not lcv:
81
- break
82
- j = int(c + i + 1)
83
-
84
- state = best_theta.copy()
85
- state[i] = not state[i]
86
- state[j] = not state[j]
87
-
88
- states[t], energies[t] = state, compute_energy_sparse(state, G_data, G_rows, G_cols, n)
89
-
90
- s += thread_batch
91
- else:
92
- for t in prange(thread_count):
93
- s = int(t)
94
- while s < combo_count:
95
- c = int(s)
96
- i = int(0)
97
- lcv = int(n - 1)
98
- while c >= lcv:
99
- c -= lcv
100
- i += 1
101
- lcv -= 1
102
- if not lcv:
103
- break
104
- j = int(c + i + 1)
105
-
106
- state = best_theta.copy()
107
- state[i] = not state[i]
108
- state[j] = not state[j]
109
-
110
- states[t], energies[t] = state, compute_cut_sparse(state, G_data, G_rows, G_cols, n)
111
-
112
- s += thread_batch
64
+ for t in prange(thread_count):
65
+ s = int(t)
66
+ while s < combo_count:
67
+ c = int(s)
68
+ i = int(0)
69
+ lcv = int(n - 1)
70
+ while c >= lcv:
71
+ c -= lcv
72
+ i += 1
73
+ lcv -= 1
74
+ if not lcv:
75
+ break
76
+ j = int(c + i + 1)
77
+
78
+ state = best_theta.copy()
79
+ state[i] = not state[i]
80
+ state[j] = not state[j]
81
+
82
+ states[t], energies[t] = state, compute_cut_diff_2_sparse(i, j, state, G_data, G_rows, G_cols, n)
83
+
84
+ s += thread_batch
113
85
 
114
86
  best_index = np.argmax(energies)
115
87
  best_energy = energies[best_index]
88
+ if is_spin_glass:
89
+ best_energy *= 2.0
116
90
  best_state = states[best_index]
117
91
 
118
92
  return best_energy, best_state
@@ -155,54 +129,30 @@ def pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_data, G_rows
155
129
 
156
130
 
157
131
  @njit(parallel=True, cache=True)
158
- def run_gray_optimization(
159
- best_theta,
160
- iterators,
161
- energies,
162
- gray_iterations,
163
- thread_count,
164
- is_spin_glass,
165
- G_data,
166
- G_rows,
167
- G_cols,
168
- ):
132
+ def run_gray_optimization(best_theta, iterators, gray_iterations, thread_count, is_spin_glass, G_data, G_rows, G_cols):
169
133
  n = len(best_theta)
170
134
  thread_iterations = (gray_iterations + thread_count - 1) // thread_count
171
135
  blocks = (n + 63) >> 6
136
+ energies = np.empty(thread_count, dtype=dtype)
172
137
 
173
- if is_spin_glass:
174
- for i in prange(thread_count):
175
- iterator = iterators[i]
176
- best_energy = energies[i]
177
- for curr_idx in range(thread_iterations):
178
- for block in range(blocks):
179
- flip_bit = gray_code_next(iterator, curr_idx, block << 6)
180
- energy = compute_energy_sparse(iterator, G_data, G_rows, G_cols, n)
181
- if energy > best_energy:
182
- best_energy = energy
183
- else:
184
- # Revert iterator
185
- iterator[flip_bit] = not iterator[flip_bit]
186
- if best_energy > energies[i]:
187
- energies[i] = best_energy
188
- else:
189
- for i in prange(thread_count):
190
- iterator = iterators[i]
191
- best_energy = energies[i]
192
- for curr_idx in range(thread_iterations):
193
- for block in range(blocks):
194
- flip_bit = gray_code_next(iterator, curr_idx, block << 6)
195
- energy = compute_cut_sparse(iterator, G_data, G_rows, G_cols, n)
196
- if energy > best_energy:
197
- best_energy = energy
198
- else:
199
- # Revert iterator
200
- iterator[flip_bit] = not iterator[flip_bit]
201
- if best_energy > energies[i]:
202
- energies[i] = best_energy
138
+ for i in prange(thread_count):
139
+ iterator = iterators[i]
140
+ for curr_idx in range(thread_iterations):
141
+ best_energy = 0.0
142
+ for block in range(blocks):
143
+ flip_bit = gray_code_next(iterator, curr_idx, block << 6)
144
+ energy = compute_cut_diff(flip_bit, iterator, G_data, G_rows, G_cols, n)
145
+ if energy > 0.0:
146
+ best_energy += energy
147
+ else:
148
+ # Revert iterator
149
+ iterator[flip_bit] = not iterator[flip_bit]
150
+ energies[i] += best_energy
203
151
 
204
152
  best_index = np.argmax(energies)
205
153
  best_energy = energies[best_index]
154
+ if is_spin_glass:
155
+ best_energy *= 2.0
206
156
  best_state = iterators[best_index]
207
157
 
208
158
  return best_energy, best_state
@@ -273,9 +223,9 @@ def run_bit_flips_opencl(
273
223
  best_x = np.argmax(max_energy_host)
274
224
  energy = max_energy_host[best_x]
275
225
 
276
- if energy <= best_energy:
226
+ if energy <= 0.0:
277
227
  # No improvement: we can exit early
278
- return best_energy, theta
228
+ return 0.0, theta
279
229
 
280
230
  # We need the best index
281
231
  queue.finish()
@@ -353,9 +303,9 @@ def run_gray_search_opencl(
353
303
  best_x = np.argmax(max_energy_host)
354
304
  energy = max_energy_host[best_x]
355
305
 
356
- if energy <= best_energy:
306
+ if energy <= 0.0:
357
307
  # No improvement: we can exit early
358
- return best_energy, theta
308
+ return 0.0, theta
359
309
 
360
310
  # We need the best index
361
311
  queue.finish()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 9.15.2
3
+ Version: 9.15.4
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
@@ -7,7 +7,7 @@ with open(README_PATH) as readme_file:
7
7
 
8
8
  setup(
9
9
  name="pyqrackising",
10
- version="9.15.2",
10
+ version="9.15.4",
11
11
  author="Dan Strano",
12
12
  author_email="stranoj@gmail.com",
13
13
  description="Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)",
File without changes
File without changes
File without changes
File without changes