PyQrackIsing 1.7.0__tar.gz → 1.8.1__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.7.0/PyQrackIsing.egg-info → pyqrackising-1.8.1}/PKG-INFO +1 -1
  2. pyqrackising-1.8.1/PyQrackIsing/spin_glass_solver.py +114 -0
  3. {pyqrackising-1.7.0 → pyqrackising-1.8.1/PyQrackIsing.egg-info}/PKG-INFO +1 -1
  4. {pyqrackising-1.7.0 → pyqrackising-1.8.1}/README.md +3 -3
  5. {pyqrackising-1.7.0 → pyqrackising-1.8.1}/pyproject.toml +2 -1
  6. {pyqrackising-1.7.0 → pyqrackising-1.8.1}/setup.py +1 -1
  7. pyqrackising-1.7.0/PyQrackIsing/spin_glass_solver.py +0 -98
  8. {pyqrackising-1.7.0 → pyqrackising-1.8.1}/CMakeLists.txt +0 -0
  9. {pyqrackising-1.7.0 → pyqrackising-1.8.1}/LICENSE.md +0 -0
  10. {pyqrackising-1.7.0 → pyqrackising-1.8.1}/MANIFEST.in +0 -0
  11. {pyqrackising-1.7.0 → pyqrackising-1.8.1}/PyQrackIsing/__init__.py +0 -0
  12. {pyqrackising-1.7.0 → pyqrackising-1.8.1}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
  13. {pyqrackising-1.7.0 → pyqrackising-1.8.1}/PyQrackIsing/generate_tfim_samples.py +0 -0
  14. {pyqrackising-1.7.0 → pyqrackising-1.8.1}/PyQrackIsing/maxcut_tfim.py +0 -0
  15. {pyqrackising-1.7.0 → pyqrackising-1.8.1}/PyQrackIsing/tfim_magnetization.py +0 -0
  16. {pyqrackising-1.7.0 → pyqrackising-1.8.1}/PyQrackIsing/tfim_square_magnetization.py +0 -0
  17. {pyqrackising-1.7.0 → pyqrackising-1.8.1}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
  18. {pyqrackising-1.7.0 → pyqrackising-1.8.1}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
  19. {pyqrackising-1.7.0 → pyqrackising-1.8.1}/PyQrackIsing.egg-info/not-zip-safe +0 -0
  20. {pyqrackising-1.7.0 → pyqrackising-1.8.1}/PyQrackIsing.egg-info/top_level.txt +0 -0
  21. {pyqrackising-1.7.0 → pyqrackising-1.8.1}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyQrackIsing
3
- Version: 1.7.0
3
+ Version: 1.8.1
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,114 @@
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
+ def evaluate_cut_edges(state, edge_keys, edge_values):
9
+ cut_edges = []
10
+ cut_value = 0
11
+ for i in range(len(edge_values)):
12
+ k = i << 1
13
+ u, v = edge_keys[k], edge_keys[k + 1]
14
+ if ((state >> u) & 1) != ((state >> v) & 1):
15
+ cut_edges.append((u, v))
16
+ cut_value += edge_values[i]
17
+
18
+ return float(cut_value), cut_edges
19
+
20
+
21
+ @njit
22
+ def compute_energy(theta_bits, edge_keys, edge_values):
23
+ energy = 0
24
+ for i in range(len(edge_values)):
25
+ k = i << 1
26
+ u, v = edge_keys[k], edge_keys[k + 1]
27
+ spin_u = 1 if theta_bits[u] else -1
28
+ spin_v = 1 if theta_bits[v] else -1
29
+ energy += edge_values[i] * spin_u * spin_v
30
+
31
+ return energy
32
+
33
+
34
+ @njit
35
+ def bootstrap_worker(theta, edge_keys, edge_values, indices):
36
+ local_theta = theta.copy()
37
+ for i in indices:
38
+ local_theta[i] = not local_theta[i]
39
+ energy = compute_energy(local_theta, edge_keys, edge_values)
40
+
41
+ return energy
42
+
43
+
44
+ @njit(parallel=True)
45
+ def bootstrap(theta, edge_keys, edge_values, k, indices_array):
46
+ n = len(indices_array) // k
47
+ energies = np.empty(n)
48
+ j = 0
49
+ for i in prange(n):
50
+ energies[i] = bootstrap_worker(theta, edge_keys, edge_values, indices_array[j:j+k])
51
+ j += k
52
+
53
+ return energies
54
+
55
+
56
+ # By Gemini (Google Search AI)
57
+ def int_to_bitstring(integer, length):
58
+ return (bin(integer)[2:].zfill(length))[::-1]
59
+
60
+
61
+ def spin_glass_solver(G, quality=2, best_guess=None):
62
+ bitstring = ''
63
+ if isinstance(best_guess, str):
64
+ bitstring = best_guess
65
+ elif isinstance(best_guess, int):
66
+ bitstring = int_to_bitstring(best_guess)
67
+ elif isinstance(best_guess, list):
68
+ bitstring = "".join(["1" if b else "0" for b in best_guess])
69
+ else:
70
+ cut_value, bitstring, cut_edges = maxcut_tfim(G, quality=0)
71
+ best_theta = [ b == '1' for b in list(bitstring)]
72
+ n_qubits = len(best_theta)
73
+
74
+ edge_keys = []
75
+ edge_values = []
76
+ for u, v, data in G.edges(data=True):
77
+ edge_keys.append(u)
78
+ edge_keys.append(v)
79
+ edge_values.append(data.get("weight", 1.0))
80
+
81
+ min_energy = compute_energy(best_theta, edge_keys, edge_values)
82
+ improved = True
83
+ while improved:
84
+ improved = False
85
+ for k in range(1, quality + 1):
86
+ if n_qubits < k:
87
+ break
88
+
89
+ theta = best_theta.copy()
90
+
91
+ combos = list(item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist)
92
+ energies = bootstrap(theta, edge_keys, edge_values, k, combos)
93
+
94
+ energy = min(energies)
95
+ if energy < min_energy:
96
+ index_match = np.random.choice(np.where(energies == energy)[0])
97
+ indices = combos[(index_match * k):((index_match + 1) * k)]
98
+ min_energy = energy
99
+ for i in indices:
100
+ best_theta[i] = not best_theta[i]
101
+ improved = True
102
+ break
103
+
104
+ sample = 0
105
+ bitstring = ""
106
+ for i in range(len(best_theta)):
107
+ b = best_theta[i]
108
+ bitstring += '1' if b else '0'
109
+ if b:
110
+ sample |= 1 << i
111
+
112
+ cut_value, cut_edges = evaluate_cut_edges(sample, edge_keys, edge_values)
113
+
114
+ 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.7.0
3
+ Version: 1.8.1
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
@@ -68,10 +68,10 @@ 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)
72
- cut_value, bitstring, cut_edges, min_energy = spin_glass_solver(G)
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)
73
73
  ```
74
- It 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.
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
75
 
76
76
  ## About
77
77
  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.7.0"
14
+ version = "1.8.1"
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.7.0',
54
+ version='1.8.1',
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,98 +0,0 @@
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):
37
- cut_value, bitstring, cut_edges = maxcut_tfim(G, quality=0)
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
File without changes
File without changes
File without changes