PyQrackIsing 7.3.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.3.0/pyqrackising.egg-info → pyqrackising-7.4.1}/PKG-INFO +1 -1
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/pyproject.toml +1 -1
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/pyqrackising/tsp.py +117 -78
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/pyqrackising/tsp_maxcut.py +107 -62
- {pyqrackising-7.3.0 → pyqrackising-7.4.1/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/setup.py +1 -1
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/CMakeLists.txt +0 -0
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/LICENSE.md +0 -0
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/MANIFEST.in +0 -0
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/README.md +0 -0
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/pyqrackising/__init__.py +0 -0
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/pyqrackising/maxcut_tfim.py +0 -0
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/pyqrackising/maxcut_tfim_sparse.py +0 -0
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/pyqrackising/maxcut_tfim_streaming.py +0 -0
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/pyqrackising/maxcut_tfim_util.py +0 -0
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/pyqrackising/spin_glass_solver.py +0 -0
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/pyqrackising/spin_glass_solver_sparse.py +0 -0
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/pyqrackising/spin_glass_solver_streaming.py +0 -0
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/pyqrackising/spin_glass_solver_util.py +0 -0
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-7.3.0 → pyqrackising-7.4.1}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-7.3.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.
|
|
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,9 +1,12 @@
|
|
|
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
6
|
import networkx as nx
|
|
5
7
|
from numba import njit
|
|
6
8
|
import numpy as np
|
|
9
|
+
import os
|
|
7
10
|
from scipy.sparse import lil_matrix
|
|
8
11
|
|
|
9
12
|
|
|
@@ -516,7 +519,8 @@ def tsp_symmetric(
|
|
|
516
519
|
k_neighbors=20,
|
|
517
520
|
is_cyclic=True,
|
|
518
521
|
multi_start=1,
|
|
519
|
-
is_top_level=True
|
|
522
|
+
is_top_level=True,
|
|
523
|
+
is_parallel=False
|
|
520
524
|
):
|
|
521
525
|
dtype = opencl_context.dtype
|
|
522
526
|
nodes = None
|
|
@@ -576,32 +580,40 @@ def tsp_symmetric(
|
|
|
576
580
|
|
|
577
581
|
G_a, G_b = init_G_a_b(G_m, a, b)
|
|
578
582
|
|
|
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
|
-
|
|
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()
|
|
588
|
+
else:
|
|
589
|
+
sol_a = tsp_symmetric(
|
|
590
|
+
G_a,
|
|
591
|
+
quality=quality,
|
|
592
|
+
shots=shots,
|
|
593
|
+
is_alt_gpu_sampling=is_alt_gpu_sampling,
|
|
594
|
+
is_base_maxcut_gpu=is_base_maxcut_gpu,
|
|
595
|
+
is_combo_maxcut_gpu=is_combo_maxcut_gpu,
|
|
596
|
+
monte_carlo=monte_carlo,
|
|
597
|
+
is_cyclic=False,
|
|
598
|
+
is_top_level=False,
|
|
599
|
+
k_neighbors=0,
|
|
600
|
+
multi_start=multi_start,
|
|
601
|
+
is_parallel=False
|
|
602
|
+
)
|
|
603
|
+
sol_b = tsp_symmetric(
|
|
604
|
+
G_b,
|
|
605
|
+
quality=quality,
|
|
606
|
+
shots=shots,
|
|
607
|
+
is_alt_gpu_sampling=is_alt_gpu_sampling,
|
|
608
|
+
is_base_maxcut_gpu=is_base_maxcut_gpu,
|
|
609
|
+
is_combo_maxcut_gpu=is_combo_maxcut_gpu,
|
|
610
|
+
monte_carlo=monte_carlo,
|
|
611
|
+
is_cyclic=False,
|
|
612
|
+
is_top_level=False,
|
|
613
|
+
k_neighbors=0,
|
|
614
|
+
multi_start=multi_start,
|
|
615
|
+
is_parallel=False
|
|
616
|
+
)
|
|
605
617
|
|
|
606
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)
|
|
607
619
|
|
|
@@ -701,7 +713,8 @@ def tsp_asymmetric(
|
|
|
701
713
|
k_neighbors=20,
|
|
702
714
|
is_cyclic=True,
|
|
703
715
|
multi_start=1,
|
|
704
|
-
is_top_level=True
|
|
716
|
+
is_top_level=True,
|
|
717
|
+
is_parallel=False
|
|
705
718
|
):
|
|
706
719
|
dtype = opencl_context.dtype
|
|
707
720
|
nodes = None
|
|
@@ -765,32 +778,40 @@ def tsp_asymmetric(
|
|
|
765
778
|
|
|
766
779
|
G_a, G_b = init_G_a_b(G_m, a, b)
|
|
767
780
|
|
|
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
|
-
|
|
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()
|
|
786
|
+
else:
|
|
787
|
+
sol_a = tsp_asymmetric(
|
|
788
|
+
G_a,
|
|
789
|
+
quality=quality,
|
|
790
|
+
shots=shots,
|
|
791
|
+
is_alt_gpu_sampling=is_alt_gpu_sampling,
|
|
792
|
+
is_base_maxcut_gpu=is_base_maxcut_gpu,
|
|
793
|
+
is_combo_maxcut_gpu=is_combo_maxcut_gpu,
|
|
794
|
+
monte_carlo=monte_carlo,
|
|
795
|
+
is_cyclic=False,
|
|
796
|
+
is_top_level=False,
|
|
797
|
+
k_neighbors=0,
|
|
798
|
+
multi_start=multi_start,
|
|
799
|
+
is_parallel=False
|
|
800
|
+
)
|
|
801
|
+
sol_b = tsp_asymmetric(
|
|
802
|
+
G_b,
|
|
803
|
+
quality=quality,
|
|
804
|
+
shots=shots,
|
|
805
|
+
is_alt_gpu_sampling=is_alt_gpu_sampling,
|
|
806
|
+
is_base_maxcut_gpu=is_base_maxcut_gpu,
|
|
807
|
+
is_combo_maxcut_gpu=is_combo_maxcut_gpu,
|
|
808
|
+
monte_carlo=monte_carlo,
|
|
809
|
+
is_cyclic=False,
|
|
810
|
+
is_top_level=False,
|
|
811
|
+
k_neighbors=0,
|
|
812
|
+
multi_start=multi_start,
|
|
813
|
+
is_parallel=False
|
|
814
|
+
)
|
|
794
815
|
|
|
795
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)
|
|
796
817
|
|
|
@@ -1126,7 +1147,8 @@ def tsp_symmetric_sparse_driver(G_data, G_rows, G_cols, is_top_level, k_neighbor
|
|
|
1126
1147
|
def tsp_symmetric_sparse(
|
|
1127
1148
|
G,
|
|
1128
1149
|
k_neighbors=20,
|
|
1129
|
-
is_top_level=True
|
|
1150
|
+
is_top_level=True,
|
|
1151
|
+
is_parallel=True
|
|
1130
1152
|
):
|
|
1131
1153
|
dtype = opencl_context.dtype
|
|
1132
1154
|
nodes = None
|
|
@@ -1153,16 +1175,24 @@ def tsp_symmetric_sparse(
|
|
|
1153
1175
|
|
|
1154
1176
|
G_a, G_b = init_G_a_b_sparse(G_m, a, b, dtype)
|
|
1155
1177
|
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
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()
|
|
1183
|
+
else:
|
|
1184
|
+
sol_a = tsp_symmetric_sparse(
|
|
1185
|
+
G_a,
|
|
1186
|
+
is_top_level=False,
|
|
1187
|
+
k_neighbors=0,
|
|
1188
|
+
is_parallel=False
|
|
1189
|
+
)
|
|
1190
|
+
sol_b = tsp_symmetric_sparse(
|
|
1191
|
+
G_b,
|
|
1192
|
+
is_top_level=False,
|
|
1193
|
+
k_neighbors=0,
|
|
1194
|
+
is_parallel=False
|
|
1195
|
+
)
|
|
1166
1196
|
|
|
1167
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)
|
|
1168
1198
|
|
|
@@ -1440,7 +1470,8 @@ def tsp_symmetric_streaming(
|
|
|
1440
1470
|
G_func,
|
|
1441
1471
|
nodes,
|
|
1442
1472
|
k_neighbors=20,
|
|
1443
|
-
is_top_level=True
|
|
1473
|
+
is_top_level=True,
|
|
1474
|
+
is_parallel=True
|
|
1444
1475
|
):
|
|
1445
1476
|
dtype = opencl_context.dtype
|
|
1446
1477
|
n_nodes = len(nodes)
|
|
@@ -1455,17 +1486,25 @@ def tsp_symmetric_streaming(
|
|
|
1455
1486
|
|
|
1456
1487
|
a, b = monte_carlo_loop(n_nodes)
|
|
1457
1488
|
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
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()
|
|
1494
|
+
else:
|
|
1495
|
+
sol_a = tsp_symmetric_streaming(
|
|
1496
|
+
G_func,
|
|
1497
|
+
a,
|
|
1498
|
+
is_top_level=False,
|
|
1499
|
+
k_neighbors=0,
|
|
1500
|
+
is_parallel=False
|
|
1501
|
+
)
|
|
1502
|
+
sol_b = tsp_symmetric_streaming(
|
|
1503
|
+
G_func,
|
|
1504
|
+
b,
|
|
1505
|
+
is_top_level=False,
|
|
1506
|
+
k_neighbors=0,
|
|
1507
|
+
is_parallel=False
|
|
1508
|
+
)
|
|
1470
1509
|
|
|
1471
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,7 +78,21 @@ def get_bitstring(partition, nodes):
|
|
|
73
78
|
return bitstring
|
|
74
79
|
|
|
75
80
|
|
|
76
|
-
|
|
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
|
+
|
|
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):
|
|
79
98
|
nodes = list(G.nodes())
|
|
@@ -84,17 +103,12 @@ def tsp_maxcut(G, k_neighbors=20, is_optimized=False, **kwargs):
|
|
|
84
103
|
nodes = list(range(n_qubits))
|
|
85
104
|
G_m = G
|
|
86
105
|
|
|
87
|
-
path, length = tsp_symmetric(G, is_cyclic=False, monte_carlo=True, k_neighbors=k_neighbors)
|
|
88
|
-
|
|
106
|
+
path, length = tsp_symmetric(G, is_cyclic=False, monte_carlo=True, k_neighbors=k_neighbors, is_parallel=is_parallel)
|
|
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, **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,14 +173,33 @@ 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
|
-
def tsp_maxcut_sparse(G, k_neighbors=20, is_optimized=False, **kwargs):
|
|
202
|
+
def tsp_maxcut_sparse(G, k_neighbors=20, is_optimized=False, is_parallel=True, **kwargs):
|
|
166
203
|
G_m = None
|
|
167
204
|
if isinstance(G, nx.Graph):
|
|
168
205
|
nodes = list(G.nodes())
|
|
@@ -173,17 +210,12 @@ def tsp_maxcut_sparse(G, k_neighbors=20, is_optimized=False, **kwargs):
|
|
|
173
210
|
nodes = list(range(n_qubits))
|
|
174
211
|
G_m = G
|
|
175
212
|
|
|
176
|
-
path, length = tsp_symmetric_sparse(G, k_neighbors=k_neighbors)
|
|
177
|
-
|
|
213
|
+
path, length = tsp_symmetric_sparse(G, k_neighbors=k_neighbors, is_parallel=is_parallel)
|
|
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, **kwargs):
|
|
|
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
|
|
243
279
|
|
|
244
280
|
|
|
245
|
-
|
|
281
|
+
@njit(parallel=True)
|
|
282
|
+
def early_exit_streaming(G_func, partition, nodes):
|
|
246
283
|
n_qubits = len(nodes)
|
|
247
|
-
|
|
248
|
-
|
|
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
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
def tsp_maxcut_streaming(G_func, nodes, k_neighbors=20, is_optimized=False, is_parallel=True, **kwargs):
|
|
296
|
+
n_qubits = len(nodes)
|
|
297
|
+
path, length = tsp_symmetric_streaming(G_func, nodes, k_neighbors=k_neighbors, is_parallel=is_parallel)
|
|
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.
|
|
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
|