PyQrackIsing 7.5.3__tar.gz → 8.0.1__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-7.5.3/pyqrackising.egg-info → pyqrackising-8.0.1}/PKG-INFO +1 -1
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/pyproject.toml +1 -1
- pyqrackising-8.0.1/pyqrackising/kernels.cl +417 -0
- pyqrackising-8.0.1/pyqrackising/maxcut_tfim.py +251 -0
- pyqrackising-8.0.1/pyqrackising/maxcut_tfim_sparse.py +279 -0
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/pyqrackising/maxcut_tfim_streaming.py +11 -89
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/pyqrackising/maxcut_tfim_util.py +2 -14
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/pyqrackising/spin_glass_solver.py +3 -4
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/pyqrackising/spin_glass_solver_sparse.py +3 -4
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/pyqrackising/spin_glass_solver_streaming.py +6 -4
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/pyqrackising/tsp.py +6 -18
- {pyqrackising-7.5.3 → pyqrackising-8.0.1/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/setup.py +1 -1
- pyqrackising-7.5.3/pyqrackising/kernels.cl +0 -946
- pyqrackising-7.5.3/pyqrackising/maxcut_tfim.py +0 -413
- pyqrackising-7.5.3/pyqrackising/maxcut_tfim_sparse.py +0 -448
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/CMakeLists.txt +0 -0
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/LICENSE.md +0 -0
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/MANIFEST.in +0 -0
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/README.md +0 -0
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/pyqrackising/__init__.py +0 -0
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/pyqrackising/spin_glass_solver_util.py +0 -0
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/pyqrackising/tsp_maxcut.py +0 -0
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-7.5.3 → pyqrackising-8.0.1}/setup.cfg +0 -0
|
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
|
|
|
12
12
|
|
|
13
13
|
[project]
|
|
14
14
|
name = "pyqrackising"
|
|
15
|
-
version = "
|
|
15
|
+
version = "8.0.1"
|
|
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"}
|
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
#if FP16
|
|
2
|
+
#define fwrapper(f, a) (half)f((float)(a))
|
|
3
|
+
#define fwrapper2(f, a, b) (half)f((float)(a), (float)(b))
|
|
4
|
+
#else
|
|
5
|
+
#define fwrapper(f, a) f(a)
|
|
6
|
+
#define fwrapper2(f, a, b) f(a, b)
|
|
7
|
+
#endif
|
|
8
|
+
|
|
9
|
+
inline uint xorshift32(uint *state) {
|
|
10
|
+
uint x = *state;
|
|
11
|
+
x ^= x << 13;
|
|
12
|
+
x ^= x >> 17;
|
|
13
|
+
x ^= x << 5;
|
|
14
|
+
*state = x;
|
|
15
|
+
return x;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
real1 bootstrap_worker(__constant char* theta, __global const real1* G_m, __constant int* indices, const int k, const int n) {
|
|
19
|
+
real1 energy = ZERO_R1;
|
|
20
|
+
const size_t n_st = (size_t)n;
|
|
21
|
+
for (int u = 0; u < n; ++u) {
|
|
22
|
+
const size_t u_offset = u * n_st;
|
|
23
|
+
bool u_bit = theta[u];
|
|
24
|
+
for (int x = 0; x < k; ++x) {
|
|
25
|
+
if (indices[x] == u) {
|
|
26
|
+
u_bit = !u_bit;
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
for (int v = u + 1; v < n; ++v) {
|
|
31
|
+
const real1 val = G_m[u_offset + v];
|
|
32
|
+
bool v_bit = theta[v];
|
|
33
|
+
for (int x = 0; x < k; ++x) {
|
|
34
|
+
if (indices[x] == v) {
|
|
35
|
+
v_bit = !v_bit;
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
energy += (u_bit == v_bit) ? val : -val;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return energy;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
__kernel void bootstrap(
|
|
47
|
+
uint prng_seed,
|
|
48
|
+
__global const real1* G_m,
|
|
49
|
+
__constant char* best_theta,
|
|
50
|
+
__constant int* indices_array,
|
|
51
|
+
__constant int* args, // args[0] = n, args[1] = k
|
|
52
|
+
__global real1* min_energy_ptr, // output: per-group min energy
|
|
53
|
+
__global int* min_index_ptr, // output: per-group best index (i)
|
|
54
|
+
__local real1* loc_energy, // local memory buffer
|
|
55
|
+
__local int* loc_index // local memory buffer
|
|
56
|
+
) {
|
|
57
|
+
const int n = args[0];
|
|
58
|
+
const int k = args[1];
|
|
59
|
+
const int combo_count = args[2];
|
|
60
|
+
int i = get_global_id(0);
|
|
61
|
+
|
|
62
|
+
// The inputs are chaotic, and this doesn't need to be high-quality, just uniform.
|
|
63
|
+
prng_seed ^= (uint)i;
|
|
64
|
+
|
|
65
|
+
real1 best_energy = INFINITY;
|
|
66
|
+
int best_i = i;
|
|
67
|
+
|
|
68
|
+
for (; i < combo_count; i += MAX_PROC_ELEM) {
|
|
69
|
+
const int j = i * k;
|
|
70
|
+
const real1 energy = bootstrap_worker(best_theta, G_m, indices_array + j, k, n);
|
|
71
|
+
if (energy < best_energy) {
|
|
72
|
+
best_energy = energy;
|
|
73
|
+
best_i = i;
|
|
74
|
+
} else if (((energy - best_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
75
|
+
best_i = i;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const int lt_id = get_local_id(0);
|
|
80
|
+
const int lt_size = get_local_size(0);
|
|
81
|
+
|
|
82
|
+
loc_energy[lt_id] = best_energy;
|
|
83
|
+
loc_index[lt_id] = best_i;
|
|
84
|
+
|
|
85
|
+
// Reduce within workgroup
|
|
86
|
+
for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
|
|
87
|
+
barrier(CLK_LOCAL_MEM_FENCE);
|
|
88
|
+
if (lt_id < offset) {
|
|
89
|
+
real1 hid_energy = loc_energy[lt_id + offset];
|
|
90
|
+
real1 lid_energy = loc_energy[lt_id];
|
|
91
|
+
if (hid_energy < lid_energy) {
|
|
92
|
+
loc_energy[lt_id] = hid_energy;
|
|
93
|
+
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
94
|
+
} else if (((hid_energy - lid_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
95
|
+
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Write out per-group result
|
|
101
|
+
if (lt_id == 0) {
|
|
102
|
+
min_energy_ptr[get_group_id(0)] = loc_energy[0];
|
|
103
|
+
min_index_ptr[get_group_id(0)] = loc_index[0];
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
real1 bootstrap_worker_sparse(__constant char* theta, __global const real1* G_data, __global const uint* G_rows, __global const uint* G_cols, __constant int* indices, const int k, const int n) {
|
|
108
|
+
real1 energy = ZERO_R1;
|
|
109
|
+
for (int u = 0; u < n; ++u) {
|
|
110
|
+
bool u_bit = theta[u];
|
|
111
|
+
for (int x = 0; x < k; ++x) {
|
|
112
|
+
if (indices[x] == u) {
|
|
113
|
+
u_bit = !u_bit;
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const size_t mCol = G_rows[u + 1];
|
|
118
|
+
for (int col = G_rows[u]; col < mCol; ++col) {
|
|
119
|
+
const int v = G_cols[col];
|
|
120
|
+
const real1 val = G_data[col];
|
|
121
|
+
bool v_bit = theta[v];
|
|
122
|
+
for (int x = 0; x < k; ++x) {
|
|
123
|
+
if (indices[x] == v) {
|
|
124
|
+
v_bit = !v_bit;
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
energy += (u_bit == v_bit) ? val : -val;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return energy;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
__kernel void bootstrap_sparse(
|
|
136
|
+
uint prng_seed,
|
|
137
|
+
__global const real1* G_data,
|
|
138
|
+
__global const uint* G_rows,
|
|
139
|
+
__global const uint* G_cols,
|
|
140
|
+
__constant char* best_theta,
|
|
141
|
+
__constant int* indices_array,
|
|
142
|
+
__constant int* args, // args[0] = n, args[1] = k
|
|
143
|
+
__global real1* min_energy_ptr, // output: per-group min energy
|
|
144
|
+
__global int* min_index_ptr, // output: per-group best index (i)
|
|
145
|
+
__local real1* loc_energy, // local memory buffer
|
|
146
|
+
__local int* loc_index // local memory buffer
|
|
147
|
+
) {
|
|
148
|
+
const int n = args[0];
|
|
149
|
+
const int k = args[1];
|
|
150
|
+
const int combo_count = args[2];
|
|
151
|
+
int i = get_global_id(0);
|
|
152
|
+
|
|
153
|
+
prng_seed ^= (uint)i;
|
|
154
|
+
|
|
155
|
+
real1 best_energy = INFINITY;
|
|
156
|
+
int best_i = i;
|
|
157
|
+
|
|
158
|
+
for (; i < combo_count; i += MAX_PROC_ELEM) {
|
|
159
|
+
const int j = i * k;
|
|
160
|
+
const real1 energy = bootstrap_worker_sparse(best_theta, G_data, G_rows, G_cols, indices_array + j, k, n);
|
|
161
|
+
if (energy < best_energy) {
|
|
162
|
+
best_energy = energy;
|
|
163
|
+
best_i = i;
|
|
164
|
+
} else if (((energy - best_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
165
|
+
best_i = i;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const int lt_id = get_local_id(0);
|
|
170
|
+
const int lt_size = get_local_size(0);
|
|
171
|
+
|
|
172
|
+
loc_energy[lt_id] = best_energy;
|
|
173
|
+
loc_index[lt_id] = best_i;
|
|
174
|
+
|
|
175
|
+
// Reduce within workgroup
|
|
176
|
+
for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
|
|
177
|
+
barrier(CLK_LOCAL_MEM_FENCE);
|
|
178
|
+
if (lt_id < offset) {
|
|
179
|
+
real1 hid_energy = loc_energy[lt_id + offset];
|
|
180
|
+
real1 lid_energy = loc_energy[lt_id];
|
|
181
|
+
if (hid_energy < lid_energy) {
|
|
182
|
+
loc_energy[lt_id] = hid_energy;
|
|
183
|
+
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
184
|
+
} else if (((hid_energy - lid_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
185
|
+
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Write out per-group result
|
|
191
|
+
if (lt_id == 0) {
|
|
192
|
+
min_energy_ptr[get_group_id(0)] = loc_energy[0];
|
|
193
|
+
min_index_ptr[get_group_id(0)] = loc_index[0];
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Helper to read from segmented G_m
|
|
198
|
+
inline real1 get_G_m(
|
|
199
|
+
__global const real1** G_m,
|
|
200
|
+
size_t flat_idx,
|
|
201
|
+
int segment_size
|
|
202
|
+
) {
|
|
203
|
+
return G_m[flat_idx / segment_size][flat_idx % segment_size];
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
real1 bootstrap_worker_segmented(
|
|
207
|
+
__constant char* theta,
|
|
208
|
+
__global const real1** G_m,
|
|
209
|
+
__constant int* indices,
|
|
210
|
+
const int k,
|
|
211
|
+
const int n,
|
|
212
|
+
const int segment_size
|
|
213
|
+
) {
|
|
214
|
+
real1 energy = ZERO_R1;
|
|
215
|
+
const size_t n_st = (size_t)n;
|
|
216
|
+
|
|
217
|
+
for (int u = 0; u < n; ++u) {
|
|
218
|
+
const size_t u_offset = u * n_st;
|
|
219
|
+
bool u_bit = theta[u];
|
|
220
|
+
for (int x = 0; x < k; ++x) {
|
|
221
|
+
if (indices[x] == u) {
|
|
222
|
+
u_bit = !u_bit;
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
for (int v = u + 1; v < n; ++v) {
|
|
227
|
+
size_t flat_idx = u_offset + v;
|
|
228
|
+
real1 val = get_G_m(G_m, flat_idx, segment_size);
|
|
229
|
+
|
|
230
|
+
bool v_bit = theta[v];
|
|
231
|
+
for (int x = 0; x < k; ++x) {
|
|
232
|
+
if (indices[x] == v) {
|
|
233
|
+
v_bit = !v_bit;
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
energy += (u_bit == v_bit) ? val : -val;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return energy;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
__kernel void bootstrap_segmented(
|
|
246
|
+
uint prng_seed,
|
|
247
|
+
__global const real1* G_m0,
|
|
248
|
+
__global const real1* G_m1,
|
|
249
|
+
__global const real1* G_m2,
|
|
250
|
+
__global const real1* G_m3,
|
|
251
|
+
__constant char* best_theta,
|
|
252
|
+
__constant int* indices_array,
|
|
253
|
+
__constant int* args, // args[0]=n, args[1]=k, args[2]=combo_count, args[3]=segment_size
|
|
254
|
+
__global real1* min_energy_ptr,
|
|
255
|
+
__global int* min_index_ptr,
|
|
256
|
+
__local real1* loc_energy,
|
|
257
|
+
__local int* loc_index
|
|
258
|
+
) {
|
|
259
|
+
__global const real1* G_m[4] = { G_m0, G_m1, G_m2, G_m3 };
|
|
260
|
+
|
|
261
|
+
const int n = args[0];
|
|
262
|
+
const int k = args[1];
|
|
263
|
+
const int combo_count = args[2];
|
|
264
|
+
const int segment_size = args[3];
|
|
265
|
+
int i = get_global_id(0);
|
|
266
|
+
|
|
267
|
+
prng_seed ^= (uint)i;
|
|
268
|
+
|
|
269
|
+
real1 best_energy = INFINITY;
|
|
270
|
+
int best_i = i;
|
|
271
|
+
|
|
272
|
+
for (; i < combo_count; i += MAX_PROC_ELEM) {
|
|
273
|
+
const int j = i * k;
|
|
274
|
+
const real1 energy = bootstrap_worker_segmented(best_theta, G_m, indices_array + j, k, n, segment_size);
|
|
275
|
+
if (energy < best_energy) {
|
|
276
|
+
best_energy = energy;
|
|
277
|
+
best_i = i;
|
|
278
|
+
} else if (((energy - best_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
279
|
+
best_i = i;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const int lt_id = get_local_id(0);
|
|
284
|
+
const int lt_size = get_local_size(0);
|
|
285
|
+
|
|
286
|
+
loc_energy[lt_id] = best_energy;
|
|
287
|
+
loc_index[lt_id] = best_i;
|
|
288
|
+
|
|
289
|
+
for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
|
|
290
|
+
barrier(CLK_LOCAL_MEM_FENCE);
|
|
291
|
+
if (lt_id < offset) {
|
|
292
|
+
real1 hid_energy = loc_energy[lt_id + offset];
|
|
293
|
+
real1 lid_energy = loc_energy[lt_id];
|
|
294
|
+
if (hid_energy < lid_energy) {
|
|
295
|
+
loc_energy[lt_id] = hid_energy;
|
|
296
|
+
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
297
|
+
} else if (((hid_energy - lid_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
298
|
+
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (lt_id == 0) {
|
|
304
|
+
min_energy_ptr[get_group_id(0)] = loc_energy[0];
|
|
305
|
+
min_index_ptr[get_group_id(0)] = loc_index[0];
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
real1 bootstrap_worker_sparse_segmented(
|
|
310
|
+
__constant char* theta,
|
|
311
|
+
__global const real1** G_data,
|
|
312
|
+
__global const uint* G_rows,
|
|
313
|
+
__global const uint* G_cols,
|
|
314
|
+
__constant int* indices,
|
|
315
|
+
const int k,
|
|
316
|
+
const int n,
|
|
317
|
+
const int segment_size
|
|
318
|
+
) {
|
|
319
|
+
real1 energy = ZERO_R1;
|
|
320
|
+
|
|
321
|
+
for (int u = 0; u < n; ++u) {
|
|
322
|
+
bool u_bit = theta[u];
|
|
323
|
+
for (int x = 0; x < k; ++x) {
|
|
324
|
+
if (indices[x] == u) {
|
|
325
|
+
u_bit = !u_bit;
|
|
326
|
+
break;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const uint row_start = G_rows[u];
|
|
331
|
+
const uint row_end = G_rows[u + 1];
|
|
332
|
+
|
|
333
|
+
for (uint col = row_start; col < row_end; ++col) {
|
|
334
|
+
const int v = G_cols[col];
|
|
335
|
+
real1 val = get_G_m(G_data, col, segment_size);
|
|
336
|
+
|
|
337
|
+
bool v_bit = theta[v];
|
|
338
|
+
for (int x = 0; x < k; ++x) {
|
|
339
|
+
if (indices[x] == v) {
|
|
340
|
+
v_bit = !v_bit;
|
|
341
|
+
break;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
energy += (u_bit == v_bit) ? val : -val;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
return energy;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
__kernel void bootstrap_sparse_segmented(
|
|
353
|
+
uint prng_seed,
|
|
354
|
+
__global const real1* G_data0,
|
|
355
|
+
__global const real1* G_data1,
|
|
356
|
+
__global const real1* G_data2,
|
|
357
|
+
__global const real1* G_data3,
|
|
358
|
+
__global const uint* G_rows,
|
|
359
|
+
__global const uint* G_cols,
|
|
360
|
+
__constant char* best_theta,
|
|
361
|
+
__constant int* indices_array,
|
|
362
|
+
__constant int* args, // args[0] = n, args[1] = k, args[2] = combo_count, args[3] = segment_size
|
|
363
|
+
__global real1* min_energy_ptr,
|
|
364
|
+
__global int* min_index_ptr,
|
|
365
|
+
__local real1* loc_energy,
|
|
366
|
+
__local int* loc_index
|
|
367
|
+
) {
|
|
368
|
+
__global const real1* G_data[4] = { G_data0, G_data1, G_data2, G_data3 };
|
|
369
|
+
|
|
370
|
+
const int n = args[0];
|
|
371
|
+
const int k = args[1];
|
|
372
|
+
const int combo_count = args[2];
|
|
373
|
+
const int segment_size = args[3];
|
|
374
|
+
int i = get_global_id(0);
|
|
375
|
+
|
|
376
|
+
prng_seed ^= (uint)i;
|
|
377
|
+
|
|
378
|
+
real1 best_energy = INFINITY;
|
|
379
|
+
int best_i = i;
|
|
380
|
+
|
|
381
|
+
for (; i < combo_count; i += MAX_PROC_ELEM) {
|
|
382
|
+
const int j = i * k;
|
|
383
|
+
const real1 energy = bootstrap_worker_sparse_segmented(best_theta, G_data, G_rows, G_cols, indices_array + j, k, n, segment_size);
|
|
384
|
+
if (energy < best_energy) {
|
|
385
|
+
best_energy = energy;
|
|
386
|
+
best_i = i;
|
|
387
|
+
} else if (((energy - best_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
388
|
+
best_i = i;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const int lt_id = get_local_id(0);
|
|
393
|
+
const int lt_size = get_local_size(0);
|
|
394
|
+
|
|
395
|
+
loc_energy[lt_id] = best_energy;
|
|
396
|
+
loc_index[lt_id] = best_i;
|
|
397
|
+
|
|
398
|
+
for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
|
|
399
|
+
barrier(CLK_LOCAL_MEM_FENCE);
|
|
400
|
+
if (lt_id < offset) {
|
|
401
|
+
real1 hid_energy = loc_energy[lt_id + offset];
|
|
402
|
+
real1 lid_energy = loc_energy[lt_id];
|
|
403
|
+
if (hid_energy < lid_energy) {
|
|
404
|
+
loc_energy[lt_id] = hid_energy;
|
|
405
|
+
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
406
|
+
} else if (((hid_energy - lid_energy) <= EPSILON) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
407
|
+
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if (lt_id == 0) {
|
|
413
|
+
min_energy_ptr[get_group_id(0)] = loc_energy[0];
|
|
414
|
+
min_index_ptr[get_group_id(0)] = loc_index[0];
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import math
|
|
2
|
+
import networkx as nx
|
|
3
|
+
import numpy as np
|
|
4
|
+
import os
|
|
5
|
+
from numba import njit, prange
|
|
6
|
+
|
|
7
|
+
from .maxcut_tfim_util import fix_cdf, get_cut, init_theta, init_thresholds, maxcut_hamming_cdf, opencl_context
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
epsilon = opencl_context.epsilon
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@njit
|
|
14
|
+
def update_repulsion_choice(G_m, max_edge, weights, n, used, node):
|
|
15
|
+
# Select node
|
|
16
|
+
used[node] = True
|
|
17
|
+
|
|
18
|
+
# Repulsion: penalize neighbors
|
|
19
|
+
for nbr in range(n):
|
|
20
|
+
if used[nbr]:
|
|
21
|
+
continue
|
|
22
|
+
weights[nbr] *= max(epsilon, 1 - G_m[node, nbr] / max_edge)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
26
|
+
@njit
|
|
27
|
+
def local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, n, m):
|
|
28
|
+
"""
|
|
29
|
+
Pick m nodes out of n with repulsion bias:
|
|
30
|
+
- High-degree nodes are already less likely
|
|
31
|
+
- After choosing a node, its neighbors' probabilities are further reduced
|
|
32
|
+
adjacency_data, adjacency_rows: CSR-format sparse adjacency data
|
|
33
|
+
weights: float32 array of shape (n,)
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
weights = weights.copy()
|
|
37
|
+
used = np.zeros(n, dtype=np.bool_) # False = available, True = used
|
|
38
|
+
|
|
39
|
+
# First bit:
|
|
40
|
+
r = np.random.rand()
|
|
41
|
+
cum = 0.0
|
|
42
|
+
node = 0
|
|
43
|
+
for i in range(n):
|
|
44
|
+
cum += weights[i]
|
|
45
|
+
if (tot_init_weight * r) < cum:
|
|
46
|
+
node = i
|
|
47
|
+
break
|
|
48
|
+
|
|
49
|
+
if m == 1:
|
|
50
|
+
used[node] = True
|
|
51
|
+
return used
|
|
52
|
+
|
|
53
|
+
update_repulsion_choice(G_m, max_edge, weights, n, used, node)
|
|
54
|
+
|
|
55
|
+
for _ in range(1, m - 1):
|
|
56
|
+
# Count available
|
|
57
|
+
total_w = 0.0
|
|
58
|
+
for i in range(n):
|
|
59
|
+
if used[i]:
|
|
60
|
+
continue
|
|
61
|
+
total_w += weights[i]
|
|
62
|
+
|
|
63
|
+
# Normalize & sample
|
|
64
|
+
r = np.random.rand()
|
|
65
|
+
cum = 0.0
|
|
66
|
+
node = -1
|
|
67
|
+
for i in range(n):
|
|
68
|
+
if used[i]:
|
|
69
|
+
continue
|
|
70
|
+
cum += weights[i]
|
|
71
|
+
if (total_w * r) < cum:
|
|
72
|
+
node = i
|
|
73
|
+
break
|
|
74
|
+
|
|
75
|
+
if node == -1:
|
|
76
|
+
node = 0
|
|
77
|
+
while used[node]:
|
|
78
|
+
node += 1
|
|
79
|
+
|
|
80
|
+
# Update answer and weights
|
|
81
|
+
update_repulsion_choice(G_m, max_edge, weights, n, used, node)
|
|
82
|
+
|
|
83
|
+
# Count available
|
|
84
|
+
total_w = 0.0
|
|
85
|
+
for i in range(n):
|
|
86
|
+
if used[i]:
|
|
87
|
+
continue
|
|
88
|
+
total_w += weights[i]
|
|
89
|
+
|
|
90
|
+
# Normalize & sample
|
|
91
|
+
r = np.random.rand()
|
|
92
|
+
cum = 0.0
|
|
93
|
+
node = -1
|
|
94
|
+
for i in range(n):
|
|
95
|
+
if used[i]:
|
|
96
|
+
continue
|
|
97
|
+
cum += weights[i]
|
|
98
|
+
if (total_w * r) < cum:
|
|
99
|
+
node = i
|
|
100
|
+
break
|
|
101
|
+
|
|
102
|
+
if node == -1:
|
|
103
|
+
node = 0
|
|
104
|
+
while used[node]:
|
|
105
|
+
node += 1
|
|
106
|
+
|
|
107
|
+
used[node] = True
|
|
108
|
+
|
|
109
|
+
return used
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
@njit
|
|
113
|
+
def compute_energy(sample, G_m, n_qubits):
|
|
114
|
+
energy = 0
|
|
115
|
+
for u in range(n_qubits):
|
|
116
|
+
for v in range(u + 1, n_qubits):
|
|
117
|
+
val = G_m[u, v]
|
|
118
|
+
energy += val if sample[u] == sample[v] else -val
|
|
119
|
+
|
|
120
|
+
return energy
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
@njit(parallel=True)
|
|
124
|
+
def sample_for_solution(G_m, shots, thresholds, weights, dtype):
|
|
125
|
+
shots = max(1, shots >> 1)
|
|
126
|
+
n = len(G_m)
|
|
127
|
+
max_edge = G_m.max()
|
|
128
|
+
tot_init_weight = weights.sum()
|
|
129
|
+
|
|
130
|
+
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
131
|
+
energies = np.empty(shots, dtype=dtype)
|
|
132
|
+
|
|
133
|
+
best_solution = solutions[0]
|
|
134
|
+
best_energy = float("inf")
|
|
135
|
+
|
|
136
|
+
improved = True
|
|
137
|
+
while improved:
|
|
138
|
+
improved = False
|
|
139
|
+
for s in prange(shots):
|
|
140
|
+
# First dimension: Hamming weight
|
|
141
|
+
mag_prob = np.random.random()
|
|
142
|
+
m = 0
|
|
143
|
+
while thresholds[m] < mag_prob:
|
|
144
|
+
m += 1
|
|
145
|
+
m += 1
|
|
146
|
+
|
|
147
|
+
# Second dimension: permutation within Hamming weight
|
|
148
|
+
sample = local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, n, m)
|
|
149
|
+
solutions[s] = sample
|
|
150
|
+
energies[s] = compute_energy(sample, G_m, n)
|
|
151
|
+
|
|
152
|
+
best_index = np.argmin(energies)
|
|
153
|
+
energy = energies[best_index]
|
|
154
|
+
if energy < best_energy:
|
|
155
|
+
best_energy = energy
|
|
156
|
+
best_solution = solutions[best_index].copy()
|
|
157
|
+
improved = True
|
|
158
|
+
|
|
159
|
+
best_value = 0.0
|
|
160
|
+
for u in range(n):
|
|
161
|
+
for v in range(u + 1, n):
|
|
162
|
+
if best_solution[u] != best_solution[v]:
|
|
163
|
+
best_value += G_m[u, v]
|
|
164
|
+
|
|
165
|
+
return best_solution, best_value
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
@njit(parallel=True)
|
|
169
|
+
def init_J_and_z(G_m, dtype):
|
|
170
|
+
n_qubits = len(G_m)
|
|
171
|
+
degrees = np.empty(n_qubits, dtype=np.uint32)
|
|
172
|
+
J_eff = np.empty(n_qubits, dtype=dtype)
|
|
173
|
+
J_max = -float("inf")
|
|
174
|
+
for n in prange(n_qubits):
|
|
175
|
+
degree = sum(G_m[n] != 0.0)
|
|
176
|
+
J = (-G_m[n].sum() / degree) if degree > 0 else 0
|
|
177
|
+
degrees[n] = degree
|
|
178
|
+
J_eff[n] = J
|
|
179
|
+
J_abs = abs(J)
|
|
180
|
+
J_max = max(J_abs, J_max)
|
|
181
|
+
J_eff /= J_max
|
|
182
|
+
|
|
183
|
+
return J_eff, degrees
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
@njit
|
|
187
|
+
def cpu_footer(shots, quality, n_qubits, G_m, nodes, dtype):
|
|
188
|
+
J_eff, degrees = init_J_and_z(G_m, dtype)
|
|
189
|
+
hamming_prob = init_thresholds(n_qubits, dtype)
|
|
190
|
+
|
|
191
|
+
maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, hamming_prob, dtype)
|
|
192
|
+
|
|
193
|
+
degrees = None
|
|
194
|
+
J_eff = 1.0 / (1.0 + epsilon - J_eff)
|
|
195
|
+
|
|
196
|
+
best_solution, best_value = sample_for_solution(G_m, shots, hamming_prob, J_eff, dtype)
|
|
197
|
+
|
|
198
|
+
bit_string, l, r = get_cut(best_solution, nodes)
|
|
199
|
+
|
|
200
|
+
return bit_string, best_value, (l, r)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def maxcut_tfim(
|
|
204
|
+
G,
|
|
205
|
+
quality=None,
|
|
206
|
+
shots=None
|
|
207
|
+
):
|
|
208
|
+
dtype = opencl_context.dtype
|
|
209
|
+
wgs = opencl_context.work_group_size
|
|
210
|
+
nodes = None
|
|
211
|
+
n_qubits = 0
|
|
212
|
+
G_m = None
|
|
213
|
+
if isinstance(G, nx.Graph):
|
|
214
|
+
nodes = list(G.nodes())
|
|
215
|
+
n_qubits = len(nodes)
|
|
216
|
+
G_m = nx.to_numpy_array(G, weight='weight', nonedge=0.0, dtype=dtype)
|
|
217
|
+
else:
|
|
218
|
+
n_qubits = len(G)
|
|
219
|
+
nodes = list(range(n_qubits))
|
|
220
|
+
G_m = G
|
|
221
|
+
|
|
222
|
+
if n_qubits < 3:
|
|
223
|
+
if n_qubits == 0:
|
|
224
|
+
return "", 0, ([], [])
|
|
225
|
+
|
|
226
|
+
if n_qubits == 1:
|
|
227
|
+
return "0", 0, (nodes, [])
|
|
228
|
+
|
|
229
|
+
if n_qubits == 2:
|
|
230
|
+
weight = G_m[0, 1]
|
|
231
|
+
if weight < 0.0:
|
|
232
|
+
return "00", 0, (nodes, [])
|
|
233
|
+
|
|
234
|
+
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
235
|
+
|
|
236
|
+
is_segmented = (G_m.nbytes << 1) > opencl_context.max_alloc
|
|
237
|
+
if is_segmented and is_alt_gpu_sampling:
|
|
238
|
+
print("[WARN] Using segmented solver, so disabling is_alt_gpu_sampling.")
|
|
239
|
+
is_alt_gpu_sampling = False
|
|
240
|
+
|
|
241
|
+
if quality is None:
|
|
242
|
+
quality = 2
|
|
243
|
+
|
|
244
|
+
if shots is None:
|
|
245
|
+
# Number of measurement shots
|
|
246
|
+
shots = n_qubits << quality
|
|
247
|
+
|
|
248
|
+
n_steps = n_qubits << quality
|
|
249
|
+
grid_size = n_steps * n_qubits
|
|
250
|
+
|
|
251
|
+
return cpu_footer(shots, quality, n_qubits, G_m, nodes, dtype)
|