PyQrackIsing 9.7.8__tar.gz → 9.8.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyqrackising-9.7.8/pyqrackising.egg-info → pyqrackising-9.8.1}/PKG-INFO +1 -1
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/README.md +1 -1
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/pyproject.toml +1 -1
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/pyqrackising/maxcut_tfim_util.py +174 -4
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/pyqrackising/spin_glass_solver.py +41 -40
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/pyqrackising/spin_glass_solver_streaming.py +34 -29
- {pyqrackising-9.7.8 → pyqrackising-9.8.1/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/setup.py +1 -1
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/LICENSE.md +0 -0
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/MANIFEST.in +0 -0
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/pyqrackising/__init__.py +0 -0
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/pyqrackising/maxcut_tfim.py +0 -0
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/pyqrackising/maxcut_tfim_sparse.py +0 -0
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/pyqrackising/maxcut_tfim_streaming.py +0 -0
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/pyqrackising/otoc.py +0 -0
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/pyqrackising/spin_glass_solver_sparse.py +0 -0
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/pyqrackising/tsp.py +0 -0
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/pyqrackising/tsp_maxcut.py +0 -0
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-9.7.8 → pyqrackising-9.8.1}/setup.cfg +0 -0
|
@@ -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
|
-
[](https://doi.org/10.5281/zenodo.17635216) [](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.
|
|
13
|
+
version = "9.8.1"
|
|
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"}
|
|
@@ -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
|
|
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] =
|
|
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] =
|
|
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,
|
|
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,
|
|
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,
|
|
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 =
|
|
157
|
-
if energy >
|
|
158
|
-
best_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
|
-
|
|
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 =
|
|
172
|
-
if energy >
|
|
173
|
-
best_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
|
-
|
|
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,
|
|
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
|
|
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
|
|
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,
|
|
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 >
|
|
432
|
-
max_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,
|
|
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 >
|
|
444
|
-
max_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
|
-
|
|
463
|
-
|
|
464
|
-
|
|
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 have 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,
|
|
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 = reheat_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,
|
|
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 = reheat_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] =
|
|
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] =
|
|
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,
|
|
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,
|
|
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,
|
|
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 =
|
|
148
|
-
if energy >
|
|
149
|
-
best_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
|
-
|
|
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 =
|
|
163
|
-
if energy >
|
|
164
|
-
best_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
|
-
|
|
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 >
|
|
251
|
-
max_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 >
|
|
259
|
-
max_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
|
-
|
|
274
|
-
|
|
275
|
-
|
|
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 have 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 = reheat_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 = reheat_energy + energy
|
|
295
300
|
best_theta = state
|
|
296
301
|
improved = True
|
|
297
302
|
|
|
@@ -7,7 +7,7 @@ with open(README_PATH) as readme_file:
|
|
|
7
7
|
|
|
8
8
|
setup(
|
|
9
9
|
name='pyqrackising',
|
|
10
|
-
version='9.
|
|
10
|
+
version='9.8.1',
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|