PyQrackIsing 9.15.2__tar.gz → 9.15.3__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.3}/PKG-INFO +1 -1
  2. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyproject.toml +1 -1
  3. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/kernels.cl +116 -81
  4. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/spin_glass_solver_sparse.py +116 -113
  5. {pyqrackising-9.15.2 → pyqrackising-9.15.3/pyqrackising.egg-info}/PKG-INFO +1 -1
  6. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/setup.py +1 -1
  7. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/LICENSE.md +0 -0
  8. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/MANIFEST.in +0 -0
  9. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/README.md +0 -0
  10. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/__init__.py +0 -0
  11. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
  12. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/generate_tfim_samples.py +0 -0
  13. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/maxcut_tfim.py +0 -0
  14. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/maxcut_tfim_sparse.py +0 -0
  15. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/maxcut_tfim_streaming.py +0 -0
  16. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/maxcut_tfim_util.py +0 -0
  17. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/otoc.py +0 -0
  18. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/solve_maxcut_bnb.py +0 -0
  19. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/solve_maxcut_bnb_sparse.py +0 -0
  20. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/solve_maxcut_bnb_streaming.py +0 -0
  21. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/solve_maxcut_exact.py +0 -0
  22. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/solve_maxcut_exact_sparse.py +0 -0
  23. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/solve_maxcut_exact_streaming.py +0 -0
  24. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/spin_glass_solver.py +0 -0
  25. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/spin_glass_solver_streaming.py +0 -0
  26. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/tfim_magnetization.py +0 -0
  27. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/tfim_square_magnetization.py +0 -0
  28. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/tsp.py +0 -0
  29. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/tsp_maxcut.py +0 -0
  30. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising.egg-info/SOURCES.txt +0 -0
  31. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising.egg-info/dependency_links.txt +0 -0
  32. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising.egg-info/not-zip-safe +0 -0
  33. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising.egg-info/requires.txt +0 -0
  34. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising.egg-info/top_level.txt +0 -0
  35. {pyqrackising-9.15.2 → pyqrackising-9.15.3}/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.3
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.3"
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
  }
@@ -35,26 +35,31 @@ gnl = opencl_context.GRAY_NODE_LIMIT
35
35
  @njit(parallel=True, cache=True)
36
36
  def run_single_bit_flips(best_theta, is_spin_glass, G_data, G_rows, G_cols):
37
37
  n = len(best_theta)
38
+ deltas = np.empty(n, dtype=dtype)
39
+
40
+ for i in prange(n):
41
+ # Compute delta: walk only adjacency list of i (O(degree(i)))
42
+ old_bit = best_theta[i]
43
+ new_bit = not old_bit
44
+ delta = dtype(0)
45
+ for col in range(G_rows[i], G_rows[i + 1]):
46
+ v = G_cols[col]
47
+ val = G_data[col]
48
+ v_bit = best_theta[v]
49
+ old_cut = old_bit != v_bit
50
+ new_cut = new_bit != v_bit
51
+ if old_cut != new_cut:
52
+ delta += val if new_cut else -val
53
+ if is_spin_glass:
54
+ delta *= dtype(2)
55
+ deltas[i] = delta
38
56
 
39
- 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
-
52
- best_index = np.argmax(energies)
53
- best_energy = energies[best_index]
57
+ best_index = np.argmax(deltas)
58
+ best_delta = deltas[best_index]
54
59
  best_state = best_theta.copy()
55
60
  best_state[best_index] = not best_state[best_index]
56
61
 
57
- return best_energy, best_state
62
+ return best_delta, best_state
58
63
 
59
64
 
60
65
  @njit(parallel=True, cache=True)
@@ -64,58 +69,65 @@ def run_double_bit_flips(best_theta, is_spin_glass, G_data, G_rows, G_cols, thre
64
69
  thread_batch = (combo_count + thread_count - 1) // thread_count
65
70
 
66
71
  states = np.empty((thread_count, n), dtype=np.bool_)
67
- energies = np.empty(thread_count, dtype=dtype)
68
-
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
113
-
114
- best_index = np.argmax(energies)
115
- best_energy = energies[best_index]
72
+ deltas = np.empty(thread_count, dtype=dtype)
73
+
74
+ for t in prange(thread_count):
75
+ s = int(t)
76
+ while s < combo_count:
77
+ c = int(s)
78
+ i = int(0)
79
+ lcv = int(n - 1)
80
+ while c >= lcv:
81
+ c -= lcv
82
+ i += 1
83
+ lcv -= 1
84
+ if not lcv:
85
+ break
86
+ j = int(c + i + 1)
87
+
88
+ # Delta: walk adjacency lists of i and j only
89
+ old_i = best_theta[i]
90
+ new_i = not old_i
91
+ old_j = best_theta[j]
92
+ new_j = not old_j
93
+ delta = dtype(0)
94
+
95
+ for col in range(G_rows[i], G_rows[i + 1]):
96
+ v = G_cols[col]
97
+ if v == j:
98
+ continue # k-l edge: both flip, XOR unchanged, zero delta
99
+ val = G_data[col]
100
+ v_bit = best_theta[v]
101
+ if (old_i != v_bit) != (new_i != v_bit):
102
+ delta += val if (new_i != v_bit) else -val
103
+
104
+ for col in range(G_rows[j], G_rows[j + 1]):
105
+ v = G_cols[col]
106
+ if v == i:
107
+ continue
108
+ val = G_data[col]
109
+ v_bit = best_theta[v]
110
+ if (old_j != v_bit) != (new_j != v_bit):
111
+ delta += val if (new_j != v_bit) else -val
112
+
113
+ if is_spin_glass:
114
+ delta *= dtype(2)
115
+
116
+ states[t, i] = new_i
117
+ states[t, j] = new_j
118
+ # copy other bits (only need to track i,j changes)
119
+ for k in range(n):
120
+ if k != i and k != j:
121
+ states[t, k] = best_theta[k]
122
+ deltas[t] = delta
123
+
124
+ s += thread_batch
125
+
126
+ best_index = np.argmax(deltas)
127
+ best_delta = deltas[best_index]
116
128
  best_state = states[best_index]
117
129
 
118
- return best_energy, best_state
130
+ return best_delta, best_state
119
131
 
120
132
 
121
133
  @njit(parallel=True, cache=True)
@@ -170,36 +182,32 @@ def run_gray_optimization(
170
182
  thread_iterations = (gray_iterations + thread_count - 1) // thread_count
171
183
  blocks = (n + 63) >> 6
172
184
 
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
185
+ for i in prange(thread_count):
186
+ iterator = iterators[i]
187
+ best_energy = energies[i]
188
+ for curr_idx in range(thread_iterations):
189
+ for block in range(blocks):
190
+ flip_bit = gray_code_next(iterator, curr_idx, block << 6)
191
+ # Incremental delta: walk only adjacency list of flip_bit
192
+ new_bit = iterator[flip_bit]
193
+ old_bit = not new_bit
194
+ delta = dtype(0)
195
+ for col in range(G_rows[flip_bit], G_rows[flip_bit + 1]):
196
+ v = G_cols[col]
197
+ val = G_data[col]
198
+ v_bit = iterator[v]
199
+ old_cut = old_bit != v_bit
200
+ new_cut = new_bit != v_bit
201
+ if old_cut != new_cut:
202
+ delta += val if new_cut else -val
203
+ if is_spin_glass:
204
+ delta *= dtype(2)
205
+ if delta > dtype(0):
206
+ best_energy += delta
207
+ else:
208
+ iterator[flip_bit] = not iterator[flip_bit]
209
+ if best_energy > energies[i]:
210
+ energies[i] = best_energy
203
211
 
204
212
  best_index = np.argmax(energies)
205
213
  best_energy = energies[best_index]
@@ -212,7 +220,6 @@ def run_bit_flips_opencl(
212
220
  is_double,
213
221
  n,
214
222
  kernel,
215
- best_energy,
216
223
  theta,
217
224
  theta_buf,
218
225
  G_data_buf,
@@ -269,13 +276,13 @@ def run_bit_flips_opencl(
269
276
  # Queue read for results we might not need
270
277
  cl.enqueue_copy(queue, max_index_host, max_index_buf)
271
278
 
272
- # Find global minimum
279
+ # Find global best delta
273
280
  best_x = np.argmax(max_energy_host)
274
281
  energy = max_energy_host[best_x]
275
282
 
276
- if energy <= best_energy:
277
- # No improvement: we can exit early
278
- return best_energy, theta
283
+ if energy <= 0.0:
284
+ # No improvement: best delta is non-positive, exit early.
285
+ return 0.0, theta
279
286
 
280
287
  # We need the best index
281
288
  queue.finish()
@@ -708,7 +715,6 @@ def spin_glass_solver_sparse(
708
715
  False,
709
716
  n_qubits,
710
717
  single_bit_flips_kernel,
711
- max_energy,
712
718
  best_theta,
713
719
  theta_buf,
714
720
  G_data_buf,
@@ -719,8 +725,8 @@ def spin_glass_solver_sparse(
719
725
  )
720
726
  else:
721
727
  energy, state = run_single_bit_flips(best_theta, is_spin_glass, G_m.data, G_m.indptr, G_m.indices)
722
- if energy > max_energy:
723
- max_energy = energy
728
+ if energy > 0.0:
729
+ max_energy += energy
724
730
  best_theta = state
725
731
  improved = True
726
732
  continue
@@ -732,7 +738,6 @@ def spin_glass_solver_sparse(
732
738
  True,
733
739
  n_qubits,
734
740
  double_bit_flips_kernel,
735
- max_energy,
736
741
  best_theta,
737
742
  theta_buf,
738
743
  G_data_buf,
@@ -743,8 +748,8 @@ def spin_glass_solver_sparse(
743
748
  )
744
749
  else:
745
750
  energy, state = run_double_bit_flips(best_theta, is_spin_glass, G_m.data, G_m.indptr, G_m.indices, thread_count)
746
- if energy > max_energy:
747
- max_energy = energy
751
+ if energy > 0.0:
752
+ max_energy += energy
748
753
  best_theta = state
749
754
  improved = True
750
755
  continue
@@ -810,7 +815,6 @@ def spin_glass_solver_sparse(
810
815
  False,
811
816
  n_qubits,
812
817
  single_bit_flips_kernel,
813
- max_energy,
814
818
  reheat_theta,
815
819
  theta_buf,
816
820
  G_data_buf,
@@ -821,8 +825,8 @@ def spin_glass_solver_sparse(
821
825
  )
822
826
  else:
823
827
  energy, state = run_single_bit_flips(reheat_theta, is_spin_glass, G_m.data, G_m.indptr, G_m.indices)
824
- if energy > max_energy:
825
- max_energy = energy
828
+ if energy > 0.0:
829
+ max_energy += energy
826
830
  best_theta = state
827
831
  improved = True
828
832
  continue
@@ -837,7 +841,6 @@ def spin_glass_solver_sparse(
837
841
  True,
838
842
  n_qubits,
839
843
  double_bit_flips_kernel,
840
- max_energy,
841
844
  reheat_theta,
842
845
  theta_buf,
843
846
  G_data_buf,
@@ -848,8 +851,8 @@ def spin_glass_solver_sparse(
848
851
  )
849
852
  else:
850
853
  energy, state = run_double_bit_flips(reheat_theta, is_spin_glass, G_m.data, G_m.indptr, G_m.indices, thread_count)
851
- if energy > max_energy:
852
- max_energy = energy
854
+ if energy > 0.0:
855
+ max_energy += energy
853
856
  best_theta = state
854
857
  improved = True
855
858
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 9.15.2
3
+ Version: 9.15.3
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.3",
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