PyQrackIsing 3.2.0__tar.gz → 3.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-3.2.0/pyqrackising.egg-info → pyqrackising-3.4.0}/PKG-INFO +1 -1
- {pyqrackising-3.2.0 → pyqrackising-3.4.0}/README.md +1 -1
- {pyqrackising-3.2.0 → pyqrackising-3.4.0}/pyproject.toml +1 -1
- {pyqrackising-3.2.0 → pyqrackising-3.4.0}/pyqrackising/__init__.py +1 -2
- pyqrackising-3.2.0/pyqrackising/tsp_symmetric.py → pyqrackising-3.4.0/pyqrackising/tsp.py +157 -57
- {pyqrackising-3.2.0 → pyqrackising-3.4.0/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-3.2.0 → pyqrackising-3.4.0}/pyqrackising.egg-info/SOURCES.txt +1 -2
- {pyqrackising-3.2.0 → pyqrackising-3.4.0}/setup.py +1 -1
- pyqrackising-3.2.0/pyqrackising/tsp_asymmetric.py +0 -284
- {pyqrackising-3.2.0 → pyqrackising-3.4.0}/CMakeLists.txt +0 -0
- {pyqrackising-3.2.0 → pyqrackising-3.4.0}/LICENSE.md +0 -0
- {pyqrackising-3.2.0 → pyqrackising-3.4.0}/MANIFEST.in +0 -0
- {pyqrackising-3.2.0 → pyqrackising-3.4.0}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-3.2.0 → pyqrackising-3.4.0}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-3.2.0 → pyqrackising-3.4.0}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-3.2.0 → pyqrackising-3.4.0}/pyqrackising/maxcut_tfim.py +0 -0
- {pyqrackising-3.2.0 → pyqrackising-3.4.0}/pyqrackising/spin_glass_solver.py +0 -0
- {pyqrackising-3.2.0 → pyqrackising-3.4.0}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-3.2.0 → pyqrackising-3.4.0}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-3.2.0 → pyqrackising-3.4.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-3.2.0 → pyqrackising-3.4.0}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-3.2.0 → pyqrackising-3.4.0}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-3.2.0 → pyqrackising-3.4.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.
|
|
14
|
+
version = "3.4.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"}
|
|
@@ -3,5 +3,4 @@ from .tfim_magnetization import tfim_magnetization
|
|
|
3
3
|
from .tfim_square_magnetization import tfim_square_magnetization
|
|
4
4
|
from .maxcut_tfim import maxcut_tfim
|
|
5
5
|
from .spin_glass_solver import spin_glass_solver
|
|
6
|
-
from .
|
|
7
|
-
from .tsp_asymmetric import tsp_asymmetric
|
|
6
|
+
from .tsp import tsp_symmetric, tsp_asymmetric
|
|
@@ -57,6 +57,27 @@ def anchored_two_opt(path, G):
|
|
|
57
57
|
return best_path, best_dist
|
|
58
58
|
|
|
59
59
|
|
|
60
|
+
@njit
|
|
61
|
+
def reversed_anchored_two_opt(path, G):
|
|
62
|
+
improved = True
|
|
63
|
+
best_path = path
|
|
64
|
+
best_dist = path_length(best_path, G)
|
|
65
|
+
path_len = len(path)
|
|
66
|
+
|
|
67
|
+
while improved:
|
|
68
|
+
improved = False
|
|
69
|
+
for i in range(1, path_len - 2):
|
|
70
|
+
for j in range(i + 2, path_len - 1):
|
|
71
|
+
new_path = best_path[:]
|
|
72
|
+
new_path[i:j] = best_path[j-1:i-1:-1]
|
|
73
|
+
new_dist = path_length(new_path, G)
|
|
74
|
+
if new_dist < best_dist:
|
|
75
|
+
best_path, best_dist = new_path, new_dist
|
|
76
|
+
improved = True
|
|
77
|
+
path = best_path
|
|
78
|
+
return best_path, best_dist
|
|
79
|
+
|
|
80
|
+
|
|
60
81
|
@njit
|
|
61
82
|
def two_opt(path, G):
|
|
62
83
|
improved = True
|
|
@@ -267,54 +288,7 @@ def monte_carlo_loop(n_nodes):
|
|
|
267
288
|
return bits
|
|
268
289
|
|
|
269
290
|
|
|
270
|
-
|
|
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
|
-
|
|
317
|
-
def tsp_symmetric(G, start_node=None, end_node=None, quality=1, shots=None, correction_quality=2, monte_carlo=False, k_neighbors=16, is_cyclic=True, multi_start=1, is_top_level=True):
|
|
291
|
+
def tsp_symmetric(G, start_node=None, end_node=None, quality=2, shots=None, correction_quality=2, monte_carlo=True, k_neighbors=16, is_cyclic=True, multi_start=1, is_top_level=True):
|
|
318
292
|
nodes = None
|
|
319
293
|
n_nodes = 0
|
|
320
294
|
G_m = None
|
|
@@ -331,11 +305,7 @@ def tsp_symmetric(G, start_node=None, end_node=None, quality=1, shots=None, corr
|
|
|
331
305
|
start_node = None
|
|
332
306
|
end_node = None
|
|
333
307
|
|
|
334
|
-
if n_nodes <
|
|
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
|
-
|
|
308
|
+
if n_nodes < 4:
|
|
339
309
|
if n_nodes == 3:
|
|
340
310
|
if is_cyclic:
|
|
341
311
|
weight_0 = G_m[0, 1] + G_m[1, 2] + G_m[2, 0]
|
|
@@ -410,10 +380,119 @@ def tsp_symmetric(G, start_node=None, end_node=None, quality=1, shots=None, corr
|
|
|
410
380
|
if len(c):
|
|
411
381
|
sol_weight += G_m[b[-1], c[0]]
|
|
412
382
|
|
|
413
|
-
if
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
383
|
+
if start_node is None:
|
|
384
|
+
best_path, best_weight = stitch(G_m, path_a, path_b, sol_weight)
|
|
385
|
+
else:
|
|
386
|
+
best_path = path_a + path_b
|
|
387
|
+
best_weight = G_m[path_a[0], path_b[0]]
|
|
388
|
+
weight = G_m[path_a[0], path_b[-1]]
|
|
389
|
+
if weight < best_weight:
|
|
390
|
+
path_b.reverse()
|
|
391
|
+
best_path = path_a + path_b
|
|
392
|
+
best_weight = weight
|
|
393
|
+
best_weight += sol_weight
|
|
394
|
+
|
|
395
|
+
if len(c):
|
|
396
|
+
best_path += c
|
|
397
|
+
best_weight += G_m[best_path[-1], c[0]]
|
|
398
|
+
|
|
399
|
+
if is_top_level:
|
|
400
|
+
if is_cyclic:
|
|
401
|
+
best_path += [best_path[0]]
|
|
402
|
+
best_path, best_weight = two_opt(best_path, G_m)
|
|
403
|
+
elif not end_node is None:
|
|
404
|
+
best_path, best_weight = two_opt(best_path, G_m)
|
|
405
|
+
elif not start_node is None:
|
|
406
|
+
best_path, best_weight = anchored_two_opt(best_path, G_m)
|
|
407
|
+
else:
|
|
408
|
+
best_path, best_weight = one_way_two_opt(best_path, G_m)
|
|
409
|
+
|
|
410
|
+
if k_neighbors > 0:
|
|
411
|
+
best_path, best_weight = targeted_three_opt(best_path, G_m, k_neighbors)
|
|
412
|
+
else:
|
|
413
|
+
best_path, best_weight = one_way_two_opt(best_path, G_m)
|
|
414
|
+
|
|
415
|
+
if is_cyclic:
|
|
416
|
+
cycle_node = best_path[0]
|
|
417
|
+
best_weight += G_m[cycle_node, best_path[-1]]
|
|
418
|
+
best_path += [cycle_node]
|
|
419
|
+
|
|
420
|
+
return [nodes[x] for x in best_path], best_weight
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
def tsp_asymmetric(G, start_node=None, end_node=None, quality=1, shots=None, correction_quality=2, monte_carlo=True, k_neighbors=16, is_cyclic=True, multi_start=1, is_top_level=True):
|
|
424
|
+
nodes = None
|
|
425
|
+
n_nodes = 0
|
|
426
|
+
G_m = None
|
|
427
|
+
if isinstance(G, nx.Graph):
|
|
428
|
+
nodes = list(G.nodes())
|
|
429
|
+
n_nodes = len(nodes)
|
|
430
|
+
G_m = nx.to_numpy_array(G, weight='weight', nonedge=0.0)
|
|
431
|
+
else:
|
|
432
|
+
n_nodes = len(G)
|
|
433
|
+
nodes = list(range(n_nodes))
|
|
434
|
+
G_m = G
|
|
435
|
+
|
|
436
|
+
if is_cyclic:
|
|
437
|
+
start_node = None
|
|
438
|
+
end_node = None
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
if n_nodes < 3:
|
|
442
|
+
if n_nodes == 2:
|
|
443
|
+
weight = G_m[0, 1]
|
|
444
|
+
if G_m[1, 0] < weight:
|
|
445
|
+
weight = G_m[1, 0]
|
|
446
|
+
nodes.reverse()
|
|
447
|
+
|
|
448
|
+
if is_cyclic:
|
|
449
|
+
return (nodes + [nodes[0]], 2 * weight)
|
|
450
|
+
|
|
451
|
+
return (nodes, weight)
|
|
452
|
+
|
|
453
|
+
return (nodes, 0)
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
if (start_node is None) and not (end_node is None):
|
|
457
|
+
start_node = end_node
|
|
458
|
+
end_node = None
|
|
459
|
+
|
|
460
|
+
a = []
|
|
461
|
+
b = []
|
|
462
|
+
c = []
|
|
463
|
+
if (start_node is None) and (end_node is None):
|
|
464
|
+
if monte_carlo:
|
|
465
|
+
a, b = monte_carlo_loop(n_nodes)
|
|
466
|
+
else:
|
|
467
|
+
best_energy = float("inf")
|
|
468
|
+
for _ in range(multi_start):
|
|
469
|
+
bits = ([], [])
|
|
470
|
+
while (len(bits[0]) == 0) or (len(bits[1]) == 0):
|
|
471
|
+
_, _, bits, energy = spin_glass_solver((G_m + G_m.T) / 2, quality=quality, shots=shots, correction_quality=correction_quality)
|
|
472
|
+
if energy < best_energy:
|
|
473
|
+
best_energy = energy
|
|
474
|
+
a, b = bits
|
|
475
|
+
else:
|
|
476
|
+
is_cyclic = False
|
|
477
|
+
a.append(nodes.index(start_node))
|
|
478
|
+
b = list(range(n_nodes))
|
|
479
|
+
b.remove(a[0])
|
|
480
|
+
if end_node is not None:
|
|
481
|
+
c.append(nodes.index(end_node))
|
|
482
|
+
b.remove(c[0])
|
|
483
|
+
|
|
484
|
+
G_a, G_b = init_G_a_b(G_m, a, b)
|
|
485
|
+
|
|
486
|
+
sol_a = tsp_asymmetric(G_a, quality=quality, correction_quality=correction_quality, monte_carlo=monte_carlo, is_cyclic=False, is_top_level=False, k_neighbors=0, multi_start=multi_start)
|
|
487
|
+
sol_b = tsp_asymmetric(G_b, quality=quality, correction_quality=correction_quality, monte_carlo=monte_carlo, is_cyclic=False, is_top_level=False, k_neighbors=0, multi_start=multi_start)
|
|
488
|
+
|
|
489
|
+
path_a = [a[x] for x in sol_a[0]]
|
|
490
|
+
path_b = [b[x] for x in sol_b[0]]
|
|
491
|
+
|
|
492
|
+
sol_weight = sol_a[1] + sol_b[1]
|
|
493
|
+
|
|
494
|
+
if len(c):
|
|
495
|
+
sol_weight += G_m[b[-1], c[0]]
|
|
417
496
|
|
|
418
497
|
if start_node is None:
|
|
419
498
|
best_path, best_weight = stitch(G_m, path_a, path_b, sol_weight)
|
|
@@ -435,17 +514,38 @@ def tsp_symmetric(G, start_node=None, end_node=None, quality=1, shots=None, corr
|
|
|
435
514
|
if is_cyclic:
|
|
436
515
|
best_path += [best_path[0]]
|
|
437
516
|
best_path, best_weight = two_opt(best_path, G_m)
|
|
517
|
+
path = list(reversed(best_path))
|
|
518
|
+
path, weight = two_opt(path, G_m)
|
|
519
|
+
if weight < best_weight:
|
|
520
|
+
best_path, best_weight = path, weight
|
|
438
521
|
elif not end_node is None:
|
|
439
522
|
best_path, best_weight = two_opt(best_path, G_m)
|
|
523
|
+
path = list(reversed(best_path))
|
|
524
|
+
path, weight = two_opt(path, G_m)
|
|
525
|
+
if weight < best_weight:
|
|
526
|
+
best_path, best_weight = path, weight
|
|
440
527
|
elif not start_node is None:
|
|
441
528
|
best_path, best_weight = anchored_two_opt(best_path, G_m)
|
|
529
|
+
path = list(reversed(best_path))
|
|
530
|
+
path, weight = reversed_anchored_two_opt(path, G_m)
|
|
531
|
+
if weight < best_weight:
|
|
532
|
+
best_path, best_weight = path, weight
|
|
442
533
|
else:
|
|
443
534
|
best_path, best_weight = one_way_two_opt(best_path, G_m)
|
|
535
|
+
path = list(reversed(best_path))
|
|
536
|
+
path, weight = one_way_two_opt(path, G_m)
|
|
537
|
+
if weight < best_weight:
|
|
538
|
+
best_path, best_weight = path, weight
|
|
444
539
|
|
|
445
540
|
if k_neighbors > 0:
|
|
446
541
|
best_path, best_weight = targeted_three_opt(best_path, G_m, k_neighbors)
|
|
447
542
|
else:
|
|
448
543
|
best_path, best_weight = one_way_two_opt(best_path, G_m)
|
|
544
|
+
if not start_node and not end_node:
|
|
545
|
+
path = list(reversed(best_path))
|
|
546
|
+
path, weight = one_way_two_opt(path, G_m)
|
|
547
|
+
if weight < best_weight:
|
|
548
|
+
best_path, best_weight = path, weight
|
|
449
549
|
|
|
450
550
|
if is_cyclic:
|
|
451
551
|
cycle_node = best_path[0]
|
|
@@ -12,8 +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/
|
|
16
|
-
pyqrackising/tsp_symmetric.py
|
|
15
|
+
pyqrackising/tsp.py
|
|
17
16
|
pyqrackising.egg-info/PKG-INFO
|
|
18
17
|
pyqrackising.egg-info/SOURCES.txt
|
|
19
18
|
pyqrackising.egg-info/dependency_links.txt
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='pyqrackising',
|
|
54
|
-
version='3.
|
|
54
|
+
version='3.4.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)',
|
|
@@ -1,284 +0,0 @@
|
|
|
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=32, 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)
|
|
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
|