PyQrackIsing 4.0.0__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-4.0.0/pyqrackising.egg-info → pyqrackising-4.0.1}/PKG-INFO +1 -1
- {pyqrackising-4.0.0 → pyqrackising-4.0.1}/pyproject.toml +1 -1
- {pyqrackising-4.0.0 → pyqrackising-4.0.1}/pyqrackising/tsp.py +127 -104
- {pyqrackising-4.0.0 → pyqrackising-4.0.1/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-4.0.0 → pyqrackising-4.0.1}/setup.py +1 -1
- {pyqrackising-4.0.0 → pyqrackising-4.0.1}/CMakeLists.txt +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.0.1}/LICENSE.md +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.0.1}/MANIFEST.in +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.0.1}/README.md +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.0.1}/pyqrackising/__init__.py +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.0.1}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.0.1}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.0.1}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.0.1}/pyqrackising/maxcut_tfim.py +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.0.1}/pyqrackising/maxcut_tfim_sparse.py +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.0.1}/pyqrackising/spin_glass_solver.py +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.0.1}/pyqrackising/spin_glass_solver_sparse.py +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.0.1}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.0.1}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.0.1}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.0.1}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.0.1}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.0.1}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-4.0.0 → pyqrackising-4.0.1}/setup.cfg +0 -0
|
@@ -11,7 +11,7 @@ build-backend = "setuptools.build_meta"
|
|
|
11
11
|
|
|
12
12
|
[project]
|
|
13
13
|
name = "pyqrackising"
|
|
14
|
-
version = "4.0.
|
|
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"}
|
|
@@ -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)
|
|
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]
|
|
442
417
|
|
|
443
|
-
|
|
418
|
+
if weight_0 >= weight_1:
|
|
419
|
+
return (nodes + [nodes[0]], weight_0)
|
|
444
420
|
|
|
445
|
-
|
|
421
|
+
nodes.reverse()
|
|
446
422
|
|
|
447
|
-
|
|
448
|
-
w_021 = G_m[0, 1] + G_m[0, 2]
|
|
449
|
-
w_120 = G_m[0, 2] + G_m[1, 2]
|
|
423
|
+
return ([nodes[2]] + nodes, weight_1)
|
|
450
424
|
|
|
451
|
-
|
|
452
|
-
|
|
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]
|
|
453
428
|
|
|
454
|
-
|
|
455
|
-
|
|
429
|
+
if w_012 >= w_021 and w_012 >= w_120:
|
|
430
|
+
return (nodes, w_012)
|
|
456
431
|
|
|
457
|
-
|
|
432
|
+
if w_021 >= w_012 and w_021 >= w_120:
|
|
433
|
+
return ([nodes[1], nodes[0], nodes[2]], w_021)
|
|
458
434
|
|
|
459
|
-
|
|
460
|
-
if is_cyclic:
|
|
461
|
-
return (nodes + [nodes[0]], 2 * G_m[0, 1])
|
|
435
|
+
return ([nodes[0], nodes[2], nodes[1]], w_120)
|
|
462
436
|
|
|
463
|
-
|
|
437
|
+
if n_nodes == 2:
|
|
438
|
+
if is_cyclic:
|
|
439
|
+
return (nodes + [nodes[0]], 2 * G_m[0, 1])
|
|
464
440
|
|
|
465
|
-
return (nodes, 0)
|
|
441
|
+
return (nodes, G_m[0, 1])
|
|
466
442
|
|
|
443
|
+
return (nodes, 0)
|
|
467
444
|
|
|
468
|
-
if (start_node is None) and not (end_node is None):
|
|
469
|
-
start_node = end_node
|
|
470
|
-
end_node = None
|
|
471
|
-
|
|
472
|
-
a = []
|
|
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)
|
|
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])
|
|
495
|
-
|
|
496
|
-
G_a, G_b = init_G_a_b(G_m, a, b)
|
|
497
|
-
|
|
498
|
-
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)
|
|
499
|
-
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)
|
|
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, mont
|
|
|
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, mont
|
|
|
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, mon
|
|
|
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, mon
|
|
|
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()
|
|
518
|
+
return tsp_symmetric_brute_force_driver(G_m, n_nodes, nodes, is_cyclic)
|
|
575
519
|
|
|
576
|
-
if is_cyclic:
|
|
577
|
-
return (nodes + [nodes[0]], 2 * weight)
|
|
578
|
-
|
|
579
|
-
return (nodes, weight)
|
|
580
|
-
|
|
581
|
-
return (nodes, 0)
|
|
582
|
-
|
|
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, mon
|
|
|
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, mon
|
|
|
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)
|
|
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)
|
|
619
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, mon
|
|
|
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, mon
|
|
|
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='4.0.
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|