PyQrackIsing 7.1.0__tar.gz → 7.3.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.
Files changed (31) hide show
  1. {pyqrackising-7.1.0/pyqrackising.egg-info → pyqrackising-7.3.0}/PKG-INFO +1 -1
  2. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/README.md +4 -4
  3. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/pyproject.toml +1 -1
  4. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/pyqrackising/__init__.py +1 -1
  5. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/pyqrackising/maxcut_tfim_sparse.py +1 -33
  6. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/pyqrackising/maxcut_tfim_util.py +32 -0
  7. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/pyqrackising/tsp.py +682 -1
  8. pyqrackising-7.3.0/pyqrackising/tsp_maxcut.py +268 -0
  9. {pyqrackising-7.1.0 → pyqrackising-7.3.0/pyqrackising.egg-info}/PKG-INFO +1 -1
  10. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/setup.py +1 -1
  11. pyqrackising-7.1.0/pyqrackising/tsp_maxcut.py +0 -49
  12. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/CMakeLists.txt +0 -0
  13. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/LICENSE.md +0 -0
  14. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/MANIFEST.in +0 -0
  15. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/pyqrackising/_pyqrack_ising.cpp +0 -0
  16. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
  17. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/pyqrackising/generate_tfim_samples.py +0 -0
  18. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/pyqrackising/kernels.cl +0 -0
  19. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/pyqrackising/maxcut_tfim.py +0 -0
  20. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/pyqrackising/maxcut_tfim_streaming.py +0 -0
  21. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/pyqrackising/spin_glass_solver.py +0 -0
  22. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/pyqrackising/spin_glass_solver_sparse.py +0 -0
  23. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/pyqrackising/spin_glass_solver_streaming.py +0 -0
  24. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/pyqrackising/spin_glass_solver_util.py +0 -0
  25. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/pyqrackising/tfim_magnetization.py +0 -0
  26. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/pyqrackising/tfim_square_magnetization.py +0 -0
  27. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/pyqrackising.egg-info/SOURCES.txt +0 -0
  28. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
  29. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/pyqrackising.egg-info/not-zip-safe +0 -0
  30. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/pyqrackising.egg-info/top_level.txt +0 -0
  31. {pyqrackising-7.1.0 → pyqrackising-7.3.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 7.1.0
3
+ Version: 7.3.0
4
4
  Summary: Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)
5
5
  Home-page: https://github.com/vm6502q/PyQrackIsing
6
6
  Author: Dan Strano
@@ -139,17 +139,17 @@ solution_bit_string, cut_value, node_groups, energy = spin_glass_solver_streamin
139
139
  # solution_bit_string, cut_value, node_groups = maxcut_tfim_streaming(G_func, nodes, G_func_args_tuple=args_tuple)
140
140
  ```
141
141
 
142
- Finally, combining insights from both the (Monte Carlo) TSP and MAXCUT solvers, we have `tsp_maxcut(G)`:
142
+ Finally, combining insights from both the (Monte Carlo) TSP and MAXCUT solvers, we have `tsp_maxcut(G)`, `tsp_maxcut_sparse(G)`, and `tsp_maxcut_streaming(G_func, nodes)`:
143
143
 
144
144
  ```
145
- from pyqrackising import tsp_maxcut
145
+ from pyqrackising import tsp_maxcut_sparse
146
146
  import networkx as nx
147
147
 
148
148
  G = nx.petersen_graph()
149
- best_partition, best_cut_value = tsp_maxcut(G, k_neighbors=20)
149
+ best_partition, best_cut_value = tsp_maxcut_sparse(G, k_neighbors=20, is_optimized=False)
150
150
  ```
151
151
 
152
- This solver is entirely serial and can be parallelized over CPU processing elements by user code, easily.
152
+ When `is_optimized=True`, the `spin_glass_solver(G)` is used as a final optimization pass. When `is_optimized=False`, this solver becomes entirely serial and can be parallelized over CPU processing elements by user code, easily.
153
153
 
154
154
  ## Environment Variables
155
155
 
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
12
12
 
13
13
  [project]
14
14
  name = "pyqrackising"
15
- version = "7.1.0"
15
+ version = "7.3.0"
16
16
  requires-python = ">=3.8"
17
17
  description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
18
18
  readme = {file = "README.txt", content-type = "text/markdown"}
@@ -8,5 +8,5 @@ from .spin_glass_solver import spin_glass_solver
8
8
  from .spin_glass_solver_sparse import spin_glass_solver_sparse
9
9
  from .spin_glass_solver_streaming import spin_glass_solver_streaming
10
10
  from .tsp import tsp_symmetric, tsp_asymmetric
11
- from .tsp_maxcut import tsp_maxcut
11
+ from .tsp_maxcut import tsp_maxcut, tsp_maxcut_sparse, tsp_maxcut_streaming
12
12
  from .convert_tensor_network_to_tsp import convert_tensor_network_to_tsp, convert_quimb_tree_to_tsp
@@ -3,9 +3,8 @@ import networkx as nx
3
3
  import numpy as np
4
4
  import os
5
5
  from numba import njit, prange
6
- from scipy.sparse import lil_matrix, csr_matrix
7
6
 
8
- from .maxcut_tfim_util import fix_cdf, get_cut, init_thresholds, maxcut_hamming_cdf, opencl_context
7
+ from .maxcut_tfim_util import binary_search, fix_cdf, get_cut, init_thresholds, maxcut_hamming_cdf, opencl_context, to_scipy_sparse_upper_triangular
9
8
 
10
9
  IS_OPENCL_AVAILABLE = True
11
10
  try:
@@ -14,25 +13,6 @@ except ImportError:
14
13
  IS_OPENCL_AVAILABLE = False
15
14
 
16
15
 
17
- @njit
18
- def binary_search(l, t):
19
- left = 0
20
- right = len(l) - 1
21
-
22
- while left <= right:
23
- mid = (left + right) >> 1
24
-
25
- if l[mid] == t:
26
- return mid
27
-
28
- if l[mid] < t:
29
- left = mid + 1
30
- else:
31
- right = mid - 1
32
-
33
- return len(l)
34
-
35
-
36
16
  @njit
37
17
  def update_repulsion_choice(G_cols, G_data, G_rows, max_weight, weights, n, used, node):
38
18
  # Select node
@@ -292,18 +272,6 @@ def gpu_footer(shots, n_qubits, G_data, G_rows, G_cols, weights, hamming_prob, n
292
272
  return bit_string, best_value, (l, r)
293
273
 
294
274
 
295
- def to_scipy_sparse_upper_triangular(G, nodes, n_nodes, dtype):
296
- lil = lil_matrix((n_nodes, n_nodes), dtype=opencl_context.dtype)
297
- for u in range(n_nodes):
298
- u_node = nodes[u]
299
- for v in range(u + 1, n_nodes):
300
- v_node = nodes[v]
301
- if G.has_edge(u_node, v_node):
302
- lil[u, v] = G[u_node][v_node].get('weight', 1.0)
303
-
304
- return lil.tocsr()
305
-
306
-
307
275
  def maxcut_tfim_sparse(
308
276
  G,
309
277
  quality=None,
@@ -3,6 +3,7 @@ import networkx as nx
3
3
  import numpy as np
4
4
  import os
5
5
  from numba import njit, prange
6
+ from scipy.sparse import lil_matrix
6
7
 
7
8
 
8
9
  @njit
@@ -20,6 +21,37 @@ def get_cut(solution, nodes):
20
21
  return bit_string, l, r
21
22
 
22
23
 
24
+ @njit
25
+ def binary_search(l, t):
26
+ left = 0
27
+ right = len(l) - 1
28
+
29
+ while left <= right:
30
+ mid = (left + right) >> 1
31
+
32
+ if l[mid] == t:
33
+ return mid
34
+
35
+ if l[mid] < t:
36
+ left = mid + 1
37
+ else:
38
+ right = mid - 1
39
+
40
+ return len(l)
41
+
42
+
43
+ def to_scipy_sparse_upper_triangular(G, nodes, n_nodes, dtype):
44
+ lil = lil_matrix((n_nodes, n_nodes), dtype=opencl_context.dtype)
45
+ for u in range(n_nodes):
46
+ u_node = nodes[u]
47
+ for v in range(u + 1, n_nodes):
48
+ v_node = nodes[v]
49
+ if G.has_edge(u_node, v_node):
50
+ lil[u, v] = G[u_node][v_node].get('weight', 1.0)
51
+
52
+ return lil.tocsr()
53
+
54
+
23
55
  @njit(parallel=True)
24
56
  def init_theta(h_mult, n_qubits, J_eff, degrees, dtype):
25
57
  theta = np.empty(n_qubits, dtype=dtype)
@@ -1,9 +1,10 @@
1
1
  from .spin_glass_solver import spin_glass_solver
2
- from .maxcut_tfim_util import opencl_context
2
+ from .maxcut_tfim_util import binary_search, opencl_context, to_scipy_sparse_upper_triangular
3
3
  import itertools
4
4
  import networkx as nx
5
5
  from numba import njit
6
6
  import numpy as np
7
+ from scipy.sparse import lil_matrix
7
8
 
8
9
 
9
10
  # two_opt() and targeted_three_opt() written by Elara (OpenAI ChatGPT instance)
@@ -34,6 +35,7 @@ def one_way_two_opt(path, G):
34
35
  best_path, best_dist = new_path, new_dist
35
36
  improved = True
36
37
  path = best_path
38
+
37
39
  return best_path, best_dist
38
40
 
39
41
 
@@ -55,6 +57,7 @@ def anchored_two_opt(path, G):
55
57
  best_path, best_dist = new_path, new_dist
56
58
  improved = True
57
59
  path = best_path
60
+
58
61
  return best_path, best_dist
59
62
 
60
63
 
@@ -76,6 +79,7 @@ def reversed_anchored_two_opt(path, G):
76
79
  best_path, best_dist = new_path, new_dist
77
80
  improved = True
78
81
  path = best_path
82
+
79
83
  return best_path, best_dist
80
84
 
81
85
 
@@ -97,6 +101,7 @@ def two_opt(path, G):
97
101
  best_path, best_dist = new_path, new_dist
98
102
  improved = True
99
103
  path = best_path
104
+
100
105
  return best_path, best_dist
101
106
 
102
107
 
@@ -788,3 +793,679 @@ def tsp_asymmetric(
788
793
  )
789
794
 
790
795
  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)
796
+
797
+
798
+ @njit
799
+ def get_G_m(G_data, G_rows, G_cols, low, high):
800
+ if high < low:
801
+ low, high = high, low
802
+
803
+ start = G_rows[low]
804
+ end = G_rows[low + 1]
805
+
806
+ i = binary_search(G_cols[start:end], high) + start
807
+ if i < end:
808
+ return G_data[i]
809
+
810
+ return 0.0
811
+
812
+
813
+ @njit
814
+ def path_length_sparse(path, G_data, G_rows, G_cols):
815
+ tot_len = 0.0
816
+ for i in range(len(path) - 1):
817
+ low, high = path[i], path[i + 1]
818
+ if high < low:
819
+ low, high = high, low
820
+
821
+ start = G_rows[low]
822
+ end = G_rows[low + 1]
823
+
824
+ i = binary_search(G_cols[start:end], high) + start
825
+ if i < end:
826
+ tot_len += G_data[i]
827
+
828
+ return tot_len
829
+
830
+
831
+ @njit
832
+ def one_way_two_opt_sparse(path, G_data, G_rows, G_cols):
833
+ improved = True
834
+ best_path = path
835
+ best_dist = path_length_sparse(best_path, G_data, G_rows, G_cols)
836
+ path_len = len(path)
837
+
838
+ while improved:
839
+ improved = False
840
+ for i in range(1, path_len - 1):
841
+ for j in range(i + 2, path_len):
842
+ new_path = best_path[:]
843
+ new_path[i:j] = best_path[j-1:i-1:-1]
844
+ new_dist = path_length_sparse(new_path, G_data, G_rows, G_cols)
845
+ if new_dist < best_dist:
846
+ best_path, best_dist = new_path, new_dist
847
+ improved = True
848
+ path = best_path
849
+
850
+ return best_path, best_dist
851
+
852
+
853
+ @njit
854
+ def targeted_three_opt_sparse(path, G_data, G_rows, G_cols, neighbor_lists, k_neighbors=20):
855
+ n = len(path)
856
+ best_path = path[:]
857
+ best_dist = path_length_sparse(best_path, G_data, G_rows, G_cols)
858
+ improved = True
859
+
860
+ while improved:
861
+ improved = False
862
+ for i in range(n - 5):
863
+ for _j in range(k_neighbors):
864
+ j = neighbor_lists[path[i], _j]
865
+ if j <= i or j >= n-3:
866
+ continue
867
+ for _k in range(k_neighbors):
868
+ k = neighbor_lists[path[j], _k]
869
+ if k <= j or k >= n-1:
870
+ continue
871
+
872
+ # Extract indices
873
+ A, B, C, D, E, F = path[i], path[i+1], path[j], path[j+1], path[k], path[k+1]
874
+
875
+ # 7 unique cases (same as brute force, but restricted)
876
+ new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:]
877
+ dist = path_length_sparse(new_path, G_data, G_rows, G_cols)
878
+ if dist < best_dist:
879
+ best_path, best_dist, improved = new_path, dist, True
880
+ break
881
+
882
+ new_path = best_path[:j+1] + best_path[j+1:k+1][::-1] + best_path[k+1:]
883
+ dist = path_length_sparse(new_path, G_data, G_rows, G_cols)
884
+ if dist < best_dist:
885
+ best_path, best_dist, improved = new_path, dist, True
886
+ break
887
+
888
+ new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:k+1][::-1] + best_path[k+1:]
889
+ dist = path_length_sparse(new_path, G_data, G_rows, G_cols)
890
+ if dist < best_dist:
891
+ best_path, best_dist, improved = new_path, dist, True
892
+ break
893
+
894
+ new_path = best_path[:i+1] + best_path[j+1:k+1] + best_path[i+1:j+1] + best_path[k+1:]
895
+ dist = path_length_sparse(new_path, G_data, G_rows, G_cols)
896
+ if dist < best_dist:
897
+ best_path, best_dist, improved = new_path, dist, True
898
+ break
899
+
900
+ new_path = best_path[:i+1] + best_path[j+1:k+1][::-1] + best_path[i+1:j+1][::-1] + best_path[k+1:]
901
+ dist = path_length_sparse(new_path, G_data, G_rows, G_cols)
902
+ if dist < best_dist:
903
+ best_path, best_dist, improved = new_path, dist, True
904
+ break
905
+
906
+ new_path = best_path[:i+1] + best_path[j+1:k+1] + best_path[i+1:j+1][::-1] + best_path[k+1:]
907
+ dist = path_length_sparse(new_path, G_data, G_rows, G_cols)
908
+ if dist < best_dist:
909
+ best_path, best_dist, improved = new_path, dist, True
910
+ break
911
+
912
+ new_path = best_path[:i+1] + best_path[j+1:k+1][::-1] + best_path[i+1:j+1] + best_path[k+1:]
913
+ dist = path_length_sparse(new_path, G_data, G_rows, G_cols)
914
+ if dist < best_dist:
915
+ best_path, best_dist, improved = new_path, dist, True
916
+ break
917
+
918
+ if improved:
919
+ break
920
+
921
+ if improved:
922
+ break
923
+
924
+ path = best_path[:]
925
+
926
+ return best_path, best_dist
927
+
928
+
929
+ @njit
930
+ def stich_singlet_sparse(G_data, G_rows, G_cols, singlet, bulk):
931
+ best_path = bulk.copy()
932
+ best_weight = get_G_m(G_data, G_rows, G_cols, singlet, bulk[0])
933
+ weight = get_G_m(G_data, G_rows, G_cols, singlet, bulk[-1])
934
+ if weight < best_weight:
935
+ best_weight = weight
936
+ best_path += [singlet]
937
+ else:
938
+ best_path = [singlet] + best_path
939
+
940
+ for i in range(1, len(bulk)):
941
+ weight = (
942
+ get_G_m(G_data, G_rows, G_cols, singlet, bulk[i - 1]) +
943
+ get_G_m(G_data, G_rows, G_cols, singlet, bulk[i]) -
944
+ get_G_m(G_data, G_rows, G_cols, bulk[i - 1], bulk[i])
945
+ )
946
+ if weight < best_weight:
947
+ best_weight = weight
948
+ best_path = bulk.copy()
949
+ best_path.insert(i, singlet)
950
+
951
+ return best_path
952
+
953
+
954
+ @njit
955
+ def stitch_sparse_symmetric(G_data, G_rows, G_cols, path_a, path_b):
956
+ if len(path_a) == 1:
957
+ return stich_singlet_sparse(G_data, G_rows, G_cols, path_a[0], path_b)
958
+
959
+ if len(path_b) == 1:
960
+ return stich_singlet_sparse(G_data, G_rows, G_cols, path_b[0], path_a)
961
+
962
+ terminals_a = [path_a[0], path_a[-1]]
963
+ terminals_b = [path_b[0], path_b[-1]]
964
+
965
+ best_connect = get_G_m(G_data, G_rows, G_cols, terminals_a[1], terminals_b[0])
966
+ best_path = path_b.copy()
967
+ weight = get_G_m(G_data, G_rows, G_cols, terminals_a[0], terminals_b[1])
968
+ if weight < best_connect:
969
+ best_connect = weight
970
+ best_path += path_a
971
+ else:
972
+ best_path = path_a + best_path
973
+
974
+ for _ in range(2):
975
+ for _ in range(2):
976
+ for i in range(1, len(path_b)):
977
+ weight = (
978
+ get_G_m(G_data, G_rows, G_cols, terminals_a[0], path_b[i - 1]) +
979
+ get_G_m(G_data, G_rows, G_cols, terminals_a[1], path_b[i]) -
980
+ get_G_m(G_data, G_rows, G_cols, path_b[i - 1], path_b[i])
981
+ )
982
+ if weight < best_connect:
983
+ best_connect = weight
984
+ best_path = path_b.copy()
985
+ best_path[i:i] = path_a
986
+ path_a.reverse()
987
+ terminals_a.reverse()
988
+ path_a, path_b = path_b, path_a
989
+ terminals_a, terminals_b = terminals_b, terminals_a
990
+
991
+ return best_path
992
+
993
+
994
+ @njit
995
+ def restitch_sparse(G_data, G_rows, G_cols, path):
996
+ l = len(path)
997
+ mid = ((l + 1) if (l & 1) and (np.random.random() < 0.5) else l) >> 1
998
+
999
+ if mid < 4:
1000
+ return path
1001
+
1002
+ path_a = restitch_sparse(G_data, G_rows, G_cols, path[:mid])
1003
+ path_b = restitch_sparse(G_data, G_rows, G_cols, path[mid:])
1004
+
1005
+ return stitch_sparse_symmetric(G_data, G_rows, G_cols, path_a, path_b)
1006
+
1007
+
1008
+ @njit
1009
+ def tsp_sparse_brute_force_driver(G_data, G_rows, G_cols, n_nodes, nodes):
1010
+ if n_nodes == 3:
1011
+ w_012 = get_G_m(G_data, G_rows, G_cols, 0, 1) + get_G_m(G_data, G_rows, G_cols, 1, 2)
1012
+ w_021 = get_G_m(G_data, G_rows, G_cols, 0, 1) + get_G_m(G_data, G_rows, G_cols, 0, 2)
1013
+ w_120 = get_G_m(G_data, G_rows, G_cols, 0, 2) + get_G_m(G_data, G_rows, G_cols, 1, 2)
1014
+
1015
+ if w_012 >= w_021 and w_012 >= w_120:
1016
+ return (nodes, w_012)
1017
+
1018
+ if w_021 >= w_012 and w_021 >= w_120:
1019
+ return ([nodes[1], nodes[0], nodes[2]], w_021)
1020
+
1021
+ return ([nodes[0], nodes[2], nodes[1]], w_120)
1022
+
1023
+ if n_nodes == 2:
1024
+ return (nodes, get_G_m(G_data, G_rows, G_cols, 0, 1))
1025
+
1026
+ return (nodes, 0)
1027
+
1028
+
1029
+ @njit
1030
+ def tsp_sparse_bruteforce(G_data, G_rows, G_cols, perms):
1031
+ n = len(G_rows) - 1
1032
+ best_weight = float('inf')
1033
+ best_path = None
1034
+
1035
+ # Must fix node 0 at start to remove rotational symmetry in cyclic case!
1036
+
1037
+ max_i = len(perms[0]) - 1
1038
+
1039
+ for path in perms:
1040
+ weight = 0.0
1041
+ for i in range(max_i):
1042
+ low, high = path[i], path[i + 1]
1043
+ if high < low:
1044
+ low, high = high, low
1045
+
1046
+ start = G_rows[low]
1047
+ end = G_rows[low + 1]
1048
+
1049
+ i = binary_search(G_cols[start:end], high) + start
1050
+ if i < end:
1051
+ weight += G_data[i]
1052
+
1053
+ if weight < best_weight:
1054
+ best_weight = weight
1055
+ best_path = path
1056
+
1057
+ best_path = list(best_path)
1058
+
1059
+ return best_path, best_weight
1060
+
1061
+
1062
+ def init_G_a_b_sparse(G_m, a, b, dtype):
1063
+ n_a_nodes = len(a)
1064
+ n_b_nodes = len(b)
1065
+ G_a = lil_matrix((n_a_nodes, n_a_nodes), dtype=opencl_context.dtype)
1066
+ G_b = lil_matrix((n_b_nodes, n_b_nodes), dtype=opencl_context.dtype)
1067
+ for i in range(n_a_nodes):
1068
+ for j in range(n_a_nodes):
1069
+ if i == j:
1070
+ continue
1071
+ weight = G_m[a[i], a[j]]
1072
+ if weight != 0.0:
1073
+ G_a[i, j] = weight
1074
+ for i in range(n_b_nodes):
1075
+ for j in range(n_b_nodes):
1076
+ if i == j:
1077
+ continue
1078
+ weight = G_m[b[i], b[j]]
1079
+ if weight != 0.0:
1080
+ G_b[i, j] = weight
1081
+
1082
+ return G_a.tocsr(), G_b.tocsr()
1083
+
1084
+ def tsp_symmetric_sparse_driver(G_data, G_rows, G_cols, is_top_level, k_neighbors, nodes, sol_a, sol_b, a, b):
1085
+ path_a = [a[x] for x in sol_a[0]]
1086
+ path_b = [b[x] for x in sol_b[0]]
1087
+
1088
+ restitch_sparse(G_data, G_rows, G_cols, path_a)
1089
+ restitch_sparse(G_data, G_rows, G_cols, path_b)
1090
+
1091
+ best_path = stitch_sparse_symmetric(G_data, G_rows, G_cols, path_a, path_b)
1092
+
1093
+ if is_top_level:
1094
+ best_path, _ = one_way_two_opt_sparse(best_path, G_data, G_rows, G_cols)
1095
+
1096
+ if k_neighbors > 0:
1097
+ # Precompute nearest neighbors for each node
1098
+ n_rows = len(G_rows) - 1
1099
+
1100
+ neighbor_lists = [
1101
+ ([(float("inf"), 0)] * k_neighbors) for i in range(n_rows)
1102
+ ]
1103
+
1104
+ for i in range(n_rows):
1105
+ for j in range(n_rows):
1106
+ val = get_G_m(G_data, G_rows, G_cols, i, j)
1107
+ if val < neighbor_lists[i][-1][0]:
1108
+ neighbor_lists[i][-1] = (val, j)
1109
+ neighbor_lists[i].sort(key=lambda x: x[0])
1110
+
1111
+ for i in range(len(neighbor_lists)):
1112
+ neighbor_lists[i] = [c[1] for c in neighbor_lists[i]]
1113
+
1114
+ best_path, _ = targeted_three_opt_sparse(best_path, G_data, G_rows, G_cols, np.array(neighbor_lists), k_neighbors)
1115
+
1116
+ # We just corrected segments of 2 and 3,
1117
+ # and this is top level,
1118
+ # so correct segments of 4 to 7.
1119
+ restitch_sparse(G_data, G_rows, G_cols, best_path)
1120
+
1121
+ best_weight = path_length_sparse(best_path, G_data, G_rows, G_cols)
1122
+
1123
+ return [nodes[x] for x in best_path], best_weight
1124
+
1125
+
1126
+ def tsp_symmetric_sparse(
1127
+ G,
1128
+ k_neighbors=20,
1129
+ is_top_level=True
1130
+ ):
1131
+ dtype = opencl_context.dtype
1132
+ nodes = None
1133
+ n_nodes = 0
1134
+ G_m = None
1135
+ if isinstance(G, nx.Graph):
1136
+ nodes = list(G.nodes())
1137
+ n_nodes = len(nodes)
1138
+ G_m = to_scipy_sparse_upper_triangular(G, nodes, n_nodes, dtype)
1139
+ else:
1140
+ n_nodes = G.shape[0]
1141
+ nodes = list(range(n_nodes))
1142
+ G_m = G
1143
+
1144
+ if n_nodes < 7:
1145
+ if n_nodes > 3:
1146
+ best_path, best_weight = tsp_sparse_bruteforce(G_m.data, G_m.indptr, G_m.indices, list(itertools.permutations(list(range(n_nodes)))))
1147
+
1148
+ return [nodes[x] for x in best_path], best_weight
1149
+
1150
+ return tsp_sparse_brute_force_driver(G_m.data, G_m.indptr, G_m.indices, n_nodes, nodes)
1151
+
1152
+ a, b = monte_carlo_loop(n_nodes)
1153
+
1154
+ G_a, G_b = init_G_a_b_sparse(G_m, a, b, dtype)
1155
+
1156
+ sol_a = tsp_symmetric_sparse(
1157
+ G_a,
1158
+ is_top_level=False,
1159
+ k_neighbors=0
1160
+ )
1161
+ sol_b = tsp_symmetric_sparse(
1162
+ G_b,
1163
+ is_top_level=False,
1164
+ k_neighbors=0
1165
+ )
1166
+
1167
+ return tsp_symmetric_sparse_driver(G_m.data, G_m.indptr, G_m.indices, is_top_level, k_neighbors, nodes, sol_a, sol_b, a, b)
1168
+
1169
+
1170
+ @njit
1171
+ def path_length_streaming(path, G_func):
1172
+ tot_len = 0.0
1173
+ for i in range(len(path) - 1):
1174
+ tot_len += G_func(path[i], path[i + 1])
1175
+
1176
+ return tot_len
1177
+
1178
+
1179
+ @njit
1180
+ def one_way_two_opt_streaming(path, G_func):
1181
+ improved = True
1182
+ best_path = path
1183
+ best_dist = path_length_streaming(best_path, G_func)
1184
+ path_len = len(path)
1185
+
1186
+ while improved:
1187
+ improved = False
1188
+ for i in range(1, path_len - 1):
1189
+ for j in range(i + 2, path_len):
1190
+ new_path = best_path[:]
1191
+ new_path[i:j] = best_path[j-1:i-1:-1]
1192
+ new_dist = path_length_streaming(new_path, G_func)
1193
+ if new_dist < best_dist:
1194
+ best_path, best_dist = new_path, new_dist
1195
+ improved = True
1196
+ path = best_path
1197
+
1198
+ return best_path, best_dist
1199
+
1200
+
1201
+ @njit
1202
+ def targeted_three_opt_streaming(path, G_func, neighbor_lists, k_neighbors=20):
1203
+ n = len(path)
1204
+ best_path = path[:]
1205
+ best_dist = path_length_streaming(best_path, G_func)
1206
+ improved = True
1207
+
1208
+ while improved:
1209
+ improved = False
1210
+ for i in range(n - 5):
1211
+ for _j in range(k_neighbors):
1212
+ j = neighbor_lists[path[i], _j]
1213
+ if j <= i or j >= n-3:
1214
+ continue
1215
+ for _k in range(k_neighbors):
1216
+ k = neighbor_lists[path[j], _k]
1217
+ if k <= j or k >= n-1:
1218
+ continue
1219
+
1220
+ # Extract indices
1221
+ A, B, C, D, E, F = path[i], path[i+1], path[j], path[j+1], path[k], path[k+1]
1222
+
1223
+ # 7 unique cases (same as brute force, but restricted)
1224
+ new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:]
1225
+ dist = path_length_streaming(new_path, G_func)
1226
+ if dist < best_dist:
1227
+ best_path, best_dist, improved = new_path, dist, True
1228
+ break
1229
+
1230
+ new_path = best_path[:j+1] + best_path[j+1:k+1][::-1] + best_path[k+1:]
1231
+ dist = path_length_streaming(new_path, G_func)
1232
+ if dist < best_dist:
1233
+ best_path, best_dist, improved = new_path, dist, True
1234
+ break
1235
+
1236
+ new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:k+1][::-1] + best_path[k+1:]
1237
+ dist = path_length_streaming(new_path, G_func)
1238
+ if dist < best_dist:
1239
+ best_path, best_dist, improved = new_path, dist, True
1240
+ break
1241
+
1242
+ new_path = best_path[:i+1] + best_path[j+1:k+1] + best_path[i+1:j+1] + best_path[k+1:]
1243
+ dist = path_length_streaming(new_path, G_func)
1244
+ if dist < best_dist:
1245
+ best_path, best_dist, improved = new_path, dist, True
1246
+ break
1247
+
1248
+ new_path = best_path[:i+1] + best_path[j+1:k+1][::-1] + best_path[i+1:j+1][::-1] + best_path[k+1:]
1249
+ dist = path_length_streaming(new_path, G_func)
1250
+ if dist < best_dist:
1251
+ best_path, best_dist, improved = new_path, dist, True
1252
+ break
1253
+
1254
+ new_path = best_path[:i+1] + best_path[j+1:k+1] + best_path[i+1:j+1][::-1] + best_path[k+1:]
1255
+ dist = path_length_streaming(new_path, G_func)
1256
+ if dist < best_dist:
1257
+ best_path, best_dist, improved = new_path, dist, True
1258
+ break
1259
+
1260
+ new_path = best_path[:i+1] + best_path[j+1:k+1][::-1] + best_path[i+1:j+1] + best_path[k+1:]
1261
+ dist = path_length_streaming(new_path, G_func)
1262
+ if dist < best_dist:
1263
+ best_path, best_dist, improved = new_path, dist, True
1264
+ break
1265
+
1266
+ if improved:
1267
+ break
1268
+
1269
+ if improved:
1270
+ break
1271
+
1272
+ path = best_path[:]
1273
+
1274
+ return best_path, best_dist
1275
+
1276
+
1277
+ @njit
1278
+ def stich_singlet_streaming(G_func, singlet, bulk):
1279
+ best_path = bulk.copy()
1280
+ best_weight = G_func(singlet, bulk[0])
1281
+ weight = G_func(singlet, bulk[-1])
1282
+ if weight < best_weight:
1283
+ best_weight = weight
1284
+ best_path += [singlet]
1285
+ else:
1286
+ best_path = [singlet] + best_path
1287
+
1288
+ for i in range(1, len(bulk)):
1289
+ weight = (
1290
+ G_func(singlet, bulk[i - 1]) +
1291
+ G_func(singlet, bulk[i]) -
1292
+ G_func(bulk[i - 1], bulk[i])
1293
+ )
1294
+ if weight < best_weight:
1295
+ best_weight = weight
1296
+ best_path = bulk.copy()
1297
+ best_path.insert(i, singlet)
1298
+
1299
+ return best_path
1300
+
1301
+
1302
+ @njit
1303
+ def stitch_streaming_symmetric(G_func, path_a, path_b):
1304
+ if len(path_a) == 1:
1305
+ return stich_singlet_streaming(G_func, path_a[0], path_b)
1306
+
1307
+ if len(path_b) == 1:
1308
+ return stich_singlet_streaming(G_func, path_b[0], path_a)
1309
+
1310
+ terminals_a = [path_a[0], path_a[-1]]
1311
+ terminals_b = [path_b[0], path_b[-1]]
1312
+
1313
+ best_connect = G_func(terminals_a[1], terminals_b[0])
1314
+ best_path = path_b.copy()
1315
+ weight = G_func(terminals_a[0], terminals_b[1])
1316
+ if weight < best_connect:
1317
+ best_connect = weight
1318
+ best_path += path_a
1319
+ else:
1320
+ best_path = path_a + best_path
1321
+
1322
+ for _ in range(2):
1323
+ for _ in range(2):
1324
+ for i in range(1, len(path_b)):
1325
+ weight = (
1326
+ G_func(terminals_a[0], path_b[i - 1]) +
1327
+ G_func(terminals_a[1], path_b[i]) -
1328
+ G_func(path_b[i - 1], path_b[i])
1329
+ )
1330
+ if weight < best_connect:
1331
+ best_connect = weight
1332
+ best_path = path_b.copy()
1333
+ best_path[i:i] = path_a
1334
+ path_a.reverse()
1335
+ terminals_a.reverse()
1336
+ path_a, path_b = path_b, path_a
1337
+ terminals_a, terminals_b = terminals_b, terminals_a
1338
+
1339
+ return best_path
1340
+
1341
+
1342
+ @njit
1343
+ def restitch_streaming(G_func, path):
1344
+ l = len(path)
1345
+ mid = ((l + 1) if (l & 1) and (np.random.random() < 0.5) else l) >> 1
1346
+
1347
+ if mid < 4:
1348
+ return path
1349
+
1350
+ path_a = restitch_streaming(G_func, path[:mid])
1351
+ path_b = restitch_streaming(G_func, path[mid:])
1352
+
1353
+ return stitch_streaming_symmetric(G_func, path_a, path_b)
1354
+
1355
+
1356
+ @njit
1357
+ def tsp_streaming_brute_force_driver(G_func, n_nodes, nodes):
1358
+ if n_nodes == 3:
1359
+ w_012 = G_func(0, 1) + G_func(1, 2)
1360
+ w_021 = G_func(0, 1) + G_func(0, 2)
1361
+ w_120 = G_func(0, 2) + G_func(1, 2)
1362
+
1363
+ if w_012 >= w_021 and w_012 >= w_120:
1364
+ return (nodes, w_012)
1365
+
1366
+ if w_021 >= w_012 and w_021 >= w_120:
1367
+ return ([nodes[1], nodes[0], nodes[2]], w_021)
1368
+
1369
+ return ([nodes[0], nodes[2], nodes[1]], w_120)
1370
+
1371
+ if n_nodes == 2:
1372
+ return (nodes, G_func(0, 1))
1373
+
1374
+ return (nodes, 0)
1375
+
1376
+
1377
+ @njit
1378
+ def tsp_streaming_bruteforce(G_func, perms, n):
1379
+ best_weight = float('inf')
1380
+ best_path = None
1381
+
1382
+ # Must fix node 0 at start to remove rotational symmetry in cyclic case!
1383
+
1384
+ max_i = len(perms[0]) - 1
1385
+
1386
+ for path in perms:
1387
+ weight = 0.0
1388
+ for i in range(max_i):
1389
+ weight += G_func(path[i], path[i+1])
1390
+
1391
+ if weight < best_weight:
1392
+ best_weight = weight
1393
+ best_path = path
1394
+
1395
+ best_path = list(best_path)
1396
+
1397
+ return best_path, best_weight
1398
+
1399
+
1400
+ def tsp_symmetric_streaming_driver(G_func, is_top_level, k_neighbors, nodes, path_a, path_b):
1401
+ restitch_streaming(G_func, path_a)
1402
+ restitch_streaming(G_func, path_b)
1403
+
1404
+ best_path = stitch_streaming_symmetric(G_func, path_a, path_b)
1405
+
1406
+ if is_top_level:
1407
+ best_path, _ = one_way_two_opt_streaming(best_path, G_func)
1408
+
1409
+ if k_neighbors > 0:
1410
+ # Precompute nearest neighbors for each node
1411
+ n_rows = len(nodes)
1412
+
1413
+ neighbor_lists = [
1414
+ ([(float("inf"), 0)] * k_neighbors) for i in range(n_rows)
1415
+ ]
1416
+
1417
+ for i in nodes:
1418
+ for j in nodes:
1419
+ val = G_func(i, j)
1420
+ if val < neighbor_lists[i][-1][0]:
1421
+ neighbor_lists[i][-1] = (val, j)
1422
+ neighbor_lists[i].sort(key=lambda x: x[0])
1423
+
1424
+ for i in range(len(neighbor_lists)):
1425
+ neighbor_lists[i] = [c[1] for c in neighbor_lists[i]]
1426
+
1427
+ best_path, _ = targeted_three_opt_streaming(best_path, G_func, np.array(neighbor_lists), k_neighbors)
1428
+
1429
+ # We just corrected segments of 2 and 3,
1430
+ # and this is top level,
1431
+ # so correct segments of 4 to 7.
1432
+ restitch_streaming(G_func, best_path)
1433
+
1434
+ best_weight = path_length_streaming(best_path, G_func)
1435
+
1436
+ return [nodes[x] for x in best_path], best_weight
1437
+
1438
+
1439
+ def tsp_symmetric_streaming(
1440
+ G_func,
1441
+ nodes,
1442
+ k_neighbors=20,
1443
+ is_top_level=True
1444
+ ):
1445
+ dtype = opencl_context.dtype
1446
+ n_nodes = len(nodes)
1447
+
1448
+ if n_nodes < 7:
1449
+ if n_nodes > 3:
1450
+ best_path, best_weight = tsp_streaming_bruteforce(G_func, list(itertools.permutations(nodes)), n_nodes)
1451
+
1452
+ return best_path, best_weight
1453
+
1454
+ return tsp_streaming_brute_force_driver(G_func, n_nodes, nodes)
1455
+
1456
+ a, b = monte_carlo_loop(n_nodes)
1457
+
1458
+ sol_a = tsp_symmetric_streaming(
1459
+ G_func,
1460
+ a,
1461
+ is_top_level=False,
1462
+ k_neighbors=0
1463
+ )
1464
+ sol_b = tsp_symmetric_streaming(
1465
+ G_func,
1466
+ b,
1467
+ is_top_level=False,
1468
+ k_neighbors=0
1469
+ )
1470
+
1471
+ return tsp_symmetric_streaming_driver(G_func, is_top_level, k_neighbors, nodes, sol_a[0], sol_b[0])
@@ -0,0 +1,268 @@
1
+ from .maxcut_tfim_util import binary_search, to_scipy_sparse_upper_triangular
2
+ from .tsp import tsp_symmetric, tsp_symmetric_sparse, tsp_symmetric_streaming
3
+ from .spin_glass_solver import spin_glass_solver
4
+ from .spin_glass_solver_sparse import spin_glass_solver_sparse
5
+ from .spin_glass_solver_streaming import spin_glass_solver_streaming
6
+ import networkx as nx
7
+ from numba import njit
8
+
9
+
10
+ @njit
11
+ def tsp_to_maxcut_bipartition(tsp_path, weights):
12
+ n = len(tsp_path)
13
+ best_cut_value = -float('inf')
14
+ best_partition = None
15
+ direction = 0
16
+
17
+ for offset in [-1, 0, 1]:
18
+ mid = n // 2 + offset
19
+ A = tsp_path[:mid]
20
+ B = tsp_path[mid:]
21
+ cut_value = 0.0
22
+ for u in A:
23
+ for v in B:
24
+ cut_value += weights[u, v]
25
+ if cut_value > best_cut_value:
26
+ best_cut_value = cut_value
27
+ best_partition = (A, B)
28
+ direction = offset
29
+
30
+ if direction == 0:
31
+ return best_partition, best_cut_value
32
+
33
+ improved = True
34
+ best_offset = direction
35
+ while improved:
36
+ improved = False
37
+ offset = best_offset + direction
38
+ mid = n // 2 + offset
39
+ A = tsp_path[:mid]
40
+ B = tsp_path[mid:]
41
+ cut_value = 0.0
42
+ for u in A:
43
+ for v in B:
44
+ cut_value += weights[u, v]
45
+ if cut_value > best_cut_value:
46
+ best_cut_value = cut_value
47
+ best_partition = (A, B)
48
+ best_offset = offset
49
+ improved = True
50
+
51
+ return best_partition, best_cut_value
52
+
53
+
54
+ @njit
55
+ def get_cut(bitstring, nodes):
56
+ solution = list(bitstring)
57
+ l, r = [], []
58
+ for i in range(len(solution)):
59
+ if solution[i] == "1":
60
+ r.append(nodes[i])
61
+ else:
62
+ l.append(nodes[i])
63
+
64
+ return l, r
65
+
66
+
67
+ @njit
68
+ def get_bitstring(partition, nodes):
69
+ bitstring = ""
70
+ for node in nodes:
71
+ bitstring += "0" if node in partition[0] else "1"
72
+
73
+ return bitstring
74
+
75
+
76
+ def tsp_maxcut(G, k_neighbors=20, is_optimized=False, **kwargs):
77
+ G_m = None
78
+ if isinstance(G, nx.Graph):
79
+ nodes = list(G.nodes())
80
+ n_qubits = len(nodes)
81
+ G_m = nx.to_numpy_array(G, weight='weight', nonedge=0.0, dtype=dtype)
82
+ else:
83
+ n_qubits = len(G)
84
+ nodes = list(range(n_qubits))
85
+ G_m = G
86
+
87
+ path, length = tsp_symmetric(G, is_cyclic=False, monte_carlo=True, k_neighbors=k_neighbors)
88
+ partition, cut_value = tsp_to_maxcut_bipartition(path, G_m)
89
+
90
+ if not is_optimized:
91
+ bitstring = get_bitstring(partition, nodes)
92
+ theta_bits = [(b == "1") for b in list(bitstring)]
93
+ energy = 0
94
+ for u in range(n_qubits):
95
+ for v in range(u + 1, n_qubits):
96
+ val = G_m[nodes[u], nodes[v]]
97
+ energy += (val if theta_bits[u] == theta_bits[v] else -val)
98
+
99
+ return bitstring, cut_value, partition, energy
100
+
101
+ bitint = 0
102
+ for b in partition[0]:
103
+ bitint |= 1 << b
104
+
105
+ bitstring, cut_value, cut, energy = spin_glass_solver(G_m, best_guess=bitint, **kwargs)
106
+ l, r = get_cut(bitstring, nodes)
107
+
108
+ return bitstring, cut_value, (l, r), energy
109
+
110
+
111
+ @njit
112
+ def tsp_to_maxcut_bipartition_sparse(tsp_path, G_data, G_rows, G_cols):
113
+ n = len(tsp_path)
114
+ best_cut_value = -float('inf')
115
+ best_partition = None
116
+ direction = 0
117
+
118
+ for offset in [-1, 0, 1]:
119
+ mid = n // 2 + offset
120
+ A = tsp_path[:mid]
121
+ B = tsp_path[mid:]
122
+ cut_value = 0.0
123
+ for u in A:
124
+ for v in B:
125
+ low, high = (u, v) if u < v else (v, u)
126
+ start = G_rows[low]
127
+ end = G_rows[low + 1]
128
+ i = binary_search(G_cols[start:end], high) + start
129
+ if i < end:
130
+ cut_value += G_data[i]
131
+ if cut_value > best_cut_value:
132
+ best_cut_value = cut_value
133
+ best_partition = (A, B)
134
+ direction = offset
135
+
136
+ if direction == 0:
137
+ return best_partition, best_cut_value
138
+
139
+ improved = True
140
+ best_offset = direction
141
+ while improved:
142
+ improved = False
143
+ offset = best_offset + direction
144
+ mid = n // 2 + offset
145
+ A = tsp_path[:mid]
146
+ B = tsp_path[mid:]
147
+ cut_value = 0.0
148
+ for u in A:
149
+ for v in B:
150
+ low, high = (u, v) if u < v else (v, u)
151
+ start = G_rows[low]
152
+ end = G_rows[low + 1]
153
+ i = binary_search(G_cols[start:end], high) + start
154
+ if i < end:
155
+ cut_value += G_data[i]
156
+ if cut_value > best_cut_value:
157
+ best_cut_value = cut_value
158
+ best_partition = (A, B)
159
+ best_offset = offset
160
+ improved = True
161
+
162
+ return best_partition, best_cut_value
163
+
164
+
165
+ def tsp_maxcut_sparse(G, k_neighbors=20, is_optimized=False, **kwargs):
166
+ G_m = None
167
+ if isinstance(G, nx.Graph):
168
+ nodes = list(G.nodes())
169
+ n_qubits = len(nodes)
170
+ G_m = to_scipy_sparse_upper_triangular(G, nodes, n_qubits, dtype)
171
+ else:
172
+ n_qubits = G.shape[0]
173
+ nodes = list(range(n_qubits))
174
+ G_m = G
175
+
176
+ path, length = tsp_symmetric_sparse(G, k_neighbors=k_neighbors)
177
+ partition, cut_value = tsp_to_maxcut_bipartition_sparse(path, G_m.data, G_m.indptr, G_m.indices)
178
+
179
+ if not is_optimized:
180
+ bitstring = get_bitstring(partition, nodes)
181
+ theta_bits = [(b == "1") for b in list(bitstring)]
182
+ energy = 0
183
+ for u in range(n_qubits):
184
+ for v in range(u + 1, n_qubits):
185
+ val = G_m[nodes[u], nodes[v]]
186
+ energy += (val if theta_bits[u] == theta_bits[v] else -val)
187
+
188
+ return bitstring, cut_value, partition, energy
189
+
190
+ bitint = 0
191
+ for b in partition[0]:
192
+ bitint |= 1 << b
193
+
194
+ bitstring, cut_value, cut, energy = spin_glass_solver_sparse(G_m, best_guess=bitint, **kwargs)
195
+ l, r = get_cut(bitstring, nodes)
196
+
197
+ return bitstring, cut_value, (l, r), energy
198
+
199
+
200
+ def tsp_to_maxcut_bipartition_streaming(tsp_path, G_func):
201
+ n = len(tsp_path)
202
+ best_cut_value = -float('inf')
203
+ best_partition = None
204
+ direction = 0
205
+
206
+ for offset in [-1, 0, 1]:
207
+ mid = n // 2 + offset
208
+ A = tsp_path[:mid]
209
+ B = tsp_path[mid:]
210
+ cut_value = sum(
211
+ G_func(u, v)
212
+ for u in A
213
+ for v in B
214
+ )
215
+ if cut_value > best_cut_value:
216
+ best_cut_value = cut_value
217
+ best_partition = (A, B)
218
+ direction = offset
219
+
220
+ if direction == 0:
221
+ return best_partition, best_cut_value
222
+
223
+ improved = True
224
+ best_offset = direction
225
+ while improved:
226
+ improved = False
227
+ offset = best_offset + direction
228
+ mid = n // 2 + offset
229
+ A = tsp_path[:mid]
230
+ B = tsp_path[mid:]
231
+ cut_value = sum(
232
+ G_func(u, v)
233
+ for u in A
234
+ for v in B
235
+ )
236
+ if cut_value > best_cut_value:
237
+ best_cut_value = cut_value
238
+ best_partition = (A, B)
239
+ best_offset = offset
240
+ improved = True
241
+
242
+ return best_partition, best_cut_value
243
+
244
+
245
+ def tsp_maxcut_streaming(G_func, nodes, k_neighbors=20, is_optimized=False, **kwargs):
246
+ n_qubits = len(nodes)
247
+ path, length = tsp_symmetric_streaming(G_func, nodes, k_neighbors=k_neighbors)
248
+ partition, cut_value = tsp_to_maxcut_bipartition_streaming(path, G_func)
249
+
250
+ if not is_optimized:
251
+ bitstring = get_bitstring(partition, nodes)
252
+ theta_bits = [(b == "1") for b in list(bitstring)]
253
+ energy = 0
254
+ for u in range(n_qubits):
255
+ for v in range(u + 1, n_qubits):
256
+ val = G_func(nodes[u], nodes[v])
257
+ energy += (val if theta_bits[u] == theta_bits[v] else -val)
258
+
259
+ return bitstring, cut_value, partition, energy
260
+
261
+ bitint = 0
262
+ for b in partition[0]:
263
+ bitint |= 1 << b
264
+
265
+ bitstring, cut_value, cut, energy = spin_glass_solver_streaming(G_func, nodes, best_guess=bitint, **kwargs)
266
+ l, r = get_cut(bitstring, nodes)
267
+
268
+ return bitstring, cut_value, (l, r), energy
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 7.1.0
3
+ Version: 7.3.0
4
4
  Summary: Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)
5
5
  Home-page: https://github.com/vm6502q/PyQrackIsing
6
6
  Author: Dan Strano
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
51
51
 
52
52
  setup(
53
53
  name='pyqrackising',
54
- version='7.1.0',
54
+ version='7.3.0',
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)',
@@ -1,49 +0,0 @@
1
- from .tsp import tsp_symmetric
2
-
3
- def tsp_to_maxcut_bipartition(tsp_path, weights):
4
- n = len(tsp_path)
5
- best_cut_value = -float('inf')
6
- best_partition = None
7
- direction = 0
8
-
9
- for offset in [-1, 0, 1]:
10
- mid = n // 2 + offset
11
- A = tsp_path[:mid]
12
- B = tsp_path[mid:]
13
- cut_value = sum(
14
- weights[u, v]
15
- for u in A
16
- for v in B
17
- )
18
- if cut_value > best_cut_value:
19
- best_cut_value = cut_value
20
- best_partition = (A, B)
21
- direction = offset
22
-
23
- if direction == 0:
24
- return best_partition, best_cut_value
25
-
26
- improved = True
27
- best_offset = direction
28
- while improved:
29
- improved = False
30
- offset = best_offset + direction
31
- mid = n // 2 + offset
32
- A = tsp_path[:mid]
33
- B = tsp_path[mid:]
34
- cut_value = sum(
35
- weights[u, v]
36
- for u in A
37
- for v in B
38
- )
39
- if cut_value > best_cut_value:
40
- best_cut_value = cut_value
41
- best_partition = (A, B)
42
- best_offset = offset
43
- improved = True
44
-
45
- return best_partition, best_cut_value
46
-
47
- def tsp_maxcut(G, k_neighbors=20):
48
- path, length = tsp_symmetric(G=G, is_cyclic=False, monte_carlo=True, k_neighbors=k_neighbors)
49
- return tsp_to_maxcut_bipartition(path, G)
File without changes
File without changes
File without changes