PyQrackIsing 5.7.4__tar.gz → 6.0.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-5.7.4/pyqrackising.egg-info → pyqrackising-6.0.0}/PKG-INFO +1 -1
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/README.md +2 -2
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/pyproject.toml +1 -1
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/pyqrackising/kernels.cl +91 -99
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/pyqrackising/maxcut_tfim.py +6 -6
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/pyqrackising/maxcut_tfim_sparse.py +5 -5
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/pyqrackising/spin_glass_solver.py +2 -3
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/pyqrackising/spin_glass_solver_sparse.py +2 -3
- {pyqrackising-5.7.4 → pyqrackising-6.0.0/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/setup.py +1 -1
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/CMakeLists.txt +0 -0
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/LICENSE.md +0 -0
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/MANIFEST.in +0 -0
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/pyqrackising/__init__.py +0 -0
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/pyqrackising/maxcut_tfim_streaming.py +0 -0
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/pyqrackising/maxcut_tfim_util.py +0 -0
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/pyqrackising/spin_glass_solver_streaming.py +0 -0
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/pyqrackising/spin_glass_solver_util.py +0 -0
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/pyqrackising/tsp.py +0 -0
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-5.7.4 → pyqrackising-6.0.0}/setup.cfg +0 -0
|
@@ -44,7 +44,7 @@ samples = generate_tfim_samples(
|
|
|
44
44
|
|
|
45
45
|
There are two other functions, `tfim_magnetization()` and `tfim_square_magnetization()`, that follow the same function signature except without the `shots` argument.
|
|
46
46
|
|
|
47
|
-
The library also provides a TFIM-inspired (approximate) MAXCUT solver:
|
|
47
|
+
The library also provides a TFIM-inspired (approximate) MAXCUT solver (which accepts a `networkx` graph or a 32-bit adjacency matrix):
|
|
48
48
|
```py
|
|
49
49
|
from pyqrackising import maxcut_tfim
|
|
50
50
|
import networkx as nx
|
|
@@ -76,7 +76,7 @@ G = generate_spin_glass_graph(n_nodes=64, seed=42)
|
|
|
76
76
|
solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=2, best_guess=None, is_base_maxcut_gpu=True, is_alt_gpu_sampling=True, is_combo_maxcut_gpu=True)
|
|
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)`, for `scipy` **upper-triangular** CSR sparse arrays (or `networkx` graphs). The (integer) default `quality` setting is `2`. `is_combo_maxcut_gpu` controls whether gradient descent optimization is done on GPU (which is the most costly GPU-based feature), while `is_base_maxcut_gpu` is passed through the underlying `maxcut_tfim(G)` 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. 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)`, for `scipy` (32-bit) **upper-triangular** CSR sparse arrays (or `networkx` graphs). The (integer) default `quality` setting is `2`. `is_combo_maxcut_gpu` controls whether gradient descent optimization is done on GPU (which is the most costly GPU-based feature), while `is_base_maxcut_gpu` is passed through the underlying `maxcut_tfim(G)` 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. 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 `spin_glass_solver()`, 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 = "
|
|
15
|
+
version = "6.0.0"
|
|
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"}
|
|
@@ -1,69 +1,61 @@
|
|
|
1
1
|
__kernel void init_theta(
|
|
2
|
-
__constant
|
|
2
|
+
__constant float* fargs,
|
|
3
3
|
const int n_qubits,
|
|
4
|
-
__constant
|
|
4
|
+
__constant float* J_eff,
|
|
5
5
|
__constant unsigned* degrees,
|
|
6
|
-
__global
|
|
6
|
+
__global float* theta
|
|
7
7
|
) {
|
|
8
8
|
const int q = get_global_id(0);
|
|
9
9
|
if (q >= n_qubits) {
|
|
10
10
|
return;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
const double J = J_eff[q];
|
|
13
|
+
const float h_mult = fabs(fargs[2]);
|
|
14
|
+
const float J = J_eff[q];
|
|
16
15
|
const unsigned z = degrees[q];
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
float val;
|
|
20
|
-
if (abs_zJ < 2e-52) {
|
|
21
|
-
val = (J > 0.0) ? M_PI_F : -M_PI_F;
|
|
22
|
-
} else {
|
|
23
|
-
val = asin(fmax(-1.0, fmin(1.0, h_mult / (z * J))));
|
|
24
|
-
}
|
|
16
|
+
const float abs_zJ = fabs(z * J);
|
|
25
17
|
|
|
26
|
-
theta[q] =
|
|
18
|
+
theta[q] = (abs_zJ <= FLT_EPSILON) ? ((J > 0.0f) ? M_PI_F : -M_PI_F) : (asin(fmax(-1.0f, fmin(1.0f, h_mult / (z * J)))));
|
|
27
19
|
}
|
|
28
20
|
|
|
29
21
|
// By Google Search AI
|
|
30
|
-
inline void
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
} oldVal, newVal;
|
|
22
|
+
inline void atomic_add_float(__global float* address, float val) {
|
|
23
|
+
int old_val_ll;
|
|
24
|
+
int new_val_ll;
|
|
25
|
+
float old_val_d;
|
|
35
26
|
|
|
36
27
|
do {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
28
|
+
old_val_d = *address;
|
|
29
|
+
old_val_ll = as_int(old_val_d); // Reinterpret float as int
|
|
30
|
+
new_val_ll = as_int(old_val_d + val); // Perform addition, then reinterpret
|
|
31
|
+
|
|
32
|
+
} while (atom_cmpxchg((__global int*)address, old_val_ll, new_val_ll) != old_val_ll);
|
|
41
33
|
}
|
|
42
34
|
|
|
43
35
|
// By Elara (custom OpenAI GPT)
|
|
44
|
-
inline
|
|
45
|
-
int q,
|
|
36
|
+
inline float probability_by_hamming_weight(
|
|
37
|
+
int q, float J, float h, unsigned z, float theta, float t, int n_qubits
|
|
46
38
|
) {
|
|
47
|
-
|
|
48
|
-
if (ratio > 1.
|
|
49
|
-
ratio = 1.
|
|
39
|
+
float ratio = fabs(h) / (z * J);
|
|
40
|
+
if (ratio > 1.0f) {
|
|
41
|
+
ratio = 1.0f;
|
|
50
42
|
}
|
|
51
|
-
|
|
43
|
+
float theta_c = asin(ratio);
|
|
52
44
|
|
|
53
|
-
|
|
54
|
-
* (1.
|
|
55
|
-
- 0.
|
|
45
|
+
float p = pow(2.0f, fabs(J / h) - 1.0f)
|
|
46
|
+
* (1.0f + sin(theta - theta_c) * cos(1.5f * M_PI_F * J * t + theta) / (1.0f + sqrt(t)))
|
|
47
|
+
- 0.5f;
|
|
56
48
|
|
|
57
|
-
if ((p * (n_qubits + 2.
|
|
58
|
-
return 0.
|
|
49
|
+
if ((p * (n_qubits + 2.0f)) >= 128.0f) {
|
|
50
|
+
return 0.0f;
|
|
59
51
|
}
|
|
60
52
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
53
|
+
float numerator = pow(2.0f, (n_qubits + 2.0f) * p) - 1.0f;
|
|
54
|
+
float denominator = pow(2.0f, p) - 1.0f;
|
|
55
|
+
float result = numerator * pow(2.0f, -((n_qubits + 1.0f) * p) - p * q) / denominator;
|
|
64
56
|
|
|
65
57
|
if (isnan(result) || isinf(result)) {
|
|
66
|
-
return 0.
|
|
58
|
+
return 0.0f;
|
|
67
59
|
}
|
|
68
60
|
|
|
69
61
|
return result;
|
|
@@ -72,34 +64,34 @@ inline double probability_by_hamming_weight(
|
|
|
72
64
|
__kernel void maxcut_hamming_cdf(
|
|
73
65
|
int n_qubits,
|
|
74
66
|
__constant unsigned* degrees,
|
|
75
|
-
__constant
|
|
76
|
-
__constant
|
|
77
|
-
__constant
|
|
78
|
-
__global
|
|
67
|
+
__constant float* args,
|
|
68
|
+
__constant float* J_func,
|
|
69
|
+
__constant float* theta,
|
|
70
|
+
__global float* hamming_prob
|
|
79
71
|
) {
|
|
80
|
-
const
|
|
81
|
-
const
|
|
82
|
-
const
|
|
72
|
+
const float delta_t = args[0U];
|
|
73
|
+
const float tot_t = args[1U];
|
|
74
|
+
const float h_mult = args[2U];
|
|
83
75
|
const int step = get_group_id(0) / n_qubits;
|
|
84
76
|
const int qi = get_group_id(0) % n_qubits;
|
|
85
|
-
const
|
|
77
|
+
const float J_eff = J_func[qi];
|
|
86
78
|
const unsigned z = degrees[qi];
|
|
87
|
-
if (fabs(z * J_eff) <=
|
|
79
|
+
if (fabs(z * J_eff) <= FLT_EPSILON) {
|
|
88
80
|
return;
|
|
89
81
|
}
|
|
90
82
|
|
|
91
|
-
const
|
|
92
|
-
const
|
|
93
|
-
const
|
|
94
|
-
const
|
|
83
|
+
const float theta_eff = theta[qi];
|
|
84
|
+
const float t = step * delta_t;
|
|
85
|
+
const float tm1 = (step - 1) * delta_t;
|
|
86
|
+
const float h_t = h_mult * (tot_t - t);
|
|
95
87
|
|
|
96
88
|
const int n_threads = get_local_size(0);
|
|
97
89
|
|
|
98
90
|
for (int qo = get_local_id(0); qo < n_qubits; qo += n_threads) {
|
|
99
91
|
int _qo = (J_eff > 0.0) ? (n_qubits - (1 + qo)) : qo;
|
|
100
|
-
|
|
92
|
+
float diff = probability_by_hamming_weight(_qo, J_eff, h_t, z, theta_eff, t, n_qubits);
|
|
101
93
|
diff -= probability_by_hamming_weight(_qo, J_eff, h_t, z, theta_eff, tm1, n_qubits);
|
|
102
|
-
|
|
94
|
+
atomic_add_float(&(hamming_prob[_qo]), diff);
|
|
103
95
|
}
|
|
104
96
|
}
|
|
105
97
|
|
|
@@ -112,8 +104,8 @@ inline uint xorshift32(uint *state) {
|
|
|
112
104
|
return x;
|
|
113
105
|
}
|
|
114
106
|
|
|
115
|
-
float bootstrap_worker(__constant char* theta, __global
|
|
116
|
-
|
|
107
|
+
float bootstrap_worker(__constant char* theta, __global float* G_m, __constant int* indices, const int k, const int n) {
|
|
108
|
+
float energy = 0.0;
|
|
117
109
|
const size_t n_st = (size_t)n;
|
|
118
110
|
for (int u = 0; u < n; ++u) {
|
|
119
111
|
const size_t u_offset = u * n_st;
|
|
@@ -125,7 +117,7 @@ float bootstrap_worker(__constant char* theta, __global double* G_m, __constant
|
|
|
125
117
|
}
|
|
126
118
|
}
|
|
127
119
|
for (int v = u + 1; v < n; ++v) {
|
|
128
|
-
const
|
|
120
|
+
const float val = G_m[u_offset + v];
|
|
129
121
|
bool v_bit = theta[v];
|
|
130
122
|
for (int x = 0; x < k; ++x) {
|
|
131
123
|
if (indices[x] == v) {
|
|
@@ -137,14 +129,12 @@ float bootstrap_worker(__constant char* theta, __global double* G_m, __constant
|
|
|
137
129
|
}
|
|
138
130
|
}
|
|
139
131
|
|
|
140
|
-
return
|
|
132
|
+
return energy;
|
|
141
133
|
}
|
|
142
134
|
|
|
143
|
-
#define EPSILON 1.1920928955078125e-7
|
|
144
|
-
|
|
145
135
|
__kernel void bootstrap(
|
|
146
136
|
uint prng_seed,
|
|
147
|
-
__global
|
|
137
|
+
__global float* G_m,
|
|
148
138
|
__constant char* best_theta,
|
|
149
139
|
__constant int* indices_array,
|
|
150
140
|
__constant int* args, // args[0] = n, args[1] = k
|
|
@@ -185,7 +175,7 @@ __kernel void bootstrap(
|
|
|
185
175
|
if (hid_energy < lid_energy) {
|
|
186
176
|
loc_energy[lt_id] = hid_energy;
|
|
187
177
|
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
188
|
-
} else if (((hid_energy - lid_energy) <=
|
|
178
|
+
} else if (((hid_energy - lid_energy) <= FLT_EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
189
179
|
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
190
180
|
}
|
|
191
181
|
}
|
|
@@ -198,8 +188,8 @@ __kernel void bootstrap(
|
|
|
198
188
|
}
|
|
199
189
|
}
|
|
200
190
|
|
|
201
|
-
float bootstrap_worker_sparse(__constant char* theta, __global
|
|
202
|
-
|
|
191
|
+
float bootstrap_worker_sparse(__constant char* theta, __global float* G_data, __global unsigned* G_rows, __global unsigned* G_cols, __constant int* indices, const int k, const int n) {
|
|
192
|
+
float energy = 0.0f;
|
|
203
193
|
for (int u = 0; u < n; ++u) {
|
|
204
194
|
bool u_bit = theta[u];
|
|
205
195
|
for (int x = 0; x < k; ++x) {
|
|
@@ -211,7 +201,7 @@ float bootstrap_worker_sparse(__constant char* theta, __global double* G_data, _
|
|
|
211
201
|
const size_t mCol = G_rows[u + 1];
|
|
212
202
|
for (int col = G_rows[u]; col < mCol; ++col) {
|
|
213
203
|
const int v = G_cols[col];
|
|
214
|
-
const
|
|
204
|
+
const float val = G_data[col];
|
|
215
205
|
bool v_bit = theta[v];
|
|
216
206
|
for (int x = 0; x < k; ++x) {
|
|
217
207
|
if (indices[x] == v) {
|
|
@@ -223,12 +213,12 @@ float bootstrap_worker_sparse(__constant char* theta, __global double* G_data, _
|
|
|
223
213
|
}
|
|
224
214
|
}
|
|
225
215
|
|
|
226
|
-
return
|
|
216
|
+
return energy;
|
|
227
217
|
}
|
|
228
218
|
|
|
229
219
|
__kernel void bootstrap_sparse(
|
|
230
220
|
uint prng_seed,
|
|
231
|
-
__global
|
|
221
|
+
__global float* G_data,
|
|
232
222
|
__global unsigned* G_rows,
|
|
233
223
|
__global unsigned* G_cols,
|
|
234
224
|
__constant char* best_theta,
|
|
@@ -270,7 +260,7 @@ __kernel void bootstrap_sparse(
|
|
|
270
260
|
if (hid_energy < lid_energy) {
|
|
271
261
|
loc_energy[lt_id] = hid_energy;
|
|
272
262
|
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
273
|
-
} else if (((hid_energy - lid_energy) <=
|
|
263
|
+
} else if (((hid_energy - lid_energy) <= FLT_EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
274
264
|
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
275
265
|
}
|
|
276
266
|
}
|
|
@@ -290,8 +280,8 @@ inline float rand_uniform(uint *state) {
|
|
|
290
280
|
}
|
|
291
281
|
|
|
292
282
|
// Compute cut value from bitset solution
|
|
293
|
-
|
|
294
|
-
|
|
283
|
+
float compute_cut_bitset(__global const float* G_m, const uint* sol_bits, int n) {
|
|
284
|
+
float cut_val = 0.0f;
|
|
295
285
|
for (int u = 0; u < n; u++) {
|
|
296
286
|
int u_word = u >> 5; // divide by 32
|
|
297
287
|
int u_bit = u & 31;
|
|
@@ -307,6 +297,7 @@ double compute_cut_bitset(__global const double* G_m, const uint* sol_bits, int
|
|
|
307
297
|
}
|
|
308
298
|
}
|
|
309
299
|
}
|
|
300
|
+
|
|
310
301
|
return cut_val;
|
|
311
302
|
}
|
|
312
303
|
|
|
@@ -314,11 +305,11 @@ double compute_cut_bitset(__global const double* G_m, const uint* sol_bits, int
|
|
|
314
305
|
#define MAX_WORDS_MASK 4095
|
|
315
306
|
|
|
316
307
|
__kernel void sample_for_solution_best_bitset(
|
|
317
|
-
__global const
|
|
308
|
+
__global const float* G_m,
|
|
318
309
|
__global const float* thresholds,
|
|
319
310
|
const int n,
|
|
320
311
|
const int shots,
|
|
321
|
-
const
|
|
312
|
+
const float max_weight,
|
|
322
313
|
__global float* rng_seeds,
|
|
323
314
|
__global uint* best_solutions, // [num_groups × ceil(n/32)]
|
|
324
315
|
__global float* best_energies, // [num_groups]
|
|
@@ -339,7 +330,7 @@ __kernel void sample_for_solution_best_bitset(
|
|
|
339
330
|
for (int w = 0; w < words; w++) sol_bits[w] = 0;
|
|
340
331
|
uint temp_sol[MAX_WORDS];
|
|
341
332
|
|
|
342
|
-
|
|
333
|
+
float cut_val = -INFINITY;
|
|
343
334
|
for (int gid = gid_orig; gid < shots; gid += MAX_PROC_ELEM) {
|
|
344
335
|
|
|
345
336
|
// --- 1. Choose Hamming weight
|
|
@@ -353,7 +344,7 @@ __kernel void sample_for_solution_best_bitset(
|
|
|
353
344
|
temp_sol[(gid >> 5) & MAX_WORDS_MASK] |= 1U << (gid & 31);
|
|
354
345
|
|
|
355
346
|
for (int count = 1; count < m; ++count) {
|
|
356
|
-
|
|
347
|
+
float highest_weights[TOP_N];
|
|
357
348
|
int best_bits[TOP_N];
|
|
358
349
|
for (int x = 0; x < TOP_N; ++x) {
|
|
359
350
|
highest_weights[x] = -INFINITY;
|
|
@@ -371,7 +362,7 @@ __kernel void sample_for_solution_best_bitset(
|
|
|
371
362
|
}
|
|
372
363
|
const int u_offset = u * n;
|
|
373
364
|
|
|
374
|
-
|
|
365
|
+
float weight = 1.0f;
|
|
375
366
|
for (int k = 0; k < n; k += 32) {
|
|
376
367
|
for (int l = 0; l < 32; ++l) {
|
|
377
368
|
const int v = k + l;
|
|
@@ -379,16 +370,16 @@ __kernel void sample_for_solution_best_bitset(
|
|
|
379
370
|
break;
|
|
380
371
|
}
|
|
381
372
|
if ((temp_sol[k >> 5] >> l) & 1) {
|
|
382
|
-
weight *= max(
|
|
373
|
+
weight *= max(FLT_EPSILON, 1.0f - G_m[u_offset + v] / max_weight);
|
|
383
374
|
}
|
|
384
375
|
}
|
|
385
376
|
}
|
|
386
377
|
|
|
387
378
|
int lowest_option = 0;
|
|
388
|
-
|
|
379
|
+
float lowest_weight = highest_weights[0];
|
|
389
380
|
if (lowest_weight != -INFINITY) {
|
|
390
381
|
for (int x = 0; x < TOP_N; ++x) {
|
|
391
|
-
|
|
382
|
+
float val = highest_weights[x];
|
|
392
383
|
if (val < lowest_weight) {
|
|
393
384
|
lowest_option = x;
|
|
394
385
|
lowest_weight = highest_weights[x];
|
|
@@ -406,9 +397,9 @@ __kernel void sample_for_solution_best_bitset(
|
|
|
406
397
|
}
|
|
407
398
|
}
|
|
408
399
|
|
|
409
|
-
|
|
400
|
+
float total_weight = 0.0f;
|
|
410
401
|
for (int x = 0; x < TOP_N; ++x) {
|
|
411
|
-
const
|
|
402
|
+
const float val = highest_weights[x];
|
|
412
403
|
if (val == -INFINITY) {
|
|
413
404
|
continue;
|
|
414
405
|
}
|
|
@@ -416,10 +407,10 @@ __kernel void sample_for_solution_best_bitset(
|
|
|
416
407
|
}
|
|
417
408
|
|
|
418
409
|
const float bit_prob = rand_uniform(&state);
|
|
419
|
-
|
|
410
|
+
float tot_prob = 0.0f;
|
|
420
411
|
int best_bit = 0;
|
|
421
412
|
for (int x = 0; x < TOP_N; ++x) {
|
|
422
|
-
const
|
|
413
|
+
const float val = highest_weights[x];
|
|
423
414
|
if (val == -INFINITY) {
|
|
424
415
|
continue;
|
|
425
416
|
}
|
|
@@ -436,7 +427,7 @@ __kernel void sample_for_solution_best_bitset(
|
|
|
436
427
|
}
|
|
437
428
|
|
|
438
429
|
// --- 3. Compute cut value
|
|
439
|
-
|
|
430
|
+
float temp_cut = compute_cut_bitset(G_m, temp_sol, n);
|
|
440
431
|
if (temp_cut > cut_val) {
|
|
441
432
|
cut_val = temp_cut;
|
|
442
433
|
for (int i = 0; i < words; ++i) {
|
|
@@ -474,8 +465,8 @@ __kernel void sample_for_solution_best_bitset(
|
|
|
474
465
|
}
|
|
475
466
|
|
|
476
467
|
// Compute cut value from bitset solution
|
|
477
|
-
|
|
478
|
-
|
|
468
|
+
float compute_cut_bitset_sparse(__global const float* G_data, __global const unsigned* G_rows, __global const unsigned* G_cols, const uint* sol_bits, int n) {
|
|
469
|
+
float cut_val = 0.0f;
|
|
479
470
|
for (unsigned u = 0; u < n; u++) {
|
|
480
471
|
int u_word = u >> 5; // divide by 32
|
|
481
472
|
int u_bit = u & 31;
|
|
@@ -493,6 +484,7 @@ double compute_cut_bitset_sparse(__global const double* G_data, __global const u
|
|
|
493
484
|
}
|
|
494
485
|
}
|
|
495
486
|
}
|
|
487
|
+
|
|
496
488
|
return cut_val;
|
|
497
489
|
}
|
|
498
490
|
|
|
@@ -518,13 +510,13 @@ int binary_search(__global const unsigned* l, const unsigned t, const unsigned l
|
|
|
518
510
|
}
|
|
519
511
|
|
|
520
512
|
__kernel void sample_for_solution_best_bitset_sparse(
|
|
521
|
-
__global const
|
|
513
|
+
__global const float* G_data,
|
|
522
514
|
__global const unsigned* G_rows,
|
|
523
515
|
__global const unsigned* G_cols,
|
|
524
516
|
__global const float* thresholds,
|
|
525
517
|
const int n,
|
|
526
518
|
const int shots,
|
|
527
|
-
const
|
|
519
|
+
const float max_weight,
|
|
528
520
|
__global float* rng_seeds,
|
|
529
521
|
__global uint* best_solutions, // [num_groups × ceil(n/32)]
|
|
530
522
|
__global float* best_energies, // [num_groups]
|
|
@@ -545,7 +537,7 @@ __kernel void sample_for_solution_best_bitset_sparse(
|
|
|
545
537
|
for (int w = 0; w < words; w++) sol_bits[w] = 0;
|
|
546
538
|
uint temp_sol[MAX_WORDS];
|
|
547
539
|
|
|
548
|
-
|
|
540
|
+
float cut_val = -INFINITY;
|
|
549
541
|
for (int gid = gid_orig; gid < shots; gid += MAX_PROC_ELEM) {
|
|
550
542
|
|
|
551
543
|
// --- 1. Choose Hamming weight
|
|
@@ -559,7 +551,7 @@ __kernel void sample_for_solution_best_bitset_sparse(
|
|
|
559
551
|
temp_sol[(gid >> 5) & MAX_WORDS_MASK] |= 1U << (gid & 31);
|
|
560
552
|
|
|
561
553
|
for (int count = 1; count < m; ++count) {
|
|
562
|
-
|
|
554
|
+
float highest_weights[TOP_N];
|
|
563
555
|
int best_bits[TOP_N];
|
|
564
556
|
for (int x = 0; x < TOP_N; ++x) {
|
|
565
557
|
highest_weights[x] = -INFINITY;
|
|
@@ -576,14 +568,14 @@ __kernel void sample_for_solution_best_bitset_sparse(
|
|
|
576
568
|
continue;
|
|
577
569
|
}
|
|
578
570
|
|
|
579
|
-
|
|
571
|
+
float weight = 1.0f;
|
|
580
572
|
|
|
581
573
|
unsigned max_col = G_rows[u + 1];
|
|
582
574
|
for (int col = G_rows[u]; col < max_col; ++col) {
|
|
583
575
|
const int v = G_cols[col];
|
|
584
576
|
|
|
585
577
|
if ((temp_sol[v >> 5] >> (v & 31)) & 1) {
|
|
586
|
-
weight *= max(
|
|
578
|
+
weight *= max(FLT_EPSILON, 1.0f - G_data[col] / max_weight);
|
|
587
579
|
}
|
|
588
580
|
}
|
|
589
581
|
|
|
@@ -595,15 +587,15 @@ __kernel void sample_for_solution_best_bitset_sparse(
|
|
|
595
587
|
int end = G_rows[v + 1];
|
|
596
588
|
int j = binary_search(&(G_cols[start]), u, end - start) + start;
|
|
597
589
|
if (j < end) {
|
|
598
|
-
weight *= max(
|
|
590
|
+
weight *= max(FLT_EPSILON, 1.0f - G_data[j] / max_weight);
|
|
599
591
|
}
|
|
600
592
|
}
|
|
601
593
|
|
|
602
594
|
int lowest_option = 0;
|
|
603
|
-
|
|
595
|
+
float lowest_weight = highest_weights[0];
|
|
604
596
|
if (lowest_weight != -INFINITY) {
|
|
605
597
|
for (int x = 0; x < TOP_N; ++x) {
|
|
606
|
-
|
|
598
|
+
float val = highest_weights[x];
|
|
607
599
|
if (val < lowest_weight) {
|
|
608
600
|
lowest_option = x;
|
|
609
601
|
lowest_weight = highest_weights[x];
|
|
@@ -621,9 +613,9 @@ __kernel void sample_for_solution_best_bitset_sparse(
|
|
|
621
613
|
}
|
|
622
614
|
}
|
|
623
615
|
|
|
624
|
-
|
|
616
|
+
float total_weight = 0.0f;
|
|
625
617
|
for (int x = 0; x < TOP_N; ++x) {
|
|
626
|
-
const
|
|
618
|
+
const float val = highest_weights[x];
|
|
627
619
|
if (val == -INFINITY) {
|
|
628
620
|
continue;
|
|
629
621
|
}
|
|
@@ -631,10 +623,10 @@ __kernel void sample_for_solution_best_bitset_sparse(
|
|
|
631
623
|
}
|
|
632
624
|
|
|
633
625
|
const float bit_prob = rand_uniform(&state);
|
|
634
|
-
|
|
626
|
+
float tot_prob = 0.0f;
|
|
635
627
|
int best_bit = 0;
|
|
636
628
|
for (int x = 0; x < TOP_N; ++x) {
|
|
637
|
-
const
|
|
629
|
+
const float val = highest_weights[x];
|
|
638
630
|
if (val == -INFINITY) {
|
|
639
631
|
continue;
|
|
640
632
|
}
|
|
@@ -651,7 +643,7 @@ __kernel void sample_for_solution_best_bitset_sparse(
|
|
|
651
643
|
}
|
|
652
644
|
|
|
653
645
|
// --- 3. Compute cut value
|
|
654
|
-
|
|
646
|
+
float temp_cut = compute_cut_bitset_sparse(G_data, G_rows, G_cols, temp_sol, n);
|
|
655
647
|
if (temp_cut > cut_val) {
|
|
656
648
|
cut_val = temp_cut;
|
|
657
649
|
for (int i = 0; i < words; ++i) {
|
|
@@ -33,7 +33,7 @@ def local_repulsion_choice(G_m, max_weight, weights, n, m, shot):
|
|
|
33
33
|
- High-degree nodes are already less likely
|
|
34
34
|
- After choosing a node, its neighbors' probabilities are further reduced
|
|
35
35
|
adjacency_data, adjacency_rows: CSR-format sparse adjacency data
|
|
36
|
-
weights:
|
|
36
|
+
weights: float32 array of shape (n,)
|
|
37
37
|
"""
|
|
38
38
|
|
|
39
39
|
weights = weights.copy()
|
|
@@ -120,7 +120,7 @@ def sample_for_solution(G_m, shots, thresholds, weights):
|
|
|
120
120
|
def init_J_and_z(G_m):
|
|
121
121
|
n_qubits = len(G_m)
|
|
122
122
|
degrees = np.empty(n_qubits, dtype=np.uint32)
|
|
123
|
-
J_eff = np.empty(n_qubits, dtype=np.
|
|
123
|
+
J_eff = np.empty(n_qubits, dtype=np.float32)
|
|
124
124
|
J_max = -float("inf")
|
|
125
125
|
for n in prange(n_qubits):
|
|
126
126
|
degree = sum(G_m[n] != 0.0)
|
|
@@ -175,7 +175,7 @@ def run_sampling_opencl(G_m_np, thresholds_np, shots, n, is_g_buf_reused):
|
|
|
175
175
|
thresholds_buf,
|
|
176
176
|
np.int32(n),
|
|
177
177
|
np.int32(shots),
|
|
178
|
-
np.
|
|
178
|
+
np.float32(G_m_np.max()),
|
|
179
179
|
rng_buf,
|
|
180
180
|
solutions_buf,
|
|
181
181
|
best_energies_buf,
|
|
@@ -252,7 +252,7 @@ def maxcut_tfim(
|
|
|
252
252
|
if isinstance(G, nx.Graph):
|
|
253
253
|
nodes = list(G.nodes())
|
|
254
254
|
n_qubits = len(nodes)
|
|
255
|
-
G_m = nx.to_numpy_array(G, weight='weight', nonedge=0.0)
|
|
255
|
+
G_m = nx.to_numpy_array(G, weight='weight', nonedge=0.0, dtype=np.float32)
|
|
256
256
|
else:
|
|
257
257
|
n_qubits = len(G)
|
|
258
258
|
nodes = list(range(n_qubits))
|
|
@@ -291,7 +291,7 @@ def maxcut_tfim(
|
|
|
291
291
|
tot_t = 2.0 * n_steps * delta_t
|
|
292
292
|
h_mult = 2.0 / tot_t
|
|
293
293
|
|
|
294
|
-
args = np.empty(3, dtype=np.
|
|
294
|
+
args = np.empty(3, dtype=np.float32)
|
|
295
295
|
args[0] = delta_t
|
|
296
296
|
args[1] = tot_t
|
|
297
297
|
args[2] = h_mult
|
|
@@ -301,7 +301,7 @@ def maxcut_tfim(
|
|
|
301
301
|
args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
|
|
302
302
|
J_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
|
|
303
303
|
deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
|
|
304
|
-
theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE, size=(n_qubits * np.
|
|
304
|
+
theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE, size=(n_qubits * np.float32().nbytes))
|
|
305
305
|
|
|
306
306
|
# Warp size is 32:
|
|
307
307
|
group_size = min(n_qubits, 64)
|
|
@@ -63,7 +63,7 @@ def local_repulsion_choice(G_cols, G_data, G_rows, max_weight, weights, n, m, sh
|
|
|
63
63
|
- High-degree nodes are already less likely
|
|
64
64
|
- After choosing a node, its neighbors' probabilities are further reduced
|
|
65
65
|
adjacency_data, adjacency_rows: CSR-format sparse adjacency data
|
|
66
|
-
weights:
|
|
66
|
+
weights: float32 array of shape (n,)
|
|
67
67
|
"""
|
|
68
68
|
|
|
69
69
|
weights = weights.copy()
|
|
@@ -152,7 +152,7 @@ def sample_for_solution(G_data, G_rows, G_cols, shots, thresholds, weights):
|
|
|
152
152
|
def init_J_and_z(G_data, G_rows, G_cols):
|
|
153
153
|
n_qubits = G_rows.shape[0] - 1
|
|
154
154
|
degrees = np.empty(n_qubits, dtype=np.uint32)
|
|
155
|
-
J_eff = np.empty(n_qubits, dtype=np.
|
|
155
|
+
J_eff = np.empty(n_qubits, dtype=np.float32)
|
|
156
156
|
for r in prange(n_qubits):
|
|
157
157
|
# Row sum
|
|
158
158
|
start = G_rows[r]
|
|
@@ -226,7 +226,7 @@ def run_sampling_opencl(G_m_csr, thresholds_np, shots, n, is_g_buf_reused):
|
|
|
226
226
|
thresholds_buf,
|
|
227
227
|
np.int32(n),
|
|
228
228
|
np.int32(shots),
|
|
229
|
-
np.
|
|
229
|
+
np.float32(G_m_csr.data.max()),
|
|
230
230
|
rng_buf,
|
|
231
231
|
solutions_buf,
|
|
232
232
|
best_energies_buf,
|
|
@@ -292,7 +292,7 @@ def gpu_footer(shots, n_qubits, G_data, G_rows, G_cols, weights, hamming_prob, n
|
|
|
292
292
|
|
|
293
293
|
|
|
294
294
|
def to_scipy_sparse_upper_triangular(G, nodes, n_nodes):
|
|
295
|
-
lil = lil_matrix((n_nodes, n_nodes), dtype=np.
|
|
295
|
+
lil = lil_matrix((n_nodes, n_nodes), dtype=np.float32)
|
|
296
296
|
for u in range(n_nodes):
|
|
297
297
|
u_node = nodes[u]
|
|
298
298
|
for v in range(u + 1, n_nodes):
|
|
@@ -356,7 +356,7 @@ def maxcut_tfim_sparse(
|
|
|
356
356
|
tot_t = 2.0 * n_steps * delta_t
|
|
357
357
|
h_mult = 2.0 / tot_t
|
|
358
358
|
|
|
359
|
-
args = np.empty(3, dtype=np.
|
|
359
|
+
args = np.empty(3, dtype=np.float32)
|
|
360
360
|
args[0] = delta_t
|
|
361
361
|
args[1] = tot_t
|
|
362
362
|
args[2] = h_mult
|
|
@@ -124,8 +124,7 @@ def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy):
|
|
|
124
124
|
if min_energy < energy:
|
|
125
125
|
return min_energy
|
|
126
126
|
|
|
127
|
-
|
|
128
|
-
choices = np.where(abs(min_energy_host - energy) <= ENERGY_EPSILON)[0]
|
|
127
|
+
choices = np.where(np.isclose(min_energy_host, energy, atol=2e-23, rtol=0))[0]
|
|
129
128
|
best_i = np.random.choice(choices) if len(choices) else np.argmin(min_energy_host)
|
|
130
129
|
|
|
131
130
|
flip_index_start = best_i * k
|
|
@@ -152,7 +151,7 @@ def spin_glass_solver(
|
|
|
152
151
|
if isinstance(G, nx.Graph):
|
|
153
152
|
nodes = list(G.nodes())
|
|
154
153
|
n_qubits = len(nodes)
|
|
155
|
-
G_m = nx.to_numpy_array(G, weight='weight', nonedge=0.0)
|
|
154
|
+
G_m = nx.to_numpy_array(G, weight='weight', nonedge=0.0, dtype=np.float32)
|
|
156
155
|
else:
|
|
157
156
|
n_qubits = len(G)
|
|
158
157
|
nodes = list(range(n_qubits))
|
|
@@ -129,8 +129,7 @@ def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices
|
|
|
129
129
|
if min_energy < energy:
|
|
130
130
|
return min_energy
|
|
131
131
|
|
|
132
|
-
|
|
133
|
-
choices = np.where(abs(min_energy_host - energy) <= ENERGY_EPSILON)[0]
|
|
132
|
+
choices = np.where(np.isclose(min_energy_host, energy, atol=2e-23, rtol=0))[0]
|
|
134
133
|
best_i = np.random.choice(choices) if len(choices) else np.argmin(min_energy_host)
|
|
135
134
|
|
|
136
135
|
flip_index_start = best_i * k
|
|
@@ -143,7 +142,7 @@ def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices
|
|
|
143
142
|
|
|
144
143
|
|
|
145
144
|
def to_scipy_sparse_upper_triangular(G, nodes, n_nodes):
|
|
146
|
-
lil = lil_matrix((n_nodes, n_nodes), dtype=np.
|
|
145
|
+
lil = lil_matrix((n_nodes, n_nodes), dtype=np.float32)
|
|
147
146
|
for u in range(n_nodes):
|
|
148
147
|
u_node = nodes[u]
|
|
149
148
|
for v in range(u + 1, n_nodes):
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='pyqrackising',
|
|
54
|
-
version='
|
|
54
|
+
version='6.0.0',
|
|
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
|