PyQrackIsing 5.3.1__tar.gz → 5.3.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.3.1/pyqrackising.egg-info → pyqrackising-5.3.2}/PKG-INFO +1 -1
- {pyqrackising-5.3.1 → pyqrackising-5.3.2}/pyproject.toml +1 -1
- {pyqrackising-5.3.1 → pyqrackising-5.3.2}/pyqrackising/maxcut_tfim.py +70 -69
- {pyqrackising-5.3.1 → pyqrackising-5.3.2}/pyqrackising/maxcut_tfim_sparse.py +68 -68
- {pyqrackising-5.3.1 → pyqrackising-5.3.2}/pyqrackising/maxcut_tfim_streaming.py +68 -68
- {pyqrackising-5.3.1 → pyqrackising-5.3.2}/pyqrackising/maxcut_tfim_util.py +31 -0
- {pyqrackising-5.3.1 → pyqrackising-5.3.2/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-5.3.1 → pyqrackising-5.3.2}/setup.py +1 -1
- {pyqrackising-5.3.1 → pyqrackising-5.3.2}/CMakeLists.txt +0 -0
- {pyqrackising-5.3.1 → pyqrackising-5.3.2}/LICENSE.md +0 -0
- {pyqrackising-5.3.1 → pyqrackising-5.3.2}/MANIFEST.in +0 -0
- {pyqrackising-5.3.1 → pyqrackising-5.3.2}/README.md +0 -0
- {pyqrackising-5.3.1 → pyqrackising-5.3.2}/pyqrackising/__init__.py +0 -0
- {pyqrackising-5.3.1 → pyqrackising-5.3.2}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-5.3.1 → pyqrackising-5.3.2}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-5.3.1 → pyqrackising-5.3.2}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-5.3.1 → pyqrackising-5.3.2}/pyqrackising/spin_glass_solver.py +0 -0
- {pyqrackising-5.3.1 → pyqrackising-5.3.2}/pyqrackising/spin_glass_solver_sparse.py +0 -0
- {pyqrackising-5.3.1 → pyqrackising-5.3.2}/pyqrackising/spin_glass_solver_streaming.py +0 -0
- {pyqrackising-5.3.1 → pyqrackising-5.3.2}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-5.3.1 → pyqrackising-5.3.2}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-5.3.1 → pyqrackising-5.3.2}/pyqrackising/tsp.py +0 -0
- {pyqrackising-5.3.1 → pyqrackising-5.3.2}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-5.3.1 → pyqrackising-5.3.2}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-5.3.1 → pyqrackising-5.3.2}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-5.3.1 → pyqrackising-5.3.2}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-5.3.1 → pyqrackising-5.3.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.3.
|
|
15
|
+
version = "5.3.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"}
|
|
@@ -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 init_theta, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
7
|
+
from .maxcut_tfim_util import get_cut, init_theta, init_thresholds, low_width, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
8
8
|
|
|
9
9
|
IS_OPENCL_AVAILABLE = True
|
|
10
10
|
try:
|
|
@@ -78,7 +78,8 @@ def compute_energy(sample, G_m, n_qubits):
|
|
|
78
78
|
|
|
79
79
|
|
|
80
80
|
@njit(parallel=True)
|
|
81
|
-
def sample_for_solution(G_m, shots, thresholds,
|
|
81
|
+
def sample_for_solution(G_m, shots, thresholds, J_eff):
|
|
82
|
+
n = len(G_m)
|
|
82
83
|
max_weight = G_m.max()
|
|
83
84
|
weights = 1.0 / (1.0 + (2 ** -52) - J_eff)
|
|
84
85
|
|
|
@@ -128,6 +129,36 @@ def init_J_and_z(G_m):
|
|
|
128
129
|
return J_eff, degrees
|
|
129
130
|
|
|
130
131
|
|
|
132
|
+
@njit
|
|
133
|
+
def cpu_footer(shots, quality, n_qubits, G_m, nodes):
|
|
134
|
+
J_eff, degrees = init_J_and_z(G_m)
|
|
135
|
+
hamming_prob = init_thresholds(n_qubits)
|
|
136
|
+
|
|
137
|
+
maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, hamming_prob)
|
|
138
|
+
|
|
139
|
+
best_solution, best_value = sample_for_solution(G_m, shots, hamming_prob, J_eff)
|
|
140
|
+
|
|
141
|
+
bit_string, l, r = get_cut(best_solution, nodes)
|
|
142
|
+
|
|
143
|
+
return bit_string, best_value, (l, r)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
@njit
|
|
147
|
+
def gpu_footer(shots, n_qubits, G_m, J_eff, hamming_prob, nodes):
|
|
148
|
+
hamming_prob /= hamming_prob.sum()
|
|
149
|
+
tot_prob = 0.0
|
|
150
|
+
for i in range(n_qubits - 1):
|
|
151
|
+
tot_prob += hamming_prob[i]
|
|
152
|
+
hamming_prob[i] = tot_prob
|
|
153
|
+
hamming_prob[-1] = 2.0
|
|
154
|
+
|
|
155
|
+
best_solution, best_value = sample_for_solution(G_m, shots, hamming_prob, J_eff)
|
|
156
|
+
|
|
157
|
+
bit_string, l, r = get_cut(best_solution, nodes)
|
|
158
|
+
|
|
159
|
+
return bit_string, best_value, (l, r)
|
|
160
|
+
|
|
161
|
+
|
|
131
162
|
def maxcut_tfim(
|
|
132
163
|
G,
|
|
133
164
|
quality=None,
|
|
@@ -146,18 +177,7 @@ def maxcut_tfim(
|
|
|
146
177
|
G_m = G
|
|
147
178
|
|
|
148
179
|
if n_qubits < 3:
|
|
149
|
-
|
|
150
|
-
return "", 0, ([], [])
|
|
151
|
-
|
|
152
|
-
if n_qubits == 1:
|
|
153
|
-
return "0", 0, (nodes, [])
|
|
154
|
-
|
|
155
|
-
if n_qubits == 2:
|
|
156
|
-
weight = G_m[0, 1]
|
|
157
|
-
if weight < 0.0:
|
|
158
|
-
return "00", 0, (nodes, [])
|
|
159
|
-
|
|
160
|
-
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
180
|
+
return low_width(G_m, nodes, n_qubits)
|
|
161
181
|
|
|
162
182
|
if quality is None:
|
|
163
183
|
quality = 8
|
|
@@ -169,61 +189,42 @@ def maxcut_tfim(
|
|
|
169
189
|
n_steps = 1 << quality
|
|
170
190
|
grid_size = n_steps * n_qubits
|
|
171
191
|
|
|
192
|
+
if not (IS_OPENCL_AVAILABLE and grid_size >= 128):
|
|
193
|
+
return cpu_footer(shots, quality, n_qubits, G_m, nodes)
|
|
194
|
+
|
|
172
195
|
J_eff, degrees = init_J_and_z(G_m)
|
|
173
196
|
hamming_prob = init_thresholds(n_qubits)
|
|
174
197
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
opencl_context.
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
hamming_prob /= hamming_prob.sum()
|
|
209
|
-
tot_prob = 0.0
|
|
210
|
-
for i in range(n_qubits - 1):
|
|
211
|
-
tot_prob += hamming_prob[i]
|
|
212
|
-
hamming_prob[i] = tot_prob
|
|
213
|
-
hamming_prob[-1] = 2.0
|
|
214
|
-
else:
|
|
215
|
-
maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, hamming_prob)
|
|
216
|
-
|
|
217
|
-
best_solution, best_value = sample_for_solution(G_m, shots, hamming_prob, degrees.sum(), J_eff, n_qubits)
|
|
218
|
-
|
|
219
|
-
bit_string = ""
|
|
220
|
-
l, r = [], []
|
|
221
|
-
for i in range(len(best_solution)):
|
|
222
|
-
if best_solution[i]:
|
|
223
|
-
bit_string += "1"
|
|
224
|
-
r.append(nodes[i])
|
|
225
|
-
else:
|
|
226
|
-
bit_string += "0"
|
|
227
|
-
l.append(nodes[i])
|
|
228
|
-
|
|
229
|
-
return bit_string, best_value, (l, r)
|
|
198
|
+
delta_t = 1.0 / n_steps
|
|
199
|
+
tot_t = 2.0 * n_steps * delta_t
|
|
200
|
+
h_mult = 2.0 / tot_t
|
|
201
|
+
theta = init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees)
|
|
202
|
+
args = np.empty(3, dtype=np.float32)
|
|
203
|
+
args[0] = delta_t
|
|
204
|
+
args[1] = tot_t
|
|
205
|
+
args[2] = h_mult
|
|
206
|
+
|
|
207
|
+
# Warp size is 32:
|
|
208
|
+
group_size = n_qubits - 1
|
|
209
|
+
if group_size > 256:
|
|
210
|
+
group_size = 256
|
|
211
|
+
grid_dim = n_steps * n_qubits * group_size
|
|
212
|
+
|
|
213
|
+
# Move to GPU
|
|
214
|
+
mf = cl.mem_flags
|
|
215
|
+
args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
|
|
216
|
+
J_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
|
|
217
|
+
deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
|
|
218
|
+
theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=theta)
|
|
219
|
+
ham_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=hamming_prob)
|
|
220
|
+
|
|
221
|
+
# Kernel execution
|
|
222
|
+
opencl_context.maxcut_hamming_cdf_kernel(
|
|
223
|
+
opencl_context.queue, (grid_dim,), (group_size,),
|
|
224
|
+
np.int32(n_qubits), deg_buf, args_buf, J_buf, theta_buf, ham_buf
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
# Fetch results
|
|
228
|
+
cl.enqueue_copy(opencl_context.queue, hamming_prob, ham_buf)
|
|
229
|
+
|
|
230
|
+
return gpu_footer(shots, n_qubits, G_m, 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 init_theta, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
7
|
+
from .maxcut_tfim_util import get_cut, init_theta, init_thresholds, low_width, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
8
8
|
|
|
9
9
|
IS_OPENCL_AVAILABLE = True
|
|
10
10
|
try:
|
|
@@ -175,6 +175,36 @@ def init_J_and_z(G_data, G_rows, G_cols):
|
|
|
175
175
|
return J_eff, degrees
|
|
176
176
|
|
|
177
177
|
|
|
178
|
+
@njit
|
|
179
|
+
def cpu_footer(shots, quality, n_qubits, G_data, G_rows, G_cols, nodes):
|
|
180
|
+
J_eff, degrees = init_J_and_z(G_data, G_rows, G_cols)
|
|
181
|
+
hamming_prob = init_thresholds(n_qubits)
|
|
182
|
+
|
|
183
|
+
maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, hamming_prob)
|
|
184
|
+
|
|
185
|
+
best_solution, best_value = sample_for_solution(G_data, G_rows, G_cols, shots, hamming_prob, J_eff)
|
|
186
|
+
|
|
187
|
+
bit_string, l, r = get_cut(best_solution, nodes)
|
|
188
|
+
|
|
189
|
+
return bit_string, best_value, (l, r)
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
@njit
|
|
193
|
+
def gpu_footer(shots, n_qubits, G_data, G_rows, G_cols, J_eff, hamming_prob, nodes):
|
|
194
|
+
hamming_prob /= hamming_prob.sum()
|
|
195
|
+
tot_prob = 0.0
|
|
196
|
+
for i in range(n_qubits - 1):
|
|
197
|
+
tot_prob += hamming_prob[i]
|
|
198
|
+
hamming_prob[i] = tot_prob
|
|
199
|
+
hamming_prob[-1] = 2.0
|
|
200
|
+
|
|
201
|
+
best_solution, best_value = sample_for_solution(G_data, G_rows, G_cols, shots, hamming_prob, J_eff)
|
|
202
|
+
|
|
203
|
+
bit_string, l, r = get_cut(best_solution, nodes)
|
|
204
|
+
|
|
205
|
+
return bit_string, best_value, (l, r)
|
|
206
|
+
|
|
207
|
+
|
|
178
208
|
def to_scipy_sparse_upper_triangular(G, nodes, n_nodes):
|
|
179
209
|
lil = lil_matrix((n_nodes, n_nodes), dtype=np.float32)
|
|
180
210
|
for u in range(n_nodes):
|
|
@@ -205,18 +235,7 @@ def maxcut_tfim_sparse(
|
|
|
205
235
|
G_m = G
|
|
206
236
|
|
|
207
237
|
if n_qubits < 3:
|
|
208
|
-
|
|
209
|
-
return "", 0, ([], [])
|
|
210
|
-
|
|
211
|
-
if n_qubits == 1:
|
|
212
|
-
return "0", 0, (nodes, [])
|
|
213
|
-
|
|
214
|
-
if n_qubits == 2:
|
|
215
|
-
weight = G_m[0, 1]
|
|
216
|
-
if weight < 0.0:
|
|
217
|
-
return "00", 0, (nodes, [])
|
|
218
|
-
|
|
219
|
-
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
238
|
+
return low_width(G_m, nodes, n_qubits)
|
|
220
239
|
|
|
221
240
|
if quality is None:
|
|
222
241
|
quality = 8
|
|
@@ -228,61 +247,42 @@ def maxcut_tfim_sparse(
|
|
|
228
247
|
n_steps = 1 << quality
|
|
229
248
|
grid_size = n_steps * n_qubits
|
|
230
249
|
|
|
250
|
+
if not (IS_OPENCL_AVAILABLE and grid_size >= 128):
|
|
251
|
+
return cpu_footer(shots, quality, n_qubits, G_m.data, G_m.indptr, G_m.indices, nodes)
|
|
252
|
+
|
|
231
253
|
J_eff, degrees = init_J_and_z(G_m.data, G_m.indptr, G_m.indices)
|
|
232
254
|
hamming_prob = init_thresholds(n_qubits)
|
|
233
255
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
opencl_context.
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
hamming_prob /= hamming_prob.sum()
|
|
268
|
-
tot_prob = 0.0
|
|
269
|
-
for i in range(n_qubits - 1):
|
|
270
|
-
tot_prob += hamming_prob[i]
|
|
271
|
-
hamming_prob[i] = tot_prob
|
|
272
|
-
hamming_prob[-1] = 2.0
|
|
273
|
-
else:
|
|
274
|
-
maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, hamming_prob)
|
|
275
|
-
|
|
276
|
-
best_solution, best_value = sample_for_solution(G_m.data, G_m.indptr, G_m.indices, shots, hamming_prob, J_eff)
|
|
277
|
-
|
|
278
|
-
bit_string = ""
|
|
279
|
-
l, r = [], []
|
|
280
|
-
for i in range(len(best_solution)):
|
|
281
|
-
if best_solution[i]:
|
|
282
|
-
bit_string += "1"
|
|
283
|
-
r.append(nodes[i])
|
|
284
|
-
else:
|
|
285
|
-
bit_string += "0"
|
|
286
|
-
l.append(nodes[i])
|
|
287
|
-
|
|
288
|
-
return bit_string, best_value, (l, r)
|
|
256
|
+
delta_t = 1.0 / n_steps
|
|
257
|
+
tot_t = 2.0 * n_steps * delta_t
|
|
258
|
+
h_mult = 2.0 / tot_t
|
|
259
|
+
theta = init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees)
|
|
260
|
+
args = np.empty(3, dtype=np.float32)
|
|
261
|
+
args[0] = delta_t
|
|
262
|
+
args[1] = tot_t
|
|
263
|
+
args[2] = h_mult
|
|
264
|
+
|
|
265
|
+
# Warp size is 32:
|
|
266
|
+
group_size = n_qubits - 1
|
|
267
|
+
if group_size > 256:
|
|
268
|
+
group_size = 256
|
|
269
|
+
grid_dim = n_steps * n_qubits * group_size
|
|
270
|
+
|
|
271
|
+
# Move to GPU
|
|
272
|
+
mf = cl.mem_flags
|
|
273
|
+
args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
|
|
274
|
+
J_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
|
|
275
|
+
deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
|
|
276
|
+
theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=theta)
|
|
277
|
+
ham_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=hamming_prob)
|
|
278
|
+
|
|
279
|
+
# Kernel execution
|
|
280
|
+
opencl_context.maxcut_hamming_cdf_kernel(
|
|
281
|
+
opencl_context.queue, (grid_dim,), (group_size,),
|
|
282
|
+
np.int32(n_qubits), deg_buf, args_buf, J_buf, theta_buf, ham_buf
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
# Fetch results
|
|
286
|
+
cl.enqueue_copy(opencl_context.queue, hamming_prob, ham_buf)
|
|
287
|
+
|
|
288
|
+
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 init_theta, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
7
|
+
from .maxcut_tfim_util import get_cut, init_theta, init_thresholds, low_width, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
8
8
|
|
|
9
9
|
IS_OPENCL_AVAILABLE = True
|
|
10
10
|
try:
|
|
@@ -135,6 +135,36 @@ def init_J_and_z(G_func, G_func_args_tuple, nodes):
|
|
|
135
135
|
return J_eff, degrees, G_max
|
|
136
136
|
|
|
137
137
|
|
|
138
|
+
@njit
|
|
139
|
+
def cpu_footer(shots, quality, n_qubits, G_func, G_func_args_tuple, nodes):
|
|
140
|
+
J_eff, degrees, G_max = init_J_and_z(G_func, G_func_args_tuple, nodes)
|
|
141
|
+
hamming_prob = init_thresholds(n_qubits)
|
|
142
|
+
|
|
143
|
+
maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, hamming_prob)
|
|
144
|
+
|
|
145
|
+
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)
|
|
146
|
+
|
|
147
|
+
bit_string, l, r = get_cut(best_solution, nodes)
|
|
148
|
+
|
|
149
|
+
return bit_string, best_value, (l, r)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
@njit
|
|
153
|
+
def gpu_footer(shots, n_qubits, G_func, G_func_args_tuple, nodes, G_max, J_eff, degrees, hamming_prob):
|
|
154
|
+
hamming_prob /= hamming_prob.sum()
|
|
155
|
+
tot_prob = 0.0
|
|
156
|
+
for i in range(n_qubits - 1):
|
|
157
|
+
tot_prob += hamming_prob[i]
|
|
158
|
+
hamming_prob[i] = tot_prob
|
|
159
|
+
hamming_prob[-1] = 2.0
|
|
160
|
+
|
|
161
|
+
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)
|
|
162
|
+
|
|
163
|
+
bit_string, l, r = get_cut(best_solution, nodes)
|
|
164
|
+
|
|
165
|
+
return bit_string, best_value, (l, r)
|
|
166
|
+
|
|
167
|
+
|
|
138
168
|
def maxcut_tfim_streaming(
|
|
139
169
|
G_func,
|
|
140
170
|
nodes,
|
|
@@ -145,18 +175,7 @@ def maxcut_tfim_streaming(
|
|
|
145
175
|
n_qubits = len(nodes)
|
|
146
176
|
|
|
147
177
|
if n_qubits < 3:
|
|
148
|
-
|
|
149
|
-
return "", 0, ([], [])
|
|
150
|
-
|
|
151
|
-
if n_qubits == 1:
|
|
152
|
-
return "0", 0, (nodes, [])
|
|
153
|
-
|
|
154
|
-
if n_qubits == 2:
|
|
155
|
-
weight = G_func((nodes[0], nodes[1]), G_func_args_tuple)
|
|
156
|
-
if weight < 0.0:
|
|
157
|
-
return "00", 0, (nodes, [])
|
|
158
|
-
|
|
159
|
-
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
178
|
+
return low_width(G_m, nodes, n_qubits)
|
|
160
179
|
|
|
161
180
|
if quality is None:
|
|
162
181
|
quality = 8
|
|
@@ -168,61 +187,42 @@ def maxcut_tfim_streaming(
|
|
|
168
187
|
n_steps = 1 << quality
|
|
169
188
|
grid_size = n_steps * n_qubits
|
|
170
189
|
|
|
190
|
+
if not (IS_OPENCL_AVAILABLE and grid_size >= 128):
|
|
191
|
+
return cpu_footer(shots, quality, n_qubits, G_func, G_func_args_tuple, nodes)
|
|
192
|
+
|
|
171
193
|
J_eff, degrees, G_max = init_J_and_z(G_func, G_func_args_tuple, nodes)
|
|
172
194
|
hamming_prob = init_thresholds(n_qubits)
|
|
173
195
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
opencl_context.
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
hamming_prob /= hamming_prob.sum()
|
|
208
|
-
tot_prob = 0.0
|
|
209
|
-
for i in range(n_qubits - 1):
|
|
210
|
-
tot_prob += hamming_prob[i]
|
|
211
|
-
hamming_prob[i] = tot_prob
|
|
212
|
-
hamming_prob[-1] = 2.0
|
|
213
|
-
else:
|
|
214
|
-
maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, hamming_prob)
|
|
215
|
-
|
|
216
|
-
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)
|
|
217
|
-
|
|
218
|
-
bit_string = ""
|
|
219
|
-
l, r = [], []
|
|
220
|
-
for i in range(len(best_solution)):
|
|
221
|
-
if best_solution[i]:
|
|
222
|
-
bit_string += "1"
|
|
223
|
-
r.append(nodes[i])
|
|
224
|
-
else:
|
|
225
|
-
bit_string += "0"
|
|
226
|
-
l.append(nodes[i])
|
|
227
|
-
|
|
228
|
-
return bit_string, best_value, (l, r)
|
|
196
|
+
delta_t = 1.0 / n_steps
|
|
197
|
+
tot_t = 2.0 * n_steps * delta_t
|
|
198
|
+
h_mult = 2.0 / tot_t
|
|
199
|
+
theta = init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees)
|
|
200
|
+
args = np.empty(3, dtype=np.float32)
|
|
201
|
+
args[0] = delta_t
|
|
202
|
+
args[1] = tot_t
|
|
203
|
+
args[2] = h_mult
|
|
204
|
+
|
|
205
|
+
# Warp size is 32:
|
|
206
|
+
group_size = n_qubits - 1
|
|
207
|
+
if group_size > 256:
|
|
208
|
+
group_size = 256
|
|
209
|
+
grid_dim = n_steps * n_qubits * group_size
|
|
210
|
+
|
|
211
|
+
# Move to GPU
|
|
212
|
+
mf = cl.mem_flags
|
|
213
|
+
args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
|
|
214
|
+
J_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
|
|
215
|
+
deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
|
|
216
|
+
theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=theta)
|
|
217
|
+
ham_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=hamming_prob)
|
|
218
|
+
|
|
219
|
+
# Kernel execution
|
|
220
|
+
opencl_context.maxcut_hamming_cdf_kernel(
|
|
221
|
+
opencl_context.queue, (grid_dim,), (group_size,),
|
|
222
|
+
np.int32(n_qubits), deg_buf, args_buf, J_buf, theta_buf, ham_buf
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
# Fetch results
|
|
226
|
+
cl.enqueue_copy(opencl_context.queue, hamming_prob, ham_buf)
|
|
227
|
+
|
|
228
|
+
return gpu_footer(shots, n_qubits, G_func, G_func_args_tuple, nodes, G_max, J_eff, degrees, hamming_prob)
|
|
@@ -5,6 +5,21 @@ import os
|
|
|
5
5
|
from numba import njit, prange
|
|
6
6
|
|
|
7
7
|
|
|
8
|
+
@njit
|
|
9
|
+
def get_cut(solution, nodes):
|
|
10
|
+
bit_string = ""
|
|
11
|
+
l, r = [], []
|
|
12
|
+
for i in range(len(solution)):
|
|
13
|
+
if solution[i]:
|
|
14
|
+
bit_string += "1"
|
|
15
|
+
r.append(nodes[i])
|
|
16
|
+
else:
|
|
17
|
+
bit_string += "0"
|
|
18
|
+
l.append(nodes[i])
|
|
19
|
+
|
|
20
|
+
return bit_string, l, r
|
|
21
|
+
|
|
22
|
+
|
|
8
23
|
@njit(parallel=True)
|
|
9
24
|
def init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees):
|
|
10
25
|
theta = np.empty(n_qubits, dtype=np.float32)
|
|
@@ -45,6 +60,22 @@ def init_thresholds(n_qubits):
|
|
|
45
60
|
return thresholds
|
|
46
61
|
|
|
47
62
|
|
|
63
|
+
@njit
|
|
64
|
+
def low_width(G_m, nodes, n_qubits):
|
|
65
|
+
if n_qubits == 0:
|
|
66
|
+
return "", 0, ([], [])
|
|
67
|
+
|
|
68
|
+
if n_qubits == 1:
|
|
69
|
+
return "0", 0, (nodes, [])
|
|
70
|
+
|
|
71
|
+
if n_qubits == 2:
|
|
72
|
+
weight = G_m[0, 1]
|
|
73
|
+
if weight < 0.0:
|
|
74
|
+
return "00", 0, (nodes, [])
|
|
75
|
+
|
|
76
|
+
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
77
|
+
|
|
78
|
+
|
|
48
79
|
@njit(parallel=True)
|
|
49
80
|
def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, hamming_prob):
|
|
50
81
|
if n_qubits < 2:
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='pyqrackising',
|
|
54
|
-
version='5.3.
|
|
54
|
+
version='5.3.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)',
|
|
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
|