PyQrackIsing 8.9.0__tar.gz → 8.9.2__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.9.0/pyqrackising.egg-info → pyqrackising-8.9.2}/PKG-INFO +1 -1
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/README.md +2 -2
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/pyproject.toml +1 -1
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/pyqrackising/kernels.cl +37 -14
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/pyqrackising/spin_glass_solver.py +29 -19
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/pyqrackising/spin_glass_solver_sparse.py +31 -17
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/pyqrackising/spin_glass_solver_streaming.py +24 -14
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/pyqrackising/tsp.py +4 -4
- {pyqrackising-8.9.0 → pyqrackising-8.9.2/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/setup.py +1 -1
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/CMakeLists.txt +0 -0
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/LICENSE.md +0 -0
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/MANIFEST.in +0 -0
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/pyqrackising/__init__.py +0 -0
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/pyqrackising/maxcut_tfim.py +0 -0
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/pyqrackising/maxcut_tfim_sparse.py +0 -0
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/pyqrackising/maxcut_tfim_streaming.py +0 -0
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/pyqrackising/maxcut_tfim_util.py +0 -0
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/pyqrackising/spin_glass_solver_util.py +0 -0
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/pyqrackising/tsp_maxcut.py +0 -0
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-8.9.0 → pyqrackising-8.9.2}/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.9.
|
|
15
|
+
version = "8.9.2"
|
|
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;
|
|
@@ -40,22 +40,22 @@ def compute_energy(theta_bits, G_m):
|
|
|
40
40
|
|
|
41
41
|
|
|
42
42
|
@njit
|
|
43
|
-
def bootstrap_worker(theta, G_m, indices):
|
|
43
|
+
def bootstrap_worker(theta, G_m, indices, is_spin_glass):
|
|
44
44
|
local_theta = theta.copy()
|
|
45
45
|
for i in indices:
|
|
46
46
|
local_theta[i] = not local_theta[i]
|
|
47
|
-
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)
|
|
48
48
|
|
|
49
49
|
return energy
|
|
50
50
|
|
|
51
51
|
|
|
52
52
|
@njit(parallel=True)
|
|
53
|
-
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):
|
|
54
54
|
n = len(indices_array) // k
|
|
55
55
|
energies = np.empty(n, dtype=dtype)
|
|
56
56
|
for i in prange(n):
|
|
57
57
|
j = i * k
|
|
58
|
-
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)
|
|
59
59
|
|
|
60
60
|
energy = energies.min()
|
|
61
61
|
if energy < min_energy:
|
|
@@ -68,7 +68,7 @@ def bootstrap(best_theta, G_m, indices_array, k, min_energy, dtype):
|
|
|
68
68
|
return min_energy
|
|
69
69
|
|
|
70
70
|
|
|
71
|
-
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):
|
|
72
72
|
ctx = opencl_context.ctx
|
|
73
73
|
queue = opencl_context.queue
|
|
74
74
|
bootstrap_kernel = opencl_context.bootstrap_segmented_kernel if is_segmented else opencl_context.bootstrap_kernel
|
|
@@ -82,7 +82,7 @@ def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy, i
|
|
|
82
82
|
best_theta_np = np.array([(1 if b else 0) for b in best_theta], dtype=np.int8)
|
|
83
83
|
|
|
84
84
|
# Args: [n, k, combo_count, segment_size]
|
|
85
|
-
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)
|
|
86
86
|
|
|
87
87
|
# Buffers
|
|
88
88
|
mf = cl.mem_flags
|
|
@@ -159,6 +159,16 @@ def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy, i
|
|
|
159
159
|
return min_energy_host[best_i]
|
|
160
160
|
|
|
161
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
|
+
|
|
162
172
|
def spin_glass_solver(
|
|
163
173
|
G,
|
|
164
174
|
quality=None,
|
|
@@ -244,12 +254,10 @@ def spin_glass_solver(
|
|
|
244
254
|
if max_order is None:
|
|
245
255
|
max_order = n_qubits
|
|
246
256
|
|
|
247
|
-
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)
|
|
248
258
|
|
|
249
259
|
if is_log:
|
|
250
|
-
|
|
251
|
-
cut_value = evaluate_cut_edges(best_theta, G_m)
|
|
252
|
-
print(bitstring, float(cut_value), (l, r), float(min_energy))
|
|
260
|
+
log_intermediate(best_theta, G_m, nodes, min_energy, is_spin_glass)
|
|
253
261
|
|
|
254
262
|
combos_list = []
|
|
255
263
|
reheat_theta = best_theta.copy()
|
|
@@ -274,20 +282,17 @@ def spin_glass_solver(
|
|
|
274
282
|
combos = combos_list[k - 1]
|
|
275
283
|
|
|
276
284
|
if is_combo_maxcut_gpu and IS_OPENCL_AVAILABLE:
|
|
277
|
-
energy = run_bootstrap_opencl(reheat_theta, G_m_buf, combos, k, reheat_min_energy, is_segmented, segment_size)
|
|
285
|
+
energy = run_bootstrap_opencl(reheat_theta, G_m_buf, combos, k, reheat_min_energy, is_segmented, segment_size, is_spin_glass)
|
|
278
286
|
else:
|
|
279
|
-
energy = bootstrap(reheat_theta, G_m, combos, k, reheat_min_energy, dtype)
|
|
287
|
+
energy = bootstrap(reheat_theta, G_m, combos, k, reheat_min_energy, dtype, is_spin_glass)
|
|
280
288
|
|
|
281
289
|
if energy < reheat_min_energy:
|
|
282
290
|
reheat_min_energy = energy
|
|
283
291
|
improved = True
|
|
284
292
|
if correction_quality < (k + 1):
|
|
285
293
|
correction_quality = k + 1
|
|
286
|
-
|
|
287
294
|
if is_log:
|
|
288
|
-
|
|
289
|
-
cut_value = evaluate_cut_edges(reheat_theta, G_m)
|
|
290
|
-
print(bitstring, float(cut_value), (l, r), float(reheat_min_energy))
|
|
295
|
+
log_intermediate(reheat_theta, G_m, nodes, reheat_min_energy, is_spin_glass)
|
|
291
296
|
|
|
292
297
|
break
|
|
293
298
|
|
|
@@ -304,10 +309,15 @@ def spin_glass_solver(
|
|
|
304
309
|
bits_to_flip = random.sample(list(range(n_qubits)), num_to_flip)
|
|
305
310
|
for bit in bits_to_flip:
|
|
306
311
|
reheat_theta[bit] = not reheat_theta[bit]
|
|
307
|
-
reheat_min_energy = compute_energy(reheat_theta, G_m)
|
|
312
|
+
reheat_min_energy = compute_energy(reheat_theta, G_m) if is_spin_glass else -evaluate_cut_edges(reheat_theta, G_m)
|
|
308
313
|
|
|
309
|
-
bitstring, l, r = get_cut_from_bit_array(best_theta, nodes)
|
|
310
|
-
cut_value = evaluate_cut_edges(best_theta, G_m)
|
|
311
314
|
opencl_context.G_m_buf = None
|
|
312
315
|
|
|
316
|
+
bitstring, l, r = get_cut_from_bit_array(best_theta, nodes)
|
|
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)
|
|
322
|
+
|
|
313
323
|
return bitstring, float(cut_value), (l, r), float(min_energy)
|
|
@@ -43,22 +43,22 @@ def compute_energy(sample, G_data, G_rows, G_cols):
|
|
|
43
43
|
|
|
44
44
|
|
|
45
45
|
@njit
|
|
46
|
-
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):
|
|
47
47
|
local_theta = theta.copy()
|
|
48
48
|
for i in indices:
|
|
49
49
|
local_theta[i] = not local_theta[i]
|
|
50
|
-
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)
|
|
51
51
|
|
|
52
52
|
return energy
|
|
53
53
|
|
|
54
54
|
|
|
55
55
|
@njit(parallel=True)
|
|
56
|
-
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):
|
|
57
57
|
n = len(indices_array) // k
|
|
58
58
|
energies = np.empty(n, dtype=dtype)
|
|
59
59
|
for i in prange(n):
|
|
60
60
|
j = i * k
|
|
61
|
-
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)
|
|
62
62
|
|
|
63
63
|
energy = energies.min()
|
|
64
64
|
if energy < min_energy:
|
|
@@ -71,7 +71,7 @@ def bootstrap(best_theta, G_data, G_rows, G_cols, indices_array, k, min_energy,
|
|
|
71
71
|
return min_energy
|
|
72
72
|
|
|
73
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):
|
|
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
75
|
ctx = opencl_context.ctx
|
|
76
76
|
queue = opencl_context.queue
|
|
77
77
|
bootstrap_kernel = opencl_context.bootstrap_sparse_segmented_kernel if is_segmented else opencl_context.bootstrap_sparse_kernel
|
|
@@ -85,7 +85,7 @@ def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices
|
|
|
85
85
|
best_theta_np = np.array([(1 if b else 0) for b in best_theta], dtype=np.int8)
|
|
86
86
|
|
|
87
87
|
# Args: [n, k, combo_count, segment_size]
|
|
88
|
-
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)
|
|
89
89
|
|
|
90
90
|
# Buffers
|
|
91
91
|
mf = cl.mem_flags
|
|
@@ -178,6 +178,16 @@ def to_scipy_sparse_upper_triangular(G, nodes, n_nodes, dtype):
|
|
|
178
178
|
return lil.tocsr()
|
|
179
179
|
|
|
180
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
|
+
|
|
181
191
|
def spin_glass_solver_sparse(
|
|
182
192
|
G,
|
|
183
193
|
quality=None,
|
|
@@ -267,12 +277,10 @@ def spin_glass_solver_sparse(
|
|
|
267
277
|
if max_order is None:
|
|
268
278
|
max_order = n_qubits
|
|
269
279
|
|
|
270
|
-
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)
|
|
271
281
|
|
|
272
282
|
if is_log:
|
|
273
|
-
|
|
274
|
-
cut_value = evaluate_cut_edges(best_theta, G_m.data, G_m.indptr, G_m.indices)
|
|
275
|
-
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)
|
|
276
284
|
|
|
277
285
|
combos_list = []
|
|
278
286
|
reheat_theta = best_theta.copy()
|
|
@@ -297,9 +305,9 @@ def spin_glass_solver_sparse(
|
|
|
297
305
|
combos = combos_list[k - 1]
|
|
298
306
|
|
|
299
307
|
if is_combo_maxcut_gpu and IS_OPENCL_AVAILABLE:
|
|
300
|
-
energy = run_bootstrap_opencl(reheat_theta, G_data_buf, G_rows_buf, G_cols_buf, combos, k, reheat_min_energy, is_segmented, segment_size)
|
|
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)
|
|
301
309
|
else:
|
|
302
|
-
energy = bootstrap(reheat_theta, G_m.data, G_m.indptr, G_m.indices, combos, k, reheat_min_energy, dtype)
|
|
310
|
+
energy = bootstrap(reheat_theta, G_m.data, G_m.indptr, G_m.indices, combos, k, reheat_min_energy, dtype, is_spin_glass)
|
|
303
311
|
|
|
304
312
|
if energy < reheat_min_energy:
|
|
305
313
|
reheat_min_energy = energy
|
|
@@ -308,9 +316,7 @@ def spin_glass_solver_sparse(
|
|
|
308
316
|
correction_quality = k + 1
|
|
309
317
|
|
|
310
318
|
if is_log:
|
|
311
|
-
|
|
312
|
-
cut_value = evaluate_cut_edges(reheat_theta, G_m.data, G_m.indptr, G_m.indices)
|
|
313
|
-
print(bitstring, float(cut_value), (l, r), float(reheat_min_energy))
|
|
319
|
+
log_intermediate(reheat_theta, G_m.data, G_m.indptr, G_m.indices, nodes, min_energy, is_spin_glass)
|
|
314
320
|
|
|
315
321
|
break
|
|
316
322
|
|
|
@@ -327,9 +333,17 @@ def spin_glass_solver_sparse(
|
|
|
327
333
|
bits_to_flip = random.sample(list(range(n_qubits)), num_to_flip)
|
|
328
334
|
for bit in bits_to_flip:
|
|
329
335
|
reheat_theta[bit] = not reheat_theta[bit]
|
|
330
|
-
reheat_min_energy = compute_energy(reheat_theta, G_m.data, G_m.indptr, G_m.indices)
|
|
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
|
|
331
341
|
|
|
332
342
|
bitstring, l, r = get_cut_from_bit_array(best_theta, nodes)
|
|
333
|
-
|
|
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)
|
|
334
348
|
|
|
335
349
|
return bitstring, float(cut_value), (l, r), float(min_energy)
|
|
@@ -37,22 +37,22 @@ def compute_energy(theta_bits, G_func, nodes):
|
|
|
37
37
|
|
|
38
38
|
|
|
39
39
|
@njit
|
|
40
|
-
def bootstrap_worker(theta, G_func, nodes, indices):
|
|
40
|
+
def bootstrap_worker(theta, G_func, nodes, indices, is_spin_glass):
|
|
41
41
|
local_theta = theta.copy()
|
|
42
42
|
for i in indices:
|
|
43
43
|
local_theta[i] = not local_theta[i]
|
|
44
|
-
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)
|
|
45
45
|
|
|
46
46
|
return energy
|
|
47
47
|
|
|
48
48
|
|
|
49
49
|
@njit(parallel=True)
|
|
50
|
-
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):
|
|
51
51
|
n = len(indices_array) // k
|
|
52
52
|
energies = np.empty(n, dtype=dtype)
|
|
53
53
|
for i in prange(n):
|
|
54
54
|
j = i * k
|
|
55
|
-
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)
|
|
56
56
|
|
|
57
57
|
energy = energies.min()
|
|
58
58
|
if energy < min_energy:
|
|
@@ -67,6 +67,16 @@ def bootstrap(best_theta, G_func, nodes, indices_array, k, min_energy, dtype):
|
|
|
67
67
|
return min_energy
|
|
68
68
|
|
|
69
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
|
+
|
|
70
80
|
def spin_glass_solver_streaming(
|
|
71
81
|
G_func,
|
|
72
82
|
nodes,
|
|
@@ -113,12 +123,10 @@ def spin_glass_solver_streaming(
|
|
|
113
123
|
if max_order is None:
|
|
114
124
|
max_order = n_qubits
|
|
115
125
|
|
|
116
|
-
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)
|
|
117
127
|
|
|
118
128
|
if is_log:
|
|
119
|
-
|
|
120
|
-
cut_value = evaluate_cut_edges(best_theta, G_func, nodes)
|
|
121
|
-
print(bitstring, float(cut_value), (l, r), float(min_energy))
|
|
129
|
+
log_intermediate(best_theta, G_func, nodes, min_energy, is_spin_glass)
|
|
122
130
|
|
|
123
131
|
combos_list = []
|
|
124
132
|
reheat_theta = best_theta.copy()
|
|
@@ -142,7 +150,7 @@ def spin_glass_solver_streaming(
|
|
|
142
150
|
else:
|
|
143
151
|
combos = combos_list[k - 1]
|
|
144
152
|
|
|
145
|
-
energy = bootstrap(best_theta, G_func, nodes, combos, k, min_energy, dtype)
|
|
153
|
+
energy = bootstrap(best_theta, G_func, nodes, combos, k, min_energy, dtype, is_spin_glass)
|
|
146
154
|
|
|
147
155
|
if energy < reheat_min_energy:
|
|
148
156
|
reheat_min_energy = energy
|
|
@@ -151,9 +159,7 @@ def spin_glass_solver_streaming(
|
|
|
151
159
|
correction_quality = k + 1
|
|
152
160
|
|
|
153
161
|
if is_log:
|
|
154
|
-
|
|
155
|
-
cut_value = evaluate_cut_edges(best_theta, G_func, nodes)
|
|
156
|
-
print(bitstring, float(cut_value), (l, r), float(reheat_min_energy))
|
|
162
|
+
log_intermediate(reheat_theta, G_func, nodes, min_energy, is_spin_glass)
|
|
157
163
|
|
|
158
164
|
break
|
|
159
165
|
|
|
@@ -170,9 +176,13 @@ def spin_glass_solver_streaming(
|
|
|
170
176
|
bits_to_flip = random.sample(list(range(n_qubits)), num_to_flip)
|
|
171
177
|
for bit in bits_to_flip:
|
|
172
178
|
reheat_theta[bit] = not reheat_theta[bit]
|
|
173
|
-
reheat_min_energy = compute_energy(reheat_theta, G_func, nodes)
|
|
179
|
+
reheat_min_energy = compute_energy(reheat_theta, G_func, nodes) if is_spin_glass else -evaluate_cut_edges(reheat_theta, G_func, nodes)
|
|
174
180
|
|
|
175
181
|
bitstring, l, r = get_cut_from_bit_array(best_theta, nodes)
|
|
176
|
-
|
|
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)
|
|
177
187
|
|
|
178
188
|
return bitstring, float(cut_value), (l, r), float(min_energy)
|
|
@@ -585,6 +585,8 @@ def tsp_symmetric_driver(G_m, is_cyclic, is_top_level, start_node, end_node, k_n
|
|
|
585
585
|
if not c is None:
|
|
586
586
|
best_path.append(c)
|
|
587
587
|
|
|
588
|
+
best_path = restitch(G_m, best_path, True)
|
|
589
|
+
|
|
588
590
|
if is_top_level:
|
|
589
591
|
if is_cyclic:
|
|
590
592
|
best_path += [best_path[0]]
|
|
@@ -614,8 +616,6 @@ def tsp_symmetric_driver(G_m, is_cyclic, is_top_level, start_node, end_node, k_n
|
|
|
614
616
|
else:
|
|
615
617
|
best_path, _ = targeted_three_opt(best_path, G_m, neighbor_lists, k_neighbors)
|
|
616
618
|
|
|
617
|
-
best_path = restitch(G_m, best_path, True)
|
|
618
|
-
|
|
619
619
|
best_weight = path_length(best_path, G_m)
|
|
620
620
|
|
|
621
621
|
return [nodes[x] for x in best_path], best_weight
|
|
@@ -780,6 +780,8 @@ def tsp_asymmetric_driver(G_m, is_reversed, is_cyclic, is_top_level, start_node,
|
|
|
780
780
|
if not c is None:
|
|
781
781
|
best_path.append(c)
|
|
782
782
|
|
|
783
|
+
best_path = restitch(G_m, best_path, False)
|
|
784
|
+
|
|
783
785
|
if is_top_level:
|
|
784
786
|
if is_cyclic:
|
|
785
787
|
best_path += [best_path[0]]
|
|
@@ -817,8 +819,6 @@ def tsp_asymmetric_driver(G_m, is_reversed, is_cyclic, is_top_level, start_node,
|
|
|
817
819
|
else:
|
|
818
820
|
final_path = best_path
|
|
819
821
|
|
|
820
|
-
final_path = restitch(G_m, final_path, False)
|
|
821
|
-
|
|
822
822
|
best_weight = path_length(final_path, G_m)
|
|
823
823
|
|
|
824
824
|
if is_reversed:
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='pyqrackising',
|
|
54
|
-
version='8.9.
|
|
54
|
+
version='8.9.2',
|
|
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
|