PyQrackIsing 5.4.4__tar.gz → 5.4.5__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-5.4.4/pyqrackising.egg-info → pyqrackising-5.4.5}/PKG-INFO +1 -1
  2. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/pyproject.toml +1 -1
  3. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/pyqrackising/kernels.cl +1 -0
  4. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/pyqrackising/maxcut_tfim.py +87 -9
  5. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/pyqrackising/maxcut_tfim_sparse.py +13 -7
  6. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/pyqrackising/maxcut_tfim_streaming.py +13 -7
  7. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/pyqrackising/maxcut_tfim_util.py +10 -0
  8. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/pyqrackising/spin_glass_solver.py +7 -3
  9. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/pyqrackising/spin_glass_solver_sparse.py +9 -5
  10. {pyqrackising-5.4.4 → pyqrackising-5.4.5/pyqrackising.egg-info}/PKG-INFO +1 -1
  11. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/setup.py +1 -1
  12. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/CMakeLists.txt +0 -0
  13. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/LICENSE.md +0 -0
  14. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/MANIFEST.in +0 -0
  15. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/README.md +0 -0
  16. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/pyqrackising/__init__.py +0 -0
  17. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/pyqrackising/_pyqrack_ising.cpp +0 -0
  18. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/pyqrackising/generate_tfim_samples.py +0 -0
  19. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/pyqrackising/spin_glass_solver_streaming.py +0 -0
  20. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/pyqrackising/spin_glass_solver_util.py +0 -0
  21. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/pyqrackising/tfim_magnetization.py +0 -0
  22. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/pyqrackising/tfim_square_magnetization.py +0 -0
  23. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/pyqrackising/tsp.py +0 -0
  24. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/pyqrackising.egg-info/SOURCES.txt +0 -0
  25. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/pyqrackising.egg-info/dependency_links.txt +0 -0
  26. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/pyqrackising.egg-info/not-zip-safe +0 -0
  27. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/pyqrackising.egg-info/top_level.txt +0 -0
  28. {pyqrackising-5.4.4 → pyqrackising-5.4.5}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 5.4.4
3
+ Version: 5.4.5
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
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
12
12
 
13
13
  [project]
14
14
  name = "pyqrackising"
15
- version = "5.4.4"
15
+ version = "5.4.5"
16
16
  requires-python = ">=3.8"
17
17
  description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
18
18
  readme = {file = "README.txt", content-type = "text/markdown"}
@@ -243,3 +243,4 @@ __kernel void bootstrap_sparse(
243
243
  min_index_ptr[get_group_id(0)] = loc_index[0];
244
244
  }
245
245
  }
246
+
@@ -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, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
7
+ from .maxcut_tfim_util import fix_cdf, get_cut, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
8
8
 
9
9
  IS_OPENCL_AVAILABLE = True
10
10
  try:
@@ -126,6 +126,78 @@ def init_J_and_z(G_m):
126
126
  return J_eff, degrees
127
127
 
128
128
 
129
+ def run_sampling_opencl(G_m_np, thresholds_np, shots, n):
130
+ ctx = opencl_context.ctx
131
+ queue = opencl_context.queue
132
+ kernel = opencl_context.sample_for_solution_best_bitset_kernel
133
+
134
+ max_local_size = 64 # tune
135
+ max_global_size = 65536
136
+ global_size = min(((shots + max_local_size - 1) // max_local_size) * max_local_size, max_global_size)
137
+ local_size = max_local_size
138
+ num_groups = global_size // local_size
139
+
140
+ # Bit-packing params
141
+ words = (n + 31) // 32
142
+
143
+ # Random seeds (host)
144
+ rng_seeds_np = np.random.randint(1, 2**31-1, size=global_size, dtype=np.uint32)
145
+
146
+ # Device buffers
147
+ mf = cl.mem_flags
148
+ G_m_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m_np)
149
+ thresholds_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=thresholds_np)
150
+ rng_buf = cl.Buffer(ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=rng_seeds_np)
151
+
152
+ # Solutions buffer: each work-item writes a candidate bitset
153
+ solutions_buf = cl.Buffer(ctx, mf.READ_WRITE, size=num_groups * words * np.uint32().nbytes)
154
+
155
+ # Best energies and solutions (per group)
156
+ # We'll reuse solutions_buf to hold them after reduction (kernel copies winners into group slices)
157
+ best_energies_np = np.empty(num_groups, dtype=np.float32)
158
+ best_energies_buf = cl.Buffer(ctx, mf.WRITE_ONLY, best_energies_np.nbytes)
159
+
160
+ # Local memory buffers
161
+ local_energy_buf = cl.LocalMemory(np.dtype(np.float32).itemsize * local_size)
162
+ local_index_buf = cl.LocalMemory(np.dtype(np.int32).itemsize * local_size)
163
+
164
+ # Set kernel args
165
+ kernel.set_args(
166
+ G_m_buf,
167
+ thresholds_buf,
168
+ np.int32(n),
169
+ np.int32(shots),
170
+ rng_buf,
171
+ solutions_buf,
172
+ best_energies_buf,
173
+ local_energy_buf,
174
+ local_index_buf
175
+ )
176
+
177
+ # Launch
178
+ cl.enqueue_nd_range_kernel(queue, kernel, (global_size,), (local_size,))
179
+
180
+ # Copy back
181
+ cl.enqueue_copy(queue, best_energies_np, best_energies_buf)
182
+ solutions_np = np.empty(num_groups * words, dtype=np.uint32)
183
+ cl.enqueue_copy(queue, solutions_np, solutions_buf) # all candidates, includes per-group winners
184
+ queue.finish()
185
+
186
+ # Host reduction
187
+ best_group = np.argmax(best_energies_np) # max cut
188
+ best_energy = float(best_energies_np[best_group])
189
+ best_solution_bits = solutions_np[best_group * words : (best_group + 1) * words]
190
+
191
+ # Unpack bitset into boolean vector
192
+ best_solution = np.zeros(n, dtype=np.bool_)
193
+ for u in range(n):
194
+ w = u >> 5
195
+ b = u & 31
196
+ best_solution[u] = (best_solution_bits[w] >> b) & 1
197
+
198
+ return best_solution, best_energy
199
+
200
+
129
201
  @njit
130
202
  def cpu_footer(shots, quality, n_qubits, G_m, nodes):
131
203
  J_eff, degrees = init_J_and_z(G_m)
@@ -142,12 +214,7 @@ def cpu_footer(shots, quality, n_qubits, G_m, nodes):
142
214
 
143
215
  @njit
144
216
  def gpu_footer(shots, n_qubits, G_m, J_eff, hamming_prob, nodes):
145
- hamming_prob /= hamming_prob.sum()
146
- tot_prob = 0.0
147
- for i in range(n_qubits - 1):
148
- tot_prob += hamming_prob[i]
149
- hamming_prob[i] = tot_prob
150
- hamming_prob[-1] = 2.0
217
+ fix_cdf(hamming_prob)
151
218
 
152
219
  best_solution, best_value = sample_for_solution(G_m, shots, hamming_prob, J_eff)
153
220
 
@@ -159,7 +226,7 @@ def gpu_footer(shots, n_qubits, G_m, J_eff, hamming_prob, nodes):
159
226
  def maxcut_tfim(
160
227
  G,
161
228
  quality=None,
162
- shots=None,
229
+ shots=None
163
230
  ):
164
231
  nodes = None
165
232
  n_qubits = 0
@@ -216,7 +283,7 @@ def maxcut_tfim(
216
283
  args_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
217
284
  J_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
218
285
  deg_buf = cl.Buffer(opencl_context.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
219
- theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE, size=(n_qubits * 4))
286
+ theta_buf = cl.Buffer(opencl_context.ctx, mf.READ_WRITE, size=(n_qubits * 8))
220
287
 
221
288
  # Warp size is 32:
222
289
  group_size = min(n_qubits, 64)
@@ -246,5 +313,16 @@ def maxcut_tfim(
246
313
 
247
314
  # Fetch results
248
315
  cl.enqueue_copy(opencl_context.queue, hamming_prob, ham_buf)
316
+ opencl_context.queue.finish()
317
+
318
+ args_buf.release()
319
+ J_buf.release()
320
+ deg_buf.release()
321
+ theta_buf.release()
322
+
323
+ args_buf = None
324
+ J_buf = None
325
+ deg_buf = None
326
+ theta_buf = None
249
327
 
250
328
  return gpu_footer(shots, n_qubits, G_m, J_eff, hamming_prob, nodes)
@@ -5,7 +5,7 @@ import os
5
5
  from numba import njit, prange
6
6
  from scipy.sparse import lil_matrix, csr_matrix
7
7
 
8
- from .maxcut_tfim_util import get_cut, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
8
+ from .maxcut_tfim_util import fix_cdf, get_cut, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
9
9
 
10
10
  IS_OPENCL_AVAILABLE = True
11
11
  try:
@@ -189,12 +189,7 @@ def cpu_footer(shots, quality, n_qubits, G_data, G_rows, G_cols, nodes):
189
189
 
190
190
  @njit
191
191
  def gpu_footer(shots, n_qubits, G_data, G_rows, G_cols, J_eff, hamming_prob, nodes):
192
- hamming_prob /= hamming_prob.sum()
193
- tot_prob = 0.0
194
- for i in range(n_qubits - 1):
195
- tot_prob += hamming_prob[i]
196
- hamming_prob[i] = tot_prob
197
- hamming_prob[-1] = 2.0
192
+ fix_cdf(hamming_prob)
198
193
 
199
194
  best_solution, best_value = sample_for_solution(G_data, G_rows, G_cols, shots, hamming_prob, J_eff)
200
195
 
@@ -305,5 +300,16 @@ def maxcut_tfim_sparse(
305
300
 
306
301
  # Fetch results
307
302
  cl.enqueue_copy(opencl_context.queue, hamming_prob, ham_buf)
303
+ opencl_context.queue.finish()
304
+
305
+ args_buf.release()
306
+ J_buf.release()
307
+ deg_buf.release()
308
+ theta_buf.release()
309
+
310
+ args_buf = None
311
+ J_buf = None
312
+ deg_buf = None
313
+ theta_buf = None
308
314
 
309
315
  return gpu_footer(shots, n_qubits, G_m.data, G_m.indptr, G_m.indices, J_eff, hamming_prob, nodes)
@@ -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, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
7
+ from .maxcut_tfim_util import fix_cdf, get_cut, init_thresholds, maxcut_hamming_cdf, opencl_context, probability_by_hamming_weight
8
8
 
9
9
  IS_OPENCL_AVAILABLE = True
10
10
  try:
@@ -148,12 +148,7 @@ def cpu_footer(shots, quality, n_qubits, G_func, G_func_args_tuple, nodes):
148
148
 
149
149
  @njit
150
150
  def gpu_footer(shots, n_qubits, G_func, G_func_args_tuple, nodes, G_max, J_eff, degrees, hamming_prob):
151
- hamming_prob /= hamming_prob.sum()
152
- tot_prob = 0.0
153
- for i in range(n_qubits - 1):
154
- tot_prob += hamming_prob[i]
155
- hamming_prob[i] = tot_prob
156
- hamming_prob[-1] = 2.0
151
+ fix_cdf(hamming_prob)
157
152
 
158
153
  best_solution, best_value = sample_for_solution(G_func, G_func_args_tuple, nodes, G_max, shots, hamming_prob, degrees.sum(), J_eff, n_qubits)
159
154
 
@@ -244,5 +239,16 @@ def maxcut_tfim_streaming(
244
239
 
245
240
  # Fetch results
246
241
  cl.enqueue_copy(opencl_context.queue, hamming_prob, ham_buf)
242
+ opencl_context.queue.finish()
243
+
244
+ args_buf.release()
245
+ J_buf.release()
246
+ deg_buf.release()
247
+ theta_buf.release()
248
+
249
+ args_buf = None
250
+ J_buf = None
251
+ deg_buf = None
252
+ theta_buf = None
247
253
 
248
254
  return gpu_footer(shots, n_qubits, G_func, G_func_args_tuple, nodes, G_max, J_eff, degrees, hamming_prob)
@@ -101,6 +101,16 @@ def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, hamming_prob):
101
101
  hamming_prob[-1] = 2.0
102
102
 
103
103
 
104
+ @njit
105
+ def fix_cdf(hamming_prob):
106
+ hamming_prob /= hamming_prob.sum()
107
+ tot_prob = 0.0
108
+ for i in range(len(hamming_prob)):
109
+ tot_prob += hamming_prob[i]
110
+ hamming_prob[i] = tot_prob
111
+ hamming_prob[-1] = 2.0
112
+
113
+
104
114
  @njit
105
115
  def probability_by_hamming_weight(J, h, z, theta, t, n_qubits):
106
116
  bias = np.empty(n_qubits - 1, dtype=np.float32)
@@ -67,7 +67,7 @@ def bootstrap(best_theta, G_m, indices_array, k, min_energy):
67
67
  return min_energy
68
68
 
69
69
 
70
- def run_bootstrap_opencl(best_theta, G_m_np, indices_array_np, k, min_energy):
70
+ def run_bootstrap_opencl(best_theta, G_m_buf, indices_array_np, k, min_energy):
71
71
  ctx = opencl_context.ctx
72
72
  queue = opencl_context.queue
73
73
  bootstrap_kernel = opencl_context.bootstrap_kernel
@@ -83,7 +83,6 @@ def run_bootstrap_opencl(best_theta, G_m_np, indices_array_np, k, min_energy):
83
83
  # Buffers
84
84
  mf = cl.mem_flags
85
85
  best_theta_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=best_theta_np)
86
- G_m_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m_np)
87
86
  indices_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=indices_array_np)
88
87
  args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args_np)
89
88
 
@@ -177,6 +176,11 @@ def spin_glass_solver(G, quality=None, shots=None, best_guess=None):
177
176
  bitstring, _, _ = maxcut_tfim(G_m, quality=quality, shots=shots)
178
177
  best_theta = np.array([b == "1" for b in list(bitstring)], dtype=np.bool_)
179
178
 
179
+ if IS_OPENCL_AVAILABLE:
180
+ mf = cl.mem_flags
181
+ ctx = opencl_context.ctx
182
+ G_m_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m)
183
+
180
184
  min_energy = compute_energy(best_theta, G_m)
181
185
  improved = True
182
186
  correction_quality = 1
@@ -198,7 +202,7 @@ def spin_glass_solver(G, quality=None, shots=None, best_guess=None):
198
202
  combos = combos_list[k - 1]
199
203
 
200
204
  if IS_OPENCL_AVAILABLE:
201
- energy = run_bootstrap_opencl(best_theta, G_m, combos, k, min_energy)
205
+ energy = run_bootstrap_opencl(best_theta, G_m_buf, combos, k, min_energy)
202
206
  else:
203
207
  energy = bootstrap(best_theta, G_m, combos, k, min_energy)
204
208
 
@@ -70,7 +70,7 @@ def bootstrap(best_theta, G_data, G_rows, G_cols, indices_array, k, min_energy):
70
70
  return min_energy
71
71
 
72
72
 
73
- def run_bootstrap_opencl(best_theta, G_data_np, G_rows_np, G_cols_np, indices_array_np, k, min_energy):
73
+ def run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, indices_array_np, k, min_energy):
74
74
  ctx = opencl_context.ctx
75
75
  queue = opencl_context.queue
76
76
  bootstrap_kernel = opencl_context.bootstrap_sparse_kernel
@@ -86,9 +86,6 @@ def run_bootstrap_opencl(best_theta, G_data_np, G_rows_np, G_cols_np, indices_ar
86
86
  # Buffers
87
87
  mf = cl.mem_flags
88
88
  best_theta_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=best_theta_np)
89
- G_data_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_data_np)
90
- G_rows_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_rows_np.astype(np.uint64))
91
- G_cols_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_cols_np.astype(np.uint64))
92
89
  indices_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=indices_array_np)
93
90
  args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args_np)
94
91
 
@@ -196,6 +193,13 @@ def spin_glass_solver_sparse(G, quality=None, shots=None, best_guess=None):
196
193
  bitstring, _, _ = maxcut_tfim_sparse(G_m, quality=quality, shots=shots)
197
194
  best_theta = np.array([b == "1" for b in list(bitstring)], dtype=np.bool_)
198
195
 
196
+ if IS_OPENCL_AVAILABLE:
197
+ mf = cl.mem_flags
198
+ ctx = opencl_context.ctx
199
+ G_data_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m.data)
200
+ G_rows_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m.indptr.astype(np.uint64))
201
+ G_cols_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G_m.indices.astype(np.uint64))
202
+
199
203
  min_energy = compute_energy(best_theta, G_m.data, G_m.indptr, G_m.indices)
200
204
  improved = True
201
205
  correction_quality = 1
@@ -217,7 +221,7 @@ def spin_glass_solver_sparse(G, quality=None, shots=None, best_guess=None):
217
221
  combos = combos_list[k - 1]
218
222
 
219
223
  if IS_OPENCL_AVAILABLE:
220
- energy = run_bootstrap_opencl(best_theta, G_m.data, G_m.indptr, G_m.indices, combos, k, min_energy)
224
+ energy = run_bootstrap_opencl(best_theta, G_data_buf, G_rows_buf, G_cols_buf, combos, k, min_energy)
221
225
  else:
222
226
  energy = bootstrap(best_theta, G_m.data, G_m.indptr, G_m.indices, combos, k, min_energy)
223
227
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 5.4.4
3
+ Version: 5.4.5
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
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
51
51
 
52
52
  setup(
53
53
  name='pyqrackising',
54
- version='5.4.4',
54
+ version='5.4.5',
55
55
  author='Dan Strano',
56
56
  author_email='stranoj@gmail.com',
57
57
  description='Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)',
File without changes
File without changes
File without changes
File without changes