PyQrackIsing 7.2.0__tar.gz → 7.4.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.2.0/pyqrackising.egg-info → pyqrackising-7.4.0}/PKG-INFO +1 -1
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/README.md +2 -2
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/pyproject.toml +1 -1
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/pyqrackising/tsp.py +157 -80
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/pyqrackising/tsp_maxcut.py +118 -18
- {pyqrackising-7.2.0 → pyqrackising-7.4.0/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/setup.py +1 -1
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/CMakeLists.txt +0 -0
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/LICENSE.md +0 -0
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/MANIFEST.in +0 -0
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/pyqrackising/__init__.py +0 -0
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/pyqrackising/maxcut_tfim.py +0 -0
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/pyqrackising/maxcut_tfim_sparse.py +0 -0
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/pyqrackising/maxcut_tfim_streaming.py +0 -0
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/pyqrackising/maxcut_tfim_util.py +0 -0
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/pyqrackising/spin_glass_solver.py +0 -0
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/pyqrackising/spin_glass_solver_sparse.py +0 -0
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/pyqrackising/spin_glass_solver_streaming.py +0 -0
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/pyqrackising/spin_glass_solver_util.py +0 -0
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-7.2.0 → pyqrackising-7.4.0}/setup.cfg +0 -0
|
@@ -146,10 +146,10 @@ 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 = tsp_maxcut_sparse(G, k_neighbors=20)
|
|
149
|
+
best_partition, best_cut_value = tsp_maxcut_sparse(G, k_neighbors=20, is_optimized=False)
|
|
150
150
|
```
|
|
151
151
|
|
|
152
|
-
|
|
152
|
+
When `is_optimized=True`, the `spin_glass_solver(G)` is used as a final optimization pass. When `is_optimized=False`, this solver becomes entirely serial and can be parallelized over CPU processing elements by user code, easily.
|
|
153
153
|
|
|
154
154
|
## Environment Variables
|
|
155
155
|
|
|
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
|
|
|
12
12
|
|
|
13
13
|
[project]
|
|
14
14
|
name = "pyqrackising"
|
|
15
|
-
version = "7.
|
|
15
|
+
version = "7.4.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"}
|
|
@@ -1,12 +1,35 @@
|
|
|
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
3
|
import itertools
|
|
4
|
+
import multiprocessing
|
|
5
|
+
import multiprocessing.pool
|
|
4
6
|
import networkx as nx
|
|
5
7
|
from numba import njit
|
|
6
8
|
import numpy as np
|
|
7
9
|
from scipy.sparse import lil_matrix
|
|
8
10
|
|
|
9
11
|
|
|
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
|
+
|
|
10
33
|
# two_opt() and targeted_three_opt() written by Elara (OpenAI ChatGPT instance)
|
|
11
34
|
@njit
|
|
12
35
|
def path_length(path, G_m):
|
|
@@ -516,7 +539,8 @@ def tsp_symmetric(
|
|
|
516
539
|
k_neighbors=20,
|
|
517
540
|
is_cyclic=True,
|
|
518
541
|
multi_start=1,
|
|
519
|
-
is_top_level=True
|
|
542
|
+
is_top_level=True,
|
|
543
|
+
is_parallel=False
|
|
520
544
|
):
|
|
521
545
|
dtype = opencl_context.dtype
|
|
522
546
|
nodes = None
|
|
@@ -576,32 +600,40 @@ def tsp_symmetric(
|
|
|
576
600
|
|
|
577
601
|
G_a, G_b = init_G_a_b(G_m, a, b)
|
|
578
602
|
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
603
|
+
if monte_carlo and is_parallel and len(a) > 3 and len(b) > 3:
|
|
604
|
+
args = (
|
|
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)
|
|
610
|
+
else:
|
|
611
|
+
sol_a = tsp_symmetric(
|
|
612
|
+
G_a,
|
|
613
|
+
quality=quality,
|
|
614
|
+
shots=shots,
|
|
615
|
+
is_alt_gpu_sampling=is_alt_gpu_sampling,
|
|
616
|
+
is_base_maxcut_gpu=is_base_maxcut_gpu,
|
|
617
|
+
is_combo_maxcut_gpu=is_combo_maxcut_gpu,
|
|
618
|
+
monte_carlo=monte_carlo,
|
|
619
|
+
is_cyclic=False,
|
|
620
|
+
is_top_level=False,
|
|
621
|
+
k_neighbors=0,
|
|
622
|
+
multi_start=multi_start
|
|
623
|
+
)
|
|
624
|
+
sol_b = tsp_symmetric(
|
|
625
|
+
G_b,
|
|
626
|
+
quality=quality,
|
|
627
|
+
shots=shots,
|
|
628
|
+
is_alt_gpu_sampling=is_alt_gpu_sampling,
|
|
629
|
+
is_base_maxcut_gpu=is_base_maxcut_gpu,
|
|
630
|
+
is_combo_maxcut_gpu=is_combo_maxcut_gpu,
|
|
631
|
+
monte_carlo=monte_carlo,
|
|
632
|
+
is_cyclic=False,
|
|
633
|
+
is_top_level=False,
|
|
634
|
+
k_neighbors=0,
|
|
635
|
+
multi_start=multi_start
|
|
636
|
+
)
|
|
605
637
|
|
|
606
638
|
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)
|
|
607
639
|
|
|
@@ -701,7 +733,8 @@ def tsp_asymmetric(
|
|
|
701
733
|
k_neighbors=20,
|
|
702
734
|
is_cyclic=True,
|
|
703
735
|
multi_start=1,
|
|
704
|
-
is_top_level=True
|
|
736
|
+
is_top_level=True,
|
|
737
|
+
is_parallel=False
|
|
705
738
|
):
|
|
706
739
|
dtype = opencl_context.dtype
|
|
707
740
|
nodes = None
|
|
@@ -765,32 +798,40 @@ def tsp_asymmetric(
|
|
|
765
798
|
|
|
766
799
|
G_a, G_b = init_G_a_b(G_m, a, b)
|
|
767
800
|
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
801
|
+
if monte_carlo and is_parallel and len(a) > 2 and len(b) > 2:
|
|
802
|
+
args = (
|
|
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)
|
|
808
|
+
else:
|
|
809
|
+
sol_a = tsp_asymmetric(
|
|
810
|
+
G_a,
|
|
811
|
+
quality=quality,
|
|
812
|
+
shots=shots,
|
|
813
|
+
is_alt_gpu_sampling=is_alt_gpu_sampling,
|
|
814
|
+
is_base_maxcut_gpu=is_base_maxcut_gpu,
|
|
815
|
+
is_combo_maxcut_gpu=is_combo_maxcut_gpu,
|
|
816
|
+
monte_carlo=monte_carlo,
|
|
817
|
+
is_cyclic=False,
|
|
818
|
+
is_top_level=False,
|
|
819
|
+
k_neighbors=0,
|
|
820
|
+
multi_start=multi_start
|
|
821
|
+
)
|
|
822
|
+
sol_b = tsp_asymmetric(
|
|
823
|
+
G_b,
|
|
824
|
+
quality=quality,
|
|
825
|
+
shots=shots,
|
|
826
|
+
is_alt_gpu_sampling=is_alt_gpu_sampling,
|
|
827
|
+
is_base_maxcut_gpu=is_base_maxcut_gpu,
|
|
828
|
+
is_combo_maxcut_gpu=is_combo_maxcut_gpu,
|
|
829
|
+
monte_carlo=monte_carlo,
|
|
830
|
+
is_cyclic=False,
|
|
831
|
+
is_top_level=False,
|
|
832
|
+
k_neighbors=0,
|
|
833
|
+
multi_start=multi_start
|
|
834
|
+
)
|
|
794
835
|
|
|
795
836
|
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
837
|
|
|
@@ -814,7 +855,16 @@ def get_G_m(G_data, G_rows, G_cols, low, high):
|
|
|
814
855
|
def path_length_sparse(path, G_data, G_rows, G_cols):
|
|
815
856
|
tot_len = 0.0
|
|
816
857
|
for i in range(len(path) - 1):
|
|
817
|
-
|
|
858
|
+
low, high = path[i], path[i + 1]
|
|
859
|
+
if high < low:
|
|
860
|
+
low, high = high, low
|
|
861
|
+
|
|
862
|
+
start = G_rows[low]
|
|
863
|
+
end = G_rows[low + 1]
|
|
864
|
+
|
|
865
|
+
i = binary_search(G_cols[start:end], high) + start
|
|
866
|
+
if i < end:
|
|
867
|
+
tot_len += G_data[i]
|
|
818
868
|
|
|
819
869
|
return tot_len
|
|
820
870
|
|
|
@@ -1030,7 +1080,16 @@ def tsp_sparse_bruteforce(G_data, G_rows, G_cols, perms):
|
|
|
1030
1080
|
for path in perms:
|
|
1031
1081
|
weight = 0.0
|
|
1032
1082
|
for i in range(max_i):
|
|
1033
|
-
|
|
1083
|
+
low, high = path[i], path[i + 1]
|
|
1084
|
+
if high < low:
|
|
1085
|
+
low, high = high, low
|
|
1086
|
+
|
|
1087
|
+
start = G_rows[low]
|
|
1088
|
+
end = G_rows[low + 1]
|
|
1089
|
+
|
|
1090
|
+
i = binary_search(G_cols[start:end], high) + start
|
|
1091
|
+
if i < end:
|
|
1092
|
+
weight += G_data[i]
|
|
1034
1093
|
|
|
1035
1094
|
if weight < best_weight:
|
|
1036
1095
|
best_weight = weight
|
|
@@ -1108,7 +1167,8 @@ def tsp_symmetric_sparse_driver(G_data, G_rows, G_cols, is_top_level, k_neighbor
|
|
|
1108
1167
|
def tsp_symmetric_sparse(
|
|
1109
1168
|
G,
|
|
1110
1169
|
k_neighbors=20,
|
|
1111
|
-
is_top_level=True
|
|
1170
|
+
is_top_level=True,
|
|
1171
|
+
is_parallel=True
|
|
1112
1172
|
):
|
|
1113
1173
|
dtype = opencl_context.dtype
|
|
1114
1174
|
nodes = None
|
|
@@ -1135,16 +1195,24 @@ def tsp_symmetric_sparse(
|
|
|
1135
1195
|
|
|
1136
1196
|
G_a, G_b = init_G_a_b_sparse(G_m, a, b, dtype)
|
|
1137
1197
|
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1198
|
+
if is_parallel and len(a) > 3 and len(b) > 3:
|
|
1199
|
+
args = (
|
|
1200
|
+
(G_a, 0, False, True),
|
|
1201
|
+
(G_b, 0, False, True)
|
|
1202
|
+
)
|
|
1203
|
+
with NestablePool(2) as pool:
|
|
1204
|
+
sol_a, sol_b = pool.starmap(tsp_symmetric_sparse, args)
|
|
1205
|
+
else:
|
|
1206
|
+
sol_a = tsp_symmetric_sparse(
|
|
1207
|
+
G_a,
|
|
1208
|
+
is_top_level=False,
|
|
1209
|
+
k_neighbors=0
|
|
1210
|
+
)
|
|
1211
|
+
sol_b = tsp_symmetric_sparse(
|
|
1212
|
+
G_b,
|
|
1213
|
+
is_top_level=False,
|
|
1214
|
+
k_neighbors=0
|
|
1215
|
+
)
|
|
1148
1216
|
|
|
1149
1217
|
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
1218
|
|
|
@@ -1422,7 +1490,8 @@ def tsp_symmetric_streaming(
|
|
|
1422
1490
|
G_func,
|
|
1423
1491
|
nodes,
|
|
1424
1492
|
k_neighbors=20,
|
|
1425
|
-
is_top_level=True
|
|
1493
|
+
is_top_level=True,
|
|
1494
|
+
is_parallel=True
|
|
1426
1495
|
):
|
|
1427
1496
|
dtype = opencl_context.dtype
|
|
1428
1497
|
n_nodes = len(nodes)
|
|
@@ -1437,17 +1506,25 @@ def tsp_symmetric_streaming(
|
|
|
1437
1506
|
|
|
1438
1507
|
a, b = monte_carlo_loop(n_nodes)
|
|
1439
1508
|
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1509
|
+
if is_parallel and len(a) > 3 and len(b) > 3:
|
|
1510
|
+
args = (
|
|
1511
|
+
(G_func, a, 0, False, True),
|
|
1512
|
+
(G_func, b, 0, False, True)
|
|
1513
|
+
)
|
|
1514
|
+
with NestablePool(2) as pool:
|
|
1515
|
+
sol_a, sol_b = pool.starmap(tsp_symmetric_streaming, args)
|
|
1516
|
+
else:
|
|
1517
|
+
sol_a = tsp_symmetric_streaming(
|
|
1518
|
+
G_func,
|
|
1519
|
+
a,
|
|
1520
|
+
is_top_level=False,
|
|
1521
|
+
k_neighbors=0
|
|
1522
|
+
)
|
|
1523
|
+
sol_b = tsp_symmetric_streaming(
|
|
1524
|
+
G_func,
|
|
1525
|
+
b,
|
|
1526
|
+
is_top_level=False,
|
|
1527
|
+
k_neighbors=0
|
|
1528
|
+
)
|
|
1452
1529
|
|
|
1453
1530
|
return tsp_symmetric_streaming_driver(G_func, is_top_level, k_neighbors, nodes, sol_a[0], sol_b[0])
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
from .maxcut_tfim_util import to_scipy_sparse_upper_triangular
|
|
1
|
+
from .maxcut_tfim_util import binary_search, to_scipy_sparse_upper_triangular
|
|
2
2
|
from .tsp import tsp_symmetric, tsp_symmetric_sparse, tsp_symmetric_streaming
|
|
3
3
|
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
8
|
|
|
8
9
|
|
|
10
|
+
@njit
|
|
9
11
|
def tsp_to_maxcut_bipartition(tsp_path, weights):
|
|
10
12
|
n = len(tsp_path)
|
|
11
13
|
best_cut_value = -float('inf')
|
|
@@ -16,11 +18,10 @@ def tsp_to_maxcut_bipartition(tsp_path, weights):
|
|
|
16
18
|
mid = n // 2 + offset
|
|
17
19
|
A = tsp_path[:mid]
|
|
18
20
|
B = tsp_path[mid:]
|
|
19
|
-
cut_value =
|
|
20
|
-
|
|
21
|
-
for
|
|
22
|
-
|
|
23
|
-
)
|
|
21
|
+
cut_value = 0.0
|
|
22
|
+
for u in A:
|
|
23
|
+
for v in B:
|
|
24
|
+
cut_value += weights[u, v]
|
|
24
25
|
if cut_value > best_cut_value:
|
|
25
26
|
best_cut_value = cut_value
|
|
26
27
|
best_partition = (A, B)
|
|
@@ -37,11 +38,10 @@ def tsp_to_maxcut_bipartition(tsp_path, weights):
|
|
|
37
38
|
mid = n // 2 + offset
|
|
38
39
|
A = tsp_path[:mid]
|
|
39
40
|
B = tsp_path[mid:]
|
|
40
|
-
cut_value =
|
|
41
|
-
|
|
42
|
-
for
|
|
43
|
-
|
|
44
|
-
)
|
|
41
|
+
cut_value = 0.0
|
|
42
|
+
for u in A:
|
|
43
|
+
for v in B:
|
|
44
|
+
cut_value += weights[u, v]
|
|
45
45
|
if cut_value > best_cut_value:
|
|
46
46
|
best_cut_value = cut_value
|
|
47
47
|
best_partition = (A, B)
|
|
@@ -50,6 +50,8 @@ def tsp_to_maxcut_bipartition(tsp_path, weights):
|
|
|
50
50
|
|
|
51
51
|
return best_partition, best_cut_value
|
|
52
52
|
|
|
53
|
+
|
|
54
|
+
@njit
|
|
53
55
|
def get_cut(bitstring, nodes):
|
|
54
56
|
solution = list(bitstring)
|
|
55
57
|
l, r = [], []
|
|
@@ -61,7 +63,17 @@ def get_cut(bitstring, nodes):
|
|
|
61
63
|
|
|
62
64
|
return l, r
|
|
63
65
|
|
|
64
|
-
|
|
66
|
+
|
|
67
|
+
@njit
|
|
68
|
+
def get_bitstring(partition, nodes):
|
|
69
|
+
bitstring = ""
|
|
70
|
+
for node in nodes:
|
|
71
|
+
bitstring += "0" if node in partition[0] else "1"
|
|
72
|
+
|
|
73
|
+
return bitstring
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def tsp_maxcut(G, k_neighbors=20, is_optimized=False, is_parallel=True, **kwargs):
|
|
65
77
|
G_m = None
|
|
66
78
|
if isinstance(G, nx.Graph):
|
|
67
79
|
nodes = list(G.nodes())
|
|
@@ -72,9 +84,20 @@ def tsp_maxcut(G, k_neighbors=20, **kwargs):
|
|
|
72
84
|
nodes = list(range(n_qubits))
|
|
73
85
|
G_m = G
|
|
74
86
|
|
|
75
|
-
path, length = tsp_symmetric(G, is_cyclic=False, monte_carlo=True, k_neighbors=k_neighbors)
|
|
87
|
+
path, length = tsp_symmetric(G, is_cyclic=False, monte_carlo=True, k_neighbors=k_neighbors, is_parallel=is_parallel)
|
|
76
88
|
partition, cut_value = tsp_to_maxcut_bipartition(path, G_m)
|
|
77
89
|
|
|
90
|
+
if not is_optimized:
|
|
91
|
+
bitstring = get_bitstring(partition, nodes)
|
|
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)
|
|
98
|
+
|
|
99
|
+
return bitstring, cut_value, partition, energy
|
|
100
|
+
|
|
78
101
|
bitint = 0
|
|
79
102
|
for b in partition[0]:
|
|
80
103
|
bitint |= 1 << b
|
|
@@ -85,7 +108,61 @@ def tsp_maxcut(G, k_neighbors=20, **kwargs):
|
|
|
85
108
|
return bitstring, cut_value, (l, r), energy
|
|
86
109
|
|
|
87
110
|
|
|
88
|
-
|
|
111
|
+
@njit
|
|
112
|
+
def tsp_to_maxcut_bipartition_sparse(tsp_path, G_data, G_rows, G_cols):
|
|
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 = 0.0
|
|
123
|
+
for u in A:
|
|
124
|
+
for v in B:
|
|
125
|
+
low, high = (u, v) if u < v else (v, u)
|
|
126
|
+
start = G_rows[low]
|
|
127
|
+
end = G_rows[low + 1]
|
|
128
|
+
i = binary_search(G_cols[start:end], high) + start
|
|
129
|
+
if i < end:
|
|
130
|
+
cut_value += G_data[i]
|
|
131
|
+
if cut_value > best_cut_value:
|
|
132
|
+
best_cut_value = cut_value
|
|
133
|
+
best_partition = (A, B)
|
|
134
|
+
direction = offset
|
|
135
|
+
|
|
136
|
+
if direction == 0:
|
|
137
|
+
return best_partition, best_cut_value
|
|
138
|
+
|
|
139
|
+
improved = True
|
|
140
|
+
best_offset = direction
|
|
141
|
+
while improved:
|
|
142
|
+
improved = False
|
|
143
|
+
offset = best_offset + direction
|
|
144
|
+
mid = n // 2 + offset
|
|
145
|
+
A = tsp_path[:mid]
|
|
146
|
+
B = tsp_path[mid:]
|
|
147
|
+
cut_value = 0.0
|
|
148
|
+
for u in A:
|
|
149
|
+
for v in B:
|
|
150
|
+
low, high = (u, v) if u < v else (v, u)
|
|
151
|
+
start = G_rows[low]
|
|
152
|
+
end = G_rows[low + 1]
|
|
153
|
+
i = binary_search(G_cols[start:end], high) + start
|
|
154
|
+
if i < end:
|
|
155
|
+
cut_value += G_data[i]
|
|
156
|
+
if cut_value > best_cut_value:
|
|
157
|
+
best_cut_value = cut_value
|
|
158
|
+
best_partition = (A, B)
|
|
159
|
+
best_offset = offset
|
|
160
|
+
improved = True
|
|
161
|
+
|
|
162
|
+
return best_partition, best_cut_value
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def tsp_maxcut_sparse(G, k_neighbors=20, is_optimized=False, is_parallel=True, **kwargs):
|
|
89
166
|
G_m = None
|
|
90
167
|
if isinstance(G, nx.Graph):
|
|
91
168
|
nodes = list(G.nodes())
|
|
@@ -96,8 +173,19 @@ def tsp_maxcut_sparse(G, k_neighbors=20, **kwargs):
|
|
|
96
173
|
nodes = list(range(n_qubits))
|
|
97
174
|
G_m = G
|
|
98
175
|
|
|
99
|
-
path, length = tsp_symmetric_sparse(G, k_neighbors=k_neighbors)
|
|
100
|
-
partition, cut_value =
|
|
176
|
+
path, length = tsp_symmetric_sparse(G, k_neighbors=k_neighbors, is_parallel=is_parallel)
|
|
177
|
+
partition, cut_value = tsp_to_maxcut_bipartition_sparse(path, G_m.data, G_m.indptr, G_m.indices)
|
|
178
|
+
|
|
179
|
+
if not is_optimized:
|
|
180
|
+
bitstring = get_bitstring(partition, nodes)
|
|
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)
|
|
187
|
+
|
|
188
|
+
return bitstring, cut_value, partition, energy
|
|
101
189
|
|
|
102
190
|
bitint = 0
|
|
103
191
|
for b in partition[0]:
|
|
@@ -154,10 +242,22 @@ def tsp_to_maxcut_bipartition_streaming(tsp_path, G_func):
|
|
|
154
242
|
return best_partition, best_cut_value
|
|
155
243
|
|
|
156
244
|
|
|
157
|
-
def tsp_maxcut_streaming(G_func, nodes, k_neighbors=20, **kwargs):
|
|
158
|
-
|
|
245
|
+
def tsp_maxcut_streaming(G_func, nodes, k_neighbors=20, is_optimized=False, is_parallel=True, **kwargs):
|
|
246
|
+
n_qubits = len(nodes)
|
|
247
|
+
path, length = tsp_symmetric_streaming(G_func, nodes, k_neighbors=k_neighbors, is_parallel=is_parallel)
|
|
159
248
|
partition, cut_value = tsp_to_maxcut_bipartition_streaming(path, G_func)
|
|
160
249
|
|
|
250
|
+
if not is_optimized:
|
|
251
|
+
bitstring = get_bitstring(partition, nodes)
|
|
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)
|
|
258
|
+
|
|
259
|
+
return bitstring, cut_value, partition, energy
|
|
260
|
+
|
|
161
261
|
bitint = 0
|
|
162
262
|
for b in partition[0]:
|
|
163
263
|
bitint |= 1 << b
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='pyqrackising',
|
|
54
|
-
version='7.
|
|
54
|
+
version='7.4.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)',
|
|
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
|