PyQrackIsing 5.6.0__tar.gz → 5.7.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-5.6.0/pyqrackising.egg-info → pyqrackising-5.7.1}/PKG-INFO +1 -1
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/README.md +4 -4
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/pyproject.toml +1 -1
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/pyqrackising/kernels.cl +34 -15
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/pyqrackising/maxcut_tfim.py +19 -9
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/pyqrackising/maxcut_tfim_sparse.py +19 -9
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/pyqrackising/maxcut_tfim_streaming.py +19 -8
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/pyqrackising/spin_glass_solver.py +9 -7
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/pyqrackising/spin_glass_solver_sparse.py +9 -7
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/pyqrackising/spin_glass_solver_streaming.py +3 -2
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/pyqrackising/tsp.py +84 -8
- {pyqrackising-5.6.0 → pyqrackising-5.7.1/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/setup.py +1 -1
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/CMakeLists.txt +0 -0
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/LICENSE.md +0 -0
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/MANIFEST.in +0 -0
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/pyqrackising/__init__.py +0 -0
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/pyqrackising/maxcut_tfim_util.py +0 -0
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/pyqrackising/spin_glass_solver_util.py +0 -0
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-5.6.0 → pyqrackising-5.7.1}/setup.cfg +0 -0
|
@@ -50,10 +50,10 @@ from pyqrackising import maxcut_tfim
|
|
|
50
50
|
import networkx as nx
|
|
51
51
|
|
|
52
52
|
G = nx.petersen_graph()
|
|
53
|
-
best_solution_bit_string, best_cut_value, best_node_groups = maxcut_tfim(G, quality=2, is_alt_gpu_sampling=True)
|
|
53
|
+
best_solution_bit_string, best_cut_value, best_node_groups = maxcut_tfim(G, quality=2, is_base_maxcut_gpu=True, is_alt_gpu_sampling=True)
|
|
54
54
|
```
|
|
55
55
|
|
|
56
|
-
We also provide `maxcut_tfim_sparse(G)`, for `scipy` CSR sparse arrays (or `networkx` graphs). The (integer) `quality` setting is optional, with a default value of `2`, but you can turn it up for higher-quality results, or turn it down to save time. (You can also optionally specify the number of measurement `shots` as an argument, if you want specific fine-grained control over resource usage.) `is_alt_gpu_sampling` is `False` by default and, if set to `True`, will dispatch almost the entire algorithm on GPU, instead of just the TFIM-heuristic component (although solution quality is often lower). If you want to run MAXCUT on a graph with non-uniform edge weights, specify them as the `weight` attribute of each edge, with `networkx`. (If any `weight` attribute is not defined, the solver assumes it's `1.0` for that edge.)
|
|
56
|
+
We also provide `maxcut_tfim_sparse(G)`, for `scipy` CSR sparse arrays (or `networkx` graphs). The (integer) `quality` setting is optional, with a default value of `2`, but you can turn it up for higher-quality results, or turn it down to save time. (You can also optionally specify the number of measurement `shots` as an argument, if you want specific fine-grained control over resource usage.) `is_base_maxcut_gpu` enables or disables GPU execution. `is_alt_gpu_sampling` is `False` by default and, if set to `True`, will dispatch almost the entire algorithm on GPU, instead of just the TFIM-heuristic component (although solution quality is often lower). If you want to run MAXCUT on a graph with non-uniform edge weights, specify them as the `weight` attribute of each edge, with `networkx`. (If any `weight` attribute is not defined, the solver assumes it's `1.0` for that edge.)
|
|
57
57
|
|
|
58
58
|
Based on a combination of the TFIM-inspired MAXCUT solver and another technique for finding ground-state energy in quantum chemistry that we call the _"binary Clifford eigensolver,"_ we also provide an (approximate) spin glass ground-state solver:
|
|
59
59
|
```py
|
|
@@ -73,10 +73,10 @@ def generate_spin_glass_graph(n_nodes=16, degree=3, seed=None):
|
|
|
73
73
|
|
|
74
74
|
|
|
75
75
|
G = generate_spin_glass_graph(n_nodes=64, seed=42)
|
|
76
|
-
solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=2, best_guess=None, is_alt_gpu_sampling=True)
|
|
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`. `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` **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 = "5.
|
|
15
|
+
version = "5.7.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"}
|
|
@@ -94,6 +94,15 @@ __kernel void maxcut_hamming_cdf(
|
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
+
inline uint xorshift32(uint *state) {
|
|
98
|
+
uint x = *state;
|
|
99
|
+
x ^= x << 13;
|
|
100
|
+
x ^= x >> 17;
|
|
101
|
+
x ^= x << 5;
|
|
102
|
+
*state = x;
|
|
103
|
+
return x;
|
|
104
|
+
}
|
|
105
|
+
|
|
97
106
|
float bootstrap_worker(__constant char* theta, __global double* G_m, __constant int* indices, const int k, const int n) {
|
|
98
107
|
double energy = 0.0;
|
|
99
108
|
const size_t n_st = (size_t)n;
|
|
@@ -123,6 +132,7 @@ float bootstrap_worker(__constant char* theta, __global double* G_m, __constant
|
|
|
123
132
|
}
|
|
124
133
|
|
|
125
134
|
__kernel void bootstrap(
|
|
135
|
+
uint prng_seed,
|
|
126
136
|
__global double* G_m,
|
|
127
137
|
__constant char* best_theta,
|
|
128
138
|
__constant int* indices_array,
|
|
@@ -137,6 +147,9 @@ __kernel void bootstrap(
|
|
|
137
147
|
const int combo_count = args[2];
|
|
138
148
|
const int i = get_global_id(0);
|
|
139
149
|
|
|
150
|
+
// The inputs are chaotic, and this doesn't need to be high-quality, just uniform.
|
|
151
|
+
prng_seed ^= (uint)i;
|
|
152
|
+
|
|
140
153
|
float energy = INFINITY;
|
|
141
154
|
|
|
142
155
|
if (i < combo_count) {
|
|
@@ -155,9 +168,15 @@ __kernel void bootstrap(
|
|
|
155
168
|
// Reduce within workgroup
|
|
156
169
|
for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
|
|
157
170
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
158
|
-
if (
|
|
159
|
-
|
|
160
|
-
|
|
171
|
+
if (lt_id < offset) {
|
|
172
|
+
float hid_energy = loc_energy[lt_id + offset];
|
|
173
|
+
float lid_energy = loc_energy[lt_id];
|
|
174
|
+
if (hid_energy < lid_energy) {
|
|
175
|
+
loc_energy[lt_id] = hid_energy;
|
|
176
|
+
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
177
|
+
} else if ((hid_energy == lid_energy) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
178
|
+
loc_index[lt_id] = hid_energy;
|
|
179
|
+
}
|
|
161
180
|
}
|
|
162
181
|
}
|
|
163
182
|
|
|
@@ -197,6 +216,7 @@ float bootstrap_worker_sparse(__constant char* theta, __global double* G_data, _
|
|
|
197
216
|
}
|
|
198
217
|
|
|
199
218
|
__kernel void bootstrap_sparse(
|
|
219
|
+
uint prng_seed,
|
|
200
220
|
__global double* G_data,
|
|
201
221
|
__global unsigned* G_rows,
|
|
202
222
|
__global unsigned* G_cols,
|
|
@@ -213,6 +233,8 @@ __kernel void bootstrap_sparse(
|
|
|
213
233
|
const int combo_count = args[2];
|
|
214
234
|
const int i = get_global_id(0);
|
|
215
235
|
|
|
236
|
+
prng_seed ^= (uint)i;
|
|
237
|
+
|
|
216
238
|
float energy = INFINITY;
|
|
217
239
|
|
|
218
240
|
if (i < combo_count) {
|
|
@@ -231,9 +253,15 @@ __kernel void bootstrap_sparse(
|
|
|
231
253
|
// Reduce within workgroup
|
|
232
254
|
for (int offset = lt_size >> 1; offset > 0; offset >>= 1) {
|
|
233
255
|
barrier(CLK_LOCAL_MEM_FENCE);
|
|
234
|
-
if (
|
|
235
|
-
|
|
236
|
-
|
|
256
|
+
if (lt_id < offset) {
|
|
257
|
+
float hid_energy = loc_energy[lt_id + offset];
|
|
258
|
+
float lid_energy = loc_energy[lt_id];
|
|
259
|
+
if (hid_energy < lid_energy) {
|
|
260
|
+
loc_energy[lt_id] = hid_energy;
|
|
261
|
+
loc_index[lt_id] = loc_index[lt_id + offset];
|
|
262
|
+
} else if ((hid_energy == lid_energy) && ((xorshift32(&prng_seed) >> 31) & 1)) {
|
|
263
|
+
loc_index[lt_id] = hid_energy;
|
|
264
|
+
}
|
|
237
265
|
}
|
|
238
266
|
}
|
|
239
267
|
|
|
@@ -246,15 +274,6 @@ __kernel void bootstrap_sparse(
|
|
|
246
274
|
|
|
247
275
|
/// ------ Alternative random sampling rejection kernel by Elara (the OpenAI custom GPT) is below ------ ///
|
|
248
276
|
|
|
249
|
-
inline uint xorshift32(uint *state) {
|
|
250
|
-
uint x = *state;
|
|
251
|
-
x ^= x << 13;
|
|
252
|
-
x ^= x >> 17;
|
|
253
|
-
x ^= x << 5;
|
|
254
|
-
*state = x;
|
|
255
|
-
return x;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
277
|
inline float rand_uniform(uint *state) {
|
|
259
278
|
return (float)xorshift32(state) / (float)0xFFFFFFFF;
|
|
260
279
|
}
|
|
@@ -84,10 +84,9 @@ def compute_energy(sample, G_m, n_qubits):
|
|
|
84
84
|
|
|
85
85
|
|
|
86
86
|
@njit(parallel=True)
|
|
87
|
-
def sample_for_solution(G_m, shots, thresholds,
|
|
87
|
+
def sample_for_solution(G_m, shots, thresholds, weights):
|
|
88
88
|
n = len(G_m)
|
|
89
89
|
max_weight = G_m.max()
|
|
90
|
-
weights = (1.0 / (1.0 + (2e-52) - J_eff)).astype(np.float64)
|
|
91
90
|
|
|
92
91
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
93
92
|
energies = np.empty(shots, dtype=np.float32)
|
|
@@ -218,7 +217,12 @@ def cpu_footer(shots, quality, n_qubits, G_m, nodes):
|
|
|
218
217
|
|
|
219
218
|
maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, hamming_prob)
|
|
220
219
|
|
|
221
|
-
|
|
220
|
+
degrees = None
|
|
221
|
+
J_eff = 1.0 / (1.0 + (2e-52) - J_eff)
|
|
222
|
+
weights = J_eff.astype(np.float64)
|
|
223
|
+
J_eff = None
|
|
224
|
+
|
|
225
|
+
best_solution, best_value = sample_for_solution(G_m, shots, hamming_prob, weights)
|
|
222
226
|
|
|
223
227
|
bit_string, l, r = get_cut(best_solution, nodes)
|
|
224
228
|
|
|
@@ -226,10 +230,10 @@ def cpu_footer(shots, quality, n_qubits, G_m, nodes):
|
|
|
226
230
|
|
|
227
231
|
|
|
228
232
|
@njit
|
|
229
|
-
def gpu_footer(shots, n_qubits, G_m,
|
|
233
|
+
def gpu_footer(shots, n_qubits, G_m, weights, hamming_prob, nodes):
|
|
230
234
|
fix_cdf(hamming_prob)
|
|
231
235
|
|
|
232
|
-
best_solution, best_value = sample_for_solution(G_m, shots, hamming_prob,
|
|
236
|
+
best_solution, best_value = sample_for_solution(G_m, shots, hamming_prob, weights)
|
|
233
237
|
|
|
234
238
|
bit_string, l, r = get_cut(best_solution, nodes)
|
|
235
239
|
|
|
@@ -240,8 +244,9 @@ def maxcut_tfim(
|
|
|
240
244
|
G,
|
|
241
245
|
quality=None,
|
|
242
246
|
shots=None,
|
|
243
|
-
is_alt_gpu_sampling
|
|
244
|
-
is_g_buf_reused
|
|
247
|
+
is_alt_gpu_sampling=False,
|
|
248
|
+
is_g_buf_reused=False,
|
|
249
|
+
is_base_maxcut_gpu=True
|
|
245
250
|
):
|
|
246
251
|
nodes = None
|
|
247
252
|
n_qubits = 0
|
|
@@ -279,7 +284,7 @@ def maxcut_tfim(
|
|
|
279
284
|
n_steps = 2 << quality
|
|
280
285
|
grid_size = n_steps * n_qubits
|
|
281
286
|
|
|
282
|
-
if not (IS_OPENCL_AVAILABLE and grid_size >= 128):
|
|
287
|
+
if (not is_base_maxcut_gpu) or not (IS_OPENCL_AVAILABLE and grid_size >= 128):
|
|
283
288
|
return cpu_footer(shots, quality, n_qubits, G_m, nodes)
|
|
284
289
|
|
|
285
290
|
J_eff, degrees = init_J_and_z(G_m)
|
|
@@ -341,7 +346,12 @@ def maxcut_tfim(
|
|
|
341
346
|
theta_buf = None
|
|
342
347
|
|
|
343
348
|
if not is_alt_gpu_sampling:
|
|
344
|
-
|
|
349
|
+
degrees = None
|
|
350
|
+
J_eff = 1.0 / (1.0 + (2e-52) - J_eff)
|
|
351
|
+
weights = J_eff.astype(np.float64)
|
|
352
|
+
J_eff = None
|
|
353
|
+
|
|
354
|
+
return gpu_footer(shots, n_qubits, G_m, weights, hamming_prob, nodes)
|
|
345
355
|
|
|
346
356
|
fix_cdf(hamming_prob)
|
|
347
357
|
best_solution, best_value = run_sampling_opencl(G_m, hamming_prob, shots, n_qubits, is_g_buf_reused)
|
|
@@ -116,10 +116,9 @@ def compute_energy(sample, G_data, G_rows, G_cols):
|
|
|
116
116
|
|
|
117
117
|
|
|
118
118
|
@njit(parallel=True)
|
|
119
|
-
def sample_for_solution(G_data, G_rows, G_cols, shots, thresholds,
|
|
119
|
+
def sample_for_solution(G_data, G_rows, G_cols, shots, thresholds, weights):
|
|
120
120
|
n = G_rows.shape[0] - 1
|
|
121
121
|
max_weight = G_data.max()
|
|
122
|
-
weights = (1.0 / (1.0 + (2e-52) - J_eff)).astype(np.float64)
|
|
123
122
|
|
|
124
123
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
125
124
|
energies = np.empty(shots, dtype=np.float32)
|
|
@@ -271,7 +270,12 @@ def cpu_footer(shots, quality, n_qubits, G_data, G_rows, G_cols, nodes):
|
|
|
271
270
|
|
|
272
271
|
maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, hamming_prob)
|
|
273
272
|
|
|
274
|
-
|
|
273
|
+
degrees = None
|
|
274
|
+
J_eff = 1.0 / (1.0 + (2e-52) - J_eff)
|
|
275
|
+
weights = J_eff.astype(np.float64)
|
|
276
|
+
J_eff = None
|
|
277
|
+
|
|
278
|
+
best_solution, best_value = sample_for_solution(G_data, G_rows, G_cols, shots, hamming_prob, weights)
|
|
275
279
|
|
|
276
280
|
bit_string, l, r = get_cut(best_solution, nodes)
|
|
277
281
|
|
|
@@ -279,10 +283,10 @@ def cpu_footer(shots, quality, n_qubits, G_data, G_rows, G_cols, nodes):
|
|
|
279
283
|
|
|
280
284
|
|
|
281
285
|
@njit
|
|
282
|
-
def gpu_footer(shots, n_qubits, G_data, G_rows, G_cols,
|
|
286
|
+
def gpu_footer(shots, n_qubits, G_data, G_rows, G_cols, weights, hamming_prob, nodes):
|
|
283
287
|
fix_cdf(hamming_prob)
|
|
284
288
|
|
|
285
|
-
best_solution, best_value = sample_for_solution(G_data, G_rows, G_cols, shots, hamming_prob,
|
|
289
|
+
best_solution, best_value = sample_for_solution(G_data, G_rows, G_cols, shots, hamming_prob, weights)
|
|
286
290
|
|
|
287
291
|
bit_string, l, r = get_cut(best_solution, nodes)
|
|
288
292
|
|
|
@@ -305,8 +309,9 @@ def maxcut_tfim_sparse(
|
|
|
305
309
|
G,
|
|
306
310
|
quality=None,
|
|
307
311
|
shots=None,
|
|
308
|
-
is_alt_gpu_sampling
|
|
309
|
-
is_g_buf_reused
|
|
312
|
+
is_alt_gpu_sampling=False,
|
|
313
|
+
is_g_buf_reused=False,
|
|
314
|
+
is_base_maxcut_gpu=True
|
|
310
315
|
):
|
|
311
316
|
nodes = None
|
|
312
317
|
n_qubits = 0
|
|
@@ -344,7 +349,7 @@ def maxcut_tfim_sparse(
|
|
|
344
349
|
n_steps = 2 << quality
|
|
345
350
|
grid_size = n_steps * n_qubits
|
|
346
351
|
|
|
347
|
-
if not (IS_OPENCL_AVAILABLE and grid_size >= 128):
|
|
352
|
+
if (not is_base_maxcut_gpu) or not (IS_OPENCL_AVAILABLE and grid_size >= 128):
|
|
348
353
|
return cpu_footer(shots, quality, n_qubits, G_m.data, G_m.indptr, G_m.indices, nodes)
|
|
349
354
|
|
|
350
355
|
J_eff, degrees = init_J_and_z(G_m.data, G_m.indptr, G_m.indices)
|
|
@@ -406,7 +411,12 @@ def maxcut_tfim_sparse(
|
|
|
406
411
|
theta_buf = None
|
|
407
412
|
|
|
408
413
|
if not is_alt_gpu_sampling:
|
|
409
|
-
|
|
414
|
+
degrees = None
|
|
415
|
+
J_eff = 1.0 / (1.0 + (2e-52) - J_eff)
|
|
416
|
+
weights = J_eff.astype(np.float64)
|
|
417
|
+
J_eff = None
|
|
418
|
+
|
|
419
|
+
return gpu_footer(shots, n_qubits, G_m.data, G_m.indptr, G_m.indices, weights, hamming_prob, nodes)
|
|
410
420
|
|
|
411
421
|
fix_cdf(hamming_prob)
|
|
412
422
|
best_solution, best_value = run_sampling_opencl(G_m, hamming_prob, shots, n_qubits, is_g_buf_reused)
|
|
@@ -87,9 +87,7 @@ def compute_energy(sample, G_func, G_func_args_tuple, nodes, n_qubits):
|
|
|
87
87
|
|
|
88
88
|
|
|
89
89
|
@njit(parallel=True)
|
|
90
|
-
def sample_for_solution(G_func, G_func_args_tuple, nodes, max_weight, shots, thresholds, degrees_sum,
|
|
91
|
-
weights = (1.0 / (1.0 + (2e-52) - J_eff)).astype(np.float64)
|
|
92
|
-
|
|
90
|
+
def sample_for_solution(G_func, G_func_args_tuple, nodes, max_weight, shots, thresholds, degrees_sum, weights, n):
|
|
93
91
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
94
92
|
energies = np.empty(shots, dtype=np.float32)
|
|
95
93
|
|
|
@@ -151,7 +149,13 @@ def cpu_footer(shots, quality, n_qubits, G_func, G_func_args_tuple, nodes):
|
|
|
151
149
|
|
|
152
150
|
maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, hamming_prob)
|
|
153
151
|
|
|
154
|
-
|
|
152
|
+
max_weight = degrees.sum()
|
|
153
|
+
degrees = None
|
|
154
|
+
J_eff = 1.0 / (1.0 + (2e-52) - J_eff)
|
|
155
|
+
weights = J_eff.astype(np.float64)
|
|
156
|
+
J_eff = None
|
|
157
|
+
|
|
158
|
+
best_solution, best_value = sample_for_solution(G_func, G_func_args_tuple, nodes, G_max, shots, hamming_prob, max_weight, weights, n_qubits)
|
|
155
159
|
|
|
156
160
|
bit_string, l, r = get_cut(best_solution, nodes)
|
|
157
161
|
|
|
@@ -159,10 +163,10 @@ def cpu_footer(shots, quality, n_qubits, G_func, G_func_args_tuple, nodes):
|
|
|
159
163
|
|
|
160
164
|
|
|
161
165
|
@njit
|
|
162
|
-
def gpu_footer(shots, n_qubits, G_func, G_func_args_tuple, nodes, G_max,
|
|
166
|
+
def gpu_footer(shots, n_qubits, G_func, G_func_args_tuple, nodes, G_max, weights, degrees, hamming_prob, max_weight):
|
|
163
167
|
fix_cdf(hamming_prob)
|
|
164
168
|
|
|
165
|
-
best_solution, best_value = sample_for_solution(G_func, G_func_args_tuple, nodes, G_max, shots, hamming_prob,
|
|
169
|
+
best_solution, best_value = sample_for_solution(G_func, G_func_args_tuple, nodes, G_max, shots, hamming_prob, max_weight, weights, n_qubits)
|
|
166
170
|
|
|
167
171
|
bit_string, l, r = get_cut(best_solution, nodes)
|
|
168
172
|
|
|
@@ -175,6 +179,7 @@ def maxcut_tfim_streaming(
|
|
|
175
179
|
G_func_args_tuple=None,
|
|
176
180
|
quality=None,
|
|
177
181
|
shots=None,
|
|
182
|
+
is_base_maxcut_gpu=True
|
|
178
183
|
):
|
|
179
184
|
n_qubits = len(nodes)
|
|
180
185
|
|
|
@@ -202,7 +207,7 @@ def maxcut_tfim_streaming(
|
|
|
202
207
|
n_steps = 2 << quality
|
|
203
208
|
grid_size = n_steps * n_qubits
|
|
204
209
|
|
|
205
|
-
if not (IS_OPENCL_AVAILABLE and grid_size >= 128):
|
|
210
|
+
if (not is_base_maxcut_gpu) or not (IS_OPENCL_AVAILABLE and grid_size >= 128):
|
|
206
211
|
return cpu_footer(shots, quality, n_qubits, G_func, G_func_args_tuple, nodes)
|
|
207
212
|
|
|
208
213
|
J_eff, degrees, G_max = init_J_and_z(G_func, G_func_args_tuple, nodes)
|
|
@@ -263,4 +268,10 @@ def maxcut_tfim_streaming(
|
|
|
263
268
|
deg_buf = None
|
|
264
269
|
theta_buf = None
|
|
265
270
|
|
|
266
|
-
|
|
271
|
+
max_weight = degrees.sum()
|
|
272
|
+
degrees = None
|
|
273
|
+
J_eff = 1.0 / (1.0 + (2e-52) - J_eff)
|
|
274
|
+
weights = J_eff.astype(np.float64)
|
|
275
|
+
J_eff = None
|
|
276
|
+
|
|
277
|
+
return gpu_footer(shots, n_qubits, G_func, G_func_args_tuple, nodes, G_max, weights, degrees, hamming_prob, max_weight)
|
|
@@ -101,6 +101,7 @@ def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy):
|
|
|
101
101
|
|
|
102
102
|
# Set kernel args
|
|
103
103
|
bootstrap_kernel.set_args(
|
|
104
|
+
np.random.randint(1<<32, dtype='uint32'),
|
|
104
105
|
G_m_buf,
|
|
105
106
|
best_theta_buf,
|
|
106
107
|
indices_buf,
|
|
@@ -119,12 +120,11 @@ def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy):
|
|
|
119
120
|
queue.finish()
|
|
120
121
|
|
|
121
122
|
# Find global minimum
|
|
122
|
-
|
|
123
|
-
energy = min_energy_host[min_idx]
|
|
123
|
+
energy = min_energy_host.min()
|
|
124
124
|
if min_energy < energy:
|
|
125
125
|
return min_energy
|
|
126
126
|
|
|
127
|
-
best_i =
|
|
127
|
+
best_i = np.random.choice(np.where(min_energy_host == energy)[0])
|
|
128
128
|
|
|
129
129
|
flip_index_start = best_i * k
|
|
130
130
|
indices_to_flip = indices_array_np[flip_index_start : flip_index_start + k]
|
|
@@ -140,7 +140,9 @@ def spin_glass_solver(
|
|
|
140
140
|
quality=None,
|
|
141
141
|
shots=None,
|
|
142
142
|
best_guess=None,
|
|
143
|
-
is_alt_gpu_sampling=False
|
|
143
|
+
is_alt_gpu_sampling=False,
|
|
144
|
+
is_base_maxcut_gpu=True,
|
|
145
|
+
is_combo_maxcut_gpu=True
|
|
144
146
|
):
|
|
145
147
|
nodes = None
|
|
146
148
|
n_qubits = 0
|
|
@@ -179,10 +181,10 @@ def spin_glass_solver(
|
|
|
179
181
|
elif isinstance(best_guess, list):
|
|
180
182
|
bitstring = "".join(["1" if b else "0" for b in best_guess])
|
|
181
183
|
else:
|
|
182
|
-
bitstring, _, _ = maxcut_tfim(G_m, quality=quality, shots=shots, is_alt_gpu_sampling=is_alt_gpu_sampling, is_g_buf_reused=True)
|
|
184
|
+
bitstring, _, _ = maxcut_tfim(G_m, quality=quality, shots=shots, is_alt_gpu_sampling=is_alt_gpu_sampling, is_g_buf_reused=True, is_base_maxcut_gpu=is_base_maxcut_gpu)
|
|
183
185
|
best_theta = np.array([b == "1" for b in list(bitstring)], dtype=np.bool_)
|
|
184
186
|
|
|
185
|
-
if IS_OPENCL_AVAILABLE:
|
|
187
|
+
if is_combo_maxcut_gpu and IS_OPENCL_AVAILABLE:
|
|
186
188
|
if not (opencl_context.G_m_buf is None):
|
|
187
189
|
G_m_buf = opencl_context.G_m_buf
|
|
188
190
|
else:
|
|
@@ -210,7 +212,7 @@ def spin_glass_solver(
|
|
|
210
212
|
else:
|
|
211
213
|
combos = combos_list[k - 1]
|
|
212
214
|
|
|
213
|
-
if IS_OPENCL_AVAILABLE:
|
|
215
|
+
if is_combo_maxcut_gpu and IS_OPENCL_AVAILABLE:
|
|
214
216
|
energy = run_bootstrap_opencl(best_theta, G_m_buf, combos, k, min_energy)
|
|
215
217
|
else:
|
|
216
218
|
energy = bootstrap(best_theta, G_m, combos, k, min_energy)
|
|
@@ -104,6 +104,7 @@ def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices
|
|
|
104
104
|
|
|
105
105
|
# Set kernel args
|
|
106
106
|
bootstrap_kernel.set_args(
|
|
107
|
+
np.random.randint(1<<32, dtype='uint32'),
|
|
107
108
|
G_data_buf,
|
|
108
109
|
G_rows_buf,
|
|
109
110
|
G_cols_buf,
|
|
@@ -124,12 +125,11 @@ def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices
|
|
|
124
125
|
queue.finish()
|
|
125
126
|
|
|
126
127
|
# Find global minimum
|
|
127
|
-
|
|
128
|
-
energy = min_energy_host[min_idx]
|
|
128
|
+
energy = min_energy_host.min()
|
|
129
129
|
if min_energy < energy:
|
|
130
130
|
return min_energy
|
|
131
131
|
|
|
132
|
-
best_i =
|
|
132
|
+
best_i = np.random.choice(np.where(min_energy_host == energy)[0])
|
|
133
133
|
|
|
134
134
|
flip_index_start = best_i * k
|
|
135
135
|
indices_to_flip = indices_array_np[flip_index_start : flip_index_start + k]
|
|
@@ -157,7 +157,9 @@ def spin_glass_solver_sparse(
|
|
|
157
157
|
quality=None,
|
|
158
158
|
shots=None,
|
|
159
159
|
best_guess=None,
|
|
160
|
-
is_alt_gpu_sampling=False
|
|
160
|
+
is_alt_gpu_sampling=False,
|
|
161
|
+
is_base_maxcut_gpu=True,
|
|
162
|
+
is_combo_maxcut_gpu=True
|
|
161
163
|
):
|
|
162
164
|
nodes = None
|
|
163
165
|
n_qubits = 0
|
|
@@ -196,10 +198,10 @@ def spin_glass_solver_sparse(
|
|
|
196
198
|
elif isinstance(best_guess, list):
|
|
197
199
|
bitstring = "".join(["1" if b else "0" for b in best_guess])
|
|
198
200
|
else:
|
|
199
|
-
bitstring, _, _ = maxcut_tfim_sparse(G_m, quality=quality, shots=shots, is_alt_gpu_sampling=is_alt_gpu_sampling, is_g_buf_reused=True)
|
|
201
|
+
bitstring, _, _ = maxcut_tfim_sparse(G_m, quality=quality, shots=shots, is_alt_gpu_sampling=is_alt_gpu_sampling, is_g_buf_reused=True, is_base_maxcut_gpu=is_base_maxcut_gpu)
|
|
200
202
|
best_theta = np.array([b == "1" for b in list(bitstring)], dtype=np.bool_)
|
|
201
203
|
|
|
202
|
-
if IS_OPENCL_AVAILABLE:
|
|
204
|
+
if is_combo_maxcut_gpu and IS_OPENCL_AVAILABLE:
|
|
203
205
|
if not (opencl_context.G_data_buf is None):
|
|
204
206
|
G_data_buf = opencl_context.G_data_buf
|
|
205
207
|
G_rows_buf = opencl_context.G_rows_buf
|
|
@@ -231,7 +233,7 @@ def spin_glass_solver_sparse(
|
|
|
231
233
|
else:
|
|
232
234
|
combos = combos_list[k - 1]
|
|
233
235
|
|
|
234
|
-
if IS_OPENCL_AVAILABLE:
|
|
236
|
+
if is_combo_maxcut_gpu and IS_OPENCL_AVAILABLE:
|
|
235
237
|
energy = run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, combos, k, min_energy)
|
|
236
238
|
else:
|
|
237
239
|
energy = bootstrap(best_theta, G_m.data, G_m.indptr, G_m.indices, combos, k, min_energy)
|
|
@@ -66,7 +66,8 @@ def spin_glass_solver_streaming(
|
|
|
66
66
|
G_func_args_tuple=None,
|
|
67
67
|
quality=None,
|
|
68
68
|
shots=None,
|
|
69
|
-
best_guess=None
|
|
69
|
+
best_guess=None,
|
|
70
|
+
is_base_maxcut_gpu=True
|
|
70
71
|
):
|
|
71
72
|
n_qubits = len(nodes)
|
|
72
73
|
|
|
@@ -95,7 +96,7 @@ def spin_glass_solver_streaming(
|
|
|
95
96
|
elif isinstance(best_guess, list):
|
|
96
97
|
bitstring = "".join(["1" if b else "0" for b in best_guess])
|
|
97
98
|
else:
|
|
98
|
-
bitstring, _, _ = maxcut_tfim_streaming(G_func, nodes, G_func_args_tuple=G_func_args_tuple, quality=quality, shots=shots)
|
|
99
|
+
bitstring, _, _ = maxcut_tfim_streaming(G_func, nodes, G_func_args_tuple=G_func_args_tuple, quality=quality, shots=shots, is_base_maxcut_gpu=is_base_maxcut_gpu)
|
|
99
100
|
best_theta = np.array([b == "1" for b in list(bitstring)], dtype=np.bool_)
|
|
100
101
|
|
|
101
102
|
min_energy = compute_energy(best_theta, G_func, G_func_args_tuple, nodes)
|
|
@@ -497,7 +497,21 @@ def tsp_symmetric_driver(G_m, is_cyclic, is_top_level, start_node, end_node, k_n
|
|
|
497
497
|
return [nodes[x] for x in best_path], best_weight
|
|
498
498
|
|
|
499
499
|
|
|
500
|
-
def tsp_symmetric(
|
|
500
|
+
def tsp_symmetric(
|
|
501
|
+
G,
|
|
502
|
+
start_node=None,
|
|
503
|
+
end_node=None,
|
|
504
|
+
quality=None,
|
|
505
|
+
shots=None,
|
|
506
|
+
is_alt_gpu_sampling=False,
|
|
507
|
+
is_base_maxcut_gpu=True,
|
|
508
|
+
is_combo_maxcut_gpu=True,
|
|
509
|
+
monte_carlo=True,
|
|
510
|
+
k_neighbors=20,
|
|
511
|
+
is_cyclic=True,
|
|
512
|
+
multi_start=1,
|
|
513
|
+
is_top_level=True
|
|
514
|
+
):
|
|
501
515
|
nodes = None
|
|
502
516
|
n_nodes = 0
|
|
503
517
|
G_m = None
|
|
@@ -540,7 +554,7 @@ def tsp_symmetric(G, start_node=None, end_node=None, quality=None, shots=None, i
|
|
|
540
554
|
for _ in range(multi_start):
|
|
541
555
|
bits = ([], [])
|
|
542
556
|
while (len(bits[0]) == 0) or (len(bits[1]) == 0):
|
|
543
|
-
_, _, bits, energy = spin_glass_solver(G_m, quality=quality, shots=shots, is_alt_gpu_sampling=is_alt_gpu_sampling)
|
|
557
|
+
_, _, bits, energy = spin_glass_solver(G_m, quality=quality, shots=shots, is_alt_gpu_sampling=is_alt_gpu_sampling, is_base_maxcut_gpu=is_base_maxcut_gpu, is_combo_maxcut_gpu=is_combo_maxcut_gpu)
|
|
544
558
|
if energy < best_energy:
|
|
545
559
|
best_energy = energy
|
|
546
560
|
a, b = bits
|
|
@@ -555,8 +569,32 @@ def tsp_symmetric(G, start_node=None, end_node=None, quality=None, shots=None, i
|
|
|
555
569
|
|
|
556
570
|
G_a, G_b = init_G_a_b(G_m, a, b)
|
|
557
571
|
|
|
558
|
-
sol_a = tsp_symmetric(
|
|
559
|
-
|
|
572
|
+
sol_a = tsp_symmetric(
|
|
573
|
+
G_a,
|
|
574
|
+
quality=quality,
|
|
575
|
+
shots=shots,
|
|
576
|
+
is_alt_gpu_sampling=is_alt_gpu_sampling,
|
|
577
|
+
is_base_maxcut_gpu=is_base_maxcut_gpu,
|
|
578
|
+
is_combo_maxcut_gpu=is_combo_maxcut_gpu,
|
|
579
|
+
monte_carlo=monte_carlo,
|
|
580
|
+
is_cyclic=False,
|
|
581
|
+
is_top_level=False,
|
|
582
|
+
k_neighbors=0,
|
|
583
|
+
multi_start=multi_start
|
|
584
|
+
)
|
|
585
|
+
sol_b = tsp_symmetric(
|
|
586
|
+
G_b,
|
|
587
|
+
quality=quality,
|
|
588
|
+
shots=shots,
|
|
589
|
+
is_alt_gpu_sampling=is_alt_gpu_sampling,
|
|
590
|
+
is_base_maxcut_gpu=is_base_maxcut_gpu,
|
|
591
|
+
is_combo_maxcut_gpu=is_combo_maxcut_gpu,
|
|
592
|
+
monte_carlo=monte_carlo,
|
|
593
|
+
is_cyclic=False,
|
|
594
|
+
is_top_level=False,
|
|
595
|
+
k_neighbors=0,
|
|
596
|
+
multi_start=multi_start
|
|
597
|
+
)
|
|
560
598
|
|
|
561
599
|
return tsp_symmetric_driver(G_m, is_cyclic, is_top_level, start_node, end_node, k_neighbors, nodes, sol_a, sol_b, a, b, c[0] if len(c) else None)
|
|
562
600
|
|
|
@@ -643,7 +681,21 @@ def tsp_asymmetric_driver(G_m, is_reversed, is_cyclic, is_top_level, start_node,
|
|
|
643
681
|
return [nodes[x] for x in final_path], best_weight
|
|
644
682
|
|
|
645
683
|
|
|
646
|
-
def tsp_asymmetric(
|
|
684
|
+
def tsp_asymmetric(
|
|
685
|
+
G,
|
|
686
|
+
start_node=None,
|
|
687
|
+
end_node=None,
|
|
688
|
+
quality=None,
|
|
689
|
+
shots=None,
|
|
690
|
+
is_alt_gpu_sampling=False,
|
|
691
|
+
is_base_maxcut_gpu=True,
|
|
692
|
+
is_combo_maxcut_gpu=True,
|
|
693
|
+
monte_carlo=True,
|
|
694
|
+
k_neighbors=20,
|
|
695
|
+
is_cyclic=True,
|
|
696
|
+
multi_start=1,
|
|
697
|
+
is_top_level=True
|
|
698
|
+
):
|
|
647
699
|
nodes = None
|
|
648
700
|
n_nodes = 0
|
|
649
701
|
G_m = None
|
|
@@ -690,7 +742,7 @@ def tsp_asymmetric(G, start_node=None, end_node=None, quality=None, shots=None,
|
|
|
690
742
|
for _ in range(multi_start):
|
|
691
743
|
bits = ([], [])
|
|
692
744
|
while (len(bits[0]) == 0) or (len(bits[1]) == 0):
|
|
693
|
-
_, _, bits, energy = spin_glass_solver((G_m + G_m.T) / 2, quality=quality, shots=shots, is_alt_gpu_sampling=is_alt_gpu_sampling)
|
|
745
|
+
_, _, bits, energy = spin_glass_solver((G_m + G_m.T) / 2, quality=quality, shots=shots, is_alt_gpu_sampling=is_alt_gpu_sampling, is_base_maxcut_gpu=is_base_maxcut_gpu, is_combo_maxcut_gpu=is_combo_maxcut_gpu)
|
|
694
746
|
if energy < best_energy:
|
|
695
747
|
best_energy = energy
|
|
696
748
|
a, b = bits
|
|
@@ -705,7 +757,31 @@ def tsp_asymmetric(G, start_node=None, end_node=None, quality=None, shots=None,
|
|
|
705
757
|
|
|
706
758
|
G_a, G_b = init_G_a_b(G_m, a, b)
|
|
707
759
|
|
|
708
|
-
sol_a = tsp_asymmetric(
|
|
709
|
-
|
|
760
|
+
sol_a = tsp_asymmetric(
|
|
761
|
+
G_a,
|
|
762
|
+
quality=quality,
|
|
763
|
+
shots=shots,
|
|
764
|
+
is_alt_gpu_sampling=is_alt_gpu_sampling,
|
|
765
|
+
is_base_maxcut_gpu=is_base_maxcut_gpu,
|
|
766
|
+
is_combo_maxcut_gpu=is_combo_maxcut_gpu,
|
|
767
|
+
monte_carlo=monte_carlo,
|
|
768
|
+
is_cyclic=False,
|
|
769
|
+
is_top_level=False,
|
|
770
|
+
k_neighbors=0,
|
|
771
|
+
multi_start=multi_start
|
|
772
|
+
)
|
|
773
|
+
sol_b = tsp_asymmetric(
|
|
774
|
+
G_b,
|
|
775
|
+
quality=quality,
|
|
776
|
+
shots=shots,
|
|
777
|
+
is_alt_gpu_sampling=is_alt_gpu_sampling,
|
|
778
|
+
is_base_maxcut_gpu=is_base_maxcut_gpu,
|
|
779
|
+
is_combo_maxcut_gpu=is_combo_maxcut_gpu,
|
|
780
|
+
monte_carlo=monte_carlo,
|
|
781
|
+
is_cyclic=False,
|
|
782
|
+
is_top_level=False,
|
|
783
|
+
k_neighbors=0,
|
|
784
|
+
multi_start=multi_start
|
|
785
|
+
)
|
|
710
786
|
|
|
711
787
|
return tsp_asymmetric_driver(G_m, is_reversed, is_cyclic, is_top_level, start_node, end_node, k_neighbors, nodes, sol_a, sol_b, a, b, c[0] if len(c) else None)
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='pyqrackising',
|
|
54
|
-
version='5.
|
|
54
|
+
version='5.7.1',
|
|
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
|