PyQrackIsing 7.4.3__tar.gz → 7.4.5__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 (30) hide show
  1. {pyqrackising-7.4.3/pyqrackising.egg-info → pyqrackising-7.4.5}/PKG-INFO +1 -1
  2. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/pyproject.toml +1 -1
  3. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/pyqrackising/tsp.py +391 -87
  4. {pyqrackising-7.4.3 → pyqrackising-7.4.5/pyqrackising.egg-info}/PKG-INFO +1 -1
  5. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/setup.py +1 -1
  6. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/CMakeLists.txt +0 -0
  7. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/LICENSE.md +0 -0
  8. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/MANIFEST.in +0 -0
  9. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/README.md +0 -0
  10. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/pyqrackising/__init__.py +0 -0
  11. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/pyqrackising/_pyqrack_ising.cpp +0 -0
  12. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
  13. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/pyqrackising/generate_tfim_samples.py +0 -0
  14. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/pyqrackising/kernels.cl +0 -0
  15. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/pyqrackising/maxcut_tfim.py +0 -0
  16. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/pyqrackising/maxcut_tfim_sparse.py +0 -0
  17. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/pyqrackising/maxcut_tfim_streaming.py +0 -0
  18. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/pyqrackising/maxcut_tfim_util.py +0 -0
  19. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/pyqrackising/spin_glass_solver.py +0 -0
  20. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/pyqrackising/spin_glass_solver_sparse.py +0 -0
  21. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/pyqrackising/spin_glass_solver_streaming.py +0 -0
  22. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/pyqrackising/spin_glass_solver_util.py +0 -0
  23. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/pyqrackising/tfim_magnetization.py +0 -0
  24. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/pyqrackising/tfim_square_magnetization.py +0 -0
  25. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/pyqrackising/tsp_maxcut.py +0 -0
  26. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/pyqrackising.egg-info/SOURCES.txt +0 -0
  27. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/pyqrackising.egg-info/dependency_links.txt +0 -0
  28. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/pyqrackising.egg-info/not-zip-safe +0 -0
  29. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/pyqrackising.egg-info/top_level.txt +0 -0
  30. {pyqrackising-7.4.3 → pyqrackising-7.4.5}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 7.4.3
3
+ Version: 7.4.5
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
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
12
12
 
13
13
  [project]
14
14
  name = "pyqrackising"
15
- version = "7.4.3"
15
+ version = "7.4.5"
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"}
@@ -4,13 +4,14 @@ 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())
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(path, G):
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(path)
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
- path = best_path
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(path, G):
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(path)
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 reversed_anchored_two_opt(path, G):
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
- path_len = len(path)
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(1, path_len - 2):
80
- for j in range(i + 2, path_len - 1):
81
- new_path = best_path[:]
82
- new_path[i:j] = best_path[j-1:i-1:-1]
83
- new_dist = path_length(new_path, G)
84
- if new_dist < best_dist:
85
- best_path, best_dist = new_path, new_dist
86
- improved = True
87
- path = best_path
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(path, G):
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(path)
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
- path = best_path
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
 
@@ -136,9 +187,6 @@ def targeted_three_opt(path, W, neighbor_lists, k_neighbors=20):
136
187
  if k <= j or k >= n-1:
137
188
  continue
138
189
 
139
- # Extract indices
140
- A, B, C, D, E, F = path[i], path[i+1], path[j], path[j+1], path[k], path[k+1]
141
-
142
190
  # 7 unique cases (same as brute force, but restricted)
143
191
  new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:]
144
192
  dist = path_length(new_path, W)
@@ -193,6 +241,66 @@ def targeted_three_opt(path, W, neighbor_lists, k_neighbors=20):
193
241
  return best_path, best_dist
194
242
 
195
243
 
244
+ @njit(parallel=True)
245
+ def targeted_three_opt_parallel(path, W, neighbor_lists, k_neighbors=20):
246
+ n = len(path)
247
+ best_path = path[:]
248
+ best_dist = path_length(best_path, W)
249
+ paths = [best_path] * 7
250
+ dists = [best_dist] * 7
251
+ improved = True
252
+
253
+ while improved:
254
+ improved = False
255
+ for i in range(n - 5):
256
+ for _j in range(k_neighbors):
257
+ j = neighbor_lists[path[i], _j]
258
+ if j <= i or j >= n-3:
259
+ continue
260
+ for _k in range(k_neighbors):
261
+ k = neighbor_lists[path[j], _k]
262
+ if k <= j or k >= n-1:
263
+ continue
264
+
265
+ # 7 unique cases (same as brute force, but restricted)
266
+ for l in prange(7):
267
+ match l:
268
+ case 0:
269
+ new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:]
270
+ case 1:
271
+ new_path = best_path[:j+1] + best_path[j+1:k+1][::-1] + best_path[k+1:]
272
+ case 2:
273
+ new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:k+1][::-1] + best_path[k+1:]
274
+ case 3:
275
+ new_path = best_path[:i+1] + best_path[j+1:k+1] + best_path[i+1:j+1] + best_path[k+1:]
276
+ case 4:
277
+ new_path = best_path[:i+1] + best_path[j+1:k+1][::-1] + best_path[i+1:j+1][::-1] + best_path[k+1:]
278
+ case 5:
279
+ new_path = best_path[:i+1] + best_path[j+1:k+1] + best_path[i+1:j+1][::-1] + best_path[k+1:]
280
+ case _:
281
+ new_path = best_path[:i+1] + best_path[j+1:k+1][::-1] + best_path[i+1:j+1] + best_path[k+1:]
282
+
283
+ dists[l] = path_length(new_path, W)
284
+ paths[l] = new_path
285
+
286
+ for i in range(7):
287
+ if dists[i] < best_dist:
288
+ best_path, best_dist, improved = paths[i], dists[i], True
289
+
290
+ if improved:
291
+ break
292
+
293
+ if improved:
294
+ break
295
+
296
+ if improved:
297
+ break
298
+
299
+ path = best_path[:]
300
+
301
+ return best_path, best_dist
302
+
303
+
196
304
  @njit
197
305
  def init_G_a_b(G_m, a, b):
198
306
  n_a_nodes = len(a)
@@ -459,13 +567,10 @@ def tsp_symmetric_brute_force_driver(G_m, n_nodes, nodes, is_cyclic):
459
567
 
460
568
 
461
569
  @njit
462
- 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):
570
+ 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):
463
571
  path_a = [a[x] for x in sol_a[0]]
464
572
  path_b = [b[x] for x in sol_b[0]]
465
573
 
466
- restitch(G_m, path_a, True)
467
- restitch(G_m, path_b, True)
468
-
469
574
  if start_node is None:
470
575
  best_path = stitch_symmetric(G_m, path_a, path_b)
471
576
  else:
@@ -483,21 +588,33 @@ def tsp_symmetric_driver(G_m, is_cyclic, is_top_level, start_node, end_node, k_n
483
588
  if is_top_level:
484
589
  if is_cyclic:
485
590
  best_path += [best_path[0]]
486
- best_path, _ = two_opt(best_path, G_m)
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)
487
595
  elif not end_node is None:
488
- best_path, _ = two_opt(best_path, G_m)
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)
489
600
  elif not start_node is None:
490
- best_path, _ = anchored_two_opt(best_path, G_m)
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)
491
605
  else:
492
- best_path, _ = one_way_two_opt(best_path, G_m)
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)
493
610
 
494
611
  if k_neighbors > 0:
495
- best_path, _ = targeted_three_opt(best_path, G_m, neighbor_lists, k_neighbors)
612
+ if is_parallel:
613
+ best_path, _ = targeted_three_opt_parallel(best_path, G_m, neighbor_lists, k_neighbors)
614
+ else:
615
+ best_path, _ = targeted_three_opt(best_path, G_m, neighbor_lists, k_neighbors)
496
616
 
497
- # We just corrected segments of 2 and 3,
498
- # and this is top level,
499
- # so correct segments of 4 to 7.
500
- restitch(G_m, best_path, True)
617
+ best_path = restitch(G_m, best_path, True)
501
618
 
502
619
  best_weight = path_length(best_path, G_m)
503
620
 
@@ -629,9 +746,9 @@ def tsp_symmetric(
629
746
  neighbor_lists[i].sort(key=lambda x: x[0])
630
747
 
631
748
  for i in range(len(neighbor_lists)):
632
- neighbor_lists[i] = [c[1] for c in neighbor_lists[i]]
749
+ neighbor_lists[i] = [x[1] for x in neighbor_lists[i]]
633
750
 
634
- 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))
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)
635
752
 
636
753
 
637
754
  @njit
@@ -651,13 +768,10 @@ def tsp_asymmetric_brute_force_driver(G_m, n_nodes, nodes, is_cyclic):
651
768
 
652
769
 
653
770
  @njit
654
- 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):
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):
655
772
  path_a = [a[x] for x in sol_a[0]]
656
773
  path_b = [b[x] for x in sol_b[0]]
657
774
 
658
- restitch(G_m, path_a, False)
659
- restitch(G_m, path_b, False)
660
-
661
775
  if start_node is None:
662
776
  best_path = stitch_asymmetric(G_m, path_a, path_b)
663
777
  else:
@@ -669,41 +783,41 @@ def tsp_asymmetric_driver(G_m, is_reversed, is_cyclic, is_top_level, start_node,
669
783
  if is_top_level:
670
784
  if is_cyclic:
671
785
  best_path += [best_path[0]]
672
- 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)
673
787
  best_path.reverse()
674
- 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)
675
789
  if weight < best_weight:
676
790
  final_path, best_weight = path, weight
677
791
  elif not end_node is None:
678
- 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)
679
793
  best_path.reverse()
680
- 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)
681
795
  if weight < best_weight:
682
796
  final_path, best_weight = path, weight
683
797
  elif not start_node is None:
684
- 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)
685
799
  best_path.reverse()
686
- path, weight = reversed_anchored_two_opt(best_path, G_m)
800
+ path, weight = two_opt_parallel(best_path, G_m) if is_parallel else two_opt(best_path, G_m)
687
801
  if weight < best_weight:
688
802
  final_path, best_weight = path, weight
689
803
  else:
690
- 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)
691
805
  best_path.reverse()
692
- 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)
693
807
  if weight < best_weight:
694
808
  final_path, best_weight = path, weight
695
809
 
696
810
  if k_neighbors > 0:
697
811
  best_path = final_path
698
- final_path, best_weight = 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)
699
813
  best_path.reverse()
700
- path, weight = targeted_three_opt(best_path.copy(), G_m, k_neighbors)
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)
701
815
  if weight < best_weight:
702
816
  final_path, best_weight = path, weight
703
817
  else:
704
818
  final_path = best_path
705
819
 
706
- final_path = restitch(G_m, final_path, True)
820
+ final_path = restitch(G_m, final_path, False)
707
821
 
708
822
  best_weight = path_length(final_path, G_m)
709
823
 
@@ -827,7 +941,24 @@ def tsp_asymmetric(
827
941
  is_parallel=False
828
942
  )
829
943
 
830
- 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)
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)
831
962
 
832
963
 
833
964
  @njit
@@ -864,11 +995,10 @@ def path_length_sparse(path, G_data, G_rows, G_cols):
864
995
 
865
996
 
866
997
  @njit
867
- def one_way_two_opt_sparse(path, G_data, G_rows, G_cols):
998
+ def one_way_two_opt_sparse(best_path, G_data, G_rows, G_cols):
868
999
  improved = True
869
- best_path = path
870
1000
  best_dist = path_length_sparse(best_path, G_data, G_rows, G_cols)
871
- path_len = len(path)
1001
+ path_len = len(best_path)
872
1002
 
873
1003
  while improved:
874
1004
  improved = False
@@ -880,7 +1010,32 @@ def one_way_two_opt_sparse(path, G_data, G_rows, G_cols):
880
1010
  if new_dist < best_dist:
881
1011
  best_path, best_dist = new_path, new_dist
882
1012
  improved = True
883
- path = best_path
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
884
1039
 
885
1040
  return best_path, best_dist
886
1041
 
@@ -904,9 +1059,6 @@ def targeted_three_opt_sparse(path, G_data, G_rows, G_cols, neighbor_lists, k_ne
904
1059
  if k <= j or k >= n-1:
905
1060
  continue
906
1061
 
907
- # Extract indices
908
- A, B, C, D, E, F = path[i], path[i+1], path[j], path[j+1], path[k], path[k+1]
909
-
910
1062
  # 7 unique cases (same as brute force, but restricted)
911
1063
  new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:]
912
1064
  dist = path_length_sparse(new_path, G_data, G_rows, G_cols)
@@ -961,6 +1113,66 @@ def targeted_three_opt_sparse(path, G_data, G_rows, G_cols, neighbor_lists, k_ne
961
1113
  return best_path, best_dist
962
1114
 
963
1115
 
1116
+ @njit(parallel=True)
1117
+ def targeted_three_opt_sparse_parallel(path, G_data, G_rows, G_cols, neighbor_lists, k_neighbors=20):
1118
+ n = len(path)
1119
+ best_path = path[:]
1120
+ best_dist = path_length_sparse(best_path, G_data, G_rows, G_cols)
1121
+ paths = [best_path] * 7
1122
+ dists = [best_dist] * 7
1123
+ improved = True
1124
+
1125
+ while improved:
1126
+ improved = False
1127
+ for i in range(n - 5):
1128
+ for _j in range(k_neighbors):
1129
+ j = neighbor_lists[path[i], _j]
1130
+ if j <= i or j >= n-3:
1131
+ continue
1132
+ for _k in range(k_neighbors):
1133
+ k = neighbor_lists[path[j], _k]
1134
+ if k <= j or k >= n-1:
1135
+ continue
1136
+
1137
+ # 7 unique cases (same as brute force, but restricted)
1138
+ for l in prange(7):
1139
+ match l:
1140
+ case 0:
1141
+ new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:]
1142
+ case 1:
1143
+ new_path = best_path[:j+1] + best_path[j+1:k+1][::-1] + best_path[k+1:]
1144
+ case 2:
1145
+ new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:k+1][::-1] + best_path[k+1:]
1146
+ case 3:
1147
+ new_path = best_path[:i+1] + best_path[j+1:k+1] + best_path[i+1:j+1] + best_path[k+1:]
1148
+ case 4:
1149
+ new_path = best_path[:i+1] + best_path[j+1:k+1][::-1] + best_path[i+1:j+1][::-1] + best_path[k+1:]
1150
+ case 5:
1151
+ new_path = best_path[:i+1] + best_path[j+1:k+1] + best_path[i+1:j+1][::-1] + best_path[k+1:]
1152
+ case _:
1153
+ new_path = best_path[:i+1] + best_path[j+1:k+1][::-1] + best_path[i+1:j+1] + best_path[k+1:]
1154
+
1155
+ dists[l] = path_length_sparse(new_path, G_data, G_rows, G_cols)
1156
+ paths[l] = new_path
1157
+
1158
+ for i in range(7):
1159
+ if dists[i] < best_dist:
1160
+ best_path, best_dist, improved = paths[i], dists[i], True
1161
+
1162
+ if improved:
1163
+ break
1164
+
1165
+ if improved:
1166
+ break
1167
+
1168
+ if improved:
1169
+ break
1170
+
1171
+ path = best_path[:]
1172
+
1173
+ return best_path, best_dist
1174
+
1175
+
964
1176
  @njit
965
1177
  def stich_singlet_sparse(G_data, G_rows, G_cols, singlet, bulk):
966
1178
  best_path = bulk.copy()
@@ -1118,17 +1330,23 @@ def init_G_a_b_sparse(G_m, a, b, dtype):
1118
1330
 
1119
1331
 
1120
1332
  @njit
1121
- 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):
1333
+ 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):
1122
1334
  path_a = [a[x] for x in sol_a[0]]
1123
1335
  path_b = [b[x] for x in sol_b[0]]
1124
1336
 
1125
1337
  best_path = stitch_sparse_symmetric(G_data, G_rows, G_cols, path_a, path_b)
1126
1338
 
1127
1339
  if is_top_level:
1128
- best_path, _ = one_way_two_opt_sparse(best_path, G_data, G_rows, G_cols)
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)
1129
1344
 
1130
1345
  if k_neighbors > 0:
1131
- best_path, _ = targeted_three_opt_sparse(best_path, G_data, G_rows, G_cols, neighbor_lists, k_neighbors)
1346
+ if is_parallel:
1347
+ best_path, _ = targeted_three_opt_sparse_parallel(best_path, G_data, G_rows, G_cols, neighbor_lists, k_neighbors)
1348
+ else:
1349
+ best_path, _ = targeted_three_opt_sparse(best_path, G_data, G_rows, G_cols, neighbor_lists, k_neighbors)
1132
1350
 
1133
1351
  best_path = restitch_sparse(G_data, G_rows, G_cols, best_path)
1134
1352
 
@@ -1203,9 +1421,9 @@ def tsp_symmetric_sparse(
1203
1421
  neighbor_lists[i].sort(key=lambda x: x[0])
1204
1422
 
1205
1423
  for i in range(len(neighbor_lists)):
1206
- neighbor_lists[i] = [c[1] for c in neighbor_lists[i]]
1424
+ neighbor_lists[i] = [x[1] for x in neighbor_lists[i]]
1207
1425
 
1208
- 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))
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)
1209
1427
 
1210
1428
 
1211
1429
  @njit
@@ -1218,11 +1436,10 @@ def path_length_streaming(path, G_func):
1218
1436
 
1219
1437
 
1220
1438
  @njit
1221
- def one_way_two_opt_streaming(path, G_func):
1439
+ def one_way_two_opt_streaming(best_path, G_func):
1222
1440
  improved = True
1223
- best_path = path
1224
1441
  best_dist = path_length_streaming(best_path, G_func)
1225
- path_len = len(path)
1442
+ path_len = len(best_path)
1226
1443
 
1227
1444
  while improved:
1228
1445
  improved = False
@@ -1232,9 +1449,33 @@ def one_way_two_opt_streaming(path, G_func):
1232
1449
  new_path[i:j] = best_path[j-1:i-1:-1]
1233
1450
  new_dist = path_length_streaming(new_path, G_func)
1234
1451
  if new_dist < best_dist:
1235
- best_path, best_dist = new_path, new_dist
1236
- improved = True
1237
- path = best_path
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
1238
1479
 
1239
1480
  return best_path, best_dist
1240
1481
 
@@ -1258,9 +1499,6 @@ def targeted_three_opt_streaming(path, G_func, neighbor_lists, k_neighbors=20):
1258
1499
  if k <= j or k >= n-1:
1259
1500
  continue
1260
1501
 
1261
- # Extract indices
1262
- A, B, C, D, E, F = path[i], path[i+1], path[j], path[j+1], path[k], path[k+1]
1263
-
1264
1502
  # 7 unique cases (same as brute force, but restricted)
1265
1503
  new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:]
1266
1504
  dist = path_length_streaming(new_path, G_func)
@@ -1315,6 +1553,66 @@ def targeted_three_opt_streaming(path, G_func, neighbor_lists, k_neighbors=20):
1315
1553
  return best_path, best_dist
1316
1554
 
1317
1555
 
1556
+ @njit(parallel=True)
1557
+ def targeted_three_opt_streaming_parallel(path, G_func, neighbor_lists, k_neighbors=20):
1558
+ n = len(path)
1559
+ best_path = path[:]
1560
+ best_dist = path_length_streaming(best_path, G_func)
1561
+ paths = [best_path] * 7
1562
+ dists = [best_dist] * 7
1563
+ improved = True
1564
+
1565
+ while improved:
1566
+ improved = False
1567
+ for i in range(n - 5):
1568
+ for _j in range(k_neighbors):
1569
+ j = neighbor_lists[path[i], _j]
1570
+ if j <= i or j >= n-3:
1571
+ continue
1572
+ for _k in range(k_neighbors):
1573
+ k = neighbor_lists[path[j], _k]
1574
+ if k <= j or k >= n-1:
1575
+ continue
1576
+
1577
+ # 7 unique cases (same as brute force, but restricted)
1578
+ for l in prange(7):
1579
+ match l:
1580
+ case 0:
1581
+ new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:]
1582
+ case 1:
1583
+ new_path = best_path[:j+1] + best_path[j+1:k+1][::-1] + best_path[k+1:]
1584
+ case 2:
1585
+ new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:k+1][::-1] + best_path[k+1:]
1586
+ case 3:
1587
+ new_path = best_path[:i+1] + best_path[j+1:k+1] + best_path[i+1:j+1] + best_path[k+1:]
1588
+ case 4:
1589
+ new_path = best_path[:i+1] + best_path[j+1:k+1][::-1] + best_path[i+1:j+1][::-1] + best_path[k+1:]
1590
+ case 5:
1591
+ new_path = best_path[:i+1] + best_path[j+1:k+1] + best_path[i+1:j+1][::-1] + best_path[k+1:]
1592
+ case _:
1593
+ new_path = best_path[:i+1] + best_path[j+1:k+1][::-1] + best_path[i+1:j+1] + best_path[k+1:]
1594
+
1595
+ dists[l] = path_length_streaming(new_path, G_func)
1596
+ paths[l] = new_path
1597
+
1598
+ for i in range(7):
1599
+ if dists[i] < best_dist:
1600
+ best_path, best_dist, improved = paths[i], dists[i], True
1601
+
1602
+ if improved:
1603
+ break
1604
+
1605
+ if improved:
1606
+ break
1607
+
1608
+ if improved:
1609
+ break
1610
+
1611
+ path = best_path[:]
1612
+
1613
+ return best_path, best_dist
1614
+
1615
+
1318
1616
  @njit
1319
1617
  def stich_singlet_streaming(G_func, singlet, bulk):
1320
1618
  best_path = bulk.copy()
@@ -1439,14 +1737,20 @@ def tsp_streaming_bruteforce(G_func, perms, n):
1439
1737
 
1440
1738
 
1441
1739
  @njit
1442
- def tsp_symmetric_streaming_driver(G_func, is_top_level, k_neighbors, nodes, path_a, path_b, neighbor_lists):
1740
+ def tsp_symmetric_streaming_driver(G_func, is_top_level, k_neighbors, nodes, path_a, path_b, neighbor_lists, is_parallel):
1443
1741
  best_path = stitch_streaming_symmetric(G_func, path_a, path_b)
1444
1742
 
1445
1743
  if is_top_level:
1446
- best_path, _ = one_way_two_opt_streaming(best_path, G_func)
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)
1447
1748
 
1448
1749
  if k_neighbors > 0:
1449
- best_path, _ = targeted_three_opt_streaming(best_path, G_func, neighbor_lists, k_neighbors)
1750
+ if is_parallel:
1751
+ best_path, _ = targeted_three_opt_streaming_parallel(best_path, G_func, neighbor_lists, k_neighbors)
1752
+ else:
1753
+ best_path, _ = targeted_three_opt_streaming(best_path, G_func, neighbor_lists, k_neighbors)
1450
1754
 
1451
1755
  best_path = restitch_streaming(G_func, best_path)
1452
1756
 
@@ -1512,6 +1816,6 @@ def tsp_symmetric_streaming(
1512
1816
  neighbor_lists[i].sort(key=lambda x: x[0])
1513
1817
 
1514
1818
  for i in range(len(neighbor_lists)):
1515
- neighbor_lists[i] = [c[1] for c in neighbor_lists[i]]
1819
+ neighbor_lists[i] = [x[1] for x in neighbor_lists[i]]
1516
1820
 
1517
- return tsp_symmetric_streaming_driver(G_func, is_top_level, k_neighbors, nodes, sol_a[0], sol_b[0], np.array(neighbor_lists))
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)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 7.4.3
3
+ Version: 7.4.5
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
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
51
51
 
52
52
  setup(
53
53
  name='pyqrackising',
54
- version='7.4.3',
54
+ version='7.4.5',
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