PyQrackIsing 3.5.5__tar.gz → 4.0.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-3.5.5/pyqrackising.egg-info → pyqrackising-4.0.1}/PKG-INFO +1 -1
- {pyqrackising-3.5.5 → pyqrackising-4.0.1}/README.md +2 -3
- {pyqrackising-3.5.5 → pyqrackising-4.0.1}/pyproject.toml +1 -1
- {pyqrackising-3.5.5 → pyqrackising-4.0.1}/pyqrackising/spin_glass_solver.py +8 -7
- {pyqrackising-3.5.5 → pyqrackising-4.0.1}/pyqrackising/spin_glass_solver_sparse.py +8 -5
- {pyqrackising-3.5.5 → pyqrackising-4.0.1}/pyqrackising/tsp.py +127 -104
- {pyqrackising-3.5.5 → pyqrackising-4.0.1/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-3.5.5 → pyqrackising-4.0.1}/setup.py +1 -1
- {pyqrackising-3.5.5 → pyqrackising-4.0.1}/CMakeLists.txt +0 -0
- {pyqrackising-3.5.5 → pyqrackising-4.0.1}/LICENSE.md +0 -0
- {pyqrackising-3.5.5 → pyqrackising-4.0.1}/MANIFEST.in +0 -0
- {pyqrackising-3.5.5 → pyqrackising-4.0.1}/pyqrackising/__init__.py +0 -0
- {pyqrackising-3.5.5 → pyqrackising-4.0.1}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-3.5.5 → pyqrackising-4.0.1}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-3.5.5 → pyqrackising-4.0.1}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-3.5.5 → pyqrackising-4.0.1}/pyqrackising/maxcut_tfim.py +0 -0
- {pyqrackising-3.5.5 → pyqrackising-4.0.1}/pyqrackising/maxcut_tfim_sparse.py +0 -0
- {pyqrackising-3.5.5 → pyqrackising-4.0.1}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-3.5.5 → pyqrackising-4.0.1}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-3.5.5 → pyqrackising-4.0.1}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-3.5.5 → pyqrackising-4.0.1}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-3.5.5 → pyqrackising-4.0.1}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-3.5.5 → pyqrackising-4.0.1}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-3.5.5 → pyqrackising-4.0.1}/setup.cfg +0 -0
|
@@ -73,10 +73,10 @@ def generate_spin_glass_graph(n_nodes=16, degree=3, seed=None):
|
|
|
73
73
|
|
|
74
74
|
|
|
75
75
|
G = generate_spin_glass_graph(n_nodes=64, seed=42)
|
|
76
|
-
solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=3,
|
|
76
|
+
solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=3, best_guess=None)
|
|
77
77
|
# solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, best_guess=maxcut_tfim(G, quality=8)[0])
|
|
78
78
|
```
|
|
79
|
-
We also provide `spin_glass_solver_sparse(G)`, for `scipy` CSR sparse arrays (or `networkx` graphs). The (integer) `quality` setting is the same as `maxcut_tfim`. `
|
|
79
|
+
We also provide `spin_glass_solver_sparse(G)`, for `scipy` CSR sparse arrays (or `networkx` graphs). The (integer) `quality` setting is the same as `maxcut_tfim`. `best_guess` gives the option to seed the algorithm with a best guess as to the maximal cut (as an integer, binary string, or list of booleans). By default, `spin_glass_solver()` uses `maxcut_tfim(G)` with passed-through `quality` as `best_guess`, which typically works well, but it could be seeded with higher `maxcut_tfim()` `quality` or Goemans-Williamson, for example. This function is designed with a sign convention for weights such that it can immediately be used as a MAXCUT solver itself: you might need to reverse the sign convention on your weights for spin glass graphs, but this is only convention.
|
|
80
80
|
|
|
81
81
|
From the `spin_glass_solver()`, we provide a (recursive) Traveling Salesman Problem (TSP) solver:
|
|
82
82
|
```py
|
|
@@ -103,7 +103,6 @@ circuit, path_length = tsp_symmetric(
|
|
|
103
103
|
end_node=None,
|
|
104
104
|
monte_carlo=True,
|
|
105
105
|
quality=2,
|
|
106
|
-
correction_quality=2,
|
|
107
106
|
is_cyclic=True,
|
|
108
107
|
multi_start=1,
|
|
109
108
|
k_neighbors=20
|
|
@@ -11,7 +11,7 @@ build-backend = "setuptools.build_meta"
|
|
|
11
11
|
|
|
12
12
|
[project]
|
|
13
13
|
name = "pyqrackising"
|
|
14
|
-
version = "
|
|
14
|
+
version = "4.0.1"
|
|
15
15
|
requires-python = ">=3.8"
|
|
16
16
|
description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
|
|
17
17
|
readme = {file = "README.txt", content-type = "text/markdown"}
|
|
@@ -56,7 +56,7 @@ def int_to_bitstring(integer, length):
|
|
|
56
56
|
return (bin(integer)[2:].zfill(length))[::-1]
|
|
57
57
|
|
|
58
58
|
|
|
59
|
-
def spin_glass_solver(G, quality=None, shots=None,
|
|
59
|
+
def spin_glass_solver(G, quality=None, shots=None, best_guess=None):
|
|
60
60
|
nodes = None
|
|
61
61
|
n_qubits = 0
|
|
62
62
|
G_m = None
|
|
@@ -82,11 +82,6 @@ def spin_glass_solver(G, quality=None, shots=None, correction_quality=None, best
|
|
|
82
82
|
|
|
83
83
|
return "01", weight, ([nodes[0]], [nodes[1]]), -weight
|
|
84
84
|
|
|
85
|
-
if correction_quality is None:
|
|
86
|
-
# maxcut_tfim(G) scales roughly like n^4,
|
|
87
|
-
# so its match order of overhead.
|
|
88
|
-
correction_quality = 2
|
|
89
|
-
|
|
90
85
|
bitstring = ""
|
|
91
86
|
if isinstance(best_guess, str):
|
|
92
87
|
bitstring = best_guess
|
|
@@ -100,9 +95,11 @@ def spin_glass_solver(G, quality=None, shots=None, correction_quality=None, best
|
|
|
100
95
|
|
|
101
96
|
min_energy = compute_energy(best_theta, G_m)
|
|
102
97
|
improved = True
|
|
98
|
+
correction_quality = 1
|
|
103
99
|
while improved:
|
|
104
100
|
improved = False
|
|
105
|
-
|
|
101
|
+
k = 1
|
|
102
|
+
while k <= correction_quality:
|
|
106
103
|
if n_qubits < k:
|
|
107
104
|
break
|
|
108
105
|
|
|
@@ -121,8 +118,12 @@ def spin_glass_solver(G, quality=None, shots=None, correction_quality=None, best
|
|
|
121
118
|
for i in indices:
|
|
122
119
|
best_theta[i] = not best_theta[i]
|
|
123
120
|
improved = True
|
|
121
|
+
if correction_quality < (k + 1):
|
|
122
|
+
correction_quality = k + 1
|
|
124
123
|
break
|
|
125
124
|
|
|
125
|
+
k = k + 1
|
|
126
|
+
|
|
126
127
|
bitstring = ""
|
|
127
128
|
l, r = [], []
|
|
128
129
|
for i in range(len(best_theta)):
|
|
@@ -61,7 +61,7 @@ def int_to_bitstring(integer, length):
|
|
|
61
61
|
return (bin(integer)[2:].zfill(length))[::-1]
|
|
62
62
|
|
|
63
63
|
|
|
64
|
-
def spin_glass_solver_sparse(G, quality=None, shots=None,
|
|
64
|
+
def spin_glass_solver_sparse(G, quality=None, shots=None, best_guess=None):
|
|
65
65
|
nodes = None
|
|
66
66
|
n_qubits = 0
|
|
67
67
|
G_m = None
|
|
@@ -87,9 +87,6 @@ def spin_glass_solver_sparse(G, quality=None, shots=None, correction_quality=Non
|
|
|
87
87
|
|
|
88
88
|
return "01", weight, ([nodes[0]], [nodes[1]]), -weight
|
|
89
89
|
|
|
90
|
-
if correction_quality is None:
|
|
91
|
-
correction_quality = 2
|
|
92
|
-
|
|
93
90
|
bitstring = ""
|
|
94
91
|
if isinstance(best_guess, str):
|
|
95
92
|
bitstring = best_guess
|
|
@@ -105,9 +102,11 @@ def spin_glass_solver_sparse(G, quality=None, shots=None, correction_quality=Non
|
|
|
105
102
|
|
|
106
103
|
min_energy = compute_energy(best_theta, G_m.data, G_m.indptr, G_m.indices)
|
|
107
104
|
improved = True
|
|
105
|
+
correction_quality = 1
|
|
108
106
|
while improved:
|
|
109
107
|
improved = False
|
|
110
|
-
|
|
108
|
+
k = 1
|
|
109
|
+
while k <= correction_quality:
|
|
111
110
|
if n_qubits < k:
|
|
112
111
|
break
|
|
113
112
|
|
|
@@ -126,8 +125,12 @@ def spin_glass_solver_sparse(G, quality=None, shots=None, correction_quality=Non
|
|
|
126
125
|
for i in indices:
|
|
127
126
|
best_theta[i] = not best_theta[i]
|
|
128
127
|
improved = True
|
|
128
|
+
if correction_quality < (k + 1):
|
|
129
|
+
correction_quality = k + 1
|
|
129
130
|
break
|
|
130
131
|
|
|
132
|
+
k = k + 1
|
|
133
|
+
|
|
131
134
|
bitstring = ""
|
|
132
135
|
l, r = [], []
|
|
133
136
|
for i in range(len(best_theta)):
|
|
@@ -270,6 +270,7 @@ def stitch_symmetric(G_m, path_a, path_b):
|
|
|
270
270
|
return best_path
|
|
271
271
|
|
|
272
272
|
|
|
273
|
+
@njit
|
|
273
274
|
def stitch_asymmetric(G_m, path_a, path_b):
|
|
274
275
|
if len(path_a) == 1:
|
|
275
276
|
return stich_singlet(G_m, path_a[0], path_b)
|
|
@@ -311,6 +312,7 @@ def stitch_asymmetric(G_m, path_a, path_b):
|
|
|
311
312
|
return best_path
|
|
312
313
|
|
|
313
314
|
|
|
315
|
+
@njit
|
|
314
316
|
def restitch(G_m, path, is_sym):
|
|
315
317
|
l = len(path)
|
|
316
318
|
mid = ((l + 1) if (l & 1) and (np.random.random() < 0.5) else l) >> 1
|
|
@@ -406,98 +408,43 @@ def tsp_bruteforce_acyclic(G_m, perms):
|
|
|
406
408
|
return best_path, best_weight
|
|
407
409
|
|
|
408
410
|
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
n_nodes
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
n_nodes = len(nodes)
|
|
416
|
-
G_m = nx.to_numpy_array(G, weight='weight', nonedge=0.0)
|
|
417
|
-
else:
|
|
418
|
-
n_nodes = len(G)
|
|
419
|
-
nodes = list(range(n_nodes))
|
|
420
|
-
G_m = G
|
|
421
|
-
|
|
422
|
-
if is_cyclic:
|
|
423
|
-
start_node = None
|
|
424
|
-
end_node = None
|
|
425
|
-
|
|
426
|
-
if n_nodes < 7:
|
|
427
|
-
if n_nodes > 3:
|
|
428
|
-
if is_cyclic:
|
|
429
|
-
best_path, best_weight = tsp_bruteforce_cyclic(G_m, list(itertools.permutations(list(range(1, n_nodes)))))
|
|
430
|
-
else:
|
|
431
|
-
best_path, best_weight = tsp_bruteforce_acyclic(G_m, list(itertools.permutations(list(range(n_nodes)))))
|
|
432
|
-
|
|
433
|
-
return [nodes[x] for x in best_path], best_weight
|
|
434
|
-
|
|
435
|
-
if n_nodes == 3:
|
|
436
|
-
if is_cyclic:
|
|
437
|
-
weight_0 = G_m[0, 1] + G_m[1, 2] + G_m[2, 0]
|
|
438
|
-
weight_1 = G_m[0, 2] + G_m[2, 1] + G_m[1, 0]
|
|
439
|
-
|
|
440
|
-
if weight_0 >= weight_1:
|
|
441
|
-
return (nodes + [nodes[0]], weight_0)
|
|
442
|
-
|
|
443
|
-
nodes.reverse()
|
|
444
|
-
|
|
445
|
-
return ([nodes[2]] + nodes, weight_1)
|
|
446
|
-
|
|
447
|
-
w_012 = G_m[0, 1] + G_m[1, 2]
|
|
448
|
-
w_021 = G_m[0, 1] + G_m[0, 2]
|
|
449
|
-
w_120 = G_m[0, 2] + G_m[1, 2]
|
|
411
|
+
@njit
|
|
412
|
+
def tsp_symmetric_brute_force_driver(G_m, n_nodes, nodes, is_cyclic):
|
|
413
|
+
if n_nodes == 3:
|
|
414
|
+
if is_cyclic:
|
|
415
|
+
weight_0 = G_m[0, 1] + G_m[1, 2] + G_m[2, 0]
|
|
416
|
+
weight_1 = G_m[0, 2] + G_m[2, 1] + G_m[1, 0]
|
|
450
417
|
|
|
451
|
-
if
|
|
452
|
-
return (nodes,
|
|
418
|
+
if weight_0 >= weight_1:
|
|
419
|
+
return (nodes + [nodes[0]], weight_0)
|
|
453
420
|
|
|
454
|
-
|
|
455
|
-
return ([nodes[1], nodes[0], nodes[2]], w_021)
|
|
421
|
+
nodes.reverse()
|
|
456
422
|
|
|
457
|
-
return ([nodes[
|
|
423
|
+
return ([nodes[2]] + nodes, weight_1)
|
|
458
424
|
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
425
|
+
w_012 = G_m[0, 1] + G_m[1, 2]
|
|
426
|
+
w_021 = G_m[0, 1] + G_m[0, 2]
|
|
427
|
+
w_120 = G_m[0, 2] + G_m[1, 2]
|
|
462
428
|
|
|
463
|
-
|
|
429
|
+
if w_012 >= w_021 and w_012 >= w_120:
|
|
430
|
+
return (nodes, w_012)
|
|
464
431
|
|
|
465
|
-
|
|
432
|
+
if w_021 >= w_012 and w_021 >= w_120:
|
|
433
|
+
return ([nodes[1], nodes[0], nodes[2]], w_021)
|
|
466
434
|
|
|
435
|
+
return ([nodes[0], nodes[2], nodes[1]], w_120)
|
|
467
436
|
|
|
468
|
-
if
|
|
469
|
-
|
|
470
|
-
|
|
437
|
+
if n_nodes == 2:
|
|
438
|
+
if is_cyclic:
|
|
439
|
+
return (nodes + [nodes[0]], 2 * G_m[0, 1])
|
|
471
440
|
|
|
472
|
-
|
|
473
|
-
b = []
|
|
474
|
-
c = []
|
|
475
|
-
if (start_node is None) and (end_node is None):
|
|
476
|
-
if monte_carlo:
|
|
477
|
-
a, b = monte_carlo_loop(n_nodes)
|
|
478
|
-
else:
|
|
479
|
-
best_energy = float("inf")
|
|
480
|
-
for _ in range(multi_start):
|
|
481
|
-
bits = ([], [])
|
|
482
|
-
while (len(bits[0]) == 0) or (len(bits[1]) == 0):
|
|
483
|
-
_, _, bits, energy = spin_glass_solver(G_m, quality=quality, shots=shots, correction_quality=correction_quality)
|
|
484
|
-
if energy < best_energy:
|
|
485
|
-
best_energy = energy
|
|
486
|
-
a, b = bits
|
|
487
|
-
else:
|
|
488
|
-
is_cyclic = False
|
|
489
|
-
a.append(nodes.index(start_node))
|
|
490
|
-
b = list(range(n_nodes))
|
|
491
|
-
b.remove(a[0])
|
|
492
|
-
if end_node is not None:
|
|
493
|
-
c.append(nodes.index(end_node))
|
|
494
|
-
b.remove(c[0])
|
|
441
|
+
return (nodes, G_m[0, 1])
|
|
495
442
|
|
|
496
|
-
|
|
443
|
+
return (nodes, 0)
|
|
497
444
|
|
|
498
|
-
sol_a = tsp_symmetric(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)
|
|
499
|
-
sol_b = tsp_symmetric(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)
|
|
500
445
|
|
|
446
|
+
@njit
|
|
447
|
+
def tsp_symmetric_driver(G_m, is_cyclic, is_top_level, start_node, end_node, k_neighbors, nodes, sol_a, sol_b, a, b, c):
|
|
501
448
|
path_a = [a[x] for x in sol_a[0]]
|
|
502
449
|
path_b = [b[x] for x in sol_b[0]]
|
|
503
450
|
|
|
@@ -515,7 +462,8 @@ def tsp_symmetric(G, start_node=None, end_node=None, quality=2, shots=None, corr
|
|
|
515
462
|
best_path = path_a + path_b
|
|
516
463
|
best_weight = weight
|
|
517
464
|
|
|
518
|
-
|
|
465
|
+
if not c is None:
|
|
466
|
+
best_path.append(c)
|
|
519
467
|
|
|
520
468
|
if is_top_level:
|
|
521
469
|
if is_cyclic:
|
|
@@ -541,7 +489,7 @@ def tsp_symmetric(G, start_node=None, end_node=None, quality=2, shots=None, corr
|
|
|
541
489
|
return [nodes[x] for x in best_path], best_weight
|
|
542
490
|
|
|
543
491
|
|
|
544
|
-
def
|
|
492
|
+
def tsp_symmetric(G, start_node=None, end_node=None, quality=2, shots=None, monte_carlo=True, k_neighbors=20, is_cyclic=True, multi_start=1, is_top_level=True):
|
|
545
493
|
nodes = None
|
|
546
494
|
n_nodes = 0
|
|
547
495
|
G_m = None
|
|
@@ -559,7 +507,7 @@ def tsp_asymmetric(G, start_node=None, end_node=None, quality=2, shots=None, cor
|
|
|
559
507
|
end_node = None
|
|
560
508
|
|
|
561
509
|
if n_nodes < 7:
|
|
562
|
-
if n_nodes >
|
|
510
|
+
if n_nodes > 3:
|
|
563
511
|
if is_cyclic:
|
|
564
512
|
best_path, best_weight = tsp_bruteforce_cyclic(G_m, list(itertools.permutations(list(range(1, n_nodes)))))
|
|
565
513
|
else:
|
|
@@ -567,26 +515,11 @@ def tsp_asymmetric(G, start_node=None, end_node=None, quality=2, shots=None, cor
|
|
|
567
515
|
|
|
568
516
|
return [nodes[x] for x in best_path], best_weight
|
|
569
517
|
|
|
570
|
-
|
|
571
|
-
weight = G_m[0, 1]
|
|
572
|
-
if G_m[1, 0] < weight:
|
|
573
|
-
weight = G_m[1, 0]
|
|
574
|
-
nodes.reverse()
|
|
575
|
-
|
|
576
|
-
if is_cyclic:
|
|
577
|
-
return (nodes + [nodes[0]], 2 * weight)
|
|
578
|
-
|
|
579
|
-
return (nodes, weight)
|
|
580
|
-
|
|
581
|
-
return (nodes, 0)
|
|
518
|
+
return tsp_symmetric_brute_force_driver(G_m, n_nodes, nodes, is_cyclic)
|
|
582
519
|
|
|
583
|
-
|
|
584
|
-
is_reversed = False
|
|
585
520
|
if (start_node is None) and not (end_node is None):
|
|
586
|
-
is_reversed = True
|
|
587
521
|
start_node = end_node
|
|
588
522
|
end_node = None
|
|
589
|
-
G_m = G_m.T
|
|
590
523
|
|
|
591
524
|
a = []
|
|
592
525
|
b = []
|
|
@@ -599,7 +532,7 @@ def tsp_asymmetric(G, start_node=None, end_node=None, quality=2, shots=None, cor
|
|
|
599
532
|
for _ in range(multi_start):
|
|
600
533
|
bits = ([], [])
|
|
601
534
|
while (len(bits[0]) == 0) or (len(bits[1]) == 0):
|
|
602
|
-
_, _, bits, energy = spin_glass_solver(
|
|
535
|
+
_, _, bits, energy = spin_glass_solver(G_m, quality=quality, shots=shots)
|
|
603
536
|
if energy < best_energy:
|
|
604
537
|
best_energy = energy
|
|
605
538
|
a, b = bits
|
|
@@ -614,9 +547,30 @@ def tsp_asymmetric(G, start_node=None, end_node=None, quality=2, shots=None, cor
|
|
|
614
547
|
|
|
615
548
|
G_a, G_b = init_G_a_b(G_m, a, b)
|
|
616
549
|
|
|
617
|
-
sol_a =
|
|
618
|
-
sol_b =
|
|
550
|
+
sol_a = tsp_symmetric(G_a, quality=quality, monte_carlo=monte_carlo, is_cyclic=False, is_top_level=False, k_neighbors=0, multi_start=multi_start)
|
|
551
|
+
sol_b = tsp_symmetric(G_b, quality=quality, monte_carlo=monte_carlo, is_cyclic=False, is_top_level=False, k_neighbors=0, multi_start=multi_start)
|
|
619
552
|
|
|
553
|
+
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)
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
@njit
|
|
557
|
+
def tsp_asymmetric_brute_force_driver(G_m, n_nodes, nodes, is_cyclic):
|
|
558
|
+
if n_nodes == 2:
|
|
559
|
+
weight = G_m[0, 1]
|
|
560
|
+
if G_m[1, 0] < weight:
|
|
561
|
+
weight = G_m[1, 0]
|
|
562
|
+
nodes.reverse()
|
|
563
|
+
|
|
564
|
+
if is_cyclic:
|
|
565
|
+
return (nodes + [nodes[0]], 2 * weight)
|
|
566
|
+
|
|
567
|
+
return (nodes, weight)
|
|
568
|
+
|
|
569
|
+
return (nodes, 0)
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
@njit
|
|
573
|
+
def 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):
|
|
620
574
|
path_a = [a[x] for x in sol_a[0]]
|
|
621
575
|
path_b = [b[x] for x in sol_b[0]]
|
|
622
576
|
|
|
@@ -628,7 +582,8 @@ def tsp_asymmetric(G, start_node=None, end_node=None, quality=2, shots=None, cor
|
|
|
628
582
|
else:
|
|
629
583
|
best_path = path_a + path_b
|
|
630
584
|
|
|
631
|
-
|
|
585
|
+
if not c is None:
|
|
586
|
+
best_path.append(c)
|
|
632
587
|
|
|
633
588
|
if is_top_level:
|
|
634
589
|
if is_cyclic:
|
|
@@ -672,9 +627,77 @@ def tsp_asymmetric(G, start_node=None, end_node=None, quality=2, shots=None, cor
|
|
|
672
627
|
else:
|
|
673
628
|
final_path = best_path
|
|
674
629
|
|
|
675
|
-
best_weight = path_length(
|
|
630
|
+
best_weight = path_length(final_path, G_m)
|
|
676
631
|
|
|
677
632
|
if is_reversed:
|
|
678
633
|
final_path.reverse()
|
|
679
634
|
|
|
680
635
|
return [nodes[x] for x in final_path], best_weight
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
def tsp_asymmetric(G, start_node=None, end_node=None, quality=2, shots=None, monte_carlo=True, k_neighbors=20, is_cyclic=True, multi_start=1, is_top_level=True):
|
|
639
|
+
nodes = None
|
|
640
|
+
n_nodes = 0
|
|
641
|
+
G_m = None
|
|
642
|
+
if isinstance(G, nx.Graph):
|
|
643
|
+
nodes = list(G.nodes())
|
|
644
|
+
n_nodes = len(nodes)
|
|
645
|
+
G_m = nx.to_numpy_array(G, weight='weight', nonedge=0.0)
|
|
646
|
+
else:
|
|
647
|
+
n_nodes = len(G)
|
|
648
|
+
nodes = list(range(n_nodes))
|
|
649
|
+
G_m = G
|
|
650
|
+
|
|
651
|
+
if is_cyclic:
|
|
652
|
+
start_node = None
|
|
653
|
+
end_node = None
|
|
654
|
+
|
|
655
|
+
if n_nodes < 7:
|
|
656
|
+
if n_nodes > 2:
|
|
657
|
+
if is_cyclic:
|
|
658
|
+
best_path, best_weight = tsp_bruteforce_cyclic(G_m, list(itertools.permutations(list(range(1, n_nodes)))))
|
|
659
|
+
else:
|
|
660
|
+
best_path, best_weight = tsp_bruteforce_acyclic(G_m, list(itertools.permutations(list(range(n_nodes)))))
|
|
661
|
+
|
|
662
|
+
return [nodes[x] for x in best_path], best_weight
|
|
663
|
+
|
|
664
|
+
return tsp_asymmetric_brute_force_driver(G_m, n_nodes, nodes, is_cyclic)
|
|
665
|
+
|
|
666
|
+
|
|
667
|
+
is_reversed = False
|
|
668
|
+
if (start_node is None) and not (end_node is None):
|
|
669
|
+
is_reversed = True
|
|
670
|
+
start_node = end_node
|
|
671
|
+
end_node = None
|
|
672
|
+
G_m = G_m.T
|
|
673
|
+
|
|
674
|
+
a = []
|
|
675
|
+
b = []
|
|
676
|
+
c = []
|
|
677
|
+
if (start_node is None) and (end_node is None):
|
|
678
|
+
if monte_carlo:
|
|
679
|
+
a, b = monte_carlo_loop(n_nodes)
|
|
680
|
+
else:
|
|
681
|
+
best_energy = float("inf")
|
|
682
|
+
for _ in range(multi_start):
|
|
683
|
+
bits = ([], [])
|
|
684
|
+
while (len(bits[0]) == 0) or (len(bits[1]) == 0):
|
|
685
|
+
_, _, bits, energy = spin_glass_solver((G_m + G_m.T) / 2, quality=quality, shots=shots)
|
|
686
|
+
if energy < best_energy:
|
|
687
|
+
best_energy = energy
|
|
688
|
+
a, b = bits
|
|
689
|
+
else:
|
|
690
|
+
is_cyclic = False
|
|
691
|
+
a.append(nodes.index(start_node))
|
|
692
|
+
b = list(range(n_nodes))
|
|
693
|
+
b.remove(a[0])
|
|
694
|
+
if end_node is not None:
|
|
695
|
+
c.append(nodes.index(end_node))
|
|
696
|
+
b.remove(c[0])
|
|
697
|
+
|
|
698
|
+
G_a, G_b = init_G_a_b(G_m, a, b)
|
|
699
|
+
|
|
700
|
+
sol_a = tsp_asymmetric(G_a, quality=quality, monte_carlo=monte_carlo, is_cyclic=False, is_top_level=False, k_neighbors=0, multi_start=multi_start)
|
|
701
|
+
sol_b = tsp_asymmetric(G_b, quality=quality, monte_carlo=monte_carlo, is_cyclic=False, is_top_level=False, k_neighbors=0, multi_start=multi_start)
|
|
702
|
+
|
|
703
|
+
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)
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='pyqrackising',
|
|
54
|
-
version='
|
|
54
|
+
version='4.0.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
|