PyQrackIsing 8.15.0__tar.gz → 9.0.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.
Files changed (31) hide show
  1. {pyqrackising-8.15.0/pyqrackising.egg-info → pyqrackising-9.0.0}/PKG-INFO +1 -1
  2. {pyqrackising-8.15.0 → pyqrackising-9.0.0}/README.md +2 -2
  3. {pyqrackising-8.15.0 → pyqrackising-9.0.0}/pyproject.toml +1 -1
  4. {pyqrackising-8.15.0 → pyqrackising-9.0.0}/pyqrackising/kernels.cl +2 -307
  5. {pyqrackising-8.15.0 → pyqrackising-9.0.0}/pyqrackising/maxcut_tfim.py +2 -24
  6. {pyqrackising-8.15.0 → pyqrackising-9.0.0}/pyqrackising/maxcut_tfim_sparse.py +6 -32
  7. {pyqrackising-8.15.0 → pyqrackising-9.0.0}/pyqrackising/maxcut_tfim_streaming.py +4 -27
  8. {pyqrackising-8.15.0 → pyqrackising-9.0.0}/pyqrackising/maxcut_tfim_util.py +106 -15
  9. pyqrackising-9.0.0/pyqrackising/spin_glass_solver.py +211 -0
  10. pyqrackising-9.0.0/pyqrackising/spin_glass_solver_sparse.py +212 -0
  11. pyqrackising-9.0.0/pyqrackising/spin_glass_solver_streaming.py +195 -0
  12. {pyqrackising-8.15.0 → pyqrackising-9.0.0/pyqrackising.egg-info}/PKG-INFO +1 -1
  13. {pyqrackising-8.15.0 → pyqrackising-9.0.0}/pyqrackising.egg-info/SOURCES.txt +0 -1
  14. {pyqrackising-8.15.0 → pyqrackising-9.0.0}/setup.py +1 -1
  15. pyqrackising-8.15.0/pyqrackising/spin_glass_solver.py +0 -273
  16. pyqrackising-8.15.0/pyqrackising/spin_glass_solver_sparse.py +0 -297
  17. pyqrackising-8.15.0/pyqrackising/spin_glass_solver_streaming.py +0 -184
  18. pyqrackising-8.15.0/pyqrackising/spin_glass_solver_util.py +0 -21
  19. {pyqrackising-8.15.0 → pyqrackising-9.0.0}/LICENSE.md +0 -0
  20. {pyqrackising-8.15.0 → pyqrackising-9.0.0}/MANIFEST.in +0 -0
  21. {pyqrackising-8.15.0 → pyqrackising-9.0.0}/pyqrackising/__init__.py +0 -0
  22. {pyqrackising-8.15.0 → pyqrackising-9.0.0}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
  23. {pyqrackising-8.15.0 → pyqrackising-9.0.0}/pyqrackising/generate_tfim_samples.py +0 -0
  24. {pyqrackising-8.15.0 → pyqrackising-9.0.0}/pyqrackising/tfim_magnetization.py +0 -0
  25. {pyqrackising-8.15.0 → pyqrackising-9.0.0}/pyqrackising/tfim_square_magnetization.py +0 -0
  26. {pyqrackising-8.15.0 → pyqrackising-9.0.0}/pyqrackising/tsp.py +0 -0
  27. {pyqrackising-8.15.0 → pyqrackising-9.0.0}/pyqrackising/tsp_maxcut.py +0 -0
  28. {pyqrackising-8.15.0 → pyqrackising-9.0.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
  29. {pyqrackising-8.15.0 → pyqrackising-9.0.0}/pyqrackising.egg-info/not-zip-safe +0 -0
  30. {pyqrackising-8.15.0 → pyqrackising-9.0.0}/pyqrackising.egg-info/top_level.txt +0 -0
  31. {pyqrackising-8.15.0 → pyqrackising-9.0.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 8.15.0
3
+ Version: 9.0.0
4
4
  Summary: Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)
5
5
  Home-page: https://github.com/vm6502q/PyQrackIsing
6
6
  Author: Dan Strano
@@ -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=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)
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_maxcut_gpu=True)
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`). `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.
79
+ We also provide `spin_glass_solver_sparse(G)` and `spin_glass_solver_streaming(G_func, nodes)`. `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. `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.15.0"
13
+ version = "9.0.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"}
@@ -28,136 +28,8 @@ void reduce_energy_index(real1 energy, int i, __local real1* loc_energy, __local
28
28
  }
29
29
  }
30
30
 
31
- real1 bootstrap_worker(__constant char* theta, __global const real1* G_m, __global int* indices, const int k, const int n, const bool is_spin_glass) {
32
- real1 energy = ZERO_R1;
33
- const size_t n_st = (size_t)n;
34
- for (int u = 0; u < n; ++u) {
35
- const size_t u_offset = u * n_st;
36
- bool u_bit = theta[u];
37
- for (int x = 0; x < k; ++x) {
38
- if (indices[x] == u) {
39
- u_bit = !u_bit;
40
- break;
41
- }
42
- }
43
- for (int v = u + 1; v < n; ++v) {
44
- const real1 val = G_m[u_offset + v];
45
- bool v_bit = theta[v];
46
- for (int x = 0; x < k; ++x) {
47
- if (indices[x] == v) {
48
- v_bit = !v_bit;
49
- break;
50
- }
51
- }
52
- if (u_bit != v_bit) {
53
- energy += val;
54
- } else if (is_spin_glass) {
55
- energy -= val;
56
- }
57
- }
58
- }
59
-
60
- return energy;
61
- }
62
-
63
- __kernel void bootstrap(
64
- __global const real1* G_m,
65
- __constant char* best_theta,
66
- __global int* indices,
67
- __constant int* args, // args[0] = n, args[1] = k
68
- __global real1* max_energy_ptr, // output: per-group min energy
69
- __global int* max_index_ptr, // output: per-group best index (i)
70
- __local real1* loc_energy, // local memory buffer
71
- __local int* loc_index // local memory buffer
72
- ) {
73
- const int n = args[0];
74
- const int k = args[1];
75
- const int combo_count = args[2];
76
- const bool is_spin_glass = args[3];
77
-
78
- int i = get_global_id(0);
79
- const int max_i = get_global_size(0);
80
-
81
- real1 best_energy = -INFINITY;
82
- int best_i = i;
83
-
84
- for (; i < combo_count; i += max_i) {
85
- const int j = i * k;
86
- const real1 energy = bootstrap_worker(best_theta, G_m, indices + j, k, n, is_spin_glass);
87
- if (energy > best_energy) {
88
- best_energy = energy;
89
- best_i = i;
90
- }
91
- }
92
-
93
- reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
94
- }
95
-
96
- real1 bootstrap_worker_sparse(__constant char* theta, __global const real1* G_data, __global const uint* G_rows, __global const uint* G_cols, __global int* indices, const int k, const int n, const bool is_spin_glass) {
97
- real1 energy = ZERO_R1;
98
- for (int u = 0; u < n; ++u) {
99
- bool u_bit = theta[u];
100
- for (int x = 0; x < k; ++x) {
101
- if (indices[x] == u) {
102
- u_bit = !u_bit;
103
- break;
104
- }
105
- }
106
- const size_t mCol = G_rows[u + 1];
107
- for (int col = G_rows[u]; col < mCol; ++col) {
108
- const int v = G_cols[col];
109
- const real1 val = G_data[col];
110
- bool v_bit = theta[v];
111
- for (int x = 0; x < k; ++x) {
112
- if (indices[x] == v) {
113
- v_bit = !v_bit;
114
- break;
115
- }
116
- }
117
- if (u_bit != v_bit) {
118
- energy += val;
119
- } else if (is_spin_glass) {
120
- energy -= val;
121
- }
122
- }
123
- }
124
-
125
- return energy;
126
- }
127
-
128
- __kernel void bootstrap_sparse(
129
- __global const real1* G_data,
130
- __global const uint* G_rows,
131
- __global const uint* G_cols,
132
- __constant char* best_theta,
133
- __global int* indices,
134
- __constant int* args, // args[0] = n, args[1] = k
135
- __global real1* max_energy_ptr, // output: per-group min energy
136
- __global int* max_index_ptr, // output: per-group best index (i)
137
- __local real1* loc_energy, // local memory buffer
138
- __local int* loc_index // local memory buffer
139
- ) {
140
- const int n = args[0];
141
- const int k = args[1];
142
- const int combo_count = args[2];
143
- const bool is_spin_glass = args[3];
144
-
145
- int i = get_global_id(0);
146
- const int max_i = get_global_size(0);
147
-
148
- real1 best_energy = -INFINITY;
149
- int best_i = i;
150
-
151
- for (; i < combo_count; i += max_i) {
152
- const int j = i * k;
153
- const real1 energy = bootstrap_worker_sparse(best_theta, G_data, G_rows, G_cols, indices + j, k, n, is_spin_glass);
154
- if (energy > best_energy) {
155
- best_energy = energy;
156
- best_i = i;
157
- }
158
- }
159
-
160
- reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
31
+ inline bool get_bit(__global const uint* theta, const size_t u) {
32
+ return (theta[u >> 5U] >> (u & 31U)) & 1U;
161
33
  }
162
34
 
163
35
  // Helper to read from segmented G_m
@@ -169,183 +41,6 @@ inline real1 get_G_m(
169
41
  return G_m[flat_idx / segment_size][flat_idx % segment_size];
170
42
  }
171
43
 
172
- real1 bootstrap_worker_segmented(
173
- __constant char* theta,
174
- __global const real1** G_m,
175
- __global int* indices,
176
- const int k,
177
- const int n,
178
- const int segment_size,
179
- const bool is_spin_glass
180
- ) {
181
- real1 energy = ZERO_R1;
182
- const size_t n_st = (size_t)n;
183
-
184
- for (int u = 0; u < n; ++u) {
185
- const size_t u_offset = u * n_st;
186
- bool u_bit = theta[u];
187
- for (int x = 0; x < k; ++x) {
188
- if (indices[x] == u) {
189
- u_bit = !u_bit;
190
- break;
191
- }
192
- }
193
- for (int v = u + 1; v < n; ++v) {
194
- const size_t flat_idx = u_offset + v;
195
- const real1 val = get_G_m(G_m, flat_idx, segment_size);
196
-
197
- bool v_bit = theta[v];
198
- for (int x = 0; x < k; ++x) {
199
- if (indices[x] == v) {
200
- v_bit = !v_bit;
201
- break;
202
- }
203
- }
204
-
205
- if (u_bit != v_bit) {
206
- energy += val;
207
- } else if (is_spin_glass) {
208
- energy -= val;
209
- }
210
- }
211
- }
212
-
213
- return energy;
214
- }
215
-
216
-
217
- __kernel void bootstrap_segmented(
218
- __global const real1* G_m0,
219
- __global const real1* G_m1,
220
- __global const real1* G_m2,
221
- __global const real1* G_m3,
222
- __constant char* best_theta,
223
- __global int* indices,
224
- __constant int* args, // args[0]=n, args[1]=k, args[2]=combo_count, args[3]=segment_size
225
- __global real1* max_energy_ptr,
226
- __global int* max_index_ptr,
227
- __local real1* loc_energy,
228
- __local int* loc_index
229
- ) {
230
- __global const real1* G_m[4] = { G_m0, G_m1, G_m2, G_m3 };
231
-
232
- const int n = args[0];
233
- const int k = args[1];
234
- const int combo_count = args[2];
235
- const bool is_spin_glass = args[3];
236
- const int segment_size = args[4];
237
-
238
- int i = get_global_id(0);
239
- const int max_i = get_global_size(0);
240
-
241
- real1 best_energy = -INFINITY;
242
- int best_i = i;
243
-
244
- for (; i < combo_count; i += max_i) {
245
- const int j = i * k;
246
- const real1 energy = bootstrap_worker_segmented(best_theta, G_m, indices + j, k, n, segment_size, is_spin_glass);
247
- if (energy > best_energy) {
248
- best_energy = energy;
249
- best_i = i;
250
- }
251
- }
252
-
253
- reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
254
- }
255
-
256
- real1 bootstrap_worker_sparse_segmented(
257
- __constant char* theta,
258
- __global const real1** G_data,
259
- __global const uint* G_rows,
260
- __global const uint* G_cols,
261
- __global int* indices,
262
- const int k,
263
- const int n,
264
- const int segment_size,
265
- const bool is_spin_glass
266
- ) {
267
- real1 energy = ZERO_R1;
268
-
269
- for (int u = 0; u < n; ++u) {
270
- bool u_bit = theta[u];
271
- for (int x = 0; x < k; ++x) {
272
- if (indices[x] == u) {
273
- u_bit = !u_bit;
274
- break;
275
- }
276
- }
277
-
278
- const uint row_start = G_rows[u];
279
- const uint row_end = G_rows[u + 1];
280
-
281
- for (uint col = row_start; col < row_end; ++col) {
282
- const int v = G_cols[col];
283
- const real1 val = get_G_m(G_data, col, segment_size);
284
-
285
- bool v_bit = theta[v];
286
- for (int x = 0; x < k; ++x) {
287
- if (indices[x] == v) {
288
- v_bit = !v_bit;
289
- break;
290
- }
291
- }
292
-
293
- if (u_bit != v_bit) {
294
- energy += val;
295
- } else if (is_spin_glass) {
296
- energy -= val;
297
- }
298
- }
299
- }
300
-
301
- return energy;
302
- }
303
-
304
- __kernel void bootstrap_sparse_segmented(
305
- __global const real1* G_data0,
306
- __global const real1* G_data1,
307
- __global const real1* G_data2,
308
- __global const real1* G_data3,
309
- __global const uint* G_rows,
310
- __global const uint* G_cols,
311
- __constant char* best_theta,
312
- __global int* indices,
313
- __constant int* args, // args[0] = n, args[1] = k, args[2] = combo_count, args[3] = segment_size
314
- __global real1* max_energy_ptr,
315
- __global int* max_index_ptr,
316
- __local real1* loc_energy,
317
- __local int* loc_index
318
- ) {
319
- __global const real1* G_data[4] = { G_data0, G_data1, G_data2, G_data3 };
320
-
321
- const int n = args[0];
322
- const int k = args[1];
323
- const int combo_count = args[2];
324
- const bool is_spin_glass = args[3];
325
- const int segment_size = args[4];
326
-
327
- int i = get_global_id(0);
328
- const int max_i = get_global_size(0);
329
-
330
- real1 best_energy = -INFINITY;
331
- int best_i = i;
332
-
333
- for (; i < combo_count; i += max_i) {
334
- const int j = i * k;
335
- const real1 energy = bootstrap_worker_sparse_segmented(best_theta, G_data, G_rows, G_cols, indices + j, k, n, segment_size, is_spin_glass);
336
- if (energy > best_energy) {
337
- best_energy = energy;
338
- best_i = i;
339
- }
340
- }
341
-
342
- reduce_energy_index(best_energy, best_i, loc_energy, loc_index, max_energy_ptr, max_index_ptr);
343
- }
344
-
345
- inline bool get_bit(__global const uint* theta, const size_t u) {
346
- return (theta[u >> 5U] >> (u & 31U)) & 1U;
347
- }
348
-
349
44
  real1 cut_worker(__global const uint* theta, __global const real1* G_m, const int n, const bool is_spin_glass) {
350
45
  real1 energy = ZERO_R1;
351
46
  const size_t n_st = (size_t)n;
@@ -2,7 +2,7 @@ import networkx as nx
2
2
  import numpy as np
3
3
  from numba import njit, prange
4
4
 
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
5
+ from .maxcut_tfim_util import compute_cut, compute_energy, 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
6
6
 
7
7
  IS_OPENCL_AVAILABLE = True
8
8
  try:
@@ -58,28 +58,6 @@ def local_repulsion_choice(G_m, repulsion_base, n, m, s):
58
58
  return used
59
59
 
60
60
 
61
- @njit
62
- def compute_energy(sample, G_m, n_qubits):
63
- energy = 0
64
- for u in range(n_qubits):
65
- for v in range(u + 1, n_qubits):
66
- val = G_m[u, v]
67
- energy += val if sample[u] == sample[v] else -val
68
-
69
- return -energy
70
-
71
-
72
- @njit
73
- def compute_cut(sample, G_m, n_qubits):
74
- l, r = get_cut_base(sample, n_qubits)
75
- cut = 0
76
- for u in l:
77
- for v in r:
78
- cut += G_m[u, v]
79
-
80
- return cut
81
-
82
-
83
61
  @njit(parallel=True)
84
62
  def sample_measurement(G_m, shots, thresholds, repulsion_base, is_spin_glass):
85
63
  shots = max(1, shots >> 1)
@@ -252,7 +230,7 @@ def run_cut_opencl(best_energy, samples, G_m_buf, is_segmented, segment_size, is
252
230
  best_x = np.argmax(max_energy_host)
253
231
  energy = max_energy_host[best_x]
254
232
 
255
- if energy < best_energy:
233
+ if energy <= best_energy:
256
234
  # No improvement: we can exit early
257
235
  return samples[0], max_energy_host[0]
258
236
 
@@ -2,7 +2,7 @@ import networkx as nx
2
2
  import numpy as np
3
3
  from numba import njit, prange
4
4
 
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
5
+ from .maxcut_tfim_util import binary_search, compute_cut_sparse, compute_energy_sparse, 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
6
6
 
7
7
  IS_OPENCL_AVAILABLE = True
8
8
  try:
@@ -68,32 +68,6 @@ def local_repulsion_choice(G_data, G_rows, G_cols, repulsion_base, n, m, s):
68
68
  return used
69
69
 
70
70
 
71
- @njit
72
- def compute_energy(sample, G_data, G_rows, G_cols, n_qubits):
73
- energy = 0
74
- for u in range(n_qubits):
75
- u_bit = sample[u]
76
- for col in range(G_rows[u], G_rows[u + 1]):
77
- val = G_data[col]
78
- energy += val if u_bit == sample[G_cols[col]] else -val
79
-
80
- return -energy
81
-
82
-
83
- @njit
84
- def compute_cut(sample, G_data, G_rows, G_cols, n_qubits):
85
- l, r = get_cut_base(sample, n_qubits)
86
- s = l if len(l) < len(r) else r
87
- cut = 0
88
- for u in s:
89
- u_bit = sample[u]
90
- for col in range(G_rows[u], G_rows[u + 1]):
91
- if u_bit != sample[G_cols[col]]:
92
- cut += G_data[col]
93
-
94
- return cut
95
-
96
-
97
71
  @njit(parallel=True)
98
72
  def sample_measurement(G_data, G_rows, G_cols, shots, thresholds, repulsion_base, is_spin_glass):
99
73
  shots = max(1, shots >> 1)
@@ -116,7 +90,7 @@ def sample_measurement(G_data, G_rows, G_cols, shots, thresholds, repulsion_base
116
90
  # Second dimension: permutation within Hamming weight
117
91
  sample = local_repulsion_choice(G_data, G_rows, G_cols, repulsion_base, n, m, s)
118
92
  solutions[s] = sample
119
- energies[s] = compute_energy(sample, G_data, G_rows, G_cols, n)
93
+ energies[s] = compute_energy_sparse(sample, G_data, G_rows, G_cols, n)
120
94
  else:
121
95
  for s in prange(shots):
122
96
  # First dimension: Hamming weight
@@ -125,7 +99,7 @@ def sample_measurement(G_data, G_rows, G_cols, shots, thresholds, repulsion_base
125
99
  # Second dimension: permutation within Hamming weight
126
100
  sample = local_repulsion_choice(G_data, G_rows, G_cols, repulsion_base, n, m, s)
127
101
  solutions[s] = sample
128
- energies[s] = compute_cut(sample, G_data, G_rows, G_cols, n)
102
+ energies[s] = compute_cut_sparse(sample, G_data, G_rows, G_cols, n)
129
103
 
130
104
  best_index = np.argmax(energies)
131
105
  energy = energies[best_index]
@@ -135,7 +109,7 @@ def sample_measurement(G_data, G_rows, G_cols, shots, thresholds, repulsion_base
135
109
  improved = True
136
110
 
137
111
  if is_spin_glass:
138
- best_energy = compute_cut(best_solution, G_data, G_rows, G_cols, n)
112
+ best_energy = compute_cut_sparse(best_solution, G_data, G_rows, G_cols, n)
139
113
 
140
114
  return best_solution, best_energy
141
115
 
@@ -171,7 +145,7 @@ def sample_for_opencl(G_data, G_rows, G_cols, G_data_buf, G_rows_buf, G_cols_buf
171
145
  improved = True
172
146
 
173
147
  if is_spin_glass:
174
- best_energy = compute_cut(best_solution, G_data, G_rows, G_cols, n)
148
+ best_energy = compute_cut_sparse(best_solution, G_data, G_rows, G_cols, n)
175
149
 
176
150
  return best_solution, float(best_energy)
177
151
 
@@ -266,7 +240,7 @@ def run_cut_opencl(best_energy, samples, G_data_buf, G_rows_buf, G_cols_buf, is_
266
240
  best_x = np.argmax(max_energy_host)
267
241
  energy = max_energy_host[best_x]
268
242
 
269
- if energy < best_energy:
243
+ if energy <= best_energy:
270
244
  # No improvement: we can exit early
271
245
  return samples[0], max_energy_host[0]
272
246
 
@@ -2,7 +2,7 @@ import networkx as nx
2
2
  import numpy as np
3
3
  from numba import njit, prange
4
4
 
5
- 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 compute_cut_streaming, compute_energy_streaming, get_cut, get_cut_base, maxcut_hamming_cdf, opencl_context, sample_mag, bit_pick
6
6
 
7
7
 
8
8
  epsilon = opencl_context.epsilon
@@ -51,29 +51,6 @@ def local_repulsion_choice(G_func, nodes, repulsion_base, n, m, s):
51
51
  return used
52
52
 
53
53
 
54
- @njit
55
- def compute_energy(sample, G_func, nodes, n_qubits):
56
- energy = 0
57
- for u in range(n_qubits):
58
- u_bit = sample[u]
59
- for v in range(u + 1, n_qubits):
60
- val = G_func(nodes[u], nodes[v])
61
- energy += val if u_bit == sample[v] else -val
62
-
63
- return -energy
64
-
65
-
66
- @njit
67
- def compute_cut(sample, G_func, nodes, n_qubits):
68
- l, r = get_cut_base(sample, n_qubits)
69
- cut = 0
70
- for u in l:
71
- for v in r:
72
- cut += G_func(nodes[u], nodes[v])
73
-
74
- return cut
75
-
76
-
77
54
  @njit(parallel=True)
78
55
  def sample_measurement(G_func, nodes, shots, thresholds, n, repulsion_base, is_spin_glass):
79
56
  shots = max(1, shots >> 1)
@@ -95,7 +72,7 @@ def sample_measurement(G_func, nodes, shots, thresholds, n, repulsion_base, is_s
95
72
  # Second dimension: permutation within Hamming weight
96
73
  sample = local_repulsion_choice(G_func, nodes, repulsion_base, n, m, s)
97
74
  solutions[s] = sample
98
- energies[s] = compute_energy(sample, G_func, nodes, n)
75
+ energies[s] = compute_energy_streaming(sample, G_func, nodes, n)
99
76
  else:
100
77
  for s in prange(shots):
101
78
  # First dimension: Hamming weight
@@ -104,7 +81,7 @@ def sample_measurement(G_func, nodes, shots, thresholds, n, repulsion_base, is_s
104
81
  # Second dimension: permutation within Hamming weight
105
82
  sample = local_repulsion_choice(G_func, nodes, repulsion_base, n, m, s)
106
83
  solutions[s] = sample
107
- energies[s] = compute_cut(sample, G_func, nodes, n)
84
+ energies[s] = compute_cut_streaming(sample, G_func, nodes, n)
108
85
 
109
86
  best_index = np.argmax(energies)
110
87
  energy = energies[best_index]
@@ -114,7 +91,7 @@ def sample_measurement(G_func, nodes, shots, thresholds, n, repulsion_base, is_s
114
91
  improved = True
115
92
 
116
93
  if is_spin_glass:
117
- best_energy = compute_cut(best_solution, G_func, nodes, n)
94
+ best_energy = compute_cut_streaming(best_solution, G_func, nodes, n)
118
95
 
119
96
  return best_solution, best_energy
120
97