PyQrackIsing 9.17.0__py3-none-any.whl
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/__init__.py +22 -0
- pyqrackising/convert_tensor_network_to_tsp.py +93 -0
- pyqrackising/generate_tfim_samples.py +351 -0
- pyqrackising/kernels.cl +1238 -0
- pyqrackising/local_search.py +401 -0
- pyqrackising/maxcut_tfim.py +474 -0
- pyqrackising/maxcut_tfim_sparse.py +518 -0
- pyqrackising/maxcut_tfim_streaming.py +295 -0
- pyqrackising/maxcut_tfim_util.py +791 -0
- pyqrackising/otoc.py +248 -0
- pyqrackising/solve_maxcut_bnb.py +321 -0
- pyqrackising/solve_maxcut_bnb_sparse.py +322 -0
- pyqrackising/solve_maxcut_bnb_streaming.py +292 -0
- pyqrackising/solve_maxcut_exact.py +305 -0
- pyqrackising/solve_maxcut_exact_sparse.py +227 -0
- pyqrackising/solve_maxcut_exact_streaming.py +217 -0
- pyqrackising/solve_qubo.py +133 -0
- pyqrackising/spin_glass_solver.py +545 -0
- pyqrackising/spin_glass_solver_hybrid.py +53 -0
- pyqrackising/spin_glass_solver_sparse.py +611 -0
- pyqrackising/spin_glass_solver_streaming.py +294 -0
- pyqrackising/tfim_magnetization.py +23 -0
- pyqrackising/tfim_square_magnetization.py +22 -0
- pyqrackising/tsp.py +2014 -0
- pyqrackising/tsp_maxcut.py +312 -0
- pyqrackising-9.17.0.dist-info/METADATA +299 -0
- pyqrackising-9.17.0.dist-info/RECORD +30 -0
- pyqrackising-9.17.0.dist-info/WHEEL +5 -0
- pyqrackising-9.17.0.dist-info/licenses/LICENSE.md +165 -0
- pyqrackising-9.17.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import networkx as nx
|
|
2
|
+
import numpy as np
|
|
3
|
+
from numba import njit, prange
|
|
4
|
+
import os
|
|
5
|
+
import time
|
|
6
|
+
|
|
7
|
+
from .maxcut_tfim_util import (
|
|
8
|
+
compute_cut_streaming,
|
|
9
|
+
compute_energy_streaming,
|
|
10
|
+
compute_cut_diff_between_streaming,
|
|
11
|
+
get_cut,
|
|
12
|
+
get_cut_base,
|
|
13
|
+
heuristic_threshold,
|
|
14
|
+
init_thresholds,
|
|
15
|
+
maxcut_hamming_cdf,
|
|
16
|
+
opencl_context,
|
|
17
|
+
bit_pick_u,
|
|
18
|
+
resolve_seed,
|
|
19
|
+
rng_init,
|
|
20
|
+
rng_uniform,
|
|
21
|
+
sample_mag_u,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
epsilon = opencl_context.epsilon
|
|
26
|
+
dtype = opencl_context.dtype
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@njit(cache=True)
|
|
30
|
+
def update_repulsion_choice(G_func, nodes, weights, n, node, repulsion_base):
|
|
31
|
+
# Select node
|
|
32
|
+
weights[node] = 0.0
|
|
33
|
+
|
|
34
|
+
if abs(1.0 - repulsion_base) <= epsilon:
|
|
35
|
+
return
|
|
36
|
+
|
|
37
|
+
# Repulsion: penalize neighbors
|
|
38
|
+
for nbr in range(n):
|
|
39
|
+
if weights[nbr] == 0.0:
|
|
40
|
+
continue
|
|
41
|
+
weights[nbr] *= repulsion_base ** (-G_func(nodes[node], nodes[nbr]))
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
45
|
+
@njit(cache=True)
|
|
46
|
+
def local_repulsion_choice(G_func, nodes, repulsion_base, n, m, s, rng):
|
|
47
|
+
"""
|
|
48
|
+
Pick m nodes out of n with repulsion bias:
|
|
49
|
+
- High-degree nodes are already less likely
|
|
50
|
+
- After choosing a node, its neighbors' probabilities are further reduced
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
# First bit:
|
|
54
|
+
node = s % n
|
|
55
|
+
if m == 1:
|
|
56
|
+
used = np.zeros(n, dtype=np.bool_)
|
|
57
|
+
used[node] = True
|
|
58
|
+
return used
|
|
59
|
+
|
|
60
|
+
weights = np.ones(n, dtype=np.float64)
|
|
61
|
+
update_repulsion_choice(G_func, nodes, weights, n, node, repulsion_base)
|
|
62
|
+
|
|
63
|
+
for _ in range(1, m - 1):
|
|
64
|
+
rng, u = rng_uniform(rng)
|
|
65
|
+
node = bit_pick_u(weights, n, u)
|
|
66
|
+
update_repulsion_choice(G_func, nodes, weights, n, node, repulsion_base)
|
|
67
|
+
|
|
68
|
+
rng, u = rng_uniform(rng)
|
|
69
|
+
node = bit_pick_u(weights, n, u)
|
|
70
|
+
weights[node] = 0.0
|
|
71
|
+
|
|
72
|
+
return weights == 0.0
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@njit(parallel=True, cache=True)
|
|
76
|
+
def sample_round(G_func, nodes, shot_segment, thread_count, thresholds, n, repulsion_base, base_seed, rnd, solutions, energies):
|
|
77
|
+
for i in prange(thread_count):
|
|
78
|
+
s_offset = i * shot_segment
|
|
79
|
+
for j in range(shot_segment):
|
|
80
|
+
s = s_offset + j
|
|
81
|
+
rng = rng_init(base_seed, rnd, s)
|
|
82
|
+
|
|
83
|
+
# First dimension: Hamming weight
|
|
84
|
+
rng, u = rng_uniform(rng)
|
|
85
|
+
m = sample_mag_u(thresholds, u)
|
|
86
|
+
|
|
87
|
+
# Second dimension: permutation within Hamming weight
|
|
88
|
+
sample = local_repulsion_choice(G_func, nodes, repulsion_base, n, m, s, rng)
|
|
89
|
+
energy = compute_cut_diff_between_streaming(solutions[i], sample, G_func, nodes, n)
|
|
90
|
+
|
|
91
|
+
if energy > 0.0:
|
|
92
|
+
energies[i] += energy
|
|
93
|
+
solutions[i] = sample
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def sample_measurement(G_func, nodes, shots, thread_count, thresholds, n, repulsion_base, is_spin_glass, base_seed, deadline):
|
|
97
|
+
# Rounds run from Python so a time limit can stop between them.
|
|
98
|
+
shot_segment = (max(1, shots >> 1) + thread_count - 1) // thread_count
|
|
99
|
+
shots = shot_segment * thread_count
|
|
100
|
+
|
|
101
|
+
solutions = np.zeros((thread_count, n), dtype=np.bool_)
|
|
102
|
+
best_solution = solutions[0].copy()
|
|
103
|
+
if is_spin_glass:
|
|
104
|
+
best_energy = compute_energy_streaming(best_solution, G_func, nodes, n)
|
|
105
|
+
energies = np.full(thread_count, best_energy, dtype=dtype)
|
|
106
|
+
else:
|
|
107
|
+
best_energy = 0.0
|
|
108
|
+
energies = np.zeros(thread_count, dtype=dtype)
|
|
109
|
+
|
|
110
|
+
rnd = 0
|
|
111
|
+
improved = True
|
|
112
|
+
while improved and (rnd == 0 or time.perf_counter() < deadline):
|
|
113
|
+
improved = False
|
|
114
|
+
|
|
115
|
+
sample_round(G_func, nodes, shot_segment, thread_count, thresholds, n, repulsion_base, base_seed, rnd, solutions, energies)
|
|
116
|
+
rnd += 1
|
|
117
|
+
|
|
118
|
+
best_index = np.argmax(energies)
|
|
119
|
+
energy = energies[best_index]
|
|
120
|
+
|
|
121
|
+
if energy == float("inf"):
|
|
122
|
+
best_indices = []
|
|
123
|
+
for idx, e in enumerate(energies):
|
|
124
|
+
if e == float("inf"):
|
|
125
|
+
best_indices.append(idx)
|
|
126
|
+
best_index = best_indices[0]
|
|
127
|
+
for idx in best_indices[1:]:
|
|
128
|
+
e = compute_cut_diff_between_streaming(solutions[best_index], solutions[idx], G_func, nodes, n)
|
|
129
|
+
if e > 0.0:
|
|
130
|
+
best_index = idx
|
|
131
|
+
improved = True
|
|
132
|
+
best_solution = solutions[best_index].copy()
|
|
133
|
+
|
|
134
|
+
if is_spin_glass:
|
|
135
|
+
energy *= 2.0
|
|
136
|
+
|
|
137
|
+
if energy > best_energy:
|
|
138
|
+
best_energy = energy
|
|
139
|
+
best_solution = solutions[best_index].copy()
|
|
140
|
+
improved = True
|
|
141
|
+
|
|
142
|
+
return best_solution, best_energy
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
@njit(parallel=True, cache=True)
|
|
146
|
+
def init_J_and_z(G_func, nodes, G_min, repulsion_base):
|
|
147
|
+
n_qubits = len(nodes)
|
|
148
|
+
degrees = np.empty(n_qubits, dtype=np.uint32)
|
|
149
|
+
J_eff = np.empty(n_qubits, dtype=np.float64)
|
|
150
|
+
for n in prange(n_qubits):
|
|
151
|
+
degree = 0
|
|
152
|
+
J = 0.0
|
|
153
|
+
for m in range(n_qubits):
|
|
154
|
+
val = G_func(nodes[n], nodes[m]) - G_min
|
|
155
|
+
if val <= epsilon:
|
|
156
|
+
continue
|
|
157
|
+
degree += 1
|
|
158
|
+
J += val
|
|
159
|
+
if degree > 0:
|
|
160
|
+
J = -J / degree
|
|
161
|
+
degrees[n] = degree
|
|
162
|
+
J_eff[n] = J
|
|
163
|
+
|
|
164
|
+
return J_eff, degrees
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
@njit(cache=True)
|
|
168
|
+
def find_G_min(G_func, nodes, n_nodes):
|
|
169
|
+
G_min = float("inf")
|
|
170
|
+
for i in range(n_nodes):
|
|
171
|
+
u = nodes[i]
|
|
172
|
+
for j in range(i + 1, n_nodes):
|
|
173
|
+
v = nodes[j]
|
|
174
|
+
val = G_func(u, v)
|
|
175
|
+
if val < G_min:
|
|
176
|
+
G_min = val
|
|
177
|
+
|
|
178
|
+
return G_min
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
@njit(cache=True)
|
|
182
|
+
def exact_maxcut(G_func, n):
|
|
183
|
+
"""Brute-force exact MAXCUT solver using Numba JIT."""
|
|
184
|
+
max_cut = -1.0
|
|
185
|
+
best_mask = 0
|
|
186
|
+
|
|
187
|
+
# Enumerate all 2^n possible bitstrings
|
|
188
|
+
for mask in range(1 << n):
|
|
189
|
+
cut = 0.0
|
|
190
|
+
for i in range(n):
|
|
191
|
+
bi = (mask >> i) & 1
|
|
192
|
+
for j in range(i + 1, n):
|
|
193
|
+
if bi != ((mask >> j) & 1):
|
|
194
|
+
cut += G_func(i, j)
|
|
195
|
+
if cut > max_cut:
|
|
196
|
+
max_cut = cut
|
|
197
|
+
best_mask = mask
|
|
198
|
+
|
|
199
|
+
# Reconstruct best bitstring
|
|
200
|
+
best_bits = np.zeros(n, dtype=np.bool_)
|
|
201
|
+
for i in range(n):
|
|
202
|
+
best_bits[i] = (best_mask >> i) & 1
|
|
203
|
+
|
|
204
|
+
return best_bits, max_cut
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
@njit(cache=True)
|
|
208
|
+
def exact_spin_glass(G_func, n):
|
|
209
|
+
"""Brute-force exact spin-glass solver using Numba JIT."""
|
|
210
|
+
max_cut = -1.0
|
|
211
|
+
best_mask = 0
|
|
212
|
+
|
|
213
|
+
# Enumerate all 2^n possible bitstrings
|
|
214
|
+
for mask in range(1 << n):
|
|
215
|
+
cut = 0.0
|
|
216
|
+
for i in range(n):
|
|
217
|
+
bi = (mask >> i) & 1
|
|
218
|
+
for j in range(i + 1, n):
|
|
219
|
+
val = G_func(i, j)
|
|
220
|
+
cut += -val if bi == ((mask >> j) & 1) else val # spin-glass energy: +w across the cut (was reversed)
|
|
221
|
+
if cut > max_cut:
|
|
222
|
+
max_cut = cut
|
|
223
|
+
best_mask = mask
|
|
224
|
+
|
|
225
|
+
# Reconstruct best bitstring
|
|
226
|
+
best_bits = np.zeros(n, dtype=np.bool_)
|
|
227
|
+
for i in range(n):
|
|
228
|
+
best_bits[i] = (best_mask >> i) & 1
|
|
229
|
+
|
|
230
|
+
return best_bits, max_cut
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def maxcut_tfim_streaming(
|
|
234
|
+
G_func,
|
|
235
|
+
nodes,
|
|
236
|
+
quality=None,
|
|
237
|
+
shots=None,
|
|
238
|
+
is_spin_glass=False,
|
|
239
|
+
anneal_t=None,
|
|
240
|
+
anneal_h=None,
|
|
241
|
+
repulsion_base=None,
|
|
242
|
+
seed=None,
|
|
243
|
+
time_limit=None,
|
|
244
|
+
):
|
|
245
|
+
"""seed, time_limit: as for maxcut_tfim."""
|
|
246
|
+
deadline = float("inf") if time_limit is None else time.perf_counter() + time_limit
|
|
247
|
+
n_qubits = len(nodes)
|
|
248
|
+
|
|
249
|
+
if n_qubits < heuristic_threshold:
|
|
250
|
+
best_solution, best_value = exact_spin_glass(G_func, n_qubits) if is_spin_glass else exact_maxcut(G_func, n_qubits)
|
|
251
|
+
bit_string, l, r = get_cut(best_solution, nodes, n_qubits)
|
|
252
|
+
|
|
253
|
+
if best_value < 0.0:
|
|
254
|
+
# Best cut is trivial partition, all/empty
|
|
255
|
+
return "0" * n_qubits, 0.0, (nodes, [])
|
|
256
|
+
|
|
257
|
+
return bit_string, best_value, (l, r)
|
|
258
|
+
|
|
259
|
+
if quality is None:
|
|
260
|
+
quality = 6
|
|
261
|
+
|
|
262
|
+
if shots is None:
|
|
263
|
+
# Number of measurement shots
|
|
264
|
+
shots = n_qubits << quality
|
|
265
|
+
|
|
266
|
+
if anneal_t is None:
|
|
267
|
+
anneal_t = 8.0
|
|
268
|
+
|
|
269
|
+
if anneal_h is None:
|
|
270
|
+
anneal_h = 8.0
|
|
271
|
+
|
|
272
|
+
if repulsion_base is None:
|
|
273
|
+
repulsion_base = 5.0
|
|
274
|
+
|
|
275
|
+
G_min = find_G_min(G_func, nodes, n_qubits)
|
|
276
|
+
|
|
277
|
+
thread_count = os.cpu_count() ** 2
|
|
278
|
+
|
|
279
|
+
J_eff, degrees = init_J_and_z(G_func, nodes, G_min, repulsion_base)
|
|
280
|
+
cum_prob = maxcut_hamming_cdf(init_thresholds(n_qubits), n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
|
|
281
|
+
|
|
282
|
+
degrees = None
|
|
283
|
+
J_eff = None
|
|
284
|
+
|
|
285
|
+
best_solution, best_value = sample_measurement(
|
|
286
|
+
G_func, nodes, shots, thread_count, cum_prob, n_qubits, repulsion_base, is_spin_glass, resolve_seed(seed), deadline
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
bit_string, l, r = get_cut(best_solution, nodes, n_qubits)
|
|
290
|
+
|
|
291
|
+
if best_value < 0.0:
|
|
292
|
+
# Best cut is trivial partition, all/empty
|
|
293
|
+
return "0" * n_qubits, 0.0, (nodes, [])
|
|
294
|
+
|
|
295
|
+
return bit_string, best_value, (l, r)
|