PyQrackIsing 1.24.0__tar.gz → 1.24.2__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 (21) hide show
  1. {pyqrackising-1.24.0/PyQrackIsing.egg-info → pyqrackising-1.24.2}/PKG-INFO +1 -1
  2. {pyqrackising-1.24.0 → pyqrackising-1.24.2}/PyQrackIsing/maxcut_tfim.py +1 -1
  3. {pyqrackising-1.24.0 → pyqrackising-1.24.2}/PyQrackIsing/tsp_symmetric.py +117 -77
  4. {pyqrackising-1.24.0 → pyqrackising-1.24.2/PyQrackIsing.egg-info}/PKG-INFO +1 -1
  5. {pyqrackising-1.24.0 → pyqrackising-1.24.2}/pyproject.toml +1 -1
  6. {pyqrackising-1.24.0 → pyqrackising-1.24.2}/setup.py +1 -1
  7. {pyqrackising-1.24.0 → pyqrackising-1.24.2}/CMakeLists.txt +0 -0
  8. {pyqrackising-1.24.0 → pyqrackising-1.24.2}/LICENSE.md +0 -0
  9. {pyqrackising-1.24.0 → pyqrackising-1.24.2}/MANIFEST.in +0 -0
  10. {pyqrackising-1.24.0 → pyqrackising-1.24.2}/PyQrackIsing/__init__.py +0 -0
  11. {pyqrackising-1.24.0 → pyqrackising-1.24.2}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
  12. {pyqrackising-1.24.0 → pyqrackising-1.24.2}/PyQrackIsing/generate_tfim_samples.py +0 -0
  13. {pyqrackising-1.24.0 → pyqrackising-1.24.2}/PyQrackIsing/spin_glass_solver.py +0 -0
  14. {pyqrackising-1.24.0 → pyqrackising-1.24.2}/PyQrackIsing/tfim_magnetization.py +0 -0
  15. {pyqrackising-1.24.0 → pyqrackising-1.24.2}/PyQrackIsing/tfim_square_magnetization.py +0 -0
  16. {pyqrackising-1.24.0 → pyqrackising-1.24.2}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
  17. {pyqrackising-1.24.0 → pyqrackising-1.24.2}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
  18. {pyqrackising-1.24.0 → pyqrackising-1.24.2}/PyQrackIsing.egg-info/not-zip-safe +0 -0
  19. {pyqrackising-1.24.0 → pyqrackising-1.24.2}/PyQrackIsing.egg-info/top_level.txt +0 -0
  20. {pyqrackising-1.24.0 → pyqrackising-1.24.2}/README.md +0 -0
  21. {pyqrackising-1.24.0 → pyqrackising-1.24.2}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.24.0
3
+ Version: 1.24.2
4
4
  Summary: Near-ideal closed-form solutions for transverse field Ising model (TFIM)
5
5
  Home-page: https://github.com/vm6502q/PyQrackIsing
6
6
  Author: Dan Strano
@@ -107,7 +107,7 @@ def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, hamming_prob):
107
107
  n_bias = n_qubits - 1
108
108
 
109
109
  theta = np.empty(n_qubits, dtype=np.float64)
110
- for q in prange(n_qubits):
110
+ for q in range(n_qubits):
111
111
  J = J_func[q]
112
112
  z = degrees[q]
113
113
  theta[q] = np.arcsin(
@@ -20,12 +20,35 @@ def path_length(path, G_m):
20
20
  return tot_len
21
21
 
22
22
 
23
+ @njit
24
+ def one_way_two_opt(path, G):
25
+ improved = True
26
+ best_path = path
27
+ best_dist = path_length(best_path, G)
28
+ path_len = len(path)
29
+
30
+ while improved:
31
+ improved = False
32
+ for i in range(1, path_len - 1):
33
+ for j in range(i+1, path_len + 1):
34
+ if j - i == 1: # adjacent edges, skip
35
+ continue
36
+ new_path = best_path[:]
37
+ new_path[i:j] = best_path[j-1:i-1:-1]
38
+ new_dist = path_length(new_path, G)
39
+ if new_dist < best_dist:
40
+ best_path, best_dist = new_path, new_dist
41
+ improved = True
42
+ path = best_path
43
+ return best_path, best_dist
44
+
45
+
23
46
  @njit
24
47
  def two_opt(path, G):
25
48
  improved = True
26
49
  best_path = path
27
50
  best_dist = path_length(best_path, G)
28
-
51
+
29
52
  while improved:
30
53
  improved = False
31
54
  for i in range(1, len(path) - 2):
@@ -129,6 +152,88 @@ def targeted_three_opt(path, W, k_neighbors=20):
129
152
  return best_path, best_dist
130
153
 
131
154
 
155
+ @njit
156
+ def init_G_a_b(G_m, a, b):
157
+ n_a_nodes = len(a)
158
+ n_b_nodes = len(b)
159
+ G_a = np.zeros((n_a_nodes, n_a_nodes), dtype=np.float64)
160
+ G_b = np.zeros((n_b_nodes, n_b_nodes), dtype=np.float64)
161
+ for i in range(n_a_nodes):
162
+ for j in range(n_a_nodes):
163
+ if i == j:
164
+ continue
165
+ G_a[i, j] = G_m[a[i], a[j]]
166
+ for i in range(n_b_nodes):
167
+ for j in range(n_b_nodes):
168
+ if i == j:
169
+ continue
170
+ G_b[i, j] = G_m[b[i], b[j]]
171
+
172
+ return G_a, G_b
173
+
174
+
175
+ @njit
176
+ def stitch(G_m, path_a, path_b, sol_weight):
177
+ is_single_a = len(path_a) == 1
178
+ is_single_b = len(path_b) == 1
179
+ best_path = [0]
180
+ best_weight = 0.0
181
+ if is_single_a or is_single_b:
182
+ singlet = 0
183
+ bulk = [0]
184
+ if is_single_a:
185
+ singlet = path_a[0]
186
+ bulk = path_b
187
+ elif is_single_b:
188
+ singlet = path_b[0]
189
+ bulk = path_a
190
+
191
+ best_weight = G_m[singlet, bulk[0]]
192
+ best_path = [singlet] + bulk
193
+ weight = G_m[singlet, bulk[-1]]
194
+ if weight < best_weight:
195
+ best_weight = weight
196
+ best_path = bulk + [singlet]
197
+ for i in range(1, len(bulk)):
198
+ weight = (
199
+ G_m[singlet, bulk[i - 1]] +
200
+ G_m[singlet, bulk[i]] -
201
+ G_m[bulk[i - 1], bulk[i]]
202
+ )
203
+ if weight < best_weight:
204
+ best_weight = weight
205
+ best_path = bulk.copy()
206
+ best_path.insert(i, singlet)
207
+ else:
208
+ terminals_a = [path_a[0], path_a[-1]]
209
+ terminals_b = [path_b[0], path_b[-1]]
210
+
211
+ best_weight = G_m[terminals_a[1], terminals_b[0]]
212
+ best_path = path_a + path_b
213
+ weight = G_m[terminals_a[0], terminals_b[1]]
214
+ if weight < best_weight:
215
+ best_weight = weight
216
+ best_path = path_b + path_a
217
+ for _ in range(2):
218
+ for _ in range(2):
219
+ for i in range(1, len(path_b)):
220
+ weight = (
221
+ G_m[terminals_a[0], path_b[i - 1]] +
222
+ G_m[terminals_a[1], path_b[i]] -
223
+ G_m[path_b[i - 1], path_b[i]]
224
+ )
225
+ if weight < best_weight:
226
+ best_weight = weight
227
+ best_path = path_b.copy()
228
+ best_path[i:i] = path_a
229
+ path_a.reverse()
230
+ terminals_a.reverse()
231
+ path_a, path_b = path_b, path_a
232
+ terminals_a, terminals_b = terminals_b, terminals_a
233
+
234
+ return best_path, best_weight
235
+
236
+
132
237
  def tsp_symmetric(G, quality=1, shots=None, correction_quality=2, monte_carlo=False, is_2_opt=True, is_3_opt=True, k_neighbors=20, is_cyclic=True, multi_start=1):
133
238
  nodes = None
134
239
  n_nodes = 0
@@ -160,9 +265,8 @@ def tsp_symmetric(G, quality=1, shots=None, correction_quality=2, monte_carlo=Fa
160
265
  _a = []
161
266
  _b = []
162
267
  while (len(_a) == 0) or (len(_b) == 0):
163
- bits = None
268
+ bits = ([], [])
164
269
  if monte_carlo:
165
- bits = ([], [])
166
270
  for i in range(n_nodes):
167
271
  if np.random.random() < 0.5:
168
272
  bits[0].append(i)
@@ -176,20 +280,7 @@ def tsp_symmetric(G, quality=1, shots=None, correction_quality=2, monte_carlo=Fa
176
280
  best_energy = energy
177
281
  a, b = _a, _b
178
282
 
179
- n_a_nodes = len(a)
180
- n_b_nodes = len(b)
181
- G_a = np.zeros((n_a_nodes, n_a_nodes), dtype=np.float64)
182
- G_b = np.zeros((n_b_nodes, n_b_nodes), dtype=np.float64)
183
- for i in range(n_a_nodes):
184
- for j in range(n_a_nodes):
185
- if i == j:
186
- continue
187
- G_a[i, j] = G_m[a[i], a[j]]
188
- for i in range(n_b_nodes):
189
- for j in range(n_b_nodes):
190
- if i == j:
191
- continue
192
- G_b[i, j] = G_m[b[i], b[j]]
283
+ G_a, G_b = init_G_a_b(G_m, a, b)
193
284
 
194
285
  sol_a = tsp_symmetric(G_a, quality=quality, correction_quality=correction_quality, monte_carlo=monte_carlo, is_cyclic=False, is_2_opt=False, is_3_opt=False, multi_start=multi_start)
195
286
  sol_b = tsp_symmetric(G_b, quality=quality, correction_quality=correction_quality, monte_carlo=monte_carlo, is_cyclic=False, is_2_opt=False, is_3_opt=False, multi_start=multi_start)
@@ -199,65 +290,10 @@ def tsp_symmetric(G, quality=1, shots=None, correction_quality=2, monte_carlo=Fa
199
290
 
200
291
  sol_weight = sol_a[1] + sol_b[1]
201
292
 
202
- single = None
203
- is_single_a = len(path_a) == 1
204
- is_single_b = len(path_b) == 1
205
-
206
- if is_single_a and is_single_b:
293
+ if (len(path_a) == 1) and (len(path_b) == 1):
207
294
  return (path_a + path_b, sol_weight + G_m[path_a[0], path_b[0]])
208
295
 
209
- singlet = None
210
- bulk = None
211
- if is_single_a:
212
- singlet = path_a[0]
213
- bulk = path_b
214
- elif is_single_b:
215
- singlet = path_b[0]
216
- bulk = path_a
217
-
218
- if not singlet is None:
219
- best_weight = G_m[singlet, bulk[0]]
220
- best_path = [singlet] + bulk
221
- weight = G_m[singlet, bulk[-1]]
222
- if weight < best_weight:
223
- best_weight = weight
224
- best_path = bulk + [singlet]
225
- for i in range(len(bulk) - 1):
226
- weight = (
227
- G_m[singlet, bulk[i]] +
228
- G_m[singlet, bulk[i + 1]] -
229
- G_m[bulk[i], bulk[i + 1]]
230
- )
231
- if weight < best_weight:
232
- best_weight = weight
233
- best_path = bulk.copy()
234
- best_path.insert(i + 1, singlet)
235
- else:
236
- terminals_a = [path_a[0], path_a[-1]]
237
- terminals_b = [path_b[0], path_b[-1]]
238
-
239
- for _ in range(2):
240
- for _ in range(2):
241
- best_weight = G_m[terminals_a[1], terminals_b[0]]
242
- best_path = path_a + path_b
243
- weight = G_m[terminals_b[1], terminals_a[0]]
244
- if weight < best_weight:
245
- best_weight = weight
246
- best_path = path_b + path_a
247
- for i in range(len(path_b) - 1):
248
- weight = (
249
- G_m[terminals_a[0], path_b[i]] +
250
- G_m[terminals_a[1], path_b[i + 1]] -
251
- G_m[path_b[i], path_b[i + 1]]
252
- )
253
- if weight < best_weight:
254
- best_weight = weight
255
- best_path = path_b.copy()
256
- best_path[i + 1:i + 1] = path_a
257
- path_a.reverse()
258
- terminals_a.reverse()
259
- path_a, path_b = path_b, path_a
260
- terminals_a, terminals_b = terminals_b, terminals_a
296
+ best_path, best_weight = stitch(G_m, path_a, path_b, sol_weight)
261
297
 
262
298
  if is_2_opt or is_3_opt:
263
299
  cycle_node = best_path[0]
@@ -273,8 +309,12 @@ def tsp_symmetric(G, quality=1, shots=None, correction_quality=2, monte_carlo=Fa
273
309
  cycle_index = best_path.index(cycle_node)
274
310
  best_weight -= G_m[best_path[cycle_index], best_path[cycle_index + 1]]
275
311
  best_path = best_path[cycle_index + 1:] + best_path[:cycle_index]
276
- elif is_cyclic:
277
- cycle_node = best_path[0]
278
- best_path += [cycle_node]
312
+ else:
313
+ best_path, best_weight = one_way_two_opt(best_path, G_m)
314
+
315
+ if is_cyclic:
316
+ cycle_node = best_path[0]
317
+ best_weight += G_m[cycle_node, best_path[-1]]
318
+ best_path += [cycle_node]
279
319
 
280
320
  return [nodes[x] for x in best_path], best_weight
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.24.0
3
+ Version: 1.24.2
4
4
  Summary: Near-ideal closed-form solutions for transverse field Ising model (TFIM)
5
5
  Home-page: https://github.com/vm6502q/PyQrackIsing
6
6
  Author: Dan Strano
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
12
12
 
13
13
  [project]
14
14
  name = "PyQrackIsing"
15
- version = "1.24.0"
15
+ version = "1.24.2"
16
16
  requires-python = ">=3.8"
17
17
  description = "Near-ideal closed-form solutions for transverse field Ising model (TFIM)"
18
18
  readme = {file = "README.txt", content-type = "text/markdown"}
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
51
51
 
52
52
  setup(
53
53
  name='PyQrackIsing',
54
- version='1.24.0',
54
+ version='1.24.2',
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)',
File without changes
File without changes
File without changes
File without changes