PyQrackIsing 8.14.1__tar.gz → 8.14.3__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 (28) hide show
  1. {pyqrackising-8.14.1/pyqrackising.egg-info → pyqrackising-8.14.3}/PKG-INFO +1 -1
  2. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/pyproject.toml +1 -1
  3. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/pyqrackising/kernels.cl +21 -11
  4. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/pyqrackising/maxcut_tfim.py +41 -120
  5. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/pyqrackising/maxcut_tfim_sparse.py +50 -32
  6. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/pyqrackising/maxcut_tfim_util.py +63 -0
  7. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/pyqrackising/spin_glass_solver.py +1 -0
  8. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/pyqrackising/spin_glass_solver_sparse.py +1 -0
  9. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/pyqrackising/tsp.py +7 -23
  10. {pyqrackising-8.14.1 → pyqrackising-8.14.3/pyqrackising.egg-info}/PKG-INFO +1 -1
  11. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/setup.py +1 -1
  12. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/LICENSE.md +0 -0
  13. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/MANIFEST.in +0 -0
  14. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/README.md +0 -0
  15. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/pyqrackising/__init__.py +0 -0
  16. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
  17. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/pyqrackising/generate_tfim_samples.py +0 -0
  18. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/pyqrackising/maxcut_tfim_streaming.py +0 -0
  19. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/pyqrackising/spin_glass_solver_streaming.py +0 -0
  20. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/pyqrackising/spin_glass_solver_util.py +0 -0
  21. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/pyqrackising/tfim_magnetization.py +0 -0
  22. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/pyqrackising/tfim_square_magnetization.py +0 -0
  23. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/pyqrackising/tsp_maxcut.py +0 -0
  24. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/pyqrackising.egg-info/SOURCES.txt +0 -0
  25. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/pyqrackising.egg-info/dependency_links.txt +0 -0
  26. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/pyqrackising.egg-info/not-zip-safe +0 -0
  27. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/pyqrackising.egg-info/top_level.txt +0 -0
  28. {pyqrackising-8.14.1 → pyqrackising-8.14.3}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 8.14.1
3
+ Version: 8.14.3
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
@@ -10,7 +10,7 @@ build-backend = "setuptools.build_meta"
10
10
 
11
11
  [project]
12
12
  name = "pyqrackising"
13
- version = "8.14.1"
13
+ version = "8.14.3"
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"}
@@ -438,11 +438,11 @@ __kernel void bootstrap_sparse_segmented(
438
438
  }
439
439
  }
440
440
 
441
- inline bool get_bit(__global uint* theta, const size_t u) {
441
+ inline bool get_bit(__global const uint* theta, const size_t u) {
442
442
  return (theta[u >> 5U] >> (u & 31U)) & 1U;
443
443
  }
444
444
 
445
- real1 cut_worker(__global uint* theta, __global const real1* G_m, const int n, const bool is_spin_glass) {
445
+ real1 cut_worker(__global const uint* theta, __global const real1* G_m, const int n, const bool is_spin_glass) {
446
446
  real1 energy = ZERO_R1;
447
447
  const size_t n_st = (size_t)n;
448
448
  for (int u = 0; u < n; ++u) {
@@ -464,7 +464,7 @@ real1 cut_worker(__global uint* theta, __global const real1* G_m, const int n, c
464
464
 
465
465
  __kernel void calculate_cut(
466
466
  __global const real1* G_m,
467
- __global uint* theta,
467
+ __global const uint* theta,
468
468
  __constant int* args, // args[0] = n, args[1] = k
469
469
  __global real1* max_energy_ptr, // output: per-group min energy
470
470
  __global int* max_index_ptr, // output: per-group best index (i)
@@ -515,7 +515,7 @@ __kernel void calculate_cut(
515
515
  }
516
516
  }
517
517
 
518
- real1 cut_worker_sparse(__global uint* theta, __global const real1* G_data, __global const uint* G_rows, __global const uint* G_cols, const int n, const bool is_spin_glass) {
518
+ real1 cut_worker_sparse(__global const uint* theta, __global const real1* G_data, __global const uint* G_rows, __global const uint* G_cols, const int n, const bool is_spin_glass) {
519
519
  real1 energy = ZERO_R1;
520
520
  for (int u = 0; u < n; ++u) {
521
521
  const bool u_bit = get_bit(theta, u);
@@ -539,7 +539,7 @@ __kernel void calculate_cut_sparse(
539
539
  __global const real1* G_data,
540
540
  __global const uint* G_rows,
541
541
  __global const uint* G_cols,
542
- __global uint* theta,
542
+ __global const uint* theta,
543
543
  __constant int* args, // args[0] = n, args[1] = k
544
544
  __global real1* max_energy_ptr, // output: per-group min energy
545
545
  __global int* max_index_ptr, // output: per-group best index (i)
@@ -590,7 +590,7 @@ __kernel void calculate_cut_sparse(
590
590
  }
591
591
 
592
592
  real1 cut_worker_segmented(
593
- __global uint* theta,
593
+ __global const uint* theta,
594
594
  __global const real1** G_m,
595
595
  const int n,
596
596
  const int segment_size,
@@ -623,7 +623,10 @@ __kernel void calculate_cut_segmented(
623
623
  __global const real1* G_m1,
624
624
  __global const real1* G_m2,
625
625
  __global const real1* G_m3,
626
- __global uint* theta,
626
+ __global const uint* theta0,
627
+ __global const uint* theta1,
628
+ __global const uint* theta2,
629
+ __global const uint* theta3,
627
630
  __constant int* args, // args[0]=n, args[1]=k, args[2]=combo_count, args[3]=segment_size
628
631
  __global real1* max_energy_ptr,
629
632
  __global int* max_index_ptr,
@@ -631,11 +634,13 @@ __kernel void calculate_cut_segmented(
631
634
  __local int* loc_index
632
635
  ) {
633
636
  __global const real1* G_m[4] = { G_m0, G_m1, G_m2, G_m3 };
637
+ __global const uint* theta[4] = { theta0, theta1, theta2, theta3 };
634
638
 
635
639
  const int n = args[0];
636
640
  const int shots = args[1];
637
641
  const bool is_spin_glass = args[2];
638
642
  const int segment_size = args[3];
643
+ const int theta_segment_size = args[4];
639
644
  int i = get_global_id(0);
640
645
 
641
646
  real1 best_energy = -INFINITY;
@@ -643,7 +648,7 @@ __kernel void calculate_cut_segmented(
643
648
 
644
649
  for (; i < shots; i += MAX_PROC_ELEM) {
645
650
  const int j = (i * n + 31U) >> 5U;
646
- const real1 energy = cut_worker_segmented(theta + j, G_m, n, segment_size, is_spin_glass);
651
+ const real1 energy = cut_worker_segmented(theta[j / theta_segment_size] + (j % theta_segment_size), G_m, n, segment_size, is_spin_glass);
647
652
  if (energy > best_energy) {
648
653
  best_energy = energy;
649
654
  best_i = i;
@@ -675,7 +680,7 @@ __kernel void calculate_cut_segmented(
675
680
  }
676
681
 
677
682
  real1 cut_worker_sparse_segmented(
678
- __global uint* theta,
683
+ __global const uint* theta,
679
684
  __global const real1** G_data,
680
685
  __global const uint* G_rows,
681
686
  __global const uint* G_cols,
@@ -712,7 +717,10 @@ __kernel void calculate_cut_sparse_segmented(
712
717
  __global const real1* G_data3,
713
718
  __global const uint* G_rows,
714
719
  __global const uint* G_cols,
715
- __global uint* theta,
720
+ __global const uint* theta0,
721
+ __global const uint* theta1,
722
+ __global const uint* theta2,
723
+ __global const uint* theta3,
716
724
  __constant int* args, // args[0] = n, args[1] = k, args[2] = combo_count, args[3] = segment_size
717
725
  __global real1* max_energy_ptr,
718
726
  __global int* max_index_ptr,
@@ -720,11 +728,13 @@ __kernel void calculate_cut_sparse_segmented(
720
728
  __local int* loc_index
721
729
  ) {
722
730
  __global const real1* G_data[4] = { G_data0, G_data1, G_data2, G_data3 };
731
+ __global const uint* theta[4] = { theta0, theta1, theta2, theta3 };
723
732
 
724
733
  const int n = args[0];
725
734
  const int shots = args[1];
726
735
  const bool is_spin_glass = args[2];
727
736
  const int segment_size = args[3];
737
+ const int theta_segment_size = args[4];
728
738
  int i = get_global_id(0);
729
739
 
730
740
  real1 best_energy = -INFINITY;
@@ -732,7 +742,7 @@ __kernel void calculate_cut_sparse_segmented(
732
742
 
733
743
  for (; i < shots; i += MAX_PROC_ELEM) {
734
744
  const int j = (i * n + 31U) >> 5U;
735
- const real1 energy = cut_worker_sparse_segmented(theta + j, G_data, G_rows, G_cols, n, segment_size, is_spin_glass);
745
+ const real1 energy = cut_worker_sparse_segmented(theta[j / theta_segment_size] + (j % theta_segment_size), G_data, G_rows, G_cols, n, segment_size, is_spin_glass);
736
746
  if (energy > best_energy) {
737
747
  best_energy = energy;
738
748
  best_i = i;
@@ -4,7 +4,7 @@ import numpy as np
4
4
  import os
5
5
  from numba import njit, prange
6
6
 
7
- from .maxcut_tfim_util import get_cut, get_cut_base, make_G_m_buf, maxcut_hamming_cdf, opencl_context, sample_mag, bit_pick, init_bit_pick
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_cut_opencl, bit_pick, init_bit_pick
8
8
 
9
9
  IS_OPENCL_AVAILABLE = True
10
10
  try:
@@ -90,27 +90,33 @@ def compute_cut(sample, G_m, n_qubits):
90
90
 
91
91
  @njit(parallel=True)
92
92
  def sample_measurement(G_m, max_edge, shots, thresholds, weights, repulsion_base, is_spin_glass):
93
- shots = max(1, shots >> 1)
93
+ shots = ((max(1, shots >> 1) + 3) >> 2) << 2
94
94
  n = len(G_m)
95
95
  tot_init_weight = weights.sum()
96
96
 
97
97
  solutions = np.empty((shots, n), dtype=np.bool_)
98
98
  energies = np.empty(shots, dtype=dtype)
99
-
99
+
100
100
  best_solution = solutions[0]
101
101
  best_energy = -float("inf")
102
102
 
103
103
  improved = True
104
104
  while improved:
105
105
  improved = False
106
- for s in prange(shots):
107
- # First dimension: Hamming weight
108
- m = sample_mag(thresholds)
109
-
110
- # Second dimension: permutation within Hamming weight
111
- sample = local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, repulsion_base, n, m)
112
- solutions[s] = sample
113
- energies[s] = compute_energy(sample, G_m, n) if is_spin_glass else compute_cut(sample, G_m, n)
106
+ if is_spin_glass:
107
+ for s in prange(shots):
108
+ # First dimension: Hamming weight
109
+ m = sample_mag(thresholds)
110
+ # Second dimension: permutation within Hamming weight
111
+ solutions[s] = local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, repulsion_base, n, m)
112
+ energies[s] = compute_energy(solutions[s], G_m, n)
113
+ else:
114
+ for s in prange(shots):
115
+ # First dimension: Hamming weight
116
+ m = sample_mag(thresholds)
117
+ # Second dimension: permutation within Hamming weight
118
+ solutions[s] = local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, repulsion_base, n, m)
119
+ energies[s] = compute_cut(solutions[s], G_m, n)
114
120
 
115
121
  best_index = np.argmax(energies)
116
122
  energy = energies[best_index]
@@ -120,7 +126,7 @@ def sample_measurement(G_m, max_edge, shots, thresholds, weights, repulsion_base
120
126
  improved = True
121
127
 
122
128
  if is_spin_glass:
123
- best_energy = compute_cut(best_solution, G_m, n)
129
+ best_energy = compute_cut(best_solution, G_m, n)
124
130
 
125
131
  return best_solution, best_energy
126
132
 
@@ -140,16 +146,17 @@ def sample_for_opencl(G_m, G_m_buf, max_edge, shots, thresholds, weights, repuls
140
146
  tot_init_weight = weights.sum()
141
147
 
142
148
  solutions = np.empty((shots, n), dtype=np.bool_)
143
- energies = np.empty(shots, dtype=dtype)
144
149
 
145
150
  best_solution = solutions[0]
146
151
  best_energy = -float("inf")
147
152
 
153
+ opencl_args = setup_cut_opencl(shots, n, segment_size, is_spin_glass)
154
+
148
155
  improved = True
149
156
  while improved:
150
157
  improved = False
151
158
  shot_loop(G_m, max_edge, thresholds, weights, tot_init_weight, repulsion_base, n, shots, solutions)
152
- solution, energy = run_cut_opencl(solutions, G_m_buf, is_segmented, segment_size, is_spin_glass)
159
+ solution, energy = run_cut_opencl(solutions, G_m_buf, is_segmented, segment_size, is_spin_glass, *opencl_args)
153
160
  if energy > best_energy:
154
161
  best_energy = energy
155
162
  best_solution = solution
@@ -192,7 +199,6 @@ def init_J_and_z(G_m):
192
199
  return J_eff, degrees, G_max
193
200
 
194
201
 
195
- @njit
196
202
  def cpu_footer(shots, quality, n_qubits, G_m, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
197
203
  J_eff, degrees, max_edge = init_J_and_z(G_m)
198
204
  hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
@@ -207,52 +213,16 @@ def cpu_footer(shots, quality, n_qubits, G_m, nodes, is_spin_glass, anneal_t, an
207
213
  return bit_string, best_value, (l, r)
208
214
 
209
215
 
210
- @njit(parallel=True)
211
- def convert_bool_to_uint(samples):
212
- shots = samples.shape[0]
213
- n32 = ((samples.shape[1] + 31) >> 5) << 5
214
- theta = np.zeros(shots * (n32 >> 5), dtype=np.uint32)
215
- for i in prange(shots):
216
- i_offset = i * n32
217
- for j in range(n32):
218
- if samples[i, j]:
219
- b_index = i_offset + j
220
- theta[b_index >> 5] |= 1 << (b_index & 31)
221
-
222
- return theta
223
-
224
-
225
- def run_cut_opencl(samples, G_m_buf, is_segmented, segment_size, is_spin_glass):
226
- ctx = opencl_context.ctx
216
+ 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):
227
217
  queue = opencl_context.queue
228
218
  calculate_cut_kernel = opencl_context.calculate_cut_segmented_kernel if is_segmented else opencl_context.calculate_cut_kernel
229
- dtype = opencl_context.dtype
230
- epsilon = opencl_context.epsilon
231
- wgs = opencl_context.work_group_size
232
-
233
- shots = samples.shape[1]
234
219
 
235
- # Args: [n, shots, is_spin_glass, prng_seed, segment_size]
236
- args_np = np.array([shots, samples.shape[0], is_spin_glass, segment_size], dtype=np.int32)
220
+ shots = samples.shape[0]
221
+ n = samples.shape[1]
237
222
 
238
223
  # Buffers
239
224
  mf = cl.mem_flags
240
- theta_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=convert_bool_to_uint(samples))
241
- args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args_np)
242
-
243
- # Local memory allocation (1 float per work item)
244
- local_size = min(wgs, shots)
245
- 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
246
- global_size = min(((shots + local_size - 1) // local_size) * local_size, max_global_size)
247
- local_energy_buf = cl.LocalMemory(np.dtype(dtype).itemsize * local_size)
248
- local_index_buf = cl.LocalMemory(np.dtype(np.int32).itemsize * local_size)
249
-
250
- # Allocate max_energy and max_index result buffers per workgroup
251
- max_energy_host = np.empty(global_size, dtype=dtype)
252
- max_index_host = np.empty(global_size, dtype=np.int32)
253
-
254
- max_energy_buf = cl.Buffer(ctx, mf.WRITE_ONLY, max_energy_host.nbytes)
255
- max_index_buf = cl.Buffer(ctx, mf.WRITE_ONLY, max_index_host.nbytes)
225
+ theta_buf = make_theta_buf(convert_bool_to_uint(samples), is_segmented, shots, n)
256
226
 
257
227
  # Set kernel args
258
228
  if is_segmented:
@@ -261,7 +231,10 @@ def run_cut_opencl(samples, G_m_buf, is_segmented, segment_size, is_spin_glass):
261
231
  G_m_buf[1],
262
232
  G_m_buf[2],
263
233
  G_m_buf[3],
264
- theta_buf,
234
+ theta_buf[0],
235
+ theta_buf[1],
236
+ theta_buf[2],
237
+ theta_buf[3],
265
238
  args_buf,
266
239
  max_energy_buf,
267
240
  max_index_buf,
@@ -291,64 +264,6 @@ def run_cut_opencl(samples, G_m_buf, is_segmented, segment_size, is_spin_glass):
291
264
 
292
265
  return samples[best_i], max_energy_host[best_i]
293
266
 
294
-
295
- @njit
296
- def maxcut_tfim_pure_numba(
297
- G_m,
298
- nodes,
299
- quality=None,
300
- shots=None,
301
- is_spin_glass=False,
302
- anneal_t=None,
303
- anneal_h=None,
304
- repulsion_base=None
305
- ):
306
- n_qubits = len(G_m)
307
-
308
- if n_qubits < 3:
309
- empty = [nodes[0]]
310
- empty.clear()
311
-
312
- if n_qubits == 0:
313
- return "", 0, (empty, empty.copy())
314
-
315
- if n_qubits == 1:
316
- return "0", 0, (nodes, empty)
317
-
318
- if n_qubits == 2:
319
- weight = G_m[0, 1]
320
- if weight < 0.0:
321
- return "00", 0, (nodes, empty)
322
-
323
- return "01", weight, ([nodes[0]], [nodes[1]])
324
-
325
- if quality is None:
326
- quality = 6
327
-
328
- if shots is None:
329
- # Number of measurement shots
330
- shots = n_qubits << quality
331
-
332
- if anneal_t is None:
333
- anneal_t = 8.0
334
-
335
- if anneal_h is None:
336
- anneal_h = 8.0
337
-
338
- if repulsion_base is None:
339
- repulsion_base = 8.0
340
-
341
- bit_string, best_value, partition = cpu_footer(shots, quality, n_qubits, G_m, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base)
342
-
343
- if best_value < 0.0:
344
- # Best cut is trivial partition, all/empty
345
- empty = [nodes[0]]
346
- empty.clear()
347
-
348
- return '0' * n_qubits, 0.0, (nodes, empty)
349
-
350
- return bit_string, best_value, partition
351
-
352
267
  def maxcut_tfim(
353
268
  G,
354
269
  quality=None,
@@ -371,11 +286,6 @@ def maxcut_tfim(
371
286
 
372
287
  n_qubits = len(G_m)
373
288
 
374
- is_opencl = is_maxcut_gpu and IS_OPENCL_AVAILABLE and (n_qubits >= max(32, opencl_context.work_group_size))
375
-
376
- if not is_opencl:
377
- return maxcut_tfim_pure_numba(G_m, nodes, quality, shots, is_spin_glass, anneal_t, anneal_h, repulsion_base)
378
-
379
289
  if n_qubits < 3:
380
290
  empty = [nodes[0]]
381
291
  empty.clear()
@@ -409,8 +319,19 @@ def maxcut_tfim(
409
319
  if repulsion_base is None:
410
320
  repulsion_base = 8.0
411
321
 
322
+ is_opencl = is_maxcut_gpu and IS_OPENCL_AVAILABLE and (n_qubits >= max(32, opencl_context.work_group_size))
323
+
324
+ if not is_opencl:
325
+ bit_string, best_value, partition = cpu_footer(shots, quality, n_qubits, G_m, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base)
326
+
327
+ if best_value < 0.0:
328
+ # Best cut is trivial partition, all/empty
329
+ return '0' * n_qubits, 0.0, (nodes, [])
330
+
331
+ return bit_string, best_value, partition
332
+
412
333
  segment_size = G_m.shape[0] ** 2
413
- is_segmented = (G_m.nbytes << 1) > opencl_context.max_alloc
334
+ is_segmented = ((G_m.nbytes << 1) > opencl_context.max_alloc) or ((((((((n_qubits + 31) >> 5) << 5) * ((shots + 1) >> 1)) + 7) >> 3) << 1) > opencl_context.max_alloc)
414
335
 
415
336
  G_m_buf = make_G_m_buf(G_m, is_segmented, segment_size)
416
337
  if is_nested:
@@ -4,7 +4,7 @@ import numpy as np
4
4
  import os
5
5
  from numba import njit, prange
6
6
 
7
- from .maxcut_tfim_util import binary_search, get_cut, get_cut_base, make_G_m_csr_buf, maxcut_hamming_cdf, opencl_context, sample_mag, bit_pick, init_bit_pick, to_scipy_sparse_upper_triangular
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_cut_opencl, bit_pick, init_bit_pick, to_scipy_sparse_upper_triangular
8
8
 
9
9
  IS_OPENCL_AVAILABLE = True
10
10
  try:
@@ -18,7 +18,7 @@ dtype = opencl_context.dtype
18
18
 
19
19
 
20
20
  @njit
21
- def update_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, n, used, node, repulsion_base):
21
+ def update_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, n, used, node, repulsion_base):
22
22
  # Select node
23
23
  used[node] = True
24
24
 
@@ -41,7 +41,7 @@ def update_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, n, used,
41
41
 
42
42
  # Written by Elara (OpenAI custom GPT) and improved by Dan Strano
43
43
  @njit
44
- def local_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, tot_init_weight, repulsion_base, n, m):
44
+ def local_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, tot_init_weight, repulsion_base, n, m):
45
45
  """
46
46
  Pick m nodes out of n with repulsion bias:
47
47
  - High-degree nodes are already less likely
@@ -60,13 +60,13 @@ def local_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, tot_init_w
60
60
  used[node] = True
61
61
  return used
62
62
 
63
- update_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, n, used, node, repulsion_base)
63
+ update_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, n, used, node, repulsion_base)
64
64
 
65
65
  for _ in range(1, m - 1):
66
66
  node = bit_pick(weights, used, n)
67
67
 
68
68
  # Update answer and weights
69
- update_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, n, used, node, repulsion_base)
69
+ update_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, n, used, node, repulsion_base)
70
70
 
71
71
  node = bit_pick(weights, used, n)
72
72
 
@@ -101,28 +101,34 @@ def compute_cut(sample, G_data, G_rows, G_cols, n_qubits):
101
101
 
102
102
 
103
103
  @njit(parallel=True)
104
- def sample_measurement(max_edge, G_data, G_rows, G_cols, shots, thresholds, weights, repulsion_base, is_spin_glass):
105
- shots = max(1, shots >> 1)
104
+ def sample_measurement(G_data, G_rows, G_cols, max_edge, shots, thresholds, weights, repulsion_base, is_spin_glass):
105
+ shots = ((max(1, shots >> 1) + 3) >> 2) << 2
106
106
  n = G_rows.shape[0] - 1
107
107
  tot_init_weight = weights.sum()
108
108
 
109
109
  solutions = np.empty((shots, n), dtype=np.bool_)
110
110
  energies = np.empty(shots, dtype=dtype)
111
-
111
+
112
112
  best_solution = solutions[0]
113
113
  best_energy = -float("inf")
114
114
 
115
115
  improved = True
116
116
  while improved:
117
117
  improved = False
118
- for s in prange(shots):
119
- # First dimension: Hamming weight
120
- m = sample_mag(thresholds)
121
-
122
- # Second dimension: permutation within Hamming weight
123
- sample = local_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, tot_init_weight, repulsion_base, n, m)
124
- solutions[s] = sample
125
- energies[s] = compute_energy(sample, G_data, G_rows, G_cols, n) if is_spin_glass else compute_cut(sample, G_data, G_rows, G_cols, n)
118
+ if is_spin_glass:
119
+ for s in prange(shots):
120
+ # First dimension: Hamming weight
121
+ m = sample_mag(thresholds)
122
+ # Second dimension: permutation within Hamming weight
123
+ solutions[s] = local_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, tot_init_weight, repulsion_base, n, m)
124
+ energies[s] = compute_energy(solutions[s], G_data, G_rows, G_cols, n)
125
+ else:
126
+ for s in prange(shots):
127
+ # First dimension: Hamming weight
128
+ m = sample_mag(thresholds)
129
+ # Second dimension: permutation within Hamming weight
130
+ solutions[s] = local_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, tot_init_weight, repulsion_base, n, m)
131
+ energies[s] = compute_cut(solutions[s], G_data, G_rows, G_cols, n)
126
132
 
127
133
  best_index = np.argmax(energies)
128
134
  energy = energies[best_index]
@@ -143,7 +149,7 @@ def shot_loop(G_data, G_rows, G_cols, max_edge, thresholds, weights, tot_init_we
143
149
  # First dimension: Hamming weight
144
150
  m = sample_mag(thresholds)
145
151
  # Second dimension: permutation within Hamming weight
146
- solutions[s] = local_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, tot_init_weight, repulsion_base, n, m)
152
+ solutions[s] = local_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, tot_init_weight, repulsion_base, n, m)
147
153
 
148
154
 
149
155
  def sample_for_opencl(G_data, G_rows, G_cols, G_data_buf, G_rows_buf, G_cols_buf, max_edge, shots, thresholds, weights, repulsion_base, is_spin_glass, is_segmented, segment_size):
@@ -152,23 +158,24 @@ def sample_for_opencl(G_data, G_rows, G_cols, G_data_buf, G_rows_buf, G_cols_buf
152
158
  tot_init_weight = weights.sum()
153
159
 
154
160
  solutions = np.empty((shots, n), dtype=np.bool_)
155
- energies = np.empty(shots, dtype=dtype)
156
161
 
157
162
  best_solution = solutions[0]
158
163
  best_energy = -float("inf")
159
164
 
165
+ opencl_args = setup_cut_opencl(shots, n, segment_size, is_spin_glass)
166
+
160
167
  improved = True
161
168
  while improved:
162
169
  improved = False
163
170
  shot_loop(G_data, G_rows, G_cols, max_edge, thresholds, weights, tot_init_weight, repulsion_base, n, shots, solutions)
164
- solution, energy = run_cut_opencl(solutions, G_data_buf, G_rows_buf, G_cols_buf, is_segmented, segment_size, is_spin_glass)
171
+ solution, energy = run_cut_opencl(solutions, G_data_buf, G_rows_buf, G_cols_buf, is_segmented, segment_size, is_spin_glass, *opencl_args)
165
172
  if energy > best_energy:
166
173
  best_energy = energy
167
174
  best_solution = solution
168
175
  improved = True
169
176
 
170
177
  if is_spin_glass:
171
- best_energy = compute_cut(best_solution, G_m, n)
178
+ best_energy = compute_cut(best_solution, G_data, G_rows, G_cols, n)
172
179
 
173
180
  return best_solution, float(best_energy)
174
181
 
@@ -203,14 +210,13 @@ def init_J_and_z(G_m):
203
210
  return J_eff, degrees, G_max
204
211
 
205
212
 
206
- @njit
207
- def cpu_footer(J_eff, degrees, shots, quality, n_qubits, G_max, G_data, G_rows, G_col, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
213
+ def cpu_footer(J_eff, degrees, shots, quality, n_qubits, G_max, G_data, G_rows, G_cols, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
208
214
  hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
209
215
 
210
216
  degrees = None
211
217
  J_eff = 1.0 / (1.0 + epsilon - J_eff)
212
218
 
213
- best_solution, best_value = sample_measurement(G_max, G_data, G_rows, G_col, shots, hamming_prob, J_eff, repulsion_base, is_spin_glass)
219
+ best_solution, best_value = sample_measurement(G_data, G_rows, G_cols, G_max, shots, hamming_prob, J_eff, repulsion_base, is_spin_glass)
214
220
 
215
221
  bit_string, l, r = get_cut(best_solution, nodes)
216
222
 
@@ -232,22 +238,17 @@ def convert_bool_to_uint(samples):
232
238
  return theta
233
239
 
234
240
 
235
- def run_cut_opencl(samples, G_data_buf, G_rows_buf, G_cols_buf, is_segmented, segment_size, is_spin_glass):
241
+ def setup_cut_opencl(shots, n, segment_size, is_spin_glass):
236
242
  ctx = opencl_context.ctx
237
243
  queue = opencl_context.queue
238
- calculate_cut_kernel = opencl_context.calculate_cut_sparse_segmented_kernel if is_segmented else opencl_context.calculate_cut_sparse_kernel
239
244
  dtype = opencl_context.dtype
240
- epsilon = opencl_context.epsilon
241
245
  wgs = opencl_context.work_group_size
242
246
 
243
- shots = samples.shape[1]
244
-
245
247
  # Args: [n, shots, is_spin_glass, prng_seed, segment_size]
246
- args_np = np.array([shots, samples.shape[0], is_spin_glass, segment_size], dtype=np.int32)
248
+ args_np = np.array([n, shots, is_spin_glass, segment_size], dtype=np.int32)
247
249
 
248
250
  # Buffers
249
251
  mf = cl.mem_flags
250
- theta_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=convert_bool_to_uint(samples))
251
252
  args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args_np)
252
253
 
253
254
  # Local memory allocation (1 float per work item)
@@ -264,6 +265,20 @@ def run_cut_opencl(samples, G_data_buf, G_rows_buf, G_cols_buf, is_segmented, se
264
265
  max_energy_buf = cl.Buffer(ctx, mf.WRITE_ONLY, max_energy_host.nbytes)
265
266
  max_index_buf = cl.Buffer(ctx, mf.WRITE_ONLY, max_index_host.nbytes)
266
267
 
268
+ 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
269
+
270
+
271
+ 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):
272
+ queue = opencl_context.queue
273
+ calculate_cut_kernel = opencl_context.calculate_cut_sparse_segmented_kernel if is_segmented else opencl_context.calculate_cut_sparse_kernel
274
+
275
+ shots = samples.shape[0]
276
+ n = samples.shape[1]
277
+
278
+ # Buffers
279
+ mf = cl.mem_flags
280
+ theta_buf = make_theta_buf(convert_bool_to_uint(samples), is_segmented, shots, n)
281
+
267
282
  # Set kernel args
268
283
  if is_segmented:
269
284
  calculate_cut_kernel.set_args(
@@ -273,7 +288,10 @@ def run_cut_opencl(samples, G_data_buf, G_rows_buf, G_cols_buf, is_segmented, se
273
288
  G_data_buf[3],
274
289
  G_rows_buf,
275
290
  G_cols_buf,
276
- theta_buf,
291
+ theta_buf[0],
292
+ theta_buf[1],
293
+ theta_buf[2],
294
+ theta_buf[3],
277
295
  args_buf,
278
296
  max_energy_buf,
279
297
  max_index_buf,
@@ -376,7 +394,7 @@ def maxcut_tfim_sparse(
376
394
  return bit_string, best_value, partition
377
395
 
378
396
  segment_size = G_m.data.shape[0]
379
- is_segmented = (G_m.data.nbytes << 1) > opencl_context.max_alloc
397
+ is_segmented = (G_m.data.nbytes << 1) > opencl_context.max_alloc or ((((((((n_qubits + 31) >> 5) << 5) * ((shots + 1) >> 1)) + 7) >> 3) << 1) > opencl_context.max_alloc)
380
398
 
381
399
  G_data_buf, G_rows_buf, G_cols_buf = make_G_m_csr_buf(G_m, is_segmented, segment_size)
382
400
  if is_nested:
@@ -117,6 +117,36 @@ except ImportError:
117
117
  opencl_context = OpenCLContext(compute_units, IS_OPENCL_AVAILABLE, work_group_size, dtype, epsilon, max_alloc, ctx, queue, bootstrap_kernel, bootstrap_sparse_kernel, bootstrap_segmented_kernel, bootstrap_sparse_segmented_kernel, calculate_cut_kernel, calculate_cut_sparse_kernel, calculate_cut_segmented_kernel, calculate_cut_sparse_segmented_kernel)
118
118
 
119
119
 
120
+ def setup_cut_opencl(shots, n, segment_size, is_spin_glass):
121
+ ctx = opencl_context.ctx
122
+ queue = opencl_context.queue
123
+ dtype = opencl_context.dtype
124
+ wgs = opencl_context.work_group_size
125
+
126
+ # Args: [n, shots, is_spin_glass, prng_seed, segment_size]
127
+ args_np = np.array([n, shots, is_spin_glass, segment_size], dtype=np.int32)
128
+
129
+ # Buffers
130
+ mf = cl.mem_flags
131
+ args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args_np)
132
+
133
+ # Local memory allocation (1 float per work item)
134
+ local_size = min(wgs, shots)
135
+ 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
136
+ global_size = min(((shots + local_size - 1) // local_size) * local_size, max_global_size)
137
+ local_energy_buf = cl.LocalMemory(np.dtype(dtype).itemsize * local_size)
138
+ local_index_buf = cl.LocalMemory(np.dtype(np.int32).itemsize * local_size)
139
+
140
+ # Allocate max_energy and max_index result buffers per workgroup
141
+ max_energy_host = np.empty(global_size, dtype=dtype)
142
+ max_index_host = np.empty(global_size, dtype=np.int32)
143
+
144
+ max_energy_buf = cl.Buffer(ctx, mf.WRITE_ONLY, max_energy_host.nbytes)
145
+ max_index_buf = cl.Buffer(ctx, mf.WRITE_ONLY, max_index_host.nbytes)
146
+
147
+ 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
148
+
149
+
120
150
  def make_G_m_buf(G_m, is_segmented, segment_size):
121
151
  mf = cl.mem_flags
122
152
  ctx = opencl_context.ctx
@@ -162,6 +192,39 @@ def make_G_m_csr_buf(G_m, is_segmented, segment_size):
162
192
 
163
193
  return G_data_buf, G_rows_buf, G_cols_buf
164
194
 
195
+
196
+ def make_theta_buf(theta, is_segmented, shots, n):
197
+ mf = cl.mem_flags
198
+ ctx = opencl_context.ctx
199
+ if is_segmented:
200
+ n_shape = (((shots + 3) >> 2) << 2) * ((n + 31) >> 5)
201
+ theta = np.reshape(theta, (n_shape,))
202
+ _theta_segments = np.split(theta, 4)
203
+ theta_buf = [
204
+ cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=seg)
205
+ for seg in _theta_segments
206
+ ]
207
+ else:
208
+ theta_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=theta)
209
+
210
+ return theta_buf
211
+
212
+
213
+ @njit(parallel=True)
214
+ def convert_bool_to_uint(samples):
215
+ shots = samples.shape[0]
216
+ n32 = ((samples.shape[1] + 31) >> 5) << 5
217
+ theta = np.zeros(shots * (n32 >> 5), dtype=np.uint32)
218
+ for i in prange(shots):
219
+ i_offset = i * n32
220
+ for j in range(n32):
221
+ if samples[i, j]:
222
+ b_index = i_offset + j
223
+ theta[b_index >> 5] |= 1 << (b_index & 31)
224
+
225
+ return theta
226
+
227
+
165
228
  @njit
166
229
  def get_cut(solution, nodes):
167
230
  bit_string = ""
@@ -225,6 +225,7 @@ def spin_glass_solver(
225
225
  if is_combo_maxcut_gpu and IS_OPENCL_AVAILABLE:
226
226
  if not (opencl_context.G_m_buf is None):
227
227
  G_m_buf = opencl_context.G_m_buf
228
+ is_segmented = isinstance(G_m_buf, list)
228
229
  else:
229
230
  G_m_buf = make_G_m_buf(G_m, is_segmented, segment_size)
230
231
 
@@ -247,6 +247,7 @@ def spin_glass_solver_sparse(
247
247
  G_data_buf = opencl_context.G_data_buf
248
248
  G_rows_buf = opencl_context.G_rows_buf
249
249
  G_cols_buf = opencl_context.G_cols_buf
250
+ is_segmented = isinstance(G_data_buf, list)
250
251
  else:
251
252
  mf = cl.mem_flags
252
253
  ctx = opencl_context.ctx
@@ -1,5 +1,5 @@
1
1
  from .maxcut_tfim_util import binary_search, opencl_context, to_scipy_sparse_upper_triangular
2
- from .maxcut_tfim import maxcut_tfim_pure_numba
2
+ from .maxcut_tfim import maxcut_tfim
3
3
  from concurrent.futures import ProcessPoolExecutor
4
4
  import time
5
5
  import itertools
@@ -621,10 +621,7 @@ def tsp_symmetric_driver(G_m, is_cyclic, is_top_level, start_node, end_node, k_n
621
621
  return [nodes[x] for x in best_path], best_weight
622
622
 
623
623
 
624
- @njit
625
624
  def tsp_symmetric_header(G_m, nodes, quality, shots, anneal_t, anneal_h, repulsion_base, start_node, end_node, monte_carlo, is_cyclic, n_nodes, multi_start):
626
- n_x = nodes[0]
627
-
628
625
  if is_cyclic:
629
626
  start_node = None
630
627
  end_node = None
@@ -633,21 +630,16 @@ def tsp_symmetric_header(G_m, nodes, quality, shots, anneal_t, anneal_h, repulsi
633
630
  start_node = end_node
634
631
  end_node = None
635
632
 
636
- a, b, c = [n_x], [n_x], [n_x]
637
- a.clear()
638
- b.clear()
639
- c.clear()
633
+ a, b, c = [], [], []
640
634
  if (start_node is None) and (end_node is None):
641
635
  if monte_carlo:
642
636
  a, b = monte_carlo_loop(n_nodes)
643
637
  else:
644
638
  best_cut = -float("inf")
645
639
  for _ in range(multi_start):
646
- bits = ([n_x], [n_x])
647
- bits[0].clear()
648
- bits[1].clear()
640
+ bits = ([], [])
649
641
  while (len(bits[0]) == 0) or (len(bits[1]) == 0):
650
- _, cut_value, bits = maxcut_tfim_pure_numba(G_m, nodes, quality=quality, shots=shots, anneal_t=anneal_t, anneal_h=anneal_h, repulsion_base=repulsion_base)
642
+ _, cut_value, bits = maxcut_tfim(G_m, quality=quality, shots=shots, anneal_t=anneal_t, anneal_h=anneal_h, repulsion_base=repulsion_base)
651
643
  if cut_value > best_cut:
652
644
  best_cut = cut_value
653
645
  a, b = bits
@@ -838,10 +830,7 @@ def tsp_asymmetric_driver(G_m, is_reversed, is_cyclic, is_top_level, start_node,
838
830
  return [nodes[x] for x in final_path], best_weight
839
831
 
840
832
 
841
- @njit
842
833
  def tsp_asymmetric_header(G_m, nodes, quality, shots, anneal_t, anneal_h, repulsion_base, start_node, end_node, monte_carlo, is_cyclic, n_nodes, multi_start):
843
- n_x = nodes[0]
844
-
845
834
  if is_cyclic:
846
835
  start_node = None
847
836
  end_node = None
@@ -853,21 +842,16 @@ def tsp_asymmetric_header(G_m, nodes, quality, shots, anneal_t, anneal_h, repuls
853
842
  end_node = None
854
843
  G_m = G_m.T
855
844
 
856
- a, b, c = [n_x], [n_x], [n_x]
857
- a.clear()
858
- b.clear()
859
- c.clear()
845
+ a, b, c = [], [], []
860
846
  if (start_node is None) and (end_node is None):
861
847
  if monte_carlo:
862
848
  a, b = monte_carlo_loop(n_nodes)
863
849
  else:
864
850
  best_cut = -float("inf")
865
851
  for _ in range(multi_start):
866
- bits = ([n_x], [n_x])
867
- bits[0].clear()
868
- bits[1].clear()
852
+ bits = ([], [])
869
853
  while (len(bits[0]) == 0) or (len(bits[1]) == 0):
870
- _, cut_value, bits = maxcut_tfim_pure_numba((G_m + G_m.T) / 2, nodes, quality=quality, shots=shots, anneal_t=anneal_t, anneal_h=anneal_h, repulsion_base=repulsion_base)
854
+ _, 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)
871
855
  if cut_value > best_cut:
872
856
  best_cut = cut_value
873
857
  a, b = bits
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 8.14.1
3
+ Version: 8.14.3
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
@@ -7,7 +7,7 @@ with open(README_PATH) as readme_file:
7
7
 
8
8
  setup(
9
9
  name='pyqrackising',
10
- version='8.14.1',
10
+ version='8.14.3',
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