PyQrackIsing 8.14.2__tar.gz → 8.14.3__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-8.14.2/pyqrackising.egg-info → pyqrackising-8.14.3}/PKG-INFO +1 -1
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/pyproject.toml +1 -1
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/pyqrackising/maxcut_tfim.py +34 -102
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/pyqrackising/maxcut_tfim_sparse.py +45 -31
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/pyqrackising/maxcut_tfim_util.py +30 -0
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/pyqrackising/tsp.py +7 -23
- {pyqrackising-8.14.2 → pyqrackising-8.14.3/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/setup.py +1 -1
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/LICENSE.md +0 -0
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/MANIFEST.in +0 -0
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/README.md +0 -0
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/pyqrackising/__init__.py +0 -0
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/pyqrackising/maxcut_tfim_streaming.py +0 -0
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/pyqrackising/spin_glass_solver.py +0 -0
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/pyqrackising/spin_glass_solver_sparse.py +0 -0
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/pyqrackising/spin_glass_solver_streaming.py +0 -0
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/pyqrackising/spin_glass_solver_util.py +0 -0
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/pyqrackising/tsp_maxcut.py +0 -0
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-8.14.2 → pyqrackising-8.14.3}/setup.cfg +0 -0
|
@@ -10,7 +10,7 @@ build-backend = "setuptools.build_meta"
|
|
|
10
10
|
|
|
11
11
|
[project]
|
|
12
12
|
name = "pyqrackising"
|
|
13
|
-
version = "8.14.
|
|
13
|
+
version = "8.14.3"
|
|
14
14
|
requires-python = ">=3.8"
|
|
15
15
|
description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
|
|
16
16
|
readme = {file = "README.txt", content-type = "text/markdown"}
|
|
@@ -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 convert_bool_to_uint, get_cut, get_cut_base, make_G_m_buf, make_theta_buf, maxcut_hamming_cdf, opencl_context, sample_mag, bit_pick, init_bit_pick
|
|
7
|
+
from .maxcut_tfim_util import convert_bool_to_uint, get_cut, get_cut_base, make_G_m_buf, make_theta_buf, maxcut_hamming_cdf, opencl_context, sample_mag, setup_cut_opencl, bit_pick, init_bit_pick
|
|
8
8
|
|
|
9
9
|
IS_OPENCL_AVAILABLE = True
|
|
10
10
|
try:
|
|
@@ -96,21 +96,27 @@ def sample_measurement(G_m, max_edge, shots, thresholds, weights, repulsion_base
|
|
|
96
96
|
|
|
97
97
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
98
98
|
energies = np.empty(shots, dtype=dtype)
|
|
99
|
-
|
|
99
|
+
|
|
100
100
|
best_solution = solutions[0]
|
|
101
101
|
best_energy = -float("inf")
|
|
102
102
|
|
|
103
103
|
improved = True
|
|
104
104
|
while improved:
|
|
105
105
|
improved = False
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
106
|
+
if is_spin_glass:
|
|
107
|
+
for s in prange(shots):
|
|
108
|
+
# First dimension: Hamming weight
|
|
109
|
+
m = sample_mag(thresholds)
|
|
110
|
+
# Second dimension: permutation within Hamming weight
|
|
111
|
+
solutions[s] = local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, repulsion_base, n, m)
|
|
112
|
+
energies[s] = compute_energy(solutions[s], G_m, n)
|
|
113
|
+
else:
|
|
114
|
+
for s in prange(shots):
|
|
115
|
+
# First dimension: Hamming weight
|
|
116
|
+
m = sample_mag(thresholds)
|
|
117
|
+
# Second dimension: permutation within Hamming weight
|
|
118
|
+
solutions[s] = local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, repulsion_base, n, m)
|
|
119
|
+
energies[s] = compute_cut(solutions[s], G_m, n)
|
|
114
120
|
|
|
115
121
|
best_index = np.argmax(energies)
|
|
116
122
|
energy = energies[best_index]
|
|
@@ -120,7 +126,7 @@ def sample_measurement(G_m, max_edge, shots, thresholds, weights, repulsion_base
|
|
|
120
126
|
improved = True
|
|
121
127
|
|
|
122
128
|
if is_spin_glass:
|
|
123
|
-
best_energy = compute_cut(best_solution, G_m, n)
|
|
129
|
+
best_energy = compute_cut(best_solution, G_m, n)
|
|
124
130
|
|
|
125
131
|
return best_solution, best_energy
|
|
126
132
|
|
|
@@ -140,16 +146,17 @@ def sample_for_opencl(G_m, G_m_buf, max_edge, shots, thresholds, weights, repuls
|
|
|
140
146
|
tot_init_weight = weights.sum()
|
|
141
147
|
|
|
142
148
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
143
|
-
energies = np.empty(shots, dtype=dtype)
|
|
144
149
|
|
|
145
150
|
best_solution = solutions[0]
|
|
146
151
|
best_energy = -float("inf")
|
|
147
152
|
|
|
153
|
+
opencl_args = setup_cut_opencl(shots, n, segment_size, is_spin_glass)
|
|
154
|
+
|
|
148
155
|
improved = True
|
|
149
156
|
while improved:
|
|
150
157
|
improved = False
|
|
151
158
|
shot_loop(G_m, max_edge, thresholds, weights, tot_init_weight, repulsion_base, n, shots, solutions)
|
|
152
|
-
solution, energy = run_cut_opencl(solutions, G_m_buf, is_segmented, segment_size, is_spin_glass)
|
|
159
|
+
solution, energy = run_cut_opencl(solutions, G_m_buf, is_segmented, segment_size, is_spin_glass, *opencl_args)
|
|
153
160
|
if energy > best_energy:
|
|
154
161
|
best_energy = energy
|
|
155
162
|
best_solution = solution
|
|
@@ -192,7 +199,6 @@ def init_J_and_z(G_m):
|
|
|
192
199
|
return J_eff, degrees, G_max
|
|
193
200
|
|
|
194
201
|
|
|
195
|
-
@njit
|
|
196
202
|
def cpu_footer(shots, quality, n_qubits, G_m, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
|
|
197
203
|
J_eff, degrees, max_edge = init_J_and_z(G_m)
|
|
198
204
|
hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
|
|
@@ -207,39 +213,17 @@ def cpu_footer(shots, quality, n_qubits, G_m, nodes, is_spin_glass, anneal_t, an
|
|
|
207
213
|
return bit_string, best_value, (l, r)
|
|
208
214
|
|
|
209
215
|
|
|
210
|
-
def run_cut_opencl(samples, G_m_buf, is_segmented, segment_size, is_spin_glass):
|
|
211
|
-
ctx = opencl_context.ctx
|
|
216
|
+
def run_cut_opencl(samples, G_m_buf, is_segmented, segment_size, is_spin_glass, local_size, global_size, args_buf, local_energy_buf, local_index_buf, max_energy_host, max_index_host, max_energy_buf, max_index_buf):
|
|
212
217
|
queue = opencl_context.queue
|
|
213
218
|
calculate_cut_kernel = opencl_context.calculate_cut_segmented_kernel if is_segmented else opencl_context.calculate_cut_kernel
|
|
214
|
-
dtype = opencl_context.dtype
|
|
215
|
-
epsilon = opencl_context.epsilon
|
|
216
|
-
wgs = opencl_context.work_group_size
|
|
217
|
-
|
|
218
|
-
shots = samples.shape[1]
|
|
219
219
|
|
|
220
|
-
|
|
221
|
-
n = samples.shape[
|
|
222
|
-
args_np = np.array([shots, n, is_spin_glass, segment_size], dtype=np.int32)
|
|
220
|
+
shots = samples.shape[0]
|
|
221
|
+
n = samples.shape[1]
|
|
223
222
|
|
|
224
223
|
# Buffers
|
|
225
224
|
mf = cl.mem_flags
|
|
226
|
-
args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args_np)
|
|
227
225
|
theta_buf = make_theta_buf(convert_bool_to_uint(samples), is_segmented, shots, n)
|
|
228
226
|
|
|
229
|
-
# Local memory allocation (1 float per work item)
|
|
230
|
-
local_size = min(wgs, shots)
|
|
231
|
-
max_global_size = ((opencl_context.MAX_GPU_PROC_ELEM + local_size - 1) // local_size) * local_size # corresponds to MAX_PROC_ELEM macro in OpenCL kernel program
|
|
232
|
-
global_size = min(((shots + local_size - 1) // local_size) * local_size, max_global_size)
|
|
233
|
-
local_energy_buf = cl.LocalMemory(np.dtype(dtype).itemsize * local_size)
|
|
234
|
-
local_index_buf = cl.LocalMemory(np.dtype(np.int32).itemsize * local_size)
|
|
235
|
-
|
|
236
|
-
# Allocate max_energy and max_index result buffers per workgroup
|
|
237
|
-
max_energy_host = np.empty(global_size, dtype=dtype)
|
|
238
|
-
max_index_host = np.empty(global_size, dtype=np.int32)
|
|
239
|
-
|
|
240
|
-
max_energy_buf = cl.Buffer(ctx, mf.WRITE_ONLY, max_energy_host.nbytes)
|
|
241
|
-
max_index_buf = cl.Buffer(ctx, mf.WRITE_ONLY, max_index_host.nbytes)
|
|
242
|
-
|
|
243
227
|
# Set kernel args
|
|
244
228
|
if is_segmented:
|
|
245
229
|
calculate_cut_kernel.set_args(
|
|
@@ -280,64 +264,6 @@ def run_cut_opencl(samples, G_m_buf, is_segmented, segment_size, is_spin_glass):
|
|
|
280
264
|
|
|
281
265
|
return samples[best_i], max_energy_host[best_i]
|
|
282
266
|
|
|
283
|
-
|
|
284
|
-
@njit
|
|
285
|
-
def maxcut_tfim_pure_numba(
|
|
286
|
-
G_m,
|
|
287
|
-
nodes,
|
|
288
|
-
quality=None,
|
|
289
|
-
shots=None,
|
|
290
|
-
is_spin_glass=False,
|
|
291
|
-
anneal_t=None,
|
|
292
|
-
anneal_h=None,
|
|
293
|
-
repulsion_base=None
|
|
294
|
-
):
|
|
295
|
-
n_qubits = len(G_m)
|
|
296
|
-
|
|
297
|
-
if n_qubits < 3:
|
|
298
|
-
empty = [nodes[0]]
|
|
299
|
-
empty.clear()
|
|
300
|
-
|
|
301
|
-
if n_qubits == 0:
|
|
302
|
-
return "", 0, (empty, empty.copy())
|
|
303
|
-
|
|
304
|
-
if n_qubits == 1:
|
|
305
|
-
return "0", 0, (nodes, empty)
|
|
306
|
-
|
|
307
|
-
if n_qubits == 2:
|
|
308
|
-
weight = G_m[0, 1]
|
|
309
|
-
if weight < 0.0:
|
|
310
|
-
return "00", 0, (nodes, empty)
|
|
311
|
-
|
|
312
|
-
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
313
|
-
|
|
314
|
-
if quality is None:
|
|
315
|
-
quality = 6
|
|
316
|
-
|
|
317
|
-
if shots is None:
|
|
318
|
-
# Number of measurement shots
|
|
319
|
-
shots = n_qubits << quality
|
|
320
|
-
|
|
321
|
-
if anneal_t is None:
|
|
322
|
-
anneal_t = 8.0
|
|
323
|
-
|
|
324
|
-
if anneal_h is None:
|
|
325
|
-
anneal_h = 8.0
|
|
326
|
-
|
|
327
|
-
if repulsion_base is None:
|
|
328
|
-
repulsion_base = 8.0
|
|
329
|
-
|
|
330
|
-
bit_string, best_value, partition = cpu_footer(shots, quality, n_qubits, G_m, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base)
|
|
331
|
-
|
|
332
|
-
if best_value < 0.0:
|
|
333
|
-
# Best cut is trivial partition, all/empty
|
|
334
|
-
empty = [nodes[0]]
|
|
335
|
-
empty.clear()
|
|
336
|
-
|
|
337
|
-
return '0' * n_qubits, 0.0, (nodes, empty)
|
|
338
|
-
|
|
339
|
-
return bit_string, best_value, partition
|
|
340
|
-
|
|
341
267
|
def maxcut_tfim(
|
|
342
268
|
G,
|
|
343
269
|
quality=None,
|
|
@@ -360,11 +286,6 @@ def maxcut_tfim(
|
|
|
360
286
|
|
|
361
287
|
n_qubits = len(G_m)
|
|
362
288
|
|
|
363
|
-
is_opencl = is_maxcut_gpu and IS_OPENCL_AVAILABLE and (n_qubits >= max(32, opencl_context.work_group_size))
|
|
364
|
-
|
|
365
|
-
if not is_opencl:
|
|
366
|
-
return maxcut_tfim_pure_numba(G_m, nodes, quality, shots, is_spin_glass, anneal_t, anneal_h, repulsion_base)
|
|
367
|
-
|
|
368
289
|
if n_qubits < 3:
|
|
369
290
|
empty = [nodes[0]]
|
|
370
291
|
empty.clear()
|
|
@@ -398,6 +319,17 @@ def maxcut_tfim(
|
|
|
398
319
|
if repulsion_base is None:
|
|
399
320
|
repulsion_base = 8.0
|
|
400
321
|
|
|
322
|
+
is_opencl = is_maxcut_gpu and IS_OPENCL_AVAILABLE and (n_qubits >= max(32, opencl_context.work_group_size))
|
|
323
|
+
|
|
324
|
+
if not is_opencl:
|
|
325
|
+
bit_string, best_value, partition = cpu_footer(shots, quality, n_qubits, G_m, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base)
|
|
326
|
+
|
|
327
|
+
if best_value < 0.0:
|
|
328
|
+
# Best cut is trivial partition, all/empty
|
|
329
|
+
return '0' * n_qubits, 0.0, (nodes, [])
|
|
330
|
+
|
|
331
|
+
return bit_string, best_value, partition
|
|
332
|
+
|
|
401
333
|
segment_size = G_m.shape[0] ** 2
|
|
402
334
|
is_segmented = ((G_m.nbytes << 1) > opencl_context.max_alloc) or ((((((((n_qubits + 31) >> 5) << 5) * ((shots + 1) >> 1)) + 7) >> 3) << 1) > opencl_context.max_alloc)
|
|
403
335
|
|
|
@@ -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 binary_search, convert_bool_to_uint, get_cut, get_cut_base, make_G_m_csr_buf, make_theta_buf, maxcut_hamming_cdf, opencl_context, sample_mag, bit_pick, init_bit_pick, to_scipy_sparse_upper_triangular
|
|
7
|
+
from .maxcut_tfim_util import binary_search, convert_bool_to_uint, get_cut, get_cut_base, make_G_m_csr_buf, make_theta_buf, maxcut_hamming_cdf, opencl_context, sample_mag, setup_cut_opencl, bit_pick, init_bit_pick, to_scipy_sparse_upper_triangular
|
|
8
8
|
|
|
9
9
|
IS_OPENCL_AVAILABLE = True
|
|
10
10
|
try:
|
|
@@ -18,7 +18,7 @@ dtype = opencl_context.dtype
|
|
|
18
18
|
|
|
19
19
|
|
|
20
20
|
@njit
|
|
21
|
-
def update_repulsion_choice(
|
|
21
|
+
def update_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, n, used, node, repulsion_base):
|
|
22
22
|
# Select node
|
|
23
23
|
used[node] = True
|
|
24
24
|
|
|
@@ -41,7 +41,7 @@ def update_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, n, used,
|
|
|
41
41
|
|
|
42
42
|
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
43
43
|
@njit
|
|
44
|
-
def local_repulsion_choice(
|
|
44
|
+
def local_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, tot_init_weight, repulsion_base, n, m):
|
|
45
45
|
"""
|
|
46
46
|
Pick m nodes out of n with repulsion bias:
|
|
47
47
|
- High-degree nodes are already less likely
|
|
@@ -60,13 +60,13 @@ def local_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, tot_init_w
|
|
|
60
60
|
used[node] = True
|
|
61
61
|
return used
|
|
62
62
|
|
|
63
|
-
update_repulsion_choice(
|
|
63
|
+
update_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, n, used, node, repulsion_base)
|
|
64
64
|
|
|
65
65
|
for _ in range(1, m - 1):
|
|
66
66
|
node = bit_pick(weights, used, n)
|
|
67
67
|
|
|
68
68
|
# Update answer and weights
|
|
69
|
-
update_repulsion_choice(
|
|
69
|
+
update_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, n, used, node, repulsion_base)
|
|
70
70
|
|
|
71
71
|
node = bit_pick(weights, used, n)
|
|
72
72
|
|
|
@@ -101,28 +101,34 @@ def compute_cut(sample, G_data, G_rows, G_cols, n_qubits):
|
|
|
101
101
|
|
|
102
102
|
|
|
103
103
|
@njit(parallel=True)
|
|
104
|
-
def sample_measurement(
|
|
105
|
-
shots = max(1, shots >> 1)
|
|
104
|
+
def sample_measurement(G_data, G_rows, G_cols, max_edge, shots, thresholds, weights, repulsion_base, is_spin_glass):
|
|
105
|
+
shots = ((max(1, shots >> 1) + 3) >> 2) << 2
|
|
106
106
|
n = G_rows.shape[0] - 1
|
|
107
107
|
tot_init_weight = weights.sum()
|
|
108
108
|
|
|
109
109
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
110
110
|
energies = np.empty(shots, dtype=dtype)
|
|
111
|
-
|
|
111
|
+
|
|
112
112
|
best_solution = solutions[0]
|
|
113
113
|
best_energy = -float("inf")
|
|
114
114
|
|
|
115
115
|
improved = True
|
|
116
116
|
while improved:
|
|
117
117
|
improved = False
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
118
|
+
if is_spin_glass:
|
|
119
|
+
for s in prange(shots):
|
|
120
|
+
# First dimension: Hamming weight
|
|
121
|
+
m = sample_mag(thresholds)
|
|
122
|
+
# Second dimension: permutation within Hamming weight
|
|
123
|
+
solutions[s] = local_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, tot_init_weight, repulsion_base, n, m)
|
|
124
|
+
energies[s] = compute_energy(solutions[s], G_data, G_rows, G_cols, n)
|
|
125
|
+
else:
|
|
126
|
+
for s in prange(shots):
|
|
127
|
+
# First dimension: Hamming weight
|
|
128
|
+
m = sample_mag(thresholds)
|
|
129
|
+
# Second dimension: permutation within Hamming weight
|
|
130
|
+
solutions[s] = local_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, tot_init_weight, repulsion_base, n, m)
|
|
131
|
+
energies[s] = compute_cut(solutions[s], G_data, G_rows, G_cols, n)
|
|
126
132
|
|
|
127
133
|
best_index = np.argmax(energies)
|
|
128
134
|
energy = energies[best_index]
|
|
@@ -143,7 +149,7 @@ def shot_loop(G_data, G_rows, G_cols, max_edge, thresholds, weights, tot_init_we
|
|
|
143
149
|
# First dimension: Hamming weight
|
|
144
150
|
m = sample_mag(thresholds)
|
|
145
151
|
# Second dimension: permutation within Hamming weight
|
|
146
|
-
solutions[s] = local_repulsion_choice(
|
|
152
|
+
solutions[s] = local_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, tot_init_weight, repulsion_base, n, m)
|
|
147
153
|
|
|
148
154
|
|
|
149
155
|
def sample_for_opencl(G_data, G_rows, G_cols, G_data_buf, G_rows_buf, G_cols_buf, max_edge, shots, thresholds, weights, repulsion_base, is_spin_glass, is_segmented, segment_size):
|
|
@@ -152,23 +158,24 @@ def sample_for_opencl(G_data, G_rows, G_cols, G_data_buf, G_rows_buf, G_cols_buf
|
|
|
152
158
|
tot_init_weight = weights.sum()
|
|
153
159
|
|
|
154
160
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
155
|
-
energies = np.empty(shots, dtype=dtype)
|
|
156
161
|
|
|
157
162
|
best_solution = solutions[0]
|
|
158
163
|
best_energy = -float("inf")
|
|
159
164
|
|
|
165
|
+
opencl_args = setup_cut_opencl(shots, n, segment_size, is_spin_glass)
|
|
166
|
+
|
|
160
167
|
improved = True
|
|
161
168
|
while improved:
|
|
162
169
|
improved = False
|
|
163
170
|
shot_loop(G_data, G_rows, G_cols, max_edge, thresholds, weights, tot_init_weight, repulsion_base, n, shots, solutions)
|
|
164
|
-
solution, energy = run_cut_opencl(solutions, G_data_buf, G_rows_buf, G_cols_buf, is_segmented, segment_size, is_spin_glass)
|
|
171
|
+
solution, energy = run_cut_opencl(solutions, G_data_buf, G_rows_buf, G_cols_buf, is_segmented, segment_size, is_spin_glass, *opencl_args)
|
|
165
172
|
if energy > best_energy:
|
|
166
173
|
best_energy = energy
|
|
167
174
|
best_solution = solution
|
|
168
175
|
improved = True
|
|
169
176
|
|
|
170
177
|
if is_spin_glass:
|
|
171
|
-
best_energy = compute_cut(best_solution,
|
|
178
|
+
best_energy = compute_cut(best_solution, G_data, G_rows, G_cols, n)
|
|
172
179
|
|
|
173
180
|
return best_solution, float(best_energy)
|
|
174
181
|
|
|
@@ -203,14 +210,13 @@ def init_J_and_z(G_m):
|
|
|
203
210
|
return J_eff, degrees, G_max
|
|
204
211
|
|
|
205
212
|
|
|
206
|
-
|
|
207
|
-
def cpu_footer(J_eff, degrees, shots, quality, n_qubits, G_max, G_data, G_rows, G_col, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
|
|
213
|
+
def cpu_footer(J_eff, degrees, shots, quality, n_qubits, G_max, G_data, G_rows, G_cols, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
|
|
208
214
|
hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
|
|
209
215
|
|
|
210
216
|
degrees = None
|
|
211
217
|
J_eff = 1.0 / (1.0 + epsilon - J_eff)
|
|
212
218
|
|
|
213
|
-
best_solution, best_value = sample_measurement(
|
|
219
|
+
best_solution, best_value = sample_measurement(G_data, G_rows, G_cols, G_max, shots, hamming_prob, J_eff, repulsion_base, is_spin_glass)
|
|
214
220
|
|
|
215
221
|
bit_string, l, r = get_cut(best_solution, nodes)
|
|
216
222
|
|
|
@@ -232,24 +238,18 @@ def convert_bool_to_uint(samples):
|
|
|
232
238
|
return theta
|
|
233
239
|
|
|
234
240
|
|
|
235
|
-
def
|
|
241
|
+
def setup_cut_opencl(shots, n, segment_size, is_spin_glass):
|
|
236
242
|
ctx = opencl_context.ctx
|
|
237
243
|
queue = opencl_context.queue
|
|
238
|
-
calculate_cut_kernel = opencl_context.calculate_cut_sparse_segmented_kernel if is_segmented else opencl_context.calculate_cut_sparse_kernel
|
|
239
244
|
dtype = opencl_context.dtype
|
|
240
|
-
epsilon = opencl_context.epsilon
|
|
241
245
|
wgs = opencl_context.work_group_size
|
|
242
246
|
|
|
243
|
-
shots = samples.shape[1]
|
|
244
|
-
|
|
245
247
|
# Args: [n, shots, is_spin_glass, prng_seed, segment_size]
|
|
246
|
-
|
|
247
|
-
args_np = np.array([shots, n, is_spin_glass, segment_size], dtype=np.int32)
|
|
248
|
+
args_np = np.array([n, shots, is_spin_glass, segment_size], dtype=np.int32)
|
|
248
249
|
|
|
249
250
|
# Buffers
|
|
250
251
|
mf = cl.mem_flags
|
|
251
252
|
args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args_np)
|
|
252
|
-
theta_buf = make_theta_buf(convert_bool_to_uint(samples), is_segmented, shots, n)
|
|
253
253
|
|
|
254
254
|
# Local memory allocation (1 float per work item)
|
|
255
255
|
local_size = min(wgs, shots)
|
|
@@ -265,6 +265,20 @@ def run_cut_opencl(samples, G_data_buf, G_rows_buf, G_cols_buf, is_segmented, se
|
|
|
265
265
|
max_energy_buf = cl.Buffer(ctx, mf.WRITE_ONLY, max_energy_host.nbytes)
|
|
266
266
|
max_index_buf = cl.Buffer(ctx, mf.WRITE_ONLY, max_index_host.nbytes)
|
|
267
267
|
|
|
268
|
+
return local_size, global_size, args_buf, local_energy_buf, local_index_buf, max_energy_host, max_index_host, max_energy_buf, max_index_buf
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def run_cut_opencl(samples, G_data_buf, G_rows_buf, G_cols_buf, is_segmented, segment_size, is_spin_glass, local_size, global_size, args_buf, local_energy_buf, local_index_buf, max_energy_host, max_index_host, max_energy_buf, max_index_buf):
|
|
272
|
+
queue = opencl_context.queue
|
|
273
|
+
calculate_cut_kernel = opencl_context.calculate_cut_sparse_segmented_kernel if is_segmented else opencl_context.calculate_cut_sparse_kernel
|
|
274
|
+
|
|
275
|
+
shots = samples.shape[0]
|
|
276
|
+
n = samples.shape[1]
|
|
277
|
+
|
|
278
|
+
# Buffers
|
|
279
|
+
mf = cl.mem_flags
|
|
280
|
+
theta_buf = make_theta_buf(convert_bool_to_uint(samples), is_segmented, shots, n)
|
|
281
|
+
|
|
268
282
|
# Set kernel args
|
|
269
283
|
if is_segmented:
|
|
270
284
|
calculate_cut_kernel.set_args(
|
|
@@ -117,6 +117,36 @@ except ImportError:
|
|
|
117
117
|
opencl_context = OpenCLContext(compute_units, IS_OPENCL_AVAILABLE, work_group_size, dtype, epsilon, max_alloc, ctx, queue, bootstrap_kernel, bootstrap_sparse_kernel, bootstrap_segmented_kernel, bootstrap_sparse_segmented_kernel, calculate_cut_kernel, calculate_cut_sparse_kernel, calculate_cut_segmented_kernel, calculate_cut_sparse_segmented_kernel)
|
|
118
118
|
|
|
119
119
|
|
|
120
|
+
def setup_cut_opencl(shots, n, segment_size, is_spin_glass):
|
|
121
|
+
ctx = opencl_context.ctx
|
|
122
|
+
queue = opencl_context.queue
|
|
123
|
+
dtype = opencl_context.dtype
|
|
124
|
+
wgs = opencl_context.work_group_size
|
|
125
|
+
|
|
126
|
+
# Args: [n, shots, is_spin_glass, prng_seed, segment_size]
|
|
127
|
+
args_np = np.array([n, shots, is_spin_glass, segment_size], dtype=np.int32)
|
|
128
|
+
|
|
129
|
+
# Buffers
|
|
130
|
+
mf = cl.mem_flags
|
|
131
|
+
args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args_np)
|
|
132
|
+
|
|
133
|
+
# Local memory allocation (1 float per work item)
|
|
134
|
+
local_size = min(wgs, shots)
|
|
135
|
+
max_global_size = ((opencl_context.MAX_GPU_PROC_ELEM + local_size - 1) // local_size) * local_size # corresponds to MAX_PROC_ELEM macro in OpenCL kernel program
|
|
136
|
+
global_size = min(((shots + local_size - 1) // local_size) * local_size, max_global_size)
|
|
137
|
+
local_energy_buf = cl.LocalMemory(np.dtype(dtype).itemsize * local_size)
|
|
138
|
+
local_index_buf = cl.LocalMemory(np.dtype(np.int32).itemsize * local_size)
|
|
139
|
+
|
|
140
|
+
# Allocate max_energy and max_index result buffers per workgroup
|
|
141
|
+
max_energy_host = np.empty(global_size, dtype=dtype)
|
|
142
|
+
max_index_host = np.empty(global_size, dtype=np.int32)
|
|
143
|
+
|
|
144
|
+
max_energy_buf = cl.Buffer(ctx, mf.WRITE_ONLY, max_energy_host.nbytes)
|
|
145
|
+
max_index_buf = cl.Buffer(ctx, mf.WRITE_ONLY, max_index_host.nbytes)
|
|
146
|
+
|
|
147
|
+
return local_size, global_size, args_buf, local_energy_buf, local_index_buf, max_energy_host, max_index_host, max_energy_buf, max_index_buf
|
|
148
|
+
|
|
149
|
+
|
|
120
150
|
def make_G_m_buf(G_m, is_segmented, segment_size):
|
|
121
151
|
mf = cl.mem_flags
|
|
122
152
|
ctx = opencl_context.ctx
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
from .maxcut_tfim_util import binary_search, opencl_context, to_scipy_sparse_upper_triangular
|
|
2
|
-
from .maxcut_tfim import
|
|
2
|
+
from .maxcut_tfim import maxcut_tfim
|
|
3
3
|
from concurrent.futures import ProcessPoolExecutor
|
|
4
4
|
import time
|
|
5
5
|
import itertools
|
|
@@ -621,10 +621,7 @@ def tsp_symmetric_driver(G_m, is_cyclic, is_top_level, start_node, end_node, k_n
|
|
|
621
621
|
return [nodes[x] for x in best_path], best_weight
|
|
622
622
|
|
|
623
623
|
|
|
624
|
-
@njit
|
|
625
624
|
def tsp_symmetric_header(G_m, nodes, quality, shots, anneal_t, anneal_h, repulsion_base, start_node, end_node, monte_carlo, is_cyclic, n_nodes, multi_start):
|
|
626
|
-
n_x = nodes[0]
|
|
627
|
-
|
|
628
625
|
if is_cyclic:
|
|
629
626
|
start_node = None
|
|
630
627
|
end_node = None
|
|
@@ -633,21 +630,16 @@ def tsp_symmetric_header(G_m, nodes, quality, shots, anneal_t, anneal_h, repulsi
|
|
|
633
630
|
start_node = end_node
|
|
634
631
|
end_node = None
|
|
635
632
|
|
|
636
|
-
a, b, c = [
|
|
637
|
-
a.clear()
|
|
638
|
-
b.clear()
|
|
639
|
-
c.clear()
|
|
633
|
+
a, b, c = [], [], []
|
|
640
634
|
if (start_node is None) and (end_node is None):
|
|
641
635
|
if monte_carlo:
|
|
642
636
|
a, b = monte_carlo_loop(n_nodes)
|
|
643
637
|
else:
|
|
644
638
|
best_cut = -float("inf")
|
|
645
639
|
for _ in range(multi_start):
|
|
646
|
-
bits = ([
|
|
647
|
-
bits[0].clear()
|
|
648
|
-
bits[1].clear()
|
|
640
|
+
bits = ([], [])
|
|
649
641
|
while (len(bits[0]) == 0) or (len(bits[1]) == 0):
|
|
650
|
-
_, cut_value, bits =
|
|
642
|
+
_, cut_value, bits = maxcut_tfim(G_m, quality=quality, shots=shots, anneal_t=anneal_t, anneal_h=anneal_h, repulsion_base=repulsion_base)
|
|
651
643
|
if cut_value > best_cut:
|
|
652
644
|
best_cut = cut_value
|
|
653
645
|
a, b = bits
|
|
@@ -838,10 +830,7 @@ def tsp_asymmetric_driver(G_m, is_reversed, is_cyclic, is_top_level, start_node,
|
|
|
838
830
|
return [nodes[x] for x in final_path], best_weight
|
|
839
831
|
|
|
840
832
|
|
|
841
|
-
@njit
|
|
842
833
|
def tsp_asymmetric_header(G_m, nodes, quality, shots, anneal_t, anneal_h, repulsion_base, start_node, end_node, monte_carlo, is_cyclic, n_nodes, multi_start):
|
|
843
|
-
n_x = nodes[0]
|
|
844
|
-
|
|
845
834
|
if is_cyclic:
|
|
846
835
|
start_node = None
|
|
847
836
|
end_node = None
|
|
@@ -853,21 +842,16 @@ def tsp_asymmetric_header(G_m, nodes, quality, shots, anneal_t, anneal_h, repuls
|
|
|
853
842
|
end_node = None
|
|
854
843
|
G_m = G_m.T
|
|
855
844
|
|
|
856
|
-
a, b, c = [
|
|
857
|
-
a.clear()
|
|
858
|
-
b.clear()
|
|
859
|
-
c.clear()
|
|
845
|
+
a, b, c = [], [], []
|
|
860
846
|
if (start_node is None) and (end_node is None):
|
|
861
847
|
if monte_carlo:
|
|
862
848
|
a, b = monte_carlo_loop(n_nodes)
|
|
863
849
|
else:
|
|
864
850
|
best_cut = -float("inf")
|
|
865
851
|
for _ in range(multi_start):
|
|
866
|
-
bits = ([
|
|
867
|
-
bits[0].clear()
|
|
868
|
-
bits[1].clear()
|
|
852
|
+
bits = ([], [])
|
|
869
853
|
while (len(bits[0]) == 0) or (len(bits[1]) == 0):
|
|
870
|
-
_, cut_value, bits =
|
|
854
|
+
_, cut_value, bits = maxcut_tfim((G_m + G_m.T) / 2, quality=quality, shots=shots, anneal_t=anneal_t, anneal_h=anneal_h, repulsion_base=repulsion_base)
|
|
871
855
|
if cut_value > best_cut:
|
|
872
856
|
best_cut = cut_value
|
|
873
857
|
a, b = bits
|
|
@@ -7,7 +7,7 @@ with open(README_PATH) as readme_file:
|
|
|
7
7
|
|
|
8
8
|
setup(
|
|
9
9
|
name='pyqrackising',
|
|
10
|
-
version='8.14.
|
|
10
|
+
version='8.14.3',
|
|
11
11
|
author='Dan Strano',
|
|
12
12
|
author_email='stranoj@gmail.com',
|
|
13
13
|
description='Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)',
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|