PyQrackIsing 3.4.3__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.3/pyqrackising.egg-info → pyqrackising-3.4.4}/PKG-INFO +1 -1
  2. {pyqrackising-3.4.3 → pyqrackising-3.4.4}/pyproject.toml +1 -1
  3. {pyqrackising-3.4.3 → pyqrackising-3.4.4}/pyqrackising/tsp.py +182 -71
  4. {pyqrackising-3.4.3 → pyqrackising-3.4.4/pyqrackising.egg-info}/PKG-INFO +1 -1
  5. {pyqrackising-3.4.3 → pyqrackising-3.4.4}/setup.py +1 -1
  6. {pyqrackising-3.4.3 → pyqrackising-3.4.4}/CMakeLists.txt +0 -0
  7. {pyqrackising-3.4.3 → pyqrackising-3.4.4}/LICENSE.md +0 -0
  8. {pyqrackising-3.4.3 → pyqrackising-3.4.4}/MANIFEST.in +0 -0
  9. {pyqrackising-3.4.3 → pyqrackising-3.4.4}/README.md +0 -0
  10. {pyqrackising-3.4.3 → pyqrackising-3.4.4}/pyqrackising/__init__.py +0 -0
  11. {pyqrackising-3.4.3 → pyqrackising-3.4.4}/pyqrackising/_pyqrack_ising.cpp +0 -0
  12. {pyqrackising-3.4.3 → pyqrackising-3.4.4}/pyqrackising/generate_tfim_samples.py +0 -0
  13. {pyqrackising-3.4.3 → pyqrackising-3.4.4}/pyqrackising/kernels.cl +0 -0
  14. {pyqrackising-3.4.3 → pyqrackising-3.4.4}/pyqrackising/maxcut_tfim.py +0 -0
  15. {pyqrackising-3.4.3 → pyqrackising-3.4.4}/pyqrackising/spin_glass_solver.py +0 -0
  16. {pyqrackising-3.4.3 → pyqrackising-3.4.4}/pyqrackising/tfim_magnetization.py +0 -0
  17. {pyqrackising-3.4.3 → pyqrackising-3.4.4}/pyqrackising/tfim_square_magnetization.py +0 -0
  18. {pyqrackising-3.4.3 → pyqrackising-3.4.4}/pyqrackising.egg-info/SOURCES.txt +0 -0
  19. {pyqrackising-3.4.3 → pyqrackising-3.4.4}/pyqrackising.egg-info/dependency_links.txt +0 -0
  20. {pyqrackising-3.4.3 → pyqrackising-3.4.4}/pyqrackising.egg-info/not-zip-safe +0 -0
  21. {pyqrackising-3.4.3 → pyqrackising-3.4.4}/pyqrackising.egg-info/top_level.txt +0 -0
  22. {pyqrackising-3.4.3 → 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.3
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.3"
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,80 +207,109 @@ 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, is_asym):
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]]
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]
217
+ else:
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
+ )
228
226
  if weight < best_weight:
229
227
  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)
244
- else:
245
- terminals_a = [path_a[0], path_a[-1]]
246
- terminals_b = [path_b[0], path_b[-1]]
247
-
248
- best_connect = G_m[terminals_a[1], terminals_b[0]]
249
- best_path = path_b.copy()
250
- weight = G_m[terminals_a[0], terminals_b[1]]
251
- if weight < best_connect:
252
- best_connect = 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
- if not is_asym:
258
- path_weight = path_length(path_a, G_m) + path_length(path_a, G_m)
259
- best_weight = best_connect + path_weight
231
+ return best_path
260
232
 
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):
261
254
  for _ in range(2):
262
- for _ in range(2):
263
- if is_asym:
264
- path_weight = path_length(path_a, G_m) + path_length(path_a, G_m)
265
- best_weight = best_connect + path_weight
266
- for i in range(1, len(path_b)):
267
- weight = (
268
- G_m[terminals_a[0], path_b[i - 1]] +
269
- G_m[terminals_a[1], path_b[i]] -
270
- G_m[path_b[i - 1], path_b[i]]
271
- )
272
- if (weight + path_weight) < best_weight:
273
- best_weight = weight + path_weight
274
- best_path = path_b.copy()
275
- best_path[i:i] = path_a
276
- path_a.reverse()
277
- terminals_a.reverse()
278
- path_a, path_b = path_b, path_a
279
- terminals_a, terminals_b = terminals_b, terminals_a
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
280
269
 
281
270
  return best_path
282
271
 
283
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
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
312
+
284
313
 
285
314
  def monte_carlo_loop(n_nodes):
286
315
  bits = ([], [])
@@ -295,6 +324,72 @@ def monte_carlo_loop(n_nodes):
295
324
  return bits
296
325
 
297
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
+
298
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):
299
394
  nodes = None
300
395
  n_nodes = 0
@@ -312,7 +407,15 @@ def tsp_symmetric(G, start_node=None, end_node=None, quality=2, shots=None, corr
312
407
  start_node = None
313
408
  end_node = None
314
409
 
315
- 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
+
316
419
  if n_nodes == 3:
317
420
  if is_cyclic:
318
421
  weight_0 = G_m[0, 1] + G_m[1, 2] + G_m[2, 0]
@@ -383,7 +486,7 @@ def tsp_symmetric(G, start_node=None, end_node=None, quality=2, shots=None, corr
383
486
  path_b = [b[x] for x in sol_b[0]]
384
487
 
385
488
  if start_node is None:
386
- best_path = stitch(G_m, path_a, path_b, False)
489
+ best_path = stitch_symmetric(G_m, path_a, path_b)
387
490
  else:
388
491
  best_path = path_a + path_b
389
492
  best_weight = G_m[path_a[0], path_b[0]]
@@ -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:
@@ -492,7 +603,7 @@ def tsp_asymmetric(G, start_node=None, end_node=None, quality=1, shots=None, cor
492
603
  path_b = [b[x] for x in sol_b[0]]
493
604
 
494
605
  if start_node is None:
495
- best_path = stitch(G_m, path_a, path_b, True)
606
+ best_path = stitch_asymmetric(G_m, path_a, path_b)
496
607
  else:
497
608
  best_path = path_a + path_b
498
609
  best_weight = G_m[path_a[0], path_b[0]]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 3.4.3
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.3',
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