PyQrackIsing 8.3.1__tar.gz → 8.5.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-8.3.1/pyqrackising.egg-info → pyqrackising-8.5.0}/PKG-INFO +1 -1
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/README.md +2 -2
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/pyproject.toml +1 -1
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/pyqrackising/maxcut_tfim.py +33 -21
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/pyqrackising/maxcut_tfim_sparse.py +25 -18
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/pyqrackising/maxcut_tfim_streaming.py +24 -19
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/pyqrackising/spin_glass_solver.py +3 -2
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/pyqrackising/spin_glass_solver_sparse.py +3 -2
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/pyqrackising/spin_glass_solver_streaming.py +3 -2
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/pyqrackising/tsp.py +12 -6
- {pyqrackising-8.3.1 → pyqrackising-8.5.0/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/setup.py +1 -1
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/CMakeLists.txt +0 -0
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/LICENSE.md +0 -0
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/MANIFEST.in +0 -0
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/pyqrackising/__init__.py +0 -0
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/pyqrackising/_pyqrack_ising.cpp +0 -0
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/pyqrackising/maxcut_tfim_util.py +0 -0
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/pyqrackising/spin_glass_solver_util.py +0 -0
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/pyqrackising/tsp_maxcut.py +0 -0
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-8.3.1 → pyqrackising-8.5.0}/setup.cfg +0 -0
|
@@ -50,10 +50,10 @@ from pyqrackising import maxcut_tfim
|
|
|
50
50
|
import networkx as nx
|
|
51
51
|
|
|
52
52
|
G = nx.petersen_graph()
|
|
53
|
-
best_solution_bit_string, best_cut_value, best_node_groups = maxcut_tfim(G, quality=
|
|
53
|
+
best_solution_bit_string, best_cut_value, best_node_groups = maxcut_tfim(G, quality=5, shots=None, is_spin_glass=False, anneal_t=8.0, anneal_h=8.0, repulsion_base=4.0)
|
|
54
54
|
```
|
|
55
55
|
|
|
56
|
-
We also provide `maxcut_tfim_sparse(G)`, for `scipy` CSR sparse arrays (or `networkx` graphs), and `maxcut_tfim_streaming(G_func, nodes)` for `numba` JIT streaming weights function definitions. The (integer) `quality` setting is optional, with a default value of `
|
|
56
|
+
We also provide `maxcut_tfim_sparse(G)`, for `scipy` CSR sparse arrays (or `networkx` graphs), and `maxcut_tfim_streaming(G_func, nodes)` for `numba` JIT streaming weights function definitions. The (integer) `quality` setting is optional, with a default value of `5`, but you can turn it up for higher-quality results, or turn it down to save time. (You can also optionally specify the number of measurement `shots` as an argument, if you want specific fine-grained control over resource usage.) `anneal_t` and `anneal_h` control the physical maximum annealing time and `h` transverse field parameter, as in Trotterized Ising model. While `anneal_t` and `anneal_h` control how annealing finds the _Hamming weight_ of cuts (i.e., how many nodes end up in either partition), `repulsion_base` similarly controls the steepness of the search basin for optimizing bit-string locality ("like-like" repulsion and "like-unlike" attraction) within each partition. If you want to run MAXCUT on a graph with non-uniform edge weights, specify them as the `weight` attribute of each edge, with `networkx`. (If any `weight` attribute is not defined, the solver assumes it's `1.0` for that edge.)
|
|
57
57
|
|
|
58
58
|
Based on a combination of the TFIM-inspired MAXCUT solver and another technique for finding ground-state energy in quantum chemistry that we call the _"binary Clifford eigensolver,"_ we also provide an (approximate) spin glass ground-state solver:
|
|
59
59
|
```py
|
|
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
|
|
|
12
12
|
|
|
13
13
|
[project]
|
|
14
14
|
name = "pyqrackising"
|
|
15
|
-
version = "8.
|
|
15
|
+
version = "8.5.0"
|
|
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"}
|
|
@@ -12,7 +12,7 @@ dtype = opencl_context.dtype
|
|
|
12
12
|
|
|
13
13
|
|
|
14
14
|
@njit
|
|
15
|
-
def update_repulsion_choice(G_m, max_edge, weights, n, used, node):
|
|
15
|
+
def update_repulsion_choice(G_m, max_edge, weights, n, used, node, repulsion_base):
|
|
16
16
|
# Select node
|
|
17
17
|
used[node] = True
|
|
18
18
|
|
|
@@ -20,12 +20,12 @@ def update_repulsion_choice(G_m, max_edge, weights, n, used, node):
|
|
|
20
20
|
for nbr in range(n):
|
|
21
21
|
if used[nbr]:
|
|
22
22
|
continue
|
|
23
|
-
weights[nbr] *=
|
|
23
|
+
weights[nbr] *= repulsion_base ** (-G_m[node, nbr] / max_edge)
|
|
24
24
|
|
|
25
25
|
|
|
26
26
|
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
27
27
|
@njit
|
|
28
|
-
def local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, n, m):
|
|
28
|
+
def local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, repulsion_base, n, m):
|
|
29
29
|
"""
|
|
30
30
|
Pick m nodes out of n with repulsion bias:
|
|
31
31
|
- High-degree nodes are already less likely
|
|
@@ -51,7 +51,7 @@ def local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, n, m):
|
|
|
51
51
|
used[node] = True
|
|
52
52
|
return used
|
|
53
53
|
|
|
54
|
-
update_repulsion_choice(G_m, max_edge, weights, n, used, node)
|
|
54
|
+
update_repulsion_choice(G_m, max_edge, weights, n, used, node, repulsion_base)
|
|
55
55
|
|
|
56
56
|
for _ in range(1, m - 1):
|
|
57
57
|
# Count available
|
|
@@ -79,7 +79,7 @@ def local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, n, m):
|
|
|
79
79
|
node += 1
|
|
80
80
|
|
|
81
81
|
# Update answer and weights
|
|
82
|
-
update_repulsion_choice(G_m, max_edge, weights, n, used, node)
|
|
82
|
+
update_repulsion_choice(G_m, max_edge, weights, n, used, node, repulsion_base)
|
|
83
83
|
|
|
84
84
|
# Count available
|
|
85
85
|
total_w = 0.0
|
|
@@ -133,10 +133,9 @@ def compute_cut(sample, G_m, n_qubits):
|
|
|
133
133
|
|
|
134
134
|
|
|
135
135
|
@njit(parallel=True)
|
|
136
|
-
def sample_for_energy(G_m, shots, thresholds, weights):
|
|
136
|
+
def sample_for_energy(G_m, max_edge, shots, thresholds, weights, repulsion_base):
|
|
137
137
|
shots = max(1, shots >> 1)
|
|
138
138
|
n = len(G_m)
|
|
139
|
-
max_edge = G_m.max()
|
|
140
139
|
tot_init_weight = weights.sum()
|
|
141
140
|
|
|
142
141
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
@@ -157,7 +156,7 @@ def sample_for_energy(G_m, shots, thresholds, weights):
|
|
|
157
156
|
m += 1
|
|
158
157
|
|
|
159
158
|
# Second dimension: permutation within Hamming weight
|
|
160
|
-
sample = local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, n, m)
|
|
159
|
+
sample = local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, repulsion_base, n, m)
|
|
161
160
|
solutions[s] = sample
|
|
162
161
|
energies[s] = compute_energy(sample, G_m, n)
|
|
163
162
|
|
|
@@ -172,10 +171,9 @@ def sample_for_energy(G_m, shots, thresholds, weights):
|
|
|
172
171
|
|
|
173
172
|
|
|
174
173
|
@njit(parallel=True)
|
|
175
|
-
def sample_for_cut(G_m, shots, thresholds, weights):
|
|
174
|
+
def sample_for_cut(G_m, max_edge, shots, thresholds, weights, repulsion_base):
|
|
176
175
|
shots = max(1, shots >> 1)
|
|
177
176
|
n = len(G_m)
|
|
178
|
-
max_edge = G_m.max()
|
|
179
177
|
tot_init_weight = weights.sum()
|
|
180
178
|
|
|
181
179
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
@@ -196,7 +194,7 @@ def sample_for_cut(G_m, shots, thresholds, weights):
|
|
|
196
194
|
m += 1
|
|
197
195
|
|
|
198
196
|
# Second dimension: permutation within Hamming weight
|
|
199
|
-
sample = local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, n, m)
|
|
197
|
+
sample = local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, repulsion_base, n, m)
|
|
200
198
|
solutions[s] = sample
|
|
201
199
|
cuts[s] = compute_cut(sample, G_m, n)
|
|
202
200
|
|
|
@@ -215,27 +213,37 @@ def init_J_and_z(G_m):
|
|
|
215
213
|
n_qubits = len(G_m)
|
|
216
214
|
degrees = np.empty(n_qubits, dtype=np.uint32)
|
|
217
215
|
J_eff = np.empty(n_qubits, dtype=np.float64)
|
|
216
|
+
G_max = -float("inf")
|
|
218
217
|
for n in prange(n_qubits):
|
|
219
|
-
degree =
|
|
220
|
-
J =
|
|
218
|
+
degree = 0
|
|
219
|
+
J = 0.0
|
|
220
|
+
for m in range(n_qubits):
|
|
221
|
+
val = G_m[n, m]
|
|
222
|
+
if val != 0.0:
|
|
223
|
+
degree += 1
|
|
224
|
+
J += val
|
|
225
|
+
val = abs(val)
|
|
226
|
+
if val > G_max:
|
|
227
|
+
G_max = val
|
|
228
|
+
J = -J / degree if degree > 0 else 0
|
|
221
229
|
degrees[n] = degree
|
|
222
230
|
J_eff[n] = J
|
|
223
231
|
|
|
224
|
-
return J_eff, degrees
|
|
232
|
+
return J_eff, degrees, G_max
|
|
225
233
|
|
|
226
234
|
|
|
227
235
|
@njit
|
|
228
|
-
def cpu_footer(shots, quality, n_qubits, G_m, nodes, is_spin_glass, anneal_t, anneal_h):
|
|
229
|
-
J_eff, degrees = init_J_and_z(G_m)
|
|
236
|
+
def cpu_footer(shots, quality, n_qubits, G_m, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
|
|
237
|
+
J_eff, degrees, max_edge = init_J_and_z(G_m)
|
|
230
238
|
hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
|
|
231
239
|
|
|
232
240
|
degrees = None
|
|
233
241
|
J_eff = 1.0 / (1.0 + epsilon - J_eff)
|
|
234
242
|
|
|
235
243
|
if is_spin_glass:
|
|
236
|
-
best_solution, best_value = sample_for_energy(G_m, shots, hamming_prob, J_eff)
|
|
244
|
+
best_solution, best_value = sample_for_energy(G_m, max_edge, shots, hamming_prob, J_eff, repulsion_base)
|
|
237
245
|
else:
|
|
238
|
-
best_solution, best_value = sample_for_cut(G_m, shots, hamming_prob, J_eff)
|
|
246
|
+
best_solution, best_value = sample_for_cut(G_m, max_edge, shots, hamming_prob, J_eff, repulsion_base)
|
|
239
247
|
|
|
240
248
|
bit_string, l, r = get_cut(best_solution, nodes)
|
|
241
249
|
|
|
@@ -248,7 +256,8 @@ def maxcut_tfim(
|
|
|
248
256
|
shots=None,
|
|
249
257
|
is_spin_glass=False,
|
|
250
258
|
anneal_t=None,
|
|
251
|
-
anneal_h=None
|
|
259
|
+
anneal_h=None,
|
|
260
|
+
repulsion_base=None
|
|
252
261
|
):
|
|
253
262
|
wgs = opencl_context.work_group_size
|
|
254
263
|
nodes = None
|
|
@@ -278,7 +287,7 @@ def maxcut_tfim(
|
|
|
278
287
|
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
279
288
|
|
|
280
289
|
if quality is None:
|
|
281
|
-
quality =
|
|
290
|
+
quality = 5
|
|
282
291
|
|
|
283
292
|
if shots is None:
|
|
284
293
|
# Number of measurement shots
|
|
@@ -290,4 +299,7 @@ def maxcut_tfim(
|
|
|
290
299
|
if anneal_h is None:
|
|
291
300
|
anneal_h = 8.0
|
|
292
301
|
|
|
293
|
-
|
|
302
|
+
if repulsion_base is None:
|
|
303
|
+
repulsion_base = 4.0
|
|
304
|
+
|
|
305
|
+
return cpu_footer(shots, quality, n_qubits, G_m, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base)
|
|
@@ -12,7 +12,7 @@ dtype = opencl_context.dtype
|
|
|
12
12
|
|
|
13
13
|
|
|
14
14
|
@njit
|
|
15
|
-
def update_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, n, used, node):
|
|
15
|
+
def update_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, n, used, node, repulsion_base):
|
|
16
16
|
# Select node
|
|
17
17
|
used[node] = True
|
|
18
18
|
|
|
@@ -21,7 +21,7 @@ def update_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, n, used,
|
|
|
21
21
|
nbr = G_cols[j]
|
|
22
22
|
if used[nbr]:
|
|
23
23
|
continue
|
|
24
|
-
weights[nbr] *=
|
|
24
|
+
weights[nbr] *= repulsion_base ** (-G_data[j] / max_edge)
|
|
25
25
|
|
|
26
26
|
for nbr in range(node):
|
|
27
27
|
if used[nbr]:
|
|
@@ -30,12 +30,12 @@ def update_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, n, used,
|
|
|
30
30
|
end = G_rows[nbr + 1]
|
|
31
31
|
j = binary_search(G_cols[start:end], node) + start
|
|
32
32
|
if j < end:
|
|
33
|
-
weights[nbr] *=
|
|
33
|
+
weights[nbr] *= repulsion_base ** (-G_data[j] / max_edge)
|
|
34
34
|
|
|
35
35
|
|
|
36
36
|
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
37
37
|
@njit
|
|
38
|
-
def local_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, tot_init_weight, n, m):
|
|
38
|
+
def local_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, tot_init_weight, repulsion_base, n, m):
|
|
39
39
|
"""
|
|
40
40
|
Pick m nodes out of n with repulsion bias:
|
|
41
41
|
- High-degree nodes are already less likely
|
|
@@ -61,7 +61,7 @@ def local_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, tot_init_w
|
|
|
61
61
|
used[node] = True
|
|
62
62
|
return used
|
|
63
63
|
|
|
64
|
-
update_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, n, used, node)
|
|
64
|
+
update_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, n, used, node, repulsion_base)
|
|
65
65
|
|
|
66
66
|
for _ in range(1, m - 1):
|
|
67
67
|
# Count available
|
|
@@ -89,7 +89,7 @@ def local_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, tot_init_w
|
|
|
89
89
|
node += 1
|
|
90
90
|
|
|
91
91
|
# Update answer and weights
|
|
92
|
-
update_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, n, used, node)
|
|
92
|
+
update_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, n, used, node, repulsion_base)
|
|
93
93
|
|
|
94
94
|
# Count available
|
|
95
95
|
total_w = 0.0
|
|
@@ -145,10 +145,9 @@ def compute_cut(sample, G_data, G_rows, G_cols, n_qubits):
|
|
|
145
145
|
|
|
146
146
|
|
|
147
147
|
@njit(parallel=True)
|
|
148
|
-
def sample_for_energy(G_data, G_rows, G_cols, shots, thresholds, weights):
|
|
148
|
+
def sample_for_energy(max_edge, G_data, G_rows, G_cols, shots, thresholds, weights, repulsion_base):
|
|
149
149
|
shots = max(1, shots >> 1)
|
|
150
150
|
n = G_rows.shape[0] - 1
|
|
151
|
-
max_edge = G_data.max()
|
|
152
151
|
tot_init_weight = weights.sum()
|
|
153
152
|
|
|
154
153
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
@@ -169,7 +168,7 @@ def sample_for_energy(G_data, G_rows, G_cols, shots, thresholds, weights):
|
|
|
169
168
|
m += 1
|
|
170
169
|
|
|
171
170
|
# Second dimension: permutation within Hamming weight
|
|
172
|
-
sample = local_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, tot_init_weight, n, m)
|
|
171
|
+
sample = local_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, tot_init_weight, repulsion_base, n, m)
|
|
173
172
|
solutions[s] = sample
|
|
174
173
|
energies[s] = compute_energy(sample, G_data, G_rows, G_cols, n)
|
|
175
174
|
|
|
@@ -184,10 +183,9 @@ def sample_for_energy(G_data, G_rows, G_cols, shots, thresholds, weights):
|
|
|
184
183
|
|
|
185
184
|
|
|
186
185
|
@njit(parallel=True)
|
|
187
|
-
def sample_for_cut(G_data, G_rows, G_cols, shots, thresholds, weights):
|
|
186
|
+
def sample_for_cut(max_edge, G_data, G_rows, G_cols, shots, thresholds, weights, repulsion_base):
|
|
188
187
|
shots = max(1, shots >> 1)
|
|
189
188
|
n = G_rows.shape[0] - 1
|
|
190
|
-
max_edge = G_data.max()
|
|
191
189
|
tot_init_weight = weights.sum()
|
|
192
190
|
|
|
193
191
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
@@ -208,7 +206,7 @@ def sample_for_cut(G_data, G_rows, G_cols, shots, thresholds, weights):
|
|
|
208
206
|
m += 1
|
|
209
207
|
|
|
210
208
|
# Second dimension: permutation within Hamming weight
|
|
211
|
-
sample = local_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, tot_init_weight, n, m)
|
|
209
|
+
sample = local_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, tot_init_weight, repulsion_base, n, m)
|
|
212
210
|
solutions[s] = sample
|
|
213
211
|
cuts[s] = compute_cut(sample, G_data, G_rows, G_cols, n)
|
|
214
212
|
|
|
@@ -251,7 +249,7 @@ def init_J_and_z(G_data, G_rows, G_cols):
|
|
|
251
249
|
|
|
252
250
|
|
|
253
251
|
@njit
|
|
254
|
-
def cpu_footer(shots, quality, n_qubits, G_data, G_rows, G_col, nodes, is_spin_glass, anneal_t, anneal_h):
|
|
252
|
+
def cpu_footer(shots, quality, n_qubits, max_edge, G_data, G_rows, G_col, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
|
|
255
253
|
J_eff, degrees = init_J_and_z(G_data, G_rows, G_col)
|
|
256
254
|
hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
|
|
257
255
|
|
|
@@ -259,9 +257,9 @@ def cpu_footer(shots, quality, n_qubits, G_data, G_rows, G_col, nodes, is_spin_g
|
|
|
259
257
|
J_eff = 1.0 / (1.0 + epsilon - J_eff)
|
|
260
258
|
|
|
261
259
|
if is_spin_glass:
|
|
262
|
-
best_solution, best_value = sample_for_energy(G_data, G_rows, G_col, shots, hamming_prob, J_eff)
|
|
260
|
+
best_solution, best_value = sample_for_energy(max_edge, G_data, G_rows, G_col, shots, hamming_prob, J_eff, repulsion_base)
|
|
263
261
|
else:
|
|
264
|
-
best_solution, best_value = sample_for_cut(G_data, G_rows, G_col, shots, hamming_prob, J_eff)
|
|
262
|
+
best_solution, best_value = sample_for_cut(max_edge, G_data, G_rows, G_col, shots, hamming_prob, J_eff, repulsion_base)
|
|
265
263
|
|
|
266
264
|
bit_string, l, r = get_cut(best_solution, nodes)
|
|
267
265
|
|
|
@@ -274,7 +272,8 @@ def maxcut_tfim_sparse(
|
|
|
274
272
|
shots=None,
|
|
275
273
|
is_spin_glass=False,
|
|
276
274
|
anneal_t=None,
|
|
277
|
-
anneal_h=None
|
|
275
|
+
anneal_h=None,
|
|
276
|
+
repulsion_base=None
|
|
278
277
|
):
|
|
279
278
|
wgs = opencl_context.work_group_size
|
|
280
279
|
nodes = None
|
|
@@ -304,7 +303,7 @@ def maxcut_tfim_sparse(
|
|
|
304
303
|
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
305
304
|
|
|
306
305
|
if quality is None:
|
|
307
|
-
quality =
|
|
306
|
+
quality = 5
|
|
308
307
|
|
|
309
308
|
if shots is None:
|
|
310
309
|
# Number of measurement shots
|
|
@@ -316,4 +315,12 @@ def maxcut_tfim_sparse(
|
|
|
316
315
|
if anneal_h is None:
|
|
317
316
|
anneal_h = 8.0
|
|
318
317
|
|
|
319
|
-
|
|
318
|
+
if repulsion_base is None:
|
|
319
|
+
repulsion_base = 4.0
|
|
320
|
+
|
|
321
|
+
max_edge = abs(G_m.max())
|
|
322
|
+
min_edge = abs(G_m.min())
|
|
323
|
+
if min_edge > max_edge:
|
|
324
|
+
max_edge = min_edge
|
|
325
|
+
|
|
326
|
+
return cpu_footer(shots, quality, n_qubits, max_edge, G_m.data, G_m.indptr, G_m.indices, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base)
|
|
@@ -12,7 +12,7 @@ dtype = opencl_context.dtype
|
|
|
12
12
|
|
|
13
13
|
|
|
14
14
|
@njit
|
|
15
|
-
def update_repulsion_choice(G_func, nodes, max_edge, weights, n, used, node):
|
|
15
|
+
def update_repulsion_choice(G_func, nodes, max_edge, weights, n, used, node, repulsion_base):
|
|
16
16
|
# Select node
|
|
17
17
|
used[node] = True
|
|
18
18
|
|
|
@@ -20,12 +20,12 @@ def update_repulsion_choice(G_func, nodes, max_edge, weights, n, used, node):
|
|
|
20
20
|
for nbr in range(n):
|
|
21
21
|
if used[nbr]:
|
|
22
22
|
continue
|
|
23
|
-
weights[nbr] *=
|
|
23
|
+
weights[nbr] *= repulsion_base ** (-G_func(nodes[node], nodes[nbr]) / max_edge)
|
|
24
24
|
|
|
25
25
|
|
|
26
26
|
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
27
27
|
@njit
|
|
28
|
-
def local_repulsion_choice(G_func, nodes, max_edge, weights, tot_init_weight, n, m):
|
|
28
|
+
def local_repulsion_choice(G_func, nodes, max_edge, weights, tot_init_weight, repulsion_base, n, m):
|
|
29
29
|
"""
|
|
30
30
|
Pick m nodes out of n with repulsion bias:
|
|
31
31
|
- High-degree nodes are already less likely
|
|
@@ -51,7 +51,7 @@ def local_repulsion_choice(G_func, nodes, max_edge, weights, tot_init_weight, n,
|
|
|
51
51
|
used[node] = True
|
|
52
52
|
return used
|
|
53
53
|
|
|
54
|
-
update_repulsion_choice(G_func, nodes, max_edge, weights, n, used, node)
|
|
54
|
+
update_repulsion_choice(G_func, nodes, max_edge, weights, n, used, node, repulsion_base)
|
|
55
55
|
|
|
56
56
|
for _ in range(1, m - 1):
|
|
57
57
|
# Count available
|
|
@@ -79,7 +79,7 @@ def local_repulsion_choice(G_func, nodes, max_edge, weights, tot_init_weight, n,
|
|
|
79
79
|
node += 1
|
|
80
80
|
|
|
81
81
|
# Update answer and weights
|
|
82
|
-
update_repulsion_choice(G_func, nodes, max_edge, weights, n, used, node)
|
|
82
|
+
update_repulsion_choice(G_func, nodes, max_edge, weights, n, used, node, repulsion_base)
|
|
83
83
|
|
|
84
84
|
# Count available
|
|
85
85
|
total_w = 0.0
|
|
@@ -134,7 +134,7 @@ def compute_cut(sample, G_func, nodes, n_qubits):
|
|
|
134
134
|
|
|
135
135
|
|
|
136
136
|
@njit(parallel=True)
|
|
137
|
-
def sample_for_energy(G_func, nodes, max_edge, shots, thresholds, degrees_sum, weights, n):
|
|
137
|
+
def sample_for_energy(G_func, nodes, max_edge, shots, thresholds, degrees_sum, weights, n, repulsion_base):
|
|
138
138
|
shots = max(1, shots >> 1)
|
|
139
139
|
tot_init_weight = weights.sum()
|
|
140
140
|
|
|
@@ -156,7 +156,7 @@ def sample_for_energy(G_func, nodes, max_edge, shots, thresholds, degrees_sum, w
|
|
|
156
156
|
m += 1
|
|
157
157
|
|
|
158
158
|
# Second dimension: permutation within Hamming weight
|
|
159
|
-
sample = local_repulsion_choice(G_func, nodes, max_edge, weights, tot_init_weight, n, m)
|
|
159
|
+
sample = local_repulsion_choice(G_func, nodes, max_edge, weights, tot_init_weight, repulsion_base, n, m)
|
|
160
160
|
solutions[s] = sample
|
|
161
161
|
energies[s] = compute_energy(sample, G_func, nodes, n)
|
|
162
162
|
|
|
@@ -171,7 +171,7 @@ def sample_for_energy(G_func, nodes, max_edge, shots, thresholds, degrees_sum, w
|
|
|
171
171
|
|
|
172
172
|
|
|
173
173
|
@njit(parallel=True)
|
|
174
|
-
def sample_for_cut(G_func, nodes, max_edge, shots, thresholds, degrees_sum, weights, n):
|
|
174
|
+
def sample_for_cut(G_func, nodes, max_edge, shots, thresholds, degrees_sum, weights, n, repulsion_base):
|
|
175
175
|
shots = max(1, shots >> 1)
|
|
176
176
|
tot_init_weight = weights.sum()
|
|
177
177
|
|
|
@@ -193,7 +193,7 @@ def sample_for_cut(G_func, nodes, max_edge, shots, thresholds, degrees_sum, weig
|
|
|
193
193
|
m += 1
|
|
194
194
|
|
|
195
195
|
# Second dimension: permutation within Hamming weight
|
|
196
|
-
sample = local_repulsion_choice(G_func, nodes, max_edge, weights, tot_init_weight, n, m)
|
|
196
|
+
sample = local_repulsion_choice(G_func, nodes, max_edge, weights, tot_init_weight, repulsion_base, n, m)
|
|
197
197
|
solutions[s] = sample
|
|
198
198
|
cuts[s] = compute_cut(sample, G_func, nodes, n)
|
|
199
199
|
|
|
@@ -221,6 +221,7 @@ def init_J_and_z(G_func, nodes):
|
|
|
221
221
|
if val != 0.0:
|
|
222
222
|
degree += 1
|
|
223
223
|
J += val
|
|
224
|
+
val = abs(val)
|
|
224
225
|
if val > G_max:
|
|
225
226
|
G_max = val
|
|
226
227
|
J = -J / degree if degree > 0 else 0
|
|
@@ -231,8 +232,8 @@ def init_J_and_z(G_func, nodes):
|
|
|
231
232
|
|
|
232
233
|
|
|
233
234
|
@njit
|
|
234
|
-
def cpu_footer(shots, quality, n_qubits, G_func, nodes, is_spin_glass, anneal_t, anneal_h):
|
|
235
|
-
J_eff, degrees,
|
|
235
|
+
def cpu_footer(shots, quality, n_qubits, G_func, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
|
|
236
|
+
J_eff, degrees, max_edge = init_J_and_z(G_func, nodes)
|
|
236
237
|
hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
|
|
237
238
|
max_edge = degrees.sum()
|
|
238
239
|
|
|
@@ -240,9 +241,9 @@ def cpu_footer(shots, quality, n_qubits, G_func, nodes, is_spin_glass, anneal_t,
|
|
|
240
241
|
J_eff = 1.0 / (1.0 + epsilon - J_eff)
|
|
241
242
|
|
|
242
243
|
if is_spin_glass:
|
|
243
|
-
best_solution, best_value = sample_for_energy(G_func, nodes,
|
|
244
|
+
best_solution, best_value = sample_for_energy(G_func, nodes, max_edge, shots, hamming_prob, max_edge, J_eff, n_qubits, repulsion_base)
|
|
244
245
|
else:
|
|
245
|
-
best_solution, best_value = sample_for_cut(G_func, nodes,
|
|
246
|
+
best_solution, best_value = sample_for_cut(G_func, nodes, max_edge, shots, hamming_prob, max_edge, J_eff, n_qubits, repulsion_base)
|
|
246
247
|
|
|
247
248
|
bit_string, l, r = get_cut(best_solution, nodes)
|
|
248
249
|
|
|
@@ -256,7 +257,8 @@ def maxcut_tfim_streaming(
|
|
|
256
257
|
shots=None,
|
|
257
258
|
is_spin_glass=False,
|
|
258
259
|
anneal_t=None,
|
|
259
|
-
anneal_h=None
|
|
260
|
+
anneal_h=None,
|
|
261
|
+
repulsion_base=None
|
|
260
262
|
):
|
|
261
263
|
wgs = opencl_context.work_group_size
|
|
262
264
|
n_qubits = len(nodes)
|
|
@@ -276,7 +278,11 @@ def maxcut_tfim_streaming(
|
|
|
276
278
|
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
277
279
|
|
|
278
280
|
if quality is None:
|
|
279
|
-
quality =
|
|
281
|
+
quality = 5
|
|
282
|
+
|
|
283
|
+
if shots is None:
|
|
284
|
+
# Number of measurement shots
|
|
285
|
+
shots = n_qubits << quality
|
|
280
286
|
|
|
281
287
|
if anneal_t is None:
|
|
282
288
|
anneal_t = 8.0
|
|
@@ -284,8 +290,7 @@ def maxcut_tfim_streaming(
|
|
|
284
290
|
if anneal_h is None:
|
|
285
291
|
anneal_h = 8.0
|
|
286
292
|
|
|
287
|
-
if
|
|
288
|
-
|
|
289
|
-
shots = n_qubits << quality
|
|
293
|
+
if repulsion_base is None:
|
|
294
|
+
repulsion_base = 4.0
|
|
290
295
|
|
|
291
|
-
return cpu_footer(shots, quality, n_qubits, G_func, nodes, is_spin_glass, anneal_t, anneal_h)
|
|
296
|
+
return cpu_footer(shots, quality, n_qubits, G_func, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base)
|
|
@@ -166,7 +166,8 @@ def spin_glass_solver(
|
|
|
166
166
|
is_combo_maxcut_gpu=True,
|
|
167
167
|
is_spin_glass=True,
|
|
168
168
|
anneal_t=None,
|
|
169
|
-
anneal_h=None
|
|
169
|
+
anneal_h=None,
|
|
170
|
+
repulsion_base=None
|
|
170
171
|
):
|
|
171
172
|
dtype = opencl_context.dtype
|
|
172
173
|
nodes = None
|
|
@@ -209,7 +210,7 @@ def spin_glass_solver(
|
|
|
209
210
|
elif isinstance(best_guess, list):
|
|
210
211
|
bitstring = "".join(["1" if b else "0" for b in best_guess])
|
|
211
212
|
else:
|
|
212
|
-
bitstring, _, _ = maxcut_tfim(G_m, quality=quality, shots=shots, is_spin_glass=is_spin_glass, anneal_t=anneal_t, anneal_h=anneal_h)
|
|
213
|
+
bitstring, _, _ = maxcut_tfim(G_m, quality=quality, shots=shots, is_spin_glass=is_spin_glass, anneal_t=anneal_t, anneal_h=anneal_h, repulsion_base=repulsion_base)
|
|
213
214
|
best_theta = np.array([b == "1" for b in list(bitstring)], dtype=np.bool_)
|
|
214
215
|
|
|
215
216
|
if is_combo_maxcut_gpu and IS_OPENCL_AVAILABLE:
|
|
@@ -185,7 +185,8 @@ def spin_glass_solver_sparse(
|
|
|
185
185
|
is_combo_maxcut_gpu=True,
|
|
186
186
|
is_spin_glass=True,
|
|
187
187
|
anneal_t=None,
|
|
188
|
-
anneal_h=None
|
|
188
|
+
anneal_h=None,
|
|
189
|
+
repulsion_base=None
|
|
189
190
|
):
|
|
190
191
|
dtype = opencl_context.dtype
|
|
191
192
|
nodes = None
|
|
@@ -228,7 +229,7 @@ def spin_glass_solver_sparse(
|
|
|
228
229
|
elif isinstance(best_guess, list):
|
|
229
230
|
bitstring = "".join(["1" if b else "0" for b in best_guess])
|
|
230
231
|
else:
|
|
231
|
-
bitstring, _, _ = maxcut_tfim_sparse(G_m, quality=quality, shots=shots, is_spin_glass=is_spin_glass, anneal_t=anneal_t, anneal_h=anneal_h)
|
|
232
|
+
bitstring, _, _ = maxcut_tfim_sparse(G_m, quality=quality, shots=shots, is_spin_glass=is_spin_glass, anneal_t=anneal_t, anneal_h=anneal_h, repulsion_base=repulsion_base)
|
|
232
233
|
best_theta = np.array([b == "1" for b in list(bitstring)], dtype=np.bool_)
|
|
233
234
|
|
|
234
235
|
if is_combo_maxcut_gpu and IS_OPENCL_AVAILABLE:
|
|
@@ -74,7 +74,8 @@ def spin_glass_solver_streaming(
|
|
|
74
74
|
best_guess=None,
|
|
75
75
|
is_spin_glass=True,
|
|
76
76
|
anneal_t=None,
|
|
77
|
-
anneal_h=None
|
|
77
|
+
anneal_h=None,
|
|
78
|
+
repulsion_base=None
|
|
78
79
|
):
|
|
79
80
|
dtype = opencl_context.dtype
|
|
80
81
|
n_qubits = len(nodes)
|
|
@@ -101,7 +102,7 @@ def spin_glass_solver_streaming(
|
|
|
101
102
|
elif isinstance(best_guess, list):
|
|
102
103
|
bitstring = "".join(["1" if b else "0" for b in best_guess])
|
|
103
104
|
else:
|
|
104
|
-
bitstring, _, _ = maxcut_tfim_streaming(G_func, nodes, quality=quality, shots=shots, is_spin_glass=is_spin_glass, anneal_t=anneal_t, anneal_h=anneal_h)
|
|
105
|
+
bitstring, _, _ = maxcut_tfim_streaming(G_func, nodes, quality=quality, shots=shots, is_spin_glass=is_spin_glass, anneal_t=anneal_t, anneal_h=anneal_h, repulsion_base=repulsion_base)
|
|
105
106
|
best_theta = np.array([b == "1" for b in list(bitstring)], dtype=np.bool_)
|
|
106
107
|
|
|
107
108
|
min_energy = compute_energy(best_theta, G_func, nodes)
|
|
@@ -629,6 +629,7 @@ def tsp_symmetric(
|
|
|
629
629
|
shots=None,
|
|
630
630
|
anneal_t=None,
|
|
631
631
|
anneal_h=None,
|
|
632
|
+
repulsion_base=None,
|
|
632
633
|
monte_carlo=True,
|
|
633
634
|
k_neighbors=20,
|
|
634
635
|
is_cyclic=True,
|
|
@@ -680,7 +681,7 @@ def tsp_symmetric(
|
|
|
680
681
|
for _ in range(multi_start):
|
|
681
682
|
bits = ([], [])
|
|
682
683
|
while (len(bits[0]) == 0) or (len(bits[1]) == 0):
|
|
683
|
-
_, cut_value, bits = maxcut_tfim(G_m, quality=quality, shots=shots, anneal_t=anneal_t, anneal_h=anneal_h)
|
|
684
|
+
_, cut_value, bits = maxcut_tfim(G_m, quality=quality, shots=shots, anneal_t=anneal_t, anneal_h=anneal_h, repulsion_base=repulsion_base)
|
|
684
685
|
if cut_value > best_cut:
|
|
685
686
|
best_cut = cut_value
|
|
686
687
|
a, b = bits
|
|
@@ -697,8 +698,8 @@ def tsp_symmetric(
|
|
|
697
698
|
|
|
698
699
|
if monte_carlo and is_parallel and (parallel_level < max_parallel_level) and (len(a) > 3) and (len(b) > 3):
|
|
699
700
|
with ProcessPoolExecutor(max_workers=1) as executor:
|
|
700
|
-
f = executor.submit(tsp_symmetric, G_a, None, None, quality, shots, anneal_t, anneal_h, True, 0, False, multi_start, False, True, parallel_level + 1)
|
|
701
|
-
sol_b = tsp_symmetric(G_b, None, None, quality, shots, anneal_t, anneal_h, True, 0, False, multi_start, False, True, parallel_level + 1)
|
|
701
|
+
f = executor.submit(tsp_symmetric, G_a, None, None, quality, shots, anneal_t, anneal_h, repulsion_base, True, 0, False, multi_start, False, True, parallel_level + 1)
|
|
702
|
+
sol_b = tsp_symmetric(G_b, None, None, quality, shots, anneal_t, anneal_h, repulsion_base, True, 0, False, multi_start, False, True, parallel_level + 1)
|
|
702
703
|
sol_a = f.result()
|
|
703
704
|
else:
|
|
704
705
|
sol_a = tsp_symmetric(
|
|
@@ -707,6 +708,7 @@ def tsp_symmetric(
|
|
|
707
708
|
shots=shots,
|
|
708
709
|
anneal_t=anneal_t,
|
|
709
710
|
anneal_h=anneal_h,
|
|
711
|
+
repulsion_base=repulsion_base,
|
|
710
712
|
monte_carlo=monte_carlo,
|
|
711
713
|
is_cyclic=False,
|
|
712
714
|
is_top_level=False,
|
|
@@ -720,6 +722,7 @@ def tsp_symmetric(
|
|
|
720
722
|
shots=shots,
|
|
721
723
|
anneal_t=anneal_t,
|
|
722
724
|
anneal_h=anneal_h,
|
|
725
|
+
repulsion_base=repulsion_base,
|
|
723
726
|
monte_carlo=monte_carlo,
|
|
724
727
|
is_cyclic=False,
|
|
725
728
|
is_top_level=False,
|
|
@@ -832,6 +835,7 @@ def tsp_asymmetric(
|
|
|
832
835
|
shots=None,
|
|
833
836
|
anneal_t=None,
|
|
834
837
|
anneal_h=None,
|
|
838
|
+
repulsion_base=None,
|
|
835
839
|
monte_carlo=True,
|
|
836
840
|
k_neighbors=20,
|
|
837
841
|
is_cyclic=True,
|
|
@@ -887,7 +891,7 @@ def tsp_asymmetric(
|
|
|
887
891
|
for _ in range(multi_start):
|
|
888
892
|
bits = ([], [])
|
|
889
893
|
while (len(bits[0]) == 0) or (len(bits[1]) == 0):
|
|
890
|
-
_, cut_value, bits = maxcut_tfim((G_m + G_m.T) / 2, quality=quality, shots=shots, anneal_t=anneal_t, anneal_h=anneal_h)
|
|
894
|
+
_, cut_value, bits = maxcut_tfim((G_m + G_m.T) / 2, quality=quality, shots=shots, anneal_t=anneal_t, anneal_h=anneal_h, repulsion_base=repulsion_base)
|
|
891
895
|
if cut_value > best_cut:
|
|
892
896
|
best_cut = cut_value
|
|
893
897
|
a, b = bits
|
|
@@ -904,8 +908,8 @@ def tsp_asymmetric(
|
|
|
904
908
|
|
|
905
909
|
if monte_carlo and is_parallel and (parallel_level < max_parallel_level) and (len(a) > 2) and (len(b) > 2):
|
|
906
910
|
with ProcessPoolExecutor(max_workers=1) as executor:
|
|
907
|
-
f = executor.submit(tsp_asymmetric, G_a, None, None, quality, shots, anneal_t, anneal_h, True, 0, False, multi_start, False, True, parallel_level + 1)
|
|
908
|
-
sol_b = tsp_asymmetric(G_b, None, None, quality, shots, anneal_t, anneal_h, True, 0, False, multi_start, False, True, parallel_level + 1)
|
|
911
|
+
f = executor.submit(tsp_asymmetric, G_a, None, None, quality, shots, anneal_t, anneal_h, repulsion_base, True, 0, False, multi_start, False, True, parallel_level + 1)
|
|
912
|
+
sol_b = tsp_asymmetric(G_b, None, None, quality, shots, anneal_t, anneal_h, repulsion_base, True, 0, False, multi_start, False, True, parallel_level + 1)
|
|
909
913
|
sol_a = f.result()
|
|
910
914
|
else:
|
|
911
915
|
sol_a = tsp_asymmetric(
|
|
@@ -914,6 +918,7 @@ def tsp_asymmetric(
|
|
|
914
918
|
shots=shots,
|
|
915
919
|
anneal_t=anneal_t,
|
|
916
920
|
anneal_h=anneal_h,
|
|
921
|
+
repulsion_base=repulsion_base,
|
|
917
922
|
monte_carlo=monte_carlo,
|
|
918
923
|
is_cyclic=False,
|
|
919
924
|
is_top_level=False,
|
|
@@ -927,6 +932,7 @@ def tsp_asymmetric(
|
|
|
927
932
|
shots=shots,
|
|
928
933
|
anneal_t=anneal_t,
|
|
929
934
|
anneal_h=anneal_h,
|
|
935
|
+
repulsion_base=repulsion_base,
|
|
930
936
|
monte_carlo=monte_carlo,
|
|
931
937
|
is_cyclic=False,
|
|
932
938
|
is_top_level=False,
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='pyqrackising',
|
|
54
|
-
version='8.
|
|
54
|
+
version='8.5.0',
|
|
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
|