PyQrackIsing 1.8.0__tar.gz → 1.8.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 (21) hide show
  1. {pyqrackising-1.8.0/PyQrackIsing.egg-info → pyqrackising-1.8.2}/PKG-INFO +1 -1
  2. pyqrackising-1.8.2/PyQrackIsing/spin_glass_solver.py +115 -0
  3. {pyqrackising-1.8.0 → pyqrackising-1.8.2/PyQrackIsing.egg-info}/PKG-INFO +1 -1
  4. {pyqrackising-1.8.0 → pyqrackising-1.8.2}/README.md +5 -4
  5. {pyqrackising-1.8.0 → pyqrackising-1.8.2}/pyproject.toml +2 -1
  6. {pyqrackising-1.8.0 → pyqrackising-1.8.2}/setup.py +1 -1
  7. pyqrackising-1.8.0/PyQrackIsing/spin_glass_solver.py +0 -92
  8. {pyqrackising-1.8.0 → pyqrackising-1.8.2}/CMakeLists.txt +0 -0
  9. {pyqrackising-1.8.0 → pyqrackising-1.8.2}/LICENSE.md +0 -0
  10. {pyqrackising-1.8.0 → pyqrackising-1.8.2}/MANIFEST.in +0 -0
  11. {pyqrackising-1.8.0 → pyqrackising-1.8.2}/PyQrackIsing/__init__.py +0 -0
  12. {pyqrackising-1.8.0 → pyqrackising-1.8.2}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
  13. {pyqrackising-1.8.0 → pyqrackising-1.8.2}/PyQrackIsing/generate_tfim_samples.py +0 -0
  14. {pyqrackising-1.8.0 → pyqrackising-1.8.2}/PyQrackIsing/maxcut_tfim.py +0 -0
  15. {pyqrackising-1.8.0 → pyqrackising-1.8.2}/PyQrackIsing/tfim_magnetization.py +0 -0
  16. {pyqrackising-1.8.0 → pyqrackising-1.8.2}/PyQrackIsing/tfim_square_magnetization.py +0 -0
  17. {pyqrackising-1.8.0 → pyqrackising-1.8.2}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
  18. {pyqrackising-1.8.0 → pyqrackising-1.8.2}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
  19. {pyqrackising-1.8.0 → pyqrackising-1.8.2}/PyQrackIsing.egg-info/not-zip-safe +0 -0
  20. {pyqrackising-1.8.0 → pyqrackising-1.8.2}/PyQrackIsing.egg-info/top_level.txt +0 -0
  21. {pyqrackising-1.8.0 → pyqrackising-1.8.2}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.8.0
3
+ Version: 1.8.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
@@ -0,0 +1,115 @@
1
+ from .maxcut_tfim import maxcut_tfim
2
+ import itertools
3
+ import numpy as np
4
+ from numba import njit, prange
5
+ import os
6
+
7
+
8
+ @njit
9
+ def evaluate_cut_edges(state, edge_keys, edge_values):
10
+ cut_edges = []
11
+ cut_value = 0
12
+ for i in range(len(edge_values)):
13
+ k = i << 1
14
+ u, v = edge_keys[k], edge_keys[k + 1]
15
+ if ((state >> u) & 1) != ((state >> v) & 1):
16
+ cut_edges.append((u, v))
17
+ cut_value += edge_values[i]
18
+
19
+ return float(cut_value), cut_edges
20
+
21
+
22
+ @njit
23
+ def compute_energy(theta_bits, edge_keys, edge_values):
24
+ energy = 0
25
+ for i in range(len(edge_values)):
26
+ k = i << 1
27
+ u, v = edge_keys[k], edge_keys[k + 1]
28
+ spin_u = 1 if theta_bits[u] else -1
29
+ spin_v = 1 if theta_bits[v] else -1
30
+ energy += edge_values[i] * spin_u * spin_v
31
+
32
+ return energy
33
+
34
+
35
+ @njit
36
+ def bootstrap_worker(theta, edge_keys, edge_values, indices):
37
+ local_theta = theta.copy()
38
+ for i in indices:
39
+ local_theta[i] = not local_theta[i]
40
+ energy = compute_energy(local_theta, edge_keys, edge_values)
41
+
42
+ return energy
43
+
44
+
45
+ @njit(parallel=True)
46
+ def bootstrap(theta, edge_keys, edge_values, k, indices_array):
47
+ n = len(indices_array) // k
48
+ energies = np.empty(n)
49
+ j = 0
50
+ for i in prange(n):
51
+ energies[i] = bootstrap_worker(theta, edge_keys, edge_values, indices_array[j:j+k])
52
+ j += k
53
+
54
+ return energies
55
+
56
+
57
+ # By Gemini (Google Search AI)
58
+ def int_to_bitstring(integer, length):
59
+ return (bin(integer)[2:].zfill(length))[::-1]
60
+
61
+
62
+ def spin_glass_solver(G, quality=2, best_guess=None):
63
+ bitstring = ''
64
+ if isinstance(best_guess, str):
65
+ bitstring = best_guess
66
+ elif isinstance(best_guess, int):
67
+ bitstring = int_to_bitstring(best_guess)
68
+ elif isinstance(best_guess, list):
69
+ bitstring = "".join(["1" if b else "0" for b in best_guess])
70
+ else:
71
+ cut_value, bitstring, cut_edges = maxcut_tfim(G, quality=0)
72
+ best_theta = [ b == '1' for b in list(bitstring)]
73
+ n_qubits = len(best_theta)
74
+
75
+ edge_keys = []
76
+ edge_values = []
77
+ for u, v, data in G.edges(data=True):
78
+ edge_keys.append(u)
79
+ edge_keys.append(v)
80
+ edge_values.append(data.get("weight", 1.0))
81
+
82
+ min_energy = compute_energy(best_theta, edge_keys, edge_values)
83
+ improved = True
84
+ while improved:
85
+ improved = False
86
+ for k in range(1, quality + 1):
87
+ if n_qubits < k:
88
+ break
89
+
90
+ theta = best_theta.copy()
91
+
92
+ combos = list(item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist)
93
+ energies = bootstrap(theta, edge_keys, edge_values, k, combos)
94
+
95
+ energy = min(energies)
96
+ if energy < min_energy:
97
+ index_match = np.random.choice(np.where(energies == energy)[0])
98
+ indices = combos[(index_match * k):((index_match + 1) * k)]
99
+ min_energy = energy
100
+ for i in indices:
101
+ best_theta[i] = not best_theta[i]
102
+ improved = True
103
+ break
104
+
105
+ sample = 0
106
+ bitstring = ""
107
+ for i in range(len(best_theta)):
108
+ b = best_theta[i]
109
+ bitstring += '1' if b else '0'
110
+ if b:
111
+ sample |= 1 << i
112
+
113
+ cut_value, cut_edges = evaluate_cut_edges(sample, edge_keys, edge_values)
114
+
115
+ 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.8.0
3
+ Version: 1.8.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
@@ -1,5 +1,5 @@
1
1
  # PyQrack Ising
2
- Efficiently generate near-ideal samples from transverse field Ising model (TFIM)
2
+ Efficiently generate near-ideal samples from transverse field Ising model (TFIM), and TFIM-inspired MAXCUT solutions
3
3
 
4
4
  (It's "the **Ising** on top.")
5
5
 
@@ -68,10 +68,11 @@ def generate_spin_glass_graph(n_nodes=16, degree=3, seed=None):
68
68
  return G
69
69
 
70
70
 
71
- G = generate_spin_glass_graph(n_nodes=64, seed=42, best_guess=None)
72
- cut_value, bitstring, cut_edges, min_energy = spin_glass_solver(G, quality=2)
71
+ G = generate_spin_glass_graph(n_nodes=64, seed=42)
72
+ cut_value, bitstring, cut_edges, energy = spin_glass_solver(G, quality=2, best_guess=None)
73
+ # cut_value, bitstring, cut_edges, energy = spin_glass_solver(G, best_guess=maxcut_tfim(G, quality=12)[1])
73
74
  ```
74
- The `quality` setting default is `2`, and overhead grows roughly like the qubit count to the power of `quality` (so `O(n ** 2)` for default). `best_guess` gives the option to seed the algorithm with a best guess as to the maximal cut (as an integer, binary string, or list of booleans). This function is designed with a sign convention for weights such that it can immediately be used as a MAXCUT solver itself: you might need to reverse the sign convention on your weights for spin glass graphs, but this is only convention.
75
+ The `quality` setting default is `2`, and overhead grows roughly like the qubit count to the power of `quality` (so `O(n ** 2)` for default). `best_guess` gives the option to seed the algorithm with a best guess as to the maximal cut (as an integer, binary string, or list of booleans). By default, `spin_glass_solver()` uses `maxcut_tfim(G, quality=0)` as `best_guess`, which typically works well, but it could be seeded with higher `maxcut_tfim()` `quality` or Goemans-Williamson, for example. This function is designed with a sign convention for weights such that it can immediately be used as a MAXCUT solver itself: you might need to reverse the sign convention on your weights for spin glass graphs, but this is only convention.
75
76
 
76
77
  ## About
77
78
  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.
@@ -5,12 +5,13 @@ requires = [
5
5
  "setuptools",
6
6
  "networkx",
7
7
  "numpy",
8
+ "numba",
8
9
  ]
9
10
  build-backend = "setuptools.build_meta"
10
11
 
11
12
  [project]
12
13
  name = "PyQrackIsing"
13
- version = "1.8.0"
14
+ version = "1.8.2"
14
15
  requires-python = ">=3.8"
15
16
  description = "Near-ideal closed-form solutions for transverse field Ising model (TFIM)"
16
17
  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.8.0',
54
+ version='1.8.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)',
@@ -1,92 +0,0 @@
1
- from .maxcut_tfim import maxcut_tfim
2
- import itertools
3
- import multiprocessing
4
- import os
5
-
6
-
7
- def evaluate_cut_edges(state, G):
8
- cut_edges = []
9
- cut_value = 0
10
- for u, v, data in G.edges(data=True):
11
- if ((state >> u) & 1) != ((state >> v) & 1):
12
- cut_edges.append((u, v))
13
- cut_value += data.get("weight", 1.0)
14
-
15
- return float(cut_value), 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
-
26
- # Parallelization by Elara (OpenAI custom GPT):
27
- def bootstrap_worker(args):
28
- theta, G, indices = args
29
- local_theta = theta.copy()
30
- flipped = []
31
- for i in indices:
32
- local_theta[i] = not local_theta[i]
33
- flipped.append(local_theta[i])
34
- energy = compute_energy(local_theta, G)
35
-
36
- return indices, energy, flipped
37
-
38
-
39
- # By Gemini (Google Search AI)
40
- def int_to_bitstring(integer, length):
41
- return (bin(integer)[2:].zfill(length))[::-1]
42
-
43
-
44
- def spin_glass_solver(G, quality=2, best_guess=None):
45
- bitstring = ''
46
- if isinstance(best_guess, str):
47
- bitstring = best_guess
48
- elif isinstance(best_guess, int):
49
- bitstring = int_to_bitstring(best_guess)
50
- elif isinstance(best_guess, list):
51
- bitstring = "".join(["1" if b else "0" for b in best_guess])
52
- else:
53
- cut_value, bitstring, cut_edges = maxcut_tfim(G, quality=0)
54
- best_theta = [ b == '1' for b in list(bitstring)]
55
- n_qubits = len(best_theta)
56
- min_energy = compute_energy(best_theta, G)
57
- improved = True
58
- while improved:
59
- improved = False
60
- for k in range(1, quality + 1):
61
- if n_qubits < k:
62
- break
63
-
64
- theta = best_theta.copy()
65
- args = []
66
-
67
- for combo in itertools.combinations(range(n_qubits), k):
68
- args.append((theta, G, combo))
69
-
70
- with multiprocessing.Pool(processes=os.cpu_count()) as pool:
71
- results = pool.map(bootstrap_worker, args)
72
-
73
- results.sort(key=lambda r: r[1])
74
- indices, energy, flipped = results[0]
75
- if energy < min_energy:
76
- min_energy = energy
77
- for i in range(len(indices)):
78
- best_theta[indices[i]] = flipped[i]
79
- improved = True
80
- break
81
-
82
- sample = 0
83
- bitstring = ""
84
- for i in range(len(best_theta)):
85
- b = best_theta[i]
86
- bitstring += '1' if b else '0'
87
- if b:
88
- sample |= 1 << i
89
-
90
- cut_value, cut_edges = evaluate_cut_edges(sample, G)
91
-
92
- return cut_value, bitstring, cut_edges, min_energy
File without changes
File without changes
File without changes