PyQrackIsing 3.0.2__tar.gz → 3.1.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-3.0.2/pyqrackising.egg-info → pyqrackising-3.1.0}/PKG-INFO +1 -1
- {pyqrackising-3.0.2 → pyqrackising-3.1.0}/pyproject.toml +1 -1
- {pyqrackising-3.0.2 → pyqrackising-3.1.0}/pyqrackising/__init__.py +1 -0
- pyqrackising-3.1.0/pyqrackising/tsp_asymmetric.py +284 -0
- {pyqrackising-3.0.2 → pyqrackising-3.1.0}/pyqrackising/tsp_symmetric.py +57 -1
- {pyqrackising-3.0.2 → pyqrackising-3.1.0/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-3.0.2 → pyqrackising-3.1.0}/pyqrackising.egg-info/SOURCES.txt +1 -0
- {pyqrackising-3.0.2 → pyqrackising-3.1.0}/setup.py +1 -1
- {pyqrackising-3.0.2 → pyqrackising-3.1.0}/CMakeLists.txt +0 -0
- {pyqrackising-3.0.2 → pyqrackising-3.1.0}/LICENSE.md +0 -0
- {pyqrackising-3.0.2 → pyqrackising-3.1.0}/MANIFEST.in +0 -0
- {pyqrackising-3.0.2 → pyqrackising-3.1.0}/README.md +0 -0
- {pyqrackising-3.0.2 → pyqrackising-3.1.0}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-3.0.2 → pyqrackising-3.1.0}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-3.0.2 → pyqrackising-3.1.0}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-3.0.2 → pyqrackising-3.1.0}/pyqrackising/maxcut_tfim.py +0 -0
- {pyqrackising-3.0.2 → pyqrackising-3.1.0}/pyqrackising/spin_glass_solver.py +0 -0
- {pyqrackising-3.0.2 → pyqrackising-3.1.0}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-3.0.2 → pyqrackising-3.1.0}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-3.0.2 → pyqrackising-3.1.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-3.0.2 → pyqrackising-3.1.0}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-3.0.2 → pyqrackising-3.1.0}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-3.0.2 → pyqrackising-3.1.0}/setup.cfg +0 -0
|
@@ -11,7 +11,7 @@ build-backend = "setuptools.build_meta"
|
|
|
11
11
|
|
|
12
12
|
[project]
|
|
13
13
|
name = "pyqrackising"
|
|
14
|
-
version = "3.0
|
|
14
|
+
version = "3.1.0"
|
|
15
15
|
requires-python = ">=3.8"
|
|
16
16
|
description = "Near-ideal closed-form solutions for transverse field Ising model (TFIM)"
|
|
17
17
|
readme = {file = "README.txt", content-type = "text/markdown"}
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
# Mapping from ATSP to TSP, wholly by Elara (the OpenAI GPT)
|
|
2
|
+
|
|
3
|
+
from .tsp_symmetric import tsp_symmetric
|
|
4
|
+
import networkx as nx
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def atsp_to_symmetric(G_asym):
|
|
9
|
+
"""
|
|
10
|
+
Transform an asymmetric TSP (ATSP) instance into a symmetric TSP instance
|
|
11
|
+
using the standard 2n-node construction.
|
|
12
|
+
|
|
13
|
+
Args:
|
|
14
|
+
G_asym (np.ndarray): n x n adjacency matrix of ATSP distances.
|
|
15
|
+
Assumes G_asym[i, i] = 0.
|
|
16
|
+
|
|
17
|
+
Returns:
|
|
18
|
+
G_sym (np.ndarray): 2n x 2n symmetric adjacency matrix
|
|
19
|
+
corresponding to the transformed TSP.
|
|
20
|
+
"""
|
|
21
|
+
n = len(G_asym)
|
|
22
|
+
N = 2 * n
|
|
23
|
+
G_sym = np.full((N, N), np.inf, dtype=np.float64)
|
|
24
|
+
|
|
25
|
+
for i in range(n):
|
|
26
|
+
in_i = 2 * i
|
|
27
|
+
out_i = 2 * i + 1
|
|
28
|
+
|
|
29
|
+
# Zero edge between in-node and out-node
|
|
30
|
+
G_sym[in_i, out_i] = 0.0
|
|
31
|
+
G_sym[out_i, in_i] = 0.0 # symmetric
|
|
32
|
+
|
|
33
|
+
# Map asymmetric costs into symmetric structure
|
|
34
|
+
for j in range(n):
|
|
35
|
+
if i == j:
|
|
36
|
+
continue
|
|
37
|
+
in_j = 2 * j
|
|
38
|
+
out_j = 2 * j + 1
|
|
39
|
+
cost = G_asym[i, j]
|
|
40
|
+
G_sym[out_i, in_j] = cost
|
|
41
|
+
G_sym[in_j, out_i] = cost # symmetric
|
|
42
|
+
|
|
43
|
+
# Replace infinities with a large number (or leave as inf if solver can handle it)
|
|
44
|
+
G_sym[G_sym == np.inf] = 1e222
|
|
45
|
+
# (Note that Earth's circumference in Planck units would be ~2.5e42.)
|
|
46
|
+
|
|
47
|
+
return G_sym
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def symmetric_to_atsp_path(sym_path, n):
|
|
51
|
+
"""
|
|
52
|
+
Convert a tour from the symmetric 2n-node TSP back to an n-node ATSP tour.
|
|
53
|
+
|
|
54
|
+
Args:
|
|
55
|
+
sym_path (list[int]): Tour from tsp_symmetric() on 2n-node graph.
|
|
56
|
+
n (int): Number of cities in the original ATSP.
|
|
57
|
+
|
|
58
|
+
Returns:
|
|
59
|
+
atsp_path (list[int]): Tour over the original n nodes.
|
|
60
|
+
"""
|
|
61
|
+
atsp_path = []
|
|
62
|
+
for node in sym_path:
|
|
63
|
+
if node % 2 == 0: # only keep "in" nodes (even indices)
|
|
64
|
+
atsp_path.append(node // 2)
|
|
65
|
+
return atsp_path
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def digraph_to_asym_matrix(G):
|
|
69
|
+
"""
|
|
70
|
+
Convert a networkx.DiGraph to a numpy adjacency matrix.
|
|
71
|
+
"""
|
|
72
|
+
n = G.number_of_nodes()
|
|
73
|
+
mapping = dict(zip(G.nodes(), range(n)))
|
|
74
|
+
G = nx.relabel_nodes(G, mapping) # ensure 0..n-1
|
|
75
|
+
G_asym = np.full((n, n), np.inf, dtype=np.float64)
|
|
76
|
+
np.fill_diagonal(G_asym, 0.0)
|
|
77
|
+
|
|
78
|
+
for u, v, d in G.edges(data=True):
|
|
79
|
+
G_asym[u, v] = d.get("weight", 1.0)
|
|
80
|
+
|
|
81
|
+
return G_asym
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def symmetric_to_atsp_path_and_weight(sym_path, G_asym, nodes, is_cyclic):
|
|
85
|
+
"""
|
|
86
|
+
Convert a symmetric 2n-node TSP tour back into an ATSP tour
|
|
87
|
+
and compute its true weight.
|
|
88
|
+
|
|
89
|
+
Args:
|
|
90
|
+
sym_path (list[int]): Tour from tsp_symmetric() on 2n-node graph.
|
|
91
|
+
G_asym (np.ndarray): Original n x n asymmetric adjacency matrix.
|
|
92
|
+
|
|
93
|
+
Returns:
|
|
94
|
+
atsp_path (list[int]): Tour over original n nodes.
|
|
95
|
+
atsp_weight (float): Total weight of the ATSP tour.
|
|
96
|
+
"""
|
|
97
|
+
n = len(G_asym)
|
|
98
|
+
atsp_path = []
|
|
99
|
+
for node in sym_path:
|
|
100
|
+
if node % 2 == 0: # only keep "in" nodes
|
|
101
|
+
atsp_path.append(node // 2)
|
|
102
|
+
|
|
103
|
+
# Compute true ATSP weight
|
|
104
|
+
atsp_weight = 0.0
|
|
105
|
+
for i in range(len(atsp_path) - 1):
|
|
106
|
+
atsp_weight += G_asym[atsp_path[i], atsp_path[i+1]]
|
|
107
|
+
if is_cyclic:
|
|
108
|
+
atsp_weight += G_asym[atsp_path[-1], atsp_path[0]] # close cycle
|
|
109
|
+
|
|
110
|
+
return [nodes[x] for x in atsp_path], atsp_weight
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def tsp_asymmetric(G, start_node=None, end_node=None, quality=1, shots=None, correction_quality=2, monte_carlo=False, k_neighbors=20, is_cyclic=True, multi_start=1, is_top_level=True):
|
|
114
|
+
nodes = None
|
|
115
|
+
n_nodes = 0
|
|
116
|
+
G_m = None
|
|
117
|
+
if isinstance(G, nx.DiGraph):
|
|
118
|
+
nodes = list(G.nodes())
|
|
119
|
+
n_nodes = len(nodes)
|
|
120
|
+
G_asym = digraph_to_asym_matrix(G)
|
|
121
|
+
else:
|
|
122
|
+
n_nodes = len(G)
|
|
123
|
+
nodes = list(range(n_nodes))
|
|
124
|
+
G_asym = G
|
|
125
|
+
|
|
126
|
+
if is_cyclic:
|
|
127
|
+
start_node = None
|
|
128
|
+
end_node = None
|
|
129
|
+
|
|
130
|
+
# Transform to symmetric TSP
|
|
131
|
+
G_sym = atsp_to_symmetric(G_asym)
|
|
132
|
+
|
|
133
|
+
# Start/end node handling added by Dan Strano:
|
|
134
|
+
if start_node and end_node:
|
|
135
|
+
path0, weight0 = symmetric_to_atsp_path_and_weight(tsp_symmetric(
|
|
136
|
+
G_sym,
|
|
137
|
+
start_node=start_node,
|
|
138
|
+
end_node=end_node,
|
|
139
|
+
quality=quality,
|
|
140
|
+
shots=shots,
|
|
141
|
+
correction_quality=correction_quality,
|
|
142
|
+
monte_carlo=monte_carlo,
|
|
143
|
+
k_neighbors=k_neighbors,
|
|
144
|
+
is_cyclic=is_cyclic,
|
|
145
|
+
multi_start=multi_start,
|
|
146
|
+
is_top_level=is_top_level
|
|
147
|
+
)[0], G_asym, nodes, is_cyclic)
|
|
148
|
+
|
|
149
|
+
path1, weight1 = symmetric_to_atsp_path_and_weight(tsp_symmetric(
|
|
150
|
+
G_sym,
|
|
151
|
+
start_node=start_node + 1,
|
|
152
|
+
end_node=end_node,
|
|
153
|
+
quality=quality,
|
|
154
|
+
shots=shots,
|
|
155
|
+
correction_quality=correction_quality,
|
|
156
|
+
monte_carlo=monte_carlo,
|
|
157
|
+
k_neighbors=k_neighbors,
|
|
158
|
+
is_cyclic=is_cyclic,
|
|
159
|
+
multi_start=multi_start,
|
|
160
|
+
is_top_level=is_top_level
|
|
161
|
+
)[0], G_asym, nodes, is_cyclic)
|
|
162
|
+
|
|
163
|
+
path2, weight2 = symmetric_to_atsp_path_and_weight(tsp_symmetric(
|
|
164
|
+
G_sym,
|
|
165
|
+
start_node=start_node,
|
|
166
|
+
end_node=end_node + 1,
|
|
167
|
+
quality=quality,
|
|
168
|
+
shots=shots,
|
|
169
|
+
correction_quality=correction_quality,
|
|
170
|
+
monte_carlo=monte_carlo,
|
|
171
|
+
k_neighbors=k_neighbors,
|
|
172
|
+
is_cyclic=is_cyclic,
|
|
173
|
+
multi_start=multi_start,
|
|
174
|
+
is_top_level=is_top_level
|
|
175
|
+
)[0], G_asym, nodes, is_cyclic)
|
|
176
|
+
|
|
177
|
+
path3, weight3 = symmetric_to_atsp_path_and_weight(tsp_symmetric(
|
|
178
|
+
G_sym,
|
|
179
|
+
start_node=start_node + 1,
|
|
180
|
+
end_node=end_node + 1,
|
|
181
|
+
quality=quality,
|
|
182
|
+
shots=shots,
|
|
183
|
+
correction_quality=correction_quality,
|
|
184
|
+
monte_carlo=monte_carlo,
|
|
185
|
+
k_neighbors=k_neighbors,
|
|
186
|
+
is_cyclic=is_cyclic,
|
|
187
|
+
multi_start=multi_start,
|
|
188
|
+
is_top_level=is_top_level
|
|
189
|
+
)[0], G_asym, nodes, is_cyclic)
|
|
190
|
+
|
|
191
|
+
best_weight = min([weight0, weight1, weight2, weight3])
|
|
192
|
+
|
|
193
|
+
if best_weight == weight0:
|
|
194
|
+
return path0, weight0
|
|
195
|
+
|
|
196
|
+
if best_weight == weight1:
|
|
197
|
+
return path1, weight1
|
|
198
|
+
|
|
199
|
+
if best_weight == weight2:
|
|
200
|
+
return path2, weight2
|
|
201
|
+
|
|
202
|
+
return path3, weight3
|
|
203
|
+
|
|
204
|
+
if start_node:
|
|
205
|
+
path0, weight0 = symmetric_to_atsp_path_and_weight(tsp_symmetric(
|
|
206
|
+
G_sym,
|
|
207
|
+
start_node=start_node,
|
|
208
|
+
end_node=end_node,
|
|
209
|
+
quality=quality,
|
|
210
|
+
shots=shots,
|
|
211
|
+
correction_quality=correction_quality,
|
|
212
|
+
monte_carlo=monte_carlo,
|
|
213
|
+
k_neighbors=k_neighbors,
|
|
214
|
+
is_cyclic=is_cyclic,
|
|
215
|
+
multi_start=multi_start,
|
|
216
|
+
is_top_level=is_top_level
|
|
217
|
+
)[0], G_asym, nodes, is_cyclic)
|
|
218
|
+
|
|
219
|
+
path1, weight1 = symmetric_to_atsp_path_and_weight(tsp_symmetric(
|
|
220
|
+
G_sym,
|
|
221
|
+
start_node=start_node + 1,
|
|
222
|
+
end_node=end_node,
|
|
223
|
+
quality=quality,
|
|
224
|
+
shots=shots,
|
|
225
|
+
correction_quality=correction_quality,
|
|
226
|
+
monte_carlo=monte_carlo,
|
|
227
|
+
k_neighbors=k_neighbors,
|
|
228
|
+
is_cyclic=is_cyclic,
|
|
229
|
+
multi_start=multi_start,
|
|
230
|
+
is_top_level=is_top_level
|
|
231
|
+
)[0], G_asym, nodes, is_cyclic)
|
|
232
|
+
|
|
233
|
+
if weight0 < weight1:
|
|
234
|
+
return path0, weight0
|
|
235
|
+
|
|
236
|
+
return path1, weight1
|
|
237
|
+
|
|
238
|
+
if end_node:
|
|
239
|
+
path0, weight0 = symmetric_to_atsp_path_and_weight(tsp_symmetric(
|
|
240
|
+
G_sym,
|
|
241
|
+
start_node=start_node,
|
|
242
|
+
end_node=end_node,
|
|
243
|
+
quality=quality,
|
|
244
|
+
shots=shots,
|
|
245
|
+
correction_quality=correction_quality,
|
|
246
|
+
monte_carlo=monte_carlo,
|
|
247
|
+
k_neighbors=k_neighbors,
|
|
248
|
+
is_cyclic=is_cyclic,
|
|
249
|
+
multi_start=multi_start,
|
|
250
|
+
is_top_level=is_top_level
|
|
251
|
+
)[0], G_asym, nodes, is_cyclic)
|
|
252
|
+
|
|
253
|
+
path1, weight1 = symmetric_to_atsp_path_and_weight(tsp_symmetric(
|
|
254
|
+
G_sym,
|
|
255
|
+
start_node=start_node,
|
|
256
|
+
end_node=end_node + 1,
|
|
257
|
+
quality=quality,
|
|
258
|
+
shots=shots,
|
|
259
|
+
correction_quality=correction_quality,
|
|
260
|
+
monte_carlo=monte_carlo,
|
|
261
|
+
k_neighbors=k_neighbors,
|
|
262
|
+
is_cyclic=is_cyclic,
|
|
263
|
+
multi_start=multi_start,
|
|
264
|
+
is_top_level=is_top_level
|
|
265
|
+
)[0], G_asym, nodes, is_cyclic)
|
|
266
|
+
|
|
267
|
+
if weight0 < weight1:
|
|
268
|
+
return path0, weight0
|
|
269
|
+
|
|
270
|
+
return path1, weight1
|
|
271
|
+
|
|
272
|
+
# Solve
|
|
273
|
+
return symmetric_to_atsp_path_and_weight(tsp_symmetric(
|
|
274
|
+
G_sym,
|
|
275
|
+
start_node=start_node,
|
|
276
|
+
quality=quality,
|
|
277
|
+
shots=shots,
|
|
278
|
+
correction_quality=correction_quality,
|
|
279
|
+
monte_carlo=monte_carlo,
|
|
280
|
+
k_neighbors=k_neighbors,
|
|
281
|
+
is_cyclic=is_cyclic,
|
|
282
|
+
multi_start=multi_start,
|
|
283
|
+
is_top_level=is_top_level
|
|
284
|
+
)[0], G_asym, nodes, is_cyclic)
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
from .spin_glass_solver import spin_glass_solver
|
|
2
|
+
import itertools
|
|
2
3
|
import networkx as nx
|
|
3
4
|
from numba import njit
|
|
4
5
|
import numpy as np
|
|
@@ -266,6 +267,53 @@ def monte_carlo_loop(n_nodes):
|
|
|
266
267
|
return bits
|
|
267
268
|
|
|
268
269
|
|
|
270
|
+
# Elara suggested replacing base-case handling with her brute-force solver
|
|
271
|
+
@njit
|
|
272
|
+
def tsp_bruteforce(G_m, nodes, perms, is_cyclic):
|
|
273
|
+
"""
|
|
274
|
+
Brute-force TSP solver for small n.
|
|
275
|
+
G_m : numpy.ndarray (2D adjacency/weight matrix)
|
|
276
|
+
is_cyclic : bool (default=True) – whether to close the tour
|
|
277
|
+
|
|
278
|
+
Returns:
|
|
279
|
+
(best_path, best_weight)
|
|
280
|
+
"""
|
|
281
|
+
n = len(G_m)
|
|
282
|
+
best_weight = float('inf')
|
|
283
|
+
best_path = None
|
|
284
|
+
|
|
285
|
+
# Must fix node 0 at start to remove rotational symmetry in cyclic case!
|
|
286
|
+
|
|
287
|
+
max_i = len(perms[0]) - 1
|
|
288
|
+
|
|
289
|
+
if is_cyclic:
|
|
290
|
+
for perm in perms:
|
|
291
|
+
path = [0] + list(perm)
|
|
292
|
+
weight = 0.0
|
|
293
|
+
for i in range(max_i):
|
|
294
|
+
weight += G_m[path[i], path[i+1]]
|
|
295
|
+
weight += G_m[path[-1], path[0]]
|
|
296
|
+
|
|
297
|
+
if weight < best_weight:
|
|
298
|
+
best_weight = weight
|
|
299
|
+
best_path = path
|
|
300
|
+
|
|
301
|
+
best_path = best_path + [best_path[0]]
|
|
302
|
+
else:
|
|
303
|
+
for path in perms:
|
|
304
|
+
weight = 0.0
|
|
305
|
+
for i in range(max_i):
|
|
306
|
+
weight += G_m[path[i], path[i+1]]
|
|
307
|
+
|
|
308
|
+
if weight < best_weight:
|
|
309
|
+
best_weight = weight
|
|
310
|
+
best_path = path
|
|
311
|
+
|
|
312
|
+
best_path = list(best_path)
|
|
313
|
+
|
|
314
|
+
return best_path, best_weight
|
|
315
|
+
|
|
316
|
+
|
|
269
317
|
def tsp_symmetric(G, start_node=None, end_node=None, quality=1, shots=None, correction_quality=2, monte_carlo=False, k_neighbors=20, is_cyclic=True, multi_start=1, is_top_level=True):
|
|
270
318
|
nodes = None
|
|
271
319
|
n_nodes = 0
|
|
@@ -279,7 +327,15 @@ def tsp_symmetric(G, start_node=None, end_node=None, quality=1, shots=None, corr
|
|
|
279
327
|
nodes = list(range(n_nodes))
|
|
280
328
|
G_m = G
|
|
281
329
|
|
|
282
|
-
if
|
|
330
|
+
if is_cyclic:
|
|
331
|
+
start_node = None
|
|
332
|
+
end_node = None
|
|
333
|
+
|
|
334
|
+
if n_nodes < 5:
|
|
335
|
+
if n_nodes == 4:
|
|
336
|
+
perms = list(itertools.permutations(nodes[1:]) if is_cyclic else itertools.permutations(nodes))
|
|
337
|
+
return tsp_bruteforce(G_m, nodes, perms, is_cyclic)
|
|
338
|
+
|
|
283
339
|
if n_nodes == 3:
|
|
284
340
|
if is_cyclic:
|
|
285
341
|
weight_0 = G_m[0, 1] + G_m[1, 2] + G_m[2, 0]
|
|
@@ -12,6 +12,7 @@ pyqrackising/maxcut_tfim.py
|
|
|
12
12
|
pyqrackising/spin_glass_solver.py
|
|
13
13
|
pyqrackising/tfim_magnetization.py
|
|
14
14
|
pyqrackising/tfim_square_magnetization.py
|
|
15
|
+
pyqrackising/tsp_asymmetric.py
|
|
15
16
|
pyqrackising/tsp_symmetric.py
|
|
16
17
|
pyqrackising.egg-info/PKG-INFO
|
|
17
18
|
pyqrackising.egg-info/SOURCES.txt
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='pyqrackising',
|
|
54
|
-
version='3.0
|
|
54
|
+
version='3.1.0',
|
|
55
55
|
author='Dan Strano',
|
|
56
56
|
author_email='stranoj@gmail.com',
|
|
57
57
|
description='Near-ideal closed-form solutions for 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
|