PyQrackIsing 7.4.0__tar.gz → 7.4.1__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.1}/PKG-INFO +1 -1
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/pyproject.toml +1 -1
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/pyqrackising/tsp.py +39 -59
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/pyqrackising/tsp_maxcut.py +101 -56
- {pyqrackising-7.4.0 → pyqrackising-7.4.1/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/setup.py +1 -1
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/CMakeLists.txt +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/LICENSE.md +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/MANIFEST.in +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/README.md +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/pyqrackising/__init__.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/pyqrackising/maxcut_tfim.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/pyqrackising/maxcut_tfim_sparse.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/pyqrackising/maxcut_tfim_streaming.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/pyqrackising/maxcut_tfim_util.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/pyqrackising/spin_glass_solver.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/pyqrackising/spin_glass_solver_sparse.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/pyqrackising/spin_glass_solver_streaming.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/pyqrackising/spin_glass_solver_util.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-7.4.0 → pyqrackising-7.4.1}/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.1"
|
|
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):
|
|
@@ -600,13 +580,11 @@ def tsp_symmetric(
|
|
|
600
580
|
|
|
601
581
|
G_a, G_b = init_G_a_b(G_m, a, b)
|
|
602
582
|
|
|
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)
|
|
583
|
+
if monte_carlo and is_parallel and (len(a) > 3) and (len(b) > 3):
|
|
584
|
+
with ThreadPoolExecutor(max_workers=1) as executor:
|
|
585
|
+
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)
|
|
586
|
+
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)
|
|
587
|
+
sol_a = f.result()
|
|
610
588
|
else:
|
|
611
589
|
sol_a = tsp_symmetric(
|
|
612
590
|
G_a,
|
|
@@ -619,7 +597,8 @@ def tsp_symmetric(
|
|
|
619
597
|
is_cyclic=False,
|
|
620
598
|
is_top_level=False,
|
|
621
599
|
k_neighbors=0,
|
|
622
|
-
multi_start=multi_start
|
|
600
|
+
multi_start=multi_start,
|
|
601
|
+
is_parallel=False
|
|
623
602
|
)
|
|
624
603
|
sol_b = tsp_symmetric(
|
|
625
604
|
G_b,
|
|
@@ -632,7 +611,8 @@ def tsp_symmetric(
|
|
|
632
611
|
is_cyclic=False,
|
|
633
612
|
is_top_level=False,
|
|
634
613
|
k_neighbors=0,
|
|
635
|
-
multi_start=multi_start
|
|
614
|
+
multi_start=multi_start,
|
|
615
|
+
is_parallel=False
|
|
636
616
|
)
|
|
637
617
|
|
|
638
618
|
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)
|
|
@@ -798,13 +778,11 @@ def tsp_asymmetric(
|
|
|
798
778
|
|
|
799
779
|
G_a, G_b = init_G_a_b(G_m, a, b)
|
|
800
780
|
|
|
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)
|
|
781
|
+
if monte_carlo and is_parallel and (len(a) > 2) and (len(b) > 2):
|
|
782
|
+
with ThreadPoolExecutor(max_workers=1) as executor:
|
|
783
|
+
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)
|
|
784
|
+
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)
|
|
785
|
+
sol_a = f.result()
|
|
808
786
|
else:
|
|
809
787
|
sol_a = tsp_asymmetric(
|
|
810
788
|
G_a,
|
|
@@ -817,7 +795,8 @@ def tsp_asymmetric(
|
|
|
817
795
|
is_cyclic=False,
|
|
818
796
|
is_top_level=False,
|
|
819
797
|
k_neighbors=0,
|
|
820
|
-
multi_start=multi_start
|
|
798
|
+
multi_start=multi_start,
|
|
799
|
+
is_parallel=False
|
|
821
800
|
)
|
|
822
801
|
sol_b = tsp_asymmetric(
|
|
823
802
|
G_b,
|
|
@@ -830,7 +809,8 @@ def tsp_asymmetric(
|
|
|
830
809
|
is_cyclic=False,
|
|
831
810
|
is_top_level=False,
|
|
832
811
|
k_neighbors=0,
|
|
833
|
-
multi_start=multi_start
|
|
812
|
+
multi_start=multi_start,
|
|
813
|
+
is_parallel=False
|
|
834
814
|
)
|
|
835
815
|
|
|
836
816
|
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)
|
|
@@ -1195,23 +1175,23 @@ def tsp_symmetric_sparse(
|
|
|
1195
1175
|
|
|
1196
1176
|
G_a, G_b = init_G_a_b_sparse(G_m, a, b, dtype)
|
|
1197
1177
|
|
|
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)
|
|
1178
|
+
if is_parallel and (len(a) > 3) and (len(b) > 3):
|
|
1179
|
+
with ThreadPoolExecutor(max_workers=1) as executor:
|
|
1180
|
+
f = executor.submit(tsp_symmetric_sparse, G_a, False, True)
|
|
1181
|
+
sol_b = tsp_symmetric_sparse(G_b, False, True)
|
|
1182
|
+
sol_a = f.result()
|
|
1205
1183
|
else:
|
|
1206
1184
|
sol_a = tsp_symmetric_sparse(
|
|
1207
1185
|
G_a,
|
|
1208
1186
|
is_top_level=False,
|
|
1209
|
-
k_neighbors=0
|
|
1187
|
+
k_neighbors=0,
|
|
1188
|
+
is_parallel=False
|
|
1210
1189
|
)
|
|
1211
1190
|
sol_b = tsp_symmetric_sparse(
|
|
1212
1191
|
G_b,
|
|
1213
1192
|
is_top_level=False,
|
|
1214
|
-
k_neighbors=0
|
|
1193
|
+
k_neighbors=0,
|
|
1194
|
+
is_parallel=False
|
|
1215
1195
|
)
|
|
1216
1196
|
|
|
1217
1197
|
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)
|
|
@@ -1506,25 +1486,25 @@ def tsp_symmetric_streaming(
|
|
|
1506
1486
|
|
|
1507
1487
|
a, b = monte_carlo_loop(n_nodes)
|
|
1508
1488
|
|
|
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)
|
|
1489
|
+
if is_parallel and (len(a) > 3) and (len(b) > 3):
|
|
1490
|
+
with ThreadPoolExecutor(max_workers=1) as executor:
|
|
1491
|
+
f = executor.submit(tsp_symmetric_streaming, G_func, a, False, True)
|
|
1492
|
+
sol_b = tsp_symmetric_streaming(G_func, b, False, True)
|
|
1493
|
+
sol_a = f.result()
|
|
1516
1494
|
else:
|
|
1517
1495
|
sol_a = tsp_symmetric_streaming(
|
|
1518
1496
|
G_func,
|
|
1519
1497
|
a,
|
|
1520
1498
|
is_top_level=False,
|
|
1521
|
-
k_neighbors=0
|
|
1499
|
+
k_neighbors=0,
|
|
1500
|
+
is_parallel=False
|
|
1522
1501
|
)
|
|
1523
1502
|
sol_b = tsp_symmetric_streaming(
|
|
1524
1503
|
G_func,
|
|
1525
1504
|
b,
|
|
1526
1505
|
is_top_level=False,
|
|
1527
|
-
k_neighbors=0
|
|
1506
|
+
k_neighbors=0,
|
|
1507
|
+
is_parallel=False
|
|
1528
1508
|
)
|
|
1529
1509
|
|
|
1530
1510
|
return tsp_symmetric_streaming_driver(G_func, is_top_level, k_neighbors, nodes, sol_a[0], sol_b[0])
|
|
@@ -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.1',
|
|
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
|