PyQrackIsing 1.24.2__tar.gz → 1.26.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 (21) hide show
  1. {pyqrackising-1.24.2/PyQrackIsing.egg-info → pyqrackising-1.26.0}/PKG-INFO +1 -1
  2. {pyqrackising-1.24.2 → pyqrackising-1.26.0}/PyQrackIsing/maxcut_tfim.py +1 -1
  3. {pyqrackising-1.24.2 → pyqrackising-1.26.0}/PyQrackIsing/spin_glass_solver.py +1 -1
  4. {pyqrackising-1.24.2 → pyqrackising-1.26.0}/PyQrackIsing/tsp_symmetric.py +91 -34
  5. {pyqrackising-1.24.2 → pyqrackising-1.26.0/PyQrackIsing.egg-info}/PKG-INFO +1 -1
  6. {pyqrackising-1.24.2 → pyqrackising-1.26.0}/README.md +12 -1
  7. {pyqrackising-1.24.2 → pyqrackising-1.26.0}/pyproject.toml +1 -1
  8. {pyqrackising-1.24.2 → pyqrackising-1.26.0}/setup.py +1 -1
  9. {pyqrackising-1.24.2 → pyqrackising-1.26.0}/CMakeLists.txt +0 -0
  10. {pyqrackising-1.24.2 → pyqrackising-1.26.0}/LICENSE.md +0 -0
  11. {pyqrackising-1.24.2 → pyqrackising-1.26.0}/MANIFEST.in +0 -0
  12. {pyqrackising-1.24.2 → pyqrackising-1.26.0}/PyQrackIsing/__init__.py +0 -0
  13. {pyqrackising-1.24.2 → pyqrackising-1.26.0}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
  14. {pyqrackising-1.24.2 → pyqrackising-1.26.0}/PyQrackIsing/generate_tfim_samples.py +0 -0
  15. {pyqrackising-1.24.2 → pyqrackising-1.26.0}/PyQrackIsing/tfim_magnetization.py +0 -0
  16. {pyqrackising-1.24.2 → pyqrackising-1.26.0}/PyQrackIsing/tfim_square_magnetization.py +0 -0
  17. {pyqrackising-1.24.2 → pyqrackising-1.26.0}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
  18. {pyqrackising-1.24.2 → pyqrackising-1.26.0}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
  19. {pyqrackising-1.24.2 → pyqrackising-1.26.0}/PyQrackIsing.egg-info/not-zip-safe +0 -0
  20. {pyqrackising-1.24.2 → pyqrackising-1.26.0}/PyQrackIsing.egg-info/top_level.txt +0 -0
  21. {pyqrackising-1.24.2 → pyqrackising-1.26.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.24.2
3
+ Version: 1.26.0
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
@@ -365,7 +365,7 @@ def maxcut_tfim(
365
365
  return "01", weight, ([nodes[0]], [nodes[1]])
366
366
 
367
367
  if quality is None:
368
- quality = 3
368
+ quality = 8
369
369
 
370
370
  if shots is None:
371
371
  # Number of measurement shots
@@ -85,7 +85,7 @@ def spin_glass_solver(G, quality=None, shots=None, correction_quality=None, best
85
85
  if correction_quality is None:
86
86
  # maxcut_tfim(G) scales roughly like n^4,
87
87
  # so its match order of overhead.
88
- correction_quality = 4
88
+ correction_quality = 2
89
89
 
90
90
  bitstring = ""
91
91
  if isinstance(best_guess, str):
@@ -43,6 +43,29 @@ def one_way_two_opt(path, G):
43
43
  return best_path, best_dist
44
44
 
45
45
 
46
+ @njit
47
+ def anchored_two_opt(path, G):
48
+ improved = True
49
+ best_path = path
50
+ best_dist = path_length(best_path, G)
51
+ path_len = len(path)
52
+
53
+ while improved:
54
+ improved = False
55
+ for i in range(1, path_len - 1):
56
+ for j in range(i+1, path_len):
57
+ if j - i == 1: # adjacent edges, skip
58
+ continue
59
+ new_path = best_path[:]
60
+ new_path[i:j] = best_path[j-1:i-1:-1]
61
+ new_dist = path_length(new_path, G)
62
+ if new_dist < best_dist:
63
+ best_path, best_dist = new_path, new_dist
64
+ improved = True
65
+ path = best_path
66
+ return best_path, best_dist
67
+
68
+
46
69
  @njit
47
70
  def two_opt(path, G):
48
71
  improved = True
@@ -234,7 +257,7 @@ def stitch(G_m, path_a, path_b, sol_weight):
234
257
  return best_path, best_weight
235
258
 
236
259
 
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):
260
+ def tsp_symmetric(G, start_node=None, end_node=None, quality=1, shots=None, correction_quality=2, monte_carlo=False, is_3_opt=True, k_neighbors=20, is_cyclic=True, multi_start=1, is_top_level=True):
238
261
  nodes = None
239
262
  n_nodes = 0
240
263
  G_m = None
@@ -257,58 +280,92 @@ def tsp_symmetric(G, quality=1, shots=None, correction_quality=2, monte_carlo=Fa
257
280
  else:
258
281
  return ([nodes[0], nodes[1]], G_m[0, 1])
259
282
 
283
+ if (start_node is None) and not (end_node is None):
284
+ start_node = end_node
285
+ end_node = None
286
+
260
287
  a = []
261
288
  b = []
262
- best_energy = float("inf")
263
- for _ in range(multi_start):
264
- energy = 0.0
265
- _a = []
266
- _b = []
267
- while (len(_a) == 0) or (len(_b) == 0):
268
- bits = ([], [])
269
- if monte_carlo:
270
- for i in range(n_nodes):
271
- if np.random.random() < 0.5:
272
- bits[0].append(i)
273
- else:
274
- bits[1].append(i)
275
- else:
276
- _, _, bits, energy = spin_glass_solver(G_m, quality=quality, shots=shots, correction_quality=correction_quality)
277
- _a = list(bits[0])
278
- _b = list(bits[1])
279
- if energy < best_energy:
280
- best_energy = energy
281
- a, b = _a, _b
289
+ c = []
290
+ if (start_node is None) and (end_node is None):
291
+ best_energy = float("inf")
292
+ for _ in range(multi_start):
293
+ energy = 0.0
294
+ _a = []
295
+ _b = []
296
+ while (len(_a) == 0) or (len(_b) == 0):
297
+ bits = ([], [])
298
+ if monte_carlo:
299
+ for i in range(n_nodes):
300
+ if np.random.random() < 0.5:
301
+ bits[0].append(i)
302
+ else:
303
+ bits[1].append(i)
304
+ else:
305
+ _, _, bits, energy = spin_glass_solver(G_m, quality=quality, shots=shots, correction_quality=correction_quality)
306
+ _a = list(bits[0])
307
+ _b = list(bits[1])
308
+ if energy < best_energy:
309
+ best_energy = energy
310
+ a, b = _a, _b
311
+ else:
312
+ is_cyclic = False
313
+ a.append(nodes.index(start_node))
314
+ b = list(range(n_nodes))
315
+ b.remove(a[0])
316
+ if end_node is not None:
317
+ c.append(nodes.index(end_node))
318
+ b.remove(c[0])
282
319
 
283
320
  G_a, G_b = init_G_a_b(G_m, a, b)
284
321
 
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)
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)
322
+ sol_a = tsp_symmetric(G_a, quality=quality, correction_quality=correction_quality, monte_carlo=monte_carlo, is_cyclic=False, is_top_level=False, is_3_opt=False, multi_start=multi_start)
323
+ sol_b = tsp_symmetric(G_b, quality=quality, correction_quality=correction_quality, monte_carlo=monte_carlo, is_cyclic=False, is_top_level=False, is_3_opt=False, multi_start=multi_start)
287
324
 
288
325
  path_a = [a[x] for x in sol_a[0]]
289
326
  path_b = [b[x] for x in sol_b[0]]
290
327
 
291
328
  sol_weight = sol_a[1] + sol_b[1]
292
329
 
330
+ if len(c):
331
+ sol_weight += G_m[b[-1], c[0]]
332
+
293
333
  if (len(path_a) == 1) and (len(path_b) == 1):
294
- return (path_a + path_b, sol_weight + G_m[path_a[0], path_b[0]])
334
+ if len(c):
335
+ return (path_a + path_b + c, (sol_weight + G_m[path_b[0], c[0]] + G_m[path_a[0], c[0]]) if is_cyclic else (sol_weight + G_m[path_b[0], c[0]]))
336
+ return (path_a + path_b, (sol_weight + G_m[path_a[0], path_b[0]]) if is_cyclic else sol_weight)
295
337
 
296
- best_path, best_weight = stitch(G_m, path_a, path_b, sol_weight)
338
+ if start_node is None:
339
+ best_path, best_weight = stitch(G_m, path_a, path_b, sol_weight)
340
+ else:
341
+ best_path = path_a + path_b
342
+ best_weight = G_m[path_a[0], path_b[0]]
343
+ weight = G_m[path_a[0], path_b[-1]]
344
+ if weight < best_weight:
345
+ path_b.reverse()
346
+ best_path = path_a + path_b
347
+ best_weight = weight
348
+ best_weight += sol_weight
297
349
 
298
- if is_2_opt or is_3_opt:
299
- cycle_node = best_path[0]
300
- best_path += [cycle_node]
350
+ if len(c):
351
+ best_path += c
352
+ best_weight += G_m[best_path[-1], c[0]]
301
353
 
302
- if is_2_opt:
354
+ if is_top_level:
355
+ if is_cyclic:
356
+ best_path += [best_path[0]]
357
+ best_path, best_weight = two_opt(best_path, G_m)
358
+ elif not end_node is None:
303
359
  best_path, best_weight = two_opt(best_path, G_m)
360
+ elif not start_node is None:
361
+ best_path.reverse()
362
+ best_path, best_weight = anchored_two_opt(best_path, G_m)
363
+ best_path.reverse()
364
+ else:
365
+ best_path, best_weight = one_way_two_opt(best_path, G_m)
304
366
 
305
367
  if is_3_opt:
306
368
  best_path, best_weight = targeted_three_opt(best_path, G_m, k_neighbors)
307
-
308
- if not is_cyclic:
309
- cycle_index = best_path.index(cycle_node)
310
- best_weight -= G_m[best_path[cycle_index], best_path[cycle_index + 1]]
311
- best_path = best_path[cycle_index + 1:] + best_path[:cycle_index]
312
369
  else:
313
370
  best_path, best_weight = one_way_two_opt(best_path, G_m)
314
371
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.24.2
3
+ Version: 1.26.0
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
@@ -93,7 +93,18 @@ def generate_tsp_graph(n_nodes=64, seed=None):
93
93
 
94
94
  n_nodes = 128
95
95
  G = generate_tsp_graph(n_nodes=n_nodes, seed=42)
96
- circuit, path_length = tsp_symmetric(G, is_monte_carlo=False, quality=1, correction_quality=2, is_cyclic=True, multi_start=1, is_2_opt=True, is_3_opt=True, k_neighbors=20)
96
+ circuit, path_length = tsp_symmetric(
97
+ G,
98
+ start_node=None,
99
+ end_node=None,
100
+ is_monte_carlo=False,
101
+ quality=1,
102
+ correction_quality=2,
103
+ is_cyclic=True,
104
+ multi_start=1,
105
+ is_3_opt=True,
106
+ k_neighbors=20
107
+ )
97
108
 
98
109
  print(f"Node count: {n_nodes}")
99
110
  print(f"Path: {circuit}")
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
12
12
 
13
13
  [project]
14
14
  name = "PyQrackIsing"
15
- version = "1.24.2"
15
+ version = "1.26.0"
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.2',
54
+ version='1.26.0',
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