PyQrackIsing 8.14.8__tar.gz → 8.15.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.14.8/pyqrackising.egg-info → pyqrackising-8.15.0}/PKG-INFO +1 -1
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/README.md +4 -4
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/pyproject.toml +1 -1
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/pyqrackising/maxcut_tfim.py +41 -58
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/pyqrackising/maxcut_tfim_sparse.py +46 -108
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/pyqrackising/maxcut_tfim_streaming.py +21 -41
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/pyqrackising/maxcut_tfim_util.py +0 -12
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/pyqrackising/spin_glass_solver.py +1 -3
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/pyqrackising/spin_glass_solver_sparse.py +1 -3
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/pyqrackising/spin_glass_solver_streaming.py +1 -2
- {pyqrackising-8.14.8 → pyqrackising-8.15.0/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/setup.py +1 -1
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/LICENSE.md +0 -0
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/MANIFEST.in +0 -0
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/pyqrackising/__init__.py +0 -0
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/pyqrackising/spin_glass_solver_util.py +0 -0
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/pyqrackising/tsp.py +0 -0
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/pyqrackising/tsp_maxcut.py +0 -0
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-8.14.8 → pyqrackising-8.15.0}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-8.14.8 → pyqrackising-8.15.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=6, shots=None, is_spin_glass=False, anneal_t=8.0, anneal_h=8.0, repulsion_base=5.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 `6`, 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
|
|
@@ -73,10 +73,10 @@ def generate_spin_glass_graph(n_nodes=16, degree=3, seed=None):
|
|
|
73
73
|
|
|
74
74
|
|
|
75
75
|
G = generate_spin_glass_graph(n_nodes=64, seed=42)
|
|
76
|
-
solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=
|
|
76
|
+
solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=6, shots=None, anneal_t=8.0, anneal_h=8.0, is_spin_glass=True, best_guess=None, is_combo_maxcut_gpu=True, min_order=1, max_order=None, reheat_tries=0)
|
|
77
77
|
# solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, best_guess=maxcut_tfim(G, quality=6)[0])
|
|
78
78
|
```
|
|
79
|
-
We also provide `spin_glass_solver_sparse(G)` and `spin_glass_solver_streaming(G_func, nodes)`. `is_combo_maxcut_gpu` controls whether gradient descent optimization is done on GPU (which is costly and the only GPU-based feature). `best_guess` gives the option to seed the algorithm with a best guess as to the maximal cut (as an integer, binary string, or list of booleans). By default, `spin_glass_solver()` uses `maxcut_tfim(G)` with passed-through `quality` as `best_guess`, which typically works well, but it could be seeded with higher `maxcut_tfim()` `quality` or Goemans-Williamson, for example. `min_order` determines the minimum order of local search and defaults to `1`, meaning it will perform a local search on at least single nodes (or `2` for pairs of nodes, or `3` for triplets of nodes, etc). `max_order` sets a ceiling on adaptive search for productive local search "order," and defaults to `None` for no ceiling. After the initial optimization round, `reheat_tries` will randomly flip `log2(n)` bits out of `n` count of total nodes and re-optimize, similar to "simulated annealing," for as many additional rounds as the `reheat_tries` value (which defaults to `0`). This function is designed with a sign convention for weights such that it can immediately be used as a MAXCUT solver itself: you might need to reverse the sign convention on your weights for spin glass graphs, but this is only convention.
|
|
79
|
+
We also provide `spin_glass_solver_sparse(G)` and `spin_glass_solver_streaming(G_func, nodes)`. `is_combo_maxcut_gpu` controls whether gradient descent optimization is done on GPU (which is costly and the only GPU-based feature). `best_guess` gives the option to seed the algorithm with a best guess as to the maximal cut (as an integer, binary string, or list of booleans). By default, `spin_glass_solver()` uses `maxcut_tfim(G)` with passed-through `quality` as `best_guess`, which typically works well, but it could be seeded with higher `maxcut_tfim()` `quality` or Goemans-Williamson, for example. `min_order` determines the minimum order of local search and defaults to `1`, meaning it will perform a local search on at least single nodes (or `2` for pairs of nodes, or `3` for triplets of nodes, etc). `max_order` sets a ceiling on adaptive search for productive local search "order," and defaults to `None` for no ceiling. After the initial optimization round, `reheat_tries` will randomly flip `log2(n)` bits out of `n` count of total nodes and re-optimize, similar to "simulated annealing," for as many additional rounds as the `reheat_tries` value (which defaults to `0`). `is_spin_glass` controls whether the solver optimizes for cut value or spin-glass energy. This function is designed with a sign convention for weights such that it can immediately be used as a MAXCUT solver itself: you might need to reverse the sign convention on your weights for spin glass graphs, but this is only convention.
|
|
80
80
|
|
|
81
81
|
From the MAXCUT solvers, we provide a (recursive) Traveling Salesman Problem (TSP) solver:
|
|
82
82
|
```py
|
|
@@ -10,7 +10,7 @@ build-backend = "setuptools.build_meta"
|
|
|
10
10
|
|
|
11
11
|
[project]
|
|
12
12
|
name = "pyqrackising"
|
|
13
|
-
version = "8.
|
|
13
|
+
version = "8.15.0"
|
|
14
14
|
requires-python = ">=3.8"
|
|
15
15
|
description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
|
|
16
16
|
readme = {file = "README.txt", content-type = "text/markdown"}
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import math
|
|
2
1
|
import networkx as nx
|
|
3
2
|
import numpy as np
|
|
4
|
-
import os
|
|
5
3
|
from numba import njit, prange
|
|
6
4
|
|
|
7
|
-
from .maxcut_tfim_util import convert_bool_to_uint, get_cut, get_cut_base, make_G_m_buf, make_theta_buf, maxcut_hamming_cdf, opencl_context, sample_mag, setup_opencl, bit_pick
|
|
5
|
+
from .maxcut_tfim_util import convert_bool_to_uint, get_cut, get_cut_base, make_G_m_buf, make_theta_buf, maxcut_hamming_cdf, opencl_context, sample_mag, setup_opencl, bit_pick
|
|
8
6
|
|
|
9
7
|
IS_OPENCL_AVAILABLE = True
|
|
10
8
|
try:
|
|
@@ -19,7 +17,7 @@ wgs = opencl_context.work_group_size
|
|
|
19
17
|
|
|
20
18
|
|
|
21
19
|
@njit
|
|
22
|
-
def update_repulsion_choice(G_m,
|
|
20
|
+
def update_repulsion_choice(G_m, weights, n, used, node, repulsion_base):
|
|
23
21
|
# Select node
|
|
24
22
|
used[node] = True
|
|
25
23
|
|
|
@@ -27,41 +25,34 @@ def update_repulsion_choice(G_m, max_edge, weights, n, used, node, repulsion_bas
|
|
|
27
25
|
for nbr in range(n):
|
|
28
26
|
if used[nbr]:
|
|
29
27
|
continue
|
|
30
|
-
weights[nbr] *= repulsion_base ** (-G_m[node, nbr]
|
|
28
|
+
weights[nbr] *= repulsion_base ** (-G_m[node, nbr])
|
|
31
29
|
|
|
32
30
|
|
|
33
31
|
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
34
32
|
@njit
|
|
35
|
-
def local_repulsion_choice(G_m,
|
|
33
|
+
def local_repulsion_choice(G_m, repulsion_base, n, m, s):
|
|
36
34
|
"""
|
|
37
35
|
Pick m nodes out of n with repulsion bias:
|
|
38
36
|
- High-degree nodes are already less likely
|
|
39
37
|
- After choosing a node, its neighbors' probabilities are further reduced
|
|
40
|
-
adjacency_data, adjacency_rows: CSR-format sparse adjacency data
|
|
41
|
-
weights: float32 array of shape (n,)
|
|
42
38
|
"""
|
|
43
39
|
|
|
44
|
-
weights = weights.copy()
|
|
45
40
|
used = np.zeros(n, dtype=np.bool_) # False = available, True = used
|
|
46
41
|
|
|
47
42
|
# First bit:
|
|
48
|
-
node =
|
|
49
|
-
|
|
43
|
+
node = s % n
|
|
50
44
|
if m == 1:
|
|
51
45
|
used[node] = True
|
|
52
46
|
return used
|
|
53
47
|
|
|
54
|
-
|
|
48
|
+
weights = np.ones(n, dtype=np.float64)
|
|
49
|
+
update_repulsion_choice(G_m, weights, n, used, node, repulsion_base)
|
|
55
50
|
|
|
56
51
|
for _ in range(1, m - 1):
|
|
57
52
|
node = bit_pick(weights, used, n)
|
|
53
|
+
update_repulsion_choice(G_m, weights, n, used, node, repulsion_base)
|
|
58
54
|
|
|
59
|
-
# Update answer and weights
|
|
60
|
-
update_repulsion_choice(G_m, max_edge, weights, n, used, node, repulsion_base)
|
|
61
|
-
|
|
62
|
-
# Count available
|
|
63
55
|
node = bit_pick(weights, used, n)
|
|
64
|
-
|
|
65
56
|
used[node] = True
|
|
66
57
|
|
|
67
58
|
return used
|
|
@@ -90,10 +81,9 @@ def compute_cut(sample, G_m, n_qubits):
|
|
|
90
81
|
|
|
91
82
|
|
|
92
83
|
@njit(parallel=True)
|
|
93
|
-
def sample_measurement(G_m,
|
|
84
|
+
def sample_measurement(G_m, shots, thresholds, repulsion_base, is_spin_glass):
|
|
94
85
|
shots = max(1, shots >> 1)
|
|
95
86
|
n = len(G_m)
|
|
96
|
-
tot_init_weight = weights.sum()
|
|
97
87
|
|
|
98
88
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
99
89
|
energies = np.empty(shots, dtype=dtype)
|
|
@@ -110,7 +100,7 @@ def sample_measurement(G_m, max_edge, shots, thresholds, weights, repulsion_base
|
|
|
110
100
|
m = sample_mag(thresholds)
|
|
111
101
|
|
|
112
102
|
# Second dimension: permutation within Hamming weight
|
|
113
|
-
sample = local_repulsion_choice(G_m,
|
|
103
|
+
sample = local_repulsion_choice(G_m, repulsion_base, n, m, s)
|
|
114
104
|
solutions[s] = sample
|
|
115
105
|
energies[s] = compute_energy(sample, G_m, n)
|
|
116
106
|
else:
|
|
@@ -119,7 +109,7 @@ def sample_measurement(G_m, max_edge, shots, thresholds, weights, repulsion_base
|
|
|
119
109
|
m = sample_mag(thresholds)
|
|
120
110
|
|
|
121
111
|
# Second dimension: permutation within Hamming weight
|
|
122
|
-
sample = local_repulsion_choice(G_m,
|
|
112
|
+
sample = local_repulsion_choice(G_m, repulsion_base, n, m, s)
|
|
123
113
|
solutions[s] = sample
|
|
124
114
|
energies[s] = compute_cut(sample, G_m, n)
|
|
125
115
|
|
|
@@ -137,18 +127,17 @@ def sample_measurement(G_m, max_edge, shots, thresholds, weights, repulsion_base
|
|
|
137
127
|
|
|
138
128
|
|
|
139
129
|
@njit(parallel=True)
|
|
140
|
-
def shot_loop(G_m,
|
|
130
|
+
def shot_loop(G_m, thresholds, repulsion_base, n, shots, solutions):
|
|
141
131
|
for s in prange(shots):
|
|
142
132
|
# First dimension: Hamming weight
|
|
143
133
|
m = sample_mag(thresholds)
|
|
144
134
|
# Second dimension: permutation within Hamming weight
|
|
145
|
-
solutions[s] = local_repulsion_choice(G_m,
|
|
135
|
+
solutions[s] = local_repulsion_choice(G_m, repulsion_base, n, m, s)
|
|
146
136
|
|
|
147
137
|
|
|
148
|
-
def sample_for_opencl(G_m, G_m_buf,
|
|
138
|
+
def sample_for_opencl(G_m, G_m_buf, shots, thresholds, repulsion_base, is_spin_glass, is_segmented, segment_size, theta_segment_size):
|
|
149
139
|
shots = ((max(1, shots >> 1) + 31) >> 5) << 5
|
|
150
140
|
n = len(G_m)
|
|
151
|
-
tot_init_weight = weights.sum()
|
|
152
141
|
|
|
153
142
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
154
143
|
|
|
@@ -160,8 +149,8 @@ def sample_for_opencl(G_m, G_m_buf, max_edge, shots, thresholds, weights, repuls
|
|
|
160
149
|
improved = True
|
|
161
150
|
while improved:
|
|
162
151
|
improved = False
|
|
163
|
-
shot_loop(G_m,
|
|
164
|
-
solution, energy = run_cut_opencl(solutions, G_m_buf, is_segmented, segment_size, is_spin_glass, *opencl_args)
|
|
152
|
+
shot_loop(G_m, thresholds, repulsion_base, n, shots, solutions)
|
|
153
|
+
solution, energy = run_cut_opencl(best_energy, solutions, G_m_buf, is_segmented, segment_size, is_spin_glass, *opencl_args)
|
|
165
154
|
if energy > best_energy:
|
|
166
155
|
best_energy = energy
|
|
167
156
|
best_solution = solution.copy()
|
|
@@ -179,54 +168,39 @@ def init_J_and_z(G_m, repulsion_base):
|
|
|
179
168
|
n_qubits = len(G_m)
|
|
180
169
|
degrees = np.empty(n_qubits, dtype=np.uint32)
|
|
181
170
|
J_eff = np.empty(n_qubits, dtype=np.float64)
|
|
182
|
-
weights = np.empty(n_qubits, dtype=np.float64)
|
|
183
|
-
G_max = -float("inf")
|
|
184
171
|
for n in prange(n_qubits):
|
|
185
172
|
degree = 0
|
|
186
173
|
J = 0.0
|
|
187
|
-
weight = 0.0
|
|
188
174
|
for m in range(n_qubits):
|
|
189
|
-
val = G_m[n, m]
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
if val != 0.0:
|
|
195
|
-
degree += 1
|
|
196
|
-
J += val
|
|
175
|
+
val = G_m[n, m] - G_min
|
|
176
|
+
if val <= epsilon:
|
|
177
|
+
continue
|
|
178
|
+
degree += 1
|
|
179
|
+
J += val
|
|
197
180
|
if degree > 0:
|
|
198
181
|
J = -J / degree
|
|
199
|
-
weight = -weight / n_qubits
|
|
200
182
|
degrees[n] = degree
|
|
201
183
|
J_eff[n] = J
|
|
202
|
-
weights[n] = weight
|
|
203
184
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
G_min = abs(G_min)
|
|
207
|
-
G_max = abs(G_max)
|
|
208
|
-
if G_min > G_max:
|
|
209
|
-
G_max = G_min
|
|
210
|
-
|
|
211
|
-
return J_eff, degrees, weights, G_max
|
|
185
|
+
return J_eff, degrees
|
|
212
186
|
|
|
213
187
|
|
|
214
188
|
@njit
|
|
215
189
|
def cpu_footer(shots, quality, n_qubits, G_m, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
|
|
216
|
-
J_eff, degrees
|
|
190
|
+
J_eff, degrees = init_J_and_z(G_m, repulsion_base)
|
|
217
191
|
hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
|
|
218
192
|
|
|
219
193
|
degrees = None
|
|
220
194
|
J_eff = None
|
|
221
195
|
|
|
222
|
-
best_solution, best_value = sample_measurement(G_m,
|
|
196
|
+
best_solution, best_value = sample_measurement(G_m, shots, hamming_prob, repulsion_base, is_spin_glass)
|
|
223
197
|
|
|
224
198
|
bit_string, l, r = get_cut(best_solution, nodes, n_qubits)
|
|
225
199
|
|
|
226
200
|
return bit_string, best_value, (l, r)
|
|
227
201
|
|
|
228
202
|
|
|
229
|
-
def run_cut_opencl(samples, G_m_buf, is_segmented, segment_size, is_spin_glass, local_size, global_size, args_buf, local_energy_buf, local_index_buf, max_energy_host, max_index_host, max_energy_buf, max_index_buf):
|
|
203
|
+
def run_cut_opencl(best_energy, samples, G_m_buf, is_segmented, segment_size, is_spin_glass, local_size, global_size, args_buf, local_energy_buf, local_index_buf, max_energy_host, max_index_host, max_energy_buf, max_index_buf):
|
|
230
204
|
queue = opencl_context.queue
|
|
231
205
|
calculate_cut_kernel = opencl_context.calculate_cut_segmented_kernel if is_segmented else opencl_context.calculate_cut_kernel
|
|
232
206
|
|
|
@@ -269,14 +243,23 @@ def run_cut_opencl(samples, G_m_buf, is_segmented, segment_size, is_spin_glass,
|
|
|
269
243
|
|
|
270
244
|
# Read results
|
|
271
245
|
cl.enqueue_copy(queue, max_energy_host, max_energy_buf)
|
|
272
|
-
cl.enqueue_copy(queue, max_index_host, max_index_buf)
|
|
273
246
|
queue.finish()
|
|
274
247
|
|
|
248
|
+
# Queue read for results we might not need
|
|
249
|
+
cl.enqueue_copy(queue, max_index_host, max_index_buf)
|
|
250
|
+
|
|
275
251
|
# Find global minimum
|
|
276
252
|
best_x = np.argmax(max_energy_host)
|
|
277
|
-
|
|
253
|
+
energy = max_energy_host[best_x]
|
|
254
|
+
|
|
255
|
+
if energy < best_energy:
|
|
256
|
+
# No improvement: we can exit early
|
|
257
|
+
return samples[0], max_energy_host[0]
|
|
258
|
+
|
|
259
|
+
# We need the best index
|
|
260
|
+
queue.finish()
|
|
278
261
|
|
|
279
|
-
return samples[
|
|
262
|
+
return samples[max_index_host[best_x]], energy
|
|
280
263
|
|
|
281
264
|
def maxcut_tfim(
|
|
282
265
|
G,
|
|
@@ -318,7 +301,7 @@ def maxcut_tfim(
|
|
|
318
301
|
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
319
302
|
|
|
320
303
|
if quality is None:
|
|
321
|
-
quality =
|
|
304
|
+
quality = 6
|
|
322
305
|
|
|
323
306
|
if shots is None:
|
|
324
307
|
# Number of measurement shots
|
|
@@ -331,7 +314,7 @@ def maxcut_tfim(
|
|
|
331
314
|
anneal_h = 8.0
|
|
332
315
|
|
|
333
316
|
if repulsion_base is None:
|
|
334
|
-
repulsion_base =
|
|
317
|
+
repulsion_base = 5.0
|
|
335
318
|
|
|
336
319
|
is_opencl = is_maxcut_gpu and IS_OPENCL_AVAILABLE
|
|
337
320
|
|
|
@@ -349,13 +332,13 @@ def maxcut_tfim(
|
|
|
349
332
|
is_segmented = ((G_m.nbytes << 1) > opencl_context.max_alloc) or ((theta_segment_size << 3) > opencl_context.max_alloc)
|
|
350
333
|
G_m_buf = make_G_m_buf(G_m, is_segmented, segment_size)
|
|
351
334
|
|
|
352
|
-
J_eff, degrees
|
|
335
|
+
J_eff, degrees = init_J_and_z(G_m, repulsion_base)
|
|
353
336
|
hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
|
|
354
337
|
|
|
355
338
|
degrees = None
|
|
356
339
|
J_eff = None
|
|
357
340
|
|
|
358
|
-
best_solution, best_value = sample_for_opencl(G_m, G_m_buf,
|
|
341
|
+
best_solution, best_value = sample_for_opencl(G_m, G_m_buf, shots, hamming_prob, repulsion_base, is_spin_glass, is_segmented, segment_size, theta_segment_size)
|
|
359
342
|
|
|
360
343
|
bit_string, l, r = get_cut(best_solution, nodes, n_qubits)
|
|
361
344
|
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import math
|
|
2
1
|
import networkx as nx
|
|
3
2
|
import numpy as np
|
|
4
|
-
import os
|
|
5
3
|
from numba import njit, prange
|
|
6
4
|
|
|
7
|
-
from .maxcut_tfim_util import binary_search, convert_bool_to_uint, get_cut, get_cut_base, make_G_m_csr_buf, make_theta_buf, maxcut_hamming_cdf, opencl_context, sample_mag, setup_opencl, bit_pick,
|
|
5
|
+
from .maxcut_tfim_util import binary_search, convert_bool_to_uint, get_cut, get_cut_base, make_G_m_csr_buf, make_theta_buf, maxcut_hamming_cdf, opencl_context, sample_mag, setup_opencl, bit_pick, to_scipy_sparse_upper_triangular
|
|
8
6
|
|
|
9
7
|
IS_OPENCL_AVAILABLE = True
|
|
10
8
|
try:
|
|
@@ -19,7 +17,7 @@ wgs = opencl_context.work_group_size
|
|
|
19
17
|
|
|
20
18
|
|
|
21
19
|
@njit
|
|
22
|
-
def update_repulsion_choice(G_data, G_rows, G_cols,
|
|
20
|
+
def update_repulsion_choice(G_data, G_rows, G_cols, weights, n, used, node, repulsion_base):
|
|
23
21
|
# Select node
|
|
24
22
|
used[node] = True
|
|
25
23
|
|
|
@@ -28,7 +26,7 @@ def update_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, n, used,
|
|
|
28
26
|
nbr = G_cols[j]
|
|
29
27
|
if used[nbr]:
|
|
30
28
|
continue
|
|
31
|
-
weights[nbr] *= repulsion_base ** (-G_data[j]
|
|
29
|
+
weights[nbr] *= repulsion_base ** (-G_data[j])
|
|
32
30
|
|
|
33
31
|
for nbr in range(node):
|
|
34
32
|
if used[nbr]:
|
|
@@ -37,40 +35,34 @@ def update_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, n, used,
|
|
|
37
35
|
end = G_rows[nbr + 1]
|
|
38
36
|
j = binary_search(G_cols[start:end], node) + start
|
|
39
37
|
if j < end:
|
|
40
|
-
weights[nbr] *= repulsion_base ** (-G_data[j]
|
|
38
|
+
weights[nbr] *= repulsion_base ** (-G_data[j])
|
|
41
39
|
|
|
42
40
|
|
|
43
41
|
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
44
42
|
@njit
|
|
45
|
-
def local_repulsion_choice(G_data, G_rows, G_cols,
|
|
43
|
+
def local_repulsion_choice(G_data, G_rows, G_cols, repulsion_base, n, m, s):
|
|
46
44
|
"""
|
|
47
45
|
Pick m nodes out of n with repulsion bias:
|
|
48
46
|
- High-degree nodes are already less likely
|
|
49
47
|
- After choosing a node, its neighbors' probabilities are further reduced
|
|
50
|
-
adjacency_data, adjacency_rows: CSR-format sparse adjacency data
|
|
51
|
-
weights: float32 array of shape (n,)
|
|
52
48
|
"""
|
|
53
49
|
|
|
54
|
-
weights = weights.copy()
|
|
55
50
|
used = np.zeros(n, dtype=np.bool_) # False = available, True = used
|
|
56
51
|
|
|
57
52
|
# First bit:
|
|
58
|
-
node =
|
|
59
|
-
|
|
53
|
+
node = s % n
|
|
60
54
|
if m == 1:
|
|
61
55
|
used[node] = True
|
|
62
56
|
return used
|
|
63
57
|
|
|
64
|
-
|
|
58
|
+
weights = np.ones(n, dtype=np.float64)
|
|
59
|
+
update_repulsion_choice(G_data, G_rows, G_cols, weights, n, used, node, repulsion_base)
|
|
65
60
|
|
|
66
61
|
for _ in range(1, m - 1):
|
|
67
62
|
node = bit_pick(weights, used, n)
|
|
68
|
-
|
|
69
|
-
# Update answer and weights
|
|
70
|
-
update_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, n, used, node, repulsion_base)
|
|
63
|
+
update_repulsion_choice(G_data, G_rows, G_cols, weights, n, used, node, repulsion_base)
|
|
71
64
|
|
|
72
65
|
node = bit_pick(weights, used, n)
|
|
73
|
-
|
|
74
66
|
used[node] = True
|
|
75
67
|
|
|
76
68
|
return used
|
|
@@ -82,9 +74,8 @@ def compute_energy(sample, G_data, G_rows, G_cols, n_qubits):
|
|
|
82
74
|
for u in range(n_qubits):
|
|
83
75
|
u_bit = sample[u]
|
|
84
76
|
for col in range(G_rows[u], G_rows[u + 1]):
|
|
85
|
-
v = G_cols[col]
|
|
86
77
|
val = G_data[col]
|
|
87
|
-
energy += val if u_bit == sample[
|
|
78
|
+
energy += val if u_bit == sample[G_cols[col]] else -val
|
|
88
79
|
|
|
89
80
|
return -energy
|
|
90
81
|
|
|
@@ -97,18 +88,16 @@ def compute_cut(sample, G_data, G_rows, G_cols, n_qubits):
|
|
|
97
88
|
for u in s:
|
|
98
89
|
u_bit = sample[u]
|
|
99
90
|
for col in range(G_rows[u], G_rows[u + 1]):
|
|
100
|
-
|
|
101
|
-
if u_bit != sample[v]:
|
|
91
|
+
if u_bit != sample[G_cols[col]]:
|
|
102
92
|
cut += G_data[col]
|
|
103
93
|
|
|
104
94
|
return cut
|
|
105
95
|
|
|
106
96
|
|
|
107
97
|
@njit(parallel=True)
|
|
108
|
-
def sample_measurement(G_data, G_rows, G_cols,
|
|
98
|
+
def sample_measurement(G_data, G_rows, G_cols, shots, thresholds, repulsion_base, is_spin_glass):
|
|
109
99
|
shots = max(1, shots >> 1)
|
|
110
100
|
n = G_rows.shape[0] - 1
|
|
111
|
-
tot_init_weight = weights.sum()
|
|
112
101
|
|
|
113
102
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
114
103
|
energies = np.empty(shots, dtype=dtype)
|
|
@@ -125,7 +114,7 @@ def sample_measurement(G_data, G_rows, G_cols, max_edge, shots, thresholds, weig
|
|
|
125
114
|
m = sample_mag(thresholds)
|
|
126
115
|
|
|
127
116
|
# Second dimension: permutation within Hamming weight
|
|
128
|
-
sample = local_repulsion_choice(G_data, G_rows, G_cols,
|
|
117
|
+
sample = local_repulsion_choice(G_data, G_rows, G_cols, repulsion_base, n, m, s)
|
|
129
118
|
solutions[s] = sample
|
|
130
119
|
energies[s] = compute_energy(sample, G_data, G_rows, G_cols, n)
|
|
131
120
|
else:
|
|
@@ -134,7 +123,7 @@ def sample_measurement(G_data, G_rows, G_cols, max_edge, shots, thresholds, weig
|
|
|
134
123
|
m = sample_mag(thresholds)
|
|
135
124
|
|
|
136
125
|
# Second dimension: permutation within Hamming weight
|
|
137
|
-
sample = local_repulsion_choice(G_data, G_rows, G_cols,
|
|
126
|
+
sample = local_repulsion_choice(G_data, G_rows, G_cols, repulsion_base, n, m, s)
|
|
138
127
|
solutions[s] = sample
|
|
139
128
|
energies[s] = compute_cut(sample, G_data, G_rows, G_cols, n)
|
|
140
129
|
|
|
@@ -152,18 +141,17 @@ def sample_measurement(G_data, G_rows, G_cols, max_edge, shots, thresholds, weig
|
|
|
152
141
|
|
|
153
142
|
|
|
154
143
|
@njit(parallel=True)
|
|
155
|
-
def shot_loop(G_data, G_rows, G_cols,
|
|
144
|
+
def shot_loop(G_data, G_rows, G_cols, thresholds, repulsion_base, n, shots, solutions):
|
|
156
145
|
for s in prange(shots):
|
|
157
146
|
# First dimension: Hamming weight
|
|
158
147
|
m = sample_mag(thresholds)
|
|
159
148
|
# Second dimension: permutation within Hamming weight
|
|
160
|
-
solutions[s] = local_repulsion_choice(G_data, G_rows, G_cols,
|
|
149
|
+
solutions[s] = local_repulsion_choice(G_data, G_rows, G_cols, repulsion_base, n, m, s)
|
|
161
150
|
|
|
162
151
|
|
|
163
|
-
def sample_for_opencl(G_data, G_rows, G_cols, G_data_buf, G_rows_buf, G_cols_buf,
|
|
152
|
+
def sample_for_opencl(G_data, G_rows, G_cols, G_data_buf, G_rows_buf, G_cols_buf, shots, thresholds, repulsion_base, is_spin_glass, is_segmented, segment_size, theta_segment_size):
|
|
164
153
|
shots = ((max(1, shots >> 1) + 31) >> 5) << 5
|
|
165
154
|
n = G_rows.shape[0] - 1
|
|
166
|
-
tot_init_weight = weights.sum()
|
|
167
155
|
|
|
168
156
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
169
157
|
|
|
@@ -175,8 +163,8 @@ def sample_for_opencl(G_data, G_rows, G_cols, G_data_buf, G_rows_buf, G_cols_buf
|
|
|
175
163
|
improved = True
|
|
176
164
|
while improved:
|
|
177
165
|
improved = False
|
|
178
|
-
shot_loop(G_data, G_rows, G_cols,
|
|
179
|
-
solution, energy = run_cut_opencl(solutions, G_data_buf, G_rows_buf, G_cols_buf, is_segmented, segment_size, is_spin_glass, *opencl_args)
|
|
166
|
+
shot_loop(G_data, G_rows, G_cols, thresholds, repulsion_base, n, shots, solutions)
|
|
167
|
+
solution, energy = run_cut_opencl(best_energy, solutions, G_data_buf, G_rows_buf, G_cols_buf, is_segmented, segment_size, is_spin_glass, *opencl_args)
|
|
180
168
|
if energy > best_energy:
|
|
181
169
|
best_energy = energy
|
|
182
170
|
best_solution = solution.copy()
|
|
@@ -193,95 +181,36 @@ def init_J_and_z(G_m, repulsion_base):
|
|
|
193
181
|
n_qubits = G_m.shape[0]
|
|
194
182
|
degrees = np.empty(n_qubits, dtype=np.uint32)
|
|
195
183
|
J_eff = np.empty(n_qubits, dtype=np.float64)
|
|
196
|
-
weights = np.empty(n_qubits, dtype=np.float64)
|
|
197
|
-
G_max = -float("inf")
|
|
198
184
|
for n in prange(n_qubits):
|
|
199
185
|
degree = 0
|
|
200
186
|
J = 0.0
|
|
201
187
|
weight = 0.0
|
|
202
188
|
for m in range(n_qubits):
|
|
203
|
-
val = G_m[n, m]
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
if val != 0.0:
|
|
209
|
-
degree += 1
|
|
210
|
-
J += val
|
|
189
|
+
val = G_m[n, m] - G_min
|
|
190
|
+
if val <= epsilon:
|
|
191
|
+
continue
|
|
192
|
+
degree += 1
|
|
193
|
+
J += val
|
|
211
194
|
if degree > 0:
|
|
212
195
|
J = -J / degree
|
|
213
|
-
weight = -weight / n_qubits
|
|
214
196
|
degrees[n] = degree
|
|
215
197
|
J_eff[n] = J
|
|
216
|
-
weights[n] = weight
|
|
217
|
-
|
|
218
|
-
weights = repulsion_base ** weights
|
|
219
198
|
|
|
220
|
-
|
|
221
|
-
G_max = abs(G_max)
|
|
222
|
-
if G_min > G_max:
|
|
223
|
-
G_max = G_min
|
|
224
|
-
|
|
225
|
-
return J_eff, degrees, weights, G_max
|
|
199
|
+
return J_eff, degrees
|
|
226
200
|
|
|
227
201
|
|
|
228
202
|
@njit
|
|
229
|
-
def cpu_footer(J_eff, degrees,
|
|
203
|
+
def cpu_footer(J_eff, degrees, shots, quality, n_qubits, G_data, G_rows, G_cols, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
|
|
230
204
|
hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
|
|
231
205
|
|
|
232
|
-
best_solution, best_value = sample_measurement(G_data, G_rows, G_cols,
|
|
206
|
+
best_solution, best_value = sample_measurement(G_data, G_rows, G_cols, shots, hamming_prob, repulsion_base, is_spin_glass)
|
|
233
207
|
|
|
234
208
|
bit_string, l, r = get_cut(best_solution, nodes, n_qubits)
|
|
235
209
|
|
|
236
210
|
return bit_string, best_value, (l, r)
|
|
237
211
|
|
|
238
212
|
|
|
239
|
-
|
|
240
|
-
def convert_bool_to_uint(samples):
|
|
241
|
-
shots = samples.shape[0]
|
|
242
|
-
n32 = ((samples.shape[1] + 31) >> 5) << 5
|
|
243
|
-
theta = np.zeros(shots * (n32 >> 5), dtype=np.uint32)
|
|
244
|
-
for i in prange(shots):
|
|
245
|
-
i_offset = i * n32
|
|
246
|
-
for j in range(n32):
|
|
247
|
-
if samples[i, j]:
|
|
248
|
-
b_index = i_offset + j
|
|
249
|
-
theta[b_index >> 5] |= 1 << (b_index & 31)
|
|
250
|
-
|
|
251
|
-
return theta
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
def setup_cut_opencl(shots, n, segment_size, is_spin_glass):
|
|
255
|
-
ctx = opencl_context.ctx
|
|
256
|
-
queue = opencl_context.queue
|
|
257
|
-
dtype = opencl_context.dtype
|
|
258
|
-
wgs = opencl_context.work_group_size
|
|
259
|
-
|
|
260
|
-
# Args: [n, shots, is_spin_glass, prng_seed, segment_size]
|
|
261
|
-
args_np = np.array([n, shots, is_spin_glass, segment_size], dtype=np.int32)
|
|
262
|
-
|
|
263
|
-
# Buffers
|
|
264
|
-
mf = cl.mem_flags
|
|
265
|
-
args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args_np)
|
|
266
|
-
|
|
267
|
-
# Local memory allocation (1 float per work item)
|
|
268
|
-
local_size = min(wgs, shots)
|
|
269
|
-
max_global_size = ((opencl_context.MAX_GPU_PROC_ELEM + local_size - 1) // local_size) * local_size # corresponds to MAX_PROC_ELEM macro in OpenCL kernel program
|
|
270
|
-
global_size = min(((shots + local_size - 1) // local_size) * local_size, max_global_size)
|
|
271
|
-
local_energy_buf = cl.LocalMemory(np.dtype(dtype).itemsize * local_size)
|
|
272
|
-
local_index_buf = cl.LocalMemory(np.dtype(np.int32).itemsize * local_size)
|
|
273
|
-
|
|
274
|
-
# Allocate max_energy and max_index result buffers per workgroup
|
|
275
|
-
max_energy_host = np.empty(global_size, dtype=dtype)
|
|
276
|
-
max_index_host = np.empty(global_size, dtype=np.int32)
|
|
277
|
-
|
|
278
|
-
max_energy_buf = cl.Buffer(ctx, mf.WRITE_ONLY, max_energy_host.nbytes)
|
|
279
|
-
max_index_buf = cl.Buffer(ctx, mf.WRITE_ONLY, max_index_host.nbytes)
|
|
280
|
-
|
|
281
|
-
return local_size, global_size, args_buf, local_energy_buf, local_index_buf, max_energy_host, max_index_host, max_energy_buf, max_index_buf
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
def run_cut_opencl(samples, G_data_buf, G_rows_buf, G_cols_buf, is_segmented, segment_size, is_spin_glass, local_size, global_size, args_buf, local_energy_buf, local_index_buf, max_energy_host, max_index_host, max_energy_buf, max_index_buf):
|
|
213
|
+
def run_cut_opencl(best_energy, samples, G_data_buf, G_rows_buf, G_cols_buf, is_segmented, segment_size, is_spin_glass, local_size, global_size, args_buf, local_energy_buf, local_index_buf, max_energy_host, max_index_host, max_energy_buf, max_index_buf):
|
|
285
214
|
queue = opencl_context.queue
|
|
286
215
|
calculate_cut_kernel = opencl_context.calculate_cut_sparse_segmented_kernel if is_segmented else opencl_context.calculate_cut_sparse_kernel
|
|
287
216
|
|
|
@@ -328,14 +257,23 @@ def run_cut_opencl(samples, G_data_buf, G_rows_buf, G_cols_buf, is_segmented, se
|
|
|
328
257
|
|
|
329
258
|
# Read results
|
|
330
259
|
cl.enqueue_copy(queue, max_energy_host, max_energy_buf)
|
|
331
|
-
cl.enqueue_copy(queue, max_index_host, max_index_buf)
|
|
332
260
|
queue.finish()
|
|
333
261
|
|
|
262
|
+
# Queue read for results we might not need
|
|
263
|
+
cl.enqueue_copy(queue, max_index_host, max_index_buf)
|
|
264
|
+
|
|
334
265
|
# Find global minimum
|
|
335
266
|
best_x = np.argmax(max_energy_host)
|
|
336
|
-
|
|
267
|
+
energy = max_energy_host[best_x]
|
|
268
|
+
|
|
269
|
+
if energy < best_energy:
|
|
270
|
+
# No improvement: we can exit early
|
|
271
|
+
return samples[0], max_energy_host[0]
|
|
272
|
+
|
|
273
|
+
# We need the best index
|
|
274
|
+
queue.finish()
|
|
337
275
|
|
|
338
|
-
return samples[
|
|
276
|
+
return samples[max_index_host[best_x]], energy
|
|
339
277
|
|
|
340
278
|
|
|
341
279
|
|
|
@@ -378,7 +316,7 @@ def maxcut_tfim_sparse(
|
|
|
378
316
|
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
379
317
|
|
|
380
318
|
if quality is None:
|
|
381
|
-
quality =
|
|
319
|
+
quality = 6
|
|
382
320
|
|
|
383
321
|
if shots is None:
|
|
384
322
|
# Number of measurement shots
|
|
@@ -391,16 +329,16 @@ def maxcut_tfim_sparse(
|
|
|
391
329
|
anneal_h = 8.0
|
|
392
330
|
|
|
393
331
|
if repulsion_base is None:
|
|
394
|
-
repulsion_base =
|
|
332
|
+
repulsion_base = 5.0
|
|
395
333
|
|
|
396
|
-
J_eff, degrees
|
|
334
|
+
J_eff, degrees = init_J_and_z(G_m, repulsion_base)
|
|
397
335
|
|
|
398
336
|
n_qubits = G_m.shape[0]
|
|
399
337
|
|
|
400
338
|
is_opencl = is_maxcut_gpu and IS_OPENCL_AVAILABLE
|
|
401
339
|
|
|
402
340
|
if not is_opencl:
|
|
403
|
-
bit_string, best_value, partition = cpu_footer(J_eff, degrees,
|
|
341
|
+
bit_string, best_value, partition = cpu_footer(J_eff, degrees, shots, quality, n_qubits, G_m.data, G_m.indptr, G_m.indices, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base)
|
|
404
342
|
|
|
405
343
|
if best_value < 0.0:
|
|
406
344
|
# Best cut is trivial partition, all/empty
|
|
@@ -417,9 +355,9 @@ def maxcut_tfim_sparse(
|
|
|
417
355
|
hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
|
|
418
356
|
|
|
419
357
|
degrees = None
|
|
420
|
-
J_eff =
|
|
358
|
+
J_eff = None
|
|
421
359
|
|
|
422
|
-
best_solution, best_value = sample_for_opencl(G_m.data, G_m.indptr, G_m.indices, G_data_buf, G_rows_buf, G_cols_buf,
|
|
360
|
+
best_solution, best_value = sample_for_opencl(G_m.data, G_m.indptr, G_m.indices, G_data_buf, G_rows_buf, G_cols_buf, shots, hamming_prob, repulsion_base, is_spin_glass, is_segmented, segment_size, theta_segment_size)
|
|
423
361
|
|
|
424
362
|
bit_string, l, r = get_cut(best_solution, nodes, n_qubits)
|
|
425
363
|
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import math
|
|
2
1
|
import networkx as nx
|
|
3
2
|
import numpy as np
|
|
4
|
-
import os
|
|
5
3
|
from numba import njit, prange
|
|
6
4
|
|
|
7
|
-
from .maxcut_tfim_util import get_cut, get_cut_base, maxcut_hamming_cdf, opencl_context, sample_mag, bit_pick
|
|
5
|
+
from .maxcut_tfim_util import get_cut, get_cut_base, maxcut_hamming_cdf, opencl_context, sample_mag, bit_pick
|
|
8
6
|
|
|
9
7
|
|
|
10
8
|
epsilon = opencl_context.epsilon
|
|
@@ -12,7 +10,7 @@ dtype = opencl_context.dtype
|
|
|
12
10
|
|
|
13
11
|
|
|
14
12
|
@njit
|
|
15
|
-
def update_repulsion_choice(G_func, nodes,
|
|
13
|
+
def update_repulsion_choice(G_func, nodes, weights, n, used, node, repulsion_base):
|
|
16
14
|
# Select node
|
|
17
15
|
used[node] = True
|
|
18
16
|
|
|
@@ -20,41 +18,34 @@ def update_repulsion_choice(G_func, nodes, max_edge, weights, n, used, node, rep
|
|
|
20
18
|
for nbr in range(n):
|
|
21
19
|
if used[nbr]:
|
|
22
20
|
continue
|
|
23
|
-
weights[nbr] *= repulsion_base ** (-G_func(nodes[node], nodes[nbr])
|
|
21
|
+
weights[nbr] *= repulsion_base ** (-G_func(nodes[node], nodes[nbr]))
|
|
24
22
|
|
|
25
23
|
|
|
26
24
|
# Written by Elara (OpenAI custom GPT) and improved by Dan Strano
|
|
27
25
|
@njit
|
|
28
|
-
def local_repulsion_choice(G_func, nodes,
|
|
26
|
+
def local_repulsion_choice(G_func, nodes, repulsion_base, n, m, s):
|
|
29
27
|
"""
|
|
30
28
|
Pick m nodes out of n with repulsion bias:
|
|
31
29
|
- High-degree nodes are already less likely
|
|
32
30
|
- After choosing a node, its neighbors' probabilities are further reduced
|
|
33
|
-
adjacency_data, adjacency_rows: CSR-format sparse adjacency data
|
|
34
|
-
weights: float array of shape (n,)
|
|
35
31
|
"""
|
|
36
32
|
|
|
37
|
-
weights = weights.copy()
|
|
38
33
|
used = np.zeros(n, dtype=np.bool_) # False = available, True = used
|
|
39
34
|
|
|
40
35
|
# First bit:
|
|
41
|
-
node =
|
|
42
|
-
|
|
36
|
+
node = s % n
|
|
43
37
|
if m == 1:
|
|
44
38
|
used[node] = True
|
|
45
39
|
return used
|
|
46
40
|
|
|
47
|
-
|
|
41
|
+
weights = np.ones(n, dtype=np.float64)
|
|
42
|
+
update_repulsion_choice(G_func, nodes, weights, n, used, node, repulsion_base)
|
|
48
43
|
|
|
49
44
|
for _ in range(1, m - 1):
|
|
50
45
|
node = bit_pick(weights, used, n)
|
|
51
|
-
|
|
52
|
-
# Update answer and weights
|
|
53
|
-
update_repulsion_choice(G_func, nodes, max_edge, weights, n, used, node, repulsion_base)
|
|
46
|
+
update_repulsion_choice(G_func, nodes, weights, n, used, node, repulsion_base)
|
|
54
47
|
|
|
55
48
|
node = bit_pick(weights, used, n)
|
|
56
|
-
|
|
57
|
-
# Select node
|
|
58
49
|
used[node] = True
|
|
59
50
|
|
|
60
51
|
return used
|
|
@@ -84,9 +75,8 @@ def compute_cut(sample, G_func, nodes, n_qubits):
|
|
|
84
75
|
|
|
85
76
|
|
|
86
77
|
@njit(parallel=True)
|
|
87
|
-
def sample_measurement(G_func, nodes,
|
|
78
|
+
def sample_measurement(G_func, nodes, shots, thresholds, n, repulsion_base, is_spin_glass):
|
|
88
79
|
shots = max(1, shots >> 1)
|
|
89
|
-
tot_init_weight = weights.sum()
|
|
90
80
|
|
|
91
81
|
solutions = np.empty((shots, n), dtype=np.bool_)
|
|
92
82
|
energies = np.empty(shots, dtype=dtype)
|
|
@@ -103,7 +93,7 @@ def sample_measurement(G_func, nodes, max_edge, shots, thresholds, weights, n, r
|
|
|
103
93
|
m = sample_mag(thresholds)
|
|
104
94
|
|
|
105
95
|
# Second dimension: permutation within Hamming weight
|
|
106
|
-
sample = local_repulsion_choice(G_func, nodes,
|
|
96
|
+
sample = local_repulsion_choice(G_func, nodes, repulsion_base, n, m, s)
|
|
107
97
|
solutions[s] = sample
|
|
108
98
|
energies[s] = compute_energy(sample, G_func, nodes, n)
|
|
109
99
|
else:
|
|
@@ -112,7 +102,7 @@ def sample_measurement(G_func, nodes, max_edge, shots, thresholds, weights, n, r
|
|
|
112
102
|
m = sample_mag(thresholds)
|
|
113
103
|
|
|
114
104
|
# Second dimension: permutation within Hamming weight
|
|
115
|
-
sample = local_repulsion_choice(G_func, nodes,
|
|
105
|
+
sample = local_repulsion_choice(G_func, nodes, repulsion_base, n, m, s)
|
|
116
106
|
solutions[s] = sample
|
|
117
107
|
energies[s] = compute_cut(sample, G_func, nodes, n)
|
|
118
108
|
|
|
@@ -134,31 +124,21 @@ def init_J_and_z(G_func, nodes, G_min, repulsion_base):
|
|
|
134
124
|
n_qubits = len(nodes)
|
|
135
125
|
degrees = np.empty(n_qubits, dtype=np.uint32)
|
|
136
126
|
J_eff = np.empty(n_qubits, dtype=np.float64)
|
|
137
|
-
weights = np.empty(n_qubits, dtype=np.float64)
|
|
138
|
-
G_max = -float("inf")
|
|
139
127
|
for n in prange(n_qubits):
|
|
140
128
|
degree = 0
|
|
141
129
|
J = 0.0
|
|
142
|
-
weight = 0.0
|
|
143
130
|
for m in range(n_qubits):
|
|
144
|
-
val = G_func(nodes[n], nodes[m])
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
if val != 0.0:
|
|
150
|
-
degree += 1
|
|
151
|
-
J += val
|
|
131
|
+
val = G_func(nodes[n], nodes[m]) - G_min
|
|
132
|
+
if val <= epsilon:
|
|
133
|
+
continue
|
|
134
|
+
degree += 1
|
|
135
|
+
J += val
|
|
152
136
|
if degree > 0:
|
|
153
137
|
J = -J / degree
|
|
154
|
-
weight = -weight / n_qubits
|
|
155
138
|
degrees[n] = degree
|
|
156
139
|
J_eff[n] = J
|
|
157
|
-
weights[n] = weight
|
|
158
|
-
|
|
159
|
-
weights = repulsion_base ** weights
|
|
160
140
|
|
|
161
|
-
return J_eff, degrees
|
|
141
|
+
return J_eff, degrees
|
|
162
142
|
|
|
163
143
|
@njit
|
|
164
144
|
def find_G_min(G_func, nodes, n_nodes):
|
|
@@ -176,13 +156,13 @@ def find_G_min(G_func, nodes, n_nodes):
|
|
|
176
156
|
|
|
177
157
|
@njit
|
|
178
158
|
def cpu_footer(shots, quality, n_qubits, G_min, G_func, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
|
|
179
|
-
J_eff, degrees
|
|
159
|
+
J_eff, degrees = init_J_and_z(G_func, nodes, G_min, repulsion_base)
|
|
180
160
|
hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
|
|
181
161
|
|
|
182
162
|
degrees = None
|
|
183
163
|
J_eff = None
|
|
184
164
|
|
|
185
|
-
best_solution, best_value = sample_measurement(G_func, nodes,
|
|
165
|
+
best_solution, best_value = sample_measurement(G_func, nodes, shots, hamming_prob, n_qubits, repulsion_base, is_spin_glass)
|
|
186
166
|
|
|
187
167
|
bit_string, l, r = get_cut(best_solution, nodes, n_qubits)
|
|
188
168
|
|
|
@@ -217,7 +197,7 @@ def maxcut_tfim_streaming(
|
|
|
217
197
|
return "01", weight, ([nodes[0]], [nodes[1]])
|
|
218
198
|
|
|
219
199
|
if quality is None:
|
|
220
|
-
quality =
|
|
200
|
+
quality = 6
|
|
221
201
|
|
|
222
202
|
if shots is None:
|
|
223
203
|
# Number of measurement shots
|
|
@@ -230,7 +210,7 @@ def maxcut_tfim_streaming(
|
|
|
230
210
|
anneal_h = 8.0
|
|
231
211
|
|
|
232
212
|
if repulsion_base is None:
|
|
233
|
-
repulsion_base =
|
|
213
|
+
repulsion_base = 5.0
|
|
234
214
|
|
|
235
215
|
G_min = find_G_min(G_func, nodes, n_qubits)
|
|
236
216
|
|
|
@@ -393,18 +393,6 @@ def sample_mag(cum_prob):
|
|
|
393
393
|
|
|
394
394
|
return m + 1
|
|
395
395
|
|
|
396
|
-
@njit
|
|
397
|
-
def init_bit_pick(weights, p, n):
|
|
398
|
-
p *= np.random.rand()
|
|
399
|
-
cum = 0.0
|
|
400
|
-
node = 0
|
|
401
|
-
for i in range(n):
|
|
402
|
-
cum += weights[i]
|
|
403
|
-
if p < cum:
|
|
404
|
-
node = i
|
|
405
|
-
break
|
|
406
|
-
|
|
407
|
-
return node
|
|
408
396
|
|
|
409
397
|
@njit
|
|
410
398
|
def bit_pick(weights, used, n):
|
|
@@ -2,11 +2,9 @@ from .maxcut_tfim import maxcut_tfim
|
|
|
2
2
|
from .maxcut_tfim_util import get_cut_base, make_G_m_buf, opencl_context, setup_opencl
|
|
3
3
|
from .spin_glass_solver_util import get_cut_from_bit_array, int_to_bitstring
|
|
4
4
|
import itertools
|
|
5
|
-
import math
|
|
6
5
|
import networkx as nx
|
|
7
6
|
import numpy as np
|
|
8
7
|
from numba import njit, prange
|
|
9
|
-
import os
|
|
10
8
|
import random
|
|
11
9
|
|
|
12
10
|
|
|
@@ -258,7 +256,7 @@ def spin_glass_solver(
|
|
|
258
256
|
|
|
259
257
|
if reheat_round < reheat_tries:
|
|
260
258
|
num_to_flip = int(np.round(np.log2(n_qubits)))
|
|
261
|
-
bits_to_flip = random.sample(
|
|
259
|
+
bits_to_flip = random.sample(range(n_qubits), num_to_flip)
|
|
262
260
|
for bit in bits_to_flip:
|
|
263
261
|
reheat_theta[bit] = not reheat_theta[bit]
|
|
264
262
|
reheat_min_energy = compute_energy(reheat_theta, G_m, n_qubits) if is_spin_glass else -evaluate_cut_edges(reheat_theta, G_m, n_qubits)
|
|
@@ -2,11 +2,9 @@ from .maxcut_tfim_sparse import maxcut_tfim_sparse
|
|
|
2
2
|
from .maxcut_tfim_util import get_cut_base, make_G_m_csr_buf, opencl_context, setup_opencl
|
|
3
3
|
from .spin_glass_solver_util import get_cut_from_bit_array, int_to_bitstring
|
|
4
4
|
import itertools
|
|
5
|
-
import math
|
|
6
5
|
import networkx as nx
|
|
7
6
|
import numpy as np
|
|
8
7
|
from numba import njit, prange
|
|
9
|
-
import os
|
|
10
8
|
import random
|
|
11
9
|
from scipy.sparse import lil_matrix, csr_matrix
|
|
12
10
|
|
|
@@ -280,7 +278,7 @@ def spin_glass_solver_sparse(
|
|
|
280
278
|
|
|
281
279
|
if reheat_round < reheat_tries:
|
|
282
280
|
num_to_flip = int(np.round(np.log2(n_qubits)))
|
|
283
|
-
bits_to_flip = random.sample(
|
|
281
|
+
bits_to_flip = random.sample(range(n_qubits), num_to_flip)
|
|
284
282
|
for bit in bits_to_flip:
|
|
285
283
|
reheat_theta[bit] = not reheat_theta[bit]
|
|
286
284
|
reheat_min_energy = compute_energy(reheat_theta, G_m.data, G_m.indptr, G_m.indices, n_qubits) if is_spin_glass else -evaluate_cut_edges(reheat_theta, G_m.data, G_m.indptr, G_m.indices, n_qubits)
|
|
@@ -5,7 +5,6 @@ import itertools
|
|
|
5
5
|
import networkx as nx
|
|
6
6
|
import numpy as np
|
|
7
7
|
from numba import njit, prange
|
|
8
|
-
import os
|
|
9
8
|
import random
|
|
10
9
|
|
|
11
10
|
|
|
@@ -170,7 +169,7 @@ def spin_glass_solver_streaming(
|
|
|
170
169
|
|
|
171
170
|
if reheat_round < reheat_tries:
|
|
172
171
|
num_to_flip = int(np.round(np.log2(n_qubits)))
|
|
173
|
-
bits_to_flip = random.sample(
|
|
172
|
+
bits_to_flip = random.sample(range(n_qubits), num_to_flip)
|
|
174
173
|
for bit in bits_to_flip:
|
|
175
174
|
reheat_theta[bit] = not reheat_theta[bit]
|
|
176
175
|
reheat_min_energy = compute_energy(reheat_theta, G_func, nodes, n_qubits) if is_spin_glass else -evaluate_cut_edges(reheat_theta, G_func, nodes, n_qubits)
|
|
@@ -7,7 +7,7 @@ with open(README_PATH) as readme_file:
|
|
|
7
7
|
|
|
8
8
|
setup(
|
|
9
9
|
name='pyqrackising',
|
|
10
|
-
version='8.
|
|
10
|
+
version='8.15.0',
|
|
11
11
|
author='Dan Strano',
|
|
12
12
|
author_email='stranoj@gmail.com',
|
|
13
13
|
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
|