PyQrackIsing 7.1.0__tar.gz → 7.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyqrackising-7.1.0/pyqrackising.egg-info → pyqrackising-7.2.0}/PKG-INFO +1 -1
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/README.md +3 -3
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/pyproject.toml +1 -1
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/pyqrackising/__init__.py +1 -1
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/pyqrackising/maxcut_tfim_sparse.py +1 -33
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/pyqrackising/maxcut_tfim_util.py +32 -0
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/pyqrackising/tsp.py +664 -1
- pyqrackising-7.2.0/pyqrackising/tsp_maxcut.py +168 -0
- {pyqrackising-7.1.0 → pyqrackising-7.2.0/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/setup.py +1 -1
- pyqrackising-7.1.0/pyqrackising/tsp_maxcut.py +0 -49
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/CMakeLists.txt +0 -0
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/LICENSE.md +0 -0
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/MANIFEST.in +0 -0
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/pyqrackising/maxcut_tfim.py +0 -0
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/pyqrackising/maxcut_tfim_streaming.py +0 -0
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/pyqrackising/spin_glass_solver.py +0 -0
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/pyqrackising/spin_glass_solver_sparse.py +0 -0
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/pyqrackising/spin_glass_solver_streaming.py +0 -0
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/pyqrackising/spin_glass_solver_util.py +0 -0
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-7.1.0 → pyqrackising-7.2.0}/setup.cfg +0 -0
|
@@ -139,14 +139,14 @@ solution_bit_string, cut_value, node_groups, energy = spin_glass_solver_streamin
|
|
|
139
139
|
# solution_bit_string, cut_value, node_groups = maxcut_tfim_streaming(G_func, nodes, G_func_args_tuple=args_tuple)
|
|
140
140
|
```
|
|
141
141
|
|
|
142
|
-
Finally, combining insights from both the (Monte Carlo) TSP and MAXCUT solvers, we have `tsp_maxcut(G)`:
|
|
142
|
+
Finally, combining insights from both the (Monte Carlo) TSP and MAXCUT solvers, we have `tsp_maxcut(G)`, `tsp_maxcut_sparse(G)`, and `tsp_maxcut_streaming(G_func, nodes)`:
|
|
143
143
|
|
|
144
144
|
```
|
|
145
|
-
from pyqrackising import
|
|
145
|
+
from pyqrackising import tsp_maxcut_sparse
|
|
146
146
|
import networkx as nx
|
|
147
147
|
|
|
148
148
|
G = nx.petersen_graph()
|
|
149
|
-
best_partition, best_cut_value =
|
|
149
|
+
best_partition, best_cut_value = tsp_maxcut_sparse(G, k_neighbors=20)
|
|
150
150
|
```
|
|
151
151
|
|
|
152
152
|
This solver is entirely serial and can be parallelized over CPU processing elements by user code, easily.
|
|
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
|
|
|
12
12
|
|
|
13
13
|
[project]
|
|
14
14
|
name = "pyqrackising"
|
|
15
|
-
version = "7.
|
|
15
|
+
version = "7.2.0"
|
|
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"}
|
|
@@ -8,5 +8,5 @@ from .spin_glass_solver import spin_glass_solver
|
|
|
8
8
|
from .spin_glass_solver_sparse import spin_glass_solver_sparse
|
|
9
9
|
from .spin_glass_solver_streaming import spin_glass_solver_streaming
|
|
10
10
|
from .tsp import tsp_symmetric, tsp_asymmetric
|
|
11
|
-
from .tsp_maxcut import tsp_maxcut
|
|
11
|
+
from .tsp_maxcut import tsp_maxcut, tsp_maxcut_sparse, tsp_maxcut_streaming
|
|
12
12
|
from .convert_tensor_network_to_tsp import convert_tensor_network_to_tsp, convert_quimb_tree_to_tsp
|
|
@@ -3,9 +3,8 @@ import networkx as nx
|
|
|
3
3
|
import numpy as np
|
|
4
4
|
import os
|
|
5
5
|
from numba import njit, prange
|
|
6
|
-
from scipy.sparse import lil_matrix, csr_matrix
|
|
7
6
|
|
|
8
|
-
from .maxcut_tfim_util import fix_cdf, get_cut, init_thresholds, maxcut_hamming_cdf, opencl_context
|
|
7
|
+
from .maxcut_tfim_util import binary_search, fix_cdf, get_cut, init_thresholds, maxcut_hamming_cdf, opencl_context, to_scipy_sparse_upper_triangular
|
|
9
8
|
|
|
10
9
|
IS_OPENCL_AVAILABLE = True
|
|
11
10
|
try:
|
|
@@ -14,25 +13,6 @@ except ImportError:
|
|
|
14
13
|
IS_OPENCL_AVAILABLE = False
|
|
15
14
|
|
|
16
15
|
|
|
17
|
-
@njit
|
|
18
|
-
def binary_search(l, t):
|
|
19
|
-
left = 0
|
|
20
|
-
right = len(l) - 1
|
|
21
|
-
|
|
22
|
-
while left <= right:
|
|
23
|
-
mid = (left + right) >> 1
|
|
24
|
-
|
|
25
|
-
if l[mid] == t:
|
|
26
|
-
return mid
|
|
27
|
-
|
|
28
|
-
if l[mid] < t:
|
|
29
|
-
left = mid + 1
|
|
30
|
-
else:
|
|
31
|
-
right = mid - 1
|
|
32
|
-
|
|
33
|
-
return len(l)
|
|
34
|
-
|
|
35
|
-
|
|
36
16
|
@njit
|
|
37
17
|
def update_repulsion_choice(G_cols, G_data, G_rows, max_weight, weights, n, used, node):
|
|
38
18
|
# Select node
|
|
@@ -292,18 +272,6 @@ def gpu_footer(shots, n_qubits, G_data, G_rows, G_cols, weights, hamming_prob, n
|
|
|
292
272
|
return bit_string, best_value, (l, r)
|
|
293
273
|
|
|
294
274
|
|
|
295
|
-
def to_scipy_sparse_upper_triangular(G, nodes, n_nodes, dtype):
|
|
296
|
-
lil = lil_matrix((n_nodes, n_nodes), dtype=opencl_context.dtype)
|
|
297
|
-
for u in range(n_nodes):
|
|
298
|
-
u_node = nodes[u]
|
|
299
|
-
for v in range(u + 1, n_nodes):
|
|
300
|
-
v_node = nodes[v]
|
|
301
|
-
if G.has_edge(u_node, v_node):
|
|
302
|
-
lil[u, v] = G[u_node][v_node].get('weight', 1.0)
|
|
303
|
-
|
|
304
|
-
return lil.tocsr()
|
|
305
|
-
|
|
306
|
-
|
|
307
275
|
def maxcut_tfim_sparse(
|
|
308
276
|
G,
|
|
309
277
|
quality=None,
|
|
@@ -3,6 +3,7 @@ import networkx as nx
|
|
|
3
3
|
import numpy as np
|
|
4
4
|
import os
|
|
5
5
|
from numba import njit, prange
|
|
6
|
+
from scipy.sparse import lil_matrix
|
|
6
7
|
|
|
7
8
|
|
|
8
9
|
@njit
|
|
@@ -20,6 +21,37 @@ def get_cut(solution, nodes):
|
|
|
20
21
|
return bit_string, l, r
|
|
21
22
|
|
|
22
23
|
|
|
24
|
+
@njit
|
|
25
|
+
def binary_search(l, t):
|
|
26
|
+
left = 0
|
|
27
|
+
right = len(l) - 1
|
|
28
|
+
|
|
29
|
+
while left <= right:
|
|
30
|
+
mid = (left + right) >> 1
|
|
31
|
+
|
|
32
|
+
if l[mid] == t:
|
|
33
|
+
return mid
|
|
34
|
+
|
|
35
|
+
if l[mid] < t:
|
|
36
|
+
left = mid + 1
|
|
37
|
+
else:
|
|
38
|
+
right = mid - 1
|
|
39
|
+
|
|
40
|
+
return len(l)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def to_scipy_sparse_upper_triangular(G, nodes, n_nodes, dtype):
|
|
44
|
+
lil = lil_matrix((n_nodes, n_nodes), dtype=opencl_context.dtype)
|
|
45
|
+
for u in range(n_nodes):
|
|
46
|
+
u_node = nodes[u]
|
|
47
|
+
for v in range(u + 1, n_nodes):
|
|
48
|
+
v_node = nodes[v]
|
|
49
|
+
if G.has_edge(u_node, v_node):
|
|
50
|
+
lil[u, v] = G[u_node][v_node].get('weight', 1.0)
|
|
51
|
+
|
|
52
|
+
return lil.tocsr()
|
|
53
|
+
|
|
54
|
+
|
|
23
55
|
@njit(parallel=True)
|
|
24
56
|
def init_theta(h_mult, n_qubits, J_eff, degrees, dtype):
|
|
25
57
|
theta = np.empty(n_qubits, dtype=dtype)
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
from .spin_glass_solver import spin_glass_solver
|
|
2
|
-
from .maxcut_tfim_util import opencl_context
|
|
2
|
+
from .maxcut_tfim_util import binary_search, opencl_context, to_scipy_sparse_upper_triangular
|
|
3
3
|
import itertools
|
|
4
4
|
import networkx as nx
|
|
5
5
|
from numba import njit
|
|
6
6
|
import numpy as np
|
|
7
|
+
from scipy.sparse import lil_matrix
|
|
7
8
|
|
|
8
9
|
|
|
9
10
|
# two_opt() and targeted_three_opt() written by Elara (OpenAI ChatGPT instance)
|
|
@@ -34,6 +35,7 @@ def one_way_two_opt(path, G):
|
|
|
34
35
|
best_path, best_dist = new_path, new_dist
|
|
35
36
|
improved = True
|
|
36
37
|
path = best_path
|
|
38
|
+
|
|
37
39
|
return best_path, best_dist
|
|
38
40
|
|
|
39
41
|
|
|
@@ -55,6 +57,7 @@ def anchored_two_opt(path, G):
|
|
|
55
57
|
best_path, best_dist = new_path, new_dist
|
|
56
58
|
improved = True
|
|
57
59
|
path = best_path
|
|
60
|
+
|
|
58
61
|
return best_path, best_dist
|
|
59
62
|
|
|
60
63
|
|
|
@@ -76,6 +79,7 @@ def reversed_anchored_two_opt(path, G):
|
|
|
76
79
|
best_path, best_dist = new_path, new_dist
|
|
77
80
|
improved = True
|
|
78
81
|
path = best_path
|
|
82
|
+
|
|
79
83
|
return best_path, best_dist
|
|
80
84
|
|
|
81
85
|
|
|
@@ -97,6 +101,7 @@ def two_opt(path, G):
|
|
|
97
101
|
best_path, best_dist = new_path, new_dist
|
|
98
102
|
improved = True
|
|
99
103
|
path = best_path
|
|
104
|
+
|
|
100
105
|
return best_path, best_dist
|
|
101
106
|
|
|
102
107
|
|
|
@@ -788,3 +793,661 @@ def tsp_asymmetric(
|
|
|
788
793
|
)
|
|
789
794
|
|
|
790
795
|
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)
|
|
796
|
+
|
|
797
|
+
|
|
798
|
+
@njit
|
|
799
|
+
def get_G_m(G_data, G_rows, G_cols, low, high):
|
|
800
|
+
if high < low:
|
|
801
|
+
low, high = high, low
|
|
802
|
+
|
|
803
|
+
start = G_rows[low]
|
|
804
|
+
end = G_rows[low + 1]
|
|
805
|
+
|
|
806
|
+
i = binary_search(G_cols[start:end], high) + start
|
|
807
|
+
if i < end:
|
|
808
|
+
return G_data[i]
|
|
809
|
+
|
|
810
|
+
return 0.0
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
@njit
|
|
814
|
+
def path_length_sparse(path, G_data, G_rows, G_cols):
|
|
815
|
+
tot_len = 0.0
|
|
816
|
+
for i in range(len(path) - 1):
|
|
817
|
+
tot_len += get_G_m(G_data, G_rows, G_cols, path[i], path[i + 1])
|
|
818
|
+
|
|
819
|
+
return tot_len
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
@njit
|
|
823
|
+
def one_way_two_opt_sparse(path, G_data, G_rows, G_cols):
|
|
824
|
+
improved = True
|
|
825
|
+
best_path = path
|
|
826
|
+
best_dist = path_length_sparse(best_path, G_data, G_rows, G_cols)
|
|
827
|
+
path_len = len(path)
|
|
828
|
+
|
|
829
|
+
while improved:
|
|
830
|
+
improved = False
|
|
831
|
+
for i in range(1, path_len - 1):
|
|
832
|
+
for j in range(i + 2, path_len):
|
|
833
|
+
new_path = best_path[:]
|
|
834
|
+
new_path[i:j] = best_path[j-1:i-1:-1]
|
|
835
|
+
new_dist = path_length_sparse(new_path, G_data, G_rows, G_cols)
|
|
836
|
+
if new_dist < best_dist:
|
|
837
|
+
best_path, best_dist = new_path, new_dist
|
|
838
|
+
improved = True
|
|
839
|
+
path = best_path
|
|
840
|
+
|
|
841
|
+
return best_path, best_dist
|
|
842
|
+
|
|
843
|
+
|
|
844
|
+
@njit
|
|
845
|
+
def targeted_three_opt_sparse(path, G_data, G_rows, G_cols, neighbor_lists, k_neighbors=20):
|
|
846
|
+
n = len(path)
|
|
847
|
+
best_path = path[:]
|
|
848
|
+
best_dist = path_length_sparse(best_path, G_data, G_rows, G_cols)
|
|
849
|
+
improved = True
|
|
850
|
+
|
|
851
|
+
while improved:
|
|
852
|
+
improved = False
|
|
853
|
+
for i in range(n - 5):
|
|
854
|
+
for _j in range(k_neighbors):
|
|
855
|
+
j = neighbor_lists[path[i], _j]
|
|
856
|
+
if j <= i or j >= n-3:
|
|
857
|
+
continue
|
|
858
|
+
for _k in range(k_neighbors):
|
|
859
|
+
k = neighbor_lists[path[j], _k]
|
|
860
|
+
if k <= j or k >= n-1:
|
|
861
|
+
continue
|
|
862
|
+
|
|
863
|
+
# Extract indices
|
|
864
|
+
A, B, C, D, E, F = path[i], path[i+1], path[j], path[j+1], path[k], path[k+1]
|
|
865
|
+
|
|
866
|
+
# 7 unique cases (same as brute force, but restricted)
|
|
867
|
+
new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:]
|
|
868
|
+
dist = path_length_sparse(new_path, G_data, G_rows, G_cols)
|
|
869
|
+
if dist < best_dist:
|
|
870
|
+
best_path, best_dist, improved = new_path, dist, True
|
|
871
|
+
break
|
|
872
|
+
|
|
873
|
+
new_path = best_path[:j+1] + best_path[j+1:k+1][::-1] + best_path[k+1:]
|
|
874
|
+
dist = path_length_sparse(new_path, G_data, G_rows, G_cols)
|
|
875
|
+
if dist < best_dist:
|
|
876
|
+
best_path, best_dist, improved = new_path, dist, True
|
|
877
|
+
break
|
|
878
|
+
|
|
879
|
+
new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:k+1][::-1] + best_path[k+1:]
|
|
880
|
+
dist = path_length_sparse(new_path, G_data, G_rows, G_cols)
|
|
881
|
+
if dist < best_dist:
|
|
882
|
+
best_path, best_dist, improved = new_path, dist, True
|
|
883
|
+
break
|
|
884
|
+
|
|
885
|
+
new_path = best_path[:i+1] + best_path[j+1:k+1] + best_path[i+1:j+1] + best_path[k+1:]
|
|
886
|
+
dist = path_length_sparse(new_path, G_data, G_rows, G_cols)
|
|
887
|
+
if dist < best_dist:
|
|
888
|
+
best_path, best_dist, improved = new_path, dist, True
|
|
889
|
+
break
|
|
890
|
+
|
|
891
|
+
new_path = best_path[:i+1] + best_path[j+1:k+1][::-1] + best_path[i+1:j+1][::-1] + best_path[k+1:]
|
|
892
|
+
dist = path_length_sparse(new_path, G_data, G_rows, G_cols)
|
|
893
|
+
if dist < best_dist:
|
|
894
|
+
best_path, best_dist, improved = new_path, dist, True
|
|
895
|
+
break
|
|
896
|
+
|
|
897
|
+
new_path = best_path[:i+1] + best_path[j+1:k+1] + best_path[i+1:j+1][::-1] + best_path[k+1:]
|
|
898
|
+
dist = path_length_sparse(new_path, G_data, G_rows, G_cols)
|
|
899
|
+
if dist < best_dist:
|
|
900
|
+
best_path, best_dist, improved = new_path, dist, True
|
|
901
|
+
break
|
|
902
|
+
|
|
903
|
+
new_path = best_path[:i+1] + best_path[j+1:k+1][::-1] + best_path[i+1:j+1] + best_path[k+1:]
|
|
904
|
+
dist = path_length_sparse(new_path, G_data, G_rows, G_cols)
|
|
905
|
+
if dist < best_dist:
|
|
906
|
+
best_path, best_dist, improved = new_path, dist, True
|
|
907
|
+
break
|
|
908
|
+
|
|
909
|
+
if improved:
|
|
910
|
+
break
|
|
911
|
+
|
|
912
|
+
if improved:
|
|
913
|
+
break
|
|
914
|
+
|
|
915
|
+
path = best_path[:]
|
|
916
|
+
|
|
917
|
+
return best_path, best_dist
|
|
918
|
+
|
|
919
|
+
|
|
920
|
+
@njit
|
|
921
|
+
def stich_singlet_sparse(G_data, G_rows, G_cols, singlet, bulk):
|
|
922
|
+
best_path = bulk.copy()
|
|
923
|
+
best_weight = get_G_m(G_data, G_rows, G_cols, singlet, bulk[0])
|
|
924
|
+
weight = get_G_m(G_data, G_rows, G_cols, singlet, bulk[-1])
|
|
925
|
+
if weight < best_weight:
|
|
926
|
+
best_weight = weight
|
|
927
|
+
best_path += [singlet]
|
|
928
|
+
else:
|
|
929
|
+
best_path = [singlet] + best_path
|
|
930
|
+
|
|
931
|
+
for i in range(1, len(bulk)):
|
|
932
|
+
weight = (
|
|
933
|
+
get_G_m(G_data, G_rows, G_cols, singlet, bulk[i - 1]) +
|
|
934
|
+
get_G_m(G_data, G_rows, G_cols, singlet, bulk[i]) -
|
|
935
|
+
get_G_m(G_data, G_rows, G_cols, bulk[i - 1], bulk[i])
|
|
936
|
+
)
|
|
937
|
+
if weight < best_weight:
|
|
938
|
+
best_weight = weight
|
|
939
|
+
best_path = bulk.copy()
|
|
940
|
+
best_path.insert(i, singlet)
|
|
941
|
+
|
|
942
|
+
return best_path
|
|
943
|
+
|
|
944
|
+
|
|
945
|
+
@njit
|
|
946
|
+
def stitch_sparse_symmetric(G_data, G_rows, G_cols, path_a, path_b):
|
|
947
|
+
if len(path_a) == 1:
|
|
948
|
+
return stich_singlet_sparse(G_data, G_rows, G_cols, path_a[0], path_b)
|
|
949
|
+
|
|
950
|
+
if len(path_b) == 1:
|
|
951
|
+
return stich_singlet_sparse(G_data, G_rows, G_cols, path_b[0], path_a)
|
|
952
|
+
|
|
953
|
+
terminals_a = [path_a[0], path_a[-1]]
|
|
954
|
+
terminals_b = [path_b[0], path_b[-1]]
|
|
955
|
+
|
|
956
|
+
best_connect = get_G_m(G_data, G_rows, G_cols, terminals_a[1], terminals_b[0])
|
|
957
|
+
best_path = path_b.copy()
|
|
958
|
+
weight = get_G_m(G_data, G_rows, G_cols, terminals_a[0], terminals_b[1])
|
|
959
|
+
if weight < best_connect:
|
|
960
|
+
best_connect = weight
|
|
961
|
+
best_path += path_a
|
|
962
|
+
else:
|
|
963
|
+
best_path = path_a + best_path
|
|
964
|
+
|
|
965
|
+
for _ in range(2):
|
|
966
|
+
for _ in range(2):
|
|
967
|
+
for i in range(1, len(path_b)):
|
|
968
|
+
weight = (
|
|
969
|
+
get_G_m(G_data, G_rows, G_cols, terminals_a[0], path_b[i - 1]) +
|
|
970
|
+
get_G_m(G_data, G_rows, G_cols, terminals_a[1], path_b[i]) -
|
|
971
|
+
get_G_m(G_data, G_rows, G_cols, path_b[i - 1], path_b[i])
|
|
972
|
+
)
|
|
973
|
+
if weight < best_connect:
|
|
974
|
+
best_connect = weight
|
|
975
|
+
best_path = path_b.copy()
|
|
976
|
+
best_path[i:i] = path_a
|
|
977
|
+
path_a.reverse()
|
|
978
|
+
terminals_a.reverse()
|
|
979
|
+
path_a, path_b = path_b, path_a
|
|
980
|
+
terminals_a, terminals_b = terminals_b, terminals_a
|
|
981
|
+
|
|
982
|
+
return best_path
|
|
983
|
+
|
|
984
|
+
|
|
985
|
+
@njit
|
|
986
|
+
def restitch_sparse(G_data, G_rows, G_cols, path):
|
|
987
|
+
l = len(path)
|
|
988
|
+
mid = ((l + 1) if (l & 1) and (np.random.random() < 0.5) else l) >> 1
|
|
989
|
+
|
|
990
|
+
if mid < 4:
|
|
991
|
+
return path
|
|
992
|
+
|
|
993
|
+
path_a = restitch_sparse(G_data, G_rows, G_cols, path[:mid])
|
|
994
|
+
path_b = restitch_sparse(G_data, G_rows, G_cols, path[mid:])
|
|
995
|
+
|
|
996
|
+
return stitch_sparse_symmetric(G_data, G_rows, G_cols, path_a, path_b)
|
|
997
|
+
|
|
998
|
+
|
|
999
|
+
@njit
|
|
1000
|
+
def tsp_sparse_brute_force_driver(G_data, G_rows, G_cols, n_nodes, nodes):
|
|
1001
|
+
if n_nodes == 3:
|
|
1002
|
+
w_012 = get_G_m(G_data, G_rows, G_cols, 0, 1) + get_G_m(G_data, G_rows, G_cols, 1, 2)
|
|
1003
|
+
w_021 = get_G_m(G_data, G_rows, G_cols, 0, 1) + get_G_m(G_data, G_rows, G_cols, 0, 2)
|
|
1004
|
+
w_120 = get_G_m(G_data, G_rows, G_cols, 0, 2) + get_G_m(G_data, G_rows, G_cols, 1, 2)
|
|
1005
|
+
|
|
1006
|
+
if w_012 >= w_021 and w_012 >= w_120:
|
|
1007
|
+
return (nodes, w_012)
|
|
1008
|
+
|
|
1009
|
+
if w_021 >= w_012 and w_021 >= w_120:
|
|
1010
|
+
return ([nodes[1], nodes[0], nodes[2]], w_021)
|
|
1011
|
+
|
|
1012
|
+
return ([nodes[0], nodes[2], nodes[1]], w_120)
|
|
1013
|
+
|
|
1014
|
+
if n_nodes == 2:
|
|
1015
|
+
return (nodes, get_G_m(G_data, G_rows, G_cols, 0, 1))
|
|
1016
|
+
|
|
1017
|
+
return (nodes, 0)
|
|
1018
|
+
|
|
1019
|
+
|
|
1020
|
+
@njit
|
|
1021
|
+
def tsp_sparse_bruteforce(G_data, G_rows, G_cols, perms):
|
|
1022
|
+
n = len(G_rows) - 1
|
|
1023
|
+
best_weight = float('inf')
|
|
1024
|
+
best_path = None
|
|
1025
|
+
|
|
1026
|
+
# Must fix node 0 at start to remove rotational symmetry in cyclic case!
|
|
1027
|
+
|
|
1028
|
+
max_i = len(perms[0]) - 1
|
|
1029
|
+
|
|
1030
|
+
for path in perms:
|
|
1031
|
+
weight = 0.0
|
|
1032
|
+
for i in range(max_i):
|
|
1033
|
+
weight += get_G_m(G_data, G_rows, G_cols, path[i], path[i+1])
|
|
1034
|
+
|
|
1035
|
+
if weight < best_weight:
|
|
1036
|
+
best_weight = weight
|
|
1037
|
+
best_path = path
|
|
1038
|
+
|
|
1039
|
+
best_path = list(best_path)
|
|
1040
|
+
|
|
1041
|
+
return best_path, best_weight
|
|
1042
|
+
|
|
1043
|
+
|
|
1044
|
+
def init_G_a_b_sparse(G_m, a, b, dtype):
|
|
1045
|
+
n_a_nodes = len(a)
|
|
1046
|
+
n_b_nodes = len(b)
|
|
1047
|
+
G_a = lil_matrix((n_a_nodes, n_a_nodes), dtype=opencl_context.dtype)
|
|
1048
|
+
G_b = lil_matrix((n_b_nodes, n_b_nodes), dtype=opencl_context.dtype)
|
|
1049
|
+
for i in range(n_a_nodes):
|
|
1050
|
+
for j in range(n_a_nodes):
|
|
1051
|
+
if i == j:
|
|
1052
|
+
continue
|
|
1053
|
+
weight = G_m[a[i], a[j]]
|
|
1054
|
+
if weight != 0.0:
|
|
1055
|
+
G_a[i, j] = weight
|
|
1056
|
+
for i in range(n_b_nodes):
|
|
1057
|
+
for j in range(n_b_nodes):
|
|
1058
|
+
if i == j:
|
|
1059
|
+
continue
|
|
1060
|
+
weight = G_m[b[i], b[j]]
|
|
1061
|
+
if weight != 0.0:
|
|
1062
|
+
G_b[i, j] = weight
|
|
1063
|
+
|
|
1064
|
+
return G_a.tocsr(), G_b.tocsr()
|
|
1065
|
+
|
|
1066
|
+
def tsp_symmetric_sparse_driver(G_data, G_rows, G_cols, is_top_level, k_neighbors, nodes, sol_a, sol_b, a, b):
|
|
1067
|
+
path_a = [a[x] for x in sol_a[0]]
|
|
1068
|
+
path_b = [b[x] for x in sol_b[0]]
|
|
1069
|
+
|
|
1070
|
+
restitch_sparse(G_data, G_rows, G_cols, path_a)
|
|
1071
|
+
restitch_sparse(G_data, G_rows, G_cols, path_b)
|
|
1072
|
+
|
|
1073
|
+
best_path = stitch_sparse_symmetric(G_data, G_rows, G_cols, path_a, path_b)
|
|
1074
|
+
|
|
1075
|
+
if is_top_level:
|
|
1076
|
+
best_path, _ = one_way_two_opt_sparse(best_path, G_data, G_rows, G_cols)
|
|
1077
|
+
|
|
1078
|
+
if k_neighbors > 0:
|
|
1079
|
+
# Precompute nearest neighbors for each node
|
|
1080
|
+
n_rows = len(G_rows) - 1
|
|
1081
|
+
|
|
1082
|
+
neighbor_lists = [
|
|
1083
|
+
([(float("inf"), 0)] * k_neighbors) for i in range(n_rows)
|
|
1084
|
+
]
|
|
1085
|
+
|
|
1086
|
+
for i in range(n_rows):
|
|
1087
|
+
for j in range(n_rows):
|
|
1088
|
+
val = get_G_m(G_data, G_rows, G_cols, i, j)
|
|
1089
|
+
if val < neighbor_lists[i][-1][0]:
|
|
1090
|
+
neighbor_lists[i][-1] = (val, j)
|
|
1091
|
+
neighbor_lists[i].sort(key=lambda x: x[0])
|
|
1092
|
+
|
|
1093
|
+
for i in range(len(neighbor_lists)):
|
|
1094
|
+
neighbor_lists[i] = [c[1] for c in neighbor_lists[i]]
|
|
1095
|
+
|
|
1096
|
+
best_path, _ = targeted_three_opt_sparse(best_path, G_data, G_rows, G_cols, np.array(neighbor_lists), k_neighbors)
|
|
1097
|
+
|
|
1098
|
+
# We just corrected segments of 2 and 3,
|
|
1099
|
+
# and this is top level,
|
|
1100
|
+
# so correct segments of 4 to 7.
|
|
1101
|
+
restitch_sparse(G_data, G_rows, G_cols, best_path)
|
|
1102
|
+
|
|
1103
|
+
best_weight = path_length_sparse(best_path, G_data, G_rows, G_cols)
|
|
1104
|
+
|
|
1105
|
+
return [nodes[x] for x in best_path], best_weight
|
|
1106
|
+
|
|
1107
|
+
|
|
1108
|
+
def tsp_symmetric_sparse(
|
|
1109
|
+
G,
|
|
1110
|
+
k_neighbors=20,
|
|
1111
|
+
is_top_level=True
|
|
1112
|
+
):
|
|
1113
|
+
dtype = opencl_context.dtype
|
|
1114
|
+
nodes = None
|
|
1115
|
+
n_nodes = 0
|
|
1116
|
+
G_m = None
|
|
1117
|
+
if isinstance(G, nx.Graph):
|
|
1118
|
+
nodes = list(G.nodes())
|
|
1119
|
+
n_nodes = len(nodes)
|
|
1120
|
+
G_m = to_scipy_sparse_upper_triangular(G, nodes, n_nodes, dtype)
|
|
1121
|
+
else:
|
|
1122
|
+
n_nodes = G.shape[0]
|
|
1123
|
+
nodes = list(range(n_nodes))
|
|
1124
|
+
G_m = G
|
|
1125
|
+
|
|
1126
|
+
if n_nodes < 7:
|
|
1127
|
+
if n_nodes > 3:
|
|
1128
|
+
best_path, best_weight = tsp_sparse_bruteforce(G_m.data, G_m.indptr, G_m.indices, list(itertools.permutations(list(range(n_nodes)))))
|
|
1129
|
+
|
|
1130
|
+
return [nodes[x] for x in best_path], best_weight
|
|
1131
|
+
|
|
1132
|
+
return tsp_sparse_brute_force_driver(G_m.data, G_m.indptr, G_m.indices, n_nodes, nodes)
|
|
1133
|
+
|
|
1134
|
+
a, b = monte_carlo_loop(n_nodes)
|
|
1135
|
+
|
|
1136
|
+
G_a, G_b = init_G_a_b_sparse(G_m, a, b, dtype)
|
|
1137
|
+
|
|
1138
|
+
sol_a = tsp_symmetric_sparse(
|
|
1139
|
+
G_a,
|
|
1140
|
+
is_top_level=False,
|
|
1141
|
+
k_neighbors=0
|
|
1142
|
+
)
|
|
1143
|
+
sol_b = tsp_symmetric_sparse(
|
|
1144
|
+
G_b,
|
|
1145
|
+
is_top_level=False,
|
|
1146
|
+
k_neighbors=0
|
|
1147
|
+
)
|
|
1148
|
+
|
|
1149
|
+
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)
|
|
1150
|
+
|
|
1151
|
+
|
|
1152
|
+
@njit
|
|
1153
|
+
def path_length_streaming(path, G_func):
|
|
1154
|
+
tot_len = 0.0
|
|
1155
|
+
for i in range(len(path) - 1):
|
|
1156
|
+
tot_len += G_func(path[i], path[i + 1])
|
|
1157
|
+
|
|
1158
|
+
return tot_len
|
|
1159
|
+
|
|
1160
|
+
|
|
1161
|
+
@njit
|
|
1162
|
+
def one_way_two_opt_streaming(path, G_func):
|
|
1163
|
+
improved = True
|
|
1164
|
+
best_path = path
|
|
1165
|
+
best_dist = path_length_streaming(best_path, G_func)
|
|
1166
|
+
path_len = len(path)
|
|
1167
|
+
|
|
1168
|
+
while improved:
|
|
1169
|
+
improved = False
|
|
1170
|
+
for i in range(1, path_len - 1):
|
|
1171
|
+
for j in range(i + 2, path_len):
|
|
1172
|
+
new_path = best_path[:]
|
|
1173
|
+
new_path[i:j] = best_path[j-1:i-1:-1]
|
|
1174
|
+
new_dist = path_length_streaming(new_path, G_func)
|
|
1175
|
+
if new_dist < best_dist:
|
|
1176
|
+
best_path, best_dist = new_path, new_dist
|
|
1177
|
+
improved = True
|
|
1178
|
+
path = best_path
|
|
1179
|
+
|
|
1180
|
+
return best_path, best_dist
|
|
1181
|
+
|
|
1182
|
+
|
|
1183
|
+
@njit
|
|
1184
|
+
def targeted_three_opt_streaming(path, G_func, neighbor_lists, k_neighbors=20):
|
|
1185
|
+
n = len(path)
|
|
1186
|
+
best_path = path[:]
|
|
1187
|
+
best_dist = path_length_streaming(best_path, G_func)
|
|
1188
|
+
improved = True
|
|
1189
|
+
|
|
1190
|
+
while improved:
|
|
1191
|
+
improved = False
|
|
1192
|
+
for i in range(n - 5):
|
|
1193
|
+
for _j in range(k_neighbors):
|
|
1194
|
+
j = neighbor_lists[path[i], _j]
|
|
1195
|
+
if j <= i or j >= n-3:
|
|
1196
|
+
continue
|
|
1197
|
+
for _k in range(k_neighbors):
|
|
1198
|
+
k = neighbor_lists[path[j], _k]
|
|
1199
|
+
if k <= j or k >= n-1:
|
|
1200
|
+
continue
|
|
1201
|
+
|
|
1202
|
+
# Extract indices
|
|
1203
|
+
A, B, C, D, E, F = path[i], path[i+1], path[j], path[j+1], path[k], path[k+1]
|
|
1204
|
+
|
|
1205
|
+
# 7 unique cases (same as brute force, but restricted)
|
|
1206
|
+
new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:]
|
|
1207
|
+
dist = path_length_streaming(new_path, G_func)
|
|
1208
|
+
if dist < best_dist:
|
|
1209
|
+
best_path, best_dist, improved = new_path, dist, True
|
|
1210
|
+
break
|
|
1211
|
+
|
|
1212
|
+
new_path = best_path[:j+1] + best_path[j+1:k+1][::-1] + best_path[k+1:]
|
|
1213
|
+
dist = path_length_streaming(new_path, G_func)
|
|
1214
|
+
if dist < best_dist:
|
|
1215
|
+
best_path, best_dist, improved = new_path, dist, True
|
|
1216
|
+
break
|
|
1217
|
+
|
|
1218
|
+
new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:k+1][::-1] + best_path[k+1:]
|
|
1219
|
+
dist = path_length_streaming(new_path, G_func)
|
|
1220
|
+
if dist < best_dist:
|
|
1221
|
+
best_path, best_dist, improved = new_path, dist, True
|
|
1222
|
+
break
|
|
1223
|
+
|
|
1224
|
+
new_path = best_path[:i+1] + best_path[j+1:k+1] + best_path[i+1:j+1] + best_path[k+1:]
|
|
1225
|
+
dist = path_length_streaming(new_path, G_func)
|
|
1226
|
+
if dist < best_dist:
|
|
1227
|
+
best_path, best_dist, improved = new_path, dist, True
|
|
1228
|
+
break
|
|
1229
|
+
|
|
1230
|
+
new_path = best_path[:i+1] + best_path[j+1:k+1][::-1] + best_path[i+1:j+1][::-1] + best_path[k+1:]
|
|
1231
|
+
dist = path_length_streaming(new_path, G_func)
|
|
1232
|
+
if dist < best_dist:
|
|
1233
|
+
best_path, best_dist, improved = new_path, dist, True
|
|
1234
|
+
break
|
|
1235
|
+
|
|
1236
|
+
new_path = best_path[:i+1] + best_path[j+1:k+1] + best_path[i+1:j+1][::-1] + best_path[k+1:]
|
|
1237
|
+
dist = path_length_streaming(new_path, G_func)
|
|
1238
|
+
if dist < best_dist:
|
|
1239
|
+
best_path, best_dist, improved = new_path, dist, True
|
|
1240
|
+
break
|
|
1241
|
+
|
|
1242
|
+
new_path = best_path[:i+1] + best_path[j+1:k+1][::-1] + best_path[i+1:j+1] + best_path[k+1:]
|
|
1243
|
+
dist = path_length_streaming(new_path, G_func)
|
|
1244
|
+
if dist < best_dist:
|
|
1245
|
+
best_path, best_dist, improved = new_path, dist, True
|
|
1246
|
+
break
|
|
1247
|
+
|
|
1248
|
+
if improved:
|
|
1249
|
+
break
|
|
1250
|
+
|
|
1251
|
+
if improved:
|
|
1252
|
+
break
|
|
1253
|
+
|
|
1254
|
+
path = best_path[:]
|
|
1255
|
+
|
|
1256
|
+
return best_path, best_dist
|
|
1257
|
+
|
|
1258
|
+
|
|
1259
|
+
@njit
|
|
1260
|
+
def stich_singlet_streaming(G_func, singlet, bulk):
|
|
1261
|
+
best_path = bulk.copy()
|
|
1262
|
+
best_weight = G_func(singlet, bulk[0])
|
|
1263
|
+
weight = G_func(singlet, bulk[-1])
|
|
1264
|
+
if weight < best_weight:
|
|
1265
|
+
best_weight = weight
|
|
1266
|
+
best_path += [singlet]
|
|
1267
|
+
else:
|
|
1268
|
+
best_path = [singlet] + best_path
|
|
1269
|
+
|
|
1270
|
+
for i in range(1, len(bulk)):
|
|
1271
|
+
weight = (
|
|
1272
|
+
G_func(singlet, bulk[i - 1]) +
|
|
1273
|
+
G_func(singlet, bulk[i]) -
|
|
1274
|
+
G_func(bulk[i - 1], bulk[i])
|
|
1275
|
+
)
|
|
1276
|
+
if weight < best_weight:
|
|
1277
|
+
best_weight = weight
|
|
1278
|
+
best_path = bulk.copy()
|
|
1279
|
+
best_path.insert(i, singlet)
|
|
1280
|
+
|
|
1281
|
+
return best_path
|
|
1282
|
+
|
|
1283
|
+
|
|
1284
|
+
@njit
|
|
1285
|
+
def stitch_streaming_symmetric(G_func, path_a, path_b):
|
|
1286
|
+
if len(path_a) == 1:
|
|
1287
|
+
return stich_singlet_streaming(G_func, path_a[0], path_b)
|
|
1288
|
+
|
|
1289
|
+
if len(path_b) == 1:
|
|
1290
|
+
return stich_singlet_streaming(G_func, path_b[0], path_a)
|
|
1291
|
+
|
|
1292
|
+
terminals_a = [path_a[0], path_a[-1]]
|
|
1293
|
+
terminals_b = [path_b[0], path_b[-1]]
|
|
1294
|
+
|
|
1295
|
+
best_connect = G_func(terminals_a[1], terminals_b[0])
|
|
1296
|
+
best_path = path_b.copy()
|
|
1297
|
+
weight = G_func(terminals_a[0], terminals_b[1])
|
|
1298
|
+
if weight < best_connect:
|
|
1299
|
+
best_connect = weight
|
|
1300
|
+
best_path += path_a
|
|
1301
|
+
else:
|
|
1302
|
+
best_path = path_a + best_path
|
|
1303
|
+
|
|
1304
|
+
for _ in range(2):
|
|
1305
|
+
for _ in range(2):
|
|
1306
|
+
for i in range(1, len(path_b)):
|
|
1307
|
+
weight = (
|
|
1308
|
+
G_func(terminals_a[0], path_b[i - 1]) +
|
|
1309
|
+
G_func(terminals_a[1], path_b[i]) -
|
|
1310
|
+
G_func(path_b[i - 1], path_b[i])
|
|
1311
|
+
)
|
|
1312
|
+
if weight < best_connect:
|
|
1313
|
+
best_connect = weight
|
|
1314
|
+
best_path = path_b.copy()
|
|
1315
|
+
best_path[i:i] = path_a
|
|
1316
|
+
path_a.reverse()
|
|
1317
|
+
terminals_a.reverse()
|
|
1318
|
+
path_a, path_b = path_b, path_a
|
|
1319
|
+
terminals_a, terminals_b = terminals_b, terminals_a
|
|
1320
|
+
|
|
1321
|
+
return best_path
|
|
1322
|
+
|
|
1323
|
+
|
|
1324
|
+
@njit
|
|
1325
|
+
def restitch_streaming(G_func, path):
|
|
1326
|
+
l = len(path)
|
|
1327
|
+
mid = ((l + 1) if (l & 1) and (np.random.random() < 0.5) else l) >> 1
|
|
1328
|
+
|
|
1329
|
+
if mid < 4:
|
|
1330
|
+
return path
|
|
1331
|
+
|
|
1332
|
+
path_a = restitch_streaming(G_func, path[:mid])
|
|
1333
|
+
path_b = restitch_streaming(G_func, path[mid:])
|
|
1334
|
+
|
|
1335
|
+
return stitch_streaming_symmetric(G_func, path_a, path_b)
|
|
1336
|
+
|
|
1337
|
+
|
|
1338
|
+
@njit
|
|
1339
|
+
def tsp_streaming_brute_force_driver(G_func, n_nodes, nodes):
|
|
1340
|
+
if n_nodes == 3:
|
|
1341
|
+
w_012 = G_func(0, 1) + G_func(1, 2)
|
|
1342
|
+
w_021 = G_func(0, 1) + G_func(0, 2)
|
|
1343
|
+
w_120 = G_func(0, 2) + G_func(1, 2)
|
|
1344
|
+
|
|
1345
|
+
if w_012 >= w_021 and w_012 >= w_120:
|
|
1346
|
+
return (nodes, w_012)
|
|
1347
|
+
|
|
1348
|
+
if w_021 >= w_012 and w_021 >= w_120:
|
|
1349
|
+
return ([nodes[1], nodes[0], nodes[2]], w_021)
|
|
1350
|
+
|
|
1351
|
+
return ([nodes[0], nodes[2], nodes[1]], w_120)
|
|
1352
|
+
|
|
1353
|
+
if n_nodes == 2:
|
|
1354
|
+
return (nodes, G_func(0, 1))
|
|
1355
|
+
|
|
1356
|
+
return (nodes, 0)
|
|
1357
|
+
|
|
1358
|
+
|
|
1359
|
+
@njit
|
|
1360
|
+
def tsp_streaming_bruteforce(G_func, perms, n):
|
|
1361
|
+
best_weight = float('inf')
|
|
1362
|
+
best_path = None
|
|
1363
|
+
|
|
1364
|
+
# Must fix node 0 at start to remove rotational symmetry in cyclic case!
|
|
1365
|
+
|
|
1366
|
+
max_i = len(perms[0]) - 1
|
|
1367
|
+
|
|
1368
|
+
for path in perms:
|
|
1369
|
+
weight = 0.0
|
|
1370
|
+
for i in range(max_i):
|
|
1371
|
+
weight += G_func(path[i], path[i+1])
|
|
1372
|
+
|
|
1373
|
+
if weight < best_weight:
|
|
1374
|
+
best_weight = weight
|
|
1375
|
+
best_path = path
|
|
1376
|
+
|
|
1377
|
+
best_path = list(best_path)
|
|
1378
|
+
|
|
1379
|
+
return best_path, best_weight
|
|
1380
|
+
|
|
1381
|
+
|
|
1382
|
+
def tsp_symmetric_streaming_driver(G_func, is_top_level, k_neighbors, nodes, path_a, path_b):
|
|
1383
|
+
restitch_streaming(G_func, path_a)
|
|
1384
|
+
restitch_streaming(G_func, path_b)
|
|
1385
|
+
|
|
1386
|
+
best_path = stitch_streaming_symmetric(G_func, path_a, path_b)
|
|
1387
|
+
|
|
1388
|
+
if is_top_level:
|
|
1389
|
+
best_path, _ = one_way_two_opt_streaming(best_path, G_func)
|
|
1390
|
+
|
|
1391
|
+
if k_neighbors > 0:
|
|
1392
|
+
# Precompute nearest neighbors for each node
|
|
1393
|
+
n_rows = len(nodes)
|
|
1394
|
+
|
|
1395
|
+
neighbor_lists = [
|
|
1396
|
+
([(float("inf"), 0)] * k_neighbors) for i in range(n_rows)
|
|
1397
|
+
]
|
|
1398
|
+
|
|
1399
|
+
for i in nodes:
|
|
1400
|
+
for j in nodes:
|
|
1401
|
+
val = G_func(i, j)
|
|
1402
|
+
if val < neighbor_lists[i][-1][0]:
|
|
1403
|
+
neighbor_lists[i][-1] = (val, j)
|
|
1404
|
+
neighbor_lists[i].sort(key=lambda x: x[0])
|
|
1405
|
+
|
|
1406
|
+
for i in range(len(neighbor_lists)):
|
|
1407
|
+
neighbor_lists[i] = [c[1] for c in neighbor_lists[i]]
|
|
1408
|
+
|
|
1409
|
+
best_path, _ = targeted_three_opt_streaming(best_path, G_func, np.array(neighbor_lists), k_neighbors)
|
|
1410
|
+
|
|
1411
|
+
# We just corrected segments of 2 and 3,
|
|
1412
|
+
# and this is top level,
|
|
1413
|
+
# so correct segments of 4 to 7.
|
|
1414
|
+
restitch_streaming(G_func, best_path)
|
|
1415
|
+
|
|
1416
|
+
best_weight = path_length_streaming(best_path, G_func)
|
|
1417
|
+
|
|
1418
|
+
return [nodes[x] for x in best_path], best_weight
|
|
1419
|
+
|
|
1420
|
+
|
|
1421
|
+
def tsp_symmetric_streaming(
|
|
1422
|
+
G_func,
|
|
1423
|
+
nodes,
|
|
1424
|
+
k_neighbors=20,
|
|
1425
|
+
is_top_level=True
|
|
1426
|
+
):
|
|
1427
|
+
dtype = opencl_context.dtype
|
|
1428
|
+
n_nodes = len(nodes)
|
|
1429
|
+
|
|
1430
|
+
if n_nodes < 7:
|
|
1431
|
+
if n_nodes > 3:
|
|
1432
|
+
best_path, best_weight = tsp_streaming_bruteforce(G_func, list(itertools.permutations(nodes)), n_nodes)
|
|
1433
|
+
|
|
1434
|
+
return best_path, best_weight
|
|
1435
|
+
|
|
1436
|
+
return tsp_streaming_brute_force_driver(G_func, n_nodes, nodes)
|
|
1437
|
+
|
|
1438
|
+
a, b = monte_carlo_loop(n_nodes)
|
|
1439
|
+
|
|
1440
|
+
sol_a = tsp_symmetric_streaming(
|
|
1441
|
+
G_func,
|
|
1442
|
+
a,
|
|
1443
|
+
is_top_level=False,
|
|
1444
|
+
k_neighbors=0
|
|
1445
|
+
)
|
|
1446
|
+
sol_b = tsp_symmetric_streaming(
|
|
1447
|
+
G_func,
|
|
1448
|
+
b,
|
|
1449
|
+
is_top_level=False,
|
|
1450
|
+
k_neighbors=0
|
|
1451
|
+
)
|
|
1452
|
+
|
|
1453
|
+
return tsp_symmetric_streaming_driver(G_func, is_top_level, k_neighbors, nodes, sol_a[0], sol_b[0])
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
from .maxcut_tfim_util import to_scipy_sparse_upper_triangular
|
|
2
|
+
from .tsp import tsp_symmetric, tsp_symmetric_sparse, tsp_symmetric_streaming
|
|
3
|
+
from .spin_glass_solver import spin_glass_solver
|
|
4
|
+
from .spin_glass_solver_sparse import spin_glass_solver_sparse
|
|
5
|
+
from .spin_glass_solver_streaming import spin_glass_solver_streaming
|
|
6
|
+
import networkx as nx
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def tsp_to_maxcut_bipartition(tsp_path, weights):
|
|
10
|
+
n = len(tsp_path)
|
|
11
|
+
best_cut_value = -float('inf')
|
|
12
|
+
best_partition = None
|
|
13
|
+
direction = 0
|
|
14
|
+
|
|
15
|
+
for offset in [-1, 0, 1]:
|
|
16
|
+
mid = n // 2 + offset
|
|
17
|
+
A = tsp_path[:mid]
|
|
18
|
+
B = tsp_path[mid:]
|
|
19
|
+
cut_value = sum(
|
|
20
|
+
weights[u, v]
|
|
21
|
+
for u in A
|
|
22
|
+
for v in B
|
|
23
|
+
)
|
|
24
|
+
if cut_value > best_cut_value:
|
|
25
|
+
best_cut_value = cut_value
|
|
26
|
+
best_partition = (A, B)
|
|
27
|
+
direction = offset
|
|
28
|
+
|
|
29
|
+
if direction == 0:
|
|
30
|
+
return best_partition, best_cut_value
|
|
31
|
+
|
|
32
|
+
improved = True
|
|
33
|
+
best_offset = direction
|
|
34
|
+
while improved:
|
|
35
|
+
improved = False
|
|
36
|
+
offset = best_offset + direction
|
|
37
|
+
mid = n // 2 + offset
|
|
38
|
+
A = tsp_path[:mid]
|
|
39
|
+
B = tsp_path[mid:]
|
|
40
|
+
cut_value = sum(
|
|
41
|
+
weights[u, v]
|
|
42
|
+
for u in A
|
|
43
|
+
for v in B
|
|
44
|
+
)
|
|
45
|
+
if cut_value > best_cut_value:
|
|
46
|
+
best_cut_value = cut_value
|
|
47
|
+
best_partition = (A, B)
|
|
48
|
+
best_offset = offset
|
|
49
|
+
improved = True
|
|
50
|
+
|
|
51
|
+
return best_partition, best_cut_value
|
|
52
|
+
|
|
53
|
+
def get_cut(bitstring, nodes):
|
|
54
|
+
solution = list(bitstring)
|
|
55
|
+
l, r = [], []
|
|
56
|
+
for i in range(len(solution)):
|
|
57
|
+
if solution[i] == "1":
|
|
58
|
+
r.append(nodes[i])
|
|
59
|
+
else:
|
|
60
|
+
l.append(nodes[i])
|
|
61
|
+
|
|
62
|
+
return l, r
|
|
63
|
+
|
|
64
|
+
def tsp_maxcut(G, k_neighbors=20, **kwargs):
|
|
65
|
+
G_m = None
|
|
66
|
+
if isinstance(G, nx.Graph):
|
|
67
|
+
nodes = list(G.nodes())
|
|
68
|
+
n_qubits = len(nodes)
|
|
69
|
+
G_m = nx.to_numpy_array(G, weight='weight', nonedge=0.0, dtype=dtype)
|
|
70
|
+
else:
|
|
71
|
+
n_qubits = len(G)
|
|
72
|
+
nodes = list(range(n_qubits))
|
|
73
|
+
G_m = G
|
|
74
|
+
|
|
75
|
+
path, length = tsp_symmetric(G, is_cyclic=False, monte_carlo=True, k_neighbors=k_neighbors)
|
|
76
|
+
partition, cut_value = tsp_to_maxcut_bipartition(path, G_m)
|
|
77
|
+
|
|
78
|
+
bitint = 0
|
|
79
|
+
for b in partition[0]:
|
|
80
|
+
bitint |= 1 << b
|
|
81
|
+
|
|
82
|
+
bitstring, cut_value, cut, energy = spin_glass_solver(G_m, best_guess=bitint, **kwargs)
|
|
83
|
+
l, r = get_cut(bitstring, nodes)
|
|
84
|
+
|
|
85
|
+
return bitstring, cut_value, (l, r), energy
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def tsp_maxcut_sparse(G, k_neighbors=20, **kwargs):
|
|
89
|
+
G_m = None
|
|
90
|
+
if isinstance(G, nx.Graph):
|
|
91
|
+
nodes = list(G.nodes())
|
|
92
|
+
n_qubits = len(nodes)
|
|
93
|
+
G_m = to_scipy_sparse_upper_triangular(G, nodes, n_qubits, dtype)
|
|
94
|
+
else:
|
|
95
|
+
n_qubits = G.shape[0]
|
|
96
|
+
nodes = list(range(n_qubits))
|
|
97
|
+
G_m = G
|
|
98
|
+
|
|
99
|
+
path, length = tsp_symmetric_sparse(G, k_neighbors=k_neighbors)
|
|
100
|
+
partition, cut_value = tsp_to_maxcut_bipartition(path, G_m)
|
|
101
|
+
|
|
102
|
+
bitint = 0
|
|
103
|
+
for b in partition[0]:
|
|
104
|
+
bitint |= 1 << b
|
|
105
|
+
|
|
106
|
+
bitstring, cut_value, cut, energy = spin_glass_solver_sparse(G_m, best_guess=bitint, **kwargs)
|
|
107
|
+
l, r = get_cut(bitstring, nodes)
|
|
108
|
+
|
|
109
|
+
return bitstring, cut_value, (l, r), energy
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def tsp_to_maxcut_bipartition_streaming(tsp_path, G_func):
|
|
113
|
+
n = len(tsp_path)
|
|
114
|
+
best_cut_value = -float('inf')
|
|
115
|
+
best_partition = None
|
|
116
|
+
direction = 0
|
|
117
|
+
|
|
118
|
+
for offset in [-1, 0, 1]:
|
|
119
|
+
mid = n // 2 + offset
|
|
120
|
+
A = tsp_path[:mid]
|
|
121
|
+
B = tsp_path[mid:]
|
|
122
|
+
cut_value = sum(
|
|
123
|
+
G_func(u, v)
|
|
124
|
+
for u in A
|
|
125
|
+
for v in B
|
|
126
|
+
)
|
|
127
|
+
if cut_value > best_cut_value:
|
|
128
|
+
best_cut_value = cut_value
|
|
129
|
+
best_partition = (A, B)
|
|
130
|
+
direction = offset
|
|
131
|
+
|
|
132
|
+
if direction == 0:
|
|
133
|
+
return best_partition, best_cut_value
|
|
134
|
+
|
|
135
|
+
improved = True
|
|
136
|
+
best_offset = direction
|
|
137
|
+
while improved:
|
|
138
|
+
improved = False
|
|
139
|
+
offset = best_offset + direction
|
|
140
|
+
mid = n // 2 + offset
|
|
141
|
+
A = tsp_path[:mid]
|
|
142
|
+
B = tsp_path[mid:]
|
|
143
|
+
cut_value = sum(
|
|
144
|
+
G_func(u, v)
|
|
145
|
+
for u in A
|
|
146
|
+
for v in B
|
|
147
|
+
)
|
|
148
|
+
if cut_value > best_cut_value:
|
|
149
|
+
best_cut_value = cut_value
|
|
150
|
+
best_partition = (A, B)
|
|
151
|
+
best_offset = offset
|
|
152
|
+
improved = True
|
|
153
|
+
|
|
154
|
+
return best_partition, best_cut_value
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def tsp_maxcut_streaming(G_func, nodes, k_neighbors=20, **kwargs):
|
|
158
|
+
path, length = tsp_symmetric_streaming(G_func, nodes, k_neighbors=k_neighbors)
|
|
159
|
+
partition, cut_value = tsp_to_maxcut_bipartition_streaming(path, G_func)
|
|
160
|
+
|
|
161
|
+
bitint = 0
|
|
162
|
+
for b in partition[0]:
|
|
163
|
+
bitint |= 1 << b
|
|
164
|
+
|
|
165
|
+
bitstring, cut_value, cut, energy = spin_glass_solver_streaming(G_func, nodes, best_guess=bitint, **kwargs)
|
|
166
|
+
l, r = get_cut(bitstring, nodes)
|
|
167
|
+
|
|
168
|
+
return bitstring, cut_value, (l, r), energy
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='pyqrackising',
|
|
54
|
-
version='7.
|
|
54
|
+
version='7.2.0',
|
|
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)',
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
from .tsp import tsp_symmetric
|
|
2
|
-
|
|
3
|
-
def tsp_to_maxcut_bipartition(tsp_path, weights):
|
|
4
|
-
n = len(tsp_path)
|
|
5
|
-
best_cut_value = -float('inf')
|
|
6
|
-
best_partition = None
|
|
7
|
-
direction = 0
|
|
8
|
-
|
|
9
|
-
for offset in [-1, 0, 1]:
|
|
10
|
-
mid = n // 2 + offset
|
|
11
|
-
A = tsp_path[:mid]
|
|
12
|
-
B = tsp_path[mid:]
|
|
13
|
-
cut_value = sum(
|
|
14
|
-
weights[u, v]
|
|
15
|
-
for u in A
|
|
16
|
-
for v in B
|
|
17
|
-
)
|
|
18
|
-
if cut_value > best_cut_value:
|
|
19
|
-
best_cut_value = cut_value
|
|
20
|
-
best_partition = (A, B)
|
|
21
|
-
direction = offset
|
|
22
|
-
|
|
23
|
-
if direction == 0:
|
|
24
|
-
return best_partition, best_cut_value
|
|
25
|
-
|
|
26
|
-
improved = True
|
|
27
|
-
best_offset = direction
|
|
28
|
-
while improved:
|
|
29
|
-
improved = False
|
|
30
|
-
offset = best_offset + direction
|
|
31
|
-
mid = n // 2 + offset
|
|
32
|
-
A = tsp_path[:mid]
|
|
33
|
-
B = tsp_path[mid:]
|
|
34
|
-
cut_value = sum(
|
|
35
|
-
weights[u, v]
|
|
36
|
-
for u in A
|
|
37
|
-
for v in B
|
|
38
|
-
)
|
|
39
|
-
if cut_value > best_cut_value:
|
|
40
|
-
best_cut_value = cut_value
|
|
41
|
-
best_partition = (A, B)
|
|
42
|
-
best_offset = offset
|
|
43
|
-
improved = True
|
|
44
|
-
|
|
45
|
-
return best_partition, best_cut_value
|
|
46
|
-
|
|
47
|
-
def tsp_maxcut(G, k_neighbors=20):
|
|
48
|
-
path, length = tsp_symmetric(G=G, is_cyclic=False, monte_carlo=True, k_neighbors=k_neighbors)
|
|
49
|
-
return tsp_to_maxcut_bipartition(path, G)
|
|
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
|