PyQrackIsing 9.7.7__tar.gz → 9.8.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-9.7.7/pyqrackising.egg-info → pyqrackising-9.8.0}/PKG-INFO +1 -1
  2. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/README.md +1 -1
  3. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/pyproject.toml +1 -1
  4. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/pyqrackising/generate_tfim_samples.py +34 -20
  5. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/pyqrackising/maxcut_tfim_util.py +174 -4
  6. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/pyqrackising/spin_glass_solver.py +41 -40
  7. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/pyqrackising/spin_glass_solver_streaming.py +34 -29
  8. {pyqrackising-9.7.7 → pyqrackising-9.8.0/pyqrackising.egg-info}/PKG-INFO +1 -1
  9. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/setup.py +1 -1
  10. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/LICENSE.md +0 -0
  11. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/MANIFEST.in +0 -0
  12. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/pyqrackising/__init__.py +0 -0
  13. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
  14. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/pyqrackising/kernels.cl +0 -0
  15. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/pyqrackising/maxcut_tfim.py +0 -0
  16. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/pyqrackising/maxcut_tfim_sparse.py +0 -0
  17. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/pyqrackising/maxcut_tfim_streaming.py +0 -0
  18. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/pyqrackising/otoc.py +0 -0
  19. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/pyqrackising/spin_glass_solver_sparse.py +0 -0
  20. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/pyqrackising/tfim_magnetization.py +0 -0
  21. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/pyqrackising/tfim_square_magnetization.py +0 -0
  22. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/pyqrackising/tsp.py +0 -0
  23. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/pyqrackising/tsp_maxcut.py +0 -0
  24. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/pyqrackising.egg-info/SOURCES.txt +0 -0
  25. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
  26. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/pyqrackising.egg-info/not-zip-safe +0 -0
  27. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/pyqrackising.egg-info/top_level.txt +0 -0
  28. {pyqrackising-9.7.7 → pyqrackising-9.8.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 9.7.7
3
+ Version: 9.8.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
@@ -3,7 +3,7 @@ Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising
3
3
 
4
4
  (It's "the **Ising** on top.")
5
5
 
6
- [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.17583944.svg)](https://doi.org/10.5281/zenodo.17583944) [![PyPI Downloads](https://static.pepy.tech/badge/pyqrackising)](https://pepy.tech/projects/pyqrackising)
6
+ [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.17625519.svg)](https://doi.org/10.5281/zenodo.17625519) [![PyPI Downloads](https://static.pepy.tech/badge/pyqrackising)](https://pepy.tech/projects/pyqrackising)
7
7
 
8
8
  ## Copyright and license
9
9
  (c) Daniel Strano and the Qrack contributors 2025. All rights reserved.
@@ -10,7 +10,7 @@ build-backend = "setuptools.build_meta"
10
10
 
11
11
  [project]
12
12
  name = "pyqrackising"
13
- version = "9.7.7"
13
+ version = "9.8.0"
14
14
  requires-python = ">=3.8"
15
15
  description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
16
16
  readme = {file = "README.txt", content-type = "text/markdown"}
@@ -5,6 +5,8 @@ import sys
5
5
  import numpy as np
6
6
  from numba import njit
7
7
 
8
+ from collections import Counter
9
+
8
10
 
9
11
  epsilon = opencl_context.epsilon
10
12
 
@@ -89,7 +91,7 @@ def expected_closeness_weight(n_rows, n_cols, hamming_weight):
89
91
  def sample_hamming_weight(thresholds, shots):
90
92
  hamming_samples = np.zeros(shots, dtype=np.int32)
91
93
  for s in range(shots):
92
- hamming_samples[s] = sample_mag(thresholds) + 1
94
+ hamming_samples[s] = sample_mag(thresholds)
93
95
 
94
96
  return hamming_samples
95
97
 
@@ -101,7 +103,7 @@ def fix_cdf(hamming_prob):
101
103
  for i in range(n_bias):
102
104
  tot_prob += hamming_prob[i]
103
105
  cum_prob[i] = tot_prob
104
- cum_prob[-1] = 2.0
106
+ cum_prob[-1] = 1.0
105
107
 
106
108
  return cum_prob
107
109
 
@@ -149,32 +151,44 @@ def generate_tfim_samples(
149
151
  n_rows, n_cols = factor_width(n_qubits)
150
152
 
151
153
  # First dimension: Hamming weight
152
- thresholds = fix_cdf(get_tfim_hamming_distribution(J, h, z, theta, t, n_qubits + 1))
153
- hamming_samples = sample_hamming_weight(thresholds, shots)
154
-
155
- for m in range(len(hamming_samples)):
156
- # Second dimension: permutation within Hamming weight
157
- # (Written with help from Elara, the custom OpenAI GPT)
158
- hs = hamming_samples[m]
159
- rands = []
160
- for s in range(hs):
161
- rands.append(np.random.random())
154
+ bias = get_tfim_hamming_distribution(J=J, h=h, z=z, theta=theta, t=t, n_qubits=n_qubits)
155
+ thresholds = fix_cdf(bias)
156
+ hamming_samples = dict(Counter(sample_hamming_weight(thresholds, shots)))
157
+
158
+ for h_weight, count in hamming_samples.items():
159
+ if h_weight == 0:
160
+ samples += count * [0]
161
+ continue
162
+
163
+ if h_weight == n_qubits:
164
+ samples += count * [(1 << n_qubits) - 1]
165
+ continue
166
+
167
+ rands = [np.random.random() for _ in range(count)]
162
168
  rands.sort()
163
169
  state_int = 0
164
170
  tot_prob = 0
165
171
  s = 0
166
- for combo in itertools.combinations(range(n_qubits), m):
172
+ # How closely grouped are "like" bits to "like"?
173
+ expected_closeness = expected_closeness_weight(n_rows, n_cols, h_weight)
174
+ h_weight_combos = math.comb(n_qubits, h_weight)
175
+ for combo in itertools.combinations(range(n_qubits), h_weight):
167
176
  state_int = sum(1 << pos for pos in combo)
168
- tot_prob += (1.0 + closeness_like_bits(state_int, n_rows, n_cols)) / (
169
- 1.0 + expected_closeness_weight(n_rows, n_cols, m)
170
- )
171
- while (s < hs) and (rands[s] <= tot_prob):
177
+ # When we add all "closeness" possibilities for the particular Hamming weight, we should maintain the (n+1) mean probability dimensions.
178
+ normed_closeness = (1 + closeness_like_bits(state_int, n_rows, n_cols)) / (1 + expected_closeness)
179
+ # Use a normalized weighted average that favors the (n+1)-dimensional model at later times.
180
+ # The (n+1)-dimensional marginal probability is the product of a function of Hamming weight and "closeness," split among all basis states with that specific Hamming weight.
181
+ tot_prob += normed_closeness / h_weight_combos
182
+ while (rands[s] <= tot_prob):
172
183
  samples.append(state_int)
173
184
  s += 1
174
- if s == hs:
185
+ if s == count:
186
+ break
187
+ if s == count:
175
188
  break
176
- for r in range(hs - s):
177
- samples.append(state_int)
189
+ if s < count:
190
+ samples += (count - s) * [state_int]
191
+
178
192
 
179
193
  np.random.shuffle(samples)
180
194
 
@@ -278,11 +278,12 @@ def convert_bool_to_uint(samples):
278
278
 
279
279
  @njit
280
280
  def compute_energy(sample, G_m, n_qubits):
281
- energy = 0
281
+ energy = 0.0
282
282
  for u in range(n_qubits):
283
+ u_bit = sample[u]
283
284
  for v in range(u + 1, n_qubits):
284
285
  val = G_m[u, v]
285
- energy += val if sample[u] == sample[v] else -val
286
+ energy += val if u_bit == sample[v] else -val
286
287
 
287
288
  return -energy
288
289
 
@@ -298,9 +299,93 @@ def compute_cut(sample, G_m, n_qubits):
298
299
  return cut
299
300
 
300
301
 
302
+ @njit
303
+ def compute_energy_diff(u, sample, G_m, n_qubits):
304
+ energy = 0.0
305
+ u_bit = sample[u]
306
+ for v in range(u):
307
+ val = 2 * G_m[u, v]
308
+ energy += val if u_bit == sample[v] else -val
309
+ for v in range(u + 1, n_qubits):
310
+ val = 2 * G_m[u, v]
311
+ energy += val if u_bit == sample[v] else -val
312
+
313
+ return -energy
314
+
315
+
316
+ @njit
317
+ def compute_cut_diff(u, sample, G_m, n_qubits):
318
+ energy = 0.0
319
+ u_bit = sample[u]
320
+ for v in range(u):
321
+ val = G_m[u, v]
322
+ energy += -val if u_bit == sample[v] else val
323
+ for v in range(u + 1, n_qubits):
324
+ val = G_m[u, v]
325
+ energy += -val if u_bit == sample[v] else val
326
+
327
+ return energy
328
+
329
+
330
+ @njit
331
+ def compute_energy_diff_2(k, l, sample, G_m, n_qubits):
332
+ if l < k:
333
+ t = k
334
+ k = l
335
+ l = t
336
+ energy = 0.0
337
+ k_bit = sample[k]
338
+ l_bit = sample[l]
339
+ for v in range(k):
340
+ val = 2 * G_m[k, v]
341
+ energy += val if k_bit == sample[v] else -val
342
+ val = 2 * G_m[l, v]
343
+ energy += val if l_bit == sample[v] else -val
344
+ for v in range(k + 1, l):
345
+ val = 2 * G_m[k, v]
346
+ energy += val if k_bit == sample[v] else -val
347
+ val = 2 * G_m[l, v]
348
+ energy += val if l_bit == sample[v] else -val
349
+ for v in range(l + 1, n_qubits):
350
+ val = 2 * G_m[k, v]
351
+ energy += val if k_bit == sample[v] else -val
352
+ val = 2 * G_m[l, v]
353
+ energy += val if l_bit == sample[v] else -val
354
+
355
+ return -energy
356
+
357
+
358
+ @njit
359
+ def compute_cut_diff_2(k, l, sample, G_m, n_qubits):
360
+ if l < k:
361
+ t = k
362
+ k = l
363
+ l = t
364
+ energy = 0.0
365
+ k_bit = sample[k]
366
+ l_bit = sample[l]
367
+ for v in range(k):
368
+ val = G_m[k, v]
369
+ energy += -val if k_bit == sample[v] else val
370
+ val = G_m[l, v]
371
+ energy += -val if l_bit == sample[v] else val
372
+ for v in range(k + 1, l):
373
+ val = G_m[k, v]
374
+ energy += -val if k_bit == sample[v] else val
375
+ val = G_m[l, v]
376
+ energy += -val if l_bit == sample[v] else val
377
+ for v in range(l + 1, n_qubits):
378
+ val = G_m[k, v]
379
+ energy += -val if k_bit == sample[v] else val
380
+ val = G_m[l, v]
381
+ energy += -val if l_bit == sample[v] else val
382
+
383
+ return energy
384
+
385
+
301
386
  @njit
302
387
  def compute_energy_sparse(sample, G_data, G_rows, G_cols, n_qubits):
303
- energy = 0
388
+ energy = 0.0
304
389
  for u in range(n_qubits):
305
390
  u_bit = sample[u]
306
391
  for col in range(G_rows[u], G_rows[u + 1]):
@@ -326,7 +411,7 @@ def compute_cut_sparse(sample, G_data, G_rows, G_cols, n_qubits):
326
411
 
327
412
  @njit
328
413
  def compute_energy_streaming(sample, G_func, nodes, n_qubits):
329
- energy = 0
414
+ energy = 0.0
330
415
  for u in range(n_qubits):
331
416
  u_bit = sample[u]
332
417
  for v in range(u + 1, n_qubits):
@@ -347,6 +432,90 @@ def compute_cut_streaming(sample, G_func, nodes, n_qubits):
347
432
  return cut
348
433
 
349
434
 
435
+ @njit
436
+ def compute_energy_diff_streaming(u, sample, G_func, nodes, n_qubits):
437
+ energy = 0.0
438
+ u_bit = sample[u]
439
+ for v in range(u):
440
+ val = 2 * G_func(nodes[u], nodes[v])
441
+ energy += val if u_bit == sample[v] else -val
442
+ for v in range(u + 1, n_qubits):
443
+ val = 2 * G_func(nodes[u], nodes[v])
444
+ energy += val if u_bit == sample[v] else -val
445
+
446
+ return -energy
447
+
448
+
449
+ @njit
450
+ def compute_cut_diff_streaming(u, sample, G_func, nodes, n_qubits):
451
+ energy = 0.0
452
+ u_bit = sample[u]
453
+ for v in range(u):
454
+ val = G_func(nodes[u], nodes[v])
455
+ energy += -val if u_bit == sample[v] else val
456
+ for v in range(u + 1, n_qubits):
457
+ val = G_func(nodes[u], nodes[v])
458
+ energy += -val if u_bit == sample[v] else val
459
+
460
+ return energy
461
+
462
+
463
+ @njit
464
+ def compute_energy_diff_2_streaming(k, l, sample, G_func, nodes, n_qubits):
465
+ if l < k:
466
+ t = k
467
+ k = l
468
+ l = t
469
+ energy = 0.0
470
+ k_bit = sample[k]
471
+ l_bit = sample[l]
472
+ for v in range(k):
473
+ val = 2 * G_func(nodes[k], nodes[v])
474
+ energy += val if k_bit == sample[v] else -val
475
+ val = 2 * G_func(nodes[l], nodes[v])
476
+ energy += val if l_bit == sample[v] else -val
477
+ for v in range(k + 1, l):
478
+ val = 2 * G_func(nodes[k], nodes[v])
479
+ energy += val if k_bit == sample[v] else -val
480
+ val = 2 * G_func(nodes[l], nodes[v])
481
+ energy += val if l_bit == sample[v] else -val
482
+ for v in range(l + 1, n_qubits):
483
+ val = 2 * G_func(nodes[k], nodes[v])
484
+ energy += val if k_bit == sample[v] else -val
485
+ val = 2 * G_func(nodes[l], nodes[v])
486
+ energy += val if l_bit == sample[v] else -val
487
+
488
+ return -energy
489
+
490
+
491
+ @njit
492
+ def compute_cut_diff_2_streaming(k, l, sample, G_func, nodes, n_qubits):
493
+ if l < k:
494
+ t = k
495
+ k = l
496
+ l = t
497
+ energy = 0.0
498
+ k_bit = sample[k]
499
+ l_bit = sample[l]
500
+ for v in range(k):
501
+ val = G_func(nodes[k], nodes[v])
502
+ energy += -val if k_bit == sample[v] else val
503
+ val = G_func(nodes[l], nodes[v])
504
+ energy += -val if l_bit == sample[v] else val
505
+ for v in range(k + 1, l):
506
+ val = G_func(nodes[k], nodes[v])
507
+ energy += -val if k_bit == sample[v] else val
508
+ val = G_func(nodes[l], nodes[v])
509
+ energy += -val if l_bit == sample[v] else val
510
+ for v in range(l + 1, n_qubits):
511
+ val = G_func(nodes[k], nodes[v])
512
+ energy += -val if k_bit == sample[v] else val
513
+ val = G_func(nodes[l], nodes[v])
514
+ energy += -val if l_bit == sample[v] else val
515
+
516
+ return energy
517
+
518
+
350
519
  @njit
351
520
  def get_cut(solution, nodes, n):
352
521
  bit_string = ""
@@ -575,4 +744,5 @@ def gray_mutation(index, seed_bits, offset):
575
744
  bits = seed_bits.copy()
576
745
  for i in range(n):
577
746
  bits[n - offset - 1 - i] ^= (gray >> i) & 1
747
+
578
748
  return bits
@@ -1,5 +1,5 @@
1
1
  from .maxcut_tfim import maxcut_tfim
2
- from .maxcut_tfim_util import compute_cut, compute_energy, get_cut, gray_code_next, gray_mutation, heuristic_threshold, int_to_bitstring, make_G_m_buf, make_best_theta_buf, make_best_theta_buf_64, opencl_context, setup_opencl
2
+ from .maxcut_tfim_util import compute_cut, compute_cut_diff, compute_cut_diff_2, compute_energy, compute_energy_diff, compute_energy_diff_2, get_cut, gray_code_next, gray_mutation, heuristic_threshold, int_to_bitstring, make_G_m_buf, make_best_theta_buf, make_best_theta_buf_64, opencl_context, setup_opencl
3
3
  import networkx as nx
4
4
  import numpy as np
5
5
  from numba import njit, prange
@@ -28,12 +28,12 @@ def run_single_bit_flips(best_theta, is_spin_glass, G_m):
28
28
  for i in prange(n):
29
29
  state = best_theta.copy()
30
30
  state[i] = not state[i]
31
- energies[i] = compute_energy(state, G_m, n)
31
+ energies[i] = compute_energy_diff(i, state, G_m, n)
32
32
  else:
33
33
  for i in prange(n):
34
34
  state = best_theta.copy()
35
35
  state[i] = not state[i]
36
- energies[i] = compute_cut(state, G_m, n)
36
+ energies[i] = compute_cut_diff(i, state, G_m, n)
37
37
 
38
38
  best_index = np.argmax(energies)
39
39
  best_energy = energies[best_index]
@@ -71,7 +71,7 @@ def run_double_bit_flips(best_theta, is_spin_glass, G_m, thread_count):
71
71
  state[i] = not state[i]
72
72
  state[j] = not state[j]
73
73
 
74
- states[t], energies[t] = state, compute_energy(state, G_m, n)
74
+ states[t], energies[t] = state, compute_energy_diff_2(i, j, state, G_m, n)
75
75
 
76
76
  s += thread_batch
77
77
  else:
@@ -93,7 +93,7 @@ def run_double_bit_flips(best_theta, is_spin_glass, G_m, thread_count):
93
93
  state[i] = not state[i]
94
94
  state[j] = not state[j]
95
95
 
96
- states[t], energies[t] = state, compute_cut(state, G_m, n)
96
+ states[t], energies[t] = state, compute_cut_diff_2(i, j, state, G_m, n)
97
97
 
98
98
  s += thread_batch
99
99
 
@@ -141,41 +141,40 @@ def pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_m, n, is_spi
141
141
  return best_seeds, best_energies
142
142
 
143
143
  @njit(parallel=True)
144
- def run_gray_optimization(best_theta, iterators, energies, gray_iterations, thread_count, is_spin_glass, G_m):
144
+ def run_gray_optimization(best_theta, iterators, gray_iterations, thread_count, is_spin_glass, G_m):
145
145
  n = len(best_theta)
146
146
  thread_iterations = (gray_iterations + thread_count - 1) // thread_count
147
147
  blocks = (n + 63) >> 6
148
+ energies = np.empty(thread_count, dtype=dtype)
148
149
 
149
150
  if is_spin_glass:
150
151
  for i in prange(thread_count):
151
152
  iterator = iterators[i]
152
- best_energy = energies[i]
153
153
  for curr_idx in range(thread_iterations):
154
+ best_energy = 0.0
154
155
  for block in range(blocks):
155
156
  flip_bit = gray_code_next(iterator, curr_idx, block << 6)
156
- energy = compute_energy(iterator, G_m, n)
157
- if energy > best_energy:
158
- best_energy = energy
157
+ energy = compute_energy_diff(flip_bit, iterator, G_m, n)
158
+ if energy > 0.0:
159
+ best_energy += energy
159
160
  else:
160
161
  # Revert iterator
161
162
  iterator[flip_bit] = not iterator[flip_bit]
162
- if best_energy > energies[i]:
163
- energies[i] = best_energy
163
+ energies[i] += best_energy
164
164
  else:
165
165
  for i in prange(thread_count):
166
166
  iterator = iterators[i]
167
- best_energy = energies[i]
168
167
  for curr_idx in range(thread_iterations):
168
+ best_energy = 0.0
169
169
  for block in range(blocks):
170
170
  flip_bit = gray_code_next(iterator, curr_idx, block << 6)
171
- energy = compute_cut(iterator, G_m, n)
172
- if energy > best_energy:
173
- best_energy = energy
171
+ energy = compute_cut_diff(flip_bit, iterator, G_m, n)
172
+ if energy > 0.0:
173
+ best_energy += energy
174
174
  else:
175
175
  # Revert iterator
176
176
  iterator[flip_bit] = not iterator[flip_bit]
177
- if best_energy > energies[i]:
178
- energies[i] = best_energy
177
+ energies[i] += best_energy
179
178
 
180
179
  best_index = np.argmax(energies)
181
180
  best_energy = energies[best_index]
@@ -184,7 +183,7 @@ def run_gray_optimization(best_theta, iterators, energies, gray_iterations, thre
184
183
  return best_energy, best_state
185
184
 
186
185
 
187
- def run_bit_flips_opencl(is_double, n, kernel, best_energy, theta, theta_buf, G_m_buf, is_segmented, local_size, global_size, args_buf, local_energy_buf, local_index_buf, max_energy_host, max_index_host, max_energy_buf, max_index_buf):
186
+ def run_bit_flips_opencl(is_double, n, kernel, theta, theta_buf, G_m_buf, is_segmented, local_size, global_size, args_buf, local_energy_buf, local_index_buf, max_energy_host, max_index_host, max_energy_buf, max_index_buf):
188
187
  queue = opencl_context.queue
189
188
 
190
189
  # Set kernel args
@@ -227,9 +226,7 @@ def run_bit_flips_opencl(is_double, n, kernel, best_energy, theta, theta_buf, G_
227
226
 
228
227
  if energy <= 0.0:
229
228
  # No improvement: we can exit early
230
- return best_energy, theta
231
-
232
- energy += best_energy
229
+ return 0.0, theta
233
230
 
234
231
  # We need the best index
235
232
  queue.finish()
@@ -297,9 +294,7 @@ def run_gray_search_opencl(n, kernel, best_energy, theta, theta_buf, G_m_buf, is
297
294
 
298
295
  if energy <= 0.0:
299
296
  # No improvement: we can exit early
300
- return best_energy, theta
301
-
302
- energy += best_energy
297
+ return 0.0, theta
303
298
 
304
299
  # We need the best index
305
300
  queue.finish()
@@ -425,11 +420,11 @@ def spin_glass_solver(
425
420
  # Single bit flips with O(n^2)
426
421
  if is_opencl:
427
422
  theta_buf = make_best_theta_buf(best_theta)
428
- energy, state = run_bit_flips_opencl(False, n_qubits, single_bit_flips_kernel, max_energy, best_theta, theta_buf, G_m_buf, is_segmented, *opencl_args)
423
+ energy, state = run_bit_flips_opencl(False, n_qubits, single_bit_flips_kernel, best_theta, theta_buf, G_m_buf, is_segmented, *opencl_args)
429
424
  else:
430
425
  energy, state = run_single_bit_flips(best_theta, is_spin_glass, G_m)
431
- if energy > max_energy:
432
- max_energy = energy
426
+ if energy > 0.0:
427
+ max_energy += energy
433
428
  best_theta = state
434
429
  improved = True
435
430
  continue
@@ -437,11 +432,11 @@ def spin_glass_solver(
437
432
  # Double bit flips with O(n^3)
438
433
  if is_opencl:
439
434
  # theta_buf has not changed
440
- energy, state = run_bit_flips_opencl(True, n_qubits, double_bit_flips_kernel, max_energy, best_theta, theta_buf, G_m_buf, is_segmented, *opencl_args)
435
+ energy, state = run_bit_flips_opencl(True, n_qubits, double_bit_flips_kernel, best_theta, theta_buf, G_m_buf, is_segmented, *opencl_args)
441
436
  else:
442
437
  energy, state = run_double_bit_flips(best_theta, is_spin_glass, G_m, thread_count)
443
- if energy > max_energy:
444
- max_energy = energy
438
+ if energy > 0.0:
439
+ max_energy += energy
445
440
  best_theta = state
446
441
  improved = True
447
442
  continue
@@ -459,24 +454,30 @@ def spin_glass_solver(
459
454
  improved = True
460
455
  continue
461
456
 
462
- energy, state = run_gray_optimization(best_theta, iterators, energies, gray_iterations, thread_count, is_spin_glass, G_m)
463
- if energy > max_energy:
464
- max_energy = energy
457
+ energies = None
458
+ energy, state = run_gray_optimization(best_theta, iterators, gray_iterations, thread_count, is_spin_glass, G_m)
459
+ if energy > 0.0:
460
+ max_energy += energy
465
461
  best_theta = state
466
462
  improved = True
467
463
  continue
468
464
 
465
+ if max_energy == float("inf"):
466
+ # We no way to compare for improvement.
467
+ break
468
+
469
469
  # Post-reheat phase
470
470
  reheat_theta = state
471
+ reheat_energy = energy
471
472
 
472
473
  # Single bit flips with O(n^2)
473
474
  if is_opencl:
474
475
  theta_buf = make_best_theta_buf(reheat_theta)
475
- energy, state = run_bit_flips_opencl(False, n_qubits, single_bit_flips_kernel, max_energy, reheat_theta, theta_buf, G_m_buf, is_segmented, *opencl_args)
476
+ energy, state = run_bit_flips_opencl(False, n_qubits, single_bit_flips_kernel, reheat_theta, theta_buf, G_m_buf, is_segmented, *opencl_args)
476
477
  else:
477
478
  energy, state = run_single_bit_flips(reheat_theta, is_spin_glass, G_m)
478
- if energy > max_energy:
479
- max_energy = energy
479
+ if energy > (max_energy - reheat_energy):
480
+ max_energy += energy
480
481
  best_theta = state
481
482
  improved = True
482
483
  continue
@@ -484,11 +485,11 @@ def spin_glass_solver(
484
485
  # Double bit flips with O(n^3)
485
486
  if is_opencl:
486
487
  # theta_buf has not changed
487
- energy, state = run_bit_flips_opencl(True, n_qubits, double_bit_flips_kernel, max_energy, reheat_theta, theta_buf, G_m_buf, is_segmented, *opencl_args)
488
+ energy, state = run_bit_flips_opencl(True, n_qubits, double_bit_flips_kernel, reheat_theta, theta_buf, G_m_buf, is_segmented, *opencl_args)
488
489
  else:
489
490
  energy, state = run_double_bit_flips(reheat_theta, is_spin_glass, G_m, thread_count)
490
- if energy > max_energy:
491
- max_energy = energy
491
+ if energy > (max_energy - reheat_energy):
492
+ max_energy += energy
492
493
  best_theta = state
493
494
  improved = True
494
495
 
@@ -1,5 +1,5 @@
1
1
  from .maxcut_tfim_streaming import maxcut_tfim_streaming
2
- from .maxcut_tfim_util import compute_cut_streaming, compute_energy_streaming, get_cut, get_cut_base, gray_code_next, gray_mutation, heuristic_threshold, int_to_bitstring, opencl_context
2
+ from .maxcut_tfim_util import compute_cut_streaming, compute_cut_diff_streaming, compute_cut_diff_2_streaming, compute_energy_streaming, compute_energy_diff_streaming, compute_energy_diff_2_streaming, get_cut, get_cut_base, gray_code_next, gray_mutation, heuristic_threshold, int_to_bitstring, opencl_context
3
3
  import networkx as nx
4
4
  import numpy as np
5
5
  from numba import njit, prange
@@ -19,12 +19,12 @@ def run_single_bit_flips(best_theta, is_spin_glass, G_func, nodes):
19
19
  for i in prange(n):
20
20
  state = best_theta.copy()
21
21
  state[i] = not state[i]
22
- energies[i] = compute_energy_streaming(state, G_func, nodes, n)
22
+ energies[i] = compute_energy_diff_streaming(i, state, G_func, nodes, n)
23
23
  else:
24
24
  for i in prange(n):
25
25
  state = best_theta.copy()
26
26
  state[i] = not state[i]
27
- energies[i] = compute_cut_streaming(state, G_func, nodes, n)
27
+ energies[i] = compute_cut_diff_streaming(i, state, G_func, nodes, n)
28
28
 
29
29
  best_index = np.argmax(energies)
30
30
  best_energy = energies[best_index]
@@ -62,7 +62,7 @@ def run_double_bit_flips(best_theta, is_spin_glass, G_func, nodes, thread_count)
62
62
  state[i] = not state[i]
63
63
  state[j] = not state[j]
64
64
 
65
- states[t], energies[t] = state, compute_energy_streaming(state, G_func, nodes, n)
65
+ states[t], energies[t] = state, compute_energy_diff_2_streaming(i, j, state, G_func, nodes, n)
66
66
 
67
67
  s += thread_batch
68
68
  else:
@@ -84,7 +84,7 @@ def run_double_bit_flips(best_theta, is_spin_glass, G_func, nodes, thread_count)
84
84
  state[i] = not state[i]
85
85
  state[j] = not state[j]
86
86
 
87
- states[t], energies[t] = state, compute_cut_streaming(state, G_func, nodes, n)
87
+ states[t], energies[t] = state, compute_cut_diff_2_streaming(i, j, state, G_func, nodes, n)
88
88
 
89
89
  s += thread_batch
90
90
 
@@ -132,41 +132,40 @@ def pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_func, nodes,
132
132
  return best_seeds, best_energies
133
133
 
134
134
  @njit(parallel=True)
135
- def run_gray_optimization(best_theta, iterators, energies, gray_iterations, thread_count, is_spin_glass, G_func, nodes):
135
+ def run_gray_optimization(best_theta, iterators, gray_iterations, thread_count, is_spin_glass, G_func, nodes):
136
136
  n = len(best_theta)
137
137
  thread_iterations = (gray_iterations + thread_count - 1) // thread_count
138
138
  blocks = (n + 63) >> 6
139
+ energies = np.empty(thread_count, dtype=dtype)
139
140
 
140
141
  if is_spin_glass:
141
142
  for i in prange(thread_count):
142
143
  iterator = iterators[i]
143
- best_energy = energies[i]
144
144
  for curr_idx in range(thread_iterations):
145
+ best_energy = 0.0
145
146
  for block in range(blocks):
146
147
  flip_bit = gray_code_next(iterator, curr_idx, block << 6)
147
- energy = compute_energy_streaming(iterator, G_func, nodes, n)
148
- if energy > best_energy:
149
- best_energy = energy
148
+ energy = compute_energy_diff_streaming(flip_bit, iterator, G_func, nodes, n)
149
+ if energy > 0.0:
150
+ best_energy += energy
150
151
  else:
151
152
  # Revert iterator
152
153
  iterator[flip_bit] = not iterator[flip_bit]
153
- if best_energy > energies[i]:
154
- energies[i] = best_energy
154
+ energies[i] += best_energy
155
155
  else:
156
156
  for i in prange(thread_count):
157
157
  iterator = iterators[i]
158
- best_energy = energies[i]
159
158
  for curr_idx in range(thread_iterations):
159
+ best_energy = 0.0
160
160
  for block in range(blocks):
161
161
  flip_bit = gray_code_next(iterator, curr_idx, block << 6)
162
- energy = compute_cut_streaming(iterator, G_func, nodes, n)
163
- if energy > best_energy:
164
- best_energy = energy
162
+ energy = compute_cut_diff_streaming(flip_bit, iterator, G_func, nodes, n)
163
+ if energy > 0.0:
164
+ best_energy += energy
165
165
  else:
166
166
  # Revert iterator
167
167
  iterator[flip_bit] = not iterator[flip_bit]
168
- if best_energy > energies[i]:
169
- energies[i] = best_energy
168
+ energies[i] += best_energy
170
169
 
171
170
  best_index = np.argmax(energies)
172
171
  best_energy = energies[best_index]
@@ -247,16 +246,16 @@ def spin_glass_solver_streaming(
247
246
 
248
247
  # Single bit flips with O(n^2)
249
248
  energy, state = run_single_bit_flips(best_theta, is_spin_glass, G_func, nodes)
250
- if energy > max_energy:
251
- max_energy = energy
249
+ if energy > 0.0:
250
+ max_energy += energy
252
251
  best_theta = state
253
252
  improved = True
254
253
  continue
255
254
 
256
255
  # Double bit flips with O(n^3)
257
256
  energy, state = run_double_bit_flips(best_theta, is_spin_glass, G_func, nodes, thread_count)
258
- if energy > max_energy:
259
- max_energy = energy
257
+ if energy > 0.0:
258
+ max_energy += energy
260
259
  best_theta = state
261
260
  improved = True
262
261
  continue
@@ -270,28 +269,34 @@ def spin_glass_solver_streaming(
270
269
  improved = True
271
270
  continue
272
271
 
273
- energy, state = run_gray_optimization(best_theta, iterators, energies, gray_iterations, thread_count, is_spin_glass, G_func, nodes)
274
- if energy > max_energy:
275
- max_energy = energy
272
+ energies = None
273
+ energy, state = run_gray_optimization(best_theta, iterators, gray_iterations, thread_count, is_spin_glass, G_func, nodes)
274
+ if energy > 0.0:
275
+ max_energy += energy
276
276
  best_theta = state
277
277
  improved = True
278
278
  continue
279
279
 
280
+ if max_energy == float("inf"):
281
+ # We no way to compare for improvement.
282
+ break
283
+
280
284
  # Post-reheat phase
281
285
  reheat_theta = state
286
+ reheat_energy = energy
282
287
 
283
288
  # Single bit flips with O(n^2)
284
289
  energy, state = run_single_bit_flips(reheat_theta, is_spin_glass, G_func, nodes)
285
- if energy > max_energy:
286
- max_energy = energy
290
+ if energy > (max_energy - reheat_energy):
291
+ max_energy += energy
287
292
  best_theta = state
288
293
  improved = True
289
294
  continue
290
295
 
291
296
  # Double bit flips with O(n^3)
292
297
  energy, state = run_double_bit_flips(reheat_theta, is_spin_glass, G_func, nodes, thread_count)
293
- if energy > max_energy:
294
- max_energy = energy
298
+ if energy > (max_energy - reheat_energy):
299
+ max_energy += energy
295
300
  best_theta = state
296
301
  improved = True
297
302
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 9.7.7
3
+ Version: 9.8.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
@@ -7,7 +7,7 @@ with open(README_PATH) as readme_file:
7
7
 
8
8
  setup(
9
9
  name='pyqrackising',
10
- version='9.7.7',
10
+ version='9.8.0',
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