PyQrackIsing 4.3.0__tar.gz → 5.0.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-4.3.0/pyqrackising.egg-info → pyqrackising-5.0.1}/PKG-INFO +1 -1
- {pyqrackising-4.3.0 → pyqrackising-5.0.1}/README.md +1 -1
- {pyqrackising-4.3.0 → pyqrackising-5.0.1}/pyproject.toml +2 -1
- {pyqrackising-4.3.0 → pyqrackising-5.0.1}/pyqrackising/maxcut_tfim_sparse.py +50 -15
- {pyqrackising-4.3.0 → pyqrackising-5.0.1}/pyqrackising/spin_glass_solver_sparse.py +14 -5
- {pyqrackising-4.3.0 → pyqrackising-5.0.1/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-4.3.0 → pyqrackising-5.0.1}/setup.py +1 -1
- {pyqrackising-4.3.0 → pyqrackising-5.0.1}/CMakeLists.txt +0 -0
- {pyqrackising-4.3.0 → pyqrackising-5.0.1}/LICENSE.md +0 -0
- {pyqrackising-4.3.0 → pyqrackising-5.0.1}/MANIFEST.in +0 -0
- {pyqrackising-4.3.0 → pyqrackising-5.0.1}/pyqrackising/__init__.py +0 -0
- {pyqrackising-4.3.0 → pyqrackising-5.0.1}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-4.3.0 → pyqrackising-5.0.1}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-4.3.0 → pyqrackising-5.0.1}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-4.3.0 → pyqrackising-5.0.1}/pyqrackising/maxcut_tfim.py +0 -0
- {pyqrackising-4.3.0 → pyqrackising-5.0.1}/pyqrackising/spin_glass_solver.py +0 -0
- {pyqrackising-4.3.0 → pyqrackising-5.0.1}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-4.3.0 → pyqrackising-5.0.1}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-4.3.0 → pyqrackising-5.0.1}/pyqrackising/tsp.py +0 -0
- {pyqrackising-4.3.0 → pyqrackising-5.0.1}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-4.3.0 → pyqrackising-5.0.1}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-4.3.0 → pyqrackising-5.0.1}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-4.3.0 → pyqrackising-5.0.1}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-4.3.0 → pyqrackising-5.0.1}/setup.cfg +0 -0
|
@@ -76,7 +76,7 @@ G = generate_spin_glass_graph(n_nodes=64, seed=42)
|
|
|
76
76
|
solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=6, best_guess=None)
|
|
77
77
|
# solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, best_guess=maxcut_tfim(G, quality=6)[0])
|
|
78
78
|
```
|
|
79
|
-
We also provide `spin_glass_solver_sparse(G)`, for `scipy` CSR sparse arrays (or `networkx` graphs). The (integer) default `quality` setting is `6`. `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)` with passed-through `quality` 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.
|
|
79
|
+
We also provide `spin_glass_solver_sparse(G)`, for `scipy` **upper-triangular** CSR sparse arrays (or `networkx` graphs). The (integer) default `quality` setting is `6`. `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)` with passed-through `quality` 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.
|
|
80
80
|
|
|
81
81
|
From the `spin_glass_solver()`, we provide a (recursive) Traveling Salesman Problem (TSP) solver:
|
|
82
82
|
```py
|
|
@@ -6,12 +6,13 @@ requires = [
|
|
|
6
6
|
"networkx",
|
|
7
7
|
"numpy",
|
|
8
8
|
"numba",
|
|
9
|
+
"scipy"
|
|
9
10
|
]
|
|
10
11
|
build-backend = "setuptools.build_meta"
|
|
11
12
|
|
|
12
13
|
[project]
|
|
13
14
|
name = "pyqrackising"
|
|
14
|
-
version = "
|
|
15
|
+
version = "5.0.1"
|
|
15
16
|
requires-python = ">=3.8"
|
|
16
17
|
description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
|
|
17
18
|
readme = {file = "README.txt", content-type = "text/markdown"}
|
|
@@ -108,7 +108,7 @@ def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, hamming_prob):
|
|
|
108
108
|
|
|
109
109
|
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
110
110
|
@njit
|
|
111
|
-
def local_repulsion_choice(
|
|
111
|
+
def local_repulsion_choice(G_cols, G_data, G_rows, max_weight, weights, n, m):
|
|
112
112
|
"""
|
|
113
113
|
Pick m nodes out of n with repulsion bias:
|
|
114
114
|
- High-degree nodes are already less likely
|
|
@@ -153,10 +153,23 @@ def local_repulsion_choice(adjacency_data, adjacency_vals, adjacency_rows, max_w
|
|
|
153
153
|
mask[node] = True
|
|
154
154
|
|
|
155
155
|
# Repulsion: penalize neighbors
|
|
156
|
-
for j in range(
|
|
157
|
-
nbr =
|
|
156
|
+
for j in range(G_rows[node], G_rows[node + 1]):
|
|
157
|
+
nbr = G_cols[j]
|
|
158
158
|
if available[nbr]:
|
|
159
|
-
weights[nbr] *= 0.5 ** (
|
|
159
|
+
weights[nbr] *= 0.5 ** (G_data[j] / max_weight) # tunable penalty factor
|
|
160
|
+
|
|
161
|
+
for nbr in range(n - node, n):
|
|
162
|
+
if not available[nbr]:
|
|
163
|
+
continue
|
|
164
|
+
start = G_rows[nbr]
|
|
165
|
+
end = G_rows[nbr + 1]
|
|
166
|
+
j = start
|
|
167
|
+
while j < end:
|
|
168
|
+
if G_cols[j] == r:
|
|
169
|
+
break
|
|
170
|
+
j += 1
|
|
171
|
+
if j < end:
|
|
172
|
+
weights[nbr] *= 0.5 ** (G_data[j] / max_weight) # tunable penalty factor
|
|
160
173
|
|
|
161
174
|
return mask
|
|
162
175
|
|
|
@@ -168,8 +181,6 @@ def compute_energy(sample, G_data, G_rows, G_cols):
|
|
|
168
181
|
for u in range(n_qubits):
|
|
169
182
|
for col in range(G_rows[u], G_rows[u + 1]):
|
|
170
183
|
v = G_cols[col]
|
|
171
|
-
if v < (u + 1):
|
|
172
|
-
continue
|
|
173
184
|
energy += G_data[col] * (1 if sample[u] == sample[v] else -1)
|
|
174
185
|
|
|
175
186
|
return energy
|
|
@@ -203,8 +214,6 @@ def sample_for_solution(G_data, G_rows, G_cols, shots, thresholds, J_eff):
|
|
|
203
214
|
for u in range(n):
|
|
204
215
|
for col in range(G_rows[u], G_rows[u + 1]):
|
|
205
216
|
v = G_cols[col]
|
|
206
|
-
if v < (u + 1):
|
|
207
|
-
continue
|
|
208
217
|
if best_solution[u] != best_solution[v]:
|
|
209
218
|
best_value += G_data[col]
|
|
210
219
|
|
|
@@ -217,13 +226,27 @@ def init_J_and_z(G_data, G_rows, G_cols):
|
|
|
217
226
|
degrees = np.empty(n_qubits, dtype=np.uint32)
|
|
218
227
|
J_eff = np.empty(n_qubits, dtype=np.float64)
|
|
219
228
|
J_max = -float("inf")
|
|
220
|
-
for
|
|
221
|
-
|
|
222
|
-
|
|
229
|
+
for r in range(n_qubits):
|
|
230
|
+
# Row sum
|
|
231
|
+
start = G_rows[r]
|
|
232
|
+
end = G_rows[r + 1]
|
|
223
233
|
degree = end - start
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
234
|
+
val = G_data[start:end].sum()
|
|
235
|
+
|
|
236
|
+
J = -val / degree if degree > 0 else 0
|
|
237
|
+
degrees[r] += degree
|
|
238
|
+
J_eff[r] += J
|
|
239
|
+
|
|
240
|
+
# Column sum
|
|
241
|
+
for idx in range(start, end):
|
|
242
|
+
c = G_cols[idx]
|
|
243
|
+
degrees[c] += 1
|
|
244
|
+
J_eff[c] += G_data[idx]
|
|
245
|
+
|
|
246
|
+
for r in range(n_qubits):
|
|
247
|
+
J = J_eff[r]
|
|
248
|
+
degree = degrees[r]
|
|
249
|
+
J_eff[r] = -J / degree if degree > 0 else 0
|
|
227
250
|
J_abs = abs(J)
|
|
228
251
|
if J_abs > J_max:
|
|
229
252
|
J_max = J_abs
|
|
@@ -271,6 +294,18 @@ def init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees):
|
|
|
271
294
|
return theta
|
|
272
295
|
|
|
273
296
|
|
|
297
|
+
def to_scipy_sparse_upper_triangular(G, nodes, n_nodes):
|
|
298
|
+
lil = lil_matrix((n_nodes, n_nodes), dtype=np.float64)
|
|
299
|
+
for u in range(n_nodes):
|
|
300
|
+
u_node = nodes[u]
|
|
301
|
+
for v in range(u + 1, n_nodes):
|
|
302
|
+
v_node = nodes[v]
|
|
303
|
+
if G.has_edge(u_node, v_node):
|
|
304
|
+
lil[u, v] = G[u_node][v_node].get('weight', 1.0)
|
|
305
|
+
|
|
306
|
+
return lil.tocsr()
|
|
307
|
+
|
|
308
|
+
|
|
274
309
|
def maxcut_tfim_sparse(
|
|
275
310
|
G,
|
|
276
311
|
quality=None,
|
|
@@ -282,7 +317,7 @@ def maxcut_tfim_sparse(
|
|
|
282
317
|
if isinstance(G, nx.Graph):
|
|
283
318
|
nodes = list(G.nodes())
|
|
284
319
|
n_qubits = len(nodes)
|
|
285
|
-
G_m =
|
|
320
|
+
G_m = to_scipy_sparse_upper_triangular(G, nodes, n_qubits)
|
|
286
321
|
else:
|
|
287
322
|
n_qubits = G.shape[0]
|
|
288
323
|
nodes = list(range(n_qubits))
|
|
@@ -4,6 +4,7 @@ import networkx as nx
|
|
|
4
4
|
import numpy as np
|
|
5
5
|
from numba import njit, prange
|
|
6
6
|
import os
|
|
7
|
+
from scipy.sparse import lil_matrix, csr_matrix
|
|
7
8
|
|
|
8
9
|
|
|
9
10
|
@njit
|
|
@@ -13,8 +14,6 @@ def evaluate_cut_edges(theta_bits, G_data, G_rows, G_cols):
|
|
|
13
14
|
for u in range(n):
|
|
14
15
|
for col in range(G_rows[u], G_rows[u + 1]):
|
|
15
16
|
v = G_cols[col]
|
|
16
|
-
if v < (u + 1):
|
|
17
|
-
continue
|
|
18
17
|
if theta_bits[u] != theta_bits[v]:
|
|
19
18
|
cut += G_data[col]
|
|
20
19
|
|
|
@@ -28,8 +27,6 @@ def compute_energy(sample, G_data, G_rows, G_cols):
|
|
|
28
27
|
for u in range(n_qubits):
|
|
29
28
|
for col in range(G_rows[u], G_rows[u + 1]):
|
|
30
29
|
v = G_cols[col]
|
|
31
|
-
if v < (u + 1):
|
|
32
|
-
continue
|
|
33
30
|
energy += G_data[col] * (1 if sample[u] == sample[v] else -1)
|
|
34
31
|
|
|
35
32
|
return energy
|
|
@@ -56,6 +53,18 @@ def bootstrap(theta, G_data, G_rows, G_cols, k, indices_array):
|
|
|
56
53
|
return energies
|
|
57
54
|
|
|
58
55
|
|
|
56
|
+
def to_scipy_sparse_upper_triangular(G, nodes, n_nodes):
|
|
57
|
+
lil = lil_matrix((n_nodes, n_nodes), dtype=np.float64)
|
|
58
|
+
for u in range(n_nodes):
|
|
59
|
+
u_node = nodes[u]
|
|
60
|
+
for v in range(u + 1, n_nodes):
|
|
61
|
+
v_node = nodes[v]
|
|
62
|
+
if G.has_edge(u_node, v_node):
|
|
63
|
+
lil[u, v] = G[u_node][v_node].get('weight', 1.0)
|
|
64
|
+
|
|
65
|
+
return lil.tocsr()
|
|
66
|
+
|
|
67
|
+
|
|
59
68
|
# By Gemini (Google Search AI)
|
|
60
69
|
def int_to_bitstring(integer, length):
|
|
61
70
|
return (bin(integer)[2:].zfill(length))[::-1]
|
|
@@ -68,7 +77,7 @@ def spin_glass_solver_sparse(G, quality=6, shots=None, best_guess=None):
|
|
|
68
77
|
if isinstance(G, nx.Graph):
|
|
69
78
|
nodes = list(G.nodes())
|
|
70
79
|
n_qubits = len(nodes)
|
|
71
|
-
G_m =
|
|
80
|
+
G_m = to_scipy_sparse_upper_triangular(G, nodes, n_qubits)
|
|
72
81
|
else:
|
|
73
82
|
n_qubits = G.shape[0]
|
|
74
83
|
nodes = list(range(n_qubits))
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='pyqrackising',
|
|
54
|
-
version='
|
|
54
|
+
version='5.0.1',
|
|
55
55
|
author='Dan Strano',
|
|
56
56
|
author_email='stranoj@gmail.com',
|
|
57
57
|
description='Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)',
|
|
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
|
|
File without changes
|
|
File without changes
|