PyQrackIsing 5.1.4__tar.gz → 5.1.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.1.4/pyqrackising.egg-info → pyqrackising-5.1.6}/PKG-INFO +1 -1
- {pyqrackising-5.1.4 → pyqrackising-5.1.6}/pyproject.toml +1 -1
- {pyqrackising-5.1.4 → pyqrackising-5.1.6}/pyqrackising/maxcut_tfim_sparse.py +1 -2
- pyqrackising-5.1.6/pyqrackising/maxcut_tfim_sparse_uniform.py +273 -0
- pyqrackising-5.1.6/pyqrackising/maxcut_tfim_streaming_uniform.py +228 -0
- {pyqrackising-5.1.4 → pyqrackising-5.1.6}/pyqrackising/maxcut_tfim_util.py +2 -0
- {pyqrackising-5.1.4 → pyqrackising-5.1.6/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-5.1.4 → pyqrackising-5.1.6}/pyqrackising.egg-info/SOURCES.txt +2 -0
- {pyqrackising-5.1.4 → pyqrackising-5.1.6}/setup.py +1 -1
- {pyqrackising-5.1.4 → pyqrackising-5.1.6}/CMakeLists.txt +0 -0
- {pyqrackising-5.1.4 → pyqrackising-5.1.6}/LICENSE.md +0 -0
- {pyqrackising-5.1.4 → pyqrackising-5.1.6}/MANIFEST.in +0 -0
- {pyqrackising-5.1.4 → pyqrackising-5.1.6}/README.md +0 -0
- {pyqrackising-5.1.4 → pyqrackising-5.1.6}/pyqrackising/__init__.py +0 -0
- {pyqrackising-5.1.4 → pyqrackising-5.1.6}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-5.1.4 → pyqrackising-5.1.6}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-5.1.4 → pyqrackising-5.1.6}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-5.1.4 → pyqrackising-5.1.6}/pyqrackising/maxcut_tfim.py +0 -0
- {pyqrackising-5.1.4 → pyqrackising-5.1.6}/pyqrackising/maxcut_tfim_streaming.py +0 -0
- {pyqrackising-5.1.4 → pyqrackising-5.1.6}/pyqrackising/spin_glass_solver.py +0 -0
- {pyqrackising-5.1.4 → pyqrackising-5.1.6}/pyqrackising/spin_glass_solver_sparse.py +0 -0
- {pyqrackising-5.1.4 → pyqrackising-5.1.6}/pyqrackising/spin_glass_solver_streaming.py +0 -0
- {pyqrackising-5.1.4 → pyqrackising-5.1.6}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-5.1.4 → pyqrackising-5.1.6}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-5.1.4 → pyqrackising-5.1.6}/pyqrackising/tsp.py +0 -0
- {pyqrackising-5.1.4 → pyqrackising-5.1.6}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-5.1.4 → pyqrackising-5.1.6}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-5.1.4 → pyqrackising-5.1.6}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-5.1.4 → pyqrackising-5.1.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.1.
|
|
15
|
+
version = "5.1.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"}
|
|
@@ -154,9 +154,8 @@ def init_J_and_z(G_data, G_rows, G_cols):
|
|
|
154
154
|
degree = end - start
|
|
155
155
|
val = G_data[start:end].sum()
|
|
156
156
|
|
|
157
|
-
J = -val / degree if degree > 0 else 0
|
|
158
157
|
degrees[r] += degree
|
|
159
|
-
J_eff[r] +=
|
|
158
|
+
J_eff[r] += val
|
|
160
159
|
|
|
161
160
|
# Column sum
|
|
162
161
|
for idx in range(start, end):
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import math
|
|
2
|
+
import networkx as nx
|
|
3
|
+
import numpy as np
|
|
4
|
+
import os
|
|
5
|
+
from numba import njit, prange
|
|
6
|
+
|
|
7
|
+
from .maxcut_tfim_util import init_theta, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
8
|
+
|
|
9
|
+
IS_OPENCL_AVAILABLE = True
|
|
10
|
+
try:
|
|
11
|
+
import pyopencl as cl
|
|
12
|
+
except ImportError:
|
|
13
|
+
IS_OPENCL_AVAILABLE = False
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@njit
|
|
17
|
+
def binary_search(l, t):
|
|
18
|
+
left = 0
|
|
19
|
+
right = len(l) - 1
|
|
20
|
+
|
|
21
|
+
while left <= right:
|
|
22
|
+
mid = (left + right) >> 1
|
|
23
|
+
|
|
24
|
+
if l[mid] == t:
|
|
25
|
+
return mid
|
|
26
|
+
|
|
27
|
+
if l[mid] < t:
|
|
28
|
+
left = mid + 1
|
|
29
|
+
else:
|
|
30
|
+
right = mid - 1
|
|
31
|
+
|
|
32
|
+
return len(l)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
36
|
+
@njit
|
|
37
|
+
def local_repulsion_choice(G_cols, G_rows, n, m):
|
|
38
|
+
"""
|
|
39
|
+
Pick m nodes out of n with repulsion bias:
|
|
40
|
+
- High-degree nodes are already less likely
|
|
41
|
+
- After choosing a node, its neighbors' probabilities are further reduced
|
|
42
|
+
adjacency_data, adjacency_rows: CSR-format sparse adjacency data
|
|
43
|
+
weights: float64 array of shape (n,)
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
weights = np.ones(n, dtype=np.float32)
|
|
47
|
+
chosen = np.zeros(m, dtype=np.int32) # store chosen indices
|
|
48
|
+
available = np.ones(n, dtype=np.bool_) # True = available, False = not
|
|
49
|
+
mask = np.zeros(n, dtype=np.bool_)
|
|
50
|
+
chosen_count = 0
|
|
51
|
+
|
|
52
|
+
for _ in range(m):
|
|
53
|
+
# Count available
|
|
54
|
+
total_w = 0.0
|
|
55
|
+
for i in range(n):
|
|
56
|
+
if available[i]:
|
|
57
|
+
total_w += weights[i]
|
|
58
|
+
if total_w <= 0:
|
|
59
|
+
break
|
|
60
|
+
|
|
61
|
+
# Normalize & sample
|
|
62
|
+
r = np.random.rand()
|
|
63
|
+
cum = 0.0
|
|
64
|
+
node = -1
|
|
65
|
+
for i in range(n):
|
|
66
|
+
if available[i]:
|
|
67
|
+
cum += weights[i] / total_w
|
|
68
|
+
if r < cum:
|
|
69
|
+
node = i
|
|
70
|
+
break
|
|
71
|
+
|
|
72
|
+
if node == -1:
|
|
73
|
+
continue
|
|
74
|
+
|
|
75
|
+
# Select node
|
|
76
|
+
chosen[chosen_count] = node
|
|
77
|
+
chosen_count += 1
|
|
78
|
+
available[node] = False
|
|
79
|
+
mask[node] = True
|
|
80
|
+
|
|
81
|
+
# Repulsion: penalize neighbors
|
|
82
|
+
for j in range(G_rows[node], G_rows[node + 1]):
|
|
83
|
+
nbr = G_cols[j]
|
|
84
|
+
if available[nbr]:
|
|
85
|
+
weights[nbr] *= 0.5
|
|
86
|
+
|
|
87
|
+
for nbr in range(node):
|
|
88
|
+
if not available[nbr]:
|
|
89
|
+
continue
|
|
90
|
+
start = G_rows[nbr]
|
|
91
|
+
end = G_rows[nbr + 1]
|
|
92
|
+
j = binary_search(G_cols[start:end], node) + start
|
|
93
|
+
if j < end:
|
|
94
|
+
weights[nbr] *= 0.5
|
|
95
|
+
|
|
96
|
+
return mask
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
@njit
|
|
100
|
+
def compute_energy(sample, G_data, G_rows, G_cols):
|
|
101
|
+
n_qubits = G_rows.shape[0] - 1
|
|
102
|
+
energy = 0
|
|
103
|
+
for u in range(n_qubits):
|
|
104
|
+
for col in range(G_rows[u], G_rows[u + 1]):
|
|
105
|
+
v = G_cols[col]
|
|
106
|
+
energy += G_data[col] * (1 if sample[u] == sample[v] else -1)
|
|
107
|
+
|
|
108
|
+
return energy
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
@njit(parallel=True)
|
|
112
|
+
def sample_for_solution(G_data, G_rows, G_cols, shots, thresholds):
|
|
113
|
+
n = G_rows.shape[0] - 1
|
|
114
|
+
|
|
115
|
+
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
116
|
+
energies = np.empty(shots, dtype=np.float32)
|
|
117
|
+
|
|
118
|
+
for s in prange(shots):
|
|
119
|
+
# First dimension: Hamming weight
|
|
120
|
+
mag_prob = np.random.random()
|
|
121
|
+
m = 0
|
|
122
|
+
while thresholds[m] < mag_prob:
|
|
123
|
+
m += 1
|
|
124
|
+
m += 1
|
|
125
|
+
|
|
126
|
+
# Second dimension: permutation within Hamming weight
|
|
127
|
+
sample = local_repulsion_choice(G_cols, G_rows, max_weight, n, m)
|
|
128
|
+
solutions[s] = sample
|
|
129
|
+
energies[s] = compute_energy(sample, G_data, G_rows, G_cols)
|
|
130
|
+
|
|
131
|
+
best_solution = solutions[np.argmin(energies)]
|
|
132
|
+
|
|
133
|
+
best_value = 0.0
|
|
134
|
+
for u in range(n):
|
|
135
|
+
for col in range(G_rows[u], G_rows[u + 1]):
|
|
136
|
+
v = G_cols[col]
|
|
137
|
+
if best_solution[u] != best_solution[v]:
|
|
138
|
+
best_value += G_data[col]
|
|
139
|
+
|
|
140
|
+
return best_solution, best_value
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
@njit(parallel=True)
|
|
144
|
+
def init_z(G_data, G_rows, G_cols):
|
|
145
|
+
n_qubits = G_rows.shape[0] - 1
|
|
146
|
+
degrees = np.empty(n_qubits, dtype=np.uint32)
|
|
147
|
+
for r in prange(n_qubits):
|
|
148
|
+
# Row sum
|
|
149
|
+
start = G_rows[r]
|
|
150
|
+
end = G_rows[r + 1]
|
|
151
|
+
degree = end - start
|
|
152
|
+
val = np.count_nonzero(G_data[start:end])
|
|
153
|
+
|
|
154
|
+
degrees[r] += degree
|
|
155
|
+
|
|
156
|
+
# Column sum
|
|
157
|
+
for idx in range(start, end):
|
|
158
|
+
c = G_cols[idx]
|
|
159
|
+
degrees[c] += 1
|
|
160
|
+
|
|
161
|
+
return degrees
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def to_scipy_sparse_upper_triangular(G, nodes, n_nodes):
|
|
165
|
+
lil = lil_matrix((n_nodes, n_nodes), dtype=np.float32)
|
|
166
|
+
for u in range(n_nodes):
|
|
167
|
+
u_node = nodes[u]
|
|
168
|
+
for v in range(u + 1, n_nodes):
|
|
169
|
+
v_node = nodes[v]
|
|
170
|
+
if G.has_edge(u_node, v_node):
|
|
171
|
+
lil[u, v] = G[u_node][v_node].get('weight', 1.0)
|
|
172
|
+
|
|
173
|
+
return lil.tocsr()
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def maxcut_tfim_sparse(
|
|
177
|
+
G,
|
|
178
|
+
quality=None,
|
|
179
|
+
shots=None,
|
|
180
|
+
):
|
|
181
|
+
nodes = None
|
|
182
|
+
n_qubits = 0
|
|
183
|
+
G_m = None
|
|
184
|
+
if isinstance(G, nx.Graph):
|
|
185
|
+
nodes = list(G.nodes())
|
|
186
|
+
n_qubits = len(nodes)
|
|
187
|
+
G_m = to_scipy_sparse_upper_triangular(G, nodes, n_qubits)
|
|
188
|
+
else:
|
|
189
|
+
n_qubits = G.shape[0]
|
|
190
|
+
nodes = list(range(n_qubits))
|
|
191
|
+
G_m = G
|
|
192
|
+
|
|
193
|
+
if n_qubits < 3:
|
|
194
|
+
if n_qubits == 0:
|
|
195
|
+
return "", 0, ([], [])
|
|
196
|
+
|
|
197
|
+
if n_qubits == 1:
|
|
198
|
+
return "0", 0, (nodes, [])
|
|
199
|
+
|
|
200
|
+
if n_qubits == 2:
|
|
201
|
+
weight = G_m[0, 1]
|
|
202
|
+
if weight < 0.0:
|
|
203
|
+
return "00", 0, (nodes, [])
|
|
204
|
+
|
|
205
|
+
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
206
|
+
|
|
207
|
+
if quality is None:
|
|
208
|
+
quality = 8
|
|
209
|
+
|
|
210
|
+
if shots is None:
|
|
211
|
+
# Number of measurement shots
|
|
212
|
+
shots = n_qubits << quality
|
|
213
|
+
|
|
214
|
+
n_steps = 1 << quality
|
|
215
|
+
grid_size = n_steps * n_qubits
|
|
216
|
+
|
|
217
|
+
degrees = init_J_and_z(G_m.data, G_m.indptr, G_m.indices)
|
|
218
|
+
hamming_prob = init_thresholds(n_qubits)
|
|
219
|
+
|
|
220
|
+
if IS_OPENCL_AVAILABLE and grid_size >= 128:
|
|
221
|
+
delta_t = 1.0 / n_steps
|
|
222
|
+
tot_t = 2.0 * n_steps * delta_t
|
|
223
|
+
h_mult = 2.0 / tot_t
|
|
224
|
+
theta = init_theta(delta_t, tot_t, h_mult, n_qubits, degrees)
|
|
225
|
+
args = np.empty(3, dtype=np.float32)
|
|
226
|
+
args[0] = delta_t
|
|
227
|
+
args[1] = tot_t
|
|
228
|
+
args[2] = h_mult
|
|
229
|
+
|
|
230
|
+
# Warp size is 32:
|
|
231
|
+
group_size = n_qubits - 1
|
|
232
|
+
if group_size > 256:
|
|
233
|
+
group_size = 256
|
|
234
|
+
grid_dim = n_steps * n_qubits * group_size
|
|
235
|
+
|
|
236
|
+
# Move to GPU
|
|
237
|
+
mf = cl.mem_flags
|
|
238
|
+
args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
|
|
239
|
+
deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
|
|
240
|
+
theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=theta)
|
|
241
|
+
ham_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=hamming_prob)
|
|
242
|
+
|
|
243
|
+
# Kernel execution
|
|
244
|
+
opencl_context.maxcut_hamming_cdf_kernel(
|
|
245
|
+
opencl_context.queue, (grid_dim,), (group_size,),
|
|
246
|
+
np.int32(n_qubits), deg_buf, args_buf, theta_buf, ham_buf
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
# Fetch results
|
|
250
|
+
cl.enqueue_copy(opencl_context.queue, hamming_prob, ham_buf)
|
|
251
|
+
|
|
252
|
+
hamming_prob /= hamming_prob.sum()
|
|
253
|
+
tot_prob = 0.0
|
|
254
|
+
for i in range(n_qubits - 1):
|
|
255
|
+
tot_prob += hamming_prob[i]
|
|
256
|
+
hamming_prob[i] = tot_prob
|
|
257
|
+
hamming_prob[-1] = 2.0
|
|
258
|
+
else:
|
|
259
|
+
maxcut_hamming_cdf(n_qubits, degrees, quality, hamming_prob)
|
|
260
|
+
|
|
261
|
+
best_solution, best_value = sample_for_solution(G_m.data, G_m.indptr, G_m.indices, shots, hamming_prob)
|
|
262
|
+
|
|
263
|
+
bit_string = ""
|
|
264
|
+
l, r = [], []
|
|
265
|
+
for i in range(len(best_solution)):
|
|
266
|
+
if best_solution[i]:
|
|
267
|
+
bit_string += "1"
|
|
268
|
+
r.append(nodes[i])
|
|
269
|
+
else:
|
|
270
|
+
bit_string += "0"
|
|
271
|
+
l.append(nodes[i])
|
|
272
|
+
|
|
273
|
+
return bit_string, best_value, (l, r)
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import math
|
|
2
|
+
import networkx as nx
|
|
3
|
+
import numpy as np
|
|
4
|
+
import os
|
|
5
|
+
from numba import njit, prange
|
|
6
|
+
|
|
7
|
+
from .maxcut_tfim_util import init_theta, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
|
|
8
|
+
|
|
9
|
+
IS_OPENCL_AVAILABLE = True
|
|
10
|
+
try:
|
|
11
|
+
import pyopencl as cl
|
|
12
|
+
except ImportError:
|
|
13
|
+
IS_OPENCL_AVAILABLE = False
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
17
|
+
@njit
|
|
18
|
+
def local_repulsion_choice(G_func, G_func_args_tuple, nodes, max_weight, weights, n, m):
|
|
19
|
+
"""
|
|
20
|
+
Pick m nodes out of n with repulsion bias:
|
|
21
|
+
- High-degree nodes are already less likely
|
|
22
|
+
- After choosing a node, its neighbors' probabilities are further reduced
|
|
23
|
+
adjacency_data, adjacency_rows: CSR-format sparse adjacency data
|
|
24
|
+
weights: float64 array of shape (n,)
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
weights = weights.copy()
|
|
28
|
+
chosen = np.zeros(m, dtype=np.int32) # store chosen indices
|
|
29
|
+
available = np.ones(n, dtype=np.bool_) # True = available, False = not
|
|
30
|
+
mask = np.zeros(n, dtype=np.bool_)
|
|
31
|
+
chosen_count = 0
|
|
32
|
+
|
|
33
|
+
for _ in range(m):
|
|
34
|
+
# Count available
|
|
35
|
+
total_w = 0.0
|
|
36
|
+
for i in range(n):
|
|
37
|
+
if available[i]:
|
|
38
|
+
total_w += weights[i]
|
|
39
|
+
if total_w <= 0:
|
|
40
|
+
break
|
|
41
|
+
|
|
42
|
+
# Normalize & sample
|
|
43
|
+
r = np.random.rand()
|
|
44
|
+
cum = 0.0
|
|
45
|
+
node = -1
|
|
46
|
+
for i in range(n):
|
|
47
|
+
if available[i]:
|
|
48
|
+
cum += weights[i] / total_w
|
|
49
|
+
if r < cum:
|
|
50
|
+
node = i
|
|
51
|
+
break
|
|
52
|
+
|
|
53
|
+
if node == -1:
|
|
54
|
+
continue
|
|
55
|
+
|
|
56
|
+
# Select node
|
|
57
|
+
chosen[chosen_count] = node
|
|
58
|
+
chosen_count += 1
|
|
59
|
+
available[node] = False
|
|
60
|
+
mask[node] = True
|
|
61
|
+
|
|
62
|
+
# Repulsion: penalize neighbors
|
|
63
|
+
for nbr in range(n):
|
|
64
|
+
if available[nbr]:
|
|
65
|
+
weights[nbr] *= 0.5 ** (G_func((nodes[node], nodes[nbr]), G_func_args_tuple) / max_weight) # tunable penalty factor
|
|
66
|
+
|
|
67
|
+
return mask
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@njit
|
|
71
|
+
def compute_energy(sample, G_func, G_func_args_tuple, nodes, n_qubits):
|
|
72
|
+
energy = 0
|
|
73
|
+
for u in range(n_qubits):
|
|
74
|
+
for v in range(u + 1, n_qubits):
|
|
75
|
+
energy += G_func((nodes[u], nodes[v]), G_func_args_tuple) * (1 if sample[u] == sample[v] else -1)
|
|
76
|
+
|
|
77
|
+
return energy
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
@njit(parallel=True)
|
|
81
|
+
def sample_for_solution(G_func, G_func_args_tuple, nodes, max_weight, shots, thresholds, degrees_sum, J_eff, n):
|
|
82
|
+
weights = 1.0 / (1.0 + (2 ** -52) - J_eff)
|
|
83
|
+
|
|
84
|
+
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
85
|
+
energies = np.empty(shots, dtype=np.float32)
|
|
86
|
+
|
|
87
|
+
for s in prange(shots):
|
|
88
|
+
# First dimension: Hamming weight
|
|
89
|
+
mag_prob = np.random.random()
|
|
90
|
+
m = 0
|
|
91
|
+
while thresholds[m] < mag_prob:
|
|
92
|
+
m += 1
|
|
93
|
+
m += 1
|
|
94
|
+
|
|
95
|
+
# Second dimension: permutation within Hamming weight
|
|
96
|
+
sample = local_repulsion_choice(G_func, G_func_args_tuple, nodes, max_weight, weights, n, m)
|
|
97
|
+
|
|
98
|
+
solutions[s] = sample
|
|
99
|
+
energies[s] = compute_energy(sample, G_func, G_func_args_tuple, nodes, n)
|
|
100
|
+
|
|
101
|
+
best_solution = solutions[np.argmin(energies)]
|
|
102
|
+
|
|
103
|
+
best_value = 0.0
|
|
104
|
+
for u in range(n):
|
|
105
|
+
for v in range(u + 1, n):
|
|
106
|
+
if best_solution[u] != best_solution[v]:
|
|
107
|
+
best_value += G_func((nodes[u], nodes[v]), G_func_args_tuple)
|
|
108
|
+
|
|
109
|
+
return best_solution, best_value
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
@njit(parallel=True)
|
|
113
|
+
def init_J_and_z(G_func, G_func_args_tuple, nodes):
|
|
114
|
+
n_qubits = len(nodes)
|
|
115
|
+
degrees = np.empty(n_qubits, dtype=np.uint32)
|
|
116
|
+
J_eff = np.empty(n_qubits, dtype=np.float32)
|
|
117
|
+
J_max = -float("inf")
|
|
118
|
+
G_max = -float("inf")
|
|
119
|
+
for n in prange(n_qubits):
|
|
120
|
+
degree = 0
|
|
121
|
+
J = 0.0
|
|
122
|
+
for m in range(n_qubits):
|
|
123
|
+
val = G_func((nodes[n], nodes[m]), G_func_args_tuple)
|
|
124
|
+
if val != 0.0:
|
|
125
|
+
degree += 1
|
|
126
|
+
J += val
|
|
127
|
+
G_max = max(val, G_max)
|
|
128
|
+
J = -J / degree if degree > 0 else 0
|
|
129
|
+
degrees[n] = degree
|
|
130
|
+
J_eff[n] = J
|
|
131
|
+
J_abs = abs(J)
|
|
132
|
+
J_max = max(J_abs, J_max)
|
|
133
|
+
J_eff /= J_max
|
|
134
|
+
|
|
135
|
+
return J_eff, degrees, G_max
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def maxcut_tfim_streaming(
|
|
139
|
+
G_func,
|
|
140
|
+
nodes,
|
|
141
|
+
G_func_args_tuple=None,
|
|
142
|
+
quality=None,
|
|
143
|
+
shots=None,
|
|
144
|
+
):
|
|
145
|
+
n_qubits = len(nodes)
|
|
146
|
+
|
|
147
|
+
if n_qubits < 3:
|
|
148
|
+
if n_qubits == 0:
|
|
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]])
|
|
160
|
+
|
|
161
|
+
if quality is None:
|
|
162
|
+
quality = 8
|
|
163
|
+
|
|
164
|
+
if shots is None:
|
|
165
|
+
# Number of measurement shots
|
|
166
|
+
shots = n_qubits << quality
|
|
167
|
+
|
|
168
|
+
n_steps = 1 << quality
|
|
169
|
+
grid_size = n_steps * n_qubits
|
|
170
|
+
|
|
171
|
+
J_eff, degrees, G_max = init_J_and_z(G_func, G_func_args_tuple, nodes)
|
|
172
|
+
hamming_prob = init_thresholds(n_qubits)
|
|
173
|
+
|
|
174
|
+
if IS_OPENCL_AVAILABLE and grid_size >= 128:
|
|
175
|
+
delta_t = 1.0 / n_steps
|
|
176
|
+
tot_t = 2.0 * n_steps * delta_t
|
|
177
|
+
h_mult = 2.0 / tot_t
|
|
178
|
+
theta = init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees)
|
|
179
|
+
args = np.empty(3, dtype=np.float32)
|
|
180
|
+
args[0] = delta_t
|
|
181
|
+
args[1] = tot_t
|
|
182
|
+
args[2] = h_mult
|
|
183
|
+
|
|
184
|
+
# Warp size is 32:
|
|
185
|
+
group_size = n_qubits - 1
|
|
186
|
+
if group_size > 256:
|
|
187
|
+
group_size = 256
|
|
188
|
+
grid_dim = n_steps * n_qubits * group_size
|
|
189
|
+
|
|
190
|
+
# Move to GPU
|
|
191
|
+
mf = cl.mem_flags
|
|
192
|
+
args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
|
|
193
|
+
J_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
|
|
194
|
+
deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
|
|
195
|
+
theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=theta)
|
|
196
|
+
ham_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=hamming_prob)
|
|
197
|
+
|
|
198
|
+
# Kernel execution
|
|
199
|
+
opencl_context.maxcut_hamming_cdf_kernel(
|
|
200
|
+
opencl_context.queue, (grid_dim,), (group_size,),
|
|
201
|
+
np.int32(n_qubits), deg_buf, args_buf, J_buf, theta_buf, ham_buf
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
# Fetch results
|
|
205
|
+
cl.enqueue_copy(opencl_context.queue, hamming_prob, ham_buf)
|
|
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)
|
|
@@ -138,6 +138,7 @@ class OpenCLContext:
|
|
|
138
138
|
IS_OPENCL_AVAILABLE = True
|
|
139
139
|
ctx = None
|
|
140
140
|
queue = None
|
|
141
|
+
maxcut_hamming_cdf_kernel = None
|
|
141
142
|
try:
|
|
142
143
|
import pyopencl as cl
|
|
143
144
|
|
|
@@ -151,5 +152,6 @@ try:
|
|
|
151
152
|
maxcut_hamming_cdf_kernel = program.maxcut_hamming_cdf
|
|
152
153
|
except ImportError:
|
|
153
154
|
IS_OPENCL_AVAILABLE = False
|
|
155
|
+
print("PyOpenCL not installed. (If you have any OpenCL accelerator devices with available ICDs, you might want to optionally install pyopencl.)")
|
|
154
156
|
|
|
155
157
|
opencl_context = OpenCLContext(IS_OPENCL_AVAILABLE, ctx, queue, maxcut_hamming_cdf_kernel)
|
|
@@ -10,7 +10,9 @@ pyqrackising/generate_tfim_samples.py
|
|
|
10
10
|
pyqrackising/kernels.cl
|
|
11
11
|
pyqrackising/maxcut_tfim.py
|
|
12
12
|
pyqrackising/maxcut_tfim_sparse.py
|
|
13
|
+
pyqrackising/maxcut_tfim_sparse_uniform.py
|
|
13
14
|
pyqrackising/maxcut_tfim_streaming.py
|
|
15
|
+
pyqrackising/maxcut_tfim_streaming_uniform.py
|
|
14
16
|
pyqrackising/maxcut_tfim_util.py
|
|
15
17
|
pyqrackising/spin_glass_solver.py
|
|
16
18
|
pyqrackising/spin_glass_solver_sparse.py
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='pyqrackising',
|
|
54
|
-
version='5.1.
|
|
54
|
+
version='5.1.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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|