PyQrackIsing 1.24.1__tar.gz → 1.25.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-1.24.1/PyQrackIsing.egg-info → pyqrackising-1.25.0}/PKG-INFO +1 -1
- {pyqrackising-1.24.1 → pyqrackising-1.25.0}/PyQrackIsing/tsp_symmetric.py +127 -43
- {pyqrackising-1.24.1 → pyqrackising-1.25.0/PyQrackIsing.egg-info}/PKG-INFO +1 -1
- {pyqrackising-1.24.1 → pyqrackising-1.25.0}/README.md +12 -1
- {pyqrackising-1.24.1 → pyqrackising-1.25.0}/pyproject.toml +1 -1
- {pyqrackising-1.24.1 → pyqrackising-1.25.0}/setup.py +1 -1
- {pyqrackising-1.24.1 → pyqrackising-1.25.0}/CMakeLists.txt +0 -0
- {pyqrackising-1.24.1 → pyqrackising-1.25.0}/LICENSE.md +0 -0
- {pyqrackising-1.24.1 → pyqrackising-1.25.0}/MANIFEST.in +0 -0
- {pyqrackising-1.24.1 → pyqrackising-1.25.0}/PyQrackIsing/__init__.py +0 -0
- {pyqrackising-1.24.1 → pyqrackising-1.25.0}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
- {pyqrackising-1.24.1 → pyqrackising-1.25.0}/PyQrackIsing/generate_tfim_samples.py +0 -0
- {pyqrackising-1.24.1 → pyqrackising-1.25.0}/PyQrackIsing/maxcut_tfim.py +0 -0
- {pyqrackising-1.24.1 → pyqrackising-1.25.0}/PyQrackIsing/spin_glass_solver.py +0 -0
- {pyqrackising-1.24.1 → pyqrackising-1.25.0}/PyQrackIsing/tfim_magnetization.py +0 -0
- {pyqrackising-1.24.1 → pyqrackising-1.25.0}/PyQrackIsing/tfim_square_magnetization.py +0 -0
- {pyqrackising-1.24.1 → pyqrackising-1.25.0}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
- {pyqrackising-1.24.1 → pyqrackising-1.25.0}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
- {pyqrackising-1.24.1 → pyqrackising-1.25.0}/PyQrackIsing.egg-info/not-zip-safe +0 -0
- {pyqrackising-1.24.1 → pyqrackising-1.25.0}/PyQrackIsing.egg-info/top_level.txt +0 -0
- {pyqrackising-1.24.1 → pyqrackising-1.25.0}/setup.cfg +0 -0
|
@@ -20,12 +20,58 @@ def path_length(path, G_m):
|
|
|
20
20
|
return tot_len
|
|
21
21
|
|
|
22
22
|
|
|
23
|
+
@njit
|
|
24
|
+
def one_way_two_opt(path, G):
|
|
25
|
+
improved = True
|
|
26
|
+
best_path = path
|
|
27
|
+
best_dist = path_length(best_path, G)
|
|
28
|
+
path_len = len(path)
|
|
29
|
+
|
|
30
|
+
while improved:
|
|
31
|
+
improved = False
|
|
32
|
+
for i in range(1, path_len - 1):
|
|
33
|
+
for j in range(i+1, path_len + 1):
|
|
34
|
+
if j - i == 1: # adjacent edges, skip
|
|
35
|
+
continue
|
|
36
|
+
new_path = best_path[:]
|
|
37
|
+
new_path[i:j] = best_path[j-1:i-1:-1]
|
|
38
|
+
new_dist = path_length(new_path, G)
|
|
39
|
+
if new_dist < best_dist:
|
|
40
|
+
best_path, best_dist = new_path, new_dist
|
|
41
|
+
improved = True
|
|
42
|
+
path = best_path
|
|
43
|
+
return best_path, best_dist
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@njit
|
|
47
|
+
def anchored_two_opt(path, G):
|
|
48
|
+
improved = True
|
|
49
|
+
best_path = path
|
|
50
|
+
best_dist = path_length(best_path, G)
|
|
51
|
+
path_len = len(path)
|
|
52
|
+
|
|
53
|
+
while improved:
|
|
54
|
+
improved = False
|
|
55
|
+
for i in range(1, path_len - 1):
|
|
56
|
+
for j in range(i+1, path_len):
|
|
57
|
+
if j - i == 1: # adjacent edges, skip
|
|
58
|
+
continue
|
|
59
|
+
new_path = best_path[:]
|
|
60
|
+
new_path[i:j] = best_path[j-1:i-1:-1]
|
|
61
|
+
new_dist = path_length(new_path, G)
|
|
62
|
+
if new_dist < best_dist:
|
|
63
|
+
best_path, best_dist = new_path, new_dist
|
|
64
|
+
improved = True
|
|
65
|
+
path = best_path
|
|
66
|
+
return best_path, best_dist
|
|
67
|
+
|
|
68
|
+
|
|
23
69
|
@njit
|
|
24
70
|
def two_opt(path, G):
|
|
25
71
|
improved = True
|
|
26
72
|
best_path = path
|
|
27
73
|
best_dist = path_length(best_path, G)
|
|
28
|
-
|
|
74
|
+
|
|
29
75
|
while improved:
|
|
30
76
|
improved = False
|
|
31
77
|
for i in range(1, len(path) - 2):
|
|
@@ -185,14 +231,14 @@ def stitch(G_m, path_a, path_b, sol_weight):
|
|
|
185
231
|
terminals_a = [path_a[0], path_a[-1]]
|
|
186
232
|
terminals_b = [path_b[0], path_b[-1]]
|
|
187
233
|
|
|
234
|
+
best_weight = G_m[terminals_a[1], terminals_b[0]]
|
|
235
|
+
best_path = path_a + path_b
|
|
236
|
+
weight = G_m[terminals_a[0], terminals_b[1]]
|
|
237
|
+
if weight < best_weight:
|
|
238
|
+
best_weight = weight
|
|
239
|
+
best_path = path_b + path_a
|
|
188
240
|
for _ in range(2):
|
|
189
241
|
for _ in range(2):
|
|
190
|
-
best_weight = G_m[terminals_a[1], terminals_b[0]]
|
|
191
|
-
best_path = path_a + path_b
|
|
192
|
-
weight = G_m[terminals_b[1], terminals_a[0]]
|
|
193
|
-
if weight < best_weight:
|
|
194
|
-
best_weight = weight
|
|
195
|
-
best_path = path_b + path_a
|
|
196
242
|
for i in range(1, len(path_b)):
|
|
197
243
|
weight = (
|
|
198
244
|
G_m[terminals_a[0], path_b[i - 1]] +
|
|
@@ -211,7 +257,7 @@ def stitch(G_m, path_a, path_b, sol_weight):
|
|
|
211
257
|
return best_path, best_weight
|
|
212
258
|
|
|
213
259
|
|
|
214
|
-
def tsp_symmetric(G, quality=1, shots=None, correction_quality=2, monte_carlo=False,
|
|
260
|
+
def tsp_symmetric(G, start_node=None, end_node=None, quality=1, shots=None, correction_quality=2, monte_carlo=False, is_3_opt=True, k_neighbors=20, is_cyclic=True, multi_start=1, is_top_level=True):
|
|
215
261
|
nodes = None
|
|
216
262
|
n_nodes = 0
|
|
217
263
|
G_m = None
|
|
@@ -234,60 +280,98 @@ def tsp_symmetric(G, quality=1, shots=None, correction_quality=2, monte_carlo=Fa
|
|
|
234
280
|
else:
|
|
235
281
|
return ([nodes[0], nodes[1]], G_m[0, 1])
|
|
236
282
|
|
|
283
|
+
if (start_node is None) and not (end_node is None):
|
|
284
|
+
start_node = end_node
|
|
285
|
+
end_node = None
|
|
286
|
+
|
|
237
287
|
a = []
|
|
238
288
|
b = []
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
289
|
+
c = []
|
|
290
|
+
if (start_node is None) and (end_node is None):
|
|
291
|
+
best_energy = float("inf")
|
|
292
|
+
for _ in range(multi_start):
|
|
293
|
+
energy = 0.0
|
|
294
|
+
_a = []
|
|
295
|
+
_b = []
|
|
296
|
+
while (len(_a) == 0) or (len(_b) == 0):
|
|
297
|
+
bits = ([], [])
|
|
298
|
+
if monte_carlo:
|
|
299
|
+
for i in range(n_nodes):
|
|
300
|
+
if np.random.random() < 0.5:
|
|
301
|
+
bits[0].append(i)
|
|
302
|
+
else:
|
|
303
|
+
bits[1].append(i)
|
|
304
|
+
else:
|
|
305
|
+
_, _, bits, energy = spin_glass_solver(G_m, quality=quality, shots=shots, correction_quality=correction_quality)
|
|
306
|
+
_a = list(bits[0])
|
|
307
|
+
_b = list(bits[1])
|
|
308
|
+
if energy < best_energy:
|
|
309
|
+
best_energy = energy
|
|
310
|
+
a, b = _a, _b
|
|
311
|
+
else:
|
|
312
|
+
is_cyclic = False
|
|
313
|
+
a.append(nodes.index(start_node))
|
|
314
|
+
b = list(range(n_nodes))
|
|
315
|
+
b.remove(a[0])
|
|
316
|
+
if end_node is not None:
|
|
317
|
+
c.append(nodes.index(end_node))
|
|
318
|
+
b.remove(c[0])
|
|
259
319
|
|
|
260
320
|
G_a, G_b = init_G_a_b(G_m, a, b)
|
|
261
321
|
|
|
262
|
-
sol_a = tsp_symmetric(G_a, quality=quality, correction_quality=correction_quality, monte_carlo=monte_carlo, is_cyclic=False,
|
|
263
|
-
sol_b = tsp_symmetric(G_b, quality=quality, correction_quality=correction_quality, monte_carlo=monte_carlo, is_cyclic=False,
|
|
322
|
+
sol_a = tsp_symmetric(G_a, quality=quality, correction_quality=correction_quality, monte_carlo=monte_carlo, is_cyclic=False, is_top_level=False, is_3_opt=False, multi_start=multi_start)
|
|
323
|
+
sol_b = tsp_symmetric(G_b, quality=quality, correction_quality=correction_quality, monte_carlo=monte_carlo, is_cyclic=False, is_top_level=False, is_3_opt=False, multi_start=multi_start)
|
|
264
324
|
|
|
265
325
|
path_a = [a[x] for x in sol_a[0]]
|
|
266
326
|
path_b = [b[x] for x in sol_b[0]]
|
|
267
327
|
|
|
268
328
|
sol_weight = sol_a[1] + sol_b[1]
|
|
269
329
|
|
|
330
|
+
if len(c):
|
|
331
|
+
sol_weight += G_m[b[-1], c[0]]
|
|
332
|
+
|
|
270
333
|
if (len(path_a) == 1) and (len(path_b) == 1):
|
|
271
|
-
|
|
334
|
+
if len(c):
|
|
335
|
+
return (path_a + path_b + c, (sol_weight + G_m[path_b[0], c[0]] + G_m[path_a[0], c[0]]) if is_cyclic else (sol_weight + G_m[path_b[0], c[0]]))
|
|
336
|
+
return (path_a + path_b, (sol_weight + G_m[path_a[0], path_b[0]]) if is_cyclic else sol_weight)
|
|
272
337
|
|
|
273
|
-
|
|
338
|
+
if start_node is None:
|
|
339
|
+
best_path, best_weight = stitch(G_m, path_a, path_b, sol_weight)
|
|
340
|
+
else:
|
|
341
|
+
best_path = path_a + path_b
|
|
342
|
+
best_weight = G_m[path_a[0], path_b[0]]
|
|
343
|
+
weight = G_m[path_a[0], path_b[-1]]
|
|
344
|
+
if weight < best_weight:
|
|
345
|
+
path_b.reverse()
|
|
346
|
+
best_path = path_a + path_b
|
|
347
|
+
best_weight = weight
|
|
348
|
+
best_weight += sol_weight
|
|
274
349
|
|
|
275
|
-
if
|
|
276
|
-
|
|
277
|
-
|
|
350
|
+
if len(c):
|
|
351
|
+
best_path += c
|
|
352
|
+
best_weight += G_m[best_path[-1], c[0]]
|
|
278
353
|
|
|
279
|
-
|
|
354
|
+
if is_top_level:
|
|
355
|
+
if is_cyclic:
|
|
356
|
+
best_path += [best_path[0]]
|
|
357
|
+
best_path, best_weight = two_opt(best_path, G_m)
|
|
358
|
+
elif not end_node is None:
|
|
280
359
|
best_path, best_weight = two_opt(best_path, G_m)
|
|
360
|
+
elif not start_node is None:
|
|
361
|
+
best_path.reverse()
|
|
362
|
+
best_path, best_weight = anchored_two_opt(best_path, G_m)
|
|
363
|
+
best_path.reverse()
|
|
364
|
+
else:
|
|
365
|
+
best_path, best_weight = one_way_two_opt(best_path, G_m)
|
|
281
366
|
|
|
282
367
|
if is_3_opt:
|
|
283
368
|
best_path, best_weight = targeted_three_opt(best_path, G_m, k_neighbors)
|
|
369
|
+
else:
|
|
370
|
+
best_path, best_weight = one_way_two_opt(best_path, G_m)
|
|
284
371
|
|
|
285
|
-
if
|
|
286
|
-
|
|
287
|
-
best_weight
|
|
288
|
-
best_path
|
|
289
|
-
elif is_cyclic:
|
|
290
|
-
cycle_node = best_path[0]
|
|
291
|
-
best_path += [cycle_node]
|
|
372
|
+
if is_cyclic:
|
|
373
|
+
cycle_node = best_path[0]
|
|
374
|
+
best_weight += G_m[cycle_node, best_path[-1]]
|
|
375
|
+
best_path += [cycle_node]
|
|
292
376
|
|
|
293
377
|
return [nodes[x] for x in best_path], best_weight
|
|
@@ -93,7 +93,18 @@ def generate_tsp_graph(n_nodes=64, seed=None):
|
|
|
93
93
|
|
|
94
94
|
n_nodes = 128
|
|
95
95
|
G = generate_tsp_graph(n_nodes=n_nodes, seed=42)
|
|
96
|
-
circuit, path_length = tsp_symmetric(
|
|
96
|
+
circuit, path_length = tsp_symmetric(
|
|
97
|
+
G,
|
|
98
|
+
start_node=None,
|
|
99
|
+
end_node=None,
|
|
100
|
+
is_monte_carlo=False,
|
|
101
|
+
quality=1,
|
|
102
|
+
correction_quality=2,
|
|
103
|
+
is_cyclic=True,
|
|
104
|
+
multi_start=1,
|
|
105
|
+
is_3_opt=True,
|
|
106
|
+
k_neighbors=20
|
|
107
|
+
)
|
|
97
108
|
|
|
98
109
|
print(f"Node count: {n_nodes}")
|
|
99
110
|
print(f"Path: {circuit}")
|
|
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
|
|
|
12
12
|
|
|
13
13
|
[project]
|
|
14
14
|
name = "PyQrackIsing"
|
|
15
|
-
version = "1.
|
|
15
|
+
version = "1.25.0"
|
|
16
16
|
requires-python = ">=3.8"
|
|
17
17
|
description = "Near-ideal closed-form solutions for transverse field Ising model (TFIM)"
|
|
18
18
|
readme = {file = "README.txt", content-type = "text/markdown"}
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='PyQrackIsing',
|
|
54
|
-
version='1.
|
|
54
|
+
version='1.25.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
|