PyQrackIsing 3.4.2__tar.gz → 3.4.4__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.4.2/pyqrackising.egg-info → pyqrackising-3.4.4}/PKG-INFO +1 -1
  2. {pyqrackising-3.4.2 → pyqrackising-3.4.4}/pyproject.toml +1 -1
  3. {pyqrackising-3.4.2 → pyqrackising-3.4.4}/pyqrackising/tsp.py +184 -80
  4. {pyqrackising-3.4.2 → pyqrackising-3.4.4/pyqrackising.egg-info}/PKG-INFO +1 -1
  5. {pyqrackising-3.4.2 → pyqrackising-3.4.4}/setup.py +1 -1
  6. {pyqrackising-3.4.2 → pyqrackising-3.4.4}/CMakeLists.txt +0 -0
  7. {pyqrackising-3.4.2 → pyqrackising-3.4.4}/LICENSE.md +0 -0
  8. {pyqrackising-3.4.2 → pyqrackising-3.4.4}/MANIFEST.in +0 -0
  9. {pyqrackising-3.4.2 → pyqrackising-3.4.4}/README.md +0 -0
  10. {pyqrackising-3.4.2 → pyqrackising-3.4.4}/pyqrackising/__init__.py +0 -0
  11. {pyqrackising-3.4.2 → pyqrackising-3.4.4}/pyqrackising/_pyqrack_ising.cpp +0 -0
  12. {pyqrackising-3.4.2 → pyqrackising-3.4.4}/pyqrackising/generate_tfim_samples.py +0 -0
  13. {pyqrackising-3.4.2 → pyqrackising-3.4.4}/pyqrackising/kernels.cl +0 -0
  14. {pyqrackising-3.4.2 → pyqrackising-3.4.4}/pyqrackising/maxcut_tfim.py +0 -0
  15. {pyqrackising-3.4.2 → pyqrackising-3.4.4}/pyqrackising/spin_glass_solver.py +0 -0
  16. {pyqrackising-3.4.2 → pyqrackising-3.4.4}/pyqrackising/tfim_magnetization.py +0 -0
  17. {pyqrackising-3.4.2 → pyqrackising-3.4.4}/pyqrackising/tfim_square_magnetization.py +0 -0
  18. {pyqrackising-3.4.2 → pyqrackising-3.4.4}/pyqrackising.egg-info/SOURCES.txt +0 -0
  19. {pyqrackising-3.4.2 → pyqrackising-3.4.4}/pyqrackising.egg-info/dependency_links.txt +0 -0
  20. {pyqrackising-3.4.2 → pyqrackising-3.4.4}/pyqrackising.egg-info/not-zip-safe +0 -0
  21. {pyqrackising-3.4.2 → pyqrackising-3.4.4}/pyqrackising.egg-info/top_level.txt +0 -0
  22. {pyqrackising-3.4.2 → pyqrackising-3.4.4}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 3.4.2
3
+ Version: 3.4.4
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.4.2"
14
+ version = "3.4.4"
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"}
@@ -9,8 +9,8 @@ import numpy as np
9
9
  @njit
10
10
  def path_length(path, G_m):
11
11
  tot_len = 0.0
12
- for i in range(len(path)-1):
13
- tot_len += G_m[path[i], path[i+1]]
12
+ for i in range(len(path) - 1):
13
+ tot_len += G_m[path[i], path[i + 1]]
14
14
 
15
15
  return tot_len
16
16
 
@@ -207,72 +207,108 @@ def init_G_a_b(G_m, a, b):
207
207
 
208
208
 
209
209
  @njit
210
- def stitch(G_m, path_a, path_b, sol_weight):
211
- is_single_a = len(path_a) == 1
212
- is_single_b = len(path_b) == 1
213
- best_path = [0]
214
- best_weight = 0.0
215
- if is_single_a or is_single_b:
216
- singlet = 0
217
- bulk = [0]
218
- if is_single_a:
219
- singlet = path_a[0]
220
- bulk = path_b
221
- else:
222
- singlet = path_b[0]
223
- bulk = path_a
224
-
225
- best_path = bulk.copy()
226
- best_weight = G_m[singlet, bulk[0]]
227
- weight = G_m[singlet, bulk[-1]]
228
- if weight < best_weight:
229
- best_weight = weight
230
- best_path += [singlet]
231
- else:
232
- best_path = [singlet] + best_path
233
-
234
- for i in range(1, len(bulk)):
235
- weight = (
236
- G_m[singlet, bulk[i - 1]] +
237
- G_m[singlet, bulk[i]] -
238
- G_m[bulk[i - 1], bulk[i]]
239
- )
240
- if weight < best_weight:
241
- best_weight = weight
242
- best_path = bulk.copy()
243
- best_path.insert(i, singlet)
210
+ def stich_singlet(G_m, singlet, bulk):
211
+ best_path = bulk.copy()
212
+ best_weight = G_m[singlet, bulk[0]]
213
+ weight = G_m[singlet, bulk[-1]]
214
+ if weight < best_weight:
215
+ best_weight = weight
216
+ best_path += [singlet]
244
217
  else:
245
- terminals_a = [path_a[0], path_a[-1]]
246
- terminals_b = [path_b[0], path_b[-1]]
247
-
248
- best_weight = G_m[terminals_a[1], terminals_b[0]]
249
- best_path = path_b.copy()
250
- weight = G_m[terminals_a[0], terminals_b[1]]
218
+ best_path = [singlet] + best_path
219
+
220
+ for i in range(1, len(bulk)):
221
+ weight = (
222
+ G_m[singlet, bulk[i - 1]] +
223
+ G_m[singlet, bulk[i]] -
224
+ G_m[bulk[i - 1], bulk[i]]
225
+ )
251
226
  if weight < best_weight:
252
227
  best_weight = weight
253
- best_path += path_a
254
- else:
255
- best_path = path_a + best_path
228
+ best_path = bulk.copy()
229
+ best_path.insert(i, singlet)
256
230
 
257
- for _ in range(2):
258
- for _ in range(2):
259
- for i in range(1, len(path_b)):
260
- weight = (
261
- G_m[terminals_a[0], path_b[i - 1]] +
262
- G_m[terminals_a[1], path_b[i]] -
263
- G_m[path_b[i - 1], path_b[i]]
264
- )
265
- if weight < best_weight:
266
- best_weight = weight
267
- best_path = path_b.copy()
268
- best_path[i:i] = path_a
269
- path_a.reverse()
270
- terminals_a.reverse()
271
- path_a, path_b = path_b, path_a
272
- terminals_a, terminals_b = terminals_b, terminals_a
231
+ return best_path
273
232
 
274
- return best_path, best_weight
233
+ @njit
234
+ def stitch_symmetric(G_m, path_a, path_b):
235
+ if len(path_a) == 1:
236
+ return stich_singlet(G_m, path_a[0], path_b)
237
+
238
+ if len(path_b) == 1:
239
+ return stich_singlet(G_m, path_b[0], path_a)
240
+
241
+ terminals_a = [path_a[0], path_a[-1]]
242
+ terminals_b = [path_b[0], path_b[-1]]
243
+
244
+ best_connect = G_m[terminals_a[1], terminals_b[0]]
245
+ best_path = path_b.copy()
246
+ weight = G_m[terminals_a[0], terminals_b[1]]
247
+ if weight < best_connect:
248
+ best_connect = weight
249
+ best_path += path_a
250
+ else:
251
+ best_path = path_a + best_path
252
+
253
+ for _ in range(2):
254
+ for _ in range(2):
255
+ for i in range(1, len(path_b)):
256
+ weight = (
257
+ G_m[terminals_a[0], path_b[i - 1]] +
258
+ G_m[terminals_a[1], path_b[i]] -
259
+ G_m[path_b[i - 1], path_b[i]]
260
+ )
261
+ if weight < best_connect:
262
+ best_connect = weight
263
+ best_path = path_b.copy()
264
+ best_path[i:i] = path_a
265
+ path_a.reverse()
266
+ terminals_a.reverse()
267
+ path_a, path_b = path_b, path_a
268
+ terminals_a, terminals_b = terminals_b, terminals_a
269
+
270
+ return best_path
271
+
272
+
273
+ def stitch_asymmetric(G_m, path_a, path_b):
274
+ if len(path_a) == 1:
275
+ return stich_singlet(G_m, path_a[0], path_b)
276
+
277
+ if len(path_b) == 1:
278
+ return stich_singlet(G_m, path_b[0], path_a)
279
+
280
+ terminals_a = [path_a[0], path_a[-1]]
281
+ terminals_b = [path_b[0], path_b[-1]]
282
+
283
+ best_connect = G_m[terminals_a[1], terminals_b[0]]
284
+ best_path = path_b.copy()
285
+ weight = G_m[terminals_a[0], terminals_b[1]]
286
+ if weight < best_connect:
287
+ best_connect = weight
288
+ best_path += path_a
289
+ else:
290
+ best_path = path_a + best_path
275
291
 
292
+ for _ in range(2):
293
+ for _ in range(2):
294
+ path_weight = path_length(path_a, G_m) + path_length(path_a, G_m)
295
+ best_weight = best_connect + path_weight
296
+ for i in range(1, len(path_b)):
297
+ weight = (
298
+ G_m[terminals_a[0], path_b[i - 1]] +
299
+ G_m[terminals_a[1], path_b[i]] -
300
+ G_m[path_b[i - 1], path_b[i]]
301
+ )
302
+ if (weight + path_weight) < best_weight:
303
+ best_weight = weight + path_weight
304
+ best_path = path_b.copy()
305
+ best_path[i:i] = path_a
306
+ path_a.reverse()
307
+ terminals_a.reverse()
308
+ path_a, path_b = path_b, path_a
309
+ terminals_a, terminals_b = terminals_b, terminals_a
310
+
311
+ return best_path
276
312
 
277
313
 
278
314
  def monte_carlo_loop(n_nodes):
@@ -288,6 +324,72 @@ def monte_carlo_loop(n_nodes):
288
324
  return bits
289
325
 
290
326
 
327
+ # Elara suggested replacing base-case handling with her brute-force solver
328
+ @njit
329
+ def tsp_bruteforce_cyclic(G_m, perms):
330
+ """
331
+ Brute-force TSP solver for small n.
332
+ G_m : numpy.ndarray (2D adjacency/weight matrix)
333
+ is_cyclic : bool (default=True) – whether to close the tour
334
+
335
+ Returns:
336
+ (best_path, best_weight)
337
+ """
338
+ n = len(G_m)
339
+ best_weight = float('inf')
340
+ best_path = None
341
+
342
+ # Must fix node 0 at start to remove rotational symmetry in cyclic case!
343
+
344
+ max_i = len(perms[0]) - 1
345
+
346
+ for perm in perms:
347
+ path = [0] + list(perm)
348
+ weight = 0.0
349
+ for i in range(max_i):
350
+ weight += G_m[path[i], path[i+1]]
351
+ weight += G_m[path[-1], path[0]]
352
+
353
+ if weight < best_weight:
354
+ best_weight = weight
355
+ best_path = path
356
+
357
+ best_path = best_path + [best_path[0]]
358
+
359
+ return best_path, best_weight
360
+
361
+ @njit
362
+ def tsp_bruteforce_acyclic(G_m, perms):
363
+ """
364
+ Brute-force TSP solver for small n.
365
+ G_m : numpy.ndarray (2D adjacency/weight matrix)
366
+ is_cyclic : bool (default=True) – whether to close the tour
367
+
368
+ Returns:
369
+ (best_path, best_weight)
370
+ """
371
+ n = len(G_m)
372
+ best_weight = float('inf')
373
+ best_path = None
374
+
375
+ # Must fix node 0 at start to remove rotational symmetry in cyclic case!
376
+
377
+ max_i = len(perms[0]) - 1
378
+
379
+ for path in perms:
380
+ weight = 0.0
381
+ for i in range(max_i):
382
+ weight += G_m[path[i], path[i+1]]
383
+
384
+ if weight < best_weight:
385
+ best_weight = weight
386
+ best_path = path
387
+
388
+ best_path = list(best_path)
389
+
390
+ return best_path, best_weight
391
+
392
+
291
393
  def tsp_symmetric(G, start_node=None, end_node=None, quality=2, shots=None, correction_quality=2, monte_carlo=True, k_neighbors=16, is_cyclic=True, multi_start=1, is_top_level=True):
292
394
  nodes = None
293
395
  n_nodes = 0
@@ -305,7 +407,15 @@ def tsp_symmetric(G, start_node=None, end_node=None, quality=2, shots=None, corr
305
407
  start_node = None
306
408
  end_node = None
307
409
 
308
- if n_nodes < 4:
410
+ if n_nodes < 6:
411
+ if n_nodes > 3:
412
+ if is_cyclic:
413
+ best_path, best_weight = tsp_bruteforce_cyclic(G_m, list(itertools.permutations(list(range(1, n_nodes)))))
414
+ else:
415
+ best_path, best_weight = tsp_bruteforce_acyclic(G_m, list(itertools.permutations(list(range(n_nodes)))))
416
+
417
+ return [nodes[x] for x in best_path], best_weight
418
+
309
419
  if n_nodes == 3:
310
420
  if is_cyclic:
311
421
  weight_0 = G_m[0, 1] + G_m[1, 2] + G_m[2, 0]
@@ -375,13 +485,8 @@ def tsp_symmetric(G, start_node=None, end_node=None, quality=2, shots=None, corr
375
485
  path_a = [a[x] for x in sol_a[0]]
376
486
  path_b = [b[x] for x in sol_b[0]]
377
487
 
378
- sol_weight = sol_a[1] + sol_b[1]
379
-
380
- if len(c):
381
- sol_weight += G_m[b[-1], c[0]]
382
-
383
488
  if start_node is None:
384
- best_path, best_weight = stitch(G_m, path_a, path_b, sol_weight)
489
+ best_path = stitch_symmetric(G_m, path_a, path_b)
385
490
  else:
386
491
  best_path = path_a + path_b
387
492
  best_weight = G_m[path_a[0], path_b[0]]
@@ -390,11 +495,9 @@ def tsp_symmetric(G, start_node=None, end_node=None, quality=2, shots=None, corr
390
495
  path_b.reverse()
391
496
  best_path = path_a + path_b
392
497
  best_weight = weight
393
- best_weight += sol_weight
394
498
 
395
499
  if len(c):
396
500
  best_path += c
397
- best_weight += G_m[best_path[-1], c[0]]
398
501
 
399
502
  if is_top_level:
400
503
  if is_cyclic:
@@ -437,7 +540,15 @@ def tsp_asymmetric(G, start_node=None, end_node=None, quality=1, shots=None, cor
437
540
  start_node = None
438
541
  end_node = None
439
542
 
440
- if n_nodes < 3:
543
+ if n_nodes < 6:
544
+ if n_nodes > 2:
545
+ if is_cyclic:
546
+ best_path, best_weight = tsp_bruteforce_cyclic(G_m, list(itertools.permutations(list(range(1, n_nodes)))))
547
+ else:
548
+ best_path, best_weight = tsp_bruteforce_acyclic(G_m, list(itertools.permutations(list(range(n_nodes)))))
549
+
550
+ return [nodes[x] for x in best_path], best_weight
551
+
441
552
  if n_nodes == 2:
442
553
  weight = G_m[0, 1]
443
554
  if G_m[1, 0] < weight:
@@ -491,13 +602,8 @@ def tsp_asymmetric(G, start_node=None, end_node=None, quality=1, shots=None, cor
491
602
  path_a = [a[x] for x in sol_a[0]]
492
603
  path_b = [b[x] for x in sol_b[0]]
493
604
 
494
- sol_weight = sol_a[1] + sol_b[1]
495
-
496
- if len(c):
497
- sol_weight += G_m[b[-1], c[0]]
498
-
499
605
  if start_node is None:
500
- best_path, best_weight = stitch(G_m, path_a, path_b, sol_weight)
606
+ best_path = stitch_asymmetric(G_m, path_a, path_b)
501
607
  else:
502
608
  best_path = path_a + path_b
503
609
  best_weight = G_m[path_a[0], path_b[0]]
@@ -506,11 +612,9 @@ def tsp_asymmetric(G, start_node=None, end_node=None, quality=1, shots=None, cor
506
612
  path_b.reverse()
507
613
  best_path = path_a + path_b
508
614
  best_weight = weight
509
- best_weight += sol_weight
510
615
 
511
616
  if len(c):
512
617
  best_path += c
513
- best_weight += G_m[best_path[-1], c[0]]
514
618
 
515
619
  if is_top_level:
516
620
  if is_cyclic:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 3.4.2
3
+ Version: 3.4.4
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.4.2',
54
+ version='3.4.4',
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