PyQrackIsing 7.4.4__tar.gz → 7.4.6__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.4/pyqrackising.egg-info → pyqrackising-7.4.6}/PKG-INFO +1 -1
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/pyproject.toml +1 -1
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/pyqrackising/tsp.py +197 -73
- {pyqrackising-7.4.4 → pyqrackising-7.4.6/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/setup.py +1 -1
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/CMakeLists.txt +0 -0
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/LICENSE.md +0 -0
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/MANIFEST.in +0 -0
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/README.md +0 -0
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/pyqrackising/__init__.py +0 -0
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/pyqrackising/maxcut_tfim.py +0 -0
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/pyqrackising/maxcut_tfim_sparse.py +0 -0
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/pyqrackising/maxcut_tfim_streaming.py +0 -0
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/pyqrackising/maxcut_tfim_util.py +0 -0
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/pyqrackising/spin_glass_solver.py +0 -0
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/pyqrackising/spin_glass_solver_sparse.py +0 -0
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/pyqrackising/spin_glass_solver_streaming.py +0 -0
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/pyqrackising/spin_glass_solver_util.py +0 -0
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/pyqrackising/tsp_maxcut.py +0 -0
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-7.4.4 → pyqrackising-7.4.6}/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.6"
|
|
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"}
|
|
@@ -10,7 +10,8 @@ import os
|
|
|
10
10
|
from scipy.sparse import lil_matrix
|
|
11
11
|
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
n_threads = os.cpu_count()
|
|
14
|
+
max_parallel_level = np.log2(n_threads)
|
|
14
15
|
|
|
15
16
|
|
|
16
17
|
# two_opt() and targeted_three_opt() written by Elara (OpenAI ChatGPT instance)
|
|
@@ -24,11 +25,10 @@ def path_length(path, G_m):
|
|
|
24
25
|
|
|
25
26
|
|
|
26
27
|
@njit
|
|
27
|
-
def one_way_two_opt(
|
|
28
|
+
def one_way_two_opt(best_path, G):
|
|
28
29
|
improved = True
|
|
29
|
-
best_path = path
|
|
30
30
|
best_dist = path_length(best_path, G)
|
|
31
|
-
path_len = len(
|
|
31
|
+
path_len = len(best_path)
|
|
32
32
|
|
|
33
33
|
while improved:
|
|
34
34
|
improved = False
|
|
@@ -40,17 +40,41 @@ def one_way_two_opt(path, G):
|
|
|
40
40
|
if new_dist < best_dist:
|
|
41
41
|
best_path, best_dist = new_path, new_dist
|
|
42
42
|
improved = True
|
|
43
|
-
|
|
43
|
+
|
|
44
|
+
return best_path, best_dist
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@njit(parallel=True)
|
|
48
|
+
def one_way_two_opt_parallel(best_path, G):
|
|
49
|
+
improved = True
|
|
50
|
+
best_dist = path_length(best_path, G)
|
|
51
|
+
paths = [best_path] * n_threads
|
|
52
|
+
dists = [best_dist] * n_threads
|
|
53
|
+
path_len = len(best_path)
|
|
54
|
+
|
|
55
|
+
while improved:
|
|
56
|
+
improved = False
|
|
57
|
+
for i in range(1, path_len - 1):
|
|
58
|
+
for j in range(i + 2, path_len, n_threads):
|
|
59
|
+
max_k = min(n_threads, path_len - j)
|
|
60
|
+
for k in prange(max_k):
|
|
61
|
+
l = j + k
|
|
62
|
+
new_path = best_path[:]
|
|
63
|
+
new_path[i:l] = best_path[l-1:i-1:-1]
|
|
64
|
+
dists[k] = path_length(new_path, G)
|
|
65
|
+
paths[k] = new_path
|
|
66
|
+
for k in range(max_k):
|
|
67
|
+
if dists[k] < best_dist:
|
|
68
|
+
best_path, best_dist, improved = paths[k], dists[k], True
|
|
44
69
|
|
|
45
70
|
return best_path, best_dist
|
|
46
71
|
|
|
47
72
|
|
|
48
73
|
@njit
|
|
49
|
-
def anchored_two_opt(
|
|
74
|
+
def anchored_two_opt(best_path, G):
|
|
50
75
|
improved = True
|
|
51
|
-
best_path = path
|
|
52
76
|
best_dist = path_length(best_path, G)
|
|
53
|
-
path_len = len(
|
|
77
|
+
path_len = len(best_path)
|
|
54
78
|
|
|
55
79
|
while improved:
|
|
56
80
|
improved = False
|
|
@@ -62,39 +86,41 @@ def anchored_two_opt(path, G):
|
|
|
62
86
|
if new_dist < best_dist:
|
|
63
87
|
best_path, best_dist = new_path, new_dist
|
|
64
88
|
improved = True
|
|
65
|
-
path = best_path
|
|
66
89
|
|
|
67
90
|
return best_path, best_dist
|
|
68
91
|
|
|
69
92
|
|
|
70
|
-
@njit
|
|
71
|
-
def
|
|
93
|
+
@njit(parallel=True)
|
|
94
|
+
def anchored_two_opt_parallel(best_path, G):
|
|
72
95
|
improved = True
|
|
73
|
-
best_path = path
|
|
74
96
|
best_dist = path_length(best_path, G)
|
|
75
|
-
|
|
97
|
+
paths = [best_path] * n_threads
|
|
98
|
+
dists = [best_dist] * n_threads
|
|
99
|
+
path_len = len(best_path)
|
|
76
100
|
|
|
77
101
|
while improved:
|
|
78
102
|
improved = False
|
|
79
|
-
for i in range(
|
|
80
|
-
for j in range(i + 2, path_len
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
103
|
+
for i in range(2, path_len - 1):
|
|
104
|
+
for j in range(i + 2, path_len, n_threads):
|
|
105
|
+
max_k = min(n_threads, path_len - j)
|
|
106
|
+
for k in prange(max_k):
|
|
107
|
+
l = j + k
|
|
108
|
+
new_path = best_path[:]
|
|
109
|
+
new_path[i:l] = best_path[l-1:i-1:-1]
|
|
110
|
+
dists[k] = path_length(new_path, G)
|
|
111
|
+
paths[k] = new_path
|
|
112
|
+
for k in range(max_k):
|
|
113
|
+
if dists[k] < best_dist:
|
|
114
|
+
best_path, best_dist, improved = paths[k], dists[k], True
|
|
88
115
|
|
|
89
116
|
return best_path, best_dist
|
|
90
117
|
|
|
91
118
|
|
|
92
119
|
@njit
|
|
93
|
-
def two_opt(
|
|
120
|
+
def two_opt(best_path, G):
|
|
94
121
|
improved = True
|
|
95
|
-
best_path = path
|
|
96
122
|
best_dist = path_length(best_path, G)
|
|
97
|
-
path_len = len(
|
|
123
|
+
path_len = len(best_path)
|
|
98
124
|
|
|
99
125
|
while improved:
|
|
100
126
|
improved = False
|
|
@@ -106,7 +132,32 @@ def two_opt(path, G):
|
|
|
106
132
|
if new_dist < best_dist:
|
|
107
133
|
best_path, best_dist = new_path, new_dist
|
|
108
134
|
improved = True
|
|
109
|
-
|
|
135
|
+
|
|
136
|
+
return best_path, best_dist
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
@njit(parallel=True)
|
|
140
|
+
def two_opt_parallel(best_path, G):
|
|
141
|
+
improved = True
|
|
142
|
+
best_dist = path_length(best_path, G)
|
|
143
|
+
paths = [best_path] * n_threads
|
|
144
|
+
dists = [best_dist] * n_threads
|
|
145
|
+
path_len = len(best_path)
|
|
146
|
+
|
|
147
|
+
while improved:
|
|
148
|
+
improved = False
|
|
149
|
+
for i in range(1, path_len - 2):
|
|
150
|
+
for j in range(i + 2, path_len - 1, n_threads):
|
|
151
|
+
max_k = min(n_threads, path_len - (j + 1))
|
|
152
|
+
for k in prange(max_k):
|
|
153
|
+
l = j + k
|
|
154
|
+
new_path = best_path[:]
|
|
155
|
+
new_path[i:l] = best_path[l-1:i-1:-1]
|
|
156
|
+
dists[k] = path_length(new_path, G)
|
|
157
|
+
paths[k] = new_path
|
|
158
|
+
for k in range(max_k):
|
|
159
|
+
if dists[k] < best_dist:
|
|
160
|
+
best_path, best_dist, improved = paths[k], dists[k], True
|
|
110
161
|
|
|
111
162
|
return best_path, best_dist
|
|
112
163
|
|
|
@@ -229,7 +280,7 @@ def targeted_three_opt_parallel(path, W, neighbor_lists, k_neighbors=20):
|
|
|
229
280
|
case _:
|
|
230
281
|
new_path = best_path[:i+1] + best_path[j+1:k+1][::-1] + best_path[i+1:j+1] + best_path[k+1:]
|
|
231
282
|
|
|
232
|
-
dists[l] = path_length(
|
|
283
|
+
dists[l] = path_length(new_path, W)
|
|
233
284
|
paths[l] = new_path
|
|
234
285
|
|
|
235
286
|
for i in range(7):
|
|
@@ -520,9 +571,6 @@ def tsp_symmetric_driver(G_m, is_cyclic, is_top_level, start_node, end_node, k_n
|
|
|
520
571
|
path_a = [a[x] for x in sol_a[0]]
|
|
521
572
|
path_b = [b[x] for x in sol_b[0]]
|
|
522
573
|
|
|
523
|
-
restitch(G_m, path_a, True)
|
|
524
|
-
restitch(G_m, path_b, True)
|
|
525
|
-
|
|
526
574
|
if start_node is None:
|
|
527
575
|
best_path = stitch_symmetric(G_m, path_a, path_b)
|
|
528
576
|
else:
|
|
@@ -540,13 +588,25 @@ def tsp_symmetric_driver(G_m, is_cyclic, is_top_level, start_node, end_node, k_n
|
|
|
540
588
|
if is_top_level:
|
|
541
589
|
if is_cyclic:
|
|
542
590
|
best_path += [best_path[0]]
|
|
543
|
-
|
|
591
|
+
if is_parallel:
|
|
592
|
+
best_path, _ = two_opt_parallel(best_path, G_m)
|
|
593
|
+
else:
|
|
594
|
+
best_path, _ = two_opt(best_path, G_m)
|
|
544
595
|
elif not end_node is None:
|
|
545
|
-
|
|
596
|
+
if is_parallel:
|
|
597
|
+
best_path, _ = two_opt_parallel(best_path, G_m)
|
|
598
|
+
else:
|
|
599
|
+
best_path, _ = two_opt(best_path, G_m)
|
|
546
600
|
elif not start_node is None:
|
|
547
|
-
|
|
601
|
+
if is_parallel:
|
|
602
|
+
best_path, _ = anchored_two_opt_parallel(best_path, G_m)
|
|
603
|
+
else:
|
|
604
|
+
best_path, _ = anchored_two_opt(best_path, G_m)
|
|
548
605
|
else:
|
|
549
|
-
|
|
606
|
+
if is_parallel:
|
|
607
|
+
best_path, _ = one_way_two_opt_parallel(best_path, G_m)
|
|
608
|
+
else:
|
|
609
|
+
best_path, _ = one_way_two_opt(best_path, G_m)
|
|
550
610
|
|
|
551
611
|
if k_neighbors > 0:
|
|
552
612
|
if is_parallel:
|
|
@@ -554,10 +614,7 @@ def tsp_symmetric_driver(G_m, is_cyclic, is_top_level, start_node, end_node, k_n
|
|
|
554
614
|
else:
|
|
555
615
|
best_path, _ = targeted_three_opt(best_path, G_m, neighbor_lists, k_neighbors)
|
|
556
616
|
|
|
557
|
-
|
|
558
|
-
# and this is top level,
|
|
559
|
-
# so correct segments of 4 to 7.
|
|
560
|
-
restitch(G_m, best_path, True)
|
|
617
|
+
best_path = restitch(G_m, best_path, True)
|
|
561
618
|
|
|
562
619
|
best_weight = path_length(best_path, G_m)
|
|
563
620
|
|
|
@@ -689,7 +746,7 @@ def tsp_symmetric(
|
|
|
689
746
|
neighbor_lists[i].sort(key=lambda x: x[0])
|
|
690
747
|
|
|
691
748
|
for i in range(len(neighbor_lists)):
|
|
692
|
-
neighbor_lists[i] = [
|
|
749
|
+
neighbor_lists[i] = [x[1] for x in neighbor_lists[i]]
|
|
693
750
|
|
|
694
751
|
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)
|
|
695
752
|
|
|
@@ -711,13 +768,10 @@ def tsp_asymmetric_brute_force_driver(G_m, n_nodes, nodes, is_cyclic):
|
|
|
711
768
|
|
|
712
769
|
|
|
713
770
|
@njit
|
|
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):
|
|
771
|
+
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, neighbor_lists, is_parallel):
|
|
715
772
|
path_a = [a[x] for x in sol_a[0]]
|
|
716
773
|
path_b = [b[x] for x in sol_b[0]]
|
|
717
774
|
|
|
718
|
-
restitch(G_m, path_a, False)
|
|
719
|
-
restitch(G_m, path_b, False)
|
|
720
|
-
|
|
721
775
|
if start_node is None:
|
|
722
776
|
best_path = stitch_asymmetric(G_m, path_a, path_b)
|
|
723
777
|
else:
|
|
@@ -729,41 +783,41 @@ def tsp_asymmetric_driver(G_m, is_reversed, is_cyclic, is_top_level, start_node,
|
|
|
729
783
|
if is_top_level:
|
|
730
784
|
if is_cyclic:
|
|
731
785
|
best_path += [best_path[0]]
|
|
732
|
-
final_path, best_weight = two_opt(best_path.copy(), G_m)
|
|
786
|
+
final_path, best_weight = two_opt_parallel(best_path.copy(), G_m) if is_parallel else two_opt(best_path.copy(), G_m)
|
|
733
787
|
best_path.reverse()
|
|
734
|
-
path, weight = two_opt(best_path, G_m)
|
|
788
|
+
path, weight = two_opt_parallel(best_path, G_m) if is_parallel else two_opt(best_path, G_m)
|
|
735
789
|
if weight < best_weight:
|
|
736
790
|
final_path, best_weight = path, weight
|
|
737
791
|
elif not end_node is None:
|
|
738
|
-
final_path, best_weight = two_opt(best_path.copy(), G_m)
|
|
792
|
+
final_path, best_weight = two_opt_parallel(best_path.copy(), G_m) if is_parallel else two_opt(best_path.copy(), G_m)
|
|
739
793
|
best_path.reverse()
|
|
740
|
-
path, weight = two_opt(best_path, G_m)
|
|
794
|
+
path, weight = two_opt_parallel(best_path, G_m) if is_parallel else two_opt(best_path, G_m)
|
|
741
795
|
if weight < best_weight:
|
|
742
796
|
final_path, best_weight = path, weight
|
|
743
797
|
elif not start_node is None:
|
|
744
|
-
final_path, best_weight = anchored_two_opt(best_path.copy(), G_m)
|
|
798
|
+
final_path, best_weight = anchored_two_opt_parallel(best_path.copy(), G_m) if is_parallel else anchored_two_opt(best_path.copy(), G_m)
|
|
745
799
|
best_path.reverse()
|
|
746
|
-
path, weight =
|
|
800
|
+
path, weight = two_opt_parallel(best_path, G_m) if is_parallel else two_opt(best_path, G_m)
|
|
747
801
|
if weight < best_weight:
|
|
748
802
|
final_path, best_weight = path, weight
|
|
749
803
|
else:
|
|
750
|
-
final_path, best_weight = one_way_two_opt(best_path.copy(), G_m)
|
|
804
|
+
final_path, best_weight = one_way_two_opt_parallel(best_path.copy(), G_m) if is_parallel else one_way_two_opt(best_path.copy(), G_m)
|
|
751
805
|
best_path.reverse()
|
|
752
|
-
path, weight = one_way_two_opt(best_path, G_m)
|
|
806
|
+
path, weight = one_way_two_opt_parallel(best_path, G_m) if is_parallel else one_way_two_opt(best_path, G_m)
|
|
753
807
|
if weight < best_weight:
|
|
754
808
|
final_path, best_weight = path, weight
|
|
755
809
|
|
|
756
810
|
if k_neighbors > 0:
|
|
757
811
|
best_path = final_path
|
|
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)
|
|
812
|
+
final_path, best_weight = targeted_three_opt_parallel(best_path.copy(), G_m, neighbor_lists, k_neighbors) if is_parallel else targeted_three_opt(best_path.copy(), G_m, neighbor_lists, k_neighbors)
|
|
759
813
|
best_path.reverse()
|
|
760
|
-
path, weight = targeted_three_opt_parallel(best_path
|
|
814
|
+
path, weight = targeted_three_opt_parallel(best_path, G_m, neighbor_lists, k_neighbors) if is_parallel else targeted_three_opt(best_path, G_m, neighbor_lists, k_neighbors)
|
|
761
815
|
if weight < best_weight:
|
|
762
816
|
final_path, best_weight = path, weight
|
|
763
817
|
else:
|
|
764
818
|
final_path = best_path
|
|
765
819
|
|
|
766
|
-
final_path = restitch(G_m, final_path,
|
|
820
|
+
final_path = restitch(G_m, final_path, False)
|
|
767
821
|
|
|
768
822
|
best_weight = path_length(final_path, G_m)
|
|
769
823
|
|
|
@@ -887,7 +941,24 @@ def tsp_asymmetric(
|
|
|
887
941
|
is_parallel=False
|
|
888
942
|
)
|
|
889
943
|
|
|
890
|
-
|
|
944
|
+
neighbor_lists = [[0]]
|
|
945
|
+
if k_neighbors > 0:
|
|
946
|
+
# Precompute nearest neighbors for each node
|
|
947
|
+
neighbor_lists = [
|
|
948
|
+
([(float("inf"), 0)] * k_neighbors) for i in range(n_nodes)
|
|
949
|
+
]
|
|
950
|
+
|
|
951
|
+
for i in range(n_nodes):
|
|
952
|
+
for j in range(n_nodes):
|
|
953
|
+
val = G_m[i, j]
|
|
954
|
+
if val < neighbor_lists[i][-1][0]:
|
|
955
|
+
neighbor_lists[i][-1] = (val, j)
|
|
956
|
+
neighbor_lists[i].sort(key=lambda x: x[0])
|
|
957
|
+
|
|
958
|
+
for i in range(len(neighbor_lists)):
|
|
959
|
+
neighbor_lists[i] = [x[1] for x in neighbor_lists[i]]
|
|
960
|
+
|
|
961
|
+
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, np.array(neighbor_lists), is_parallel)
|
|
891
962
|
|
|
892
963
|
|
|
893
964
|
@njit
|
|
@@ -924,11 +995,10 @@ def path_length_sparse(path, G_data, G_rows, G_cols):
|
|
|
924
995
|
|
|
925
996
|
|
|
926
997
|
@njit
|
|
927
|
-
def one_way_two_opt_sparse(
|
|
998
|
+
def one_way_two_opt_sparse(best_path, G_data, G_rows, G_cols):
|
|
928
999
|
improved = True
|
|
929
|
-
best_path = path
|
|
930
1000
|
best_dist = path_length_sparse(best_path, G_data, G_rows, G_cols)
|
|
931
|
-
path_len = len(
|
|
1001
|
+
path_len = len(best_path)
|
|
932
1002
|
|
|
933
1003
|
while improved:
|
|
934
1004
|
improved = False
|
|
@@ -940,7 +1010,32 @@ def one_way_two_opt_sparse(path, G_data, G_rows, G_cols):
|
|
|
940
1010
|
if new_dist < best_dist:
|
|
941
1011
|
best_path, best_dist = new_path, new_dist
|
|
942
1012
|
improved = True
|
|
943
|
-
|
|
1013
|
+
|
|
1014
|
+
return best_path, best_dist
|
|
1015
|
+
|
|
1016
|
+
|
|
1017
|
+
@njit(parallel=True)
|
|
1018
|
+
def one_way_two_opt_sparse_parallel(best_path, G_data, G_rows, G_cols):
|
|
1019
|
+
improved = True
|
|
1020
|
+
best_dist = path_length_sparse(best_path, G_data, G_rows, G_cols)
|
|
1021
|
+
paths = [best_path] * n_threads
|
|
1022
|
+
dists = [best_dist] * n_threads
|
|
1023
|
+
path_len = len(best_path)
|
|
1024
|
+
|
|
1025
|
+
while improved:
|
|
1026
|
+
improved = False
|
|
1027
|
+
for i in range(1, path_len - 1):
|
|
1028
|
+
for j in range(i + 2, path_len, n_threads):
|
|
1029
|
+
max_k = min(n_threads, path_len - j)
|
|
1030
|
+
for k in prange(max_k):
|
|
1031
|
+
l = j + k
|
|
1032
|
+
new_path = best_path[:]
|
|
1033
|
+
new_path[i:l] = best_path[l-1:i-1:-1]
|
|
1034
|
+
dists[k] = path_length_sparse(new_path, G_data, G_rows, G_cols)
|
|
1035
|
+
paths[k] = new_path
|
|
1036
|
+
for k in range(max_k):
|
|
1037
|
+
if dists[k] < best_dist:
|
|
1038
|
+
best_path, best_dist, improved = paths[k], dists[k], True
|
|
944
1039
|
|
|
945
1040
|
return best_path, best_dist
|
|
946
1041
|
|
|
@@ -1242,7 +1337,10 @@ def tsp_symmetric_sparse_driver(G_data, G_rows, G_cols, is_top_level, k_neighbor
|
|
|
1242
1337
|
best_path = stitch_sparse_symmetric(G_data, G_rows, G_cols, path_a, path_b)
|
|
1243
1338
|
|
|
1244
1339
|
if is_top_level:
|
|
1245
|
-
|
|
1340
|
+
if is_parallel:
|
|
1341
|
+
best_path, _ = one_way_two_opt_sparse_parallel(best_path, G_data, G_rows, G_cols)
|
|
1342
|
+
else:
|
|
1343
|
+
best_path, _ = one_way_two_opt_sparse(best_path, G_data, G_rows, G_cols)
|
|
1246
1344
|
|
|
1247
1345
|
if k_neighbors > 0:
|
|
1248
1346
|
if is_parallel:
|
|
@@ -1291,8 +1389,8 @@ def tsp_symmetric_sparse(
|
|
|
1291
1389
|
|
|
1292
1390
|
if is_parallel and (parallel_level < max_parallel_level) and (len(a) > 3) and (len(b) > 3):
|
|
1293
1391
|
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)
|
|
1392
|
+
f = executor.submit(tsp_symmetric_sparse, G_a, 0, False, True, parallel_level + 1)
|
|
1393
|
+
sol_b = tsp_symmetric_sparse(G_b, 0, False, True, parallel_level + 1)
|
|
1296
1394
|
sol_a = f.result()
|
|
1297
1395
|
else:
|
|
1298
1396
|
sol_a = tsp_symmetric_sparse(
|
|
@@ -1323,7 +1421,7 @@ def tsp_symmetric_sparse(
|
|
|
1323
1421
|
neighbor_lists[i].sort(key=lambda x: x[0])
|
|
1324
1422
|
|
|
1325
1423
|
for i in range(len(neighbor_lists)):
|
|
1326
|
-
neighbor_lists[i] = [
|
|
1424
|
+
neighbor_lists[i] = [x[1] for x in neighbor_lists[i]]
|
|
1327
1425
|
|
|
1328
1426
|
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)
|
|
1329
1427
|
|
|
@@ -1338,11 +1436,10 @@ def path_length_streaming(path, G_func):
|
|
|
1338
1436
|
|
|
1339
1437
|
|
|
1340
1438
|
@njit
|
|
1341
|
-
def one_way_two_opt_streaming(
|
|
1439
|
+
def one_way_two_opt_streaming(best_path, G_func):
|
|
1342
1440
|
improved = True
|
|
1343
|
-
best_path = path
|
|
1344
1441
|
best_dist = path_length_streaming(best_path, G_func)
|
|
1345
|
-
path_len = len(
|
|
1442
|
+
path_len = len(best_path)
|
|
1346
1443
|
|
|
1347
1444
|
while improved:
|
|
1348
1445
|
improved = False
|
|
@@ -1352,9 +1449,33 @@ def one_way_two_opt_streaming(path, G_func):
|
|
|
1352
1449
|
new_path[i:j] = best_path[j-1:i-1:-1]
|
|
1353
1450
|
new_dist = path_length_streaming(new_path, G_func)
|
|
1354
1451
|
if new_dist < best_dist:
|
|
1355
|
-
best_path, best_dist = new_path, new_dist
|
|
1356
|
-
|
|
1357
|
-
|
|
1452
|
+
best_path, best_dist, improved = new_path, new_dist, True
|
|
1453
|
+
|
|
1454
|
+
return best_path, best_dist
|
|
1455
|
+
|
|
1456
|
+
|
|
1457
|
+
@njit(parallel=True)
|
|
1458
|
+
def one_way_two_opt_streaming_parallel(best_path, G_func):
|
|
1459
|
+
improved = True
|
|
1460
|
+
best_dist = path_length_streaming(best_path, G_func)
|
|
1461
|
+
paths = [best_path] * n_threads
|
|
1462
|
+
dists = [best_dist] * n_threads
|
|
1463
|
+
path_len = len(best_path)
|
|
1464
|
+
|
|
1465
|
+
while improved:
|
|
1466
|
+
improved = False
|
|
1467
|
+
for i in range(1, path_len - 1):
|
|
1468
|
+
for j in range(i + 2, path_len, n_threads):
|
|
1469
|
+
max_k = min(n_threads, path_len - j)
|
|
1470
|
+
for k in prange(max_k):
|
|
1471
|
+
l = j + k
|
|
1472
|
+
new_path = best_path[:]
|
|
1473
|
+
new_path[i:l] = best_path[l-1:i-1:-1]
|
|
1474
|
+
dists[k] = path_length_streaming(new_path, G_func)
|
|
1475
|
+
paths[k] = new_path
|
|
1476
|
+
for k in range(max_k):
|
|
1477
|
+
if dists[k] < best_dist:
|
|
1478
|
+
best_path, best_dist, improved = paths[k], dists[k], True
|
|
1358
1479
|
|
|
1359
1480
|
return best_path, best_dist
|
|
1360
1481
|
|
|
@@ -1620,7 +1741,10 @@ def tsp_symmetric_streaming_driver(G_func, is_top_level, k_neighbors, nodes, pat
|
|
|
1620
1741
|
best_path = stitch_streaming_symmetric(G_func, path_a, path_b)
|
|
1621
1742
|
|
|
1622
1743
|
if is_top_level:
|
|
1623
|
-
|
|
1744
|
+
if is_parallel:
|
|
1745
|
+
best_path, _ = one_way_two_opt_streaming_parallel(best_path, G_func)
|
|
1746
|
+
else:
|
|
1747
|
+
best_path, _ = one_way_two_opt_streaming(best_path, G_func)
|
|
1624
1748
|
|
|
1625
1749
|
if k_neighbors > 0:
|
|
1626
1750
|
if is_parallel:
|
|
@@ -1658,8 +1782,8 @@ def tsp_symmetric_streaming(
|
|
|
1658
1782
|
|
|
1659
1783
|
if is_parallel and (parallel_level < max_parallel_level) and (len(a) > 3) and (len(b) > 3):
|
|
1660
1784
|
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)
|
|
1785
|
+
f = executor.submit(tsp_symmetric_streaming, G_func, a, 0, False, True, parallel_level + 1)
|
|
1786
|
+
sol_b = tsp_symmetric_streaming(G_func, b, 0, False, True, parallel_level + 1)
|
|
1663
1787
|
sol_a = f.result()
|
|
1664
1788
|
else:
|
|
1665
1789
|
sol_a = tsp_symmetric_streaming(
|
|
@@ -1692,6 +1816,6 @@ def tsp_symmetric_streaming(
|
|
|
1692
1816
|
neighbor_lists[i].sort(key=lambda x: x[0])
|
|
1693
1817
|
|
|
1694
1818
|
for i in range(len(neighbor_lists)):
|
|
1695
|
-
neighbor_lists[i] = [
|
|
1819
|
+
neighbor_lists[i] = [x[1] for x in neighbor_lists[i]]
|
|
1696
1820
|
|
|
1697
1821
|
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.6',
|
|
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
|