PyQrackIsing 8.10.0__py3-none-win_amd64.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 +12 -0
- pyqrackising/convert_tensor_network_to_tsp.py +97 -0
- pyqrackising/generate_tfim_samples.py +142 -0
- pyqrackising/kernels.cl +440 -0
- pyqrackising/maxcut_tfim.py +219 -0
- pyqrackising/maxcut_tfim_sparse.py +239 -0
- pyqrackising/maxcut_tfim_streaming.py +210 -0
- pyqrackising/maxcut_tfim_util.py +317 -0
- pyqrackising/spin_glass_solver.py +323 -0
- pyqrackising/spin_glass_solver_sparse.py +349 -0
- pyqrackising/spin_glass_solver_streaming.py +188 -0
- pyqrackising/spin_glass_solver_util.py +21 -0
- pyqrackising/tfim_magnetization.py +15 -0
- pyqrackising/tfim_square_magnetization.py +14 -0
- pyqrackising/tsp.py +1821 -0
- pyqrackising/tsp_maxcut.py +312 -0
- pyqrackising-8.10.0.dist-info/METADATA +27 -0
- pyqrackising-8.10.0.dist-info/RECORD +21 -0
- pyqrackising-8.10.0.dist-info/WHEEL +5 -0
- pyqrackising-8.10.0.dist-info/licenses/LICENSE.md +165 -0
- pyqrackising-8.10.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
from .maxcut_tfim_sparse import maxcut_tfim_sparse
|
|
2
|
+
from .maxcut_tfim_util import opencl_context
|
|
3
|
+
from .spin_glass_solver_util import get_cut_from_bit_array, int_to_bitstring
|
|
4
|
+
import itertools
|
|
5
|
+
import networkx as nx
|
|
6
|
+
import numpy as np
|
|
7
|
+
from numba import njit, prange
|
|
8
|
+
import os
|
|
9
|
+
import random
|
|
10
|
+
from scipy.sparse import lil_matrix, csr_matrix
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
IS_OPENCL_AVAILABLE = True
|
|
14
|
+
try:
|
|
15
|
+
import pyopencl as cl
|
|
16
|
+
except ImportError:
|
|
17
|
+
IS_OPENCL_AVAILABLE = False
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@njit
|
|
21
|
+
def evaluate_cut_edges(theta_bits, G_data, G_rows, G_cols):
|
|
22
|
+
n = G_rows.shape[0] - 1
|
|
23
|
+
cut = 0.0
|
|
24
|
+
for u in range(n):
|
|
25
|
+
for col in range(G_rows[u], G_rows[u + 1]):
|
|
26
|
+
v = G_cols[col]
|
|
27
|
+
if theta_bits[u] != theta_bits[v]:
|
|
28
|
+
cut += G_data[col]
|
|
29
|
+
|
|
30
|
+
return cut
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@njit
|
|
34
|
+
def compute_energy(sample, G_data, G_rows, G_cols):
|
|
35
|
+
n_qubits = G_rows.shape[0] - 1
|
|
36
|
+
energy = 0
|
|
37
|
+
for u in range(n_qubits):
|
|
38
|
+
for col in range(G_rows[u], G_rows[u + 1]):
|
|
39
|
+
v = G_cols[col]
|
|
40
|
+
energy += (G_data[col] if sample[u] == sample[v] else -G_data[col])
|
|
41
|
+
|
|
42
|
+
return energy
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@njit
|
|
46
|
+
def bootstrap_worker(theta, G_data, G_rows, G_cols, indices, is_spin_glass):
|
|
47
|
+
local_theta = theta.copy()
|
|
48
|
+
for i in indices:
|
|
49
|
+
local_theta[i] = not local_theta[i]
|
|
50
|
+
energy = compute_energy(local_theta, G_data, G_rows, G_cols) if is_spin_glass else -evaluate_cut_edges(local_theta, G_data, G_rows, G_cols)
|
|
51
|
+
|
|
52
|
+
return energy
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@njit(parallel=True)
|
|
56
|
+
def bootstrap(best_theta, G_data, G_rows, G_cols, indices_array, k, min_energy, dtype, is_spin_glass):
|
|
57
|
+
n = len(indices_array) // k
|
|
58
|
+
energies = np.empty(n, dtype=dtype)
|
|
59
|
+
for i in prange(n):
|
|
60
|
+
j = i * k
|
|
61
|
+
energies[i] = bootstrap_worker(best_theta, G_data, G_rows, G_cols, indices_array[j : j + k], is_spin_glass)
|
|
62
|
+
|
|
63
|
+
energy = energies.min()
|
|
64
|
+
if energy < min_energy:
|
|
65
|
+
index_match = np.random.choice(np.where(energies == energy)[0])
|
|
66
|
+
indices = indices_array[(index_match * k) : ((index_match + 1) * k)]
|
|
67
|
+
min_energy = energy
|
|
68
|
+
for i in indices:
|
|
69
|
+
best_theta[i] = not best_theta[i]
|
|
70
|
+
|
|
71
|
+
return min_energy
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices_array_np, k, min_energy, is_segmented, segment_size, is_spin_glass):
|
|
75
|
+
ctx = opencl_context.ctx
|
|
76
|
+
queue = opencl_context.queue
|
|
77
|
+
bootstrap_kernel = opencl_context.bootstrap_sparse_segmented_kernel if is_segmented else opencl_context.bootstrap_sparse_kernel
|
|
78
|
+
dtype = opencl_context.dtype
|
|
79
|
+
epsilon = opencl_context.epsilon
|
|
80
|
+
wgs = opencl_context.work_group_size
|
|
81
|
+
|
|
82
|
+
n = best_theta.shape[0]
|
|
83
|
+
combo_count = len(indices_array_np) // k
|
|
84
|
+
|
|
85
|
+
best_theta_np = np.array([(1 if b else 0) for b in best_theta], dtype=np.int8)
|
|
86
|
+
|
|
87
|
+
# Args: [n, k, combo_count, segment_size]
|
|
88
|
+
args_np = np.array([n, k, combo_count, is_spin_glass, segment_size], dtype=np.int32)
|
|
89
|
+
|
|
90
|
+
# Buffers
|
|
91
|
+
mf = cl.mem_flags
|
|
92
|
+
best_theta_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=best_theta_np)
|
|
93
|
+
indices_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=indices_array_np)
|
|
94
|
+
args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args_np)
|
|
95
|
+
|
|
96
|
+
# Local memory allocation (1 float per work item)
|
|
97
|
+
local_size = min(wgs, n)
|
|
98
|
+
max_global_size = ((opencl_context.MAX_GPU_PROC_ELEM + local_size - 1) // local_size) * local_size # corresponds to MAX_PROC_ELEM macro in OpenCL kernel program
|
|
99
|
+
global_size = min(((combo_count + local_size - 1) // local_size) * local_size, max_global_size)
|
|
100
|
+
local_energy_buf = cl.LocalMemory(np.dtype(dtype).itemsize * local_size)
|
|
101
|
+
local_index_buf = cl.LocalMemory(np.dtype(np.int32).itemsize * local_size)
|
|
102
|
+
|
|
103
|
+
# Allocate min_energy and min_index result buffers per workgroup
|
|
104
|
+
min_energy_host = np.empty(global_size, dtype=dtype)
|
|
105
|
+
min_index_host = np.empty(global_size, dtype=np.int32)
|
|
106
|
+
|
|
107
|
+
min_energy_buf = cl.Buffer(ctx, mf.WRITE_ONLY, min_energy_host.nbytes)
|
|
108
|
+
min_index_buf = cl.Buffer(ctx, mf.WRITE_ONLY, min_index_host.nbytes)
|
|
109
|
+
|
|
110
|
+
# Set kernel args
|
|
111
|
+
if is_segmented:
|
|
112
|
+
bootstrap_kernel.set_args(
|
|
113
|
+
np.random.randint(1<<32, dtype='uint32'),
|
|
114
|
+
G_data_buf[0],
|
|
115
|
+
G_data_buf[1],
|
|
116
|
+
G_data_buf[2],
|
|
117
|
+
G_data_buf[3],
|
|
118
|
+
G_rows_buf,
|
|
119
|
+
G_cols_buf,
|
|
120
|
+
best_theta_buf,
|
|
121
|
+
indices_buf,
|
|
122
|
+
args_buf,
|
|
123
|
+
min_energy_buf,
|
|
124
|
+
min_index_buf,
|
|
125
|
+
local_energy_buf,
|
|
126
|
+
local_index_buf
|
|
127
|
+
)
|
|
128
|
+
else:
|
|
129
|
+
bootstrap_kernel.set_args(
|
|
130
|
+
np.random.randint(1<<32, dtype='uint32'),
|
|
131
|
+
G_data_buf,
|
|
132
|
+
G_rows_buf,
|
|
133
|
+
G_cols_buf,
|
|
134
|
+
best_theta_buf,
|
|
135
|
+
indices_buf,
|
|
136
|
+
args_buf,
|
|
137
|
+
min_energy_buf,
|
|
138
|
+
min_index_buf,
|
|
139
|
+
local_energy_buf,
|
|
140
|
+
local_index_buf
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
cl.enqueue_nd_range_kernel(queue, bootstrap_kernel, (global_size,), (local_size,))
|
|
144
|
+
|
|
145
|
+
# Read results
|
|
146
|
+
cl.enqueue_copy(queue, min_energy_host, min_energy_buf)
|
|
147
|
+
cl.enqueue_copy(queue, min_index_host, min_index_buf)
|
|
148
|
+
queue.finish()
|
|
149
|
+
|
|
150
|
+
# Find global minimum
|
|
151
|
+
energy = min_energy_host.min()
|
|
152
|
+
if min_energy < energy:
|
|
153
|
+
return min_energy
|
|
154
|
+
|
|
155
|
+
atol = dtype(epsilon)
|
|
156
|
+
rtol = dtype(0)
|
|
157
|
+
choices = np.where(np.isclose(min_energy_host, energy, atol=atol, rtol=rtol))[0]
|
|
158
|
+
best_i = np.random.choice(choices) if len(choices) else np.argmin(min_energy_host)
|
|
159
|
+
|
|
160
|
+
flip_index_start = best_i * k
|
|
161
|
+
indices_to_flip = indices_array_np[flip_index_start : flip_index_start + k]
|
|
162
|
+
|
|
163
|
+
for i in indices_to_flip:
|
|
164
|
+
best_theta[i] = not best_theta[i]
|
|
165
|
+
|
|
166
|
+
return min_energy_host[best_i]
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def to_scipy_sparse_upper_triangular(G, nodes, n_nodes, dtype):
|
|
170
|
+
lil = lil_matrix((n_nodes, n_nodes), dtype=dtype)
|
|
171
|
+
for u in range(n_nodes):
|
|
172
|
+
u_node = nodes[u]
|
|
173
|
+
for v in range(u + 1, n_nodes):
|
|
174
|
+
v_node = nodes[v]
|
|
175
|
+
if G.has_edge(u_node, v_node):
|
|
176
|
+
lil[u, v] = G[u_node][v_node].get('weight', 1.0)
|
|
177
|
+
|
|
178
|
+
return lil.tocsr()
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def log_intermediate(theta, G_data, G_rows, G_cols, nodes, min_energy, is_spin_glass):
|
|
182
|
+
bitstring, l, r = get_cut_from_bit_array(theta, nodes)
|
|
183
|
+
if is_spin_glass:
|
|
184
|
+
cut_value = evaluate_cut_edges(theta, G_data, G_rows, G_cols)
|
|
185
|
+
else:
|
|
186
|
+
cut_value = -min_energy
|
|
187
|
+
min_energy = compute_energy(theta, G_data, G_rows, G_cols)
|
|
188
|
+
print(bitstring, float(cut_value), (l, r), float(min_energy))
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def spin_glass_solver_sparse(
|
|
192
|
+
G,
|
|
193
|
+
quality=None,
|
|
194
|
+
shots=None,
|
|
195
|
+
best_guess=None,
|
|
196
|
+
is_combo_maxcut_gpu=True,
|
|
197
|
+
is_spin_glass=True,
|
|
198
|
+
anneal_t=None,
|
|
199
|
+
anneal_h=None,
|
|
200
|
+
repulsion_base=None,
|
|
201
|
+
min_order=1,
|
|
202
|
+
max_order=None,
|
|
203
|
+
is_log=False,
|
|
204
|
+
reheat_tries=0
|
|
205
|
+
):
|
|
206
|
+
dtype = opencl_context.dtype
|
|
207
|
+
nodes = None
|
|
208
|
+
n_qubits = 0
|
|
209
|
+
G_m = None
|
|
210
|
+
if isinstance(G, nx.Graph):
|
|
211
|
+
nodes = list(G.nodes())
|
|
212
|
+
n_qubits = len(nodes)
|
|
213
|
+
G_m = to_scipy_sparse_upper_triangular(G, nodes, n_qubits, dtype)
|
|
214
|
+
else:
|
|
215
|
+
n_qubits = G.shape[0]
|
|
216
|
+
nodes = list(range(n_qubits))
|
|
217
|
+
G_m = G
|
|
218
|
+
|
|
219
|
+
if n_qubits < 3:
|
|
220
|
+
if n_qubits == 0:
|
|
221
|
+
return "", 0, ([], []), 0
|
|
222
|
+
|
|
223
|
+
if n_qubits == 1:
|
|
224
|
+
return "0", 0, (nodes, []), 0
|
|
225
|
+
|
|
226
|
+
if n_qubits == 2:
|
|
227
|
+
weight = G_m[0, 1]
|
|
228
|
+
if weight < 0.0:
|
|
229
|
+
return "00", 0, (nodes, []), weight
|
|
230
|
+
|
|
231
|
+
return "01", weight, ([nodes[0]], [nodes[1]]), -weight
|
|
232
|
+
|
|
233
|
+
segment_size = G_m.data.shape[0]
|
|
234
|
+
is_segmented = (G_m.data.nbytes << 1) > opencl_context.max_alloc
|
|
235
|
+
if is_segmented and is_alt_gpu_sampling:
|
|
236
|
+
print("[WARN] Using segmented solver, so disabling is_alt_gpu_sampling.")
|
|
237
|
+
is_alt_gpu_sampling = False
|
|
238
|
+
|
|
239
|
+
bitstring = ""
|
|
240
|
+
if isinstance(best_guess, str):
|
|
241
|
+
bitstring = best_guess
|
|
242
|
+
elif isinstance(best_guess, int):
|
|
243
|
+
bitstring = int_to_bitstring(best_guess, n_qubits)
|
|
244
|
+
elif isinstance(best_guess, list):
|
|
245
|
+
bitstring = "".join(["1" if b else "0" for b in best_guess])
|
|
246
|
+
else:
|
|
247
|
+
bitstring, _, _ = maxcut_tfim_sparse(G_m, quality=quality, shots=shots, is_spin_glass=is_spin_glass, anneal_t=anneal_t, anneal_h=anneal_h, repulsion_base=repulsion_base)
|
|
248
|
+
best_theta = np.array([b == "1" for b in list(bitstring)], dtype=np.bool_)
|
|
249
|
+
|
|
250
|
+
if is_combo_maxcut_gpu and IS_OPENCL_AVAILABLE:
|
|
251
|
+
if not (opencl_context.G_data_buf is None):
|
|
252
|
+
G_data_buf = opencl_context.G_data_buf
|
|
253
|
+
G_rows_buf = opencl_context.G_rows_buf
|
|
254
|
+
G_cols_buf = opencl_context.G_cols_buf
|
|
255
|
+
else:
|
|
256
|
+
mf = cl.mem_flags
|
|
257
|
+
ctx = opencl_context.ctx
|
|
258
|
+
G_rows_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m.indptr)
|
|
259
|
+
G_cols_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m.indices)
|
|
260
|
+
if is_segmented:
|
|
261
|
+
o_shape = segment_size
|
|
262
|
+
segment_size = (segment_size + 3) >> 2
|
|
263
|
+
n_shape = segment_size << 2
|
|
264
|
+
_G_data = np.reshape(G_m.data, (o_shape,))
|
|
265
|
+
if n_shape != o_shape:
|
|
266
|
+
np.resize(_G_data, (n_shape,))
|
|
267
|
+
_G_data_segments = np.split(_G_data, 4)
|
|
268
|
+
G_data_buf = [
|
|
269
|
+
cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=seg)
|
|
270
|
+
for seg in _G_data_segments
|
|
271
|
+
]
|
|
272
|
+
_G_data = None
|
|
273
|
+
_G_data_segments = None
|
|
274
|
+
else:
|
|
275
|
+
G_data_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m.data)
|
|
276
|
+
|
|
277
|
+
if max_order is None:
|
|
278
|
+
max_order = n_qubits
|
|
279
|
+
|
|
280
|
+
min_energy = compute_energy(best_theta, G_m.data, G_m.indptr, G_m.indices) if is_spin_glass else -evaluate_cut_edges(best_theta, G_m.data, G_m.indptr, G_m.indices)
|
|
281
|
+
|
|
282
|
+
if is_log:
|
|
283
|
+
log_intermediate(best_theta, G_m.data, G_m.indptr, G_m.indices, nodes, min_energy, is_spin_glass)
|
|
284
|
+
|
|
285
|
+
combos_list = []
|
|
286
|
+
reheat_theta = best_theta.copy()
|
|
287
|
+
reheat_min_energy = min_energy
|
|
288
|
+
for reheat_round in range(reheat_tries + 1):
|
|
289
|
+
improved = True
|
|
290
|
+
correction_quality = min_order
|
|
291
|
+
while improved:
|
|
292
|
+
improved = False
|
|
293
|
+
k = 1
|
|
294
|
+
while (k <= correction_quality) and (k <= max_order):
|
|
295
|
+
if n_qubits < k:
|
|
296
|
+
break
|
|
297
|
+
|
|
298
|
+
combos = []
|
|
299
|
+
if len(combos_list) < k:
|
|
300
|
+
combos = np.array(list(
|
|
301
|
+
item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist
|
|
302
|
+
))
|
|
303
|
+
combos_list.append(combos)
|
|
304
|
+
else:
|
|
305
|
+
combos = combos_list[k - 1]
|
|
306
|
+
|
|
307
|
+
if is_combo_maxcut_gpu and IS_OPENCL_AVAILABLE:
|
|
308
|
+
energy = run_bootstrap_opencl(reheat_theta, G_data_buf, G_rows_buf, G_cols_buf, combos, k, reheat_min_energy, is_segmented, segment_size, is_spin_glass)
|
|
309
|
+
else:
|
|
310
|
+
energy = bootstrap(reheat_theta, G_m.data, G_m.indptr, G_m.indices, combos, k, reheat_min_energy, dtype, is_spin_glass)
|
|
311
|
+
|
|
312
|
+
if energy < reheat_min_energy:
|
|
313
|
+
reheat_min_energy = energy
|
|
314
|
+
improved = True
|
|
315
|
+
if correction_quality < (k + 1):
|
|
316
|
+
correction_quality = k + 1
|
|
317
|
+
|
|
318
|
+
if is_log:
|
|
319
|
+
log_intermediate(reheat_theta, G_m.data, G_m.indptr, G_m.indices, nodes, min_energy, is_spin_glass)
|
|
320
|
+
|
|
321
|
+
break
|
|
322
|
+
|
|
323
|
+
k = k + 1
|
|
324
|
+
|
|
325
|
+
if min_energy < reheat_min_energy:
|
|
326
|
+
reheat_theta = best_theta.copy()
|
|
327
|
+
else:
|
|
328
|
+
best_theta = reheat_theta.copy()
|
|
329
|
+
min_energy = reheat_min_energy
|
|
330
|
+
|
|
331
|
+
if reheat_round < reheat_tries:
|
|
332
|
+
num_to_flip = int(np.round(np.log2(n_qubits)))
|
|
333
|
+
bits_to_flip = random.sample(list(range(n_qubits)), num_to_flip)
|
|
334
|
+
for bit in bits_to_flip:
|
|
335
|
+
reheat_theta[bit] = not reheat_theta[bit]
|
|
336
|
+
reheat_min_energy = compute_energy(reheat_theta, G_m.data, G_m.indptr, G_m.indices) if is_spin_glass else -evaluate_cut_edges(reheat_theta, G_m.data, G_m.indptr, G_m.indices)
|
|
337
|
+
|
|
338
|
+
opencl_context.G_data_buf = None
|
|
339
|
+
opencl_context.G_rows_buf = None
|
|
340
|
+
opencl_context.G_cols_buf = None
|
|
341
|
+
|
|
342
|
+
bitstring, l, r = get_cut_from_bit_array(best_theta, nodes)
|
|
343
|
+
if is_spin_glass:
|
|
344
|
+
cut_value = evaluate_cut_edges(best_theta, G_m.data, G_m.indptr, G_m.indices)
|
|
345
|
+
else:
|
|
346
|
+
cut_value = -min_energy
|
|
347
|
+
min_energy = compute_energy(best_theta, G_m.data, G_m.indptr, G_m.indices)
|
|
348
|
+
|
|
349
|
+
return bitstring, float(cut_value), (l, r), float(min_energy)
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
from .maxcut_tfim_streaming import maxcut_tfim_streaming
|
|
2
|
+
from .maxcut_tfim_util import opencl_context
|
|
3
|
+
from .spin_glass_solver_util import get_cut_from_bit_array, int_to_bitstring
|
|
4
|
+
import itertools
|
|
5
|
+
import networkx as nx
|
|
6
|
+
import numpy as np
|
|
7
|
+
from numba import njit, prange
|
|
8
|
+
import os
|
|
9
|
+
import random
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
epsilon = opencl_context.epsilon
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@njit
|
|
16
|
+
def evaluate_cut_edges(theta_bits, G_func, nodes):
|
|
17
|
+
n_qubits = len(nodes)
|
|
18
|
+
cut = 0
|
|
19
|
+
for u in range(n_qubits):
|
|
20
|
+
for v in range(u + 1, n_qubits):
|
|
21
|
+
if theta_bits[u] != theta_bits[v]:
|
|
22
|
+
cut += G_func(nodes[u], nodes[v])
|
|
23
|
+
|
|
24
|
+
return cut
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@njit
|
|
28
|
+
def compute_energy(theta_bits, G_func, nodes):
|
|
29
|
+
n_qubits = len(nodes)
|
|
30
|
+
energy = 0
|
|
31
|
+
for u in range(n_qubits):
|
|
32
|
+
for v in range(u + 1, n_qubits):
|
|
33
|
+
val = G_func(nodes[u], nodes[v])
|
|
34
|
+
energy += (val if theta_bits[u] == theta_bits[v] else -val)
|
|
35
|
+
|
|
36
|
+
return energy
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@njit
|
|
40
|
+
def bootstrap_worker(theta, G_func, nodes, indices, is_spin_glass):
|
|
41
|
+
local_theta = theta.copy()
|
|
42
|
+
for i in indices:
|
|
43
|
+
local_theta[i] = not local_theta[i]
|
|
44
|
+
energy = compute_energy(local_theta, G_func, nodes) if is_spin_glass else -evaluate_cut_edges(local_theta, G_func, nodes)
|
|
45
|
+
|
|
46
|
+
return energy
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@njit(parallel=True)
|
|
50
|
+
def bootstrap(best_theta, G_func, nodes, indices_array, k, min_energy, dtype, is_spin_glass):
|
|
51
|
+
n = len(indices_array) // k
|
|
52
|
+
energies = np.empty(n, dtype=dtype)
|
|
53
|
+
for i in prange(n):
|
|
54
|
+
j = i * k
|
|
55
|
+
energies[i] = bootstrap_worker(best_theta, G_func, nodes, indices_array[j : j + k], is_spin_glass)
|
|
56
|
+
|
|
57
|
+
energy = energies.min()
|
|
58
|
+
if energy < min_energy:
|
|
59
|
+
atol = dtype(epsilon)
|
|
60
|
+
rtol = dtype(0)
|
|
61
|
+
index_match = np.random.choice(np.where(np.isclose(energies, energy, atol=atol, rtol=rtol))[0])
|
|
62
|
+
indices = indices_array[(index_match * k) : ((index_match + 1) * k)]
|
|
63
|
+
min_energy = energies[index_match]
|
|
64
|
+
for i in indices:
|
|
65
|
+
best_theta[i] = not best_theta[i]
|
|
66
|
+
|
|
67
|
+
return min_energy
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def log_intermediate(theta, G_func, nodes, min_energy, is_spin_glass):
|
|
71
|
+
bitstring, l, r = get_cut_from_bit_array(theta, nodes)
|
|
72
|
+
if is_spin_glass:
|
|
73
|
+
cut_value = evaluate_cut_edges(theta, G_func, nodes)
|
|
74
|
+
else:
|
|
75
|
+
cut_value = -min_energy
|
|
76
|
+
min_energy = compute_energy(theta, G_func, nodes)
|
|
77
|
+
print(bitstring, float(cut_value), (l, r), float(min_energy))
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def spin_glass_solver_streaming(
|
|
81
|
+
G_func,
|
|
82
|
+
nodes,
|
|
83
|
+
quality=None,
|
|
84
|
+
shots=None,
|
|
85
|
+
best_guess=None,
|
|
86
|
+
is_spin_glass=True,
|
|
87
|
+
anneal_t=None,
|
|
88
|
+
anneal_h=None,
|
|
89
|
+
repulsion_base=None,
|
|
90
|
+
min_order=1,
|
|
91
|
+
max_order=None,
|
|
92
|
+
is_log=False,
|
|
93
|
+
reheat_tries=0
|
|
94
|
+
):
|
|
95
|
+
dtype = opencl_context.dtype
|
|
96
|
+
n_qubits = len(nodes)
|
|
97
|
+
|
|
98
|
+
if n_qubits < 3:
|
|
99
|
+
if n_qubits == 0:
|
|
100
|
+
return "", 0, ([], []), 0
|
|
101
|
+
|
|
102
|
+
if n_qubits == 1:
|
|
103
|
+
return "0", 0, (nodes, []), 0
|
|
104
|
+
|
|
105
|
+
if n_qubits == 2:
|
|
106
|
+
weight = G_func(nodes[0], nodes[1])
|
|
107
|
+
if weight < 0.0:
|
|
108
|
+
return "00", 0, (nodes, []), weight
|
|
109
|
+
|
|
110
|
+
return "01", weight, ([nodes[0]], [nodes[1]]), -weight
|
|
111
|
+
|
|
112
|
+
bitstring = ""
|
|
113
|
+
if isinstance(best_guess, str):
|
|
114
|
+
bitstring = best_guess
|
|
115
|
+
elif isinstance(best_guess, int):
|
|
116
|
+
bitstring = int_to_bitstring(best_guess, n_qubits)
|
|
117
|
+
elif isinstance(best_guess, list):
|
|
118
|
+
bitstring = "".join(["1" if b else "0" for b in best_guess])
|
|
119
|
+
else:
|
|
120
|
+
bitstring, _, _ = maxcut_tfim_streaming(G_func, nodes, quality=quality, shots=shots, is_spin_glass=is_spin_glass, anneal_t=anneal_t, anneal_h=anneal_h, repulsion_base=repulsion_base)
|
|
121
|
+
best_theta = np.array([b == "1" for b in list(bitstring)], dtype=np.bool_)
|
|
122
|
+
|
|
123
|
+
if max_order is None:
|
|
124
|
+
max_order = n_qubits
|
|
125
|
+
|
|
126
|
+
min_energy = compute_energy(best_theta, G_func, nodes) if is_spin_glass else -evaluate_cut_edges(best_theta, G_func, nodes)
|
|
127
|
+
|
|
128
|
+
if is_log:
|
|
129
|
+
log_intermediate(best_theta, G_func, nodes, min_energy, is_spin_glass)
|
|
130
|
+
|
|
131
|
+
combos_list = []
|
|
132
|
+
reheat_theta = best_theta.copy()
|
|
133
|
+
reheat_min_energy = min_energy
|
|
134
|
+
for reheat_round in range(reheat_tries + 1):
|
|
135
|
+
improved = True
|
|
136
|
+
correction_quality = min_order
|
|
137
|
+
while improved:
|
|
138
|
+
improved = False
|
|
139
|
+
k = 1
|
|
140
|
+
while (k <= correction_quality) and (k <= max_order):
|
|
141
|
+
if n_qubits < k:
|
|
142
|
+
break
|
|
143
|
+
|
|
144
|
+
combos = []
|
|
145
|
+
if len(combos_list) < k:
|
|
146
|
+
combos = np.array(list(
|
|
147
|
+
item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist
|
|
148
|
+
))
|
|
149
|
+
combos_list.append(combos)
|
|
150
|
+
else:
|
|
151
|
+
combos = combos_list[k - 1]
|
|
152
|
+
|
|
153
|
+
energy = bootstrap(best_theta, G_func, nodes, combos, k, min_energy, dtype, is_spin_glass)
|
|
154
|
+
|
|
155
|
+
if energy < reheat_min_energy:
|
|
156
|
+
reheat_min_energy = energy
|
|
157
|
+
improved = True
|
|
158
|
+
if correction_quality < (k + 1):
|
|
159
|
+
correction_quality = k + 1
|
|
160
|
+
|
|
161
|
+
if is_log:
|
|
162
|
+
log_intermediate(reheat_theta, G_func, nodes, min_energy, is_spin_glass)
|
|
163
|
+
|
|
164
|
+
break
|
|
165
|
+
|
|
166
|
+
k = k + 1
|
|
167
|
+
|
|
168
|
+
if min_energy < reheat_min_energy:
|
|
169
|
+
reheat_theta = best_theta.copy()
|
|
170
|
+
else:
|
|
171
|
+
best_theta = reheat_theta.copy()
|
|
172
|
+
min_energy = reheat_min_energy
|
|
173
|
+
|
|
174
|
+
if reheat_round < reheat_tries:
|
|
175
|
+
num_to_flip = int(np.round(np.log2(n_qubits)))
|
|
176
|
+
bits_to_flip = random.sample(list(range(n_qubits)), num_to_flip)
|
|
177
|
+
for bit in bits_to_flip:
|
|
178
|
+
reheat_theta[bit] = not reheat_theta[bit]
|
|
179
|
+
reheat_min_energy = compute_energy(reheat_theta, G_func, nodes) if is_spin_glass else -evaluate_cut_edges(reheat_theta, G_func, nodes)
|
|
180
|
+
|
|
181
|
+
bitstring, l, r = get_cut_from_bit_array(best_theta, nodes)
|
|
182
|
+
if is_spin_glass:
|
|
183
|
+
cut_value = evaluate_cut_edges(best_theta, G_func, nodes)
|
|
184
|
+
else:
|
|
185
|
+
cut_value = -min_energy
|
|
186
|
+
min_energy = compute_energy(best_theta, G_func, nodes)
|
|
187
|
+
|
|
188
|
+
return bitstring, float(cut_value), (l, r), float(min_energy)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from numba import njit
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
@njit
|
|
5
|
+
def get_cut_from_bit_array(theta, nodes):
|
|
6
|
+
bitstring = ""
|
|
7
|
+
l, r = [], []
|
|
8
|
+
for i in range(len(theta)):
|
|
9
|
+
b = theta[i]
|
|
10
|
+
if b:
|
|
11
|
+
bitstring += "1"
|
|
12
|
+
r.append(nodes[i])
|
|
13
|
+
else:
|
|
14
|
+
bitstring += "0"
|
|
15
|
+
l.append(nodes[i])
|
|
16
|
+
|
|
17
|
+
return bitstring, l, r
|
|
18
|
+
|
|
19
|
+
# By Gemini (Google Search AI)
|
|
20
|
+
def int_to_bitstring(integer, length):
|
|
21
|
+
return (bin(integer)[2:].zfill(length))[::-1]
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from .maxcut_tfim_util import probability_by_hamming_weight
|
|
2
|
+
from numba import njit
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
@njit
|
|
6
|
+
def tfim_magnetization(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56):
|
|
7
|
+
bias = probability_by_hamming_weight(J, h, z, theta, t, n_qubits + 1)
|
|
8
|
+
bias /= bias.sum()
|
|
9
|
+
magnetization = 0.0
|
|
10
|
+
nqs = int(n_qubits)
|
|
11
|
+
nqd = float(n_qubits)
|
|
12
|
+
for q in range(n_qubits + 1):
|
|
13
|
+
mag = (nqs - 2 * q) / nqd
|
|
14
|
+
magnetization += bias[q] * mag
|
|
15
|
+
return magnetization
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from .maxcut_tfim_util import probability_by_hamming_weight
|
|
2
|
+
from numba import njit
|
|
3
|
+
|
|
4
|
+
@njit
|
|
5
|
+
def tfim_square_magnetization(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56):
|
|
6
|
+
bias = probability_by_hamming_weight(J, h, z, theta, t, n_qubits + 1)
|
|
7
|
+
bias /= bias.sum()
|
|
8
|
+
square_magnetization = 0.0
|
|
9
|
+
nqs = int(n_qubits)
|
|
10
|
+
nqd = float(n_qubits)
|
|
11
|
+
for q in range(n_qubits + 1):
|
|
12
|
+
mag = (nqs - 2 * q) / nqd
|
|
13
|
+
square_magnetization += bias[q] * mag * mag
|
|
14
|
+
return square_magnetization
|