PyQrackIsing 8.8.0__tar.gz → 8.9.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-8.8.0/pyqrackising.egg-info → pyqrackising-8.9.1}/PKG-INFO +1 -1
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/README.md +2 -2
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/pyproject.toml +1 -1
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/pyqrackising/kernels.cl +37 -14
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/pyqrackising/spin_glass_solver.py +76 -48
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/pyqrackising/spin_glass_solver_sparse.py +79 -47
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/pyqrackising/spin_glass_solver_streaming.py +72 -44
- {pyqrackising-8.8.0 → pyqrackising-8.9.1/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/setup.py +1 -1
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/CMakeLists.txt +0 -0
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/LICENSE.md +0 -0
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/MANIFEST.in +0 -0
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/pyqrackising/__init__.py +0 -0
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/pyqrackising/maxcut_tfim.py +0 -0
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/pyqrackising/maxcut_tfim_sparse.py +0 -0
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/pyqrackising/maxcut_tfim_streaming.py +0 -0
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/pyqrackising/maxcut_tfim_util.py +0 -0
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/pyqrackising/spin_glass_solver_util.py +0 -0
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/pyqrackising/tsp.py +0 -0
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/pyqrackising/tsp_maxcut.py +0 -0
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-8.8.0 → pyqrackising-8.9.1}/setup.cfg +0 -0
|
@@ -73,10 +73,10 @@ def generate_spin_glass_graph(n_nodes=16, degree=3, seed=None):
|
|
|
73
73
|
|
|
74
74
|
|
|
75
75
|
G = generate_spin_glass_graph(n_nodes=64, seed=42)
|
|
76
|
-
solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=4, shots=None, anneal_t=8.0, anneal_h=8.0, best_guess=None, is_combo_maxcut_gpu=True, min_order=1, max_order=None)
|
|
76
|
+
solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=4, shots=None, anneal_t=8.0, anneal_h=8.0, best_guess=None, is_combo_maxcut_gpu=True, min_order=1, max_order=None, reheat_tries=0)
|
|
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)` and `spin_glass_solver_streaming(G_func, nodes)`. `is_combo_maxcut_gpu` controls whether gradient descent optimization is done on GPU (which is costly and the only GPU-based feature). `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. `min_order` determines the minimum order of local search and defaults to `1`, meaning it will perform a local search on at least single nodes (or `2` for pairs of nodes, or `3` for triplets of nodes, etc). `max_order` sets a ceiling on adaptive search for productive local search "order," and defaults to `None` for no ceiling. 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)` and `spin_glass_solver_streaming(G_func, nodes)`. `is_combo_maxcut_gpu` controls whether gradient descent optimization is done on GPU (which is costly and the only GPU-based feature). `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. `min_order` determines the minimum order of local search and defaults to `1`, meaning it will perform a local search on at least single nodes (or `2` for pairs of nodes, or `3` for triplets of nodes, etc). `max_order` sets a ceiling on adaptive search for productive local search "order," and defaults to `None` for no ceiling. After the initial optimization round, `reheat_tries` will randomly flip `log2(n)` bits out of `n` count of total nodes and re-optimize, similar to "simulated annealing," for as many additional rounds as the `reheat_tries` value (which defaults to `0`). 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 MAXCUT solvers, we provide a (recursive) Traveling Salesman Problem (TSP) solver:
|
|
82
82
|
```py
|
|
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
|
|
|
12
12
|
|
|
13
13
|
[project]
|
|
14
14
|
name = "pyqrackising"
|
|
15
|
-
version = "8.
|
|
15
|
+
version = "8.9.1"
|
|
16
16
|
requires-python = ">=3.8"
|
|
17
17
|
description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
|
|
18
18
|
readme = {file = "README.txt", content-type = "text/markdown"}
|
|
@@ -15,7 +15,7 @@ inline uint xorshift32(uint *state) {
|
|
|
15
15
|
return x;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
real1 bootstrap_worker(__constant char* theta, __global const real1* G_m, __constant int* indices, const int k, const int n) {
|
|
18
|
+
real1 bootstrap_worker(__constant char* theta, __global const real1* G_m, __constant int* indices, const int k, const int n, const bool is_spin_glass) {
|
|
19
19
|
real1 energy = ZERO_R1;
|
|
20
20
|
const size_t n_st = (size_t)n;
|
|
21
21
|
for (int u = 0; u < n; ++u) {
|
|
@@ -36,7 +36,11 @@ real1 bootstrap_worker(__constant char* theta, __global const real1* G_m, __cons
|
|
|
36
36
|
break;
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
|
-
|
|
39
|
+
if (u_bit != v_bit) {
|
|
40
|
+
energy -= val;
|
|
41
|
+
} else if (is_spin_glass) {
|
|
42
|
+
energy += val;
|
|
43
|
+
}
|
|
40
44
|
}
|
|
41
45
|
}
|
|
42
46
|
|
|
@@ -57,6 +61,7 @@ __kernel void bootstrap(
|
|
|
57
61
|
const int n = args[0];
|
|
58
62
|
const int k = args[1];
|
|
59
63
|
const int combo_count = args[2];
|
|
64
|
+
const bool is_spin_glass = args[3];
|
|
60
65
|
int i = get_global_id(0);
|
|
61
66
|
|
|
62
67
|
// The inputs are chaotic, and this doesn't need to be high-quality, just uniform.
|
|
@@ -67,7 +72,7 @@ __kernel void bootstrap(
|
|
|
67
72
|
|
|
68
73
|
for (; i < combo_count; i += MAX_PROC_ELEM) {
|
|
69
74
|
const int j = i * k;
|
|
70
|
-
const real1 energy = bootstrap_worker(best_theta, G_m, indices_array + j, k, n);
|
|
75
|
+
const real1 energy = bootstrap_worker(best_theta, G_m, indices_array + j, k, n, is_spin_glass);
|
|
71
76
|
if (energy < best_energy) {
|
|
72
77
|
best_energy = energy;
|
|
73
78
|
best_i = i;
|
|
@@ -104,7 +109,7 @@ __kernel void bootstrap(
|
|
|
104
109
|
}
|
|
105
110
|
}
|
|
106
111
|
|
|
107
|
-
real1 bootstrap_worker_sparse(__constant char* theta, __global const real1* G_data, __global const uint* G_rows, __global const uint* G_cols, __constant int* indices, const int k, const int n) {
|
|
112
|
+
real1 bootstrap_worker_sparse(__constant char* theta, __global const real1* G_data, __global const uint* G_rows, __global const uint* G_cols, __constant int* indices, const int k, const int n, const bool is_spin_glass) {
|
|
108
113
|
real1 energy = ZERO_R1;
|
|
109
114
|
for (int u = 0; u < n; ++u) {
|
|
110
115
|
bool u_bit = theta[u];
|
|
@@ -125,7 +130,11 @@ real1 bootstrap_worker_sparse(__constant char* theta, __global const real1* G_da
|
|
|
125
130
|
break;
|
|
126
131
|
}
|
|
127
132
|
}
|
|
128
|
-
|
|
133
|
+
if (u_bit != v_bit) {
|
|
134
|
+
energy -= val;
|
|
135
|
+
} else if (is_spin_glass) {
|
|
136
|
+
energy += val;
|
|
137
|
+
}
|
|
129
138
|
}
|
|
130
139
|
}
|
|
131
140
|
|
|
@@ -148,6 +157,7 @@ __kernel void bootstrap_sparse(
|
|
|
148
157
|
const int n = args[0];
|
|
149
158
|
const int k = args[1];
|
|
150
159
|
const int combo_count = args[2];
|
|
160
|
+
const bool is_spin_glass = args[3];
|
|
151
161
|
int i = get_global_id(0);
|
|
152
162
|
|
|
153
163
|
prng_seed ^= (uint)i;
|
|
@@ -157,7 +167,7 @@ __kernel void bootstrap_sparse(
|
|
|
157
167
|
|
|
158
168
|
for (; i < combo_count; i += MAX_PROC_ELEM) {
|
|
159
169
|
const int j = i * k;
|
|
160
|
-
const real1 energy = bootstrap_worker_sparse(best_theta, G_data, G_rows, G_cols, indices_array + j, k, n);
|
|
170
|
+
const real1 energy = bootstrap_worker_sparse(best_theta, G_data, G_rows, G_cols, indices_array + j, k, n, is_spin_glass);
|
|
161
171
|
if (energy < best_energy) {
|
|
162
172
|
best_energy = energy;
|
|
163
173
|
best_i = i;
|
|
@@ -209,7 +219,8 @@ real1 bootstrap_worker_segmented(
|
|
|
209
219
|
__constant int* indices,
|
|
210
220
|
const int k,
|
|
211
221
|
const int n,
|
|
212
|
-
const int segment_size
|
|
222
|
+
const int segment_size,
|
|
223
|
+
const bool is_spin_glass
|
|
213
224
|
) {
|
|
214
225
|
real1 energy = ZERO_R1;
|
|
215
226
|
const size_t n_st = (size_t)n;
|
|
@@ -234,7 +245,12 @@ real1 bootstrap_worker_segmented(
|
|
|
234
245
|
break;
|
|
235
246
|
}
|
|
236
247
|
}
|
|
237
|
-
|
|
248
|
+
|
|
249
|
+
if (u_bit != v_bit) {
|
|
250
|
+
energy -= val;
|
|
251
|
+
} else if (is_spin_glass) {
|
|
252
|
+
energy += val;
|
|
253
|
+
}
|
|
238
254
|
}
|
|
239
255
|
}
|
|
240
256
|
|
|
@@ -261,7 +277,8 @@ __kernel void bootstrap_segmented(
|
|
|
261
277
|
const int n = args[0];
|
|
262
278
|
const int k = args[1];
|
|
263
279
|
const int combo_count = args[2];
|
|
264
|
-
const
|
|
280
|
+
const bool is_spin_glass = args[3];
|
|
281
|
+
const int segment_size = args[4];
|
|
265
282
|
int i = get_global_id(0);
|
|
266
283
|
|
|
267
284
|
prng_seed ^= (uint)i;
|
|
@@ -271,7 +288,7 @@ __kernel void bootstrap_segmented(
|
|
|
271
288
|
|
|
272
289
|
for (; i < combo_count; i += MAX_PROC_ELEM) {
|
|
273
290
|
const int j = i * k;
|
|
274
|
-
const real1 energy = bootstrap_worker_segmented(best_theta, G_m, indices_array + j, k, n, segment_size);
|
|
291
|
+
const real1 energy = bootstrap_worker_segmented(best_theta, G_m, indices_array + j, k, n, segment_size, is_spin_glass);
|
|
275
292
|
if (energy < best_energy) {
|
|
276
293
|
best_energy = energy;
|
|
277
294
|
best_i = i;
|
|
@@ -314,7 +331,8 @@ real1 bootstrap_worker_sparse_segmented(
|
|
|
314
331
|
__constant int* indices,
|
|
315
332
|
const int k,
|
|
316
333
|
const int n,
|
|
317
|
-
const int segment_size
|
|
334
|
+
const int segment_size,
|
|
335
|
+
const bool is_spin_glass
|
|
318
336
|
) {
|
|
319
337
|
real1 energy = ZERO_R1;
|
|
320
338
|
|
|
@@ -342,7 +360,11 @@ real1 bootstrap_worker_sparse_segmented(
|
|
|
342
360
|
}
|
|
343
361
|
}
|
|
344
362
|
|
|
345
|
-
|
|
363
|
+
if (u_bit != v_bit) {
|
|
364
|
+
energy -= val;
|
|
365
|
+
} else if (is_spin_glass) {
|
|
366
|
+
energy += val;
|
|
367
|
+
}
|
|
346
368
|
}
|
|
347
369
|
}
|
|
348
370
|
|
|
@@ -370,7 +392,8 @@ __kernel void bootstrap_sparse_segmented(
|
|
|
370
392
|
const int n = args[0];
|
|
371
393
|
const int k = args[1];
|
|
372
394
|
const int combo_count = args[2];
|
|
373
|
-
const
|
|
395
|
+
const bool is_spin_glass = args[3];
|
|
396
|
+
const int segment_size = args[4];
|
|
374
397
|
int i = get_global_id(0);
|
|
375
398
|
|
|
376
399
|
prng_seed ^= (uint)i;
|
|
@@ -380,7 +403,7 @@ __kernel void bootstrap_sparse_segmented(
|
|
|
380
403
|
|
|
381
404
|
for (; i < combo_count; i += MAX_PROC_ELEM) {
|
|
382
405
|
const int j = i * k;
|
|
383
|
-
const real1 energy = bootstrap_worker_sparse_segmented(best_theta, G_data, G_rows, G_cols, indices_array + j, k, n, segment_size);
|
|
406
|
+
const real1 energy = bootstrap_worker_sparse_segmented(best_theta, G_data, G_rows, G_cols, indices_array + j, k, n, segment_size, is_spin_glass);
|
|
384
407
|
if (energy < best_energy) {
|
|
385
408
|
best_energy = energy;
|
|
386
409
|
best_i = i;
|
|
@@ -6,6 +6,7 @@ import networkx as nx
|
|
|
6
6
|
import numpy as np
|
|
7
7
|
from numba import njit, prange
|
|
8
8
|
import os
|
|
9
|
+
import random
|
|
9
10
|
|
|
10
11
|
|
|
11
12
|
IS_OPENCL_AVAILABLE = True
|
|
@@ -39,22 +40,22 @@ def compute_energy(theta_bits, G_m):
|
|
|
39
40
|
|
|
40
41
|
|
|
41
42
|
@njit
|
|
42
|
-
def bootstrap_worker(theta, G_m, indices):
|
|
43
|
+
def bootstrap_worker(theta, G_m, indices, is_spin_glass):
|
|
43
44
|
local_theta = theta.copy()
|
|
44
45
|
for i in indices:
|
|
45
46
|
local_theta[i] = not local_theta[i]
|
|
46
|
-
energy = compute_energy(local_theta, G_m)
|
|
47
|
+
energy = compute_energy(local_theta, G_m) if is_spin_glass else -evaluate_cut_edges(local_theta, G_m)
|
|
47
48
|
|
|
48
49
|
return energy
|
|
49
50
|
|
|
50
51
|
|
|
51
52
|
@njit(parallel=True)
|
|
52
|
-
def bootstrap(best_theta, G_m, indices_array, k, min_energy, dtype):
|
|
53
|
+
def bootstrap(best_theta, G_m, indices_array, k, min_energy, dtype, is_spin_glass):
|
|
53
54
|
n = len(indices_array) // k
|
|
54
55
|
energies = np.empty(n, dtype=dtype)
|
|
55
56
|
for i in prange(n):
|
|
56
57
|
j = i * k
|
|
57
|
-
energies[i] = bootstrap_worker(best_theta, G_m, indices_array[j : j + k])
|
|
58
|
+
energies[i] = bootstrap_worker(best_theta, G_m, indices_array[j : j + k], is_spin_glass)
|
|
58
59
|
|
|
59
60
|
energy = energies.min()
|
|
60
61
|
if energy < min_energy:
|
|
@@ -67,7 +68,7 @@ def bootstrap(best_theta, G_m, indices_array, k, min_energy, dtype):
|
|
|
67
68
|
return min_energy
|
|
68
69
|
|
|
69
70
|
|
|
70
|
-
def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy, is_segmented, segment_size):
|
|
71
|
+
def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy, is_segmented, segment_size, is_spin_glass):
|
|
71
72
|
ctx = opencl_context.ctx
|
|
72
73
|
queue = opencl_context.queue
|
|
73
74
|
bootstrap_kernel = opencl_context.bootstrap_segmented_kernel if is_segmented else opencl_context.bootstrap_kernel
|
|
@@ -81,7 +82,7 @@ def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy, i
|
|
|
81
82
|
best_theta_np = np.array([(1 if b else 0) for b in best_theta], dtype=np.int8)
|
|
82
83
|
|
|
83
84
|
# Args: [n, k, combo_count, segment_size]
|
|
84
|
-
args_np = np.array([n, k, combo_count, segment_size], dtype=np.int32)
|
|
85
|
+
args_np = np.array([n, k, combo_count, is_spin_glass, segment_size], dtype=np.int32)
|
|
85
86
|
|
|
86
87
|
# Buffers
|
|
87
88
|
mf = cl.mem_flags
|
|
@@ -158,6 +159,16 @@ def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy, i
|
|
|
158
159
|
return min_energy_host[best_i]
|
|
159
160
|
|
|
160
161
|
|
|
162
|
+
def log_intermediate(theta, G_m, nodes, min_energy, is_spin_glass):
|
|
163
|
+
bitstring, l, r = get_cut_from_bit_array(theta, nodes)
|
|
164
|
+
if is_spin_glass:
|
|
165
|
+
cut_value = evaluate_cut_edges(theta, G_m)
|
|
166
|
+
else:
|
|
167
|
+
cut_value = -min_energy
|
|
168
|
+
min_energy = compute_energy(theta, G_m)
|
|
169
|
+
print(bitstring, float(cut_value), (l, r), float(min_energy))
|
|
170
|
+
|
|
171
|
+
|
|
161
172
|
def spin_glass_solver(
|
|
162
173
|
G,
|
|
163
174
|
quality=None,
|
|
@@ -170,7 +181,8 @@ def spin_glass_solver(
|
|
|
170
181
|
repulsion_base=None,
|
|
171
182
|
min_order=1,
|
|
172
183
|
max_order=None,
|
|
173
|
-
is_log=False
|
|
184
|
+
is_log=False,
|
|
185
|
+
reheat_tries=0
|
|
174
186
|
):
|
|
175
187
|
dtype = opencl_context.dtype
|
|
176
188
|
nodes = None
|
|
@@ -242,54 +254,70 @@ def spin_glass_solver(
|
|
|
242
254
|
if max_order is None:
|
|
243
255
|
max_order = n_qubits
|
|
244
256
|
|
|
245
|
-
min_energy = compute_energy(best_theta, G_m)
|
|
257
|
+
min_energy = compute_energy(best_theta, G_m) if is_spin_glass else -evaluate_cut_edges(best_theta, G_m)
|
|
246
258
|
|
|
247
259
|
if is_log:
|
|
248
|
-
|
|
249
|
-
cut_value = evaluate_cut_edges(best_theta, G_m)
|
|
250
|
-
print(bitstring, float(cut_value), (l, r), float(min_energy))
|
|
260
|
+
log_intermediate(best_theta, G_m, nodes, min_energy, is_spin_glass)
|
|
251
261
|
|
|
252
|
-
improved = True
|
|
253
|
-
correction_quality = min_order
|
|
254
262
|
combos_list = []
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
263
|
+
reheat_theta = best_theta.copy()
|
|
264
|
+
reheat_min_energy = min_energy
|
|
265
|
+
for reheat_round in range(reheat_tries + 1):
|
|
266
|
+
improved = True
|
|
267
|
+
correction_quality = min_order
|
|
268
|
+
while improved:
|
|
269
|
+
improved = False
|
|
270
|
+
k = 1
|
|
271
|
+
while (k <= correction_quality) and (k <= max_order):
|
|
272
|
+
if n_qubits < k:
|
|
273
|
+
break
|
|
274
|
+
|
|
275
|
+
combos = []
|
|
276
|
+
if len(combos_list) < k:
|
|
277
|
+
combos = np.array(list(
|
|
278
|
+
item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist
|
|
279
|
+
))
|
|
280
|
+
combos_list.append(combos)
|
|
281
|
+
else:
|
|
282
|
+
combos = combos_list[k - 1]
|
|
283
|
+
|
|
284
|
+
if is_combo_maxcut_gpu and IS_OPENCL_AVAILABLE:
|
|
285
|
+
energy = run_bootstrap_opencl(reheat_theta, G_m_buf, combos, k, reheat_min_energy, is_segmented, segment_size, is_spin_glass)
|
|
286
|
+
else:
|
|
287
|
+
energy = bootstrap(reheat_theta, G_m, combos, k, reheat_min_energy, dtype, is_spin_glass)
|
|
288
|
+
|
|
289
|
+
if energy < reheat_min_energy:
|
|
290
|
+
reheat_min_energy = energy
|
|
291
|
+
improved = True
|
|
292
|
+
if correction_quality < (k + 1):
|
|
293
|
+
correction_quality = k + 1
|
|
294
|
+
if is_log:
|
|
295
|
+
log_intermediate(reheat_theta, G_m, nodes, reheat_min_energy, is_spin_glass)
|
|
296
|
+
|
|
297
|
+
break
|
|
298
|
+
|
|
299
|
+
k = k + 1
|
|
300
|
+
|
|
301
|
+
if min_energy < reheat_min_energy:
|
|
302
|
+
reheat_theta = best_theta.copy()
|
|
303
|
+
else:
|
|
304
|
+
best_theta = reheat_theta.copy()
|
|
305
|
+
min_energy = reheat_min_energy
|
|
286
306
|
|
|
287
|
-
|
|
307
|
+
if reheat_round < reheat_tries:
|
|
308
|
+
num_to_flip = int(np.round(np.log2(n_qubits)))
|
|
309
|
+
bits_to_flip = random.sample(list(range(n_qubits)), num_to_flip)
|
|
310
|
+
for bit in bits_to_flip:
|
|
311
|
+
reheat_theta[bit] = not reheat_theta[bit]
|
|
312
|
+
reheat_min_energy = compute_energy(reheat_theta, G_m) if is_spin_glass else -evaluate_cut_edges(reheat_theta, G_m)
|
|
288
313
|
|
|
289
|
-
|
|
314
|
+
opencl_context.G_m_buf = None
|
|
290
315
|
|
|
291
316
|
bitstring, l, r = get_cut_from_bit_array(best_theta, nodes)
|
|
292
|
-
|
|
293
|
-
|
|
317
|
+
if is_spin_glass:
|
|
318
|
+
cut_value = evaluate_cut_edges(best_theta, G_m)
|
|
319
|
+
else:
|
|
320
|
+
cut_value = -min_energy
|
|
321
|
+
min_energy = compute_energy(best_theta, G_m)
|
|
294
322
|
|
|
295
323
|
return bitstring, float(cut_value), (l, r), float(min_energy)
|
|
@@ -6,6 +6,7 @@ import networkx as nx
|
|
|
6
6
|
import numpy as np
|
|
7
7
|
from numba import njit, prange
|
|
8
8
|
import os
|
|
9
|
+
import random
|
|
9
10
|
from scipy.sparse import lil_matrix, csr_matrix
|
|
10
11
|
|
|
11
12
|
|
|
@@ -42,22 +43,22 @@ def compute_energy(sample, G_data, G_rows, G_cols):
|
|
|
42
43
|
|
|
43
44
|
|
|
44
45
|
@njit
|
|
45
|
-
def bootstrap_worker(theta, G_data, G_rows, G_cols, indices):
|
|
46
|
+
def bootstrap_worker(theta, G_data, G_rows, G_cols, indices, is_spin_glass):
|
|
46
47
|
local_theta = theta.copy()
|
|
47
48
|
for i in indices:
|
|
48
49
|
local_theta[i] = not local_theta[i]
|
|
49
|
-
energy = compute_energy(local_theta, G_data, G_rows, G_cols)
|
|
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)
|
|
50
51
|
|
|
51
52
|
return energy
|
|
52
53
|
|
|
53
54
|
|
|
54
55
|
@njit(parallel=True)
|
|
55
|
-
def bootstrap(best_theta, G_data, G_rows, G_cols, indices_array, k, min_energy, dtype):
|
|
56
|
+
def bootstrap(best_theta, G_data, G_rows, G_cols, indices_array, k, min_energy, dtype, is_spin_glass):
|
|
56
57
|
n = len(indices_array) // k
|
|
57
58
|
energies = np.empty(n, dtype=dtype)
|
|
58
59
|
for i in prange(n):
|
|
59
60
|
j = i * k
|
|
60
|
-
energies[i] = bootstrap_worker(best_theta, G_data, G_rows, G_cols, indices_array[j : j + k])
|
|
61
|
+
energies[i] = bootstrap_worker(best_theta, G_data, G_rows, G_cols, indices_array[j : j + k], is_spin_glass)
|
|
61
62
|
|
|
62
63
|
energy = energies.min()
|
|
63
64
|
if energy < min_energy:
|
|
@@ -70,7 +71,7 @@ def bootstrap(best_theta, G_data, G_rows, G_cols, indices_array, k, min_energy,
|
|
|
70
71
|
return min_energy
|
|
71
72
|
|
|
72
73
|
|
|
73
|
-
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):
|
|
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):
|
|
74
75
|
ctx = opencl_context.ctx
|
|
75
76
|
queue = opencl_context.queue
|
|
76
77
|
bootstrap_kernel = opencl_context.bootstrap_sparse_segmented_kernel if is_segmented else opencl_context.bootstrap_sparse_kernel
|
|
@@ -84,7 +85,7 @@ def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices
|
|
|
84
85
|
best_theta_np = np.array([(1 if b else 0) for b in best_theta], dtype=np.int8)
|
|
85
86
|
|
|
86
87
|
# Args: [n, k, combo_count, segment_size]
|
|
87
|
-
args_np = np.array([n, k, combo_count, segment_size], dtype=np.int32)
|
|
88
|
+
args_np = np.array([n, k, combo_count, is_spin_glass, segment_size], dtype=np.int32)
|
|
88
89
|
|
|
89
90
|
# Buffers
|
|
90
91
|
mf = cl.mem_flags
|
|
@@ -177,6 +178,16 @@ def to_scipy_sparse_upper_triangular(G, nodes, n_nodes, dtype):
|
|
|
177
178
|
return lil.tocsr()
|
|
178
179
|
|
|
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
|
+
|
|
180
191
|
def spin_glass_solver_sparse(
|
|
181
192
|
G,
|
|
182
193
|
quality=None,
|
|
@@ -189,7 +200,8 @@ def spin_glass_solver_sparse(
|
|
|
189
200
|
repulsion_base=None,
|
|
190
201
|
min_order=1,
|
|
191
202
|
max_order=None,
|
|
192
|
-
is_log=False
|
|
203
|
+
is_log=False,
|
|
204
|
+
reheat_tries=0
|
|
193
205
|
):
|
|
194
206
|
dtype = opencl_context.dtype
|
|
195
207
|
nodes = None
|
|
@@ -265,53 +277,73 @@ def spin_glass_solver_sparse(
|
|
|
265
277
|
if max_order is None:
|
|
266
278
|
max_order = n_qubits
|
|
267
279
|
|
|
268
|
-
min_energy = compute_energy(best_theta, G_m.data, G_m.indptr, G_m.indices)
|
|
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)
|
|
269
281
|
|
|
270
282
|
if is_log:
|
|
271
|
-
|
|
272
|
-
cut_value = evaluate_cut_edges(best_theta, G_m.data, G_m.indptr, G_m.indices)
|
|
273
|
-
print(bitstring, float(cut_value), (l, r), float(min_energy))
|
|
283
|
+
log_intermediate(best_theta, G_m.data, G_m.indptr, G_m.indices, nodes, min_energy, is_spin_glass)
|
|
274
284
|
|
|
275
|
-
improved = True
|
|
276
|
-
correction_quality = min_order
|
|
277
285
|
combos_list = []
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
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
|
|
309
330
|
|
|
310
|
-
|
|
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)
|
|
311
337
|
|
|
312
|
-
|
|
338
|
+
opencl_context.G_data_buf = None
|
|
339
|
+
opencl_context.G_rows_buf = None
|
|
340
|
+
opencl_context.G_cols_buf = None
|
|
313
341
|
|
|
314
342
|
bitstring, l, r = get_cut_from_bit_array(best_theta, nodes)
|
|
315
|
-
|
|
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)
|
|
316
348
|
|
|
317
349
|
return bitstring, float(cut_value), (l, r), float(min_energy)
|
|
@@ -6,6 +6,7 @@ import networkx as nx
|
|
|
6
6
|
import numpy as np
|
|
7
7
|
from numba import njit, prange
|
|
8
8
|
import os
|
|
9
|
+
import random
|
|
9
10
|
|
|
10
11
|
|
|
11
12
|
epsilon = opencl_context.epsilon
|
|
@@ -36,22 +37,22 @@ def compute_energy(theta_bits, G_func, nodes):
|
|
|
36
37
|
|
|
37
38
|
|
|
38
39
|
@njit
|
|
39
|
-
def bootstrap_worker(theta, G_func, nodes, indices):
|
|
40
|
+
def bootstrap_worker(theta, G_func, nodes, indices, is_spin_glass):
|
|
40
41
|
local_theta = theta.copy()
|
|
41
42
|
for i in indices:
|
|
42
43
|
local_theta[i] = not local_theta[i]
|
|
43
|
-
energy = compute_energy(local_theta, G_func, nodes)
|
|
44
|
+
energy = compute_energy(local_theta, G_func, nodes) if is_spin_glass else -evaluate_cut_edges(local_theta, G_func, nodes)
|
|
44
45
|
|
|
45
46
|
return energy
|
|
46
47
|
|
|
47
48
|
|
|
48
49
|
@njit(parallel=True)
|
|
49
|
-
def bootstrap(best_theta, G_func, nodes, indices_array, k, min_energy, dtype):
|
|
50
|
+
def bootstrap(best_theta, G_func, nodes, indices_array, k, min_energy, dtype, is_spin_glass):
|
|
50
51
|
n = len(indices_array) // k
|
|
51
52
|
energies = np.empty(n, dtype=dtype)
|
|
52
53
|
for i in prange(n):
|
|
53
54
|
j = i * k
|
|
54
|
-
energies[i] = bootstrap_worker(best_theta, G_func, nodes, indices_array[j : j + k])
|
|
55
|
+
energies[i] = bootstrap_worker(best_theta, G_func, nodes, indices_array[j : j + k], is_spin_glass)
|
|
55
56
|
|
|
56
57
|
energy = energies.min()
|
|
57
58
|
if energy < min_energy:
|
|
@@ -66,6 +67,16 @@ def bootstrap(best_theta, G_func, nodes, indices_array, k, min_energy, dtype):
|
|
|
66
67
|
return min_energy
|
|
67
68
|
|
|
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
|
+
|
|
69
80
|
def spin_glass_solver_streaming(
|
|
70
81
|
G_func,
|
|
71
82
|
nodes,
|
|
@@ -78,7 +89,8 @@ def spin_glass_solver_streaming(
|
|
|
78
89
|
repulsion_base=None,
|
|
79
90
|
min_order=1,
|
|
80
91
|
max_order=None,
|
|
81
|
-
is_log=False
|
|
92
|
+
is_log=False,
|
|
93
|
+
reheat_tries=0
|
|
82
94
|
):
|
|
83
95
|
dtype = opencl_context.dtype
|
|
84
96
|
n_qubits = len(nodes)
|
|
@@ -111,50 +123,66 @@ def spin_glass_solver_streaming(
|
|
|
111
123
|
if max_order is None:
|
|
112
124
|
max_order = n_qubits
|
|
113
125
|
|
|
114
|
-
min_energy = compute_energy(best_theta, G_func, nodes)
|
|
126
|
+
min_energy = compute_energy(best_theta, G_func, nodes) if is_spin_glass else -evaluate_cut_edges(best_theta, G_func, nodes)
|
|
115
127
|
|
|
116
128
|
if is_log:
|
|
117
|
-
|
|
118
|
-
cut_value = evaluate_cut_edges(best_theta, G_func, nodes)
|
|
119
|
-
print(bitstring, float(cut_value), (l, r), float(min_energy))
|
|
129
|
+
log_intermediate(best_theta, G_func, nodes, min_energy, is_spin_glass)
|
|
120
130
|
|
|
121
|
-
improved = True
|
|
122
|
-
correction_quality = min_order
|
|
123
131
|
combos_list = []
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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)
|
|
156
180
|
|
|
157
181
|
bitstring, l, r = get_cut_from_bit_array(best_theta, nodes)
|
|
158
|
-
|
|
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)
|
|
159
187
|
|
|
160
188
|
return bitstring, float(cut_value), (l, r), float(min_energy)
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='pyqrackising',
|
|
54
|
-
version='8.
|
|
54
|
+
version='8.9.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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|