PyQrackIsing 9.10.0__tar.gz → 9.11.0__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-9.10.0/pyqrackising.egg-info → pyqrackising-9.11.0}/PKG-INFO +5 -1
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/pyproject.toml +8 -5
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/pyqrackising/__init__.py +1 -1
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/pyqrackising/convert_tensor_network_to_tsp.py +1 -5
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/pyqrackising/generate_tfim_samples.py +14 -7
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/pyqrackising/maxcut_tfim.py +66 -12
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/pyqrackising/maxcut_tfim_sparse.py +90 -14
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/pyqrackising/maxcut_tfim_streaming.py +18 -5
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/pyqrackising/maxcut_tfim_util.py +72 -37
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/pyqrackising/otoc.py +20 -10
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/pyqrackising/spin_glass_solver.py +125 -22
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/pyqrackising/spin_glass_solver_sparse.py +184 -27
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/pyqrackising/spin_glass_solver_streaming.py +25 -3
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/pyqrackising/tsp.py +371 -183
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/pyqrackising/tsp_maxcut.py +7 -7
- {pyqrackising-9.10.0 → pyqrackising-9.11.0/pyqrackising.egg-info}/PKG-INFO +5 -1
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/pyqrackising.egg-info/SOURCES.txt +1 -0
- pyqrackising-9.11.0/pyqrackising.egg-info/requires.txt +4 -0
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/setup.py +8 -8
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/LICENSE.md +0 -0
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/MANIFEST.in +0 -0
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/README.md +0 -0
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-9.10.0 → pyqrackising-9.11.0}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pyqrackising
|
|
3
|
-
Version: 9.
|
|
3
|
+
Version: 9.11.0
|
|
4
4
|
Summary: Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)
|
|
5
5
|
Home-page: https://github.com/vm6502q/PyQrackIsing
|
|
6
6
|
Author: Dan Strano
|
|
@@ -22,6 +22,10 @@ Classifier: Topic :: Scientific/Engineering
|
|
|
22
22
|
Requires-Python: >=3.8
|
|
23
23
|
Description-Content-Type: text/markdown
|
|
24
24
|
License-File: LICENSE.md
|
|
25
|
+
Requires-Dist: networkx
|
|
26
|
+
Requires-Dist: numpy
|
|
27
|
+
Requires-Dist: numba
|
|
28
|
+
Requires-Dist: scipy
|
|
25
29
|
Dynamic: author
|
|
26
30
|
Dynamic: home-page
|
|
27
31
|
Dynamic: license-file
|
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
[build-system]
|
|
2
2
|
requires = [
|
|
3
3
|
"setuptools",
|
|
4
|
-
"
|
|
5
|
-
"numpy",
|
|
6
|
-
"numba",
|
|
7
|
-
"scipy"
|
|
4
|
+
"wheel"
|
|
8
5
|
]
|
|
9
6
|
build-backend = "setuptools.build_meta"
|
|
10
7
|
|
|
11
8
|
[project]
|
|
12
9
|
name = "pyqrackising"
|
|
13
|
-
version = "9.
|
|
10
|
+
version = "9.11.0"
|
|
14
11
|
requires-python = ">=3.8"
|
|
15
12
|
description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
|
|
16
13
|
readme = {file = "README.txt", content-type = "text/markdown"}
|
|
@@ -31,6 +28,12 @@ classifiers=[
|
|
|
31
28
|
"Programming Language :: Python :: 3.10",
|
|
32
29
|
"Topic :: Scientific/Engineering",
|
|
33
30
|
]
|
|
31
|
+
dependencies=[
|
|
32
|
+
"networkx",
|
|
33
|
+
"numpy",
|
|
34
|
+
"numba",
|
|
35
|
+
"scipy"
|
|
36
|
+
]
|
|
34
37
|
|
|
35
38
|
[project.urls]
|
|
36
39
|
Homepage = "https://github.com/vm6502q/PyQrackIsing"
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from .generate_tfim_samples import generate_tfim_samples, get_tfim_hamming_distribution
|
|
1
|
+
from .generate_tfim_samples import generate_tfim_samples, get_tfim_hamming_distribution, generate_fermi_hubbard_samples
|
|
2
2
|
from .tfim_magnetization import tfim_magnetization
|
|
3
3
|
from .tfim_square_magnetization import tfim_square_magnetization
|
|
4
4
|
from .otoc import generate_otoc_samples, get_otoc_hamming_distribution
|
|
@@ -68,7 +68,6 @@ def convert_tensor_network_to_tsp(tensors, index_dims, dtype=np.float32):
|
|
|
68
68
|
return dist_matrix, tensor_ids
|
|
69
69
|
|
|
70
70
|
|
|
71
|
-
|
|
72
71
|
def convert_quimb_tree_to_tsp(tn, dtype=np.float32):
|
|
73
72
|
"""
|
|
74
73
|
Converts a quimb TensorNetwork to a TSP distance matrix for contraction optimization.
|
|
@@ -86,10 +85,7 @@ def convert_quimb_tree_to_tsp(tn, dtype=np.float32):
|
|
|
86
85
|
for tensor in tn.tensors:
|
|
87
86
|
# Use a unique tag or fallback to name
|
|
88
87
|
tensor_id = frozenset(tensor.tags) if tensor.tags else str(tensor.name)
|
|
89
|
-
tensors.append({
|
|
90
|
-
"id": tensor_id,
|
|
91
|
-
"indices": set(tensor.inds)
|
|
92
|
-
})
|
|
88
|
+
tensors.append({"id": tensor_id, "indices": set(tensor.inds)})
|
|
93
89
|
|
|
94
90
|
for ind, dim in zip(tensor.inds, tensor.shape):
|
|
95
91
|
index_dims[ind] = dim # capture dimension size
|
|
@@ -95,6 +95,7 @@ def sample_hamming_weight(thresholds, shots):
|
|
|
95
95
|
|
|
96
96
|
return hamming_samples
|
|
97
97
|
|
|
98
|
+
|
|
98
99
|
@njit
|
|
99
100
|
def fix_cdf(hamming_prob):
|
|
100
101
|
tot_prob = 0.0
|
|
@@ -109,12 +110,12 @@ def fix_cdf(hamming_prob):
|
|
|
109
110
|
|
|
110
111
|
|
|
111
112
|
@njit
|
|
112
|
-
def get_tfim_hamming_distribution(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, omega=1.5*np.pi):
|
|
113
|
+
def get_tfim_hamming_distribution(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, omega=1.5 * np.pi):
|
|
113
114
|
if abs(t) <= epsilon:
|
|
114
115
|
p = (1.0 - np.cos(theta)) / 2.0
|
|
115
116
|
bias = np.empty(n_qubits + 1, dtype=np.float64)
|
|
116
117
|
for k in range(n_qubits + 1):
|
|
117
|
-
bias[k] = comb(n_qubits, k) * (p
|
|
118
|
+
bias[k] = comb(n_qubits, k) * (p**k) * ((1.0 - p) ** (n_qubits - k))
|
|
118
119
|
|
|
119
120
|
return bias / bias.sum()
|
|
120
121
|
|
|
@@ -131,9 +132,7 @@ def get_tfim_hamming_distribution(J=-1.0, h=2.0, z=4, theta=0.174532925199432957
|
|
|
131
132
|
return bias / bias.sum()
|
|
132
133
|
|
|
133
134
|
|
|
134
|
-
def generate_tfim_samples(
|
|
135
|
-
J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, shots=100, omega=1.5*np.pi
|
|
136
|
-
):
|
|
135
|
+
def generate_tfim_samples(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, shots=100, omega=1.5 * np.pi):
|
|
137
136
|
samples = []
|
|
138
137
|
|
|
139
138
|
if abs(t) <= epsilon:
|
|
@@ -179,7 +178,7 @@ def generate_tfim_samples(
|
|
|
179
178
|
# Use a normalized weighted average that favors the (n+1)-dimensional model at later times.
|
|
180
179
|
# The (n+1)-dimensional marginal probability is the product of a function of Hamming weight and "closeness," split among all basis states with that specific Hamming weight.
|
|
181
180
|
tot_prob += normed_closeness / h_weight_combos
|
|
182
|
-
while
|
|
181
|
+
while rands[s] <= tot_prob:
|
|
183
182
|
samples.append(state_int)
|
|
184
183
|
s += 1
|
|
185
184
|
if s == count:
|
|
@@ -189,7 +188,15 @@ def generate_tfim_samples(
|
|
|
189
188
|
if s < count:
|
|
190
189
|
samples += (count - s) * [state_int]
|
|
191
190
|
|
|
192
|
-
|
|
193
191
|
np.random.shuffle(samples)
|
|
194
192
|
|
|
195
193
|
return samples
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def generate_fermi_hubbard_samples(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, shots=100, omega=1.5 * np.pi):
|
|
197
|
+
shots_x, shots_y, shots_z = np.random.multinomial(shots, [1 / 3, 1 / 3, 1 / 3])
|
|
198
|
+
return (
|
|
199
|
+
generate_tfim_samples(J=J, h=h, z=z, theta=theta, t=t, n_qubits=n_qubits, shots=shots_z)
|
|
200
|
+
+ generate_tfim_samples(J=-h, h=-J, z=z, theta=theta + np.pi / 2, t=t, n_qubits=n_qubits, shots=shots_x)
|
|
201
|
+
+ generate_tfim_samples(J=J, h=h, z=z, theta=theta + np.pi / 2, t=t, n_qubits=n_qubits, shots=shots_y)
|
|
202
|
+
)
|
|
@@ -3,7 +3,23 @@ import numpy as np
|
|
|
3
3
|
from numba import njit, prange
|
|
4
4
|
import os
|
|
5
5
|
|
|
6
|
-
from .maxcut_tfim_util import
|
|
6
|
+
from .maxcut_tfim_util import (
|
|
7
|
+
compute_cut,
|
|
8
|
+
compute_energy,
|
|
9
|
+
compute_cut_diff_between,
|
|
10
|
+
convert_bool_to_uint,
|
|
11
|
+
get_cut,
|
|
12
|
+
get_cut_base,
|
|
13
|
+
heuristic_threshold,
|
|
14
|
+
init_thresholds,
|
|
15
|
+
make_G_m_buf,
|
|
16
|
+
make_theta_buf,
|
|
17
|
+
maxcut_hamming_cdf,
|
|
18
|
+
opencl_context,
|
|
19
|
+
sample_mag,
|
|
20
|
+
setup_opencl,
|
|
21
|
+
bit_pick,
|
|
22
|
+
)
|
|
7
23
|
|
|
8
24
|
IS_OPENCL_AVAILABLE = True
|
|
9
25
|
try:
|
|
@@ -41,7 +57,7 @@ def local_repulsion_choice(G_m, repulsion_base, n, m, s):
|
|
|
41
57
|
- After choosing a node, its neighbors' probabilities are further reduced
|
|
42
58
|
"""
|
|
43
59
|
|
|
44
|
-
used = np.zeros(n, dtype=np.bool_)
|
|
60
|
+
used = np.zeros(n, dtype=np.bool_) # False = available, True = used
|
|
45
61
|
|
|
46
62
|
# First bit:
|
|
47
63
|
node = s % n
|
|
@@ -133,7 +149,17 @@ def shot_loop(G_m, thresholds, repulsion_base, n, shots, solutions):
|
|
|
133
149
|
solutions[s] = local_repulsion_choice(G_m, repulsion_base, n, m, s)
|
|
134
150
|
|
|
135
151
|
|
|
136
|
-
def sample_for_opencl(
|
|
152
|
+
def sample_for_opencl(
|
|
153
|
+
G_m,
|
|
154
|
+
G_m_buf,
|
|
155
|
+
shots,
|
|
156
|
+
thresholds,
|
|
157
|
+
repulsion_base,
|
|
158
|
+
is_spin_glass,
|
|
159
|
+
is_segmented,
|
|
160
|
+
segment_size,
|
|
161
|
+
theta_segment_size,
|
|
162
|
+
):
|
|
137
163
|
shots = ((max(1, shots >> 1) + 31) >> 5) << 5
|
|
138
164
|
n = len(G_m)
|
|
139
165
|
|
|
@@ -142,7 +168,11 @@ def sample_for_opencl(G_m, G_m_buf, shots, thresholds, repulsion_base, is_spin_g
|
|
|
142
168
|
best_solution = np.zeros(n, dtype=np.bool_)
|
|
143
169
|
best_energy = compute_energy(best_solution, G_m, n) if is_spin_glass else 0.0
|
|
144
170
|
|
|
145
|
-
opencl_args = setup_opencl(
|
|
171
|
+
opencl_args = setup_opencl(
|
|
172
|
+
shots,
|
|
173
|
+
shots,
|
|
174
|
+
np.array([n, shots, is_spin_glass, segment_size, theta_segment_size], dtype=np.int32),
|
|
175
|
+
)
|
|
146
176
|
|
|
147
177
|
improved = True
|
|
148
178
|
while improved:
|
|
@@ -183,7 +213,21 @@ def init_J_and_z(G_m, repulsion_base):
|
|
|
183
213
|
return J_eff, degrees
|
|
184
214
|
|
|
185
215
|
|
|
186
|
-
def run_cut_opencl(
|
|
216
|
+
def run_cut_opencl(
|
|
217
|
+
best_energy,
|
|
218
|
+
samples,
|
|
219
|
+
G_m_buf,
|
|
220
|
+
is_segmented,
|
|
221
|
+
local_size,
|
|
222
|
+
global_size,
|
|
223
|
+
args_buf,
|
|
224
|
+
local_energy_buf,
|
|
225
|
+
local_index_buf,
|
|
226
|
+
max_energy_host,
|
|
227
|
+
max_index_host,
|
|
228
|
+
max_energy_buf,
|
|
229
|
+
max_index_buf,
|
|
230
|
+
):
|
|
187
231
|
queue = opencl_context.queue
|
|
188
232
|
calculate_cut_kernel = opencl_context.calculate_cut_segmented_kernel if is_segmented else opencl_context.calculate_cut_kernel
|
|
189
233
|
|
|
@@ -209,7 +253,7 @@ def run_cut_opencl(best_energy, samples, G_m_buf, is_segmented, local_size, glob
|
|
|
209
253
|
max_energy_buf,
|
|
210
254
|
max_index_buf,
|
|
211
255
|
local_energy_buf,
|
|
212
|
-
local_index_buf
|
|
256
|
+
local_index_buf,
|
|
213
257
|
)
|
|
214
258
|
else:
|
|
215
259
|
calculate_cut_kernel.set_args(
|
|
@@ -219,7 +263,7 @@ def run_cut_opencl(best_energy, samples, G_m_buf, is_segmented, local_size, glob
|
|
|
219
263
|
max_energy_buf,
|
|
220
264
|
max_index_buf,
|
|
221
265
|
local_energy_buf,
|
|
222
|
-
local_index_buf
|
|
266
|
+
local_index_buf,
|
|
223
267
|
)
|
|
224
268
|
|
|
225
269
|
cl.enqueue_nd_range_kernel(queue, calculate_cut_kernel, (global_size,), (local_size,))
|
|
@@ -308,13 +352,13 @@ def maxcut_tfim(
|
|
|
308
352
|
anneal_h=None,
|
|
309
353
|
repulsion_base=None,
|
|
310
354
|
is_maxcut_gpu=True,
|
|
311
|
-
is_nested=False
|
|
355
|
+
is_nested=False,
|
|
312
356
|
):
|
|
313
357
|
nodes = None
|
|
314
358
|
G_m = None
|
|
315
359
|
if isinstance(G, nx.Graph):
|
|
316
360
|
nodes = list(G.nodes())
|
|
317
|
-
G_m = nx.to_numpy_array(G, weight=
|
|
361
|
+
G_m = nx.to_numpy_array(G, weight="weight", nonedge=0.0, dtype=dtype)
|
|
318
362
|
else:
|
|
319
363
|
nodes = list(range(len(G)))
|
|
320
364
|
G_m = G
|
|
@@ -327,7 +371,7 @@ def maxcut_tfim(
|
|
|
327
371
|
|
|
328
372
|
if best_value < 0.0:
|
|
329
373
|
# Best cut is trivial partition, all/empty
|
|
330
|
-
return
|
|
374
|
+
return "0" * n_qubits, 0.0, (nodes, [])
|
|
331
375
|
|
|
332
376
|
return bit_string, best_value, (l, r)
|
|
333
377
|
|
|
@@ -358,7 +402,17 @@ def maxcut_tfim(
|
|
|
358
402
|
theta_segment_size = (((n_qubits + 31) >> 5) * (((shots + wgs - 1) // wgs) * wgs) + 3) >> 2
|
|
359
403
|
is_segmented = ((G_m.nbytes << 1) > opencl_context.max_alloc) or ((theta_segment_size << 3) > opencl_context.max_alloc)
|
|
360
404
|
G_m_buf = make_G_m_buf(G_m, is_segmented, segment_size)
|
|
361
|
-
best_solution, best_value = sample_for_opencl(
|
|
405
|
+
best_solution, best_value = sample_for_opencl(
|
|
406
|
+
G_m,
|
|
407
|
+
G_m_buf,
|
|
408
|
+
shots,
|
|
409
|
+
cum_prob,
|
|
410
|
+
repulsion_base,
|
|
411
|
+
is_spin_glass,
|
|
412
|
+
is_segmented,
|
|
413
|
+
segment_size,
|
|
414
|
+
theta_segment_size,
|
|
415
|
+
)
|
|
362
416
|
else:
|
|
363
417
|
thread_count = os.cpu_count() ** 2
|
|
364
418
|
best_solution, best_value = sample_measurement(G_m, shots, thread_count, cum_prob, repulsion_base, is_spin_glass)
|
|
@@ -367,6 +421,6 @@ def maxcut_tfim(
|
|
|
367
421
|
|
|
368
422
|
if best_value < 0.0:
|
|
369
423
|
# Best cut is trivial partition, all/empty
|
|
370
|
-
return
|
|
424
|
+
return "0" * n_qubits, 0.0, (nodes, [])
|
|
371
425
|
|
|
372
426
|
return bit_string, best_value, (l, r)
|
|
@@ -3,7 +3,24 @@ import numpy as np
|
|
|
3
3
|
from numba import njit, prange
|
|
4
4
|
import os
|
|
5
5
|
|
|
6
|
-
from .maxcut_tfim_util import
|
|
6
|
+
from .maxcut_tfim_util import (
|
|
7
|
+
binary_search,
|
|
8
|
+
compute_cut_sparse,
|
|
9
|
+
compute_energy_sparse,
|
|
10
|
+
convert_bool_to_uint,
|
|
11
|
+
get_cut,
|
|
12
|
+
get_cut_base,
|
|
13
|
+
heuristic_threshold_sparse,
|
|
14
|
+
init_thresholds,
|
|
15
|
+
make_G_m_csr_buf,
|
|
16
|
+
make_theta_buf,
|
|
17
|
+
maxcut_hamming_cdf,
|
|
18
|
+
opencl_context,
|
|
19
|
+
sample_mag,
|
|
20
|
+
setup_opencl,
|
|
21
|
+
bit_pick,
|
|
22
|
+
to_scipy_sparse_upper_triangular,
|
|
23
|
+
)
|
|
7
24
|
|
|
8
25
|
IS_OPENCL_AVAILABLE = True
|
|
9
26
|
try:
|
|
@@ -51,7 +68,7 @@ def local_repulsion_choice(G_data, G_rows, G_cols, repulsion_base, n, m, s):
|
|
|
51
68
|
- After choosing a node, its neighbors' probabilities are further reduced
|
|
52
69
|
"""
|
|
53
70
|
|
|
54
|
-
used = np.zeros(n, dtype=np.bool_)
|
|
71
|
+
used = np.zeros(n, dtype=np.bool_) # False = available, True = used
|
|
55
72
|
|
|
56
73
|
# First bit:
|
|
57
74
|
node = s % n
|
|
@@ -140,7 +157,21 @@ def shot_loop(G_data, G_rows, G_cols, thresholds, repulsion_base, n, shots, solu
|
|
|
140
157
|
solutions[s] = local_repulsion_choice(G_data, G_rows, G_cols, repulsion_base, n, m, s)
|
|
141
158
|
|
|
142
159
|
|
|
143
|
-
def sample_for_opencl(
|
|
160
|
+
def sample_for_opencl(
|
|
161
|
+
G_data,
|
|
162
|
+
G_rows,
|
|
163
|
+
G_cols,
|
|
164
|
+
G_data_buf,
|
|
165
|
+
G_rows_buf,
|
|
166
|
+
G_cols_buf,
|
|
167
|
+
shots,
|
|
168
|
+
thresholds,
|
|
169
|
+
repulsion_base,
|
|
170
|
+
is_spin_glass,
|
|
171
|
+
is_segmented,
|
|
172
|
+
segment_size,
|
|
173
|
+
theta_segment_size,
|
|
174
|
+
):
|
|
144
175
|
shots = ((max(1, shots >> 1) + 31) >> 5) << 5
|
|
145
176
|
n = G_rows.shape[0] - 1
|
|
146
177
|
|
|
@@ -149,7 +180,11 @@ def sample_for_opencl(G_data, G_rows, G_cols, G_data_buf, G_rows_buf, G_cols_buf
|
|
|
149
180
|
best_solution = np.zeros(n, dtype=np.bool_)
|
|
150
181
|
best_energy = compute_energy(best_solution, G_m, n) if is_spin_glass else 0.0
|
|
151
182
|
|
|
152
|
-
opencl_args = setup_opencl(
|
|
183
|
+
opencl_args = setup_opencl(
|
|
184
|
+
shots,
|
|
185
|
+
shots,
|
|
186
|
+
np.array([n, shots, is_spin_glass, segment_size, theta_segment_size], dtype=np.int32),
|
|
187
|
+
)
|
|
153
188
|
|
|
154
189
|
improved = True
|
|
155
190
|
while improved:
|
|
@@ -162,7 +197,7 @@ def sample_for_opencl(G_data, G_rows, G_cols, G_data_buf, G_rows_buf, G_cols_buf
|
|
|
162
197
|
improved = True
|
|
163
198
|
|
|
164
199
|
if is_spin_glass:
|
|
165
|
-
best_energy = compute_cut_sparse(best_solution, G_data, G_rows, G_cols, n)
|
|
200
|
+
best_energy = compute_cut_sparse(best_solution, G_data, G_rows, G_cols, n)
|
|
166
201
|
|
|
167
202
|
return best_solution, float(best_energy)
|
|
168
203
|
|
|
@@ -190,7 +225,23 @@ def init_J_and_z(G_m, repulsion_base):
|
|
|
190
225
|
return J_eff, degrees
|
|
191
226
|
|
|
192
227
|
|
|
193
|
-
def run_cut_opencl(
|
|
228
|
+
def run_cut_opencl(
|
|
229
|
+
best_energy,
|
|
230
|
+
samples,
|
|
231
|
+
G_data_buf,
|
|
232
|
+
G_rows_buf,
|
|
233
|
+
G_cols_buf,
|
|
234
|
+
is_segmented,
|
|
235
|
+
local_size,
|
|
236
|
+
global_size,
|
|
237
|
+
args_buf,
|
|
238
|
+
local_energy_buf,
|
|
239
|
+
local_index_buf,
|
|
240
|
+
max_energy_host,
|
|
241
|
+
max_index_host,
|
|
242
|
+
max_energy_buf,
|
|
243
|
+
max_index_buf,
|
|
244
|
+
):
|
|
194
245
|
queue = opencl_context.queue
|
|
195
246
|
calculate_cut_kernel = opencl_context.calculate_cut_sparse_segmented_kernel if is_segmented else opencl_context.calculate_cut_sparse_kernel
|
|
196
247
|
|
|
@@ -218,7 +269,7 @@ def run_cut_opencl(best_energy, samples, G_data_buf, G_rows_buf, G_cols_buf, is_
|
|
|
218
269
|
max_energy_buf,
|
|
219
270
|
max_index_buf,
|
|
220
271
|
local_energy_buf,
|
|
221
|
-
local_index_buf
|
|
272
|
+
local_index_buf,
|
|
222
273
|
)
|
|
223
274
|
else:
|
|
224
275
|
calculate_cut_kernel.set_args(
|
|
@@ -230,7 +281,7 @@ def run_cut_opencl(best_energy, samples, G_data_buf, G_rows_buf, G_cols_buf, is_
|
|
|
230
281
|
max_energy_buf,
|
|
231
282
|
max_index_buf,
|
|
232
283
|
local_energy_buf,
|
|
233
|
-
local_index_buf
|
|
284
|
+
local_index_buf,
|
|
234
285
|
)
|
|
235
286
|
|
|
236
287
|
cl.enqueue_nd_range_kernel(queue, calculate_cut_kernel, (global_size,), (local_size,))
|
|
@@ -329,7 +380,7 @@ def maxcut_tfim_sparse(
|
|
|
329
380
|
anneal_h=None,
|
|
330
381
|
repulsion_base=None,
|
|
331
382
|
is_maxcut_gpu=True,
|
|
332
|
-
is_nested=False
|
|
383
|
+
is_nested=False,
|
|
333
384
|
):
|
|
334
385
|
wgs = opencl_context.work_group_size
|
|
335
386
|
nodes = None
|
|
@@ -345,12 +396,14 @@ def maxcut_tfim_sparse(
|
|
|
345
396
|
G_m = G
|
|
346
397
|
|
|
347
398
|
if n_qubits < heuristic_threshold_sparse:
|
|
348
|
-
best_solution, best_value =
|
|
399
|
+
best_solution, best_value = (
|
|
400
|
+
exact_spin_glass(G_m.data, G_m.indptr, G_m.indices) if is_spin_glass else exact_maxcut(G_m.data, G_m.indptr, G_m.indices)
|
|
401
|
+
)
|
|
349
402
|
bit_string, l, r = get_cut(best_solution, nodes, n_qubits)
|
|
350
403
|
|
|
351
404
|
if best_value < 0.0:
|
|
352
405
|
# Best cut is trivial partition, all/empty
|
|
353
|
-
return
|
|
406
|
+
return "0" * n_qubits, 0.0, (nodes, [])
|
|
354
407
|
|
|
355
408
|
return bit_string, best_value, (l, r)
|
|
356
409
|
|
|
@@ -383,15 +436,38 @@ def maxcut_tfim_sparse(
|
|
|
383
436
|
theta_segment_size = (((n_qubits + 31) >> 5) * (((shots + wgs - 1) // wgs) * wgs) + 3) >> 2
|
|
384
437
|
is_segmented = (G_m.data.nbytes << 1) > opencl_context.max_alloc or ((theta_segment_size << 3) > opencl_context.max_alloc)
|
|
385
438
|
G_data_buf, G_rows_buf, G_cols_buf = make_G_m_csr_buf(G_m, is_segmented, segment_size)
|
|
386
|
-
best_solution, best_value = sample_for_opencl(
|
|
439
|
+
best_solution, best_value = sample_for_opencl(
|
|
440
|
+
G_m.data,
|
|
441
|
+
G_m.indptr,
|
|
442
|
+
G_m.indices,
|
|
443
|
+
G_data_buf,
|
|
444
|
+
G_rows_buf,
|
|
445
|
+
G_cols_buf,
|
|
446
|
+
shots,
|
|
447
|
+
cum_prob,
|
|
448
|
+
repulsion_base,
|
|
449
|
+
is_spin_glass,
|
|
450
|
+
is_segmented,
|
|
451
|
+
segment_size,
|
|
452
|
+
theta_segment_size,
|
|
453
|
+
)
|
|
387
454
|
else:
|
|
388
455
|
thread_count = os.cpu_count() ** 2
|
|
389
|
-
best_solution, best_value = sample_measurement(
|
|
456
|
+
best_solution, best_value = sample_measurement(
|
|
457
|
+
G_m.data,
|
|
458
|
+
G_m.indptr,
|
|
459
|
+
G_m.indices,
|
|
460
|
+
shots,
|
|
461
|
+
thread_count,
|
|
462
|
+
cum_prob,
|
|
463
|
+
repulsion_base,
|
|
464
|
+
is_spin_glass,
|
|
465
|
+
)
|
|
390
466
|
|
|
391
467
|
bit_string, l, r = get_cut(best_solution, nodes, n_qubits)
|
|
392
468
|
|
|
393
469
|
if best_value < 0.0:
|
|
394
470
|
# Best cut is trivial partition, all/empty
|
|
395
|
-
return
|
|
471
|
+
return "0" * n_qubits, 0.0, (nodes, [])
|
|
396
472
|
|
|
397
473
|
return bit_string, best_value, (l, r)
|
|
@@ -3,7 +3,19 @@ import numpy as np
|
|
|
3
3
|
from numba import njit, prange
|
|
4
4
|
import os
|
|
5
5
|
|
|
6
|
-
from .maxcut_tfim_util import
|
|
6
|
+
from .maxcut_tfim_util import (
|
|
7
|
+
compute_cut_streaming,
|
|
8
|
+
compute_energy_streaming,
|
|
9
|
+
compute_cut_diff_between_streaming,
|
|
10
|
+
get_cut,
|
|
11
|
+
get_cut_base,
|
|
12
|
+
heuristic_threshold,
|
|
13
|
+
init_thresholds,
|
|
14
|
+
maxcut_hamming_cdf,
|
|
15
|
+
opencl_context,
|
|
16
|
+
sample_mag,
|
|
17
|
+
bit_pick,
|
|
18
|
+
)
|
|
7
19
|
|
|
8
20
|
|
|
9
21
|
epsilon = opencl_context.epsilon
|
|
@@ -34,7 +46,7 @@ def local_repulsion_choice(G_func, nodes, repulsion_base, n, m, s):
|
|
|
34
46
|
- After choosing a node, its neighbors' probabilities are further reduced
|
|
35
47
|
"""
|
|
36
48
|
|
|
37
|
-
used = np.zeros(n, dtype=np.bool_)
|
|
49
|
+
used = np.zeros(n, dtype=np.bool_) # False = available, True = used
|
|
38
50
|
|
|
39
51
|
# First bit:
|
|
40
52
|
node = s % n
|
|
@@ -137,6 +149,7 @@ def init_J_and_z(G_func, nodes, G_min, repulsion_base):
|
|
|
137
149
|
|
|
138
150
|
return J_eff, degrees
|
|
139
151
|
|
|
152
|
+
|
|
140
153
|
@njit
|
|
141
154
|
def find_G_min(G_func, nodes, n_nodes):
|
|
142
155
|
G_min = float("inf")
|
|
@@ -211,7 +224,7 @@ def maxcut_tfim_streaming(
|
|
|
211
224
|
is_spin_glass=False,
|
|
212
225
|
anneal_t=None,
|
|
213
226
|
anneal_h=None,
|
|
214
|
-
repulsion_base=None
|
|
227
|
+
repulsion_base=None,
|
|
215
228
|
):
|
|
216
229
|
wgs = opencl_context.work_group_size
|
|
217
230
|
n_qubits = len(nodes)
|
|
@@ -222,7 +235,7 @@ def maxcut_tfim_streaming(
|
|
|
222
235
|
|
|
223
236
|
if best_value < 0.0:
|
|
224
237
|
# Best cut is trivial partition, all/empty
|
|
225
|
-
return
|
|
238
|
+
return "0" * n_qubits, 0.0, (nodes, [])
|
|
226
239
|
|
|
227
240
|
return bit_string, best_value, (l, r)
|
|
228
241
|
|
|
@@ -258,6 +271,6 @@ def maxcut_tfim_streaming(
|
|
|
258
271
|
|
|
259
272
|
if best_value < 0.0:
|
|
260
273
|
# Best cut is trivial partition, all/empty
|
|
261
|
-
return
|
|
274
|
+
return "0" * n_qubits, 0.0, (nodes, [])
|
|
262
275
|
|
|
263
276
|
return bit_string, best_value, (l, r)
|