PyQrackIsing 9.15.2__tar.gz → 9.15.3__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-9.15.2/pyqrackising.egg-info → pyqrackising-9.15.3}/PKG-INFO +1 -1
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyproject.toml +1 -1
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/kernels.cl +116 -81
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/spin_glass_solver_sparse.py +116 -113
- {pyqrackising-9.15.2 → pyqrackising-9.15.3/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/setup.py +1 -1
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/LICENSE.md +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/MANIFEST.in +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/README.md +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/__init__.py +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/maxcut_tfim.py +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/maxcut_tfim_sparse.py +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/maxcut_tfim_streaming.py +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/maxcut_tfim_util.py +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/otoc.py +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/solve_maxcut_bnb.py +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/solve_maxcut_bnb_sparse.py +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/solve_maxcut_bnb_streaming.py +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/solve_maxcut_exact.py +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/solve_maxcut_exact_sparse.py +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/solve_maxcut_exact_streaming.py +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/spin_glass_solver.py +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/spin_glass_solver_streaming.py +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/tsp.py +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising/tsp_maxcut.py +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising.egg-info/requires.txt +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-9.15.2 → pyqrackising-9.15.3}/setup.cfg +0 -0
|
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
|
|
|
7
7
|
|
|
8
8
|
[project]
|
|
9
9
|
name = "pyqrackising"
|
|
10
|
-
version = "9.15.
|
|
10
|
+
version = "9.15.3"
|
|
11
11
|
requires-python = ">=3.8"
|
|
12
12
|
description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
|
|
13
13
|
readme = {file = "README.txt", content-type = "text/markdown"}
|
|
@@ -285,29 +285,31 @@ __kernel void calculate_cut_sparse(
|
|
|
285
285
|
}
|
|
286
286
|
|
|
287
287
|
real1 single_bit_flip_worker_sparse(__constant uint* theta, __global const real1* G_data, __global const uint* G_rows, __global const uint* G_cols, const int n, const bool is_spin_glass, const int k) {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
energy -= val;
|
|
306
|
-
}
|
|
288
|
+
// Only edges incident to k change when k is flipped.
|
|
289
|
+
// Walk only the adjacency list of k: O(degree(k)) instead of O(n * degree).
|
|
290
|
+
// Returns the energy DELTA (new_energy - base_energy) for this flip.
|
|
291
|
+
// All threads share the same base state (best_theta), so deltas are
|
|
292
|
+
// directly comparable for finding the best single-bit flip.
|
|
293
|
+
const bool k_bit_old = get_const_bit(theta, k);
|
|
294
|
+
const bool k_bit_new = !k_bit_old;
|
|
295
|
+
real1 delta = ZERO_R1;
|
|
296
|
+
const uint row_end = G_rows[k + 1];
|
|
297
|
+
for (uint col = G_rows[k]; col < row_end; ++col) {
|
|
298
|
+
const int v = G_cols[col];
|
|
299
|
+
const real1 val = G_data[col];
|
|
300
|
+
const bool v_bit = get_const_bit(theta, v);
|
|
301
|
+
const bool old_cut = k_bit_old != v_bit;
|
|
302
|
+
const bool new_cut = k_bit_new != v_bit;
|
|
303
|
+
if (old_cut != new_cut) {
|
|
304
|
+
delta += new_cut ? val : -val;
|
|
307
305
|
}
|
|
308
306
|
}
|
|
309
|
-
|
|
310
|
-
|
|
307
|
+
// For spin glass, the same edge contributes to both cut and non-cut terms,
|
|
308
|
+
// so the delta magnitude doubles (unlike-becomes-like costs twice: lose val + gain -val).
|
|
309
|
+
if (is_spin_glass) {
|
|
310
|
+
delta *= 2.0;
|
|
311
|
+
}
|
|
312
|
+
return delta;
|
|
311
313
|
}
|
|
312
314
|
|
|
313
315
|
__kernel void single_bit_flips_sparse(
|
|
@@ -315,8 +317,8 @@ __kernel void single_bit_flips_sparse(
|
|
|
315
317
|
__global const uint* G_rows,
|
|
316
318
|
__global const uint* G_cols,
|
|
317
319
|
__constant uint* best_theta,
|
|
318
|
-
__constant int* args, // args[0] = n, args[1] =
|
|
319
|
-
__global real1* max_energy_ptr, // output: per-group
|
|
320
|
+
__constant int* args, // args[0] = n, args[1] = is_spin_glass
|
|
321
|
+
__global real1* max_energy_ptr, // output: per-group best delta
|
|
320
322
|
__global int* max_index_ptr, // output: per-group best index (i)
|
|
321
323
|
__local real1* loc_energy, // local memory buffer
|
|
322
324
|
__local int* loc_index // local memory buffer
|
|
@@ -331,6 +333,8 @@ __kernel void single_bit_flips_sparse(
|
|
|
331
333
|
int best_i = i;
|
|
332
334
|
|
|
333
335
|
for (; i < n; i += max_i) {
|
|
336
|
+
// Worker returns delta; sign determines accept/reject.
|
|
337
|
+
// Only the largest positive delta matters for selecting the best flip.
|
|
334
338
|
const real1 energy = single_bit_flip_worker_sparse(best_theta, G_data, G_rows, G_cols, n, is_spin_glass, i);
|
|
335
339
|
if (energy > best_energy) {
|
|
336
340
|
best_energy = energy;
|
|
@@ -342,29 +346,53 @@ __kernel void single_bit_flips_sparse(
|
|
|
342
346
|
}
|
|
343
347
|
|
|
344
348
|
real1 double_bit_flip_worker_sparse(__constant uint* theta, __global const real1* G_data, __global const uint* G_rows, __global const uint* G_cols, const int n, const bool is_spin_glass, const int k, const int l) {
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
349
|
+
// Only edges incident to k or l change when both are flipped simultaneously.
|
|
350
|
+
// Walk adjacency lists of k and l: O(degree(k) + degree(l)) instead of O(n * degree).
|
|
351
|
+
// Returns the energy DELTA for this double flip.
|
|
352
|
+
const bool k_bit_old = get_const_bit(theta, k);
|
|
353
|
+
const bool k_bit_new = !k_bit_old;
|
|
354
|
+
const bool l_bit_old = get_const_bit(theta, l);
|
|
355
|
+
const bool l_bit_new = !l_bit_old;
|
|
356
|
+
real1 delta = ZERO_R1;
|
|
357
|
+
|
|
358
|
+
// Contribution from edges incident to k (excluding the k-l edge, handled below)
|
|
359
|
+
const uint k_row_end = G_rows[k + 1];
|
|
360
|
+
for (uint col = G_rows[k]; col < k_row_end; ++col) {
|
|
361
|
+
const int v = G_cols[col];
|
|
362
|
+
if (v == l) continue; // handle k-l edge separately
|
|
363
|
+
const real1 val = G_data[col];
|
|
364
|
+
// v is not flipped, so v_bit unchanged
|
|
365
|
+
const bool v_bit = get_const_bit(theta, v);
|
|
366
|
+
const bool old_cut = k_bit_old != v_bit;
|
|
367
|
+
const bool new_cut = k_bit_new != v_bit;
|
|
368
|
+
if (old_cut != new_cut) {
|
|
369
|
+
delta += new_cut ? val : -val;
|
|
350
370
|
}
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// Contribution from edges incident to l (excluding the k-l edge)
|
|
374
|
+
const uint l_row_end = G_rows[l + 1];
|
|
375
|
+
for (uint col = G_rows[l]; col < l_row_end; ++col) {
|
|
376
|
+
const int v = G_cols[col];
|
|
377
|
+
if (v == k) continue; // handle k-l edge separately
|
|
378
|
+
const real1 val = G_data[col];
|
|
379
|
+
const bool v_bit = get_const_bit(theta, v);
|
|
380
|
+
const bool old_cut = l_bit_old != v_bit;
|
|
381
|
+
const bool new_cut = l_bit_new != v_bit;
|
|
382
|
+
if (old_cut != new_cut) {
|
|
383
|
+
delta += new_cut ? val : -val;
|
|
364
384
|
}
|
|
365
385
|
}
|
|
366
386
|
|
|
367
|
-
|
|
387
|
+
// Handle the k-l edge: both endpoints flip, so XOR status may or may not change.
|
|
388
|
+
// old_cut = (k_bit_old != l_bit_old); new_cut = (k_bit_new != l_bit_new).
|
|
389
|
+
// Since both bits flip, new_cut == old_cut — the k-l edge contribution is unchanged.
|
|
390
|
+
// No delta from the k-l edge. (Included here explicitly for clarity.)
|
|
391
|
+
|
|
392
|
+
if (is_spin_glass) {
|
|
393
|
+
delta *= 2.0;
|
|
394
|
+
}
|
|
395
|
+
return delta;
|
|
368
396
|
}
|
|
369
397
|
|
|
370
398
|
__kernel void double_bit_flips_sparse(
|
|
@@ -372,8 +400,8 @@ __kernel void double_bit_flips_sparse(
|
|
|
372
400
|
__global const uint* G_rows,
|
|
373
401
|
__global const uint* G_cols,
|
|
374
402
|
__constant uint* best_theta,
|
|
375
|
-
__constant int* args, // args[0] = n, args[1] =
|
|
376
|
-
__global real1* max_energy_ptr, // output: per-group
|
|
403
|
+
__constant int* args, // args[0] = n, args[1] = is_spin_glass
|
|
404
|
+
__global real1* max_energy_ptr, // output: per-group best delta
|
|
377
405
|
__global int* max_index_ptr, // output: per-group best index (i)
|
|
378
406
|
__local real1* loc_energy, // local memory buffer
|
|
379
407
|
__local int* loc_index // local memory buffer
|
|
@@ -403,6 +431,7 @@ __kernel void double_bit_flips_sparse(
|
|
|
403
431
|
}
|
|
404
432
|
const int l = c + k + 1;
|
|
405
433
|
|
|
434
|
+
// Worker returns delta; sign determines accept/reject.
|
|
406
435
|
const real1 energy = double_bit_flip_worker_sparse(best_theta, G_data, G_rows, G_cols, n, is_spin_glass, k, l);
|
|
407
436
|
if (energy > best_energy) {
|
|
408
437
|
best_energy = energy;
|
|
@@ -879,12 +908,10 @@ inline size_t gray_code_next(ulong* theta, const size_t curr_idx, const size_t o
|
|
|
879
908
|
size_t curr = curr_idx + 1U;
|
|
880
909
|
prev = prev ^ (prev >> 1U);
|
|
881
910
|
curr = curr ^ (curr >> 1U);
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
}
|
|
887
|
-
flip_bit += offset;
|
|
911
|
+
const ulong diff = prev ^ curr;
|
|
912
|
+
// ctz gives the position of the lowest set bit in a single native instruction.
|
|
913
|
+
// diff is always non-zero (consecutive Gray codes differ in exactly one bit).
|
|
914
|
+
const size_t flip_bit = (size_t)ctz(diff) + offset;
|
|
888
915
|
theta[flip_bit >> 6U] ^= 1UL << (flip_bit & 63U);
|
|
889
916
|
|
|
890
917
|
return flip_bit;
|
|
@@ -1117,25 +1144,31 @@ __kernel void gray_sparse(
|
|
|
1117
1144
|
for (; i < gray_iterations; i += max_i) {
|
|
1118
1145
|
for (int block = 0; block < blocks; ++block) {
|
|
1119
1146
|
const size_t flip_bit = gray_code_next(theta_local, i, block << 6U);
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1147
|
+
// Incremental update: only edges incident to flip_bit change.
|
|
1148
|
+
// After gray_code_next, theta_local already has the bit flipped.
|
|
1149
|
+
// Determine the new and old bit values.
|
|
1150
|
+
const bool new_bit = get_local_bit(theta_local, flip_bit);
|
|
1151
|
+
const bool old_bit = !new_bit;
|
|
1152
|
+
real1 delta = ZERO_R1;
|
|
1153
|
+
const uint row_end = G_rows[flip_bit + 1];
|
|
1154
|
+
for (uint col = G_rows[flip_bit]; col < row_end; ++col) {
|
|
1155
|
+
const uint v = G_cols[col];
|
|
1156
|
+
const real1 val = G_data[col];
|
|
1157
|
+
const bool v_bit = get_local_bit(theta_local, v);
|
|
1158
|
+
const bool old_cut = old_bit != v_bit;
|
|
1159
|
+
const bool new_cut = new_bit != v_bit;
|
|
1160
|
+
if (old_cut != new_cut) {
|
|
1161
|
+
delta += new_cut ? val : -val;
|
|
1133
1162
|
}
|
|
1134
1163
|
}
|
|
1164
|
+
if (is_spin_glass) {
|
|
1165
|
+
delta *= 2.0;
|
|
1166
|
+
}
|
|
1135
1167
|
|
|
1136
|
-
if (
|
|
1137
|
-
best_energy
|
|
1168
|
+
if (delta > ZERO_R1) {
|
|
1169
|
+
best_energy += delta;
|
|
1138
1170
|
} else {
|
|
1171
|
+
// Reject: undo the flip
|
|
1139
1172
|
theta_local[flip_bit >> 6U] ^= 1UL << (flip_bit & 63U);
|
|
1140
1173
|
}
|
|
1141
1174
|
}
|
|
@@ -1204,25 +1237,27 @@ __kernel void gray_sparse_segmented(
|
|
|
1204
1237
|
for (; i < gray_iterations; i += max_i) {
|
|
1205
1238
|
for (int block = 0; block < blocks; ++block) {
|
|
1206
1239
|
const size_t flip_bit = gray_code_next(theta_local, i, block << 6U);
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
}
|
|
1240
|
+
// Incremental update: only edges incident to flip_bit change.
|
|
1241
|
+
const bool new_bit = get_local_bit(theta_local, flip_bit);
|
|
1242
|
+
const bool old_bit = !new_bit;
|
|
1243
|
+
real1 delta = ZERO_R1;
|
|
1244
|
+
const uint row_end = G_rows[flip_bit + 1];
|
|
1245
|
+
for (uint col = G_rows[flip_bit]; col < row_end; ++col) {
|
|
1246
|
+
const int v = G_cols[col];
|
|
1247
|
+
const real1 val = get_G_m(G_data, col, segment_size);
|
|
1248
|
+
const bool v_bit = get_local_bit(theta_local, v);
|
|
1249
|
+
const bool old_cut = old_bit != v_bit;
|
|
1250
|
+
const bool new_cut = new_bit != v_bit;
|
|
1251
|
+
if (old_cut != new_cut) {
|
|
1252
|
+
delta += new_cut ? val : -val;
|
|
1221
1253
|
}
|
|
1222
1254
|
}
|
|
1255
|
+
if (is_spin_glass) {
|
|
1256
|
+
delta *= 2.0;
|
|
1257
|
+
}
|
|
1223
1258
|
|
|
1224
|
-
if (
|
|
1225
|
-
best_energy
|
|
1259
|
+
if (delta > ZERO_R1) {
|
|
1260
|
+
best_energy += delta;
|
|
1226
1261
|
} else {
|
|
1227
1262
|
theta_local[flip_bit >> 6U] ^= 1UL << (flip_bit & 63U);
|
|
1228
1263
|
}
|
|
@@ -35,26 +35,31 @@ gnl = opencl_context.GRAY_NODE_LIMIT
|
|
|
35
35
|
@njit(parallel=True, cache=True)
|
|
36
36
|
def run_single_bit_flips(best_theta, is_spin_glass, G_data, G_rows, G_cols):
|
|
37
37
|
n = len(best_theta)
|
|
38
|
+
deltas = np.empty(n, dtype=dtype)
|
|
39
|
+
|
|
40
|
+
for i in prange(n):
|
|
41
|
+
# Compute delta: walk only adjacency list of i (O(degree(i)))
|
|
42
|
+
old_bit = best_theta[i]
|
|
43
|
+
new_bit = not old_bit
|
|
44
|
+
delta = dtype(0)
|
|
45
|
+
for col in range(G_rows[i], G_rows[i + 1]):
|
|
46
|
+
v = G_cols[col]
|
|
47
|
+
val = G_data[col]
|
|
48
|
+
v_bit = best_theta[v]
|
|
49
|
+
old_cut = old_bit != v_bit
|
|
50
|
+
new_cut = new_bit != v_bit
|
|
51
|
+
if old_cut != new_cut:
|
|
52
|
+
delta += val if new_cut else -val
|
|
53
|
+
if is_spin_glass:
|
|
54
|
+
delta *= dtype(2)
|
|
55
|
+
deltas[i] = delta
|
|
38
56
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if is_spin_glass:
|
|
42
|
-
for i in prange(n):
|
|
43
|
-
state = best_theta.copy()
|
|
44
|
-
state[i] = not state[i]
|
|
45
|
-
energies[i] = compute_energy_sparse(state, G_data, G_rows, G_cols, n)
|
|
46
|
-
else:
|
|
47
|
-
for i in prange(n):
|
|
48
|
-
state = best_theta.copy()
|
|
49
|
-
state[i] = not state[i]
|
|
50
|
-
energies[i] = compute_cut_sparse(state, G_data, G_rows, G_cols, n)
|
|
51
|
-
|
|
52
|
-
best_index = np.argmax(energies)
|
|
53
|
-
best_energy = energies[best_index]
|
|
57
|
+
best_index = np.argmax(deltas)
|
|
58
|
+
best_delta = deltas[best_index]
|
|
54
59
|
best_state = best_theta.copy()
|
|
55
60
|
best_state[best_index] = not best_state[best_index]
|
|
56
61
|
|
|
57
|
-
return
|
|
62
|
+
return best_delta, best_state
|
|
58
63
|
|
|
59
64
|
|
|
60
65
|
@njit(parallel=True, cache=True)
|
|
@@ -64,58 +69,65 @@ def run_double_bit_flips(best_theta, is_spin_glass, G_data, G_rows, G_cols, thre
|
|
|
64
69
|
thread_batch = (combo_count + thread_count - 1) // thread_count
|
|
65
70
|
|
|
66
71
|
states = np.empty((thread_count, n), dtype=np.bool_)
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
72
|
+
deltas = np.empty(thread_count, dtype=dtype)
|
|
73
|
+
|
|
74
|
+
for t in prange(thread_count):
|
|
75
|
+
s = int(t)
|
|
76
|
+
while s < combo_count:
|
|
77
|
+
c = int(s)
|
|
78
|
+
i = int(0)
|
|
79
|
+
lcv = int(n - 1)
|
|
80
|
+
while c >= lcv:
|
|
81
|
+
c -= lcv
|
|
82
|
+
i += 1
|
|
83
|
+
lcv -= 1
|
|
84
|
+
if not lcv:
|
|
85
|
+
break
|
|
86
|
+
j = int(c + i + 1)
|
|
87
|
+
|
|
88
|
+
# Delta: walk adjacency lists of i and j only
|
|
89
|
+
old_i = best_theta[i]
|
|
90
|
+
new_i = not old_i
|
|
91
|
+
old_j = best_theta[j]
|
|
92
|
+
new_j = not old_j
|
|
93
|
+
delta = dtype(0)
|
|
94
|
+
|
|
95
|
+
for col in range(G_rows[i], G_rows[i + 1]):
|
|
96
|
+
v = G_cols[col]
|
|
97
|
+
if v == j:
|
|
98
|
+
continue # k-l edge: both flip, XOR unchanged, zero delta
|
|
99
|
+
val = G_data[col]
|
|
100
|
+
v_bit = best_theta[v]
|
|
101
|
+
if (old_i != v_bit) != (new_i != v_bit):
|
|
102
|
+
delta += val if (new_i != v_bit) else -val
|
|
103
|
+
|
|
104
|
+
for col in range(G_rows[j], G_rows[j + 1]):
|
|
105
|
+
v = G_cols[col]
|
|
106
|
+
if v == i:
|
|
107
|
+
continue
|
|
108
|
+
val = G_data[col]
|
|
109
|
+
v_bit = best_theta[v]
|
|
110
|
+
if (old_j != v_bit) != (new_j != v_bit):
|
|
111
|
+
delta += val if (new_j != v_bit) else -val
|
|
112
|
+
|
|
113
|
+
if is_spin_glass:
|
|
114
|
+
delta *= dtype(2)
|
|
115
|
+
|
|
116
|
+
states[t, i] = new_i
|
|
117
|
+
states[t, j] = new_j
|
|
118
|
+
# copy other bits (only need to track i,j changes)
|
|
119
|
+
for k in range(n):
|
|
120
|
+
if k != i and k != j:
|
|
121
|
+
states[t, k] = best_theta[k]
|
|
122
|
+
deltas[t] = delta
|
|
123
|
+
|
|
124
|
+
s += thread_batch
|
|
125
|
+
|
|
126
|
+
best_index = np.argmax(deltas)
|
|
127
|
+
best_delta = deltas[best_index]
|
|
116
128
|
best_state = states[best_index]
|
|
117
129
|
|
|
118
|
-
return
|
|
130
|
+
return best_delta, best_state
|
|
119
131
|
|
|
120
132
|
|
|
121
133
|
@njit(parallel=True, cache=True)
|
|
@@ -170,36 +182,32 @@ def run_gray_optimization(
|
|
|
170
182
|
thread_iterations = (gray_iterations + thread_count - 1) // thread_count
|
|
171
183
|
blocks = (n + 63) >> 6
|
|
172
184
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
for
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
# Revert iterator
|
|
200
|
-
iterator[flip_bit] = not iterator[flip_bit]
|
|
201
|
-
if best_energy > energies[i]:
|
|
202
|
-
energies[i] = best_energy
|
|
185
|
+
for i in prange(thread_count):
|
|
186
|
+
iterator = iterators[i]
|
|
187
|
+
best_energy = energies[i]
|
|
188
|
+
for curr_idx in range(thread_iterations):
|
|
189
|
+
for block in range(blocks):
|
|
190
|
+
flip_bit = gray_code_next(iterator, curr_idx, block << 6)
|
|
191
|
+
# Incremental delta: walk only adjacency list of flip_bit
|
|
192
|
+
new_bit = iterator[flip_bit]
|
|
193
|
+
old_bit = not new_bit
|
|
194
|
+
delta = dtype(0)
|
|
195
|
+
for col in range(G_rows[flip_bit], G_rows[flip_bit + 1]):
|
|
196
|
+
v = G_cols[col]
|
|
197
|
+
val = G_data[col]
|
|
198
|
+
v_bit = iterator[v]
|
|
199
|
+
old_cut = old_bit != v_bit
|
|
200
|
+
new_cut = new_bit != v_bit
|
|
201
|
+
if old_cut != new_cut:
|
|
202
|
+
delta += val if new_cut else -val
|
|
203
|
+
if is_spin_glass:
|
|
204
|
+
delta *= dtype(2)
|
|
205
|
+
if delta > dtype(0):
|
|
206
|
+
best_energy += delta
|
|
207
|
+
else:
|
|
208
|
+
iterator[flip_bit] = not iterator[flip_bit]
|
|
209
|
+
if best_energy > energies[i]:
|
|
210
|
+
energies[i] = best_energy
|
|
203
211
|
|
|
204
212
|
best_index = np.argmax(energies)
|
|
205
213
|
best_energy = energies[best_index]
|
|
@@ -212,7 +220,6 @@ def run_bit_flips_opencl(
|
|
|
212
220
|
is_double,
|
|
213
221
|
n,
|
|
214
222
|
kernel,
|
|
215
|
-
best_energy,
|
|
216
223
|
theta,
|
|
217
224
|
theta_buf,
|
|
218
225
|
G_data_buf,
|
|
@@ -269,13 +276,13 @@ def run_bit_flips_opencl(
|
|
|
269
276
|
# Queue read for results we might not need
|
|
270
277
|
cl.enqueue_copy(queue, max_index_host, max_index_buf)
|
|
271
278
|
|
|
272
|
-
# Find global
|
|
279
|
+
# Find global best delta
|
|
273
280
|
best_x = np.argmax(max_energy_host)
|
|
274
281
|
energy = max_energy_host[best_x]
|
|
275
282
|
|
|
276
|
-
if energy <=
|
|
277
|
-
# No improvement:
|
|
278
|
-
return
|
|
283
|
+
if energy <= 0.0:
|
|
284
|
+
# No improvement: best delta is non-positive, exit early.
|
|
285
|
+
return 0.0, theta
|
|
279
286
|
|
|
280
287
|
# We need the best index
|
|
281
288
|
queue.finish()
|
|
@@ -708,7 +715,6 @@ def spin_glass_solver_sparse(
|
|
|
708
715
|
False,
|
|
709
716
|
n_qubits,
|
|
710
717
|
single_bit_flips_kernel,
|
|
711
|
-
max_energy,
|
|
712
718
|
best_theta,
|
|
713
719
|
theta_buf,
|
|
714
720
|
G_data_buf,
|
|
@@ -719,8 +725,8 @@ def spin_glass_solver_sparse(
|
|
|
719
725
|
)
|
|
720
726
|
else:
|
|
721
727
|
energy, state = run_single_bit_flips(best_theta, is_spin_glass, G_m.data, G_m.indptr, G_m.indices)
|
|
722
|
-
if energy >
|
|
723
|
-
max_energy
|
|
728
|
+
if energy > 0.0:
|
|
729
|
+
max_energy += energy
|
|
724
730
|
best_theta = state
|
|
725
731
|
improved = True
|
|
726
732
|
continue
|
|
@@ -732,7 +738,6 @@ def spin_glass_solver_sparse(
|
|
|
732
738
|
True,
|
|
733
739
|
n_qubits,
|
|
734
740
|
double_bit_flips_kernel,
|
|
735
|
-
max_energy,
|
|
736
741
|
best_theta,
|
|
737
742
|
theta_buf,
|
|
738
743
|
G_data_buf,
|
|
@@ -743,8 +748,8 @@ def spin_glass_solver_sparse(
|
|
|
743
748
|
)
|
|
744
749
|
else:
|
|
745
750
|
energy, state = run_double_bit_flips(best_theta, is_spin_glass, G_m.data, G_m.indptr, G_m.indices, thread_count)
|
|
746
|
-
if energy >
|
|
747
|
-
max_energy
|
|
751
|
+
if energy > 0.0:
|
|
752
|
+
max_energy += energy
|
|
748
753
|
best_theta = state
|
|
749
754
|
improved = True
|
|
750
755
|
continue
|
|
@@ -810,7 +815,6 @@ def spin_glass_solver_sparse(
|
|
|
810
815
|
False,
|
|
811
816
|
n_qubits,
|
|
812
817
|
single_bit_flips_kernel,
|
|
813
|
-
max_energy,
|
|
814
818
|
reheat_theta,
|
|
815
819
|
theta_buf,
|
|
816
820
|
G_data_buf,
|
|
@@ -821,8 +825,8 @@ def spin_glass_solver_sparse(
|
|
|
821
825
|
)
|
|
822
826
|
else:
|
|
823
827
|
energy, state = run_single_bit_flips(reheat_theta, is_spin_glass, G_m.data, G_m.indptr, G_m.indices)
|
|
824
|
-
if energy >
|
|
825
|
-
max_energy
|
|
828
|
+
if energy > 0.0:
|
|
829
|
+
max_energy += energy
|
|
826
830
|
best_theta = state
|
|
827
831
|
improved = True
|
|
828
832
|
continue
|
|
@@ -837,7 +841,6 @@ def spin_glass_solver_sparse(
|
|
|
837
841
|
True,
|
|
838
842
|
n_qubits,
|
|
839
843
|
double_bit_flips_kernel,
|
|
840
|
-
max_energy,
|
|
841
844
|
reheat_theta,
|
|
842
845
|
theta_buf,
|
|
843
846
|
G_data_buf,
|
|
@@ -848,8 +851,8 @@ def spin_glass_solver_sparse(
|
|
|
848
851
|
)
|
|
849
852
|
else:
|
|
850
853
|
energy, state = run_double_bit_flips(reheat_theta, is_spin_glass, G_m.data, G_m.indptr, G_m.indices, thread_count)
|
|
851
|
-
if energy >
|
|
852
|
-
max_energy
|
|
854
|
+
if energy > 0.0:
|
|
855
|
+
max_energy += energy
|
|
853
856
|
best_theta = state
|
|
854
857
|
improved = True
|
|
855
858
|
|
|
@@ -7,7 +7,7 @@ with open(README_PATH) as readme_file:
|
|
|
7
7
|
|
|
8
8
|
setup(
|
|
9
9
|
name="pyqrackising",
|
|
10
|
-
version="9.15.
|
|
10
|
+
version="9.15.3",
|
|
11
11
|
author="Dan Strano",
|
|
12
12
|
author_email="stranoj@gmail.com",
|
|
13
13
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|