PyQrackIsing 1.5.3__tar.gz → 1.6.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 (20) hide show
  1. {pyqrackising-1.5.3/PyQrackIsing.egg-info → pyqrackising-1.6.0}/PKG-INFO +1 -1
  2. {pyqrackising-1.5.3 → pyqrackising-1.6.0}/PyQrackIsing/__init__.py +1 -0
  3. pyqrackising-1.6.0/PyQrackIsing/spin_glass_solver.py +99 -0
  4. {pyqrackising-1.5.3 → pyqrackising-1.6.0/PyQrackIsing.egg-info}/PKG-INFO +1 -1
  5. {pyqrackising-1.5.3 → pyqrackising-1.6.0}/PyQrackIsing.egg-info/SOURCES.txt +1 -0
  6. {pyqrackising-1.5.3 → pyqrackising-1.6.0}/README.md +21 -0
  7. {pyqrackising-1.5.3 → pyqrackising-1.6.0}/pyproject.toml +1 -1
  8. {pyqrackising-1.5.3 → pyqrackising-1.6.0}/setup.py +1 -1
  9. {pyqrackising-1.5.3 → pyqrackising-1.6.0}/CMakeLists.txt +0 -0
  10. {pyqrackising-1.5.3 → pyqrackising-1.6.0}/LICENSE.md +0 -0
  11. {pyqrackising-1.5.3 → pyqrackising-1.6.0}/MANIFEST.in +0 -0
  12. {pyqrackising-1.5.3 → pyqrackising-1.6.0}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
  13. {pyqrackising-1.5.3 → pyqrackising-1.6.0}/PyQrackIsing/generate_tfim_samples.py +0 -0
  14. {pyqrackising-1.5.3 → pyqrackising-1.6.0}/PyQrackIsing/maxcut_tfim.py +0 -0
  15. {pyqrackising-1.5.3 → pyqrackising-1.6.0}/PyQrackIsing/tfim_magnetization.py +0 -0
  16. {pyqrackising-1.5.3 → pyqrackising-1.6.0}/PyQrackIsing/tfim_square_magnetization.py +0 -0
  17. {pyqrackising-1.5.3 → pyqrackising-1.6.0}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
  18. {pyqrackising-1.5.3 → pyqrackising-1.6.0}/PyQrackIsing.egg-info/not-zip-safe +0 -0
  19. {pyqrackising-1.5.3 → pyqrackising-1.6.0}/PyQrackIsing.egg-info/top_level.txt +0 -0
  20. {pyqrackising-1.5.3 → pyqrackising-1.6.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.5.3
3
+ Version: 1.6.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
@@ -2,3 +2,4 @@ from .generate_tfim_samples import generate_tfim_samples
2
2
  from .tfim_magnetization import tfim_magnetization
3
3
  from .tfim_square_magnetization import tfim_square_magnetization
4
4
  from .maxcut_tfim import maxcut_tfim
5
+ from .spin_glass_solver import spin_glass_solver
@@ -0,0 +1,99 @@
1
+ from .maxcut_tfim import maxcut_tfim
2
+ import multiprocessing
3
+ import os
4
+
5
+
6
+ def evaluate_cut_edges(state, flat_edges):
7
+ cut_edges = []
8
+ for i in range(len(flat_edges) // 2):
9
+ i2 = i << 1
10
+ u, v = flat_edges[i2], flat_edges[i2 + 1]
11
+ if ((state >> u) & 1) != ((state >> v) & 1):
12
+ cut_edges.append((u, v))
13
+ cut_size = len(cut_edges)
14
+
15
+ return cut_size, cut_edges
16
+
17
+
18
+ def compute_energy(theta_bits, G):
19
+ # Reconstruct Ising energy (note: MAXCUT flips sign!)
20
+ spins = {i: 1 if theta_bits[i] else -1 for i in range(len(theta_bits))}
21
+ energy = -sum(G[u][v].get("weight", 1) * spins[u] * spins[v] for u, v in G.edges())
22
+
23
+ return energy
24
+
25
+ # Parallelization by Elara (OpenAI custom GPT):
26
+ def bootstrap_worker(args):
27
+ theta, G, indices = args
28
+ local_theta = theta.copy()
29
+ flipped = []
30
+ for i in indices:
31
+ local_theta[i] = not local_theta[i]
32
+ flipped.append(local_theta[i])
33
+ energy = compute_energy(local_theta, G)
34
+
35
+ return indices, energy, flipped
36
+
37
+ def spin_glass_solver(G):
38
+ cut_value, bitstring, cut_edges = maxcut_tfim(G, quality=6)
39
+ best_theta = [ b == '1' for b in list(bitstring)]
40
+ min_energy = compute_energy(best_theta, G)
41
+ n_qubits = len(best_theta)
42
+ iter_count = 0
43
+ improved = True
44
+ while improved:
45
+ improved = False
46
+ improved_1qb = True
47
+ while improved_1qb:
48
+ improved_1qb = False
49
+ theta = best_theta.copy()
50
+
51
+ with multiprocessing.Pool(processes=os.cpu_count()) as pool:
52
+ args = []
53
+ for i in range(n_qubits):
54
+ args.append((theta, G, (i,)))
55
+ results = pool.map(bootstrap_worker, args)
56
+
57
+ results.sort(key=lambda r: r[1])
58
+ indices, energy, flipped = results[0]
59
+ if energy < min_energy:
60
+ min_energy = energy
61
+ for i in range(len(indices)):
62
+ best_theta[indices[i]] = flipped[i]
63
+ improved_1qb = True
64
+
65
+ iter_count += 1
66
+
67
+ if n_qubits < 2:
68
+ break
69
+
70
+ theta = best_theta.copy()
71
+
72
+ with multiprocessing.Pool(processes=os.cpu_count()) as pool:
73
+ args = []
74
+ for i in range(n_qubits):
75
+ for j in range(i + 1, n_qubits):
76
+ args.append((theta, G, (i, j)))
77
+ results = pool.map(bootstrap_worker, args)
78
+
79
+ results.sort(key=lambda r: r[1])
80
+ indices, energy, flipped = results[0]
81
+ if energy < min_energy:
82
+ min_energy = energy
83
+ for i in range(len(indices)):
84
+ best_theta[indices[i]] = flipped[i]
85
+ improved = True
86
+
87
+ iter_count += 1
88
+
89
+ sample = 0
90
+ bitstring = ""
91
+ for i in range(len(best_theta)):
92
+ b = best_theta[i]
93
+ bitstring += '1' if b else '0'
94
+ if b:
95
+ sample |= 1 << i
96
+
97
+ cut_value, cut_edges = evaluate_cut_edges(sample, [int(item) for tup in G.edges() for item in tup])
98
+
99
+ return cut_value, bitstring, cut_edges, min_energy
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.5.3
3
+ Version: 1.6.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
@@ -8,6 +8,7 @@ PyQrackIsing/__init__.py
8
8
  PyQrackIsing/_pyqrack_ising.cpp
9
9
  PyQrackIsing/generate_tfim_samples.py
10
10
  PyQrackIsing/maxcut_tfim.py
11
+ PyQrackIsing/spin_glass_solver.py
11
12
  PyQrackIsing/tfim_magnetization.py
12
13
  PyQrackIsing/tfim_square_magnetization.py
13
14
  PyQrackIsing.egg-info/PKG-INFO
@@ -51,6 +51,27 @@ best_cut_value, best_solution_bit_string, best_cut_edges = maxcut_tfim(G, qualit
51
51
 
52
52
  The (integer) `quality` setting is optional, with a default value of `12`, but you can turn it up for higher-quality results, or turn it down to save time. (You can also optionally specify the number of measurement `shots` as an argument, if you want specific fine-grained control over resource usage.) If you want to run MAXCUT on a graph with non-uniform edge weights, specify them as the `weight` attribute of each edge, with `networkx`. (If any `weight` attribute is not defined, the solver assumes it's `1.0` for that edge.)
53
53
 
54
+ Based on a combination of the TFIM-inspired MAXCUT solver and another technique for finding ground-state energy in quantum chemistry that we call the _"binary Clifford eigensolver,"_ we also provide an (approximate) spin glass ground-state solver:
55
+ ```py
56
+ from PyQrackIsing import spin_glass_solver
57
+ import networkx as nx
58
+ import numpy as np
59
+
60
+
61
+ # NP-complete spin glass
62
+ def generate_spin_glass_graph(n_nodes=16, degree=3, seed=None):
63
+ if not (seed is None):
64
+ np.random.seed(seed)
65
+ G = nx.random_regular_graph(d=degree, n=n_nodes, seed=seed)
66
+ for u, v in G.edges():
67
+ G[u][v]['weight'] = np.random.choice([-1, 1]) # spin glass couplings
68
+ return G
69
+
70
+
71
+ G = generate_spin_glass_graph(n_nodes=64, seed=42)
72
+ cut_value, bitstring, cut_edges, min_energy = spin_glass_solver(G)
73
+ ```
74
+
54
75
  ## About
55
76
  Transverse field Ising model (TFIM) is the basis of most claimed algorithmic "quantum advantage," circa 2025, with the notable exception of Shor's integer factoring algorithm.
56
77
 
@@ -10,7 +10,7 @@ build-backend = "setuptools.build_meta"
10
10
 
11
11
  [project]
12
12
  name = "PyQrackIsing"
13
- version = "1.5.3"
13
+ version = "1.6.0"
14
14
  requires-python = ">=3.8"
15
15
  description = "Near-ideal closed-form solutions for transverse field Ising model (TFIM)"
16
16
  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.5.3',
54
+ version='1.6.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