PyQrackIsing 5.4.0__tar.gz → 5.4.2__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.4.0/pyqrackising.egg-info → pyqrackising-5.4.2}/PKG-INFO +1 -1
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/README.md +1 -1
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/pyproject.toml +1 -1
- pyqrackising-5.4.2/pyqrackising/kernels.cl +245 -0
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/pyqrackising/maxcut_tfim.py +21 -9
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/pyqrackising/maxcut_tfim_sparse.py +21 -9
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/pyqrackising/maxcut_tfim_streaming.py +21 -9
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/pyqrackising/maxcut_tfim_util.py +13 -4
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/pyqrackising/spin_glass_solver.py +85 -5
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/pyqrackising/spin_glass_solver_sparse.py +89 -5
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/pyqrackising/spin_glass_solver_streaming.py +5 -5
- {pyqrackising-5.4.0 → pyqrackising-5.4.2/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/setup.py +1 -1
- pyqrackising-5.4.0/pyqrackising/kernels.cl +0 -70
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/CMakeLists.txt +0 -0
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/LICENSE.md +0 -0
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/MANIFEST.in +0 -0
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/pyqrackising/__init__.py +0 -0
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/pyqrackising/spin_glass_solver_util.py +0 -0
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/pyqrackising/tsp.py +0 -0
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-5.4.0 → pyqrackising-5.4.2}/setup.cfg +0 -0
|
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
|
|
|
12
12
|
|
|
13
13
|
[project]
|
|
14
14
|
name = "pyqrackising"
|
|
15
|
-
version = "5.4.
|
|
15
|
+
version = "5.4.2"
|
|
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,245 @@
|
|
|
1
|
+
__kernel void init_theta(
|
|
2
|
+
__constant float* fargs,
|
|
3
|
+
const int n_qubits,
|
|
4
|
+
__constant float* J_eff,
|
|
5
|
+
__constant float* degrees,
|
|
6
|
+
__global float* theta
|
|
7
|
+
) {
|
|
8
|
+
const int q = get_global_id(0);
|
|
9
|
+
if (q >= n_qubits) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const float h_mult = fabs(fargs[2]);
|
|
14
|
+
|
|
15
|
+
const float J = J_eff[q];
|
|
16
|
+
const float z = degrees[q];
|
|
17
|
+
const float abs_zJ = fabs(z * J);
|
|
18
|
+
|
|
19
|
+
float val;
|
|
20
|
+
if (abs_zJ < 2e-40f) {
|
|
21
|
+
val = (J > 0.0f) ? M_PI_F : -M_PI_F;
|
|
22
|
+
} else {
|
|
23
|
+
val = asin(fmax(-1.0f, fmin(1.0f, h_mult / (z * J))));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
theta[q] = val;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// By Google Search AI
|
|
30
|
+
inline void AtomicAdd_g_f(volatile __global float *source, const float operand) {
|
|
31
|
+
union {
|
|
32
|
+
unsigned int intVal;
|
|
33
|
+
float floatVal;
|
|
34
|
+
} oldVal, newVal;
|
|
35
|
+
|
|
36
|
+
do {
|
|
37
|
+
oldVal.floatVal = *source;
|
|
38
|
+
newVal.floatVal = oldVal.floatVal + operand;
|
|
39
|
+
// Use atomic_cmpxchg to atomically update the value
|
|
40
|
+
} while (atomic_cmpxchg((volatile __global unsigned int*)source, oldVal.intVal, newVal.intVal) != oldVal.intVal);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// By Elara (custom OpenAI GPT)
|
|
44
|
+
inline float probability_by_hamming_weight(
|
|
45
|
+
int q, float J, float h, float z, float theta, float t, int n_qubits
|
|
46
|
+
) {
|
|
47
|
+
float ratio = fabs(h) / (z * J);
|
|
48
|
+
if (ratio > 1.0f) ratio = 1.0f;
|
|
49
|
+
float theta_c = asin(ratio);
|
|
50
|
+
|
|
51
|
+
float p = pow(2.0f, fabs(J / h) - 1.0f)
|
|
52
|
+
* (1.0f + sin(theta - theta_c) * cos(1.5f * M_PI_F * J * t + theta) / (1.0f + sqrt(t)))
|
|
53
|
+
- 0.5f;
|
|
54
|
+
|
|
55
|
+
if ((p * (n_qubits + 2.0f)) >= 1024.0f) return 0.0f;
|
|
56
|
+
|
|
57
|
+
float numerator = pow(2.0f, (n_qubits + 2.0f) * p) - 1.0f;
|
|
58
|
+
float denominator = pow(2.0f, p) - 1.0f;
|
|
59
|
+
float result = numerator * pow(2.0f, -((n_qubits + 1.0f) * p) - p * q) / denominator;
|
|
60
|
+
|
|
61
|
+
if (isnan(result) || isinf(result)) return 0.0f;
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
__kernel void maxcut_hamming_cdf(
|
|
66
|
+
int n_qubits,
|
|
67
|
+
__constant int* degrees,
|
|
68
|
+
__constant float* args,
|
|
69
|
+
__constant float* J_func,
|
|
70
|
+
__constant float* theta,
|
|
71
|
+
__global float* hamming_prob
|
|
72
|
+
) {
|
|
73
|
+
const float delta_t = args[0U];
|
|
74
|
+
const float tot_t = args[1U];
|
|
75
|
+
const float h_mult = args[2U];
|
|
76
|
+
const int step = get_group_id(0) / n_qubits;
|
|
77
|
+
const int qi = get_group_id(0) % n_qubits;
|
|
78
|
+
const float J_eff = J_func[qi];
|
|
79
|
+
const float z = degrees[qi];
|
|
80
|
+
if (fabs(z * J_eff) <= (pow(2.0f, -52))) return;
|
|
81
|
+
|
|
82
|
+
const float theta_eff = theta[qi];
|
|
83
|
+
const float t = step * delta_t;
|
|
84
|
+
const float tm1 = (step - 1) * delta_t;
|
|
85
|
+
const float h_t = h_mult * (tot_t - t);
|
|
86
|
+
|
|
87
|
+
const int n_threads = get_local_size(0);
|
|
88
|
+
|
|
89
|
+
for (int qo = get_local_id(0); qo < n_qubits; qo += n_threads) {
|
|
90
|
+
int _qo = (J_eff > 0.0f) ? (n_qubits - (1 + qo)) : qo;
|
|
91
|
+
float diff = probability_by_hamming_weight(_qo, J_eff, h_t, z, theta_eff, t, n_qubits);
|
|
92
|
+
diff -= probability_by_hamming_weight(_qo, J_eff, h_t, z, theta_eff, tm1, n_qubits);
|
|
93
|
+
AtomicAdd_g_f(&(hamming_prob[_qo]), diff);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
float bootstrap_worker(__constant char* theta, __global double* G_m, __constant int* indices, const int k, const int n) {
|
|
98
|
+
double energy = 0.0;
|
|
99
|
+
const size_t n_st = (size_t)n;
|
|
100
|
+
for (int u = 0; u < n; ++u) {
|
|
101
|
+
const size_t u_offset = u * n_st;
|
|
102
|
+
bool u_bit = theta[u];
|
|
103
|
+
for (int x = 0; x < k; ++x) {
|
|
104
|
+
if (indices[x] == u) {
|
|
105
|
+
u_bit = !u_bit;
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
for (int v = u + 1; v < n; ++v) {
|
|
110
|
+
const double val = G_m[u_offset + v];
|
|
111
|
+
bool v_bit = theta[v];
|
|
112
|
+
for (int x = 0; x < k; ++x) {
|
|
113
|
+
if (indices[x] == v) {
|
|
114
|
+
v_bit = !v_bit;
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
energy += (u_bit == v_bit) ? val : -val;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return (float)energy;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
__kernel void bootstrap(
|
|
126
|
+
__global double* G_m,
|
|
127
|
+
__constant char* best_theta,
|
|
128
|
+
__constant int* indices_array,
|
|
129
|
+
__constant int* args, // args[0] = n, args[1] = k
|
|
130
|
+
__global float* min_energy_ptr, // output: per-group min energy
|
|
131
|
+
__global int* min_index_ptr, // output: per-group best index (i)
|
|
132
|
+
__local float* loc_energy, // local memory buffer
|
|
133
|
+
__local int* loc_index // local memory buffer
|
|
134
|
+
) {
|
|
135
|
+
const int n = args[0];
|
|
136
|
+
const int k = args[1];
|
|
137
|
+
const int combo_count = args[2];
|
|
138
|
+
const int i = get_global_id(0);
|
|
139
|
+
|
|
140
|
+
float energy = INFINITY;
|
|
141
|
+
|
|
142
|
+
if (i < combo_count) {
|
|
143
|
+
const int j = i * k;
|
|
144
|
+
// Compute energy for this combination
|
|
145
|
+
energy = bootstrap_worker(best_theta, G_m, indices_array + j, k, n);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const int lt_id = get_local_id(0);
|
|
149
|
+
const int lt_size = get_local_size(0);
|
|
150
|
+
|
|
151
|
+
// Initialize local memory
|
|
152
|
+
loc_energy[lt_id] = energy;
|
|
153
|
+
loc_index[lt_id] = i;
|
|
154
|
+
|
|
155
|
+
// Reduce within workgroup
|
|
156
|
+
for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
|
|
157
|
+
barrier(CLK_LOCAL_MEM_FENCE);
|
|
158
|
+
if ((lt_id < offset) && (loc_energy[lt_id + offset] < loc_energy[lt_id])) {
|
|
159
|
+
loc_energy[lt_id] = loc_energy[lt_id + offset];
|
|
160
|
+
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Write out per-group result
|
|
165
|
+
if (lt_id == 0) {
|
|
166
|
+
min_energy_ptr[get_group_id(0)] = loc_energy[0];
|
|
167
|
+
min_index_ptr[get_group_id(0)] = loc_index[0];
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
float bootstrap_worker_sparse(__constant char* theta, __global double* G_data, __global ulong* G_rows, __global ulong* G_cols, __constant int* indices, const int k, const int n) {
|
|
172
|
+
double energy = 0.0;
|
|
173
|
+
for (int u = 0; u < n; ++u) {
|
|
174
|
+
bool u_bit = theta[u];
|
|
175
|
+
for (int x = 0; x < k; ++x) {
|
|
176
|
+
if (indices[x] == u) {
|
|
177
|
+
u_bit = !u_bit;
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
const size_t mCol = G_rows[u + 1];
|
|
182
|
+
for (int col = G_rows[u]; col < mCol; ++col) {
|
|
183
|
+
const int v = G_cols[col];
|
|
184
|
+
const double val = G_data[col];
|
|
185
|
+
bool v_bit = theta[v];
|
|
186
|
+
for (int x = 0; x < k; ++x) {
|
|
187
|
+
if (indices[x] == v) {
|
|
188
|
+
v_bit = !v_bit;
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
energy += (u_bit == v_bit) ? val : -val;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return (float)energy;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
__kernel void bootstrap_sparse(
|
|
200
|
+
__global double* G_data,
|
|
201
|
+
__global ulong* G_rows,
|
|
202
|
+
__global ulong* G_cols,
|
|
203
|
+
__constant char* best_theta,
|
|
204
|
+
__constant int* indices_array,
|
|
205
|
+
__constant int* args, // args[0] = n, args[1] = k
|
|
206
|
+
__global float* min_energy_ptr, // output: per-group min energy
|
|
207
|
+
__global int* min_index_ptr, // output: per-group best index (i)
|
|
208
|
+
__local float* loc_energy, // local memory buffer
|
|
209
|
+
__local int* loc_index // local memory buffer
|
|
210
|
+
) {
|
|
211
|
+
const int n = args[0];
|
|
212
|
+
const int k = args[1];
|
|
213
|
+
const int combo_count = args[2];
|
|
214
|
+
const int i = get_global_id(0);
|
|
215
|
+
|
|
216
|
+
float energy = INFINITY;
|
|
217
|
+
|
|
218
|
+
if (i < combo_count) {
|
|
219
|
+
const int j = i * k;
|
|
220
|
+
// Compute energy for this combination
|
|
221
|
+
energy = bootstrap_worker_sparse(best_theta, G_data, G_rows, G_cols, indices_array + j, k, n);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const int lt_id = get_local_id(0);
|
|
225
|
+
const int lt_size = get_local_size(0);
|
|
226
|
+
|
|
227
|
+
// Initialize local memory
|
|
228
|
+
loc_energy[lt_id] = energy;
|
|
229
|
+
loc_index[lt_id] = i;
|
|
230
|
+
|
|
231
|
+
// Reduce within workgroup
|
|
232
|
+
for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
|
|
233
|
+
barrier(CLK_LOCAL_MEM_FENCE);
|
|
234
|
+
if ((lt_id < offset) && (loc_energy[lt_id + offset] < loc_energy[lt_id])) {
|
|
235
|
+
loc_energy[lt_id] = loc_energy[lt_id + offset];
|
|
236
|
+
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Write out per-group result
|
|
241
|
+
if (lt_id == 0) {
|
|
242
|
+
min_energy_ptr[get_group_id(0)] = loc_energy[0];
|
|
243
|
+
min_index_ptr[get_group_id(0)] = loc_index[0];
|
|
244
|
+
}
|
|
245
|
+
}
|
|
@@ -4,7 +4,7 @@ import numpy as np
|
|
|
4
4
|
import os
|
|
5
5
|
from numba import njit, prange
|
|
6
6
|
|
|
7
|
-
from .maxcut_tfim_util import get_cut,
|
|
7
|
+
from .maxcut_tfim_util import get_cut, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
8
8
|
|
|
9
9
|
IS_OPENCL_AVAILABLE = True
|
|
10
10
|
try:
|
|
@@ -82,7 +82,7 @@ def compute_energy(sample, G_m, n_qubits):
|
|
|
82
82
|
def sample_for_solution(G_m, shots, thresholds, J_eff):
|
|
83
83
|
n = len(G_m)
|
|
84
84
|
max_weight = G_m.max()
|
|
85
|
-
weights = (1.0 / (1.0 + (
|
|
85
|
+
weights = (1.0 / (1.0 + (2e-52) - J_eff)).astype(np.float64)
|
|
86
86
|
|
|
87
87
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
88
88
|
energies = np.empty(shots, dtype=np.float32)
|
|
@@ -205,17 +205,34 @@ def maxcut_tfim(
|
|
|
205
205
|
return cpu_footer(shots, quality, n_qubits, G_m, nodes)
|
|
206
206
|
|
|
207
207
|
J_eff, degrees = init_J_and_z(G_m)
|
|
208
|
-
hamming_prob = init_thresholds(n_qubits)
|
|
209
208
|
|
|
210
209
|
delta_t = 1.0 / n_steps
|
|
211
210
|
tot_t = 2.0 * n_steps * delta_t
|
|
212
211
|
h_mult = 2.0 / tot_t
|
|
213
|
-
|
|
212
|
+
|
|
214
213
|
args = np.empty(3, dtype=np.float32)
|
|
215
214
|
args[0] = delta_t
|
|
216
215
|
args[1] = tot_t
|
|
217
216
|
args[2] = h_mult
|
|
218
217
|
|
|
218
|
+
# Move to GPU
|
|
219
|
+
mf = cl.mem_flags
|
|
220
|
+
args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
|
|
221
|
+
J_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
|
|
222
|
+
deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
|
|
223
|
+
theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE, size=(n_qubits * 4))
|
|
224
|
+
|
|
225
|
+
# Warp size is 32:
|
|
226
|
+
group_size = min(n_qubits, 64)
|
|
227
|
+
global_size = ((n_qubits + group_size - 1) // group_size) * group_size
|
|
228
|
+
|
|
229
|
+
opencl_context.init_theta_kernel(
|
|
230
|
+
opencl_context.queue, (global_size,), (group_size,),
|
|
231
|
+
args_buf, np.int32(n_qubits), J_buf, deg_buf, theta_buf
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
hamming_prob = init_thresholds(n_qubits)
|
|
235
|
+
|
|
219
236
|
# Warp size is 32:
|
|
220
237
|
group_size = n_qubits - 1
|
|
221
238
|
if group_size > 256:
|
|
@@ -223,11 +240,6 @@ def maxcut_tfim(
|
|
|
223
240
|
grid_dim = n_steps * n_qubits * group_size
|
|
224
241
|
|
|
225
242
|
# Move to GPU
|
|
226
|
-
mf = cl.mem_flags
|
|
227
|
-
args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
|
|
228
|
-
J_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
|
|
229
|
-
deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
|
|
230
|
-
theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=theta)
|
|
231
243
|
ham_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=hamming_prob)
|
|
232
244
|
|
|
233
245
|
# Kernel execution
|
|
@@ -5,7 +5,7 @@ import os
|
|
|
5
5
|
from numba import njit, prange
|
|
6
6
|
from scipy.sparse import lil_matrix, csr_matrix
|
|
7
7
|
|
|
8
|
-
from .maxcut_tfim_util import get_cut,
|
|
8
|
+
from .maxcut_tfim_util import get_cut, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
9
9
|
|
|
10
10
|
IS_OPENCL_AVAILABLE = True
|
|
11
11
|
try:
|
|
@@ -114,7 +114,7 @@ def compute_energy(sample, G_data, G_rows, G_cols):
|
|
|
114
114
|
def sample_for_solution(G_data, G_rows, G_cols, shots, thresholds, J_eff):
|
|
115
115
|
n = G_rows.shape[0] - 1
|
|
116
116
|
max_weight = G_data.max()
|
|
117
|
-
weights = (1.0 / (1.0 + (
|
|
117
|
+
weights = (1.0 / (1.0 + (2e-52) - J_eff)).astype(np.float64)
|
|
118
118
|
|
|
119
119
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
120
120
|
energies = np.empty(shots, dtype=np.float32)
|
|
@@ -264,17 +264,34 @@ def maxcut_tfim_sparse(
|
|
|
264
264
|
return cpu_footer(shots, quality, n_qubits, G_m.data, G_m.indptr, G_m.indices, nodes)
|
|
265
265
|
|
|
266
266
|
J_eff, degrees = init_J_and_z(G_m.data, G_m.indptr, G_m.indices)
|
|
267
|
-
hamming_prob = init_thresholds(n_qubits)
|
|
268
267
|
|
|
269
268
|
delta_t = 1.0 / n_steps
|
|
270
269
|
tot_t = 2.0 * n_steps * delta_t
|
|
271
270
|
h_mult = 2.0 / tot_t
|
|
272
|
-
|
|
271
|
+
|
|
273
272
|
args = np.empty(3, dtype=np.float32)
|
|
274
273
|
args[0] = delta_t
|
|
275
274
|
args[1] = tot_t
|
|
276
275
|
args[2] = h_mult
|
|
277
276
|
|
|
277
|
+
# Move to GPU
|
|
278
|
+
mf = cl.mem_flags
|
|
279
|
+
args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
|
|
280
|
+
J_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
|
|
281
|
+
deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
|
|
282
|
+
theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE, size=(n_qubits * 4))
|
|
283
|
+
|
|
284
|
+
# Warp size is 32:
|
|
285
|
+
group_size = min(n_qubits, 64)
|
|
286
|
+
global_size = ((n_qubits + group_size - 1) // group_size) * group_size
|
|
287
|
+
|
|
288
|
+
opencl_context.init_theta_kernel(
|
|
289
|
+
opencl_context.queue, (global_size,), (group_size,),
|
|
290
|
+
args_buf, np.int32(n_qubits), J_buf, deg_buf, theta_buf
|
|
291
|
+
)
|
|
292
|
+
|
|
293
|
+
hamming_prob = init_thresholds(n_qubits)
|
|
294
|
+
|
|
278
295
|
# Warp size is 32:
|
|
279
296
|
group_size = n_qubits - 1
|
|
280
297
|
if group_size > 256:
|
|
@@ -282,11 +299,6 @@ def maxcut_tfim_sparse(
|
|
|
282
299
|
grid_dim = n_steps * n_qubits * group_size
|
|
283
300
|
|
|
284
301
|
# Move to GPU
|
|
285
|
-
mf = cl.mem_flags
|
|
286
|
-
args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
|
|
287
|
-
J_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
|
|
288
|
-
deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
|
|
289
|
-
theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=theta)
|
|
290
302
|
ham_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=hamming_prob)
|
|
291
303
|
|
|
292
304
|
# Kernel execution
|
|
@@ -4,7 +4,7 @@ import numpy as np
|
|
|
4
4
|
import os
|
|
5
5
|
from numba import njit, prange
|
|
6
6
|
|
|
7
|
-
from .maxcut_tfim_util import get_cut,
|
|
7
|
+
from .maxcut_tfim_util import get_cut, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
8
8
|
|
|
9
9
|
IS_OPENCL_AVAILABLE = True
|
|
10
10
|
try:
|
|
@@ -80,7 +80,7 @@ def compute_energy(sample, G_func, G_func_args_tuple, nodes, n_qubits):
|
|
|
80
80
|
|
|
81
81
|
@njit(parallel=True)
|
|
82
82
|
def sample_for_solution(G_func, G_func_args_tuple, nodes, max_weight, shots, thresholds, degrees_sum, J_eff, n):
|
|
83
|
-
weights = (1.0 / (1.0 + (
|
|
83
|
+
weights = (1.0 / (1.0 + (2e-52) - J_eff)).astype(np.float64)
|
|
84
84
|
|
|
85
85
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
86
86
|
energies = np.empty(shots, dtype=np.float32)
|
|
@@ -203,17 +203,34 @@ def maxcut_tfim_streaming(
|
|
|
203
203
|
return cpu_footer(shots, quality, n_qubits, G_func, G_func_args_tuple, nodes)
|
|
204
204
|
|
|
205
205
|
J_eff, degrees, G_max = init_J_and_z(G_func, G_func_args_tuple, nodes)
|
|
206
|
-
hamming_prob = init_thresholds(n_qubits)
|
|
207
206
|
|
|
208
207
|
delta_t = 1.0 / n_steps
|
|
209
208
|
tot_t = 2.0 * n_steps * delta_t
|
|
210
209
|
h_mult = 2.0 / tot_t
|
|
211
|
-
|
|
210
|
+
|
|
212
211
|
args = np.empty(3, dtype=np.float32)
|
|
213
212
|
args[0] = delta_t
|
|
214
213
|
args[1] = tot_t
|
|
215
214
|
args[2] = h_mult
|
|
216
215
|
|
|
216
|
+
# Move to GPU
|
|
217
|
+
mf = cl.mem_flags
|
|
218
|
+
args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
|
|
219
|
+
J_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
|
|
220
|
+
deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
|
|
221
|
+
theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE, size=(n_qubits * 4))
|
|
222
|
+
|
|
223
|
+
# Warp size is 32:
|
|
224
|
+
group_size = min(n_qubits, 64)
|
|
225
|
+
global_size = ((n_qubits + group_size - 1) // group_size) * group_size
|
|
226
|
+
|
|
227
|
+
opencl_context.init_theta_kernel(
|
|
228
|
+
opencl_context.queue, (global_size,), (group_size,),
|
|
229
|
+
args_buf, np.int32(n_qubits), J_buf, deg_buf, theta_buf
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
hamming_prob = init_thresholds(n_qubits)
|
|
233
|
+
|
|
217
234
|
# Warp size is 32:
|
|
218
235
|
group_size = n_qubits - 1
|
|
219
236
|
if group_size > 256:
|
|
@@ -221,11 +238,6 @@ def maxcut_tfim_streaming(
|
|
|
221
238
|
grid_dim = n_steps * n_qubits * group_size
|
|
222
239
|
|
|
223
240
|
# Move to GPU
|
|
224
|
-
mf = cl.mem_flags
|
|
225
|
-
args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
|
|
226
|
-
J_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
|
|
227
|
-
deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
|
|
228
|
-
theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=theta)
|
|
229
241
|
ham_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=hamming_prob)
|
|
230
242
|
|
|
231
243
|
# Kernel execution
|
|
@@ -21,7 +21,7 @@ def get_cut(solution, nodes):
|
|
|
21
21
|
|
|
22
22
|
|
|
23
23
|
@njit(parallel=True)
|
|
24
|
-
def init_theta(
|
|
24
|
+
def init_theta(h_mult, n_qubits, J_eff, degrees):
|
|
25
25
|
theta = np.empty(n_qubits, dtype=np.float32)
|
|
26
26
|
h_mult = abs(h_mult)
|
|
27
27
|
for q in prange(n_qubits):
|
|
@@ -133,16 +133,22 @@ def probability_by_hamming_weight(J, h, z, theta, t, n_qubits):
|
|
|
133
133
|
|
|
134
134
|
|
|
135
135
|
class OpenCLContext:
|
|
136
|
-
def __init__(self, a, c, q,
|
|
136
|
+
def __init__(self, a, c, q, i, m, b, s):
|
|
137
137
|
self.IS_OPENCL_AVAILABLE = a
|
|
138
138
|
self.ctx = c
|
|
139
139
|
self.queue = q
|
|
140
|
-
self.
|
|
140
|
+
self.init_theta_kernel = i
|
|
141
|
+
self.maxcut_hamming_cdf_kernel = m
|
|
142
|
+
self.bootstrap_kernel = b
|
|
143
|
+
self.bootstrap_sparse_kernel = s
|
|
141
144
|
|
|
142
145
|
IS_OPENCL_AVAILABLE = True
|
|
143
146
|
ctx = None
|
|
144
147
|
queue = None
|
|
148
|
+
init_theta_kernel = None
|
|
145
149
|
maxcut_hamming_cdf_kernel = None
|
|
150
|
+
bootstrap_kernel = None
|
|
151
|
+
bootstrap_sparse_kernel = None
|
|
146
152
|
try:
|
|
147
153
|
import pyopencl as cl
|
|
148
154
|
|
|
@@ -153,9 +159,12 @@ try:
|
|
|
153
159
|
# Load and build OpenCL kernels
|
|
154
160
|
kernel_src = open(os.path.dirname(os.path.abspath(__file__)) + "/kernels.cl").read()
|
|
155
161
|
program = cl.Program(ctx, kernel_src).build()
|
|
162
|
+
init_theta_kernel = program.init_theta
|
|
156
163
|
maxcut_hamming_cdf_kernel = program.maxcut_hamming_cdf
|
|
164
|
+
bootstrap_kernel = program.bootstrap
|
|
165
|
+
bootstrap_sparse_kernel = program.bootstrap_sparse
|
|
157
166
|
except ImportError:
|
|
158
167
|
IS_OPENCL_AVAILABLE = False
|
|
159
168
|
print("PyOpenCL not installed. (If you have any OpenCL accelerator devices with available ICDs, you might want to optionally install pyopencl.)")
|
|
160
169
|
|
|
161
|
-
opencl_context = OpenCLContext(IS_OPENCL_AVAILABLE, ctx, queue, maxcut_hamming_cdf_kernel)
|
|
170
|
+
opencl_context = OpenCLContext(IS_OPENCL_AVAILABLE, ctx, queue, init_theta_kernel, maxcut_hamming_cdf_kernel, bootstrap_kernel, bootstrap_sparse_kernel)
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
from .maxcut_tfim import maxcut_tfim
|
|
2
|
+
from .maxcut_tfim_util import opencl_context
|
|
2
3
|
from .spin_glass_solver_util import get_cut_from_bit_array, int_to_bitstring
|
|
3
4
|
import itertools
|
|
4
5
|
import networkx as nx
|
|
@@ -7,6 +8,13 @@ from numba import njit, prange
|
|
|
7
8
|
import os
|
|
8
9
|
|
|
9
10
|
|
|
11
|
+
IS_OPENCL_AVAILABLE = True
|
|
12
|
+
try:
|
|
13
|
+
import pyopencl as cl
|
|
14
|
+
except ImportError:
|
|
15
|
+
IS_OPENCL_AVAILABLE = False
|
|
16
|
+
|
|
17
|
+
|
|
10
18
|
@njit
|
|
11
19
|
def evaluate_cut_edges(theta_bits, G_m):
|
|
12
20
|
n_qubits = len(G_m)
|
|
@@ -41,8 +49,8 @@ def bootstrap_worker(theta, G_m, indices):
|
|
|
41
49
|
|
|
42
50
|
|
|
43
51
|
@njit(parallel=True)
|
|
44
|
-
def bootstrap(best_theta, G_m,
|
|
45
|
-
n =
|
|
52
|
+
def bootstrap(best_theta, G_m, indices_array, k, min_energy):
|
|
53
|
+
n = len(indices_array) // k
|
|
46
54
|
energies = np.empty(n, dtype=np.float32)
|
|
47
55
|
for i in prange(n):
|
|
48
56
|
j = i * k
|
|
@@ -59,6 +67,75 @@ def bootstrap(best_theta, G_m, k, indices_array, min_energy):
|
|
|
59
67
|
return min_energy
|
|
60
68
|
|
|
61
69
|
|
|
70
|
+
def run_bootstrap_opencl(best_theta, G_m_np, indices_array_np, k, min_energy):
|
|
71
|
+
ctx = opencl_context.ctx
|
|
72
|
+
queue = opencl_context.queue
|
|
73
|
+
bootstrap_kernel = opencl_context.bootstrap_kernel
|
|
74
|
+
|
|
75
|
+
n = best_theta.shape[0]
|
|
76
|
+
combo_count = len(indices_array_np) // k
|
|
77
|
+
|
|
78
|
+
best_theta_np = np.array([(1 if b else 0) for b in best_theta], dtype=np.int8)
|
|
79
|
+
|
|
80
|
+
# Args: [n, k, combo_count]
|
|
81
|
+
args_np = np.array([n, k, combo_count], dtype=np.int32)
|
|
82
|
+
|
|
83
|
+
# Buffers
|
|
84
|
+
mf = cl.mem_flags
|
|
85
|
+
best_theta_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=best_theta_np)
|
|
86
|
+
G_m_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m_np)
|
|
87
|
+
indices_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=indices_array_np)
|
|
88
|
+
args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args_np)
|
|
89
|
+
|
|
90
|
+
# Allocate min_energy and min_index result buffers per workgroup
|
|
91
|
+
min_energy_host = np.empty(combo_count, dtype=np.float32)
|
|
92
|
+
min_index_host = np.empty(combo_count, dtype=np.int32)
|
|
93
|
+
|
|
94
|
+
min_energy_buf = cl.Buffer(ctx, mf.WRITE_ONLY, min_energy_host.nbytes)
|
|
95
|
+
min_index_buf = cl.Buffer(ctx, mf.WRITE_ONLY, min_index_host.nbytes)
|
|
96
|
+
|
|
97
|
+
# Local memory allocation (1 float per work item)
|
|
98
|
+
local_size = min(64, n)
|
|
99
|
+
global_size = ((combo_count + local_size - 1) // local_size) * local_size
|
|
100
|
+
local_energy_buf = cl.LocalMemory(np.dtype(np.float32).itemsize * local_size)
|
|
101
|
+
local_index_buf = cl.LocalMemory(np.dtype(np.int32).itemsize * local_size)
|
|
102
|
+
|
|
103
|
+
# Set kernel args
|
|
104
|
+
bootstrap_kernel.set_args(
|
|
105
|
+
G_m_buf,
|
|
106
|
+
best_theta_buf,
|
|
107
|
+
indices_buf,
|
|
108
|
+
args_buf,
|
|
109
|
+
min_energy_buf,
|
|
110
|
+
min_index_buf,
|
|
111
|
+
local_energy_buf,
|
|
112
|
+
local_index_buf
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
cl.enqueue_nd_range_kernel(queue, bootstrap_kernel, (global_size,), (local_size,))
|
|
116
|
+
|
|
117
|
+
# Read results
|
|
118
|
+
cl.enqueue_copy(queue, min_energy_host, min_energy_buf)
|
|
119
|
+
cl.enqueue_copy(queue, min_index_host, min_index_buf)
|
|
120
|
+
queue.finish()
|
|
121
|
+
|
|
122
|
+
# Find global minimum
|
|
123
|
+
min_idx = np.argmin(min_energy_host)
|
|
124
|
+
energy = min_energy_host[min_idx]
|
|
125
|
+
if min_energy < energy:
|
|
126
|
+
return min_energy
|
|
127
|
+
|
|
128
|
+
best_i = min_index_host[min_idx]
|
|
129
|
+
|
|
130
|
+
flip_index_start = best_i * k
|
|
131
|
+
indices_to_flip = indices_array_np[flip_index_start : flip_index_start + k]
|
|
132
|
+
|
|
133
|
+
for i in indices_to_flip:
|
|
134
|
+
best_theta[i] = not best_theta[i]
|
|
135
|
+
|
|
136
|
+
return energy
|
|
137
|
+
|
|
138
|
+
|
|
62
139
|
def spin_glass_solver(G, quality=None, shots=None, best_guess=None):
|
|
63
140
|
nodes = None
|
|
64
141
|
n_qubits = 0
|
|
@@ -113,14 +190,17 @@ def spin_glass_solver(G, quality=None, shots=None, best_guess=None):
|
|
|
113
190
|
|
|
114
191
|
combos = []
|
|
115
192
|
if len(combos_list) < k:
|
|
116
|
-
combos = list(
|
|
193
|
+
combos = np.array(list(
|
|
117
194
|
item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist
|
|
118
|
-
)
|
|
195
|
+
))
|
|
119
196
|
combos_list.append(combos)
|
|
120
197
|
else:
|
|
121
198
|
combos = combos_list[k - 1]
|
|
122
199
|
|
|
123
|
-
|
|
200
|
+
if IS_OPENCL_AVAILABLE:
|
|
201
|
+
energy = run_bootstrap_opencl(best_theta, G_m, combos, k, min_energy)
|
|
202
|
+
else:
|
|
203
|
+
energy = bootstrap(best_theta, G_m, combos, k, min_energy)
|
|
124
204
|
|
|
125
205
|
if energy < min_energy:
|
|
126
206
|
min_energy = energy
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
from .maxcut_tfim_sparse import maxcut_tfim_sparse
|
|
2
|
+
from .maxcut_tfim_util import opencl_context
|
|
2
3
|
from .spin_glass_solver_util import get_cut_from_bit_array, int_to_bitstring
|
|
3
4
|
import itertools
|
|
4
5
|
import networkx as nx
|
|
@@ -8,6 +9,13 @@ import os
|
|
|
8
9
|
from scipy.sparse import lil_matrix, csr_matrix
|
|
9
10
|
|
|
10
11
|
|
|
12
|
+
IS_OPENCL_AVAILABLE = True
|
|
13
|
+
try:
|
|
14
|
+
import pyopencl as cl
|
|
15
|
+
except ImportError:
|
|
16
|
+
IS_OPENCL_AVAILABLE = False
|
|
17
|
+
|
|
18
|
+
|
|
11
19
|
@njit
|
|
12
20
|
def evaluate_cut_edges(theta_bits, G_data, G_rows, G_cols):
|
|
13
21
|
n = G_rows.shape[0] - 1
|
|
@@ -44,8 +52,8 @@ def bootstrap_worker(theta, G_data, G_rows, G_cols, indices):
|
|
|
44
52
|
|
|
45
53
|
|
|
46
54
|
@njit(parallel=True)
|
|
47
|
-
def bootstrap(best_theta, G_data, G_rows, G_cols,
|
|
48
|
-
n =
|
|
55
|
+
def bootstrap(best_theta, G_data, G_rows, G_cols, indices_array, k, min_energy):
|
|
56
|
+
n = len(indices_array) // k
|
|
49
57
|
energies = np.empty(n, dtype=np.float32)
|
|
50
58
|
for i in prange(n):
|
|
51
59
|
j = i * k
|
|
@@ -62,6 +70,79 @@ def bootstrap(best_theta, G_data, G_rows, G_cols, k, indices_array, min_energy):
|
|
|
62
70
|
return min_energy
|
|
63
71
|
|
|
64
72
|
|
|
73
|
+
def run_bootstrap_opencl(best_theta, G_data_np, G_rows_np, G_cols_np, indices_array_np, k, min_energy):
|
|
74
|
+
ctx = opencl_context.ctx
|
|
75
|
+
queue = opencl_context.queue
|
|
76
|
+
bootstrap_kernel = opencl_context.bootstrap_sparse_kernel
|
|
77
|
+
|
|
78
|
+
n = best_theta.shape[0]
|
|
79
|
+
combo_count = len(indices_array_np) // k
|
|
80
|
+
|
|
81
|
+
best_theta_np = np.array([(1 if b else 0) for b in best_theta], dtype=np.int8)
|
|
82
|
+
|
|
83
|
+
# Args: [n, k, combo_count]
|
|
84
|
+
args_np = np.array([n, k, combo_count], dtype=np.int32)
|
|
85
|
+
|
|
86
|
+
# Buffers
|
|
87
|
+
mf = cl.mem_flags
|
|
88
|
+
best_theta_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=best_theta_np)
|
|
89
|
+
G_data_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_data_np)
|
|
90
|
+
G_rows_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_rows_np.astype(np.uint64))
|
|
91
|
+
G_cols_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_cols_np.astype(np.uint64))
|
|
92
|
+
indices_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=indices_array_np)
|
|
93
|
+
args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args_np)
|
|
94
|
+
|
|
95
|
+
# Allocate min_energy and min_index result buffers per workgroup
|
|
96
|
+
min_energy_host = np.empty(combo_count, dtype=np.float32)
|
|
97
|
+
min_index_host = np.empty(combo_count, dtype=np.int32)
|
|
98
|
+
|
|
99
|
+
min_energy_buf = cl.Buffer(ctx, mf.WRITE_ONLY, min_energy_host.nbytes)
|
|
100
|
+
min_index_buf = cl.Buffer(ctx, mf.WRITE_ONLY, min_index_host.nbytes)
|
|
101
|
+
|
|
102
|
+
# Local memory allocation (1 float per work item)
|
|
103
|
+
local_size = min(64, n)
|
|
104
|
+
global_size = ((combo_count + local_size - 1) // local_size) * local_size
|
|
105
|
+
local_energy_buf = cl.LocalMemory(np.dtype(np.float32).itemsize * local_size)
|
|
106
|
+
local_index_buf = cl.LocalMemory(np.dtype(np.int32).itemsize * local_size)
|
|
107
|
+
|
|
108
|
+
# Set kernel args
|
|
109
|
+
bootstrap_kernel.set_args(
|
|
110
|
+
G_data_buf,
|
|
111
|
+
G_rows_buf,
|
|
112
|
+
G_cols_buf,
|
|
113
|
+
best_theta_buf,
|
|
114
|
+
indices_buf,
|
|
115
|
+
args_buf,
|
|
116
|
+
min_energy_buf,
|
|
117
|
+
min_index_buf,
|
|
118
|
+
local_energy_buf,
|
|
119
|
+
local_index_buf
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
cl.enqueue_nd_range_kernel(queue, bootstrap_kernel, (global_size,), (local_size,))
|
|
123
|
+
|
|
124
|
+
# Read results
|
|
125
|
+
cl.enqueue_copy(queue, min_energy_host, min_energy_buf)
|
|
126
|
+
cl.enqueue_copy(queue, min_index_host, min_index_buf)
|
|
127
|
+
queue.finish()
|
|
128
|
+
|
|
129
|
+
# Find global minimum
|
|
130
|
+
min_idx = np.argmin(min_energy_host)
|
|
131
|
+
energy = min_energy_host[min_idx]
|
|
132
|
+
if min_energy < energy:
|
|
133
|
+
return min_energy
|
|
134
|
+
|
|
135
|
+
best_i = min_index_host[min_idx]
|
|
136
|
+
|
|
137
|
+
flip_index_start = best_i * k
|
|
138
|
+
indices_to_flip = indices_array_np[flip_index_start : flip_index_start + k]
|
|
139
|
+
|
|
140
|
+
for i in indices_to_flip:
|
|
141
|
+
best_theta[i] = not best_theta[i]
|
|
142
|
+
|
|
143
|
+
return energy
|
|
144
|
+
|
|
145
|
+
|
|
65
146
|
def to_scipy_sparse_upper_triangular(G, nodes, n_nodes):
|
|
66
147
|
lil = lil_matrix((n_nodes, n_nodes), dtype=np.float64)
|
|
67
148
|
for u in range(n_nodes):
|
|
@@ -128,14 +209,17 @@ def spin_glass_solver_sparse(G, quality=None, shots=None, best_guess=None):
|
|
|
128
209
|
|
|
129
210
|
combos = []
|
|
130
211
|
if len(combos_list) < k:
|
|
131
|
-
combos = list(
|
|
212
|
+
combos = np.array(list(
|
|
132
213
|
item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist
|
|
133
|
-
)
|
|
214
|
+
))
|
|
134
215
|
combos_list.append(combos)
|
|
135
216
|
else:
|
|
136
217
|
combos = combos_list[k - 1]
|
|
137
218
|
|
|
138
|
-
|
|
219
|
+
if IS_OPENCL_AVAILABLE:
|
|
220
|
+
energy = run_bootstrap_opencl(best_theta, G_m.data, G_m.indptr, G_m.indices, combos, k, min_energy)
|
|
221
|
+
else:
|
|
222
|
+
energy = bootstrap(best_theta, G_m.data, G_m.indptr, G_m.indices, combos, k, min_energy)
|
|
139
223
|
|
|
140
224
|
if energy < min_energy:
|
|
141
225
|
min_energy = energy
|
|
@@ -42,8 +42,8 @@ def bootstrap_worker(theta, G_func, G_func_args_tuple, nodes, indices):
|
|
|
42
42
|
|
|
43
43
|
|
|
44
44
|
@njit(parallel=True)
|
|
45
|
-
def bootstrap(best_theta, G_func, G_func_args_tuple, nodes,
|
|
46
|
-
n =
|
|
45
|
+
def bootstrap(best_theta, G_func, G_func_args_tuple, nodes, indices_array, k, min_energy):
|
|
46
|
+
n = len(indices_array) // k
|
|
47
47
|
energies = np.empty(n, dtype=np.float32)
|
|
48
48
|
for i in prange(n):
|
|
49
49
|
j = i * k
|
|
@@ -111,14 +111,14 @@ def spin_glass_solver_streaming(
|
|
|
111
111
|
|
|
112
112
|
combos = []
|
|
113
113
|
if len(combos_list) < k:
|
|
114
|
-
combos = list(
|
|
114
|
+
combos = np.array(list(
|
|
115
115
|
item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist
|
|
116
|
-
)
|
|
116
|
+
))
|
|
117
117
|
combos_list.append(combos)
|
|
118
118
|
else:
|
|
119
119
|
combos = combos_list[k - 1]
|
|
120
120
|
|
|
121
|
-
energy = bootstrap(best_theta, G_func, G_func_args_tuple, nodes,
|
|
121
|
+
energy = bootstrap(best_theta, G_func, G_func_args_tuple, nodes, combos, k, min_energy)
|
|
122
122
|
|
|
123
123
|
if energy < min_energy:
|
|
124
124
|
min_energy = energy
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='pyqrackising',
|
|
54
|
-
version='5.4.
|
|
54
|
+
version='5.4.2',
|
|
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)',
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
// By Google Search AI
|
|
2
|
-
inline void AtomicAdd_g_f(volatile __global float *source, const float operand) {
|
|
3
|
-
union {
|
|
4
|
-
unsigned int intVal;
|
|
5
|
-
float floatVal;
|
|
6
|
-
} oldVal, newVal;
|
|
7
|
-
|
|
8
|
-
do {
|
|
9
|
-
oldVal.floatVal = *source;
|
|
10
|
-
newVal.floatVal = oldVal.floatVal + operand;
|
|
11
|
-
// Use atomic_cmpxchg to atomically update the value
|
|
12
|
-
} while (atomic_cmpxchg((volatile __global unsigned int*)source, oldVal.intVal, newVal.intVal) != oldVal.intVal);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// By Elara (custom OpenAI GPT)
|
|
16
|
-
inline float probability_by_hamming_weight(
|
|
17
|
-
int q, float J, float h, float z, float theta, float t, int n_qubits
|
|
18
|
-
) {
|
|
19
|
-
float ratio = fabs(h) / (z * J);
|
|
20
|
-
if (ratio > 1.0f) ratio = 1.0f;
|
|
21
|
-
float theta_c = asin(ratio);
|
|
22
|
-
|
|
23
|
-
float p = pow(2.0f, fabs(J / h) - 1.0f)
|
|
24
|
-
* (1.0f + sin(theta - theta_c) * cos(1.5f * M_PI_F * J * t + theta) / (1.0f + sqrt(t)))
|
|
25
|
-
- 0.5f;
|
|
26
|
-
|
|
27
|
-
if ((p * (n_qubits + 2.0f)) >= 1024.0f) return 0.0f;
|
|
28
|
-
|
|
29
|
-
float numerator = pow(2.0f, (n_qubits + 2.0f) * p) - 1.0f;
|
|
30
|
-
float denominator = pow(2.0f, p) - 1.0f;
|
|
31
|
-
float result = numerator * pow(2.0f, -((n_qubits + 1.0f) * p) - p * q) / denominator;
|
|
32
|
-
|
|
33
|
-
if (isnan(result) || isinf(result)) return 0.0f;
|
|
34
|
-
return result;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
__kernel void maxcut_hamming_cdf(
|
|
38
|
-
int n_qubits,
|
|
39
|
-
__constant int* degrees,
|
|
40
|
-
__constant float* args,
|
|
41
|
-
__constant float* J_func,
|
|
42
|
-
__constant float* theta,
|
|
43
|
-
__global float* hamming_prob
|
|
44
|
-
) {
|
|
45
|
-
float delta_t = args[0];
|
|
46
|
-
float tot_t = args[1];
|
|
47
|
-
float h_mult = args[2];
|
|
48
|
-
int step = get_group_id(0) / n_qubits;
|
|
49
|
-
int qi = get_group_id(0) % n_qubits;
|
|
50
|
-
float J_eff = J_func[qi];
|
|
51
|
-
float z = degrees[qi];
|
|
52
|
-
if (fabs(z * J_eff) <= (pow(2.0f, -52))) return;
|
|
53
|
-
|
|
54
|
-
float theta_eff = theta[qi];
|
|
55
|
-
float t = step * delta_t;
|
|
56
|
-
float tm1 = (step - 1) * delta_t;
|
|
57
|
-
float h_t = h_mult * (tot_t - t);
|
|
58
|
-
|
|
59
|
-
int qo = get_local_id(0);
|
|
60
|
-
int n_threads = get_local_size(0);
|
|
61
|
-
|
|
62
|
-
while (qo < n_qubits) {
|
|
63
|
-
int _qo = (J_eff > 0.0f) ? (n_qubits - (1 + qo)) : qo;
|
|
64
|
-
float diff = probability_by_hamming_weight(_qo, J_eff, h_t, z, theta_eff, t, n_qubits);
|
|
65
|
-
diff -= probability_by_hamming_weight(_qo, J_eff, h_t, z, theta_eff, tm1, n_qubits);
|
|
66
|
-
AtomicAdd_g_f(&(hamming_prob[_qo]), diff);
|
|
67
|
-
qo += n_threads;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
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
|