PyQrackIsing 8.14.2__tar.gz → 8.14.4__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.2/pyqrackising.egg-info → pyqrackising-8.14.4}/PKG-INFO +1 -1
  2. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/pyproject.toml +1 -1
  3. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/pyqrackising/maxcut_tfim.py +41 -103
  4. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/pyqrackising/maxcut_tfim_sparse.py +58 -35
  5. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/pyqrackising/maxcut_tfim_streaming.py +20 -9
  6. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/pyqrackising/maxcut_tfim_util.py +30 -0
  7. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/pyqrackising/spin_glass_solver.py +2 -1
  8. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/pyqrackising/spin_glass_solver_sparse.py +5 -3
  9. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/pyqrackising/spin_glass_solver_streaming.py +2 -1
  10. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/pyqrackising/tsp.py +7 -23
  11. {pyqrackising-8.14.2 → pyqrackising-8.14.4/pyqrackising.egg-info}/PKG-INFO +1 -1
  12. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/setup.py +1 -1
  13. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/LICENSE.md +0 -0
  14. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/MANIFEST.in +0 -0
  15. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/README.md +0 -0
  16. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/pyqrackising/__init__.py +0 -0
  17. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
  18. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/pyqrackising/generate_tfim_samples.py +0 -0
  19. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/pyqrackising/kernels.cl +0 -0
  20. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/pyqrackising/spin_glass_solver_util.py +0 -0
  21. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/pyqrackising/tfim_magnetization.py +0 -0
  22. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/pyqrackising/tfim_square_magnetization.py +0 -0
  23. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/pyqrackising/tsp_maxcut.py +0 -0
  24. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/pyqrackising.egg-info/SOURCES.txt +0 -0
  25. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/pyqrackising.egg-info/dependency_links.txt +0 -0
  26. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/pyqrackising.egg-info/not-zip-safe +0 -0
  27. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/pyqrackising.egg-info/top_level.txt +0 -0
  28. {pyqrackising-8.14.2 → pyqrackising-8.14.4}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 8.14.2
3
+ Version: 8.14.4
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.2"
13
+ version = "8.14.4"
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"}
@@ -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 convert_bool_to_uint, get_cut, get_cut_base, make_G_m_buf, make_theta_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,37 @@ 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) + 3) >> 2) << 2
93
+ shots = max(1, shots >> 1)
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
+
111
+ # Second dimension: permutation within Hamming weight
112
+ sample = local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, repulsion_base, n, m)
113
+ solutions[s] = sample
114
+ energies[s] = compute_energy(sample, G_m, n)
115
+ else:
116
+ for s in prange(shots):
117
+ # First dimension: Hamming weight
118
+ m = sample_mag(thresholds)
119
+
120
+ # Second dimension: permutation within Hamming weight
121
+ sample = local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, repulsion_base, n, m)
122
+ solutions[s] = sample
123
+ energies[s] = compute_cut(sample, G_m, n)
114
124
 
115
125
  best_index = np.argmax(energies)
116
126
  energy = energies[best_index]
@@ -120,7 +130,7 @@ def sample_measurement(G_m, max_edge, shots, thresholds, weights, repulsion_base
120
130
  improved = True
121
131
 
122
132
  if is_spin_glass:
123
- best_energy = compute_cut(best_solution, G_m, n)
133
+ best_energy = compute_cut(best_solution, G_m, n)
124
134
 
125
135
  return best_solution, best_energy
126
136
 
@@ -135,21 +145,22 @@ def shot_loop(G_m, max_edge, thresholds, weights, tot_init_weight, repulsion_bas
135
145
 
136
146
 
137
147
  def sample_for_opencl(G_m, G_m_buf, max_edge, shots, thresholds, weights, repulsion_base, is_spin_glass, is_segmented, segment_size):
138
- shots = max(1, shots >> 1)
148
+ shots = ((max(1, shots >> 1) + 3) >> 2) << 2
139
149
  n = len(G_m)
140
150
  tot_init_weight = weights.sum()
141
151
 
142
152
  solutions = np.empty((shots, n), dtype=np.bool_)
143
- energies = np.empty(shots, dtype=dtype)
144
153
 
145
154
  best_solution = solutions[0]
146
155
  best_energy = -float("inf")
147
156
 
157
+ opencl_args = setup_cut_opencl(shots, n, segment_size, is_spin_glass)
158
+
148
159
  improved = True
149
160
  while improved:
150
161
  improved = False
151
162
  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)
163
+ solution, energy = run_cut_opencl(solutions, G_m_buf, is_segmented, segment_size, is_spin_glass, *opencl_args)
153
164
  if energy > best_energy:
154
165
  best_energy = energy
155
166
  best_solution = solution
@@ -207,39 +218,17 @@ def cpu_footer(shots, quality, n_qubits, G_m, nodes, is_spin_glass, anneal_t, an
207
218
  return bit_string, best_value, (l, r)
208
219
 
209
220
 
210
- def run_cut_opencl(samples, G_m_buf, is_segmented, segment_size, is_spin_glass):
211
- ctx = opencl_context.ctx
221
+ 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):
212
222
  queue = opencl_context.queue
213
223
  calculate_cut_kernel = opencl_context.calculate_cut_segmented_kernel if is_segmented else opencl_context.calculate_cut_kernel
214
- dtype = opencl_context.dtype
215
- epsilon = opencl_context.epsilon
216
- wgs = opencl_context.work_group_size
217
224
 
218
- shots = samples.shape[1]
219
-
220
- # Args: [n, shots, is_spin_glass, prng_seed, segment_size]
221
- n = samples.shape[0]
222
- args_np = np.array([shots, n, is_spin_glass, segment_size], dtype=np.int32)
225
+ shots = samples.shape[0]
226
+ n = samples.shape[1]
223
227
 
224
228
  # Buffers
225
229
  mf = cl.mem_flags
226
- args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args_np)
227
230
  theta_buf = make_theta_buf(convert_bool_to_uint(samples), is_segmented, shots, n)
228
231
 
229
- # Local memory allocation (1 float per work item)
230
- local_size = min(wgs, shots)
231
- 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
232
- global_size = min(((shots + local_size - 1) // local_size) * local_size, max_global_size)
233
- local_energy_buf = cl.LocalMemory(np.dtype(dtype).itemsize * local_size)
234
- local_index_buf = cl.LocalMemory(np.dtype(np.int32).itemsize * local_size)
235
-
236
- # Allocate max_energy and max_index result buffers per workgroup
237
- max_energy_host = np.empty(global_size, dtype=dtype)
238
- max_index_host = np.empty(global_size, dtype=np.int32)
239
-
240
- max_energy_buf = cl.Buffer(ctx, mf.WRITE_ONLY, max_energy_host.nbytes)
241
- max_index_buf = cl.Buffer(ctx, mf.WRITE_ONLY, max_index_host.nbytes)
242
-
243
232
  # Set kernel args
244
233
  if is_segmented:
245
234
  calculate_cut_kernel.set_args(
@@ -280,64 +269,6 @@ def run_cut_opencl(samples, G_m_buf, is_segmented, segment_size, is_spin_glass):
280
269
 
281
270
  return samples[best_i], max_energy_host[best_i]
282
271
 
283
-
284
- @njit
285
- def maxcut_tfim_pure_numba(
286
- G_m,
287
- nodes,
288
- quality=None,
289
- shots=None,
290
- is_spin_glass=False,
291
- anneal_t=None,
292
- anneal_h=None,
293
- repulsion_base=None
294
- ):
295
- n_qubits = len(G_m)
296
-
297
- if n_qubits < 3:
298
- empty = [nodes[0]]
299
- empty.clear()
300
-
301
- if n_qubits == 0:
302
- return "", 0, (empty, empty.copy())
303
-
304
- if n_qubits == 1:
305
- return "0", 0, (nodes, empty)
306
-
307
- if n_qubits == 2:
308
- weight = G_m[0, 1]
309
- if weight < 0.0:
310
- return "00", 0, (nodes, empty)
311
-
312
- return "01", weight, ([nodes[0]], [nodes[1]])
313
-
314
- if quality is None:
315
- quality = 6
316
-
317
- if shots is None:
318
- # Number of measurement shots
319
- shots = n_qubits << quality
320
-
321
- if anneal_t is None:
322
- anneal_t = 8.0
323
-
324
- if anneal_h is None:
325
- anneal_h = 8.0
326
-
327
- if repulsion_base is None:
328
- repulsion_base = 8.0
329
-
330
- bit_string, best_value, partition = cpu_footer(shots, quality, n_qubits, G_m, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base)
331
-
332
- if best_value < 0.0:
333
- # Best cut is trivial partition, all/empty
334
- empty = [nodes[0]]
335
- empty.clear()
336
-
337
- return '0' * n_qubits, 0.0, (nodes, empty)
338
-
339
- return bit_string, best_value, partition
340
-
341
272
  def maxcut_tfim(
342
273
  G,
343
274
  quality=None,
@@ -360,11 +291,6 @@ def maxcut_tfim(
360
291
 
361
292
  n_qubits = len(G_m)
362
293
 
363
- is_opencl = is_maxcut_gpu and IS_OPENCL_AVAILABLE and (n_qubits >= max(32, opencl_context.work_group_size))
364
-
365
- if not is_opencl:
366
- return maxcut_tfim_pure_numba(G_m, nodes, quality, shots, is_spin_glass, anneal_t, anneal_h, repulsion_base)
367
-
368
294
  if n_qubits < 3:
369
295
  empty = [nodes[0]]
370
296
  empty.clear()
@@ -398,6 +324,18 @@ def maxcut_tfim(
398
324
  if repulsion_base is None:
399
325
  repulsion_base = 8.0
400
326
 
327
+ wgs = max(32, opencl_context.work_group_size)
328
+ is_opencl = is_maxcut_gpu and IS_OPENCL_AVAILABLE and (n_qubits >= wgs) and (shots >= wgs)
329
+
330
+ if not is_opencl:
331
+ bit_string, best_value, partition = cpu_footer(shots, quality, n_qubits, G_m, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base)
332
+
333
+ if best_value < 0.0:
334
+ # Best cut is trivial partition, all/empty
335
+ return '0' * n_qubits, 0.0, (nodes, [])
336
+
337
+ return bit_string, best_value, partition
338
+
401
339
  segment_size = G_m.shape[0] ** 2
402
340
  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)
403
341
 
@@ -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, convert_bool_to_uint, get_cut, get_cut_base, make_G_m_csr_buf, make_theta_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
 
@@ -79,50 +79,63 @@ def local_repulsion_choice(G_cols, G_data, G_rows, max_edge, weights, tot_init_w
79
79
  def compute_energy(sample, G_data, G_rows, G_cols, n_qubits):
80
80
  energy = 0
81
81
  for u in range(n_qubits):
82
+ u_bit = sample[u]
82
83
  for col in range(G_rows[u], G_rows[u + 1]):
83
84
  v = G_cols[col]
84
85
  val = G_data[col]
85
- energy += val if sample[u] == sample[v] else -val
86
+ energy += val if u_bit == sample[v] else -val
86
87
 
87
88
  return -energy
88
89
 
89
90
 
90
91
  @njit
91
92
  def compute_cut(sample, G_data, G_rows, G_cols, n_qubits):
92
- l, _ = get_cut_base(sample)
93
+ l, r = get_cut_base(sample)
94
+ s = l if len(l) < len(r) else r
93
95
  cut = 0
94
- for u in l:
96
+ for u in s:
97
+ u_bit = sample[u]
95
98
  for col in range(G_rows[u], G_rows[u + 1]):
96
99
  v = G_cols[col]
97
- if sample[u] != sample[v]:
100
+ if u_bit != sample[v]:
98
101
  cut += G_data[col]
99
102
 
100
103
  return cut
101
104
 
102
105
 
103
106
  @njit(parallel=True)
104
- def sample_measurement(max_edge, G_data, G_rows, G_cols, shots, thresholds, weights, repulsion_base, is_spin_glass):
107
+ def sample_measurement(G_data, G_rows, G_cols, max_edge, shots, thresholds, weights, repulsion_base, is_spin_glass):
105
108
  shots = max(1, shots >> 1)
106
109
  n = G_rows.shape[0] - 1
107
110
  tot_init_weight = weights.sum()
108
111
 
109
112
  solutions = np.empty((shots, n), dtype=np.bool_)
110
113
  energies = np.empty(shots, dtype=dtype)
111
-
114
+
112
115
  best_solution = solutions[0]
113
116
  best_energy = -float("inf")
114
117
 
115
118
  improved = True
116
119
  while improved:
117
120
  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)
121
+ if is_spin_glass:
122
+ for s in prange(shots):
123
+ # First dimension: Hamming weight
124
+ m = sample_mag(thresholds)
125
+
126
+ # Second dimension: permutation within Hamming weight
127
+ sample = local_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, tot_init_weight, repulsion_base, n, m)
128
+ solutions[s] = sample
129
+ energies[s] = compute_energy(sample, G_data, G_rows, G_cols, n)
130
+ else:
131
+ for s in prange(shots):
132
+ # First dimension: Hamming weight
133
+ m = sample_mag(thresholds)
134
+
135
+ # Second dimension: permutation within Hamming weight
136
+ sample = local_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, tot_init_weight, repulsion_base, n, m)
137
+ solutions[s] = sample
138
+ energies[s] = compute_cut(sample, G_data, G_rows, G_cols, n)
126
139
 
127
140
  best_index = np.argmax(energies)
128
141
  energy = energies[best_index]
@@ -143,32 +156,33 @@ def shot_loop(G_data, G_rows, G_cols, max_edge, thresholds, weights, tot_init_we
143
156
  # First dimension: Hamming weight
144
157
  m = sample_mag(thresholds)
145
158
  # 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)
159
+ solutions[s] = local_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, tot_init_weight, repulsion_base, n, m)
147
160
 
148
161
 
149
162
  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):
150
- shots = max(1, shots >> 1)
163
+ shots = ((max(1, shots >> 1) + 3) >> 2) << 2
151
164
  n = G_rows.shape[0] - 1
152
165
  tot_init_weight = weights.sum()
153
166
 
154
167
  solutions = np.empty((shots, n), dtype=np.bool_)
155
- energies = np.empty(shots, dtype=dtype)
156
168
 
157
169
  best_solution = solutions[0]
158
170
  best_energy = -float("inf")
159
171
 
172
+ opencl_args = setup_cut_opencl(shots, n, segment_size, is_spin_glass)
173
+
160
174
  improved = True
161
175
  while improved:
162
176
  improved = False
163
177
  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)
178
+ 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
179
  if energy > best_energy:
166
180
  best_energy = energy
167
181
  best_solution = solution
168
182
  improved = True
169
183
 
170
184
  if is_spin_glass:
171
- best_energy = compute_cut(best_solution, G_m, n)
185
+ best_energy = compute_cut(best_solution, G_data, G_rows, G_cols, n)
172
186
 
173
187
  return best_solution, float(best_energy)
174
188
 
@@ -204,13 +218,13 @@ def init_J_and_z(G_m):
204
218
 
205
219
 
206
220
  @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):
221
+ 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
222
  hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
209
223
 
210
224
  degrees = None
211
225
  J_eff = 1.0 / (1.0 + epsilon - J_eff)
212
226
 
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)
227
+ 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
228
 
215
229
  bit_string, l, r = get_cut(best_solution, nodes)
216
230
 
@@ -232,24 +246,18 @@ def convert_bool_to_uint(samples):
232
246
  return theta
233
247
 
234
248
 
235
- def run_cut_opencl(samples, G_data_buf, G_rows_buf, G_cols_buf, is_segmented, segment_size, is_spin_glass):
249
+ def setup_cut_opencl(shots, n, segment_size, is_spin_glass):
236
250
  ctx = opencl_context.ctx
237
251
  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
252
  dtype = opencl_context.dtype
240
- epsilon = opencl_context.epsilon
241
253
  wgs = opencl_context.work_group_size
242
254
 
243
- shots = samples.shape[1]
244
-
245
255
  # Args: [n, shots, is_spin_glass, prng_seed, segment_size]
246
- n = samples.shape[0]
247
- args_np = np.array([shots, n, is_spin_glass, segment_size], dtype=np.int32)
256
+ args_np = np.array([n, shots, is_spin_glass, segment_size], dtype=np.int32)
248
257
 
249
258
  # Buffers
250
259
  mf = cl.mem_flags
251
260
  args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args_np)
252
- theta_buf = make_theta_buf(convert_bool_to_uint(samples), is_segmented, shots, n)
253
261
 
254
262
  # Local memory allocation (1 float per work item)
255
263
  local_size = min(wgs, shots)
@@ -265,6 +273,20 @@ def run_cut_opencl(samples, G_data_buf, G_rows_buf, G_cols_buf, is_segmented, se
265
273
  max_energy_buf = cl.Buffer(ctx, mf.WRITE_ONLY, max_energy_host.nbytes)
266
274
  max_index_buf = cl.Buffer(ctx, mf.WRITE_ONLY, max_index_host.nbytes)
267
275
 
276
+ 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
277
+
278
+
279
+ 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):
280
+ queue = opencl_context.queue
281
+ calculate_cut_kernel = opencl_context.calculate_cut_sparse_segmented_kernel if is_segmented else opencl_context.calculate_cut_sparse_kernel
282
+
283
+ shots = samples.shape[0]
284
+ n = samples.shape[1]
285
+
286
+ # Buffers
287
+ mf = cl.mem_flags
288
+ theta_buf = make_theta_buf(convert_bool_to_uint(samples), is_segmented, shots, n)
289
+
268
290
  # Set kernel args
269
291
  if is_segmented:
270
292
  calculate_cut_kernel.set_args(
@@ -368,7 +390,8 @@ def maxcut_tfim_sparse(
368
390
 
369
391
  n_qubits = G_m.shape[0]
370
392
 
371
- is_opencl = is_maxcut_gpu and IS_OPENCL_AVAILABLE and (n_qubits >= max(32, opencl_context.work_group_size))
393
+ wgs = max(32, opencl_context.work_group_size)
394
+ is_opencl = is_maxcut_gpu and IS_OPENCL_AVAILABLE and (n_qubits >= wgs) and (shots >= wgs)
372
395
 
373
396
  if not is_opencl:
374
397
  bit_string, best_value, partition = cpu_footer(J_eff, degrees, shots, quality, n_qubits, G_max, G_m.data, G_m.indptr, G_m.indices, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base)
@@ -64,9 +64,10 @@ def local_repulsion_choice(G_func, nodes, max_edge, weights, tot_init_weight, re
64
64
  def compute_energy(sample, G_func, nodes, n_qubits):
65
65
  energy = 0
66
66
  for u in range(n_qubits):
67
+ u_bit = sample[u]
67
68
  for v in range(u + 1, n_qubits):
68
69
  val = G_func(nodes[u], nodes[v])
69
- energy += val if sample[u] == sample[v] else -val
70
+ energy += val if u_bit == sample[v] else -val
70
71
 
71
72
  return -energy
72
73
 
@@ -96,14 +97,24 @@ def sample_measurement(G_func, nodes, max_edge, shots, thresholds, degrees_sum,
96
97
  improved = True
97
98
  while improved:
98
99
  improved = False
99
- for s in prange(shots):
100
- # First dimension: Hamming weight
101
- m = sample_mag(thresholds)
102
-
103
- # Second dimension: permutation within Hamming weight
104
- sample = local_repulsion_choice(G_func, nodes, max_edge, weights, tot_init_weight, repulsion_base, n, m)
105
- solutions[s] = sample
106
- energies[s] = compute_energy(sample, G_func, nodes, n) if is_spin_glass else compute_cut(sample, G_func, nodes, n)
100
+ if is_spin_glass:
101
+ for s in prange(shots):
102
+ # First dimension: Hamming weight
103
+ m = sample_mag(thresholds)
104
+
105
+ # Second dimension: permutation within Hamming weight
106
+ sample = local_repulsion_choice(G_func, nodes, max_edge, weights, tot_init_weight, repulsion_base, n, m)
107
+ solutions[s] = sample
108
+ energies[s] = compute_energy(sample, G_func, nodes, n)
109
+ else:
110
+ for s in prange(shots):
111
+ # First dimension: Hamming weight
112
+ m = sample_mag(thresholds)
113
+
114
+ # Second dimension: permutation within Hamming weight
115
+ sample = local_repulsion_choice(G_func, nodes, max_edge, weights, tot_init_weight, repulsion_base, n, m)
116
+ solutions[s] = sample
117
+ energies[s] = compute_cut(sample, G_func, nodes, n)
107
118
 
108
119
  best_index = np.argmax(energies)
109
120
  energy = energies[best_index]
@@ -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
@@ -32,8 +32,9 @@ def compute_energy(theta_bits, G_m):
32
32
  n_qubits = len(G_m)
33
33
  energy = 0
34
34
  for u in range(n_qubits):
35
+ u_bit = theta_bits[u]
35
36
  for v in range(u + 1, n_qubits):
36
- energy += G_m[u, v] if theta_bits[u] == theta_bits[v] else -G_m[u, v]
37
+ energy += G_m[u, v] if u_bit == theta_bits[v] else -G_m[u, v]
37
38
 
38
39
  return energy
39
40
 
@@ -19,12 +19,14 @@ except ImportError:
19
19
 
20
20
  @njit
21
21
  def evaluate_cut_edges(theta_bits, G_data, G_rows, G_cols):
22
- l, _ = get_cut_base(theta_bits)
22
+ l, r = get_cut_base(theta_bits)
23
+ s = l if len(l) < len(r) else r
23
24
  cut = 0
24
- for u in l:
25
+ for u in s:
26
+ u_bit = theta_bits[u]
25
27
  for col in range(G_rows[u], G_rows[u + 1]):
26
28
  v = G_cols[col]
27
- if theta_bits[u] != theta_bits[v]:
29
+ if u_bit != theta_bits[v]:
28
30
  cut += G_data[col]
29
31
 
30
32
  return cut
@@ -28,9 +28,10 @@ def compute_energy(theta_bits, G_func, nodes):
28
28
  n_qubits = len(nodes)
29
29
  energy = 0
30
30
  for u in range(n_qubits):
31
+ u_bit = theta_bits[u]
31
32
  for v in range(u + 1, n_qubits):
32
33
  val = G_func(nodes[u], nodes[v])
33
- energy += (val if theta_bits[u] == theta_bits[v] else -val)
34
+ energy += (val if u_bit == theta_bits[v] else -val)
34
35
 
35
36
  return energy
36
37
 
@@ -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.2
3
+ Version: 8.14.4
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.2',
10
+ version='8.14.4',
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