PyQrackIsing 5.4.4__tar.gz → 5.4.6__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.4/pyqrackising.egg-info → pyqrackising-5.4.6}/PKG-INFO +1 -1
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/pyproject.toml +1 -1
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/pyqrackising/kernels.cl +1 -0
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/pyqrackising/maxcut_tfim.py +105 -18
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/pyqrackising/maxcut_tfim_sparse.py +41 -26
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/pyqrackising/maxcut_tfim_streaming.py +31 -13
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/pyqrackising/maxcut_tfim_util.py +10 -0
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/pyqrackising/spin_glass_solver.py +7 -3
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/pyqrackising/spin_glass_solver_sparse.py +9 -5
- {pyqrackising-5.4.4 → pyqrackising-5.4.6/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/setup.py +1 -1
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/CMakeLists.txt +0 -0
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/LICENSE.md +0 -0
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/MANIFEST.in +0 -0
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/README.md +0 -0
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/pyqrackising/__init__.py +0 -0
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/pyqrackising/spin_glass_solver_streaming.py +0 -0
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/pyqrackising/spin_glass_solver_util.py +0 -0
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/pyqrackising/tsp.py +0 -0
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-5.4.4 → pyqrackising-5.4.6}/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.6"
|
|
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"}
|
|
@@ -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, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
7
|
+
from .maxcut_tfim_util import fix_cdf, get_cut, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
8
8
|
|
|
9
9
|
IS_OPENCL_AVAILABLE = True
|
|
10
10
|
try:
|
|
@@ -13,6 +13,18 @@ except ImportError:
|
|
|
13
13
|
IS_OPENCL_AVAILABLE = False
|
|
14
14
|
|
|
15
15
|
|
|
16
|
+
@njit
|
|
17
|
+
def update_repulsion_choice(G_m, max_weight, weights, n, used, node):
|
|
18
|
+
# Select node
|
|
19
|
+
used[node] = True
|
|
20
|
+
|
|
21
|
+
# Repulsion: penalize neighbors
|
|
22
|
+
for nbr in range(n):
|
|
23
|
+
if used[nbr]:
|
|
24
|
+
continue
|
|
25
|
+
weights[nbr] *= max(2e-7, 1 - G_m[node, nbr] / max_weight)
|
|
26
|
+
|
|
27
|
+
|
|
16
28
|
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
17
29
|
@njit
|
|
18
30
|
def local_repulsion_choice(G_m, max_weight, weights, n, m):
|
|
@@ -27,7 +39,10 @@ def local_repulsion_choice(G_m, max_weight, weights, n, m):
|
|
|
27
39
|
weights = weights.copy()
|
|
28
40
|
used = np.zeros(n, dtype=np.bool_) # False = available, True = used
|
|
29
41
|
|
|
30
|
-
|
|
42
|
+
# Update answer and weights
|
|
43
|
+
update_repulsion_choice(G_m, max_weight, weights, n, used, np.random.randint(0, n - 1))
|
|
44
|
+
|
|
45
|
+
for _ in range(1, m):
|
|
31
46
|
# Count available
|
|
32
47
|
total_w = 0.0
|
|
33
48
|
for i in range(n):
|
|
@@ -52,14 +67,8 @@ def local_repulsion_choice(G_m, max_weight, weights, n, m):
|
|
|
52
67
|
while used[node]:
|
|
53
68
|
node += 1
|
|
54
69
|
|
|
55
|
-
#
|
|
56
|
-
used
|
|
57
|
-
|
|
58
|
-
# Repulsion: penalize neighbors
|
|
59
|
-
for nbr in range(n):
|
|
60
|
-
if used[nbr]:
|
|
61
|
-
continue
|
|
62
|
-
weights[nbr] *= max(2e-7, 1 - G_m[node, nbr] / max_weight)
|
|
70
|
+
# Update answer and weights
|
|
71
|
+
update_repulsion_choice(G_m, max_weight, weights, n, used, node)
|
|
63
72
|
|
|
64
73
|
return used
|
|
65
74
|
|
|
@@ -126,6 +135,78 @@ def init_J_and_z(G_m):
|
|
|
126
135
|
return J_eff, degrees
|
|
127
136
|
|
|
128
137
|
|
|
138
|
+
def run_sampling_opencl(G_m_np, thresholds_np, shots, n):
|
|
139
|
+
ctx = opencl_context.ctx
|
|
140
|
+
queue = opencl_context.queue
|
|
141
|
+
kernel = opencl_context.sample_for_solution_best_bitset_kernel
|
|
142
|
+
|
|
143
|
+
max_local_size = 64 # tune
|
|
144
|
+
max_global_size = 65536
|
|
145
|
+
global_size = min(((shots + max_local_size - 1) // max_local_size) * max_local_size, max_global_size)
|
|
146
|
+
local_size = max_local_size
|
|
147
|
+
num_groups = global_size // local_size
|
|
148
|
+
|
|
149
|
+
# Bit-packing params
|
|
150
|
+
words = (n + 31) // 32
|
|
151
|
+
|
|
152
|
+
# Random seeds (host)
|
|
153
|
+
rng_seeds_np = np.random.randint(1, 2**31-1, size=global_size, dtype=np.uint32)
|
|
154
|
+
|
|
155
|
+
# Device buffers
|
|
156
|
+
mf = cl.mem_flags
|
|
157
|
+
G_m_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m_np)
|
|
158
|
+
thresholds_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=thresholds_np)
|
|
159
|
+
rng_buf = cl.Buffer(ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=rng_seeds_np)
|
|
160
|
+
|
|
161
|
+
# Solutions buffer: each work-item writes a candidate bitset
|
|
162
|
+
solutions_buf = cl.Buffer(ctx, mf.READ_WRITE, size=num_groups * words * np.uint32().nbytes)
|
|
163
|
+
|
|
164
|
+
# Best energies and solutions (per group)
|
|
165
|
+
# We'll reuse solutions_buf to hold them after reduction (kernel copies winners into group slices)
|
|
166
|
+
best_energies_np = np.empty(num_groups, dtype=np.float32)
|
|
167
|
+
best_energies_buf = cl.Buffer(ctx, mf.WRITE_ONLY, best_energies_np.nbytes)
|
|
168
|
+
|
|
169
|
+
# Local memory buffers
|
|
170
|
+
local_energy_buf = cl.LocalMemory(np.dtype(np.float32).itemsize * local_size)
|
|
171
|
+
local_index_buf = cl.LocalMemory(np.dtype(np.int32).itemsize * local_size)
|
|
172
|
+
|
|
173
|
+
# Set kernel args
|
|
174
|
+
kernel.set_args(
|
|
175
|
+
G_m_buf,
|
|
176
|
+
thresholds_buf,
|
|
177
|
+
np.int32(n),
|
|
178
|
+
np.int32(shots),
|
|
179
|
+
rng_buf,
|
|
180
|
+
solutions_buf,
|
|
181
|
+
best_energies_buf,
|
|
182
|
+
local_energy_buf,
|
|
183
|
+
local_index_buf
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
# Launch
|
|
187
|
+
cl.enqueue_nd_range_kernel(queue, kernel, (global_size,), (local_size,))
|
|
188
|
+
|
|
189
|
+
# Copy back
|
|
190
|
+
cl.enqueue_copy(queue, best_energies_np, best_energies_buf)
|
|
191
|
+
solutions_np = np.empty(num_groups * words, dtype=np.uint32)
|
|
192
|
+
cl.enqueue_copy(queue, solutions_np, solutions_buf) # all candidates, includes per-group winners
|
|
193
|
+
queue.finish()
|
|
194
|
+
|
|
195
|
+
# Host reduction
|
|
196
|
+
best_group = np.argmax(best_energies_np) # max cut
|
|
197
|
+
best_energy = float(best_energies_np[best_group])
|
|
198
|
+
best_solution_bits = solutions_np[best_group * words : (best_group + 1) * words]
|
|
199
|
+
|
|
200
|
+
# Unpack bitset into boolean vector
|
|
201
|
+
best_solution = np.zeros(n, dtype=np.bool_)
|
|
202
|
+
for u in range(n):
|
|
203
|
+
w = u >> 5
|
|
204
|
+
b = u & 31
|
|
205
|
+
best_solution[u] = (best_solution_bits[w] >> b) & 1
|
|
206
|
+
|
|
207
|
+
return best_solution, best_energy
|
|
208
|
+
|
|
209
|
+
|
|
129
210
|
@njit
|
|
130
211
|
def cpu_footer(shots, quality, n_qubits, G_m, nodes):
|
|
131
212
|
J_eff, degrees = init_J_and_z(G_m)
|
|
@@ -142,12 +223,7 @@ def cpu_footer(shots, quality, n_qubits, G_m, nodes):
|
|
|
142
223
|
|
|
143
224
|
@njit
|
|
144
225
|
def gpu_footer(shots, n_qubits, G_m, J_eff, hamming_prob, nodes):
|
|
145
|
-
hamming_prob
|
|
146
|
-
tot_prob = 0.0
|
|
147
|
-
for i in range(n_qubits - 1):
|
|
148
|
-
tot_prob += hamming_prob[i]
|
|
149
|
-
hamming_prob[i] = tot_prob
|
|
150
|
-
hamming_prob[-1] = 2.0
|
|
226
|
+
fix_cdf(hamming_prob)
|
|
151
227
|
|
|
152
228
|
best_solution, best_value = sample_for_solution(G_m, shots, hamming_prob, J_eff)
|
|
153
229
|
|
|
@@ -159,7 +235,7 @@ def gpu_footer(shots, n_qubits, G_m, J_eff, hamming_prob, nodes):
|
|
|
159
235
|
def maxcut_tfim(
|
|
160
236
|
G,
|
|
161
237
|
quality=None,
|
|
162
|
-
shots=None
|
|
238
|
+
shots=None
|
|
163
239
|
):
|
|
164
240
|
nodes = None
|
|
165
241
|
n_qubits = 0
|
|
@@ -216,7 +292,7 @@ def maxcut_tfim(
|
|
|
216
292
|
args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
|
|
217
293
|
J_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
|
|
218
294
|
deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
|
|
219
|
-
theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE, size=(n_qubits *
|
|
295
|
+
theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE, size=(n_qubits * 8))
|
|
220
296
|
|
|
221
297
|
# Warp size is 32:
|
|
222
298
|
group_size = min(n_qubits, 64)
|
|
@@ -246,5 +322,16 @@ def maxcut_tfim(
|
|
|
246
322
|
|
|
247
323
|
# Fetch results
|
|
248
324
|
cl.enqueue_copy(opencl_context.queue, hamming_prob, ham_buf)
|
|
325
|
+
opencl_context.queue.finish()
|
|
326
|
+
|
|
327
|
+
args_buf.release()
|
|
328
|
+
J_buf.release()
|
|
329
|
+
deg_buf.release()
|
|
330
|
+
theta_buf.release()
|
|
331
|
+
|
|
332
|
+
args_buf = None
|
|
333
|
+
J_buf = None
|
|
334
|
+
deg_buf = None
|
|
335
|
+
theta_buf = None
|
|
249
336
|
|
|
250
337
|
return gpu_footer(shots, n_qubits, G_m, J_eff, hamming_prob, nodes)
|
|
@@ -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, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
8
|
+
from .maxcut_tfim_util import fix_cdf, get_cut, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
9
9
|
|
|
10
10
|
IS_OPENCL_AVAILABLE = True
|
|
11
11
|
try:
|
|
@@ -33,6 +33,28 @@ def binary_search(l, t):
|
|
|
33
33
|
return len(l)
|
|
34
34
|
|
|
35
35
|
|
|
36
|
+
@njit
|
|
37
|
+
def update_repulsion_choice(G_cols, G_data, G_rows, max_weight, weights, n, used, node):
|
|
38
|
+
# Select node
|
|
39
|
+
used[node] = True
|
|
40
|
+
|
|
41
|
+
# Repulsion: penalize neighbors
|
|
42
|
+
for j in range(G_rows[node], G_rows[node + 1]):
|
|
43
|
+
nbr = G_cols[j]
|
|
44
|
+
if used[nbr]:
|
|
45
|
+
continue
|
|
46
|
+
weights[nbr] *= max(2e-7, 1 - G_data[j] / max_weight)
|
|
47
|
+
|
|
48
|
+
for nbr in range(node):
|
|
49
|
+
if used[nbr]:
|
|
50
|
+
continue
|
|
51
|
+
start = G_rows[nbr]
|
|
52
|
+
end = G_rows[nbr + 1]
|
|
53
|
+
j = binary_search(G_cols[start:end], node) + start
|
|
54
|
+
if j < end:
|
|
55
|
+
weights[nbr] *= max(2e-7, 1 - G_data[j] / max_weight)
|
|
56
|
+
|
|
57
|
+
|
|
36
58
|
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
37
59
|
@njit
|
|
38
60
|
def local_repulsion_choice(G_cols, G_data, G_rows, max_weight, weights, n, m):
|
|
@@ -47,7 +69,10 @@ def local_repulsion_choice(G_cols, G_data, G_rows, max_weight, weights, n, m):
|
|
|
47
69
|
weights = weights.copy()
|
|
48
70
|
used = np.zeros(n, dtype=np.bool_) # False = available, True = used
|
|
49
71
|
|
|
50
|
-
|
|
72
|
+
# Update answer and weights
|
|
73
|
+
update_repulsion_choice(G_cols, G_data, G_rows, max_weight, weights, n, used, np.random.randint(0, n - 1))
|
|
74
|
+
|
|
75
|
+
for _ in range(1, m):
|
|
51
76
|
# Count available
|
|
52
77
|
total_w = 0.0
|
|
53
78
|
for i in range(n):
|
|
@@ -72,24 +97,8 @@ def local_repulsion_choice(G_cols, G_data, G_rows, max_weight, weights, n, m):
|
|
|
72
97
|
while used[node]:
|
|
73
98
|
node += 1
|
|
74
99
|
|
|
75
|
-
#
|
|
76
|
-
used
|
|
77
|
-
|
|
78
|
-
# Repulsion: penalize neighbors
|
|
79
|
-
for j in range(G_rows[node], G_rows[node + 1]):
|
|
80
|
-
nbr = G_cols[j]
|
|
81
|
-
if used[nbr]:
|
|
82
|
-
continue
|
|
83
|
-
weights[nbr] *= max(2e-7, 1 - G_data[j] / max_weight)
|
|
84
|
-
|
|
85
|
-
for nbr in range(node):
|
|
86
|
-
if used[nbr]:
|
|
87
|
-
continue
|
|
88
|
-
start = G_rows[nbr]
|
|
89
|
-
end = G_rows[nbr + 1]
|
|
90
|
-
j = binary_search(G_cols[start:end], node) + start
|
|
91
|
-
if j < end:
|
|
92
|
-
weights[nbr] *= max(2e-7, 1 - G_data[j] / max_weight)
|
|
100
|
+
# Update answer and weights
|
|
101
|
+
update_repulsion_choice(G_cols, G_data, G_rows, max_weight, weights, n, used, node)
|
|
93
102
|
|
|
94
103
|
return used
|
|
95
104
|
|
|
@@ -189,12 +198,7 @@ def cpu_footer(shots, quality, n_qubits, G_data, G_rows, G_cols, nodes):
|
|
|
189
198
|
|
|
190
199
|
@njit
|
|
191
200
|
def gpu_footer(shots, n_qubits, G_data, G_rows, G_cols, J_eff, hamming_prob, nodes):
|
|
192
|
-
hamming_prob
|
|
193
|
-
tot_prob = 0.0
|
|
194
|
-
for i in range(n_qubits - 1):
|
|
195
|
-
tot_prob += hamming_prob[i]
|
|
196
|
-
hamming_prob[i] = tot_prob
|
|
197
|
-
hamming_prob[-1] = 2.0
|
|
201
|
+
fix_cdf(hamming_prob)
|
|
198
202
|
|
|
199
203
|
best_solution, best_value = sample_for_solution(G_data, G_rows, G_cols, shots, hamming_prob, J_eff)
|
|
200
204
|
|
|
@@ -305,5 +309,16 @@ def maxcut_tfim_sparse(
|
|
|
305
309
|
|
|
306
310
|
# Fetch results
|
|
307
311
|
cl.enqueue_copy(opencl_context.queue, hamming_prob, ham_buf)
|
|
312
|
+
opencl_context.queue.finish()
|
|
313
|
+
|
|
314
|
+
args_buf.release()
|
|
315
|
+
J_buf.release()
|
|
316
|
+
deg_buf.release()
|
|
317
|
+
theta_buf.release()
|
|
318
|
+
|
|
319
|
+
args_buf = None
|
|
320
|
+
J_buf = None
|
|
321
|
+
deg_buf = None
|
|
322
|
+
theta_buf = None
|
|
308
323
|
|
|
309
324
|
return gpu_footer(shots, n_qubits, G_m.data, G_m.indptr, G_m.indices, J_eff, hamming_prob, nodes)
|
|
@@ -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, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
7
|
+
from .maxcut_tfim_util import fix_cdf, get_cut, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
8
8
|
|
|
9
9
|
IS_OPENCL_AVAILABLE = True
|
|
10
10
|
try:
|
|
@@ -13,6 +13,18 @@ except ImportError:
|
|
|
13
13
|
IS_OPENCL_AVAILABLE = False
|
|
14
14
|
|
|
15
15
|
|
|
16
|
+
@njit
|
|
17
|
+
def update_repulsion_choice(G_func, G_func_args_tuple, nodes, max_weight, weights, n, used, node):
|
|
18
|
+
# Select node
|
|
19
|
+
used[node] = True
|
|
20
|
+
|
|
21
|
+
# Repulsion: penalize neighbors
|
|
22
|
+
for nbr in range(n):
|
|
23
|
+
if used[nbr]:
|
|
24
|
+
continue
|
|
25
|
+
weights[nbr] *= max(2e-7, 1 - G_func((nodes[node], nodes[nbr]), G_func_args_tuple) / max_weight)
|
|
26
|
+
|
|
27
|
+
|
|
16
28
|
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
17
29
|
@njit
|
|
18
30
|
def local_repulsion_choice(G_func, G_func_args_tuple, nodes, max_weight, weights, n, m):
|
|
@@ -27,7 +39,10 @@ def local_repulsion_choice(G_func, G_func_args_tuple, nodes, max_weight, weights
|
|
|
27
39
|
weights = weights.copy()
|
|
28
40
|
used = np.zeros(n, dtype=np.bool_) # False = available, True = used
|
|
29
41
|
|
|
30
|
-
|
|
42
|
+
# Update answer and weights
|
|
43
|
+
update_repulsion_choice(G_func, G_func_args_tuple, nodes, max_weight, weights, n, used, np.random.randint(0, n - 1))
|
|
44
|
+
|
|
45
|
+
for _ in range(1, m):
|
|
31
46
|
# Count available
|
|
32
47
|
total_w = 0.0
|
|
33
48
|
for i in range(n):
|
|
@@ -55,11 +70,8 @@ def local_repulsion_choice(G_func, G_func_args_tuple, nodes, max_weight, weights
|
|
|
55
70
|
# Select node
|
|
56
71
|
used[node] = True
|
|
57
72
|
|
|
58
|
-
#
|
|
59
|
-
|
|
60
|
-
if used[nbr]:
|
|
61
|
-
continue
|
|
62
|
-
weights[nbr] *= max(2e-7, 1 - G_func((nodes[node], nodes[nbr]), G_func_args_tuple) / max_weight)
|
|
73
|
+
# Update answer and weights
|
|
74
|
+
update_repulsion_choice(G_func, G_func_args_tuple, nodes, max_weight, weights, n, used, node)
|
|
63
75
|
|
|
64
76
|
return used
|
|
65
77
|
|
|
@@ -148,12 +160,7 @@ def cpu_footer(shots, quality, n_qubits, G_func, G_func_args_tuple, nodes):
|
|
|
148
160
|
|
|
149
161
|
@njit
|
|
150
162
|
def gpu_footer(shots, n_qubits, G_func, G_func_args_tuple, nodes, G_max, J_eff, degrees, hamming_prob):
|
|
151
|
-
hamming_prob
|
|
152
|
-
tot_prob = 0.0
|
|
153
|
-
for i in range(n_qubits - 1):
|
|
154
|
-
tot_prob += hamming_prob[i]
|
|
155
|
-
hamming_prob[i] = tot_prob
|
|
156
|
-
hamming_prob[-1] = 2.0
|
|
163
|
+
fix_cdf(hamming_prob)
|
|
157
164
|
|
|
158
165
|
best_solution, best_value = sample_for_solution(G_func, G_func_args_tuple, nodes, G_max, shots, hamming_prob, degrees.sum(), J_eff, n_qubits)
|
|
159
166
|
|
|
@@ -244,5 +251,16 @@ def maxcut_tfim_streaming(
|
|
|
244
251
|
|
|
245
252
|
# Fetch results
|
|
246
253
|
cl.enqueue_copy(opencl_context.queue, hamming_prob, ham_buf)
|
|
254
|
+
opencl_context.queue.finish()
|
|
255
|
+
|
|
256
|
+
args_buf.release()
|
|
257
|
+
J_buf.release()
|
|
258
|
+
deg_buf.release()
|
|
259
|
+
theta_buf.release()
|
|
260
|
+
|
|
261
|
+
args_buf = None
|
|
262
|
+
J_buf = None
|
|
263
|
+
deg_buf = None
|
|
264
|
+
theta_buf = None
|
|
247
265
|
|
|
248
266
|
return gpu_footer(shots, n_qubits, G_func, G_func_args_tuple, nodes, G_max, J_eff, degrees, hamming_prob)
|
|
@@ -101,6 +101,16 @@ def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, hamming_prob):
|
|
|
101
101
|
hamming_prob[-1] = 2.0
|
|
102
102
|
|
|
103
103
|
|
|
104
|
+
@njit
|
|
105
|
+
def fix_cdf(hamming_prob):
|
|
106
|
+
hamming_prob /= hamming_prob.sum()
|
|
107
|
+
tot_prob = 0.0
|
|
108
|
+
for i in range(len(hamming_prob)):
|
|
109
|
+
tot_prob += hamming_prob[i]
|
|
110
|
+
hamming_prob[i] = tot_prob
|
|
111
|
+
hamming_prob[-1] = 2.0
|
|
112
|
+
|
|
113
|
+
|
|
104
114
|
@njit
|
|
105
115
|
def probability_by_hamming_weight(J, h, z, theta, t, n_qubits):
|
|
106
116
|
bias = np.empty(n_qubits - 1, dtype=np.float32)
|
|
@@ -67,7 +67,7 @@ def bootstrap(best_theta, G_m, indices_array, k, min_energy):
|
|
|
67
67
|
return min_energy
|
|
68
68
|
|
|
69
69
|
|
|
70
|
-
def run_bootstrap_opencl(best_theta,
|
|
70
|
+
def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy):
|
|
71
71
|
ctx = opencl_context.ctx
|
|
72
72
|
queue = opencl_context.queue
|
|
73
73
|
bootstrap_kernel = opencl_context.bootstrap_kernel
|
|
@@ -83,7 +83,6 @@ def run_bootstrap_opencl(best_theta, G_m_np, indices_array_np, k, min_energy):
|
|
|
83
83
|
# Buffers
|
|
84
84
|
mf = cl.mem_flags
|
|
85
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
86
|
indices_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=indices_array_np)
|
|
88
87
|
args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args_np)
|
|
89
88
|
|
|
@@ -177,6 +176,11 @@ def spin_glass_solver(G, quality=None, shots=None, best_guess=None):
|
|
|
177
176
|
bitstring, _, _ = maxcut_tfim(G_m, quality=quality, shots=shots)
|
|
178
177
|
best_theta = np.array([b == "1" for b in list(bitstring)], dtype=np.bool_)
|
|
179
178
|
|
|
179
|
+
if IS_OPENCL_AVAILABLE:
|
|
180
|
+
mf = cl.mem_flags
|
|
181
|
+
ctx = opencl_context.ctx
|
|
182
|
+
G_m_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m)
|
|
183
|
+
|
|
180
184
|
min_energy = compute_energy(best_theta, G_m)
|
|
181
185
|
improved = True
|
|
182
186
|
correction_quality = 1
|
|
@@ -198,7 +202,7 @@ def spin_glass_solver(G, quality=None, shots=None, best_guess=None):
|
|
|
198
202
|
combos = combos_list[k - 1]
|
|
199
203
|
|
|
200
204
|
if IS_OPENCL_AVAILABLE:
|
|
201
|
-
energy = run_bootstrap_opencl(best_theta,
|
|
205
|
+
energy = run_bootstrap_opencl(best_theta, G_m_buf, combos, k, min_energy)
|
|
202
206
|
else:
|
|
203
207
|
energy = bootstrap(best_theta, G_m, combos, k, min_energy)
|
|
204
208
|
|
|
@@ -70,7 +70,7 @@ def bootstrap(best_theta, G_data, G_rows, G_cols, indices_array, k, min_energy):
|
|
|
70
70
|
return min_energy
|
|
71
71
|
|
|
72
72
|
|
|
73
|
-
def run_bootstrap_opencl(best_theta,
|
|
73
|
+
def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices_array_np, k, min_energy):
|
|
74
74
|
ctx = opencl_context.ctx
|
|
75
75
|
queue = opencl_context.queue
|
|
76
76
|
bootstrap_kernel = opencl_context.bootstrap_sparse_kernel
|
|
@@ -86,9 +86,6 @@ def run_bootstrap_opencl(best_theta, G_data_np, G_rows_np, G_cols_np, indices_ar
|
|
|
86
86
|
# Buffers
|
|
87
87
|
mf = cl.mem_flags
|
|
88
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
89
|
indices_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=indices_array_np)
|
|
93
90
|
args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args_np)
|
|
94
91
|
|
|
@@ -196,6 +193,13 @@ def spin_glass_solver_sparse(G, quality=None, shots=None, best_guess=None):
|
|
|
196
193
|
bitstring, _, _ = maxcut_tfim_sparse(G_m, quality=quality, shots=shots)
|
|
197
194
|
best_theta = np.array([b == "1" for b in list(bitstring)], dtype=np.bool_)
|
|
198
195
|
|
|
196
|
+
if IS_OPENCL_AVAILABLE:
|
|
197
|
+
mf = cl.mem_flags
|
|
198
|
+
ctx = opencl_context.ctx
|
|
199
|
+
G_data_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m.data)
|
|
200
|
+
G_rows_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m.indptr.astype(np.uint64))
|
|
201
|
+
G_cols_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m.indices.astype(np.uint64))
|
|
202
|
+
|
|
199
203
|
min_energy = compute_energy(best_theta, G_m.data, G_m.indptr, G_m.indices)
|
|
200
204
|
improved = True
|
|
201
205
|
correction_quality = 1
|
|
@@ -217,7 +221,7 @@ def spin_glass_solver_sparse(G, quality=None, shots=None, best_guess=None):
|
|
|
217
221
|
combos = combos_list[k - 1]
|
|
218
222
|
|
|
219
223
|
if IS_OPENCL_AVAILABLE:
|
|
220
|
-
energy = run_bootstrap_opencl(best_theta,
|
|
224
|
+
energy = run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, combos, k, min_energy)
|
|
221
225
|
else:
|
|
222
226
|
energy = bootstrap(best_theta, G_m.data, G_m.indptr, G_m.indices, combos, k, min_energy)
|
|
223
227
|
|
|
@@ -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.6',
|
|
55
55
|
author='Dan Strano',
|
|
56
56
|
author_email='stranoj@gmail.com',
|
|
57
57
|
description='Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)',
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|