PyQrackIsing 7.4.0__tar.gz → 7.4.2__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.0/pyqrackising.egg-info → pyqrackising-7.4.2}/PKG-INFO +1 -1
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/pyproject.toml +1 -1
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/pyqrackising/tsp.py +107 -127
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/pyqrackising/tsp_maxcut.py +101 -56
- {pyqrackising-7.4.0 → pyqrackising-7.4.2/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/setup.py +1 -1
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/CMakeLists.txt +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/LICENSE.md +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/MANIFEST.in +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/README.md +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/pyqrackising/__init__.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/pyqrackising/maxcut_tfim.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/pyqrackising/maxcut_tfim_sparse.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/pyqrackising/maxcut_tfim_streaming.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/pyqrackising/maxcut_tfim_util.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/pyqrackising/spin_glass_solver.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/pyqrackising/spin_glass_solver_sparse.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/pyqrackising/spin_glass_solver_streaming.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/pyqrackising/spin_glass_solver_util.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.2}/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.2"
|
|
16
16
|
requires-python = ">=3.8"
|
|
17
17
|
description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
|
|
18
18
|
readme = {file = "README.txt", content-type = "text/markdown"}
|
|
@@ -1,35 +1,15 @@
|
|
|
1
1
|
from .spin_glass_solver import spin_glass_solver
|
|
2
2
|
from .maxcut_tfim_util import binary_search, opencl_context, to_scipy_sparse_upper_triangular
|
|
3
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
4
|
+
import time
|
|
3
5
|
import itertools
|
|
4
|
-
import multiprocessing
|
|
5
|
-
import multiprocessing.pool
|
|
6
6
|
import networkx as nx
|
|
7
7
|
from numba import njit
|
|
8
8
|
import numpy as np
|
|
9
|
+
import os
|
|
9
10
|
from scipy.sparse import lil_matrix
|
|
10
11
|
|
|
11
12
|
|
|
12
|
-
# See https://stackoverflow.com/questions/6974695/python-process-pool-non-daemonic#answer-53180921
|
|
13
|
-
class NoDaemonProcess(multiprocessing.Process):
|
|
14
|
-
@property
|
|
15
|
-
def daemon(self):
|
|
16
|
-
return False
|
|
17
|
-
|
|
18
|
-
@daemon.setter
|
|
19
|
-
def daemon(self, value):
|
|
20
|
-
pass
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
class NoDaemonContext(type(multiprocessing.get_context())):
|
|
24
|
-
Process = NoDaemonProcess
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
class NestablePool(multiprocessing.pool.Pool):
|
|
28
|
-
def __init__(self, *args, **kwargs):
|
|
29
|
-
kwargs['context'] = NoDaemonContext()
|
|
30
|
-
super(NestablePool, self).__init__(*args, **kwargs)
|
|
31
|
-
|
|
32
|
-
|
|
33
13
|
# two_opt() and targeted_three_opt() written by Elara (OpenAI ChatGPT instance)
|
|
34
14
|
@njit
|
|
35
15
|
def path_length(path, G_m):
|
|
@@ -129,7 +109,7 @@ def two_opt(path, G):
|
|
|
129
109
|
|
|
130
110
|
|
|
131
111
|
@njit
|
|
132
|
-
def targeted_three_opt(path, W, k_neighbors=20):
|
|
112
|
+
def targeted_three_opt(path, W, neighbor_lists, k_neighbors=20):
|
|
133
113
|
"""
|
|
134
114
|
Lin-Kernighan style 3-opt heuristic for TSP improvement.
|
|
135
115
|
|
|
@@ -143,11 +123,6 @@ def targeted_three_opt(path, W, k_neighbors=20):
|
|
|
143
123
|
best_dist = path_length(best_path, W)
|
|
144
124
|
improved = True
|
|
145
125
|
|
|
146
|
-
# Precompute nearest neighbors for each node
|
|
147
|
-
neighbor_lists = [
|
|
148
|
-
np.argsort(W[i])[:k_neighbors] for i in range(n)
|
|
149
|
-
]
|
|
150
|
-
|
|
151
126
|
while improved:
|
|
152
127
|
improved = False
|
|
153
128
|
for i in range(n - 5):
|
|
@@ -481,7 +456,7 @@ def tsp_symmetric_brute_force_driver(G_m, n_nodes, nodes, is_cyclic):
|
|
|
481
456
|
|
|
482
457
|
|
|
483
458
|
@njit
|
|
484
|
-
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):
|
|
459
|
+
def tsp_symmetric_driver(G_m, is_cyclic, is_top_level, start_node, end_node, k_neighbors, nodes, sol_a, sol_b, a, b, c, neighbor_lists):
|
|
485
460
|
path_a = [a[x] for x in sol_a[0]]
|
|
486
461
|
path_b = [b[x] for x in sol_b[0]]
|
|
487
462
|
|
|
@@ -514,7 +489,7 @@ def tsp_symmetric_driver(G_m, is_cyclic, is_top_level, start_node, end_node, k_n
|
|
|
514
489
|
best_path, _ = one_way_two_opt(best_path, G_m)
|
|
515
490
|
|
|
516
491
|
if k_neighbors > 0:
|
|
517
|
-
best_path, _ = targeted_three_opt(best_path, G_m, k_neighbors)
|
|
492
|
+
best_path, _ = targeted_three_opt(best_path, G_m, neighbor_lists, k_neighbors)
|
|
518
493
|
|
|
519
494
|
# We just corrected segments of 2 and 3,
|
|
520
495
|
# and this is top level,
|
|
@@ -600,13 +575,11 @@ def tsp_symmetric(
|
|
|
600
575
|
|
|
601
576
|
G_a, G_b = init_G_a_b(G_m, a, b)
|
|
602
577
|
|
|
603
|
-
if monte_carlo and is_parallel and len(a) > 3 and len(b) > 3:
|
|
604
|
-
|
|
605
|
-
(G_a, None, None, quality, shots, is_alt_gpu_sampling, is_base_maxcut_gpu, is_combo_maxcut_gpu, True, 0, False, multi_start, False, True)
|
|
606
|
-
(G_b, None, None, quality, shots, is_alt_gpu_sampling, is_base_maxcut_gpu, is_combo_maxcut_gpu, True, 0, False, multi_start, False, True)
|
|
607
|
-
|
|
608
|
-
with NestablePool(2) as pool:
|
|
609
|
-
sol_a, sol_b = pool.starmap(tsp_symmetric, args)
|
|
578
|
+
if monte_carlo and is_parallel and (len(a) > 3) and (len(b) > 3):
|
|
579
|
+
with ThreadPoolExecutor(max_workers=1) as executor:
|
|
580
|
+
f = executor.submit(tsp_symmetric, G_a, None, None, quality, shots, is_alt_gpu_sampling, is_base_maxcut_gpu, is_combo_maxcut_gpu, True, 0, False, multi_start, False, True)
|
|
581
|
+
sol_b = tsp_symmetric(G_b, None, None, quality, shots, is_alt_gpu_sampling, is_base_maxcut_gpu, is_combo_maxcut_gpu, True, 0, False, multi_start, False, True)
|
|
582
|
+
sol_a = f.result()
|
|
610
583
|
else:
|
|
611
584
|
sol_a = tsp_symmetric(
|
|
612
585
|
G_a,
|
|
@@ -619,7 +592,8 @@ def tsp_symmetric(
|
|
|
619
592
|
is_cyclic=False,
|
|
620
593
|
is_top_level=False,
|
|
621
594
|
k_neighbors=0,
|
|
622
|
-
multi_start=multi_start
|
|
595
|
+
multi_start=multi_start,
|
|
596
|
+
is_parallel=False
|
|
623
597
|
)
|
|
624
598
|
sol_b = tsp_symmetric(
|
|
625
599
|
G_b,
|
|
@@ -632,10 +606,28 @@ def tsp_symmetric(
|
|
|
632
606
|
is_cyclic=False,
|
|
633
607
|
is_top_level=False,
|
|
634
608
|
k_neighbors=0,
|
|
635
|
-
multi_start=multi_start
|
|
609
|
+
multi_start=multi_start,
|
|
610
|
+
is_parallel=False
|
|
636
611
|
)
|
|
637
612
|
|
|
638
|
-
|
|
613
|
+
neighbor_lists = [[0]]
|
|
614
|
+
if k_neighbors > 0:
|
|
615
|
+
# Precompute nearest neighbors for each node
|
|
616
|
+
neighbor_lists = [
|
|
617
|
+
([(float("inf"), 0)] * k_neighbors) for i in range(n_nodes)
|
|
618
|
+
]
|
|
619
|
+
|
|
620
|
+
for i in range(n_nodes):
|
|
621
|
+
for j in range(n_nodes):
|
|
622
|
+
val = G_m[i, j]
|
|
623
|
+
if val < neighbor_lists[i][-1][0]:
|
|
624
|
+
neighbor_lists[i][-1] = (val, j)
|
|
625
|
+
neighbor_lists[i].sort(key=lambda x: x[0])
|
|
626
|
+
|
|
627
|
+
for i in range(len(neighbor_lists)):
|
|
628
|
+
neighbor_lists[i] = [c[1] for c in neighbor_lists[i]]
|
|
629
|
+
|
|
630
|
+
return tsp_symmetric_driver(G_m, is_cyclic, is_top_level, start_node, end_node, k_neighbors, nodes, sol_a, sol_b, a, b, c[0] if len(c) else None, np.array(neighbor_lists))
|
|
639
631
|
|
|
640
632
|
|
|
641
633
|
@njit
|
|
@@ -704,14 +696,11 @@ def tsp_asymmetric_driver(G_m, is_reversed, is_cyclic, is_top_level, start_node,
|
|
|
704
696
|
path, weight = targeted_three_opt(best_path.copy(), G_m, k_neighbors)
|
|
705
697
|
if weight < best_weight:
|
|
706
698
|
final_path, best_weight = path, weight
|
|
707
|
-
|
|
708
|
-
# We just corrected segments of 2 and 3,
|
|
709
|
-
# and this is top level,
|
|
710
|
-
# so correct segments of 4 to 7.
|
|
711
|
-
restitch(G_m, final_path, True)
|
|
712
699
|
else:
|
|
713
700
|
final_path = best_path
|
|
714
701
|
|
|
702
|
+
final_path = restitch(G_m, final_path, True)
|
|
703
|
+
|
|
715
704
|
best_weight = path_length(final_path, G_m)
|
|
716
705
|
|
|
717
706
|
if is_reversed:
|
|
@@ -798,13 +787,11 @@ def tsp_asymmetric(
|
|
|
798
787
|
|
|
799
788
|
G_a, G_b = init_G_a_b(G_m, a, b)
|
|
800
789
|
|
|
801
|
-
if monte_carlo and is_parallel and len(a) > 2 and len(b) > 2:
|
|
802
|
-
|
|
803
|
-
(G_a, None, None, quality, shots, is_alt_gpu_sampling, is_base_maxcut_gpu, is_combo_maxcut_gpu, True, 0, False, multi_start, False, True)
|
|
804
|
-
(G_b, None, None, quality, shots, is_alt_gpu_sampling, is_base_maxcut_gpu, is_combo_maxcut_gpu, True, 0, False, multi_start, False, True)
|
|
805
|
-
|
|
806
|
-
with NestablePool(2) as pool:
|
|
807
|
-
sol_a, sol_b = pool.starmap(tsp_asymmetric, args)
|
|
790
|
+
if monte_carlo and is_parallel and (len(a) > 2) and (len(b) > 2):
|
|
791
|
+
with ThreadPoolExecutor(max_workers=1) as executor:
|
|
792
|
+
f = executor.submit(tsp_asymmetric, G_a, None, None, quality, shots, is_alt_gpu_sampling, is_base_maxcut_gpu, is_combo_maxcut_gpu, True, 0, False, multi_start, False, True)
|
|
793
|
+
sol_b = tsp_asymmetric(G_b, None, None, quality, shots, is_alt_gpu_sampling, is_base_maxcut_gpu, is_combo_maxcut_gpu, True, 0, False, multi_start, False, True)
|
|
794
|
+
sol_a = f.result()
|
|
808
795
|
else:
|
|
809
796
|
sol_a = tsp_asymmetric(
|
|
810
797
|
G_a,
|
|
@@ -817,7 +804,8 @@ def tsp_asymmetric(
|
|
|
817
804
|
is_cyclic=False,
|
|
818
805
|
is_top_level=False,
|
|
819
806
|
k_neighbors=0,
|
|
820
|
-
multi_start=multi_start
|
|
807
|
+
multi_start=multi_start,
|
|
808
|
+
is_parallel=False
|
|
821
809
|
)
|
|
822
810
|
sol_b = tsp_asymmetric(
|
|
823
811
|
G_b,
|
|
@@ -830,7 +818,8 @@ def tsp_asymmetric(
|
|
|
830
818
|
is_cyclic=False,
|
|
831
819
|
is_top_level=False,
|
|
832
820
|
k_neighbors=0,
|
|
833
|
-
multi_start=multi_start
|
|
821
|
+
multi_start=multi_start,
|
|
822
|
+
is_parallel=False
|
|
834
823
|
)
|
|
835
824
|
|
|
836
825
|
return tsp_asymmetric_driver(G_m, is_reversed, is_cyclic, is_top_level, start_node, end_node, k_neighbors, nodes, sol_a, sol_b, a, b, c[0] if len(c) else None)
|
|
@@ -1122,42 +1111,21 @@ def init_G_a_b_sparse(G_m, a, b, dtype):
|
|
|
1122
1111
|
|
|
1123
1112
|
return G_a.tocsr(), G_b.tocsr()
|
|
1124
1113
|
|
|
1125
|
-
|
|
1114
|
+
|
|
1115
|
+
@njit
|
|
1116
|
+
def tsp_symmetric_sparse_driver(G_data, G_rows, G_cols, is_top_level, k_neighbors, nodes, sol_a, sol_b, a, b, neighbor_lists):
|
|
1126
1117
|
path_a = [a[x] for x in sol_a[0]]
|
|
1127
1118
|
path_b = [b[x] for x in sol_b[0]]
|
|
1128
1119
|
|
|
1129
|
-
restitch_sparse(G_data, G_rows, G_cols, path_a)
|
|
1130
|
-
restitch_sparse(G_data, G_rows, G_cols, path_b)
|
|
1131
|
-
|
|
1132
1120
|
best_path = stitch_sparse_symmetric(G_data, G_rows, G_cols, path_a, path_b)
|
|
1133
1121
|
|
|
1134
1122
|
if is_top_level:
|
|
1135
1123
|
best_path, _ = one_way_two_opt_sparse(best_path, G_data, G_rows, G_cols)
|
|
1136
1124
|
|
|
1137
1125
|
if k_neighbors > 0:
|
|
1138
|
-
|
|
1139
|
-
n_rows = len(G_rows) - 1
|
|
1140
|
-
|
|
1141
|
-
neighbor_lists = [
|
|
1142
|
-
([(float("inf"), 0)] * k_neighbors) for i in range(n_rows)
|
|
1143
|
-
]
|
|
1126
|
+
best_path, _ = targeted_three_opt_sparse(best_path, G_data, G_rows, G_cols, neighbor_lists, k_neighbors)
|
|
1144
1127
|
|
|
1145
|
-
|
|
1146
|
-
for j in range(n_rows):
|
|
1147
|
-
val = get_G_m(G_data, G_rows, G_cols, i, j)
|
|
1148
|
-
if val < neighbor_lists[i][-1][0]:
|
|
1149
|
-
neighbor_lists[i][-1] = (val, j)
|
|
1150
|
-
neighbor_lists[i].sort(key=lambda x: x[0])
|
|
1151
|
-
|
|
1152
|
-
for i in range(len(neighbor_lists)):
|
|
1153
|
-
neighbor_lists[i] = [c[1] for c in neighbor_lists[i]]
|
|
1154
|
-
|
|
1155
|
-
best_path, _ = targeted_three_opt_sparse(best_path, G_data, G_rows, G_cols, np.array(neighbor_lists), k_neighbors)
|
|
1156
|
-
|
|
1157
|
-
# We just corrected segments of 2 and 3,
|
|
1158
|
-
# and this is top level,
|
|
1159
|
-
# so correct segments of 4 to 7.
|
|
1160
|
-
restitch_sparse(G_data, G_rows, G_cols, best_path)
|
|
1128
|
+
best_path = restitch_sparse(G_data, G_rows, G_cols, best_path)
|
|
1161
1129
|
|
|
1162
1130
|
best_weight = path_length_sparse(best_path, G_data, G_rows, G_cols)
|
|
1163
1131
|
|
|
@@ -1195,26 +1163,43 @@ def tsp_symmetric_sparse(
|
|
|
1195
1163
|
|
|
1196
1164
|
G_a, G_b = init_G_a_b_sparse(G_m, a, b, dtype)
|
|
1197
1165
|
|
|
1198
|
-
if is_parallel and len(a) > 3 and len(b) > 3:
|
|
1199
|
-
|
|
1200
|
-
(
|
|
1201
|
-
(G_b,
|
|
1202
|
-
|
|
1203
|
-
with NestablePool(2) as pool:
|
|
1204
|
-
sol_a, sol_b = pool.starmap(tsp_symmetric_sparse, args)
|
|
1166
|
+
if is_parallel and (len(a) > 3) and (len(b) > 3):
|
|
1167
|
+
with ThreadPoolExecutor(max_workers=1) as executor:
|
|
1168
|
+
f = executor.submit(tsp_symmetric_sparse, G_a, False, True)
|
|
1169
|
+
sol_b = tsp_symmetric_sparse(G_b, False, True)
|
|
1170
|
+
sol_a = f.result()
|
|
1205
1171
|
else:
|
|
1206
1172
|
sol_a = tsp_symmetric_sparse(
|
|
1207
1173
|
G_a,
|
|
1208
1174
|
is_top_level=False,
|
|
1209
|
-
k_neighbors=0
|
|
1175
|
+
k_neighbors=0,
|
|
1176
|
+
is_parallel=False
|
|
1210
1177
|
)
|
|
1211
1178
|
sol_b = tsp_symmetric_sparse(
|
|
1212
1179
|
G_b,
|
|
1213
1180
|
is_top_level=False,
|
|
1214
|
-
k_neighbors=0
|
|
1181
|
+
k_neighbors=0,
|
|
1182
|
+
is_parallel=False
|
|
1215
1183
|
)
|
|
1216
1184
|
|
|
1217
|
-
|
|
1185
|
+
neighbor_lists = [[0]]
|
|
1186
|
+
if k_neighbors > 0:
|
|
1187
|
+
# Precompute nearest neighbors for each node
|
|
1188
|
+
neighbor_lists = [
|
|
1189
|
+
([(float("inf"), 0)] * k_neighbors) for i in range(n_nodes)
|
|
1190
|
+
]
|
|
1191
|
+
|
|
1192
|
+
for i in range(n_nodes):
|
|
1193
|
+
for j in range(n_nodes):
|
|
1194
|
+
val = G_m[i, j]
|
|
1195
|
+
if val < neighbor_lists[i][-1][0]:
|
|
1196
|
+
neighbor_lists[i][-1] = (val, j)
|
|
1197
|
+
neighbor_lists[i].sort(key=lambda x: x[0])
|
|
1198
|
+
|
|
1199
|
+
for i in range(len(neighbor_lists)):
|
|
1200
|
+
neighbor_lists[i] = [c[1] for c in neighbor_lists[i]]
|
|
1201
|
+
|
|
1202
|
+
return tsp_symmetric_sparse_driver(G_m.data, G_m.indptr, G_m.indices, is_top_level, k_neighbors, nodes, sol_a, sol_b, a, b, np.array(neighbor_lists))
|
|
1218
1203
|
|
|
1219
1204
|
|
|
1220
1205
|
@njit
|
|
@@ -1447,39 +1432,17 @@ def tsp_streaming_bruteforce(G_func, perms, n):
|
|
|
1447
1432
|
return best_path, best_weight
|
|
1448
1433
|
|
|
1449
1434
|
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
restitch_streaming(G_func, path_b)
|
|
1453
|
-
|
|
1435
|
+
@njit
|
|
1436
|
+
def tsp_symmetric_streaming_driver(G_func, is_top_level, k_neighbors, nodes, path_a, path_b, neighbor_lists):
|
|
1454
1437
|
best_path = stitch_streaming_symmetric(G_func, path_a, path_b)
|
|
1455
1438
|
|
|
1456
1439
|
if is_top_level:
|
|
1457
1440
|
best_path, _ = one_way_two_opt_streaming(best_path, G_func)
|
|
1458
1441
|
|
|
1459
1442
|
if k_neighbors > 0:
|
|
1460
|
-
|
|
1461
|
-
n_rows = len(nodes)
|
|
1462
|
-
|
|
1463
|
-
neighbor_lists = [
|
|
1464
|
-
([(float("inf"), 0)] * k_neighbors) for i in range(n_rows)
|
|
1465
|
-
]
|
|
1443
|
+
best_path, _ = targeted_three_opt_streaming(best_path, G_func, neighbor_lists, k_neighbors)
|
|
1466
1444
|
|
|
1467
|
-
|
|
1468
|
-
for j in nodes:
|
|
1469
|
-
val = G_func(i, j)
|
|
1470
|
-
if val < neighbor_lists[i][-1][0]:
|
|
1471
|
-
neighbor_lists[i][-1] = (val, j)
|
|
1472
|
-
neighbor_lists[i].sort(key=lambda x: x[0])
|
|
1473
|
-
|
|
1474
|
-
for i in range(len(neighbor_lists)):
|
|
1475
|
-
neighbor_lists[i] = [c[1] for c in neighbor_lists[i]]
|
|
1476
|
-
|
|
1477
|
-
best_path, _ = targeted_three_opt_streaming(best_path, G_func, np.array(neighbor_lists), k_neighbors)
|
|
1478
|
-
|
|
1479
|
-
# We just corrected segments of 2 and 3,
|
|
1480
|
-
# and this is top level,
|
|
1481
|
-
# so correct segments of 4 to 7.
|
|
1482
|
-
restitch_streaming(G_func, best_path)
|
|
1445
|
+
best_path = restitch_streaming(G_func, best_path)
|
|
1483
1446
|
|
|
1484
1447
|
best_weight = path_length_streaming(best_path, G_func)
|
|
1485
1448
|
|
|
@@ -1506,25 +1469,42 @@ def tsp_symmetric_streaming(
|
|
|
1506
1469
|
|
|
1507
1470
|
a, b = monte_carlo_loop(n_nodes)
|
|
1508
1471
|
|
|
1509
|
-
if is_parallel and len(a) > 3 and len(b) > 3:
|
|
1510
|
-
|
|
1511
|
-
(G_func, a,
|
|
1512
|
-
(G_func, b,
|
|
1513
|
-
|
|
1514
|
-
with NestablePool(2) as pool:
|
|
1515
|
-
sol_a, sol_b = pool.starmap(tsp_symmetric_streaming, args)
|
|
1472
|
+
if is_parallel and (len(a) > 3) and (len(b) > 3):
|
|
1473
|
+
with ThreadPoolExecutor(max_workers=1) as executor:
|
|
1474
|
+
f = executor.submit(tsp_symmetric_streaming, G_func, a, False, True)
|
|
1475
|
+
sol_b = tsp_symmetric_streaming(G_func, b, False, True)
|
|
1476
|
+
sol_a = f.result()
|
|
1516
1477
|
else:
|
|
1517
1478
|
sol_a = tsp_symmetric_streaming(
|
|
1518
1479
|
G_func,
|
|
1519
1480
|
a,
|
|
1520
1481
|
is_top_level=False,
|
|
1521
|
-
k_neighbors=0
|
|
1482
|
+
k_neighbors=0,
|
|
1483
|
+
is_parallel=False
|
|
1522
1484
|
)
|
|
1523
1485
|
sol_b = tsp_symmetric_streaming(
|
|
1524
1486
|
G_func,
|
|
1525
1487
|
b,
|
|
1526
1488
|
is_top_level=False,
|
|
1527
|
-
k_neighbors=0
|
|
1489
|
+
k_neighbors=0,
|
|
1490
|
+
is_parallel=False
|
|
1528
1491
|
)
|
|
1529
1492
|
|
|
1530
|
-
|
|
1493
|
+
neighbor_lists = [[0]]
|
|
1494
|
+
if k_neighbors > 0:
|
|
1495
|
+
# Precompute nearest neighbors for each node
|
|
1496
|
+
neighbor_lists = [
|
|
1497
|
+
([(float("inf"), 0)] * k_neighbors) for i in range(n_nodes)
|
|
1498
|
+
]
|
|
1499
|
+
|
|
1500
|
+
for i in nodes:
|
|
1501
|
+
for j in nodes:
|
|
1502
|
+
val = G_func(i, j)
|
|
1503
|
+
if val < neighbor_lists[i][-1][0]:
|
|
1504
|
+
neighbor_lists[i][-1] = (val, j)
|
|
1505
|
+
neighbor_lists[i].sort(key=lambda x: x[0])
|
|
1506
|
+
|
|
1507
|
+
for i in range(len(neighbor_lists)):
|
|
1508
|
+
neighbor_lists[i] = [c[1] for c in neighbor_lists[i]]
|
|
1509
|
+
|
|
1510
|
+
return tsp_symmetric_streaming_driver(G_func, is_top_level, k_neighbors, nodes, sol_a[0], sol_b[0], np.array(neighbor_lists))
|
|
@@ -4,14 +4,15 @@ from .spin_glass_solver import spin_glass_solver
|
|
|
4
4
|
from .spin_glass_solver_sparse import spin_glass_solver_sparse
|
|
5
5
|
from .spin_glass_solver_streaming import spin_glass_solver_streaming
|
|
6
6
|
import networkx as nx
|
|
7
|
-
from numba import njit
|
|
7
|
+
from numba import njit, prange
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
@njit
|
|
10
|
+
@njit(parallel=True)
|
|
11
11
|
def tsp_to_maxcut_bipartition(tsp_path, weights):
|
|
12
12
|
n = len(tsp_path)
|
|
13
13
|
best_cut_value = -float('inf')
|
|
14
|
-
|
|
14
|
+
best_partition_A = None
|
|
15
|
+
best_partition_B = None
|
|
15
16
|
direction = 0
|
|
16
17
|
|
|
17
18
|
for offset in [-1, 0, 1]:
|
|
@@ -19,16 +20,18 @@ def tsp_to_maxcut_bipartition(tsp_path, weights):
|
|
|
19
20
|
A = tsp_path[:mid]
|
|
20
21
|
B = tsp_path[mid:]
|
|
21
22
|
cut_value = 0.0
|
|
22
|
-
for
|
|
23
|
+
for i in prange(len(A)):
|
|
24
|
+
u = A[i]
|
|
23
25
|
for v in B:
|
|
24
26
|
cut_value += weights[u, v]
|
|
25
27
|
if cut_value > best_cut_value:
|
|
26
28
|
best_cut_value = cut_value
|
|
27
|
-
|
|
29
|
+
best_partition_A = A
|
|
30
|
+
best_partition_B = B
|
|
28
31
|
direction = offset
|
|
29
32
|
|
|
30
33
|
if direction == 0:
|
|
31
|
-
return
|
|
34
|
+
return best_partition_A, best_partition_B, best_cut_value
|
|
32
35
|
|
|
33
36
|
improved = True
|
|
34
37
|
best_offset = direction
|
|
@@ -39,16 +42,18 @@ def tsp_to_maxcut_bipartition(tsp_path, weights):
|
|
|
39
42
|
A = tsp_path[:mid]
|
|
40
43
|
B = tsp_path[mid:]
|
|
41
44
|
cut_value = 0.0
|
|
42
|
-
for
|
|
45
|
+
for i in prange(len(A)):
|
|
46
|
+
u = A[i]
|
|
43
47
|
for v in B:
|
|
44
48
|
cut_value += weights[u, v]
|
|
45
49
|
if cut_value > best_cut_value:
|
|
46
50
|
best_cut_value = cut_value
|
|
47
|
-
|
|
51
|
+
best_partition_A = A
|
|
52
|
+
best_partition_B = B
|
|
48
53
|
best_offset = offset
|
|
49
54
|
improved = True
|
|
50
55
|
|
|
51
|
-
return
|
|
56
|
+
return best_partition_A, best_partition_B, best_cut_value
|
|
52
57
|
|
|
53
58
|
|
|
54
59
|
@njit
|
|
@@ -73,6 +78,20 @@ def get_bitstring(partition, nodes):
|
|
|
73
78
|
return bitstring
|
|
74
79
|
|
|
75
80
|
|
|
81
|
+
@njit(parallel=True)
|
|
82
|
+
def early_exit(G_m, partition, nodes):
|
|
83
|
+
n_qubits = len(nodes)
|
|
84
|
+
bitstring = get_bitstring(partition, nodes)
|
|
85
|
+
theta_bits = [(b == "1") for b in list(bitstring)]
|
|
86
|
+
energy = 0
|
|
87
|
+
for u in prange(n_qubits):
|
|
88
|
+
for v in range(u + 1, n_qubits):
|
|
89
|
+
val = G_m[nodes[u], nodes[v]]
|
|
90
|
+
energy += (val if theta_bits[u] == theta_bits[v] else -val)
|
|
91
|
+
|
|
92
|
+
return bitstring, energy
|
|
93
|
+
|
|
94
|
+
|
|
76
95
|
def tsp_maxcut(G, k_neighbors=20, is_optimized=False, is_parallel=True, **kwargs):
|
|
77
96
|
G_m = None
|
|
78
97
|
if isinstance(G, nx.Graph):
|
|
@@ -85,16 +104,11 @@ def tsp_maxcut(G, k_neighbors=20, is_optimized=False, is_parallel=True, **kwargs
|
|
|
85
104
|
G_m = G
|
|
86
105
|
|
|
87
106
|
path, length = tsp_symmetric(G, is_cyclic=False, monte_carlo=True, k_neighbors=k_neighbors, is_parallel=is_parallel)
|
|
88
|
-
|
|
107
|
+
l, r, cut_value = tsp_to_maxcut_bipartition(path, G_m)
|
|
108
|
+
partition = (l, r)
|
|
89
109
|
|
|
90
110
|
if not is_optimized:
|
|
91
|
-
bitstring =
|
|
92
|
-
theta_bits = [(b == "1") for b in list(bitstring)]
|
|
93
|
-
energy = 0
|
|
94
|
-
for u in range(n_qubits):
|
|
95
|
-
for v in range(u + 1, n_qubits):
|
|
96
|
-
val = G_m[nodes[u], nodes[v]]
|
|
97
|
-
energy += (val if theta_bits[u] == theta_bits[v] else -val)
|
|
111
|
+
bitstring, energy = early_exit(G_m, partition, nodes)
|
|
98
112
|
|
|
99
113
|
return bitstring, cut_value, partition, energy
|
|
100
114
|
|
|
@@ -108,11 +122,12 @@ def tsp_maxcut(G, k_neighbors=20, is_optimized=False, is_parallel=True, **kwargs
|
|
|
108
122
|
return bitstring, cut_value, (l, r), energy
|
|
109
123
|
|
|
110
124
|
|
|
111
|
-
@njit
|
|
125
|
+
@njit(parallel=True)
|
|
112
126
|
def tsp_to_maxcut_bipartition_sparse(tsp_path, G_data, G_rows, G_cols):
|
|
113
127
|
n = len(tsp_path)
|
|
114
128
|
best_cut_value = -float('inf')
|
|
115
|
-
|
|
129
|
+
best_partition_A = None
|
|
130
|
+
best_partition_B = None
|
|
116
131
|
direction = 0
|
|
117
132
|
|
|
118
133
|
for offset in [-1, 0, 1]:
|
|
@@ -120,7 +135,8 @@ def tsp_to_maxcut_bipartition_sparse(tsp_path, G_data, G_rows, G_cols):
|
|
|
120
135
|
A = tsp_path[:mid]
|
|
121
136
|
B = tsp_path[mid:]
|
|
122
137
|
cut_value = 0.0
|
|
123
|
-
for
|
|
138
|
+
for i in prange(len(A)):
|
|
139
|
+
u = A[i]
|
|
124
140
|
for v in B:
|
|
125
141
|
low, high = (u, v) if u < v else (v, u)
|
|
126
142
|
start = G_rows[low]
|
|
@@ -130,11 +146,12 @@ def tsp_to_maxcut_bipartition_sparse(tsp_path, G_data, G_rows, G_cols):
|
|
|
130
146
|
cut_value += G_data[i]
|
|
131
147
|
if cut_value > best_cut_value:
|
|
132
148
|
best_cut_value = cut_value
|
|
133
|
-
|
|
149
|
+
best_partition_A = A
|
|
150
|
+
best_partition_B = B
|
|
134
151
|
direction = offset
|
|
135
152
|
|
|
136
153
|
if direction == 0:
|
|
137
|
-
return
|
|
154
|
+
return best_partition_A, best_partition_B, best_cut_value
|
|
138
155
|
|
|
139
156
|
improved = True
|
|
140
157
|
best_offset = direction
|
|
@@ -145,7 +162,8 @@ def tsp_to_maxcut_bipartition_sparse(tsp_path, G_data, G_rows, G_cols):
|
|
|
145
162
|
A = tsp_path[:mid]
|
|
146
163
|
B = tsp_path[mid:]
|
|
147
164
|
cut_value = 0.0
|
|
148
|
-
for
|
|
165
|
+
for i in prange(len(A)):
|
|
166
|
+
u = A[i]
|
|
149
167
|
for v in B:
|
|
150
168
|
low, high = (u, v) if u < v else (v, u)
|
|
151
169
|
start = G_rows[low]
|
|
@@ -155,11 +173,30 @@ def tsp_to_maxcut_bipartition_sparse(tsp_path, G_data, G_rows, G_cols):
|
|
|
155
173
|
cut_value += G_data[i]
|
|
156
174
|
if cut_value > best_cut_value:
|
|
157
175
|
best_cut_value = cut_value
|
|
158
|
-
|
|
176
|
+
best_partition_A = A
|
|
177
|
+
best_partition_B = B
|
|
159
178
|
best_offset = offset
|
|
160
179
|
improved = True
|
|
161
180
|
|
|
162
|
-
return
|
|
181
|
+
return best_partition_A, best_partition_B, best_cut_value
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
@njit(parallel=True)
|
|
185
|
+
def early_exit_sparse(G_data, G_rows, G_cols, partition, nodes):
|
|
186
|
+
n_qubits = len(nodes)
|
|
187
|
+
bitstring = get_bitstring(partition, nodes)
|
|
188
|
+
theta_bits = [(b == "1") for b in list(bitstring)]
|
|
189
|
+
energy = 0
|
|
190
|
+
for u in prange(n_qubits):
|
|
191
|
+
for v in range(u + 1, n_qubits):
|
|
192
|
+
start = G_rows[u]
|
|
193
|
+
end = G_rows[u + 1]
|
|
194
|
+
i = binary_search(G_cols[start:end], v) + start
|
|
195
|
+
if i < end:
|
|
196
|
+
val = G_data[i]
|
|
197
|
+
energy += (val if theta_bits[u] == theta_bits[v] else -val)
|
|
198
|
+
|
|
199
|
+
return bitstring, energy
|
|
163
200
|
|
|
164
201
|
|
|
165
202
|
def tsp_maxcut_sparse(G, k_neighbors=20, is_optimized=False, is_parallel=True, **kwargs):
|
|
@@ -174,16 +211,11 @@ def tsp_maxcut_sparse(G, k_neighbors=20, is_optimized=False, is_parallel=True, *
|
|
|
174
211
|
G_m = G
|
|
175
212
|
|
|
176
213
|
path, length = tsp_symmetric_sparse(G, k_neighbors=k_neighbors, is_parallel=is_parallel)
|
|
177
|
-
|
|
214
|
+
l, r, cut_value = tsp_to_maxcut_bipartition_sparse(path, G_m.data, G_m.indptr, G_m.indices)
|
|
215
|
+
partition = (l, r)
|
|
178
216
|
|
|
179
217
|
if not is_optimized:
|
|
180
|
-
bitstring =
|
|
181
|
-
theta_bits = [(b == "1") for b in list(bitstring)]
|
|
182
|
-
energy = 0
|
|
183
|
-
for u in range(n_qubits):
|
|
184
|
-
for v in range(u + 1, n_qubits):
|
|
185
|
-
val = G_m[nodes[u], nodes[v]]
|
|
186
|
-
energy += (val if theta_bits[u] == theta_bits[v] else -val)
|
|
218
|
+
bitstring, energy = early_exit_sparse(G_m.data, G_m.indptr, G_m.indices, partition, nodes)
|
|
187
219
|
|
|
188
220
|
return bitstring, cut_value, partition, energy
|
|
189
221
|
|
|
@@ -197,28 +229,31 @@ def tsp_maxcut_sparse(G, k_neighbors=20, is_optimized=False, is_parallel=True, *
|
|
|
197
229
|
return bitstring, cut_value, (l, r), energy
|
|
198
230
|
|
|
199
231
|
|
|
232
|
+
@njit(parallel=True)
|
|
200
233
|
def tsp_to_maxcut_bipartition_streaming(tsp_path, G_func):
|
|
201
234
|
n = len(tsp_path)
|
|
202
235
|
best_cut_value = -float('inf')
|
|
203
|
-
|
|
236
|
+
best_partition_A = None
|
|
237
|
+
best_partition_B = None
|
|
204
238
|
direction = 0
|
|
205
239
|
|
|
206
240
|
for offset in [-1, 0, 1]:
|
|
207
241
|
mid = n // 2 + offset
|
|
208
242
|
A = tsp_path[:mid]
|
|
209
243
|
B = tsp_path[mid:]
|
|
210
|
-
cut_value =
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
for v in B
|
|
214
|
-
|
|
244
|
+
cut_value = 0.0
|
|
245
|
+
for i in prange(len(A)):
|
|
246
|
+
u = A[i]
|
|
247
|
+
for v in B:
|
|
248
|
+
cut_value += G_func(u, v)
|
|
215
249
|
if cut_value > best_cut_value:
|
|
216
250
|
best_cut_value = cut_value
|
|
217
|
-
|
|
251
|
+
best_partition_A = A
|
|
252
|
+
best_partition_B = B
|
|
218
253
|
direction = offset
|
|
219
254
|
|
|
220
255
|
if direction == 0:
|
|
221
|
-
return
|
|
256
|
+
return best_partition_A, best_partition_B, best_cut_value
|
|
222
257
|
|
|
223
258
|
improved = True
|
|
224
259
|
best_offset = direction
|
|
@@ -228,33 +263,43 @@ def tsp_to_maxcut_bipartition_streaming(tsp_path, G_func):
|
|
|
228
263
|
mid = n // 2 + offset
|
|
229
264
|
A = tsp_path[:mid]
|
|
230
265
|
B = tsp_path[mid:]
|
|
231
|
-
cut_value =
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
for v in B
|
|
235
|
-
|
|
266
|
+
cut_value = 0.0
|
|
267
|
+
for i in prange(len(A)):
|
|
268
|
+
u = A[i]
|
|
269
|
+
for v in B:
|
|
270
|
+
cut_value += G_func(u, v)
|
|
236
271
|
if cut_value > best_cut_value:
|
|
237
272
|
best_cut_value = cut_value
|
|
238
|
-
|
|
273
|
+
best_partition_A = A
|
|
274
|
+
best_partition_B = B
|
|
239
275
|
best_offset = offset
|
|
240
276
|
improved = True
|
|
241
277
|
|
|
242
|
-
return
|
|
278
|
+
return best_partition_A, best_partition_B, best_cut_value
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
@njit(parallel=True)
|
|
282
|
+
def early_exit_streaming(G_func, partition, nodes):
|
|
283
|
+
n_qubits = len(nodes)
|
|
284
|
+
bitstring = get_bitstring(partition, nodes)
|
|
285
|
+
theta_bits = [(b == "1") for b in list(bitstring)]
|
|
286
|
+
energy = 0
|
|
287
|
+
for u in prange(n_qubits):
|
|
288
|
+
for v in range(u + 1, n_qubits):
|
|
289
|
+
val = G_func(nodes[u], nodes[v])
|
|
290
|
+
energy += (val if theta_bits[u] == theta_bits[v] else -val)
|
|
291
|
+
|
|
292
|
+
return bitstring, energy
|
|
243
293
|
|
|
244
294
|
|
|
245
295
|
def tsp_maxcut_streaming(G_func, nodes, k_neighbors=20, is_optimized=False, is_parallel=True, **kwargs):
|
|
246
296
|
n_qubits = len(nodes)
|
|
247
297
|
path, length = tsp_symmetric_streaming(G_func, nodes, k_neighbors=k_neighbors, is_parallel=is_parallel)
|
|
248
|
-
|
|
298
|
+
l, r, cut_value = tsp_to_maxcut_bipartition_streaming(path, G_func)
|
|
299
|
+
partition = (l, r)
|
|
249
300
|
|
|
250
301
|
if not is_optimized:
|
|
251
|
-
bitstring =
|
|
252
|
-
theta_bits = [(b == "1") for b in list(bitstring)]
|
|
253
|
-
energy = 0
|
|
254
|
-
for u in range(n_qubits):
|
|
255
|
-
for v in range(u + 1, n_qubits):
|
|
256
|
-
val = G_func(nodes[u], nodes[v])
|
|
257
|
-
energy += (val if theta_bits[u] == theta_bits[v] else -val)
|
|
302
|
+
bitstring, energy = early_exit_streaming(G_func, partition, nodes)
|
|
258
303
|
|
|
259
304
|
return bitstring, cut_value, partition, energy
|
|
260
305
|
|
|
@@ -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.2',
|
|
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
|