PyQrackIsing 9.15.4__tar.gz → 9.16.0__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.4/pyqrackising.egg-info → pyqrackising-9.16.0}/PKG-INFO +1 -1
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/README.md +1 -1
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyproject.toml +1 -1
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising/__init__.py +1 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising/kernels.cl +81 -116
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising/maxcut_tfim_util.py +28 -119
- pyqrackising-9.16.0/pyqrackising/spin_glass_solver_hybrid.py +46 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising/spin_glass_solver_sparse.py +102 -52
- {pyqrackising-9.15.4 → pyqrackising-9.16.0/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising.egg-info/SOURCES.txt +1 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/setup.py +1 -1
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/LICENSE.md +0 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/MANIFEST.in +0 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising/maxcut_tfim.py +0 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising/maxcut_tfim_sparse.py +0 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising/maxcut_tfim_streaming.py +0 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising/otoc.py +0 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising/solve_maxcut_bnb.py +0 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising/solve_maxcut_bnb_sparse.py +0 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising/solve_maxcut_bnb_streaming.py +0 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising/solve_maxcut_exact.py +0 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising/solve_maxcut_exact_sparse.py +0 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising/solve_maxcut_exact_streaming.py +0 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising/spin_glass_solver.py +0 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising/spin_glass_solver_streaming.py +0 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising/tsp.py +0 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising/tsp_maxcut.py +0 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising.egg-info/requires.txt +0 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-9.15.4 → pyqrackising-9.16.0}/setup.cfg +0 -0
|
@@ -89,7 +89,7 @@ solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(
|
|
|
89
89
|
)
|
|
90
90
|
# solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, best_guess=maxcut_tfim(G, quality=6)[0])
|
|
91
91
|
```
|
|
92
|
-
We also provide `spin_glass_solver_sparse(G)
|
|
92
|
+
We also provide `spin_glass_solver_sparse(G)`, `spin_glass_solver_streaming(G_func, nodes)`, and `spin_glass_hybrid(G)` (which combines the dense TFIM-based solver with a sparse local-optimization solver). `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. `is_spin_glass` controls whether the solver optimizes for cut value or spin-glass energy. 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. `gray_iterations` gives manual control over how many iterations are carried out of a parallel Gray-code search on `best_guess`. `gray_seed_multiple` controls how many parallel search seeds (as a multiple of your CPU thread count) are tested for the best parallel seeds, and a value of `1` will perfectly cover the search space without collision if your node count is a power of 2. `bp_scale` controls opt-in and time complexity scaling for _belief propagation_ as a competing "warm start," with `None` disabling this option completely, `1.0` representing a budget of exactly `O(n^3)` time complexity, and lesser or greater values representing a linear multiplier on that budget. `bp_damping` similarly controls belief propagation damping.
|
|
93
93
|
|
|
94
94
|
These solvers above can be used as a "warm state" for _MaxSAT-based_ solvers or _branch-and-bound_ solvers, potentially to reach _exact_ solutions to MAXCUT:
|
|
95
95
|
|
|
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
|
|
|
7
7
|
|
|
8
8
|
[project]
|
|
9
9
|
name = "pyqrackising"
|
|
10
|
-
version = "9.
|
|
10
|
+
version = "9.16.0"
|
|
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"}
|
|
@@ -9,6 +9,7 @@ from .maxcut_tfim_util import probability_by_hamming_weight
|
|
|
9
9
|
from .spin_glass_solver import spin_glass_solver
|
|
10
10
|
from .spin_glass_solver_sparse import spin_glass_solver_sparse
|
|
11
11
|
from .spin_glass_solver_streaming import spin_glass_solver_streaming
|
|
12
|
+
from .spin_glass_solver_hybrid import spin_glass_solver_hybrid
|
|
12
13
|
from .tsp import tsp_symmetric, tsp_asymmetric
|
|
13
14
|
from .tsp_maxcut import tsp_maxcut, tsp_maxcut_sparse, tsp_maxcut_streaming
|
|
14
15
|
from .convert_tensor_network_to_tsp import convert_tensor_network_to_tsp, convert_quimb_tree_to_tsp
|
|
@@ -285,31 +285,29 @@ __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
|
-
|
|
288
|
+
real1 energy = ZERO_R1;
|
|
289
|
+
for (int u = 0; u < n; ++u) {
|
|
290
|
+
bool u_bit = get_const_bit(theta, u);
|
|
291
|
+
if (u == k) {
|
|
292
|
+
u_bit = !u_bit;
|
|
293
|
+
}
|
|
294
|
+
const size_t mCol = G_rows[u + 1];
|
|
295
|
+
for (int col = G_rows[u]; col < mCol; ++col) {
|
|
296
|
+
const int v = G_cols[col];
|
|
297
|
+
const real1 val = G_data[col];
|
|
298
|
+
bool v_bit = get_const_bit(theta, v);
|
|
299
|
+
if (v == k) {
|
|
300
|
+
v_bit = !v_bit;
|
|
301
|
+
}
|
|
302
|
+
if (u_bit != v_bit) {
|
|
303
|
+
energy += val;
|
|
304
|
+
} else if (is_spin_glass) {
|
|
305
|
+
energy -= val;
|
|
306
|
+
}
|
|
305
307
|
}
|
|
306
308
|
}
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
if (is_spin_glass) {
|
|
310
|
-
delta *= 2.0;
|
|
311
|
-
}
|
|
312
|
-
return delta;
|
|
309
|
+
|
|
310
|
+
return energy;
|
|
313
311
|
}
|
|
314
312
|
|
|
315
313
|
__kernel void single_bit_flips_sparse(
|
|
@@ -317,8 +315,8 @@ __kernel void single_bit_flips_sparse(
|
|
|
317
315
|
__global const uint* G_rows,
|
|
318
316
|
__global const uint* G_cols,
|
|
319
317
|
__constant uint* best_theta,
|
|
320
|
-
__constant int* args, // args[0] = n, args[1] =
|
|
321
|
-
__global real1* max_energy_ptr, // output: per-group
|
|
318
|
+
__constant int* args, // args[0] = n, args[1] = k
|
|
319
|
+
__global real1* max_energy_ptr, // output: per-group min energy
|
|
322
320
|
__global int* max_index_ptr, // output: per-group best index (i)
|
|
323
321
|
__local real1* loc_energy, // local memory buffer
|
|
324
322
|
__local int* loc_index // local memory buffer
|
|
@@ -333,8 +331,6 @@ __kernel void single_bit_flips_sparse(
|
|
|
333
331
|
int best_i = i;
|
|
334
332
|
|
|
335
333
|
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.
|
|
338
334
|
const real1 energy = single_bit_flip_worker_sparse(best_theta, G_data, G_rows, G_cols, n, is_spin_glass, i);
|
|
339
335
|
if (energy > best_energy) {
|
|
340
336
|
best_energy = energy;
|
|
@@ -346,53 +342,29 @@ __kernel void single_bit_flips_sparse(
|
|
|
346
342
|
}
|
|
347
343
|
|
|
348
344
|
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) {
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
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;
|
|
345
|
+
real1 energy = ZERO_R1;
|
|
346
|
+
for (int u = 0; u < n; ++u) {
|
|
347
|
+
bool u_bit = get_const_bit(theta, u);
|
|
348
|
+
if ((u == k) || (u == l)) {
|
|
349
|
+
u_bit = !u_bit;
|
|
370
350
|
}
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
351
|
+
const size_t mCol = G_rows[u + 1];
|
|
352
|
+
for (int col = G_rows[u]; col < mCol; ++col) {
|
|
353
|
+
const int v = G_cols[col];
|
|
354
|
+
const real1 val = G_data[col];
|
|
355
|
+
bool v_bit = get_const_bit(theta, v);
|
|
356
|
+
if ((v == k) || (v == l)) {
|
|
357
|
+
v_bit = !v_bit;
|
|
358
|
+
}
|
|
359
|
+
if (u_bit != v_bit) {
|
|
360
|
+
energy += val;
|
|
361
|
+
} else if (is_spin_glass) {
|
|
362
|
+
energy -= val;
|
|
363
|
+
}
|
|
384
364
|
}
|
|
385
365
|
}
|
|
386
366
|
|
|
387
|
-
|
|
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;
|
|
367
|
+
return energy;
|
|
396
368
|
}
|
|
397
369
|
|
|
398
370
|
__kernel void double_bit_flips_sparse(
|
|
@@ -400,8 +372,8 @@ __kernel void double_bit_flips_sparse(
|
|
|
400
372
|
__global const uint* G_rows,
|
|
401
373
|
__global const uint* G_cols,
|
|
402
374
|
__constant uint* best_theta,
|
|
403
|
-
__constant int* args, // args[0] = n, args[1] =
|
|
404
|
-
__global real1* max_energy_ptr, // output: per-group
|
|
375
|
+
__constant int* args, // args[0] = n, args[1] = k
|
|
376
|
+
__global real1* max_energy_ptr, // output: per-group min energy
|
|
405
377
|
__global int* max_index_ptr, // output: per-group best index (i)
|
|
406
378
|
__local real1* loc_energy, // local memory buffer
|
|
407
379
|
__local int* loc_index // local memory buffer
|
|
@@ -431,7 +403,6 @@ __kernel void double_bit_flips_sparse(
|
|
|
431
403
|
}
|
|
432
404
|
const int l = c + k + 1;
|
|
433
405
|
|
|
434
|
-
// Worker returns delta; sign determines accept/reject.
|
|
435
406
|
const real1 energy = double_bit_flip_worker_sparse(best_theta, G_data, G_rows, G_cols, n, is_spin_glass, k, l);
|
|
436
407
|
if (energy > best_energy) {
|
|
437
408
|
best_energy = energy;
|
|
@@ -908,10 +879,12 @@ inline size_t gray_code_next(ulong* theta, const size_t curr_idx, const size_t o
|
|
|
908
879
|
size_t curr = curr_idx + 1U;
|
|
909
880
|
prev = prev ^ (prev >> 1U);
|
|
910
881
|
curr = curr ^ (curr >> 1U);
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
882
|
+
size_t diff = prev ^ curr;
|
|
883
|
+
size_t flip_bit = 0;
|
|
884
|
+
while (!((diff >> flip_bit) & 1U)) {
|
|
885
|
+
++flip_bit;
|
|
886
|
+
}
|
|
887
|
+
flip_bit += offset;
|
|
915
888
|
theta[flip_bit >> 6U] ^= 1UL << (flip_bit & 63U);
|
|
916
889
|
|
|
917
890
|
return flip_bit;
|
|
@@ -1144,31 +1117,25 @@ __kernel void gray_sparse(
|
|
|
1144
1117
|
for (; i < gray_iterations; i += max_i) {
|
|
1145
1118
|
for (int block = 0; block < blocks; ++block) {
|
|
1146
1119
|
const size_t flip_bit = gray_code_next(theta_local, i, block << 6U);
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
if (old_cut != new_cut) {
|
|
1161
|
-
delta += new_cut ? val : -val;
|
|
1120
|
+
real1 energy = ZERO_R1;
|
|
1121
|
+
for (uint u = 0; u < n; u++) {
|
|
1122
|
+
const bool u_bit = get_local_bit(theta_local, u);
|
|
1123
|
+
const size_t mCol = G_rows[u + 1];
|
|
1124
|
+
for (int col = G_rows[u]; col < mCol; ++col) {
|
|
1125
|
+
const int v = G_cols[col];
|
|
1126
|
+
const real1 val = G_data[col];
|
|
1127
|
+
const bool v_bit = get_local_bit(theta_local, v);
|
|
1128
|
+
if (u_bit != v_bit) {
|
|
1129
|
+
energy += val;
|
|
1130
|
+
} else if (is_spin_glass) {
|
|
1131
|
+
energy -= val;
|
|
1132
|
+
}
|
|
1162
1133
|
}
|
|
1163
1134
|
}
|
|
1164
|
-
if (is_spin_glass) {
|
|
1165
|
-
delta *= 2.0;
|
|
1166
|
-
}
|
|
1167
1135
|
|
|
1168
|
-
if (
|
|
1169
|
-
best_energy
|
|
1136
|
+
if (energy > best_energy) {
|
|
1137
|
+
best_energy = energy;
|
|
1170
1138
|
} else {
|
|
1171
|
-
// Reject: undo the flip
|
|
1172
1139
|
theta_local[flip_bit >> 6U] ^= 1UL << (flip_bit & 63U);
|
|
1173
1140
|
}
|
|
1174
1141
|
}
|
|
@@ -1237,27 +1204,25 @@ __kernel void gray_sparse_segmented(
|
|
|
1237
1204
|
for (; i < gray_iterations; i += max_i) {
|
|
1238
1205
|
for (int block = 0; block < blocks; ++block) {
|
|
1239
1206
|
const size_t flip_bit = gray_code_next(theta_local, i, block << 6U);
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1207
|
+
real1 energy = ZERO_R1;
|
|
1208
|
+
for (uint u = 0; u < n; u++) {
|
|
1209
|
+
const size_t u_offset = u * n;
|
|
1210
|
+
const bool u_bit = get_local_bit(theta_local, u);
|
|
1211
|
+
const uint row_end = G_rows[u + 1];
|
|
1212
|
+
for (uint col = G_rows[u]; col < row_end; ++col) {
|
|
1213
|
+
const int v = G_cols[col];
|
|
1214
|
+
const real1 val = get_G_m(G_data, col, segment_size);
|
|
1215
|
+
const bool v_bit = get_local_bit(theta_local, v);
|
|
1216
|
+
if (u_bit != v_bit) {
|
|
1217
|
+
energy += val;
|
|
1218
|
+
} else if (is_spin_glass) {
|
|
1219
|
+
energy -= val;
|
|
1220
|
+
}
|
|
1253
1221
|
}
|
|
1254
1222
|
}
|
|
1255
|
-
if (is_spin_glass) {
|
|
1256
|
-
delta *= 2.0;
|
|
1257
|
-
}
|
|
1258
1223
|
|
|
1259
|
-
if (
|
|
1260
|
-
best_energy
|
|
1224
|
+
if (energy > best_energy) {
|
|
1225
|
+
best_energy = energy;
|
|
1261
1226
|
} else {
|
|
1262
1227
|
theta_local[flip_bit >> 6U] ^= 1UL << (flip_bit & 63U);
|
|
1263
1228
|
}
|
|
@@ -318,73 +318,32 @@ def convert_bool_to_uint(samples):
|
|
|
318
318
|
|
|
319
319
|
@njit(cache=True)
|
|
320
320
|
def compute_energy(sample, G_m, n_qubits):
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
u_bit = sample[u]
|
|
324
|
-
for v in range(u + 1, n_qubits):
|
|
325
|
-
val = G_m[u, v]
|
|
326
|
-
energy += -val if u_bit == sample[v] else val
|
|
321
|
+
result = np.repeat(sample, n_qubits).reshape(-1, n_qubits) ^ np.broadcast_to(sample, (n_qubits, n_qubits))
|
|
322
|
+
result = (result << 1) - 1
|
|
327
323
|
|
|
328
|
-
return
|
|
324
|
+
return (result * G_m).sum() / 2.0
|
|
329
325
|
|
|
330
326
|
|
|
331
327
|
@njit(cache=True)
|
|
332
328
|
def compute_cut(sample, G_m, n_qubits):
|
|
333
|
-
|
|
334
|
-
# The previous l×r loop double-counted on symmetric matrices and
|
|
335
|
-
# gave wrong results on upper-triangular matrices.
|
|
336
|
-
cut = 0.0
|
|
337
|
-
for u in range(n_qubits):
|
|
338
|
-
for v in range(u + 1, n_qubits):
|
|
339
|
-
if sample[u] != sample[v]:
|
|
340
|
-
cut += G_m[u, v]
|
|
329
|
+
result = np.repeat(sample, n_qubits).reshape(-1, n_qubits) ^ np.broadcast_to(sample, (n_qubits, n_qubits))
|
|
341
330
|
|
|
342
|
-
return
|
|
331
|
+
return (result * G_m).sum() / 2.0
|
|
343
332
|
|
|
344
333
|
|
|
345
334
|
@njit(cache=True)
|
|
346
335
|
def compute_cut_diff(u, sample, G_m, n_qubits):
|
|
347
|
-
|
|
348
|
-
u_bit = sample[u]
|
|
349
|
-
G_u = G_m[u]
|
|
350
|
-
for v in range(u):
|
|
351
|
-
val = G_u[v]
|
|
352
|
-
energy += -val if u_bit == sample[v] else val
|
|
353
|
-
for v in range(u + 1, n_qubits):
|
|
354
|
-
val = G_u[v]
|
|
355
|
-
energy += -val if u_bit == sample[v] else val
|
|
356
|
-
|
|
357
|
-
return energy
|
|
336
|
+
return (G_m[u] * (((sample[u] ^ sample) << 1) - 1)).sum()
|
|
358
337
|
|
|
359
338
|
|
|
360
339
|
@njit(cache=True)
|
|
361
340
|
def compute_cut_diff_2(k, l, sample, G_m, n_qubits):
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
energy = 0.0
|
|
367
|
-
k_bit = sample[k]
|
|
368
|
-
l_bit = sample[l]
|
|
369
|
-
G_k = G_m[k]
|
|
370
|
-
G_l = G_m[l]
|
|
371
|
-
for v in range(k):
|
|
372
|
-
val = G_k[v]
|
|
373
|
-
energy += -val if k_bit == sample[v] else val
|
|
374
|
-
val = G_l[v]
|
|
375
|
-
energy += -val if l_bit == sample[v] else val
|
|
376
|
-
for v in range(k + 1, l):
|
|
377
|
-
val = G_k[v]
|
|
378
|
-
energy += -val if k_bit == sample[v] else val
|
|
379
|
-
val = G_l[v]
|
|
380
|
-
energy += -val if l_bit == sample[v] else val
|
|
381
|
-
for v in range(l + 1, n_qubits):
|
|
382
|
-
val = G_k[v]
|
|
383
|
-
energy += -val if k_bit == sample[v] else val
|
|
384
|
-
val = G_l[v]
|
|
385
|
-
energy += -val if l_bit == sample[v] else val
|
|
341
|
+
k_x = ((sample[k] ^ sample) << 1) - 1
|
|
342
|
+
l_x = ((sample[l] ^ sample) << 1) - 1
|
|
343
|
+
k_x[l] = 0
|
|
344
|
+
l_x[k] = 0
|
|
386
345
|
|
|
387
|
-
return
|
|
346
|
+
return (G_m[k] * k_x).sum() + (G_m[l] * l_x).sum()
|
|
388
347
|
|
|
389
348
|
|
|
390
349
|
@njit(cache=True)
|
|
@@ -401,10 +360,6 @@ def compute_energy_sparse(sample, G_data, G_rows, G_cols, n_qubits):
|
|
|
401
360
|
|
|
402
361
|
@njit(cache=True)
|
|
403
362
|
def compute_cut_sparse(sample, G_data, G_rows, G_cols, n_qubits):
|
|
404
|
-
# Iterate all rows with a j > i guard so each edge is counted once,
|
|
405
|
-
# matching compute_energy_sparse's traversal pattern.
|
|
406
|
-
# The previous smaller-partition-only approach was correct only for
|
|
407
|
-
# symmetric CSR; upper-triangular CSR missed edges in the other half.
|
|
408
363
|
cut = 0.0
|
|
409
364
|
for u in range(n_qubits):
|
|
410
365
|
u_bit = sample[u]
|
|
@@ -416,62 +371,14 @@ def compute_cut_sparse(sample, G_data, G_rows, G_cols, n_qubits):
|
|
|
416
371
|
return cut
|
|
417
372
|
|
|
418
373
|
|
|
419
|
-
@njit(cache=True)
|
|
420
|
-
def compute_cut_diff_sparse(u, sample, G_data, G_rows, G_cols, n_qubits):
|
|
421
|
-
energy = 0.0
|
|
422
|
-
u_bit = sample[u]
|
|
423
|
-
for col in range(G_rows[u], G_rows[u + 1]):
|
|
424
|
-
v = G_cols[col]
|
|
425
|
-
val = G_data[col]
|
|
426
|
-
energy += -val if u_bit == sample[v] else val
|
|
427
|
-
|
|
428
|
-
return energy
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
@njit(cache=True)
|
|
432
|
-
def compute_energy_diff_sparse(u, sample, G_data, G_rows, G_cols, n_qubits):
|
|
433
|
-
energy = 0.0
|
|
434
|
-
u_bit = sample[u]
|
|
435
|
-
for col in range(G_rows[u], G_rows[u + 1]):
|
|
436
|
-
v = G_cols[col]
|
|
437
|
-
val = G_data[col]
|
|
438
|
-
energy += -val if u_bit == sample[v] else val
|
|
439
|
-
|
|
440
|
-
return energy
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
@njit(cache=True)
|
|
444
|
-
def compute_cut_diff_2_sparse(k, l, sample, G_data, G_rows, G_cols, n_qubits):
|
|
445
|
-
if l < k:
|
|
446
|
-
t = k
|
|
447
|
-
k = l
|
|
448
|
-
l = t
|
|
449
|
-
energy = 0.0
|
|
450
|
-
k_bit = sample[k]
|
|
451
|
-
l_bit = sample[l]
|
|
452
|
-
for col in range(G_rows[k], G_rows[k + 1]):
|
|
453
|
-
v = G_cols[col]
|
|
454
|
-
if v == l:
|
|
455
|
-
continue
|
|
456
|
-
val = G_data[col]
|
|
457
|
-
energy += -val if k_bit == sample[v] else val
|
|
458
|
-
for col in range(G_rows[l], G_rows[l + 1]):
|
|
459
|
-
v = G_cols[col]
|
|
460
|
-
if v == k:
|
|
461
|
-
continue
|
|
462
|
-
val = G_data[col]
|
|
463
|
-
energy += -val if l_bit == sample[v] else val
|
|
464
|
-
|
|
465
|
-
return energy
|
|
466
|
-
|
|
467
|
-
|
|
468
374
|
@njit(cache=True)
|
|
469
375
|
def compute_energy_streaming(sample, G_func, nodes, n_qubits):
|
|
470
376
|
energy = 0.0
|
|
471
377
|
for u in range(n_qubits):
|
|
472
378
|
u_bit = sample[u]
|
|
379
|
+
u_node = nodes[u]
|
|
473
380
|
for v in range(u + 1, n_qubits):
|
|
474
|
-
val = G_func(
|
|
381
|
+
val = G_func(u_node, nodes[v])
|
|
475
382
|
energy += -val if u_bit == sample[v] else val
|
|
476
383
|
|
|
477
384
|
return energy
|
|
@@ -479,14 +386,13 @@ def compute_energy_streaming(sample, G_func, nodes, n_qubits):
|
|
|
479
386
|
|
|
480
387
|
@njit(cache=True)
|
|
481
388
|
def compute_cut_streaming(sample, G_func, nodes, n_qubits):
|
|
482
|
-
# Iterate u < v pairs only, matching compute_energy_streaming.
|
|
483
|
-
# The previous l×r loop double-counted on symmetric G_func and
|
|
484
|
-
# gave wrong results when G_func is not symmetric.
|
|
485
389
|
cut = 0.0
|
|
486
390
|
for u in range(n_qubits):
|
|
391
|
+
u_bit = sample[u]
|
|
392
|
+
u_node = nodes[u]
|
|
487
393
|
for v in range(u + 1, n_qubits):
|
|
488
|
-
if
|
|
489
|
-
cut += G_func(
|
|
394
|
+
if u_bit != sample[v]:
|
|
395
|
+
cut += G_func(u_node, nodes[v])
|
|
490
396
|
|
|
491
397
|
return cut
|
|
492
398
|
|
|
@@ -495,11 +401,12 @@ def compute_cut_streaming(sample, G_func, nodes, n_qubits):
|
|
|
495
401
|
def compute_cut_diff_streaming(u, sample, G_func, nodes, n_qubits):
|
|
496
402
|
energy = 0.0
|
|
497
403
|
u_bit = sample[u]
|
|
404
|
+
u_node = nodes[u]
|
|
498
405
|
for v in range(u):
|
|
499
|
-
val = G_func(
|
|
406
|
+
val = G_func(u_node, nodes[v])
|
|
500
407
|
energy += -val if u_bit == sample[v] else val
|
|
501
408
|
for v in range(u + 1, n_qubits):
|
|
502
|
-
val = G_func(
|
|
409
|
+
val = G_func(u_node, nodes[v])
|
|
503
410
|
energy += -val if u_bit == sample[v] else val
|
|
504
411
|
|
|
505
412
|
return energy
|
|
@@ -513,21 +420,23 @@ def compute_cut_diff_2_streaming(k, l, sample, G_func, nodes, n_qubits):
|
|
|
513
420
|
l = t
|
|
514
421
|
energy = 0.0
|
|
515
422
|
k_bit = sample[k]
|
|
423
|
+
k_node = nodes[k]
|
|
516
424
|
l_bit = sample[l]
|
|
425
|
+
l_node = nodes[l]
|
|
517
426
|
for v in range(k):
|
|
518
|
-
val = G_func(
|
|
427
|
+
val = G_func(k_node, nodes[v])
|
|
519
428
|
energy += -val if k_bit == sample[v] else val
|
|
520
|
-
val = G_func(
|
|
429
|
+
val = G_func(l_node, nodes[v])
|
|
521
430
|
energy += -val if l_bit == sample[v] else val
|
|
522
431
|
for v in range(k + 1, l):
|
|
523
|
-
val = G_func(
|
|
432
|
+
val = G_func(k_node, nodes[v])
|
|
524
433
|
energy += -val if k_bit == sample[v] else val
|
|
525
|
-
val = G_func(
|
|
434
|
+
val = G_func(l_node, nodes[v])
|
|
526
435
|
energy += -val if l_bit == sample[v] else val
|
|
527
436
|
for v in range(l + 1, n_qubits):
|
|
528
|
-
val = G_func(
|
|
437
|
+
val = G_func(k_node, nodes[v])
|
|
529
438
|
energy += -val if k_bit == sample[v] else val
|
|
530
|
-
val = G_func(
|
|
439
|
+
val = G_func(l_node, nodes[v])
|
|
531
440
|
energy += -val if l_bit == sample[v] else val
|
|
532
441
|
|
|
533
442
|
return energy
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
from .maxcut_tfim import maxcut_tfim
|
|
2
|
+
from .spin_glass_solver_sparse import spin_glass_solver_sparse
|
|
3
|
+
|
|
4
|
+
import networkx as nx
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def spin_glass_solver_hybrid(
|
|
8
|
+
G,
|
|
9
|
+
quality=None,
|
|
10
|
+
shots=None,
|
|
11
|
+
is_maxcut_gpu=True,
|
|
12
|
+
is_spin_glass=True,
|
|
13
|
+
anneal_t=None,
|
|
14
|
+
anneal_h=None,
|
|
15
|
+
repulsion_base=None,
|
|
16
|
+
is_log=False,
|
|
17
|
+
gray_iterations=None,
|
|
18
|
+
gray_seed_multiple=None,
|
|
19
|
+
bp_scale=None,
|
|
20
|
+
bp_damping=0.5,
|
|
21
|
+
):
|
|
22
|
+
if not isinstance(G, nx.Graph):
|
|
23
|
+
raise TypeError("G graph must be a networkx.Graph for maxcut_hybrid!")
|
|
24
|
+
|
|
25
|
+
guess, _, _ = maxcut_tfim(
|
|
26
|
+
G,
|
|
27
|
+
quality=quality,
|
|
28
|
+
shots=shots,
|
|
29
|
+
repulsion_base=repulsion_base,
|
|
30
|
+
is_maxcut_gpu=is_maxcut_gpu,
|
|
31
|
+
is_spin_glass=is_spin_glass,
|
|
32
|
+
anneal_t=anneal_t,
|
|
33
|
+
anneal_h=anneal_h
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
return spin_glass_solver_sparse(
|
|
37
|
+
G,
|
|
38
|
+
best_guess=guess,
|
|
39
|
+
is_maxcut_gpu=is_maxcut_gpu,
|
|
40
|
+
is_spin_glass=is_spin_glass,
|
|
41
|
+
is_log=is_log,
|
|
42
|
+
gray_iterations=gray_iterations,
|
|
43
|
+
gray_seed_multiple=gray_seed_multiple,
|
|
44
|
+
bp_scale=bp_scale,
|
|
45
|
+
bp_damping=bp_damping
|
|
46
|
+
)
|
|
@@ -2,8 +2,6 @@ from .maxcut_tfim_sparse import maxcut_tfim_sparse
|
|
|
2
2
|
from .maxcut_tfim_util import (
|
|
3
3
|
compute_cut_sparse,
|
|
4
4
|
compute_energy_sparse,
|
|
5
|
-
compute_cut_diff_sparse,
|
|
6
|
-
compute_cut_diff_2_sparse,
|
|
7
5
|
get_cut,
|
|
8
6
|
gray_code_next,
|
|
9
7
|
gray_mutation,
|
|
@@ -37,15 +35,22 @@ gnl = opencl_context.GRAY_NODE_LIMIT
|
|
|
37
35
|
@njit(parallel=True, cache=True)
|
|
38
36
|
def run_single_bit_flips(best_theta, is_spin_glass, G_data, G_rows, G_cols):
|
|
39
37
|
n = len(best_theta)
|
|
38
|
+
|
|
40
39
|
energies = np.empty(n, dtype=dtype)
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
+
|
|
45
52
|
best_index = np.argmax(energies)
|
|
46
53
|
best_energy = energies[best_index]
|
|
47
|
-
if is_spin_glass:
|
|
48
|
-
best_energy *= 2.0
|
|
49
54
|
best_state = best_theta.copy()
|
|
50
55
|
best_state[best_index] = not best_state[best_index]
|
|
51
56
|
|
|
@@ -61,32 +66,53 @@ def run_double_bit_flips(best_theta, is_spin_glass, G_data, G_rows, G_cols, thre
|
|
|
61
66
|
states = np.empty((thread_count, n), dtype=np.bool_)
|
|
62
67
|
energies = np.empty(thread_count, dtype=dtype)
|
|
63
68
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
c
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
69
|
+
if is_spin_glass:
|
|
70
|
+
for t in prange(thread_count):
|
|
71
|
+
s = int(t)
|
|
72
|
+
while s < combo_count:
|
|
73
|
+
c = int(s)
|
|
74
|
+
i = int(0)
|
|
75
|
+
lcv = int(n - 1)
|
|
76
|
+
while c >= lcv:
|
|
77
|
+
c -= lcv
|
|
78
|
+
i += 1
|
|
79
|
+
lcv -= 1
|
|
80
|
+
if not lcv:
|
|
81
|
+
break
|
|
82
|
+
j = int(c + i + 1)
|
|
83
|
+
|
|
84
|
+
state = best_theta.copy()
|
|
85
|
+
state[i] = not state[i]
|
|
86
|
+
state[j] = not state[j]
|
|
87
|
+
|
|
88
|
+
states[t], energies[t] = state, compute_energy_sparse(state, G_data, G_rows, G_cols, n)
|
|
89
|
+
|
|
90
|
+
s += thread_batch
|
|
91
|
+
else:
|
|
92
|
+
for t in prange(thread_count):
|
|
93
|
+
s = int(t)
|
|
94
|
+
while s < combo_count:
|
|
95
|
+
c = int(s)
|
|
96
|
+
i = int(0)
|
|
97
|
+
lcv = int(n - 1)
|
|
98
|
+
while c >= lcv:
|
|
99
|
+
c -= lcv
|
|
100
|
+
i += 1
|
|
101
|
+
lcv -= 1
|
|
102
|
+
if not lcv:
|
|
103
|
+
break
|
|
104
|
+
j = int(c + i + 1)
|
|
105
|
+
|
|
106
|
+
state = best_theta.copy()
|
|
107
|
+
state[i] = not state[i]
|
|
108
|
+
state[j] = not state[j]
|
|
109
|
+
|
|
110
|
+
states[t], energies[t] = state, compute_cut_sparse(state, G_data, G_rows, G_cols, n)
|
|
111
|
+
|
|
112
|
+
s += thread_batch
|
|
85
113
|
|
|
86
114
|
best_index = np.argmax(energies)
|
|
87
115
|
best_energy = energies[best_index]
|
|
88
|
-
if is_spin_glass:
|
|
89
|
-
best_energy *= 2.0
|
|
90
116
|
best_state = states[best_index]
|
|
91
117
|
|
|
92
118
|
return best_energy, best_state
|
|
@@ -129,30 +155,54 @@ def pick_gray_seeds(best_theta, thread_count, gray_seed_multiple, G_data, G_rows
|
|
|
129
155
|
|
|
130
156
|
|
|
131
157
|
@njit(parallel=True, cache=True)
|
|
132
|
-
def run_gray_optimization(
|
|
158
|
+
def run_gray_optimization(
|
|
159
|
+
best_theta,
|
|
160
|
+
iterators,
|
|
161
|
+
energies,
|
|
162
|
+
gray_iterations,
|
|
163
|
+
thread_count,
|
|
164
|
+
is_spin_glass,
|
|
165
|
+
G_data,
|
|
166
|
+
G_rows,
|
|
167
|
+
G_cols,
|
|
168
|
+
):
|
|
133
169
|
n = len(best_theta)
|
|
134
170
|
thread_iterations = (gray_iterations + thread_count - 1) // thread_count
|
|
135
171
|
blocks = (n + 63) >> 6
|
|
136
|
-
energies = np.empty(thread_count, dtype=dtype)
|
|
137
172
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
best_energy =
|
|
142
|
-
for
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
173
|
+
if is_spin_glass:
|
|
174
|
+
for i in prange(thread_count):
|
|
175
|
+
iterator = iterators[i]
|
|
176
|
+
best_energy = energies[i]
|
|
177
|
+
for curr_idx in range(thread_iterations):
|
|
178
|
+
for block in range(blocks):
|
|
179
|
+
flip_bit = gray_code_next(iterator, curr_idx, block << 6)
|
|
180
|
+
energy = compute_energy_sparse(iterator, G_data, G_rows, G_cols, n)
|
|
181
|
+
if energy > best_energy:
|
|
182
|
+
best_energy = energy
|
|
183
|
+
else:
|
|
184
|
+
# Revert iterator
|
|
185
|
+
iterator[flip_bit] = not iterator[flip_bit]
|
|
186
|
+
if best_energy > energies[i]:
|
|
187
|
+
energies[i] = best_energy
|
|
188
|
+
else:
|
|
189
|
+
for i in prange(thread_count):
|
|
190
|
+
iterator = iterators[i]
|
|
191
|
+
best_energy = energies[i]
|
|
192
|
+
for curr_idx in range(thread_iterations):
|
|
193
|
+
for block in range(blocks):
|
|
194
|
+
flip_bit = gray_code_next(iterator, curr_idx, block << 6)
|
|
195
|
+
energy = compute_cut_sparse(iterator, G_data, G_rows, G_cols, n)
|
|
196
|
+
if energy > best_energy:
|
|
197
|
+
best_energy = energy
|
|
198
|
+
else:
|
|
199
|
+
# Revert iterator
|
|
200
|
+
iterator[flip_bit] = not iterator[flip_bit]
|
|
201
|
+
if best_energy > energies[i]:
|
|
202
|
+
energies[i] = best_energy
|
|
151
203
|
|
|
152
204
|
best_index = np.argmax(energies)
|
|
153
205
|
best_energy = energies[best_index]
|
|
154
|
-
if is_spin_glass:
|
|
155
|
-
best_energy *= 2.0
|
|
156
206
|
best_state = iterators[best_index]
|
|
157
207
|
|
|
158
208
|
return best_energy, best_state
|
|
@@ -223,9 +273,9 @@ def run_bit_flips_opencl(
|
|
|
223
273
|
best_x = np.argmax(max_energy_host)
|
|
224
274
|
energy = max_energy_host[best_x]
|
|
225
275
|
|
|
226
|
-
if energy <=
|
|
276
|
+
if energy <= best_energy:
|
|
227
277
|
# No improvement: we can exit early
|
|
228
|
-
return
|
|
278
|
+
return best_energy, theta
|
|
229
279
|
|
|
230
280
|
# We need the best index
|
|
231
281
|
queue.finish()
|
|
@@ -303,9 +353,9 @@ def run_gray_search_opencl(
|
|
|
303
353
|
best_x = np.argmax(max_energy_host)
|
|
304
354
|
energy = max_energy_host[best_x]
|
|
305
355
|
|
|
306
|
-
if energy <=
|
|
356
|
+
if energy <= best_energy:
|
|
307
357
|
# No improvement: we can exit early
|
|
308
|
-
return
|
|
358
|
+
return best_energy, theta
|
|
309
359
|
|
|
310
360
|
# We need the best index
|
|
311
361
|
queue.finish()
|
|
@@ -19,6 +19,7 @@ pyqrackising/solve_maxcut_exact.py
|
|
|
19
19
|
pyqrackising/solve_maxcut_exact_sparse.py
|
|
20
20
|
pyqrackising/solve_maxcut_exact_streaming.py
|
|
21
21
|
pyqrackising/spin_glass_solver.py
|
|
22
|
+
pyqrackising/spin_glass_solver_hybrid.py
|
|
22
23
|
pyqrackising/spin_glass_solver_sparse.py
|
|
23
24
|
pyqrackising/spin_glass_solver_streaming.py
|
|
24
25
|
pyqrackising/tfim_magnetization.py
|
|
@@ -7,7 +7,7 @@ with open(README_PATH) as readme_file:
|
|
|
7
7
|
|
|
8
8
|
setup(
|
|
9
9
|
name="pyqrackising",
|
|
10
|
-
version="9.
|
|
10
|
+
version="9.16.0",
|
|
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
|