PyQrackIsing 9.15.3__tar.gz → 9.15.5__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.3/pyqrackising.egg-info → pyqrackising-9.15.5}/PKG-INFO +1 -1
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyproject.toml +1 -1
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising/kernels.cl +81 -116
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising/maxcut_tfim_util.py +28 -70
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising/spin_glass_solver_sparse.py +113 -116
- {pyqrackising-9.15.3 → pyqrackising-9.15.5/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/setup.py +1 -1
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/LICENSE.md +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/MANIFEST.in +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/README.md +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising/__init__.py +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising/maxcut_tfim.py +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising/maxcut_tfim_sparse.py +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising/maxcut_tfim_streaming.py +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising/otoc.py +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising/solve_maxcut_bnb.py +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising/solve_maxcut_bnb_sparse.py +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising/solve_maxcut_bnb_streaming.py +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising/solve_maxcut_exact.py +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising/solve_maxcut_exact_sparse.py +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising/solve_maxcut_exact_streaming.py +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising/spin_glass_solver.py +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising/spin_glass_solver_streaming.py +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising/tsp.py +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising/tsp_maxcut.py +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising.egg-info/requires.txt +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-9.15.3 → pyqrackising-9.15.5}/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.5"
|
|
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,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]
|
|
@@ -421,8 +376,9 @@ def compute_energy_streaming(sample, G_func, nodes, n_qubits):
|
|
|
421
376
|
energy = 0.0
|
|
422
377
|
for u in range(n_qubits):
|
|
423
378
|
u_bit = sample[u]
|
|
379
|
+
u_node = nodes[u]
|
|
424
380
|
for v in range(u + 1, n_qubits):
|
|
425
|
-
val = G_func(
|
|
381
|
+
val = G_func(u_node, nodes[v])
|
|
426
382
|
energy += -val if u_bit == sample[v] else val
|
|
427
383
|
|
|
428
384
|
return energy
|
|
@@ -430,14 +386,13 @@ def compute_energy_streaming(sample, G_func, nodes, n_qubits):
|
|
|
430
386
|
|
|
431
387
|
@njit(cache=True)
|
|
432
388
|
def compute_cut_streaming(sample, G_func, nodes, n_qubits):
|
|
433
|
-
# Iterate u < v pairs only, matching compute_energy_streaming.
|
|
434
|
-
# The previous l×r loop double-counted on symmetric G_func and
|
|
435
|
-
# gave wrong results when G_func is not symmetric.
|
|
436
389
|
cut = 0.0
|
|
437
390
|
for u in range(n_qubits):
|
|
391
|
+
u_bit = sample[u]
|
|
392
|
+
u_node = nodes[u]
|
|
438
393
|
for v in range(u + 1, n_qubits):
|
|
439
|
-
if
|
|
440
|
-
cut += G_func(
|
|
394
|
+
if u_bit != sample[v]:
|
|
395
|
+
cut += G_func(u_node, nodes[v])
|
|
441
396
|
|
|
442
397
|
return cut
|
|
443
398
|
|
|
@@ -446,11 +401,12 @@ def compute_cut_streaming(sample, G_func, nodes, n_qubits):
|
|
|
446
401
|
def compute_cut_diff_streaming(u, sample, G_func, nodes, n_qubits):
|
|
447
402
|
energy = 0.0
|
|
448
403
|
u_bit = sample[u]
|
|
404
|
+
u_node = nodes[u]
|
|
449
405
|
for v in range(u):
|
|
450
|
-
val = G_func(
|
|
406
|
+
val = G_func(u_node, nodes[v])
|
|
451
407
|
energy += -val if u_bit == sample[v] else val
|
|
452
408
|
for v in range(u + 1, n_qubits):
|
|
453
|
-
val = G_func(
|
|
409
|
+
val = G_func(u_node, nodes[v])
|
|
454
410
|
energy += -val if u_bit == sample[v] else val
|
|
455
411
|
|
|
456
412
|
return energy
|
|
@@ -464,21 +420,23 @@ def compute_cut_diff_2_streaming(k, l, sample, G_func, nodes, n_qubits):
|
|
|
464
420
|
l = t
|
|
465
421
|
energy = 0.0
|
|
466
422
|
k_bit = sample[k]
|
|
423
|
+
k_node = nodes[k]
|
|
467
424
|
l_bit = sample[l]
|
|
425
|
+
l_node = nodes[l]
|
|
468
426
|
for v in range(k):
|
|
469
|
-
val = G_func(
|
|
427
|
+
val = G_func(k_node, nodes[v])
|
|
470
428
|
energy += -val if k_bit == sample[v] else val
|
|
471
|
-
val = G_func(
|
|
429
|
+
val = G_func(l_node, nodes[v])
|
|
472
430
|
energy += -val if l_bit == sample[v] else val
|
|
473
431
|
for v in range(k + 1, l):
|
|
474
|
-
val = G_func(
|
|
432
|
+
val = G_func(k_node, nodes[v])
|
|
475
433
|
energy += -val if k_bit == sample[v] else val
|
|
476
|
-
val = G_func(
|
|
434
|
+
val = G_func(l_node, nodes[v])
|
|
477
435
|
energy += -val if l_bit == sample[v] else val
|
|
478
436
|
for v in range(l + 1, n_qubits):
|
|
479
|
-
val = G_func(
|
|
437
|
+
val = G_func(k_node, nodes[v])
|
|
480
438
|
energy += -val if k_bit == sample[v] else val
|
|
481
|
-
val = G_func(
|
|
439
|
+
val = G_func(l_node, nodes[v])
|
|
482
440
|
energy += -val if l_bit == sample[v] else val
|
|
483
441
|
|
|
484
442
|
return energy
|
|
@@ -35,31 +35,26 @@ 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
|
|
56
38
|
|
|
57
|
-
|
|
58
|
-
|
|
39
|
+
energies = np.empty(n, dtype=dtype)
|
|
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]
|
|
59
54
|
best_state = best_theta.copy()
|
|
60
55
|
best_state[best_index] = not best_state[best_index]
|
|
61
56
|
|
|
62
|
-
return
|
|
57
|
+
return best_energy, best_state
|
|
63
58
|
|
|
64
59
|
|
|
65
60
|
@njit(parallel=True, cache=True)
|
|
@@ -69,65 +64,58 @@ def run_double_bit_flips(best_theta, is_spin_glass, G_data, G_rows, G_cols, thre
|
|
|
69
64
|
thread_batch = (combo_count + thread_count - 1) // thread_count
|
|
70
65
|
|
|
71
66
|
states = np.empty((thread_count, n), dtype=np.bool_)
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
c
|
|
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
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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]
|
|
67
|
+
energies = np.empty(thread_count, dtype=dtype)
|
|
68
|
+
|
|
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
|
|
113
|
+
|
|
114
|
+
best_index = np.argmax(energies)
|
|
115
|
+
best_energy = energies[best_index]
|
|
128
116
|
best_state = states[best_index]
|
|
129
117
|
|
|
130
|
-
return
|
|
118
|
+
return best_energy, best_state
|
|
131
119
|
|
|
132
120
|
|
|
133
121
|
@njit(parallel=True, cache=True)
|
|
@@ -182,32 +170,36 @@ def run_gray_optimization(
|
|
|
182
170
|
thread_iterations = (gray_iterations + thread_count - 1) // thread_count
|
|
183
171
|
blocks = (n + 63) >> 6
|
|
184
172
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
for
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
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
|
|
211
203
|
|
|
212
204
|
best_index = np.argmax(energies)
|
|
213
205
|
best_energy = energies[best_index]
|
|
@@ -220,6 +212,7 @@ def run_bit_flips_opencl(
|
|
|
220
212
|
is_double,
|
|
221
213
|
n,
|
|
222
214
|
kernel,
|
|
215
|
+
best_energy,
|
|
223
216
|
theta,
|
|
224
217
|
theta_buf,
|
|
225
218
|
G_data_buf,
|
|
@@ -276,13 +269,13 @@ def run_bit_flips_opencl(
|
|
|
276
269
|
# Queue read for results we might not need
|
|
277
270
|
cl.enqueue_copy(queue, max_index_host, max_index_buf)
|
|
278
271
|
|
|
279
|
-
# Find global
|
|
272
|
+
# Find global minimum
|
|
280
273
|
best_x = np.argmax(max_energy_host)
|
|
281
274
|
energy = max_energy_host[best_x]
|
|
282
275
|
|
|
283
|
-
if energy <=
|
|
284
|
-
# No improvement:
|
|
285
|
-
return
|
|
276
|
+
if energy <= best_energy:
|
|
277
|
+
# No improvement: we can exit early
|
|
278
|
+
return best_energy, theta
|
|
286
279
|
|
|
287
280
|
# We need the best index
|
|
288
281
|
queue.finish()
|
|
@@ -715,6 +708,7 @@ def spin_glass_solver_sparse(
|
|
|
715
708
|
False,
|
|
716
709
|
n_qubits,
|
|
717
710
|
single_bit_flips_kernel,
|
|
711
|
+
max_energy,
|
|
718
712
|
best_theta,
|
|
719
713
|
theta_buf,
|
|
720
714
|
G_data_buf,
|
|
@@ -725,8 +719,8 @@ def spin_glass_solver_sparse(
|
|
|
725
719
|
)
|
|
726
720
|
else:
|
|
727
721
|
energy, state = run_single_bit_flips(best_theta, is_spin_glass, G_m.data, G_m.indptr, G_m.indices)
|
|
728
|
-
if energy >
|
|
729
|
-
max_energy
|
|
722
|
+
if energy > max_energy:
|
|
723
|
+
max_energy = energy
|
|
730
724
|
best_theta = state
|
|
731
725
|
improved = True
|
|
732
726
|
continue
|
|
@@ -738,6 +732,7 @@ def spin_glass_solver_sparse(
|
|
|
738
732
|
True,
|
|
739
733
|
n_qubits,
|
|
740
734
|
double_bit_flips_kernel,
|
|
735
|
+
max_energy,
|
|
741
736
|
best_theta,
|
|
742
737
|
theta_buf,
|
|
743
738
|
G_data_buf,
|
|
@@ -748,8 +743,8 @@ def spin_glass_solver_sparse(
|
|
|
748
743
|
)
|
|
749
744
|
else:
|
|
750
745
|
energy, state = run_double_bit_flips(best_theta, is_spin_glass, G_m.data, G_m.indptr, G_m.indices, thread_count)
|
|
751
|
-
if energy >
|
|
752
|
-
max_energy
|
|
746
|
+
if energy > max_energy:
|
|
747
|
+
max_energy = energy
|
|
753
748
|
best_theta = state
|
|
754
749
|
improved = True
|
|
755
750
|
continue
|
|
@@ -815,6 +810,7 @@ def spin_glass_solver_sparse(
|
|
|
815
810
|
False,
|
|
816
811
|
n_qubits,
|
|
817
812
|
single_bit_flips_kernel,
|
|
813
|
+
max_energy,
|
|
818
814
|
reheat_theta,
|
|
819
815
|
theta_buf,
|
|
820
816
|
G_data_buf,
|
|
@@ -825,8 +821,8 @@ def spin_glass_solver_sparse(
|
|
|
825
821
|
)
|
|
826
822
|
else:
|
|
827
823
|
energy, state = run_single_bit_flips(reheat_theta, is_spin_glass, G_m.data, G_m.indptr, G_m.indices)
|
|
828
|
-
if energy >
|
|
829
|
-
max_energy
|
|
824
|
+
if energy > max_energy:
|
|
825
|
+
max_energy = energy
|
|
830
826
|
best_theta = state
|
|
831
827
|
improved = True
|
|
832
828
|
continue
|
|
@@ -841,6 +837,7 @@ def spin_glass_solver_sparse(
|
|
|
841
837
|
True,
|
|
842
838
|
n_qubits,
|
|
843
839
|
double_bit_flips_kernel,
|
|
840
|
+
max_energy,
|
|
844
841
|
reheat_theta,
|
|
845
842
|
theta_buf,
|
|
846
843
|
G_data_buf,
|
|
@@ -851,8 +848,8 @@ def spin_glass_solver_sparse(
|
|
|
851
848
|
)
|
|
852
849
|
else:
|
|
853
850
|
energy, state = run_double_bit_flips(reheat_theta, is_spin_glass, G_m.data, G_m.indptr, G_m.indices, thread_count)
|
|
854
|
-
if energy >
|
|
855
|
-
max_energy
|
|
851
|
+
if energy > max_energy:
|
|
852
|
+
max_energy = energy
|
|
856
853
|
best_theta = state
|
|
857
854
|
improved = True
|
|
858
855
|
|
|
@@ -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.5",
|
|
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
|