PyQrackIsing 7.2.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 (30) hide show
  1. {pyqrackising-7.2.0/pyqrackising.egg-info → pyqrackising-7.3.0}/PKG-INFO +1 -1
  2. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/README.md +2 -2
  3. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/pyproject.toml +1 -1
  4. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/pyqrackising/tsp.py +20 -2
  5. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/pyqrackising/tsp_maxcut.py +115 -15
  6. {pyqrackising-7.2.0 → pyqrackising-7.3.0/pyqrackising.egg-info}/PKG-INFO +1 -1
  7. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/setup.py +1 -1
  8. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/CMakeLists.txt +0 -0
  9. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/LICENSE.md +0 -0
  10. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/MANIFEST.in +0 -0
  11. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/pyqrackising/__init__.py +0 -0
  12. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/pyqrackising/_pyqrack_ising.cpp +0 -0
  13. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
  14. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/pyqrackising/generate_tfim_samples.py +0 -0
  15. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/pyqrackising/kernels.cl +0 -0
  16. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/pyqrackising/maxcut_tfim.py +0 -0
  17. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/pyqrackising/maxcut_tfim_sparse.py +0 -0
  18. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/pyqrackising/maxcut_tfim_streaming.py +0 -0
  19. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/pyqrackising/maxcut_tfim_util.py +0 -0
  20. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/pyqrackising/spin_glass_solver.py +0 -0
  21. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/pyqrackising/spin_glass_solver_sparse.py +0 -0
  22. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/pyqrackising/spin_glass_solver_streaming.py +0 -0
  23. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/pyqrackising/spin_glass_solver_util.py +0 -0
  24. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/pyqrackising/tfim_magnetization.py +0 -0
  25. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/pyqrackising/tfim_square_magnetization.py +0 -0
  26. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/pyqrackising.egg-info/SOURCES.txt +0 -0
  27. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
  28. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/pyqrackising.egg-info/not-zip-safe +0 -0
  29. {pyqrackising-7.2.0 → pyqrackising-7.3.0}/pyqrackising.egg-info/top_level.txt +0 -0
  30. {pyqrackising-7.2.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.2.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
@@ -146,10 +146,10 @@ 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_sparse(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.2.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"}
@@ -814,7 +814,16 @@ def get_G_m(G_data, G_rows, G_cols, low, high):
814
814
  def path_length_sparse(path, G_data, G_rows, G_cols):
815
815
  tot_len = 0.0
816
816
  for i in range(len(path) - 1):
817
- tot_len += get_G_m(G_data, G_rows, G_cols, path[i], path[i + 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]
818
827
 
819
828
  return tot_len
820
829
 
@@ -1030,7 +1039,16 @@ def tsp_sparse_bruteforce(G_data, G_rows, G_cols, perms):
1030
1039
  for path in perms:
1031
1040
  weight = 0.0
1032
1041
  for i in range(max_i):
1033
- weight += get_G_m(G_data, G_rows, G_cols, path[i], path[i+1])
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]
1034
1052
 
1035
1053
  if weight < best_weight:
1036
1054
  best_weight = weight
@@ -1,11 +1,13 @@
1
- from .maxcut_tfim_util import to_scipy_sparse_upper_triangular
1
+ from .maxcut_tfim_util import binary_search, to_scipy_sparse_upper_triangular
2
2
  from .tsp import tsp_symmetric, tsp_symmetric_sparse, tsp_symmetric_streaming
3
3
  from .spin_glass_solver import spin_glass_solver
4
4
  from .spin_glass_solver_sparse import spin_glass_solver_sparse
5
5
  from .spin_glass_solver_streaming import spin_glass_solver_streaming
6
6
  import networkx as nx
7
+ from numba import njit
7
8
 
8
9
 
10
+ @njit
9
11
  def tsp_to_maxcut_bipartition(tsp_path, weights):
10
12
  n = len(tsp_path)
11
13
  best_cut_value = -float('inf')
@@ -16,11 +18,10 @@ def tsp_to_maxcut_bipartition(tsp_path, weights):
16
18
  mid = n // 2 + offset
17
19
  A = tsp_path[:mid]
18
20
  B = tsp_path[mid:]
19
- cut_value = sum(
20
- weights[u, v]
21
- for u in A
22
- for v in B
23
- )
21
+ cut_value = 0.0
22
+ for u in A:
23
+ for v in B:
24
+ cut_value += weights[u, v]
24
25
  if cut_value > best_cut_value:
25
26
  best_cut_value = cut_value
26
27
  best_partition = (A, B)
@@ -37,11 +38,10 @@ def tsp_to_maxcut_bipartition(tsp_path, weights):
37
38
  mid = n // 2 + offset
38
39
  A = tsp_path[:mid]
39
40
  B = tsp_path[mid:]
40
- cut_value = sum(
41
- weights[u, v]
42
- for u in A
43
- for v in B
44
- )
41
+ cut_value = 0.0
42
+ for u in A:
43
+ for v in B:
44
+ cut_value += weights[u, v]
45
45
  if cut_value > best_cut_value:
46
46
  best_cut_value = cut_value
47
47
  best_partition = (A, B)
@@ -50,6 +50,8 @@ def tsp_to_maxcut_bipartition(tsp_path, weights):
50
50
 
51
51
  return best_partition, best_cut_value
52
52
 
53
+
54
+ @njit
53
55
  def get_cut(bitstring, nodes):
54
56
  solution = list(bitstring)
55
57
  l, r = [], []
@@ -61,7 +63,17 @@ def get_cut(bitstring, nodes):
61
63
 
62
64
  return l, r
63
65
 
64
- def tsp_maxcut(G, k_neighbors=20, **kwargs):
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):
65
77
  G_m = None
66
78
  if isinstance(G, nx.Graph):
67
79
  nodes = list(G.nodes())
@@ -75,6 +87,17 @@ def tsp_maxcut(G, k_neighbors=20, **kwargs):
75
87
  path, length = tsp_symmetric(G, is_cyclic=False, monte_carlo=True, k_neighbors=k_neighbors)
76
88
  partition, cut_value = tsp_to_maxcut_bipartition(path, G_m)
77
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
+
78
101
  bitint = 0
79
102
  for b in partition[0]:
80
103
  bitint |= 1 << b
@@ -85,7 +108,61 @@ def tsp_maxcut(G, k_neighbors=20, **kwargs):
85
108
  return bitstring, cut_value, (l, r), energy
86
109
 
87
110
 
88
- def tsp_maxcut_sparse(G, k_neighbors=20, **kwargs):
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):
89
166
  G_m = None
90
167
  if isinstance(G, nx.Graph):
91
168
  nodes = list(G.nodes())
@@ -97,7 +174,18 @@ def tsp_maxcut_sparse(G, k_neighbors=20, **kwargs):
97
174
  G_m = G
98
175
 
99
176
  path, length = tsp_symmetric_sparse(G, k_neighbors=k_neighbors)
100
- partition, cut_value = tsp_to_maxcut_bipartition(path, G_m)
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
101
189
 
102
190
  bitint = 0
103
191
  for b in partition[0]:
@@ -154,10 +242,22 @@ def tsp_to_maxcut_bipartition_streaming(tsp_path, G_func):
154
242
  return best_partition, best_cut_value
155
243
 
156
244
 
157
- def tsp_maxcut_streaming(G_func, nodes, k_neighbors=20, **kwargs):
245
+ def tsp_maxcut_streaming(G_func, nodes, k_neighbors=20, is_optimized=False, **kwargs):
246
+ n_qubits = len(nodes)
158
247
  path, length = tsp_symmetric_streaming(G_func, nodes, k_neighbors=k_neighbors)
159
248
  partition, cut_value = tsp_to_maxcut_bipartition_streaming(path, G_func)
160
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
+
161
261
  bitint = 0
162
262
  for b in partition[0]:
163
263
  bitint |= 1 << b
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 7.2.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.2.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)',
File without changes
File without changes
File without changes