PyQrackIsing 3.0.1__tar.gz → 3.0.3__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 (22) hide show
  1. {pyqrackising-3.0.1/pyqrackising.egg-info → pyqrackising-3.0.3}/PKG-INFO +1 -1
  2. {pyqrackising-3.0.1 → pyqrackising-3.0.3}/pyproject.toml +1 -1
  3. {pyqrackising-3.0.1 → pyqrackising-3.0.3}/pyqrackising/tsp_symmetric.py +85 -9
  4. {pyqrackising-3.0.1 → pyqrackising-3.0.3/pyqrackising.egg-info}/PKG-INFO +1 -1
  5. {pyqrackising-3.0.1 → pyqrackising-3.0.3}/setup.py +1 -1
  6. {pyqrackising-3.0.1 → pyqrackising-3.0.3}/CMakeLists.txt +0 -0
  7. {pyqrackising-3.0.1 → pyqrackising-3.0.3}/LICENSE.md +0 -0
  8. {pyqrackising-3.0.1 → pyqrackising-3.0.3}/MANIFEST.in +0 -0
  9. {pyqrackising-3.0.1 → pyqrackising-3.0.3}/README.md +0 -0
  10. {pyqrackising-3.0.1 → pyqrackising-3.0.3}/pyqrackising/__init__.py +0 -0
  11. {pyqrackising-3.0.1 → pyqrackising-3.0.3}/pyqrackising/_pyqrack_ising.cpp +0 -0
  12. {pyqrackising-3.0.1 → pyqrackising-3.0.3}/pyqrackising/generate_tfim_samples.py +0 -0
  13. {pyqrackising-3.0.1 → pyqrackising-3.0.3}/pyqrackising/kernels.cl +0 -0
  14. {pyqrackising-3.0.1 → pyqrackising-3.0.3}/pyqrackising/maxcut_tfim.py +0 -0
  15. {pyqrackising-3.0.1 → pyqrackising-3.0.3}/pyqrackising/spin_glass_solver.py +0 -0
  16. {pyqrackising-3.0.1 → pyqrackising-3.0.3}/pyqrackising/tfim_magnetization.py +0 -0
  17. {pyqrackising-3.0.1 → pyqrackising-3.0.3}/pyqrackising/tfim_square_magnetization.py +0 -0
  18. {pyqrackising-3.0.1 → pyqrackising-3.0.3}/pyqrackising.egg-info/SOURCES.txt +0 -0
  19. {pyqrackising-3.0.1 → pyqrackising-3.0.3}/pyqrackising.egg-info/dependency_links.txt +0 -0
  20. {pyqrackising-3.0.1 → pyqrackising-3.0.3}/pyqrackising.egg-info/not-zip-safe +0 -0
  21. {pyqrackising-3.0.1 → pyqrackising-3.0.3}/pyqrackising.egg-info/top_level.txt +0 -0
  22. {pyqrackising-3.0.1 → pyqrackising-3.0.3}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 3.0.1
3
+ Version: 3.0.3
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
@@ -11,7 +11,7 @@ build-backend = "setuptools.build_meta"
11
11
 
12
12
  [project]
13
13
  name = "pyqrackising"
14
- version = "3.0.1"
14
+ version = "3.0.3"
15
15
  requires-python = ">=3.8"
16
16
  description = "Near-ideal closed-form solutions for transverse field Ising model (TFIM)"
17
17
  readme = {file = "README.txt", content-type = "text/markdown"}
@@ -1,4 +1,5 @@
1
1
  from .spin_glass_solver import spin_glass_solver
2
+ import itertools
2
3
  import networkx as nx
3
4
  from numba import njit
4
5
  import numpy as np
@@ -266,6 +267,53 @@ def monte_carlo_loop(n_nodes):
266
267
  return bits
267
268
 
268
269
 
270
+ # Elara suggested replacing base-case handling with her brute-force solver
271
+ @njit
272
+ def tsp_bruteforce(G_m, nodes, perms, is_cyclic):
273
+ """
274
+ Brute-force TSP solver for small n.
275
+ G_m : numpy.ndarray (2D adjacency/weight matrix)
276
+ is_cyclic : bool (default=True) – whether to close the tour
277
+
278
+ Returns:
279
+ (best_path, best_weight)
280
+ """
281
+ n = len(G_m)
282
+ best_weight = float('inf')
283
+ best_path = None
284
+
285
+ # Must fix node 0 at start to remove rotational symmetry in cyclic case!
286
+
287
+ max_i = len(perms[0]) - 1
288
+
289
+ if is_cyclic:
290
+ for perm in perms:
291
+ path = [0] + list(perm)
292
+ weight = 0.0
293
+ for i in range(max_i):
294
+ weight += G_m[path[i], path[i+1]]
295
+ weight += G_m[path[-1], path[0]]
296
+
297
+ if weight < best_weight:
298
+ best_weight = weight
299
+ best_path = path
300
+
301
+ best_path = best_path + [best_path[0]]
302
+ else:
303
+ for path in perms:
304
+ weight = 0.0
305
+ for i in range(max_i):
306
+ weight += G_m[path[i], path[i+1]]
307
+
308
+ if weight < best_weight:
309
+ best_weight = weight
310
+ best_path = path
311
+
312
+ best_path = list(best_path)
313
+
314
+ return best_path, best_weight
315
+
316
+
269
317
  def tsp_symmetric(G, start_node=None, end_node=None, quality=1, shots=None, correction_quality=2, monte_carlo=False, k_neighbors=20, is_cyclic=True, multi_start=1, is_top_level=True):
270
318
  nodes = None
271
319
  n_nodes = 0
@@ -279,15 +327,43 @@ def tsp_symmetric(G, start_node=None, end_node=None, quality=1, shots=None, corr
279
327
  nodes = list(range(n_nodes))
280
328
  G_m = G
281
329
 
282
- if n_nodes == 0:
283
- return ([], 0)
284
- if n_nodes == 1:
285
- return ([nodes[0]], 0)
286
- if n_nodes == 2:
287
- if is_cyclic:
288
- return ([nodes[0], nodes[1], nodes[0]], 2 * G_m[0, 1])
289
- else:
290
- return ([nodes[0], nodes[1]], G_m[0, 1])
330
+ if n_nodes < 5:
331
+ if n_nodes == 4:
332
+ perms = list(itertools.permutations(nodes[1:]) if is_cyclic else itertools.permutations(nodes))
333
+ return tsp_bruteforce(G_m, nodes, perms, is_cyclic)
334
+
335
+ if n_nodes == 3:
336
+ if is_cyclic:
337
+ weight_0 = G_m[0, 1] + G_m[1, 2] + G_m[2, 0]
338
+ weight_1 = G_m[0, 2] + G_m[2, 1] + G_m[1, 0]
339
+
340
+ if weight_0 >= weight_1:
341
+ return (nodes + [nodes[0]], weight_0)
342
+
343
+ nodes.reverse()
344
+
345
+ return ([nodes[2]] + nodes, weight_1)
346
+
347
+ w_012 = G_m[0, 1] + G_m[1, 2]
348
+ w_021 = G_m[0, 1] + G_m[0, 2]
349
+ w_120 = G_m[0, 2] + G_m[1, 2]
350
+
351
+ if w_012 >= w_021 and w_012 >= w_120:
352
+ return (nodes, w_012)
353
+
354
+ if w_021 >= w_012 and w_021 >= w_120:
355
+ return ([nodes[1], nodes[0], nodes[2]], w_021)
356
+
357
+ return ([nodes[0], nodes[2], nodes[1]], w_120)
358
+
359
+ if n_nodes == 2:
360
+ if is_cyclic:
361
+ return (nodes + [nodes[0]], 2 * G_m[0, 1])
362
+
363
+ return (nodes, G_m[0, 1])
364
+
365
+ return (nodes, 0)
366
+
291
367
 
292
368
  if (start_node is None) and not (end_node is None):
293
369
  start_node = end_node
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 3.0.1
3
+ Version: 3.0.3
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
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
51
51
 
52
52
  setup(
53
53
  name='pyqrackising',
54
- version='3.0.1',
54
+ version='3.0.3',
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