PyQrackIsing 1.8.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.
- {pyqrackising-1.8.0/PyQrackIsing.egg-info → pyqrackising-1.8.1}/PKG-INFO +1 -1
- pyqrackising-1.8.1/PyQrackIsing/spin_glass_solver.py +114 -0
- {pyqrackising-1.8.0 → pyqrackising-1.8.1/PyQrackIsing.egg-info}/PKG-INFO +1 -1
- {pyqrackising-1.8.0 → pyqrackising-1.8.1}/pyproject.toml +2 -1
- {pyqrackising-1.8.0 → pyqrackising-1.8.1}/setup.py +1 -1
- pyqrackising-1.8.0/PyQrackIsing/spin_glass_solver.py +0 -92
- {pyqrackising-1.8.0 → pyqrackising-1.8.1}/CMakeLists.txt +0 -0
- {pyqrackising-1.8.0 → pyqrackising-1.8.1}/LICENSE.md +0 -0
- {pyqrackising-1.8.0 → pyqrackising-1.8.1}/MANIFEST.in +0 -0
- {pyqrackising-1.8.0 → pyqrackising-1.8.1}/PyQrackIsing/__init__.py +0 -0
- {pyqrackising-1.8.0 → pyqrackising-1.8.1}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
- {pyqrackising-1.8.0 → pyqrackising-1.8.1}/PyQrackIsing/generate_tfim_samples.py +0 -0
- {pyqrackising-1.8.0 → pyqrackising-1.8.1}/PyQrackIsing/maxcut_tfim.py +0 -0
- {pyqrackising-1.8.0 → pyqrackising-1.8.1}/PyQrackIsing/tfim_magnetization.py +0 -0
- {pyqrackising-1.8.0 → pyqrackising-1.8.1}/PyQrackIsing/tfim_square_magnetization.py +0 -0
- {pyqrackising-1.8.0 → pyqrackising-1.8.1}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
- {pyqrackising-1.8.0 → pyqrackising-1.8.1}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
- {pyqrackising-1.8.0 → pyqrackising-1.8.1}/PyQrackIsing.egg-info/not-zip-safe +0 -0
- {pyqrackising-1.8.0 → pyqrackising-1.8.1}/PyQrackIsing.egg-info/top_level.txt +0 -0
- {pyqrackising-1.8.0 → pyqrackising-1.8.1}/README.md +0 -0
- {pyqrackising-1.8.0 → pyqrackising-1.8.1}/setup.cfg +0 -0
|
@@ -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
|
|
@@ -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.
|
|
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.8.
|
|
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,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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|