PyQrackIsing 1.5.3__tar.gz → 1.6.2__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.2}/PKG-INFO +1 -1
  2. {pyqrackising-1.5.3 → pyqrackising-1.6.2}/PyQrackIsing/__init__.py +1 -0
  3. {pyqrackising-1.5.3 → pyqrackising-1.6.2}/PyQrackIsing/maxcut_tfim.py +13 -11
  4. pyqrackising-1.6.2/PyQrackIsing/spin_glass_solver.py +98 -0
  5. {pyqrackising-1.5.3 → pyqrackising-1.6.2/PyQrackIsing.egg-info}/PKG-INFO +1 -1
  6. {pyqrackising-1.5.3 → pyqrackising-1.6.2}/PyQrackIsing.egg-info/SOURCES.txt +1 -0
  7. {pyqrackising-1.5.3 → pyqrackising-1.6.2}/README.md +21 -0
  8. {pyqrackising-1.5.3 → pyqrackising-1.6.2}/pyproject.toml +1 -1
  9. {pyqrackising-1.5.3 → pyqrackising-1.6.2}/setup.py +1 -1
  10. {pyqrackising-1.5.3 → pyqrackising-1.6.2}/CMakeLists.txt +0 -0
  11. {pyqrackising-1.5.3 → pyqrackising-1.6.2}/LICENSE.md +0 -0
  12. {pyqrackising-1.5.3 → pyqrackising-1.6.2}/MANIFEST.in +0 -0
  13. {pyqrackising-1.5.3 → pyqrackising-1.6.2}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
  14. {pyqrackising-1.5.3 → pyqrackising-1.6.2}/PyQrackIsing/generate_tfim_samples.py +0 -0
  15. {pyqrackising-1.5.3 → pyqrackising-1.6.2}/PyQrackIsing/tfim_magnetization.py +0 -0
  16. {pyqrackising-1.5.3 → pyqrackising-1.6.2}/PyQrackIsing/tfim_square_magnetization.py +0 -0
  17. {pyqrackising-1.5.3 → pyqrackising-1.6.2}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
  18. {pyqrackising-1.5.3 → pyqrackising-1.6.2}/PyQrackIsing.egg-info/not-zip-safe +0 -0
  19. {pyqrackising-1.5.3 → pyqrackising-1.6.2}/PyQrackIsing.egg-info/top_level.txt +0 -0
  20. {pyqrackising-1.5.3 → pyqrackising-1.6.2}/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.2
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
@@ -49,24 +49,25 @@ def local_repulsion_choice(adjacency, degrees, weights, n, m):
49
49
  return mask
50
50
 
51
51
 
52
- def evaluate_cut_edges(samples, flat_edges):
53
- best_value = -1
52
+ def evaluate_cut_edges(samples, G):
53
+ best_value = float("-inf")
54
54
  best_solution = None
55
55
  best_cut_edges = None
56
+
56
57
  for state in samples:
57
58
  cut_edges = []
58
- for i in range(len(flat_edges) // 2):
59
- i2 = i << 1
60
- u, v = flat_edges[i2], flat_edges[i2 + 1]
59
+ cut_value = 0
60
+ for u, v, data in G.edges(data=True):
61
61
  if ((state >> u) & 1) != ((state >> v) & 1):
62
62
  cut_edges.append((u, v))
63
- cut_size = len(cut_edges)
64
- if cut_size > best_value:
65
- best_value = cut_size
63
+ cut_value += data.get("weight", 1.0)
64
+
65
+ if cut_value > best_value:
66
+ best_value = cut_value
66
67
  best_solution = state
67
68
  best_cut_edges = cut_edges
68
69
 
69
- return best_value, best_solution, best_cut_edges
70
+ return float(best_value), best_solution, best_cut_edges
70
71
 
71
72
 
72
73
  # By Gemini (Google Search AI)
@@ -89,7 +90,8 @@ def maxcut_tfim(
89
90
  degrees = np.array([sum(abs(edge_attributes.get('weight', 1.0)) for _, edge_attributes in G.adj[i].items()) for i in range(n_qubits)], dtype=np.float64)
90
91
  thresholds = tfim_sampler._maxcut_hamming_cdf(J_eff, degrees, quality)
91
92
  G_dict = nx.to_dict_of_lists(G)
92
- weights = 1.0 / (degrees + 1.0)
93
+ J_max = max(J_eff)
94
+ weights = 1.0 / (1.0 + (J_max - J_eff))
93
95
  samples = []
94
96
  for s in range(shots):
95
97
  # First dimension: Hamming weight
@@ -101,6 +103,6 @@ def maxcut_tfim(
101
103
  # Second dimension: permutation within Hamming weight
102
104
  samples.append(local_repulsion_choice(G_dict, degrees, weights, n_qubits, m))
103
105
 
104
- best_value, best_solution, best_cut_edges = evaluate_cut_edges(samples, [int(item) for tup in G.edges() for item in tup])
106
+ best_value, best_solution, best_cut_edges = evaluate_cut_edges(samples, G)
105
107
 
106
108
  return best_value, int_to_bitstring(best_solution, n_qubits), best_cut_edges
@@ -0,0 +1,98 @@
1
+ from .maxcut_tfim import maxcut_tfim
2
+ import multiprocessing
3
+ import os
4
+
5
+
6
+ def evaluate_cut_edges(state, G):
7
+ cut_edges = []
8
+ cut_value = 0
9
+ for u, v, data in G.edges(data=True):
10
+ if ((state >> u) & 1) != ((state >> v) & 1):
11
+ cut_edges.append((u, v))
12
+ cut_value += data.get("weight", 1.0)
13
+
14
+ return float(cut_value), cut_edges
15
+
16
+
17
+ def compute_energy(theta_bits, G):
18
+ # Reconstruct Ising energy (note: MAXCUT flips sign!)
19
+ spins = {i: 1 if theta_bits[i] else -1 for i in range(len(theta_bits))}
20
+ energy = -sum(G[u][v].get("weight", 1) * spins[u] * spins[v] for u, v in G.edges())
21
+
22
+ return energy
23
+
24
+ # Parallelization by Elara (OpenAI custom GPT):
25
+ def bootstrap_worker(args):
26
+ theta, G, indices = args
27
+ local_theta = theta.copy()
28
+ flipped = []
29
+ for i in indices:
30
+ local_theta[i] = not local_theta[i]
31
+ flipped.append(local_theta[i])
32
+ energy = compute_energy(local_theta, G)
33
+
34
+ return indices, energy, flipped
35
+
36
+ def spin_glass_solver(G, quality=0):
37
+ cut_value, bitstring, cut_edges = maxcut_tfim(G, quality)
38
+ best_theta = [ b == '1' for b in list(bitstring)]
39
+ min_energy = compute_energy(best_theta, G)
40
+ n_qubits = len(best_theta)
41
+ iter_count = 0
42
+ improved = True
43
+ while improved:
44
+ improved = False
45
+ improved_1qb = True
46
+ while improved_1qb:
47
+ improved_1qb = False
48
+ theta = best_theta.copy()
49
+
50
+ with multiprocessing.Pool(processes=os.cpu_count()) as pool:
51
+ args = []
52
+ for i in range(n_qubits):
53
+ args.append((theta, G, (i,)))
54
+ results = pool.map(bootstrap_worker, args)
55
+
56
+ results.sort(key=lambda r: r[1])
57
+ indices, energy, flipped = results[0]
58
+ if energy < min_energy:
59
+ min_energy = energy
60
+ for i in range(len(indices)):
61
+ best_theta[indices[i]] = flipped[i]
62
+ improved_1qb = True
63
+
64
+ iter_count += 1
65
+
66
+ if n_qubits < 2:
67
+ break
68
+
69
+ theta = best_theta.copy()
70
+
71
+ with multiprocessing.Pool(processes=os.cpu_count()) as pool:
72
+ args = []
73
+ for i in range(n_qubits):
74
+ for j in range(i + 1, n_qubits):
75
+ args.append((theta, G, (i, j)))
76
+ results = pool.map(bootstrap_worker, args)
77
+
78
+ results.sort(key=lambda r: r[1])
79
+ indices, energy, flipped = results[0]
80
+ if energy < min_energy:
81
+ min_energy = energy
82
+ for i in range(len(indices)):
83
+ best_theta[indices[i]] = flipped[i]
84
+ improved = True
85
+
86
+ iter_count += 1
87
+
88
+ sample = 0
89
+ bitstring = ""
90
+ for i in range(len(best_theta)):
91
+ b = best_theta[i]
92
+ bitstring += '1' if b else '0'
93
+ if b:
94
+ sample |= 1 << i
95
+
96
+ cut_value, cut_edges = evaluate_cut_edges(sample, G)
97
+
98
+ 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.2
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, quality=0)
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.2"
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.2',
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