PyQrackIsing 7.4.2__tar.gz → 7.4.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.
- {pyqrackising-7.4.2/pyqrackising.egg-info → pyqrackising-7.4.4}/PKG-INFO +1 -1
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/pyproject.toml +1 -1
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/pyqrackising/tsp.py +231 -44
- {pyqrackising-7.4.2 → pyqrackising-7.4.4/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/setup.py +1 -1
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/CMakeLists.txt +0 -0
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/LICENSE.md +0 -0
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/MANIFEST.in +0 -0
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/README.md +0 -0
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/pyqrackising/__init__.py +0 -0
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/pyqrackising/maxcut_tfim.py +0 -0
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/pyqrackising/maxcut_tfim_sparse.py +0 -0
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/pyqrackising/maxcut_tfim_streaming.py +0 -0
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/pyqrackising/maxcut_tfim_util.py +0 -0
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/pyqrackising/spin_glass_solver.py +0 -0
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/pyqrackising/spin_glass_solver_sparse.py +0 -0
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/pyqrackising/spin_glass_solver_streaming.py +0 -0
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/pyqrackising/spin_glass_solver_util.py +0 -0
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/pyqrackising/tsp_maxcut.py +0 -0
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-7.4.2 → pyqrackising-7.4.4}/setup.cfg +0 -0
|
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
|
|
|
12
12
|
|
|
13
13
|
[project]
|
|
14
14
|
name = "pyqrackising"
|
|
15
|
-
version = "7.4.
|
|
15
|
+
version = "7.4.4"
|
|
16
16
|
requires-python = ">=3.8"
|
|
17
17
|
description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
|
|
18
18
|
readme = {file = "README.txt", content-type = "text/markdown"}
|
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
from .spin_glass_solver import spin_glass_solver
|
|
2
2
|
from .maxcut_tfim_util import binary_search, opencl_context, to_scipy_sparse_upper_triangular
|
|
3
|
-
from concurrent.futures import
|
|
3
|
+
from concurrent.futures import ProcessPoolExecutor
|
|
4
4
|
import time
|
|
5
5
|
import itertools
|
|
6
6
|
import networkx as nx
|
|
7
|
-
from numba import njit
|
|
7
|
+
from numba import njit, prange
|
|
8
8
|
import numpy as np
|
|
9
9
|
import os
|
|
10
10
|
from scipy.sparse import lil_matrix
|
|
11
11
|
|
|
12
12
|
|
|
13
|
+
max_parallel_level = np.log2(os.cpu_count())
|
|
14
|
+
|
|
15
|
+
|
|
13
16
|
# two_opt() and targeted_three_opt() written by Elara (OpenAI ChatGPT instance)
|
|
14
17
|
@njit
|
|
15
18
|
def path_length(path, G_m):
|
|
@@ -133,9 +136,6 @@ def targeted_three_opt(path, W, neighbor_lists, k_neighbors=20):
|
|
|
133
136
|
if k <= j or k >= n-1:
|
|
134
137
|
continue
|
|
135
138
|
|
|
136
|
-
# Extract indices
|
|
137
|
-
A, B, C, D, E, F = path[i], path[i+1], path[j], path[j+1], path[k], path[k+1]
|
|
138
|
-
|
|
139
139
|
# 7 unique cases (same as brute force, but restricted)
|
|
140
140
|
new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:]
|
|
141
141
|
dist = path_length(new_path, W)
|
|
@@ -190,6 +190,66 @@ def targeted_three_opt(path, W, neighbor_lists, k_neighbors=20):
|
|
|
190
190
|
return best_path, best_dist
|
|
191
191
|
|
|
192
192
|
|
|
193
|
+
@njit(parallel=True)
|
|
194
|
+
def targeted_three_opt_parallel(path, W, neighbor_lists, k_neighbors=20):
|
|
195
|
+
n = len(path)
|
|
196
|
+
best_path = path[:]
|
|
197
|
+
best_dist = path_length(best_path, W)
|
|
198
|
+
paths = [best_path] * 7
|
|
199
|
+
dists = [best_dist] * 7
|
|
200
|
+
improved = True
|
|
201
|
+
|
|
202
|
+
while improved:
|
|
203
|
+
improved = False
|
|
204
|
+
for i in range(n - 5):
|
|
205
|
+
for _j in range(k_neighbors):
|
|
206
|
+
j = neighbor_lists[path[i], _j]
|
|
207
|
+
if j <= i or j >= n-3:
|
|
208
|
+
continue
|
|
209
|
+
for _k in range(k_neighbors):
|
|
210
|
+
k = neighbor_lists[path[j], _k]
|
|
211
|
+
if k <= j or k >= n-1:
|
|
212
|
+
continue
|
|
213
|
+
|
|
214
|
+
# 7 unique cases (same as brute force, but restricted)
|
|
215
|
+
for l in prange(7):
|
|
216
|
+
match l:
|
|
217
|
+
case 0:
|
|
218
|
+
new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:]
|
|
219
|
+
case 1:
|
|
220
|
+
new_path = best_path[:j+1] + best_path[j+1:k+1][::-1] + best_path[k+1:]
|
|
221
|
+
case 2:
|
|
222
|
+
new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:k+1][::-1] + best_path[k+1:]
|
|
223
|
+
case 3:
|
|
224
|
+
new_path = best_path[:i+1] + best_path[j+1:k+1] + best_path[i+1:j+1] + best_path[k+1:]
|
|
225
|
+
case 4:
|
|
226
|
+
new_path = best_path[:i+1] + best_path[j+1:k+1][::-1] + best_path[i+1:j+1][::-1] + best_path[k+1:]
|
|
227
|
+
case 5:
|
|
228
|
+
new_path = best_path[:i+1] + best_path[j+1:k+1] + best_path[i+1:j+1][::-1] + best_path[k+1:]
|
|
229
|
+
case _:
|
|
230
|
+
new_path = best_path[:i+1] + best_path[j+1:k+1][::-1] + best_path[i+1:j+1] + best_path[k+1:]
|
|
231
|
+
|
|
232
|
+
dists[l] = path_length(best_path, W)
|
|
233
|
+
paths[l] = new_path
|
|
234
|
+
|
|
235
|
+
for i in range(7):
|
|
236
|
+
if dists[i] < best_dist:
|
|
237
|
+
best_path, best_dist, improved = paths[i], dists[i], True
|
|
238
|
+
|
|
239
|
+
if improved:
|
|
240
|
+
break
|
|
241
|
+
|
|
242
|
+
if improved:
|
|
243
|
+
break
|
|
244
|
+
|
|
245
|
+
if improved:
|
|
246
|
+
break
|
|
247
|
+
|
|
248
|
+
path = best_path[:]
|
|
249
|
+
|
|
250
|
+
return best_path, best_dist
|
|
251
|
+
|
|
252
|
+
|
|
193
253
|
@njit
|
|
194
254
|
def init_G_a_b(G_m, a, b):
|
|
195
255
|
n_a_nodes = len(a)
|
|
@@ -456,7 +516,7 @@ def tsp_symmetric_brute_force_driver(G_m, n_nodes, nodes, is_cyclic):
|
|
|
456
516
|
|
|
457
517
|
|
|
458
518
|
@njit
|
|
459
|
-
def tsp_symmetric_driver(G_m, is_cyclic, is_top_level, start_node, end_node, k_neighbors, nodes, sol_a, sol_b, a, b, c, neighbor_lists):
|
|
519
|
+
def tsp_symmetric_driver(G_m, is_cyclic, is_top_level, start_node, end_node, k_neighbors, nodes, sol_a, sol_b, a, b, c, neighbor_lists, is_parallel):
|
|
460
520
|
path_a = [a[x] for x in sol_a[0]]
|
|
461
521
|
path_b = [b[x] for x in sol_b[0]]
|
|
462
522
|
|
|
@@ -489,7 +549,10 @@ def tsp_symmetric_driver(G_m, is_cyclic, is_top_level, start_node, end_node, k_n
|
|
|
489
549
|
best_path, _ = one_way_two_opt(best_path, G_m)
|
|
490
550
|
|
|
491
551
|
if k_neighbors > 0:
|
|
492
|
-
|
|
552
|
+
if is_parallel:
|
|
553
|
+
best_path, _ = targeted_three_opt_parallel(best_path, G_m, neighbor_lists, k_neighbors)
|
|
554
|
+
else:
|
|
555
|
+
best_path, _ = targeted_three_opt(best_path, G_m, neighbor_lists, k_neighbors)
|
|
493
556
|
|
|
494
557
|
# We just corrected segments of 2 and 3,
|
|
495
558
|
# and this is top level,
|
|
@@ -515,7 +578,8 @@ def tsp_symmetric(
|
|
|
515
578
|
is_cyclic=True,
|
|
516
579
|
multi_start=1,
|
|
517
580
|
is_top_level=True,
|
|
518
|
-
is_parallel=False
|
|
581
|
+
is_parallel=False,
|
|
582
|
+
parallel_level=0
|
|
519
583
|
):
|
|
520
584
|
dtype = opencl_context.dtype
|
|
521
585
|
nodes = None
|
|
@@ -575,10 +639,10 @@ def tsp_symmetric(
|
|
|
575
639
|
|
|
576
640
|
G_a, G_b = init_G_a_b(G_m, a, b)
|
|
577
641
|
|
|
578
|
-
if monte_carlo and is_parallel and (len(a) > 3) and (len(b) > 3):
|
|
579
|
-
with
|
|
580
|
-
f = executor.submit(tsp_symmetric, G_a, None, None, quality, shots, is_alt_gpu_sampling, is_base_maxcut_gpu, is_combo_maxcut_gpu, True, 0, False, multi_start, False, True)
|
|
581
|
-
sol_b = tsp_symmetric(G_b, None, None, quality, shots, is_alt_gpu_sampling, is_base_maxcut_gpu, is_combo_maxcut_gpu, True, 0, False, multi_start, False, True)
|
|
642
|
+
if monte_carlo and is_parallel and (parallel_level < max_parallel_level) and (len(a) > 3) and (len(b) > 3):
|
|
643
|
+
with ProcessPoolExecutor(max_workers=1) as executor:
|
|
644
|
+
f = executor.submit(tsp_symmetric, G_a, None, None, quality, shots, is_alt_gpu_sampling, is_base_maxcut_gpu, is_combo_maxcut_gpu, True, 0, False, multi_start, False, True, parallel_level + 1)
|
|
645
|
+
sol_b = tsp_symmetric(G_b, None, None, quality, shots, is_alt_gpu_sampling, is_base_maxcut_gpu, is_combo_maxcut_gpu, True, 0, False, multi_start, False, True, parallel_level + 1)
|
|
582
646
|
sol_a = f.result()
|
|
583
647
|
else:
|
|
584
648
|
sol_a = tsp_symmetric(
|
|
@@ -627,7 +691,7 @@ def tsp_symmetric(
|
|
|
627
691
|
for i in range(len(neighbor_lists)):
|
|
628
692
|
neighbor_lists[i] = [c[1] for c in neighbor_lists[i]]
|
|
629
693
|
|
|
630
|
-
return tsp_symmetric_driver(G_m, is_cyclic, is_top_level, start_node, end_node, k_neighbors, nodes, sol_a, sol_b, a, b, c[0] if len(c) else None, np.array(neighbor_lists))
|
|
694
|
+
return tsp_symmetric_driver(G_m, is_cyclic, is_top_level, start_node, end_node, k_neighbors, nodes, sol_a, sol_b, a, b, c[0] if len(c) else None, np.array(neighbor_lists), is_parallel)
|
|
631
695
|
|
|
632
696
|
|
|
633
697
|
@njit
|
|
@@ -647,7 +711,7 @@ def tsp_asymmetric_brute_force_driver(G_m, n_nodes, nodes, is_cyclic):
|
|
|
647
711
|
|
|
648
712
|
|
|
649
713
|
@njit
|
|
650
|
-
def tsp_asymmetric_driver(G_m, is_reversed, is_cyclic, is_top_level, start_node, end_node, k_neighbors, nodes, sol_a, sol_b, a, b, c):
|
|
714
|
+
def tsp_asymmetric_driver(G_m, is_reversed, is_cyclic, is_top_level, start_node, end_node, k_neighbors, nodes, sol_a, sol_b, a, b, c, is_parallel):
|
|
651
715
|
path_a = [a[x] for x in sol_a[0]]
|
|
652
716
|
path_b = [b[x] for x in sol_b[0]]
|
|
653
717
|
|
|
@@ -691,9 +755,9 @@ def tsp_asymmetric_driver(G_m, is_reversed, is_cyclic, is_top_level, start_node,
|
|
|
691
755
|
|
|
692
756
|
if k_neighbors > 0:
|
|
693
757
|
best_path = final_path
|
|
694
|
-
final_path, best_weight = targeted_three_opt(best_path.copy(), G_m, k_neighbors)
|
|
758
|
+
final_path, best_weight = targeted_three_opt_parallel(best_path.copy(), G_m, k_neighbors) if is_parallel else targeted_three_opt(best_path.copy(), G_m, k_neighbors)
|
|
695
759
|
best_path.reverse()
|
|
696
|
-
path, weight = targeted_three_opt(best_path.copy(), G_m, k_neighbors)
|
|
760
|
+
path, weight = targeted_three_opt_parallel(best_path.copy(), G_m, k_neighbors) if is_parallel else targeted_three_opt(best_path.copy(), G_m, k_neighbors)
|
|
697
761
|
if weight < best_weight:
|
|
698
762
|
final_path, best_weight = path, weight
|
|
699
763
|
else:
|
|
@@ -723,7 +787,8 @@ def tsp_asymmetric(
|
|
|
723
787
|
is_cyclic=True,
|
|
724
788
|
multi_start=1,
|
|
725
789
|
is_top_level=True,
|
|
726
|
-
is_parallel=False
|
|
790
|
+
is_parallel=False,
|
|
791
|
+
parallel_level=0
|
|
727
792
|
):
|
|
728
793
|
dtype = opencl_context.dtype
|
|
729
794
|
nodes = None
|
|
@@ -787,10 +852,10 @@ def tsp_asymmetric(
|
|
|
787
852
|
|
|
788
853
|
G_a, G_b = init_G_a_b(G_m, a, b)
|
|
789
854
|
|
|
790
|
-
if monte_carlo and is_parallel and (len(a) > 2) and (len(b) > 2):
|
|
791
|
-
with
|
|
792
|
-
f = executor.submit(tsp_asymmetric, G_a, None, None, quality, shots, is_alt_gpu_sampling, is_base_maxcut_gpu, is_combo_maxcut_gpu, True, 0, False, multi_start, False, True)
|
|
793
|
-
sol_b = tsp_asymmetric(G_b, None, None, quality, shots, is_alt_gpu_sampling, is_base_maxcut_gpu, is_combo_maxcut_gpu, True, 0, False, multi_start, False, True)
|
|
855
|
+
if monte_carlo and is_parallel and (parallel_level < max_parallel_level) and (len(a) > 2) and (len(b) > 2):
|
|
856
|
+
with ProcessPoolExecutor(max_workers=1) as executor:
|
|
857
|
+
f = executor.submit(tsp_asymmetric, G_a, None, None, quality, shots, is_alt_gpu_sampling, is_base_maxcut_gpu, is_combo_maxcut_gpu, True, 0, False, multi_start, False, True, parallel_level + 1)
|
|
858
|
+
sol_b = tsp_asymmetric(G_b, None, None, quality, shots, is_alt_gpu_sampling, is_base_maxcut_gpu, is_combo_maxcut_gpu, True, 0, False, multi_start, False, True, parallel_level + 1)
|
|
794
859
|
sol_a = f.result()
|
|
795
860
|
else:
|
|
796
861
|
sol_a = tsp_asymmetric(
|
|
@@ -822,7 +887,7 @@ def tsp_asymmetric(
|
|
|
822
887
|
is_parallel=False
|
|
823
888
|
)
|
|
824
889
|
|
|
825
|
-
return tsp_asymmetric_driver(G_m, is_reversed, is_cyclic, is_top_level, start_node, end_node, k_neighbors, nodes, sol_a, sol_b, a, b, c[0] if len(c) else None)
|
|
890
|
+
return tsp_asymmetric_driver(G_m, is_reversed, is_cyclic, is_top_level, start_node, end_node, k_neighbors, nodes, sol_a, sol_b, a, b, c[0] if len(c) else None, is_parallel)
|
|
826
891
|
|
|
827
892
|
|
|
828
893
|
@njit
|
|
@@ -899,9 +964,6 @@ def targeted_three_opt_sparse(path, G_data, G_rows, G_cols, neighbor_lists, k_ne
|
|
|
899
964
|
if k <= j or k >= n-1:
|
|
900
965
|
continue
|
|
901
966
|
|
|
902
|
-
# Extract indices
|
|
903
|
-
A, B, C, D, E, F = path[i], path[i+1], path[j], path[j+1], path[k], path[k+1]
|
|
904
|
-
|
|
905
967
|
# 7 unique cases (same as brute force, but restricted)
|
|
906
968
|
new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:]
|
|
907
969
|
dist = path_length_sparse(new_path, G_data, G_rows, G_cols)
|
|
@@ -956,6 +1018,66 @@ def targeted_three_opt_sparse(path, G_data, G_rows, G_cols, neighbor_lists, k_ne
|
|
|
956
1018
|
return best_path, best_dist
|
|
957
1019
|
|
|
958
1020
|
|
|
1021
|
+
@njit(parallel=True)
|
|
1022
|
+
def targeted_three_opt_sparse_parallel(path, G_data, G_rows, G_cols, neighbor_lists, k_neighbors=20):
|
|
1023
|
+
n = len(path)
|
|
1024
|
+
best_path = path[:]
|
|
1025
|
+
best_dist = path_length_sparse(best_path, G_data, G_rows, G_cols)
|
|
1026
|
+
paths = [best_path] * 7
|
|
1027
|
+
dists = [best_dist] * 7
|
|
1028
|
+
improved = True
|
|
1029
|
+
|
|
1030
|
+
while improved:
|
|
1031
|
+
improved = False
|
|
1032
|
+
for i in range(n - 5):
|
|
1033
|
+
for _j in range(k_neighbors):
|
|
1034
|
+
j = neighbor_lists[path[i], _j]
|
|
1035
|
+
if j <= i or j >= n-3:
|
|
1036
|
+
continue
|
|
1037
|
+
for _k in range(k_neighbors):
|
|
1038
|
+
k = neighbor_lists[path[j], _k]
|
|
1039
|
+
if k <= j or k >= n-1:
|
|
1040
|
+
continue
|
|
1041
|
+
|
|
1042
|
+
# 7 unique cases (same as brute force, but restricted)
|
|
1043
|
+
for l in prange(7):
|
|
1044
|
+
match l:
|
|
1045
|
+
case 0:
|
|
1046
|
+
new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:]
|
|
1047
|
+
case 1:
|
|
1048
|
+
new_path = best_path[:j+1] + best_path[j+1:k+1][::-1] + best_path[k+1:]
|
|
1049
|
+
case 2:
|
|
1050
|
+
new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:k+1][::-1] + best_path[k+1:]
|
|
1051
|
+
case 3:
|
|
1052
|
+
new_path = best_path[:i+1] + best_path[j+1:k+1] + best_path[i+1:j+1] + best_path[k+1:]
|
|
1053
|
+
case 4:
|
|
1054
|
+
new_path = best_path[:i+1] + best_path[j+1:k+1][::-1] + best_path[i+1:j+1][::-1] + best_path[k+1:]
|
|
1055
|
+
case 5:
|
|
1056
|
+
new_path = best_path[:i+1] + best_path[j+1:k+1] + best_path[i+1:j+1][::-1] + best_path[k+1:]
|
|
1057
|
+
case _:
|
|
1058
|
+
new_path = best_path[:i+1] + best_path[j+1:k+1][::-1] + best_path[i+1:j+1] + best_path[k+1:]
|
|
1059
|
+
|
|
1060
|
+
dists[l] = path_length_sparse(new_path, G_data, G_rows, G_cols)
|
|
1061
|
+
paths[l] = new_path
|
|
1062
|
+
|
|
1063
|
+
for i in range(7):
|
|
1064
|
+
if dists[i] < best_dist:
|
|
1065
|
+
best_path, best_dist, improved = paths[i], dists[i], True
|
|
1066
|
+
|
|
1067
|
+
if improved:
|
|
1068
|
+
break
|
|
1069
|
+
|
|
1070
|
+
if improved:
|
|
1071
|
+
break
|
|
1072
|
+
|
|
1073
|
+
if improved:
|
|
1074
|
+
break
|
|
1075
|
+
|
|
1076
|
+
path = best_path[:]
|
|
1077
|
+
|
|
1078
|
+
return best_path, best_dist
|
|
1079
|
+
|
|
1080
|
+
|
|
959
1081
|
@njit
|
|
960
1082
|
def stich_singlet_sparse(G_data, G_rows, G_cols, singlet, bulk):
|
|
961
1083
|
best_path = bulk.copy()
|
|
@@ -1113,7 +1235,7 @@ def init_G_a_b_sparse(G_m, a, b, dtype):
|
|
|
1113
1235
|
|
|
1114
1236
|
|
|
1115
1237
|
@njit
|
|
1116
|
-
def tsp_symmetric_sparse_driver(G_data, G_rows, G_cols, is_top_level, k_neighbors, nodes, sol_a, sol_b, a, b, neighbor_lists):
|
|
1238
|
+
def tsp_symmetric_sparse_driver(G_data, G_rows, G_cols, is_top_level, k_neighbors, nodes, sol_a, sol_b, a, b, neighbor_lists, is_parallel):
|
|
1117
1239
|
path_a = [a[x] for x in sol_a[0]]
|
|
1118
1240
|
path_b = [b[x] for x in sol_b[0]]
|
|
1119
1241
|
|
|
@@ -1123,7 +1245,10 @@ def tsp_symmetric_sparse_driver(G_data, G_rows, G_cols, is_top_level, k_neighbor
|
|
|
1123
1245
|
best_path, _ = one_way_two_opt_sparse(best_path, G_data, G_rows, G_cols)
|
|
1124
1246
|
|
|
1125
1247
|
if k_neighbors > 0:
|
|
1126
|
-
|
|
1248
|
+
if is_parallel:
|
|
1249
|
+
best_path, _ = targeted_three_opt_sparse_parallel(best_path, G_data, G_rows, G_cols, neighbor_lists, k_neighbors)
|
|
1250
|
+
else:
|
|
1251
|
+
best_path, _ = targeted_three_opt_sparse(best_path, G_data, G_rows, G_cols, neighbor_lists, k_neighbors)
|
|
1127
1252
|
|
|
1128
1253
|
best_path = restitch_sparse(G_data, G_rows, G_cols, best_path)
|
|
1129
1254
|
|
|
@@ -1136,7 +1261,8 @@ def tsp_symmetric_sparse(
|
|
|
1136
1261
|
G,
|
|
1137
1262
|
k_neighbors=20,
|
|
1138
1263
|
is_top_level=True,
|
|
1139
|
-
is_parallel=True
|
|
1264
|
+
is_parallel=True,
|
|
1265
|
+
parallel_level=0
|
|
1140
1266
|
):
|
|
1141
1267
|
dtype = opencl_context.dtype
|
|
1142
1268
|
nodes = None
|
|
@@ -1163,10 +1289,10 @@ def tsp_symmetric_sparse(
|
|
|
1163
1289
|
|
|
1164
1290
|
G_a, G_b = init_G_a_b_sparse(G_m, a, b, dtype)
|
|
1165
1291
|
|
|
1166
|
-
if is_parallel and (len(a) > 3) and (len(b) > 3):
|
|
1167
|
-
with
|
|
1168
|
-
f = executor.submit(tsp_symmetric_sparse, G_a, False, True)
|
|
1169
|
-
sol_b = tsp_symmetric_sparse(G_b, False, True)
|
|
1292
|
+
if is_parallel and (parallel_level < max_parallel_level) and (len(a) > 3) and (len(b) > 3):
|
|
1293
|
+
with ProcessPoolExecutor(max_workers=1) as executor:
|
|
1294
|
+
f = executor.submit(tsp_symmetric_sparse, G_a, False, True, parallel_level + 1)
|
|
1295
|
+
sol_b = tsp_symmetric_sparse(G_b, False, True, parallel_level + 1)
|
|
1170
1296
|
sol_a = f.result()
|
|
1171
1297
|
else:
|
|
1172
1298
|
sol_a = tsp_symmetric_sparse(
|
|
@@ -1199,7 +1325,7 @@ def tsp_symmetric_sparse(
|
|
|
1199
1325
|
for i in range(len(neighbor_lists)):
|
|
1200
1326
|
neighbor_lists[i] = [c[1] for c in neighbor_lists[i]]
|
|
1201
1327
|
|
|
1202
|
-
return tsp_symmetric_sparse_driver(G_m.data, G_m.indptr, G_m.indices, is_top_level, k_neighbors, nodes, sol_a, sol_b, a, b, np.array(neighbor_lists))
|
|
1328
|
+
return tsp_symmetric_sparse_driver(G_m.data, G_m.indptr, G_m.indices, is_top_level, k_neighbors, nodes, sol_a, sol_b, a, b, np.array(neighbor_lists), is_parallel)
|
|
1203
1329
|
|
|
1204
1330
|
|
|
1205
1331
|
@njit
|
|
@@ -1252,9 +1378,6 @@ def targeted_three_opt_streaming(path, G_func, neighbor_lists, k_neighbors=20):
|
|
|
1252
1378
|
if k <= j or k >= n-1:
|
|
1253
1379
|
continue
|
|
1254
1380
|
|
|
1255
|
-
# Extract indices
|
|
1256
|
-
A, B, C, D, E, F = path[i], path[i+1], path[j], path[j+1], path[k], path[k+1]
|
|
1257
|
-
|
|
1258
1381
|
# 7 unique cases (same as brute force, but restricted)
|
|
1259
1382
|
new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:]
|
|
1260
1383
|
dist = path_length_streaming(new_path, G_func)
|
|
@@ -1309,6 +1432,66 @@ def targeted_three_opt_streaming(path, G_func, neighbor_lists, k_neighbors=20):
|
|
|
1309
1432
|
return best_path, best_dist
|
|
1310
1433
|
|
|
1311
1434
|
|
|
1435
|
+
@njit(parallel=True)
|
|
1436
|
+
def targeted_three_opt_streaming_parallel(path, G_func, neighbor_lists, k_neighbors=20):
|
|
1437
|
+
n = len(path)
|
|
1438
|
+
best_path = path[:]
|
|
1439
|
+
best_dist = path_length_streaming(best_path, G_func)
|
|
1440
|
+
paths = [best_path] * 7
|
|
1441
|
+
dists = [best_dist] * 7
|
|
1442
|
+
improved = True
|
|
1443
|
+
|
|
1444
|
+
while improved:
|
|
1445
|
+
improved = False
|
|
1446
|
+
for i in range(n - 5):
|
|
1447
|
+
for _j in range(k_neighbors):
|
|
1448
|
+
j = neighbor_lists[path[i], _j]
|
|
1449
|
+
if j <= i or j >= n-3:
|
|
1450
|
+
continue
|
|
1451
|
+
for _k in range(k_neighbors):
|
|
1452
|
+
k = neighbor_lists[path[j], _k]
|
|
1453
|
+
if k <= j or k >= n-1:
|
|
1454
|
+
continue
|
|
1455
|
+
|
|
1456
|
+
# 7 unique cases (same as brute force, but restricted)
|
|
1457
|
+
for l in prange(7):
|
|
1458
|
+
match l:
|
|
1459
|
+
case 0:
|
|
1460
|
+
new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:]
|
|
1461
|
+
case 1:
|
|
1462
|
+
new_path = best_path[:j+1] + best_path[j+1:k+1][::-1] + best_path[k+1:]
|
|
1463
|
+
case 2:
|
|
1464
|
+
new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:k+1][::-1] + best_path[k+1:]
|
|
1465
|
+
case 3:
|
|
1466
|
+
new_path = best_path[:i+1] + best_path[j+1:k+1] + best_path[i+1:j+1] + best_path[k+1:]
|
|
1467
|
+
case 4:
|
|
1468
|
+
new_path = best_path[:i+1] + best_path[j+1:k+1][::-1] + best_path[i+1:j+1][::-1] + best_path[k+1:]
|
|
1469
|
+
case 5:
|
|
1470
|
+
new_path = best_path[:i+1] + best_path[j+1:k+1] + best_path[i+1:j+1][::-1] + best_path[k+1:]
|
|
1471
|
+
case _:
|
|
1472
|
+
new_path = best_path[:i+1] + best_path[j+1:k+1][::-1] + best_path[i+1:j+1] + best_path[k+1:]
|
|
1473
|
+
|
|
1474
|
+
dists[l] = path_length_streaming(new_path, G_func)
|
|
1475
|
+
paths[l] = new_path
|
|
1476
|
+
|
|
1477
|
+
for i in range(7):
|
|
1478
|
+
if dists[i] < best_dist:
|
|
1479
|
+
best_path, best_dist, improved = paths[i], dists[i], True
|
|
1480
|
+
|
|
1481
|
+
if improved:
|
|
1482
|
+
break
|
|
1483
|
+
|
|
1484
|
+
if improved:
|
|
1485
|
+
break
|
|
1486
|
+
|
|
1487
|
+
if improved:
|
|
1488
|
+
break
|
|
1489
|
+
|
|
1490
|
+
path = best_path[:]
|
|
1491
|
+
|
|
1492
|
+
return best_path, best_dist
|
|
1493
|
+
|
|
1494
|
+
|
|
1312
1495
|
@njit
|
|
1313
1496
|
def stich_singlet_streaming(G_func, singlet, bulk):
|
|
1314
1497
|
best_path = bulk.copy()
|
|
@@ -1433,14 +1616,17 @@ def tsp_streaming_bruteforce(G_func, perms, n):
|
|
|
1433
1616
|
|
|
1434
1617
|
|
|
1435
1618
|
@njit
|
|
1436
|
-
def tsp_symmetric_streaming_driver(G_func, is_top_level, k_neighbors, nodes, path_a, path_b, neighbor_lists):
|
|
1619
|
+
def tsp_symmetric_streaming_driver(G_func, is_top_level, k_neighbors, nodes, path_a, path_b, neighbor_lists, is_parallel):
|
|
1437
1620
|
best_path = stitch_streaming_symmetric(G_func, path_a, path_b)
|
|
1438
1621
|
|
|
1439
1622
|
if is_top_level:
|
|
1440
1623
|
best_path, _ = one_way_two_opt_streaming(best_path, G_func)
|
|
1441
1624
|
|
|
1442
1625
|
if k_neighbors > 0:
|
|
1443
|
-
|
|
1626
|
+
if is_parallel:
|
|
1627
|
+
best_path, _ = targeted_three_opt_streaming_parallel(best_path, G_func, neighbor_lists, k_neighbors)
|
|
1628
|
+
else:
|
|
1629
|
+
best_path, _ = targeted_three_opt_streaming(best_path, G_func, neighbor_lists, k_neighbors)
|
|
1444
1630
|
|
|
1445
1631
|
best_path = restitch_streaming(G_func, best_path)
|
|
1446
1632
|
|
|
@@ -1454,7 +1640,8 @@ def tsp_symmetric_streaming(
|
|
|
1454
1640
|
nodes,
|
|
1455
1641
|
k_neighbors=20,
|
|
1456
1642
|
is_top_level=True,
|
|
1457
|
-
is_parallel=True
|
|
1643
|
+
is_parallel=True,
|
|
1644
|
+
parallel_level=0
|
|
1458
1645
|
):
|
|
1459
1646
|
dtype = opencl_context.dtype
|
|
1460
1647
|
n_nodes = len(nodes)
|
|
@@ -1469,10 +1656,10 @@ def tsp_symmetric_streaming(
|
|
|
1469
1656
|
|
|
1470
1657
|
a, b = monte_carlo_loop(n_nodes)
|
|
1471
1658
|
|
|
1472
|
-
if is_parallel and (len(a) > 3) and (len(b) > 3):
|
|
1473
|
-
with
|
|
1474
|
-
f = executor.submit(tsp_symmetric_streaming, G_func, a, False, True)
|
|
1475
|
-
sol_b = tsp_symmetric_streaming(G_func, b, False, True)
|
|
1659
|
+
if is_parallel and (parallel_level < max_parallel_level) and (len(a) > 3) and (len(b) > 3):
|
|
1660
|
+
with ProcessPoolExecutor(max_workers=1) as executor:
|
|
1661
|
+
f = executor.submit(tsp_symmetric_streaming, G_func, a, False, True, parallel_level + 1)
|
|
1662
|
+
sol_b = tsp_symmetric_streaming(G_func, b, False, True, parallel_level + 1)
|
|
1476
1663
|
sol_a = f.result()
|
|
1477
1664
|
else:
|
|
1478
1665
|
sol_a = tsp_symmetric_streaming(
|
|
@@ -1507,4 +1694,4 @@ def tsp_symmetric_streaming(
|
|
|
1507
1694
|
for i in range(len(neighbor_lists)):
|
|
1508
1695
|
neighbor_lists[i] = [c[1] for c in neighbor_lists[i]]
|
|
1509
1696
|
|
|
1510
|
-
return tsp_symmetric_streaming_driver(G_func, is_top_level, k_neighbors, nodes, sol_a[0], sol_b[0], np.array(neighbor_lists))
|
|
1697
|
+
return tsp_symmetric_streaming_driver(G_func, is_top_level, k_neighbors, nodes, sol_a[0], sol_b[0], np.array(neighbor_lists), is_parallel)
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='pyqrackising',
|
|
54
|
-
version='7.4.
|
|
54
|
+
version='7.4.4',
|
|
55
55
|
author='Dan Strano',
|
|
56
56
|
author_email='stranoj@gmail.com',
|
|
57
57
|
description='Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)',
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
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
|
|
File without changes
|
|
File without changes
|