PyQrackIsing 8.14.7__tar.gz → 8.14.9__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.7/pyqrackising.egg-info → pyqrackising-8.14.9}/PKG-INFO +1 -1
  2. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/pyproject.toml +1 -1
  3. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/pyqrackising/maxcut_tfim.py +45 -55
  4. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/pyqrackising/maxcut_tfim_sparse.py +49 -106
  5. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/pyqrackising/maxcut_tfim_streaming.py +24 -37
  6. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/pyqrackising/maxcut_tfim_util.py +0 -12
  7. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/pyqrackising/spin_glass_solver.py +1 -3
  8. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/pyqrackising/spin_glass_solver_sparse.py +1 -3
  9. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/pyqrackising/spin_glass_solver_streaming.py +1 -2
  10. {pyqrackising-8.14.7 → pyqrackising-8.14.9/pyqrackising.egg-info}/PKG-INFO +1 -1
  11. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/setup.py +1 -1
  12. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/LICENSE.md +0 -0
  13. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/MANIFEST.in +0 -0
  14. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/README.md +0 -0
  15. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/pyqrackising/__init__.py +0 -0
  16. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
  17. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/pyqrackising/generate_tfim_samples.py +0 -0
  18. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/pyqrackising/kernels.cl +0 -0
  19. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/pyqrackising/spin_glass_solver_util.py +0 -0
  20. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/pyqrackising/tfim_magnetization.py +0 -0
  21. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/pyqrackising/tfim_square_magnetization.py +0 -0
  22. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/pyqrackising/tsp.py +0 -0
  23. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/pyqrackising/tsp_maxcut.py +0 -0
  24. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/pyqrackising.egg-info/SOURCES.txt +0 -0
  25. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/pyqrackising.egg-info/dependency_links.txt +0 -0
  26. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/pyqrackising.egg-info/not-zip-safe +0 -0
  27. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/pyqrackising.egg-info/top_level.txt +0 -0
  28. {pyqrackising-8.14.7 → pyqrackising-8.14.9}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 8.14.7
3
+ Version: 8.14.9
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.7"
13
+ version = "8.14.9"
14
14
  requires-python = ">=3.8"
15
15
  description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
16
16
  readme = {file = "README.txt", content-type = "text/markdown"}
@@ -1,10 +1,8 @@
1
- import math
2
1
  import networkx as nx
3
2
  import numpy as np
4
- import os
5
3
  from numba import njit, prange
6
4
 
7
- from .maxcut_tfim_util import convert_bool_to_uint, get_cut, get_cut_base, make_G_m_buf, make_theta_buf, maxcut_hamming_cdf, opencl_context, sample_mag, setup_opencl, bit_pick, init_bit_pick
5
+ from .maxcut_tfim_util import convert_bool_to_uint, get_cut, get_cut_base, make_G_m_buf, make_theta_buf, maxcut_hamming_cdf, opencl_context, sample_mag, setup_opencl, bit_pick
8
6
 
9
7
  IS_OPENCL_AVAILABLE = True
10
8
  try:
@@ -19,7 +17,7 @@ wgs = opencl_context.work_group_size
19
17
 
20
18
 
21
19
  @njit
22
- def update_repulsion_choice(G_m, max_edge, weights, n, used, node, repulsion_base):
20
+ def update_repulsion_choice(G_m, weights, n, used, node, repulsion_base):
23
21
  # Select node
24
22
  used[node] = True
25
23
 
@@ -27,41 +25,34 @@ def update_repulsion_choice(G_m, max_edge, weights, n, used, node, repulsion_bas
27
25
  for nbr in range(n):
28
26
  if used[nbr]:
29
27
  continue
30
- weights[nbr] *= repulsion_base ** (-G_m[node, nbr] / max_edge)
28
+ weights[nbr] *= repulsion_base ** (-G_m[node, nbr])
31
29
 
32
30
 
33
31
  # Written by Elara (OpenAI custom GPT) and improved by Dan Strano
34
32
  @njit
35
- def local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, repulsion_base, n, m):
33
+ def local_repulsion_choice(G_m, repulsion_base, n, m, s):
36
34
  """
37
35
  Pick m nodes out of n with repulsion bias:
38
36
  - High-degree nodes are already less likely
39
37
  - After choosing a node, its neighbors' probabilities are further reduced
40
- adjacency_data, adjacency_rows: CSR-format sparse adjacency data
41
- weights: float32 array of shape (n,)
42
38
  """
43
39
 
44
- weights = weights.copy()
45
40
  used = np.zeros(n, dtype=np.bool_) # False = available, True = used
46
41
 
47
42
  # First bit:
48
- node = init_bit_pick(weights, tot_init_weight, n)
49
-
43
+ node = s % n
50
44
  if m == 1:
51
45
  used[node] = True
52
46
  return used
53
47
 
54
- update_repulsion_choice(G_m, max_edge, weights, n, used, node, repulsion_base)
48
+ weights = np.ones(n, dtype=np.float64)
49
+ update_repulsion_choice(G_m, weights, n, used, node, repulsion_base)
55
50
 
56
51
  for _ in range(1, m - 1):
57
52
  node = bit_pick(weights, used, n)
53
+ update_repulsion_choice(G_m, weights, n, used, node, repulsion_base)
58
54
 
59
- # Update answer and weights
60
- update_repulsion_choice(G_m, max_edge, weights, n, used, node, repulsion_base)
61
-
62
- # Count available
63
55
  node = bit_pick(weights, used, n)
64
-
65
56
  used[node] = True
66
57
 
67
58
  return used
@@ -90,10 +81,9 @@ def compute_cut(sample, G_m, n_qubits):
90
81
 
91
82
 
92
83
  @njit(parallel=True)
93
- def sample_measurement(G_m, max_edge, shots, thresholds, weights, repulsion_base, is_spin_glass):
84
+ def sample_measurement(G_m, shots, thresholds, repulsion_base, is_spin_glass):
94
85
  shots = max(1, shots >> 1)
95
86
  n = len(G_m)
96
- tot_init_weight = weights.sum()
97
87
 
98
88
  solutions = np.empty((shots, n), dtype=np.bool_)
99
89
  energies = np.empty(shots, dtype=dtype)
@@ -110,7 +100,7 @@ def sample_measurement(G_m, max_edge, shots, thresholds, weights, repulsion_base
110
100
  m = sample_mag(thresholds)
111
101
 
112
102
  # Second dimension: permutation within Hamming weight
113
- sample = local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, repulsion_base, n, m)
103
+ sample = local_repulsion_choice(G_m, repulsion_base, n, m, s)
114
104
  solutions[s] = sample
115
105
  energies[s] = compute_energy(sample, G_m, n)
116
106
  else:
@@ -119,7 +109,7 @@ def sample_measurement(G_m, max_edge, shots, thresholds, weights, repulsion_base
119
109
  m = sample_mag(thresholds)
120
110
 
121
111
  # Second dimension: permutation within Hamming weight
122
- sample = local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, repulsion_base, n, m)
112
+ sample = local_repulsion_choice(G_m, repulsion_base, n, m, s)
123
113
  solutions[s] = sample
124
114
  energies[s] = compute_cut(sample, G_m, n)
125
115
 
@@ -137,18 +127,17 @@ def sample_measurement(G_m, max_edge, shots, thresholds, weights, repulsion_base
137
127
 
138
128
 
139
129
  @njit(parallel=True)
140
- def shot_loop(G_m, max_edge, thresholds, weights, tot_init_weight, repulsion_base, n, shots, solutions):
130
+ def shot_loop(G_m, thresholds, repulsion_base, n, shots, solutions):
141
131
  for s in prange(shots):
142
132
  # First dimension: Hamming weight
143
133
  m = sample_mag(thresholds)
144
134
  # Second dimension: permutation within Hamming weight
145
- solutions[s] = local_repulsion_choice(G_m, max_edge, weights, tot_init_weight, repulsion_base, n, m)
135
+ solutions[s] = local_repulsion_choice(G_m, repulsion_base, n, m, s)
146
136
 
147
137
 
148
- def sample_for_opencl(G_m, G_m_buf, max_edge, shots, thresholds, weights, repulsion_base, is_spin_glass, is_segmented, segment_size, theta_segment_size):
138
+ def sample_for_opencl(G_m, G_m_buf, shots, thresholds, repulsion_base, is_spin_glass, is_segmented, segment_size, theta_segment_size):
149
139
  shots = ((max(1, shots >> 1) + 31) >> 5) << 5
150
140
  n = len(G_m)
151
- tot_init_weight = weights.sum()
152
141
 
153
142
  solutions = np.empty((shots, n), dtype=np.bool_)
154
143
 
@@ -160,8 +149,8 @@ def sample_for_opencl(G_m, G_m_buf, max_edge, shots, thresholds, weights, repuls
160
149
  improved = True
161
150
  while improved:
162
151
  improved = False
163
- shot_loop(G_m, max_edge, thresholds, weights, tot_init_weight, repulsion_base, n, shots, solutions)
164
- solution, energy = run_cut_opencl(solutions, G_m_buf, is_segmented, segment_size, is_spin_glass, *opencl_args)
152
+ shot_loop(G_m, thresholds, repulsion_base, n, shots, solutions)
153
+ solution, energy = run_cut_opencl(best_energy, solutions, G_m_buf, is_segmented, segment_size, is_spin_glass, *opencl_args)
165
154
  if energy > best_energy:
166
155
  best_energy = energy
167
156
  best_solution = solution.copy()
@@ -174,52 +163,44 @@ def sample_for_opencl(G_m, G_m_buf, max_edge, shots, thresholds, weights, repuls
174
163
 
175
164
 
176
165
  @njit(parallel=True)
177
- def init_J_and_z(G_m):
166
+ def init_J_and_z(G_m, repulsion_base):
178
167
  G_min = G_m.min()
179
168
  n_qubits = len(G_m)
180
169
  degrees = np.empty(n_qubits, dtype=np.uint32)
181
170
  J_eff = np.empty(n_qubits, dtype=np.float64)
182
- G_max = -float("inf")
183
171
  for n in prange(n_qubits):
184
172
  degree = 0
185
173
  J = 0.0
186
174
  for m in range(n_qubits):
187
- val = G_m[n, m]
188
- if val > G_max:
189
- G_max = val
190
- val -= G_min
191
- if val != 0.0:
192
- degree += 1
193
- J += val
194
- val = abs(val)
195
- J = -J / degree if degree > 0 else 0
175
+ val = G_m[n, m] - G_min
176
+ if val <= epsilon:
177
+ continue
178
+ degree += 1
179
+ J += val
180
+ if degree > 0:
181
+ J = -J / degree
196
182
  degrees[n] = degree
197
183
  J_eff[n] = J
198
184
 
199
- G_min = abs(G_min)
200
- G_max = abs(G_max)
201
- if G_min > G_max:
202
- G_max = G_min
203
-
204
- return J_eff, degrees, G_max
185
+ return J_eff, degrees
205
186
 
206
187
 
207
188
  @njit
208
189
  def cpu_footer(shots, quality, n_qubits, G_m, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
209
- J_eff, degrees, max_edge = init_J_and_z(G_m)
190
+ J_eff, degrees = init_J_and_z(G_m, repulsion_base)
210
191
  hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
211
192
 
212
193
  degrees = None
213
- J_eff = repulsion_base ** J_eff
194
+ J_eff = None
214
195
 
215
- best_solution, best_value = sample_measurement(G_m, max_edge, shots, hamming_prob, J_eff, repulsion_base, is_spin_glass)
196
+ best_solution, best_value = sample_measurement(G_m, shots, hamming_prob, repulsion_base, is_spin_glass)
216
197
 
217
198
  bit_string, l, r = get_cut(best_solution, nodes, n_qubits)
218
199
 
219
200
  return bit_string, best_value, (l, r)
220
201
 
221
202
 
222
- def run_cut_opencl(samples, G_m_buf, is_segmented, segment_size, is_spin_glass, local_size, global_size, args_buf, local_energy_buf, local_index_buf, max_energy_host, max_index_host, max_energy_buf, max_index_buf):
203
+ def run_cut_opencl(best_energy, samples, G_m_buf, is_segmented, segment_size, is_spin_glass, local_size, global_size, args_buf, local_energy_buf, local_index_buf, max_energy_host, max_index_host, max_energy_buf, max_index_buf):
223
204
  queue = opencl_context.queue
224
205
  calculate_cut_kernel = opencl_context.calculate_cut_segmented_kernel if is_segmented else opencl_context.calculate_cut_kernel
225
206
 
@@ -262,14 +243,23 @@ def run_cut_opencl(samples, G_m_buf, is_segmented, segment_size, is_spin_glass,
262
243
 
263
244
  # Read results
264
245
  cl.enqueue_copy(queue, max_energy_host, max_energy_buf)
265
- cl.enqueue_copy(queue, max_index_host, max_index_buf)
266
246
  queue.finish()
267
247
 
248
+ # Queue read for results we might not need
249
+ cl.enqueue_copy(queue, max_index_host, max_index_buf)
250
+
268
251
  # Find global minimum
269
252
  best_x = np.argmax(max_energy_host)
270
- best_i = max_index_host[best_x]
253
+ energy = max_energy_host[best_x]
254
+
255
+ if energy < best_energy:
256
+ # No improvement: we can exit early
257
+ return samples[0], max_energy_host[0]
258
+
259
+ # We need the best index
260
+ queue.finish()
271
261
 
272
- return samples[best_i], max_energy_host[best_x]
262
+ return samples[max_index_host[best_x]], energy
273
263
 
274
264
  def maxcut_tfim(
275
265
  G,
@@ -324,7 +314,7 @@ def maxcut_tfim(
324
314
  anneal_h = 8.0
325
315
 
326
316
  if repulsion_base is None:
327
- repulsion_base = 8.0
317
+ repulsion_base = 2.0
328
318
 
329
319
  is_opencl = is_maxcut_gpu and IS_OPENCL_AVAILABLE
330
320
 
@@ -342,13 +332,13 @@ def maxcut_tfim(
342
332
  is_segmented = ((G_m.nbytes << 1) > opencl_context.max_alloc) or ((theta_segment_size << 3) > opencl_context.max_alloc)
343
333
  G_m_buf = make_G_m_buf(G_m, is_segmented, segment_size)
344
334
 
345
- J_eff, degrees, max_edge = init_J_and_z(G_m)
335
+ J_eff, degrees = init_J_and_z(G_m, repulsion_base)
346
336
  hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
347
337
 
348
338
  degrees = None
349
- J_eff = repulsion_base ** J_eff
339
+ J_eff = None
350
340
 
351
- best_solution, best_value = sample_for_opencl(G_m, G_m_buf, max_edge, shots, hamming_prob, J_eff, repulsion_base, is_spin_glass, is_segmented, segment_size, theta_segment_size)
341
+ best_solution, best_value = sample_for_opencl(G_m, G_m_buf, shots, hamming_prob, repulsion_base, is_spin_glass, is_segmented, segment_size, theta_segment_size)
352
342
 
353
343
  bit_string, l, r = get_cut(best_solution, nodes, n_qubits)
354
344
 
@@ -1,10 +1,8 @@
1
- import math
2
1
  import networkx as nx
3
2
  import numpy as np
4
- import os
5
3
  from numba import njit, prange
6
4
 
7
- from .maxcut_tfim_util import binary_search, convert_bool_to_uint, get_cut, get_cut_base, make_G_m_csr_buf, make_theta_buf, maxcut_hamming_cdf, opencl_context, sample_mag, setup_opencl, bit_pick, init_bit_pick, to_scipy_sparse_upper_triangular
5
+ from .maxcut_tfim_util import binary_search, convert_bool_to_uint, get_cut, get_cut_base, make_G_m_csr_buf, make_theta_buf, maxcut_hamming_cdf, opencl_context, sample_mag, setup_opencl, bit_pick, to_scipy_sparse_upper_triangular
8
6
 
9
7
  IS_OPENCL_AVAILABLE = True
10
8
  try:
@@ -19,7 +17,7 @@ wgs = opencl_context.work_group_size
19
17
 
20
18
 
21
19
  @njit
22
- def update_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, n, used, node, repulsion_base):
20
+ def update_repulsion_choice(G_data, G_rows, G_cols, weights, n, used, node, repulsion_base):
23
21
  # Select node
24
22
  used[node] = True
25
23
 
@@ -28,7 +26,7 @@ def update_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, n, used,
28
26
  nbr = G_cols[j]
29
27
  if used[nbr]:
30
28
  continue
31
- weights[nbr] *= repulsion_base ** (-G_data[j] / max_edge)
29
+ weights[nbr] *= repulsion_base ** (-G_data[j])
32
30
 
33
31
  for nbr in range(node):
34
32
  if used[nbr]:
@@ -37,40 +35,34 @@ def update_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, n, used,
37
35
  end = G_rows[nbr + 1]
38
36
  j = binary_search(G_cols[start:end], node) + start
39
37
  if j < end:
40
- weights[nbr] *= repulsion_base ** (-G_data[j] / max_edge)
38
+ weights[nbr] *= repulsion_base ** (-G_data[j])
41
39
 
42
40
 
43
41
  # Written by Elara (OpenAI custom GPT) and improved by Dan Strano
44
42
  @njit
45
- def local_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, tot_init_weight, repulsion_base, n, m):
43
+ def local_repulsion_choice(G_data, G_rows, G_cols, repulsion_base, n, m, s):
46
44
  """
47
45
  Pick m nodes out of n with repulsion bias:
48
46
  - High-degree nodes are already less likely
49
47
  - After choosing a node, its neighbors' probabilities are further reduced
50
- adjacency_data, adjacency_rows: CSR-format sparse adjacency data
51
- weights: float32 array of shape (n,)
52
48
  """
53
49
 
54
- weights = weights.copy()
55
50
  used = np.zeros(n, dtype=np.bool_) # False = available, True = used
56
51
 
57
52
  # First bit:
58
- node = init_bit_pick(weights, tot_init_weight, n)
59
-
53
+ node = s % n
60
54
  if m == 1:
61
55
  used[node] = True
62
56
  return used
63
57
 
64
- update_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, n, used, node, repulsion_base)
58
+ weights = np.ones(n, dtype=np.float64)
59
+ update_repulsion_choice(G_data, G_rows, G_cols, weights, n, used, node, repulsion_base)
65
60
 
66
61
  for _ in range(1, m - 1):
67
62
  node = bit_pick(weights, used, n)
68
-
69
- # Update answer and weights
70
- update_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, n, used, node, repulsion_base)
63
+ update_repulsion_choice(G_data, G_rows, G_cols, weights, n, used, node, repulsion_base)
71
64
 
72
65
  node = bit_pick(weights, used, n)
73
-
74
66
  used[node] = True
75
67
 
76
68
  return used
@@ -82,9 +74,8 @@ def compute_energy(sample, G_data, G_rows, G_cols, n_qubits):
82
74
  for u in range(n_qubits):
83
75
  u_bit = sample[u]
84
76
  for col in range(G_rows[u], G_rows[u + 1]):
85
- v = G_cols[col]
86
77
  val = G_data[col]
87
- energy += val if u_bit == sample[v] else -val
78
+ energy += val if u_bit == sample[G_cols[col]] else -val
88
79
 
89
80
  return -energy
90
81
 
@@ -97,18 +88,16 @@ def compute_cut(sample, G_data, G_rows, G_cols, n_qubits):
97
88
  for u in s:
98
89
  u_bit = sample[u]
99
90
  for col in range(G_rows[u], G_rows[u + 1]):
100
- v = G_cols[col]
101
- if u_bit != sample[v]:
91
+ if u_bit != sample[G_cols[col]]:
102
92
  cut += G_data[col]
103
93
 
104
94
  return cut
105
95
 
106
96
 
107
97
  @njit(parallel=True)
108
- def sample_measurement(G_data, G_rows, G_cols, max_edge, shots, thresholds, weights, repulsion_base, is_spin_glass):
98
+ def sample_measurement(G_data, G_rows, G_cols, shots, thresholds, repulsion_base, is_spin_glass):
109
99
  shots = max(1, shots >> 1)
110
100
  n = G_rows.shape[0] - 1
111
- tot_init_weight = weights.sum()
112
101
 
113
102
  solutions = np.empty((shots, n), dtype=np.bool_)
114
103
  energies = np.empty(shots, dtype=dtype)
@@ -125,7 +114,7 @@ def sample_measurement(G_data, G_rows, G_cols, max_edge, shots, thresholds, weig
125
114
  m = sample_mag(thresholds)
126
115
 
127
116
  # Second dimension: permutation within Hamming weight
128
- sample = local_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, tot_init_weight, repulsion_base, n, m)
117
+ sample = local_repulsion_choice(G_data, G_rows, G_cols, repulsion_base, n, m, s)
129
118
  solutions[s] = sample
130
119
  energies[s] = compute_energy(sample, G_data, G_rows, G_cols, n)
131
120
  else:
@@ -134,7 +123,7 @@ def sample_measurement(G_data, G_rows, G_cols, max_edge, shots, thresholds, weig
134
123
  m = sample_mag(thresholds)
135
124
 
136
125
  # Second dimension: permutation within Hamming weight
137
- sample = local_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, tot_init_weight, repulsion_base, n, m)
126
+ sample = local_repulsion_choice(G_data, G_rows, G_cols, repulsion_base, n, m, s)
138
127
  solutions[s] = sample
139
128
  energies[s] = compute_cut(sample, G_data, G_rows, G_cols, n)
140
129
 
@@ -152,18 +141,17 @@ def sample_measurement(G_data, G_rows, G_cols, max_edge, shots, thresholds, weig
152
141
 
153
142
 
154
143
  @njit(parallel=True)
155
- def shot_loop(G_data, G_rows, G_cols, max_edge, thresholds, weights, tot_init_weight, repulsion_base, n, shots, solutions):
144
+ def shot_loop(G_data, G_rows, G_cols, thresholds, repulsion_base, n, shots, solutions):
156
145
  for s in prange(shots):
157
146
  # First dimension: Hamming weight
158
147
  m = sample_mag(thresholds)
159
148
  # Second dimension: permutation within Hamming weight
160
- solutions[s] = local_repulsion_choice(G_data, G_rows, G_cols, max_edge, weights, tot_init_weight, repulsion_base, n, m)
149
+ solutions[s] = local_repulsion_choice(G_data, G_rows, G_cols, repulsion_base, n, m, s)
161
150
 
162
151
 
163
- def sample_for_opencl(G_data, G_rows, G_cols, G_data_buf, G_rows_buf, G_cols_buf, max_edge, shots, thresholds, weights, repulsion_base, is_spin_glass, is_segmented, segment_size, theta_segment_size):
152
+ def sample_for_opencl(G_data, G_rows, G_cols, G_data_buf, G_rows_buf, G_cols_buf, shots, thresholds, repulsion_base, is_spin_glass, is_segmented, segment_size, theta_segment_size):
164
153
  shots = ((max(1, shots >> 1) + 31) >> 5) << 5
165
154
  n = G_rows.shape[0] - 1
166
- tot_init_weight = weights.sum()
167
155
 
168
156
  solutions = np.empty((shots, n), dtype=np.bool_)
169
157
 
@@ -175,8 +163,8 @@ def sample_for_opencl(G_data, G_rows, G_cols, G_data_buf, G_rows_buf, G_cols_buf
175
163
  improved = True
176
164
  while improved:
177
165
  improved = False
178
- shot_loop(G_data, G_rows, G_cols, max_edge, thresholds, weights, tot_init_weight, repulsion_base, n, shots, solutions)
179
- solution, energy = run_cut_opencl(solutions, G_data_buf, G_rows_buf, G_cols_buf, is_segmented, segment_size, is_spin_glass, *opencl_args)
166
+ shot_loop(G_data, G_rows, G_cols, thresholds, repulsion_base, n, shots, solutions)
167
+ solution, energy = run_cut_opencl(best_energy, solutions, G_data_buf, G_rows_buf, G_cols_buf, is_segmented, segment_size, is_spin_glass, *opencl_args)
180
168
  if energy > best_energy:
181
169
  best_energy = energy
182
170
  best_solution = solution.copy()
@@ -188,95 +176,41 @@ def sample_for_opencl(G_data, G_rows, G_cols, G_data_buf, G_rows_buf, G_cols_buf
188
176
  return best_solution, float(best_energy)
189
177
 
190
178
 
191
- def init_J_and_z(G_m):
179
+ def init_J_and_z(G_m, repulsion_base):
192
180
  G_min = G_m.min()
193
181
  n_qubits = G_m.shape[0]
194
182
  degrees = np.empty(n_qubits, dtype=np.uint32)
195
183
  J_eff = np.empty(n_qubits, dtype=np.float64)
196
- G_max = -float("inf")
197
184
  for n in prange(n_qubits):
198
185
  degree = 0
199
186
  J = 0.0
187
+ weight = 0.0
200
188
  for m in range(n_qubits):
201
- val = G_m[n, m]
202
- if val > G_max:
203
- G_max = val
204
- val -= G_min
205
- if val != 0.0:
206
- degree += 1
207
- J += val
208
- val = abs(val)
209
- J = -J / degree if degree > 0 else 0
189
+ val = G_m[n, m] - G_min
190
+ if val <= epsilon:
191
+ continue
192
+ degree += 1
193
+ J += val
194
+ if degree > 0:
195
+ J = -J / degree
210
196
  degrees[n] = degree
211
197
  J_eff[n] = J
212
198
 
213
- G_min = abs(G_min)
214
- G_max = abs(G_max)
215
- if G_min > G_max:
216
- G_max = G_min
217
-
218
- return J_eff, degrees, G_max
199
+ return J_eff, degrees
219
200
 
220
201
 
221
202
  @njit
222
- 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):
203
+ def cpu_footer(J_eff, degrees, shots, quality, n_qubits, G_data, G_rows, G_cols, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
223
204
  hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
224
205
 
225
- J_eff = repulsion_base ** J_eff
226
-
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)
206
+ best_solution, best_value = sample_measurement(G_data, G_rows, G_cols, shots, hamming_prob, repulsion_base, is_spin_glass)
228
207
 
229
208
  bit_string, l, r = get_cut(best_solution, nodes, n_qubits)
230
209
 
231
210
  return bit_string, best_value, (l, r)
232
211
 
233
212
 
234
- @njit(parallel=True)
235
- def convert_bool_to_uint(samples):
236
- shots = samples.shape[0]
237
- n32 = ((samples.shape[1] + 31) >> 5) << 5
238
- theta = np.zeros(shots * (n32 >> 5), dtype=np.uint32)
239
- for i in prange(shots):
240
- i_offset = i * n32
241
- for j in range(n32):
242
- if samples[i, j]:
243
- b_index = i_offset + j
244
- theta[b_index >> 5] |= 1 << (b_index & 31)
245
-
246
- return theta
247
-
248
-
249
- def setup_cut_opencl(shots, n, segment_size, is_spin_glass):
250
- ctx = opencl_context.ctx
251
- queue = opencl_context.queue
252
- dtype = opencl_context.dtype
253
- wgs = opencl_context.work_group_size
254
-
255
- # Args: [n, shots, is_spin_glass, prng_seed, segment_size]
256
- args_np = np.array([n, shots, is_spin_glass, segment_size], dtype=np.int32)
257
-
258
- # Buffers
259
- mf = cl.mem_flags
260
- args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args_np)
261
-
262
- # Local memory allocation (1 float per work item)
263
- local_size = min(wgs, shots)
264
- 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
265
- global_size = min(((shots + local_size - 1) // local_size) * local_size, max_global_size)
266
- local_energy_buf = cl.LocalMemory(np.dtype(dtype).itemsize * local_size)
267
- local_index_buf = cl.LocalMemory(np.dtype(np.int32).itemsize * local_size)
268
-
269
- # Allocate max_energy and max_index result buffers per workgroup
270
- max_energy_host = np.empty(global_size, dtype=dtype)
271
- max_index_host = np.empty(global_size, dtype=np.int32)
272
-
273
- max_energy_buf = cl.Buffer(ctx, mf.WRITE_ONLY, max_energy_host.nbytes)
274
- max_index_buf = cl.Buffer(ctx, mf.WRITE_ONLY, max_index_host.nbytes)
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):
213
+ def run_cut_opencl(best_energy, samples, G_data_buf, G_rows_buf, G_cols_buf, is_segmented, segment_size, is_spin_glass, local_size, global_size, args_buf, local_energy_buf, local_index_buf, max_energy_host, max_index_host, max_energy_buf, max_index_buf):
280
214
  queue = opencl_context.queue
281
215
  calculate_cut_kernel = opencl_context.calculate_cut_sparse_segmented_kernel if is_segmented else opencl_context.calculate_cut_sparse_kernel
282
216
 
@@ -323,14 +257,23 @@ def run_cut_opencl(samples, G_data_buf, G_rows_buf, G_cols_buf, is_segmented, se
323
257
 
324
258
  # Read results
325
259
  cl.enqueue_copy(queue, max_energy_host, max_energy_buf)
326
- cl.enqueue_copy(queue, max_index_host, max_index_buf)
327
260
  queue.finish()
328
261
 
262
+ # Queue read for results we might not need
263
+ cl.enqueue_copy(queue, max_index_host, max_index_buf)
264
+
329
265
  # Find global minimum
330
266
  best_x = np.argmax(max_energy_host)
331
- best_i = max_index_host[best_x]
267
+ energy = max_energy_host[best_x]
268
+
269
+ if energy < best_energy:
270
+ # No improvement: we can exit early
271
+ return samples[0], max_energy_host[0]
272
+
273
+ # We need the best index
274
+ queue.finish()
332
275
 
333
- return samples[best_i], max_energy_host[best_x]
276
+ return samples[max_index_host[best_x]], energy
334
277
 
335
278
 
336
279
 
@@ -386,16 +329,16 @@ def maxcut_tfim_sparse(
386
329
  anneal_h = 8.0
387
330
 
388
331
  if repulsion_base is None:
389
- repulsion_base = 8.0
332
+ repulsion_base = 2.0
390
333
 
391
- J_eff, degrees, G_max = init_J_and_z(G_m)
334
+ J_eff, degrees = init_J_and_z(G_m, repulsion_base)
392
335
 
393
336
  n_qubits = G_m.shape[0]
394
337
 
395
338
  is_opencl = is_maxcut_gpu and IS_OPENCL_AVAILABLE
396
339
 
397
340
  if not is_opencl:
398
- 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)
341
+ bit_string, best_value, partition = cpu_footer(J_eff, degrees, shots, quality, n_qubits, G_m.data, G_m.indptr, G_m.indices, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base)
399
342
 
400
343
  if best_value < 0.0:
401
344
  # Best cut is trivial partition, all/empty
@@ -412,9 +355,9 @@ def maxcut_tfim_sparse(
412
355
  hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
413
356
 
414
357
  degrees = None
415
- J_eff = repulsion_base ** J_eff
358
+ J_eff = None
416
359
 
417
- best_solution, best_value = sample_for_opencl(G_m.data, G_m.indptr, G_m.indices, G_data_buf, G_rows_buf, G_cols_buf, G_max, shots, hamming_prob, J_eff, repulsion_base, is_spin_glass, is_segmented, segment_size, theta_segment_size)
360
+ best_solution, best_value = sample_for_opencl(G_m.data, G_m.indptr, G_m.indices, G_data_buf, G_rows_buf, G_cols_buf, shots, hamming_prob, repulsion_base, is_spin_glass, is_segmented, segment_size, theta_segment_size)
418
361
 
419
362
  bit_string, l, r = get_cut(best_solution, nodes, n_qubits)
420
363
 
@@ -1,10 +1,8 @@
1
- import math
2
1
  import networkx as nx
3
2
  import numpy as np
4
- import os
5
3
  from numba import njit, prange
6
4
 
7
- from .maxcut_tfim_util import get_cut, get_cut_base, maxcut_hamming_cdf, opencl_context, sample_mag, bit_pick, init_bit_pick
5
+ from .maxcut_tfim_util import get_cut, get_cut_base, maxcut_hamming_cdf, opencl_context, sample_mag, bit_pick
8
6
 
9
7
 
10
8
  epsilon = opencl_context.epsilon
@@ -12,7 +10,7 @@ dtype = opencl_context.dtype
12
10
 
13
11
 
14
12
  @njit
15
- def update_repulsion_choice(G_func, nodes, max_edge, weights, n, used, node, repulsion_base):
13
+ def update_repulsion_choice(G_func, nodes, weights, n, used, node, repulsion_base):
16
14
  # Select node
17
15
  used[node] = True
18
16
 
@@ -20,41 +18,34 @@ def update_repulsion_choice(G_func, nodes, max_edge, weights, n, used, node, rep
20
18
  for nbr in range(n):
21
19
  if used[nbr]:
22
20
  continue
23
- weights[nbr] *= repulsion_base ** (-G_func(nodes[node], nodes[nbr]) / max_edge)
21
+ weights[nbr] *= repulsion_base ** (-G_func(nodes[node], nodes[nbr]))
24
22
 
25
23
 
26
24
  # Written by Elara (OpenAI custom GPT) and improved by Dan Strano
27
25
  @njit
28
- def local_repulsion_choice(G_func, nodes, max_edge, weights, tot_init_weight, repulsion_base, n, m):
26
+ def local_repulsion_choice(G_func, nodes, repulsion_base, n, m, s):
29
27
  """
30
28
  Pick m nodes out of n with repulsion bias:
31
29
  - High-degree nodes are already less likely
32
30
  - After choosing a node, its neighbors' probabilities are further reduced
33
- adjacency_data, adjacency_rows: CSR-format sparse adjacency data
34
- weights: float array of shape (n,)
35
31
  """
36
32
 
37
- weights = weights.copy()
38
33
  used = np.zeros(n, dtype=np.bool_) # False = available, True = used
39
34
 
40
35
  # First bit:
41
- node = init_bit_pick(weights, tot_init_weight, n)
42
-
36
+ node = s % n
43
37
  if m == 1:
44
38
  used[node] = True
45
39
  return used
46
40
 
47
- update_repulsion_choice(G_func, nodes, max_edge, weights, n, used, node, repulsion_base)
41
+ weights = np.ones(n, dtype=np.float64)
42
+ update_repulsion_choice(G_func, nodes, weights, n, used, node, repulsion_base)
48
43
 
49
44
  for _ in range(1, m - 1):
50
45
  node = bit_pick(weights, used, n)
51
-
52
- # Update answer and weights
53
- update_repulsion_choice(G_func, nodes, max_edge, weights, n, used, node, repulsion_base)
46
+ update_repulsion_choice(G_func, nodes, weights, n, used, node, repulsion_base)
54
47
 
55
48
  node = bit_pick(weights, used, n)
56
-
57
- # Select node
58
49
  used[node] = True
59
50
 
60
51
  return used
@@ -84,9 +75,8 @@ def compute_cut(sample, G_func, nodes, n_qubits):
84
75
 
85
76
 
86
77
  @njit(parallel=True)
87
- def sample_measurement(G_func, nodes, max_edge, shots, thresholds, weights, n, repulsion_base, is_spin_glass):
78
+ def sample_measurement(G_func, nodes, shots, thresholds, n, repulsion_base, is_spin_glass):
88
79
  shots = max(1, shots >> 1)
89
- tot_init_weight = weights.sum()
90
80
 
91
81
  solutions = np.empty((shots, n), dtype=np.bool_)
92
82
  energies = np.empty(shots, dtype=dtype)
@@ -103,7 +93,7 @@ def sample_measurement(G_func, nodes, max_edge, shots, thresholds, weights, n, r
103
93
  m = sample_mag(thresholds)
104
94
 
105
95
  # Second dimension: permutation within Hamming weight
106
- sample = local_repulsion_choice(G_func, nodes, max_edge, weights, tot_init_weight, repulsion_base, n, m)
96
+ sample = local_repulsion_choice(G_func, nodes, repulsion_base, n, m, s)
107
97
  solutions[s] = sample
108
98
  energies[s] = compute_energy(sample, G_func, nodes, n)
109
99
  else:
@@ -112,7 +102,7 @@ def sample_measurement(G_func, nodes, max_edge, shots, thresholds, weights, n, r
112
102
  m = sample_mag(thresholds)
113
103
 
114
104
  # Second dimension: permutation within Hamming weight
115
- sample = local_repulsion_choice(G_func, nodes, max_edge, weights, tot_init_weight, repulsion_base, n, m)
105
+ sample = local_repulsion_choice(G_func, nodes, repulsion_base, n, m, s)
116
106
  solutions[s] = sample
117
107
  energies[s] = compute_cut(sample, G_func, nodes, n)
118
108
 
@@ -130,28 +120,25 @@ def sample_measurement(G_func, nodes, max_edge, shots, thresholds, weights, n, r
130
120
 
131
121
 
132
122
  @njit(parallel=True)
133
- def init_J_and_z(G_func, nodes, G_min):
123
+ def init_J_and_z(G_func, nodes, G_min, repulsion_base):
134
124
  n_qubits = len(nodes)
135
125
  degrees = np.empty(n_qubits, dtype=np.uint32)
136
126
  J_eff = np.empty(n_qubits, dtype=np.float64)
137
- G_max = -float("inf")
138
127
  for n in prange(n_qubits):
139
128
  degree = 0
140
129
  J = 0.0
141
130
  for m in range(n_qubits):
142
- val = G_func(nodes[n], nodes[m])
143
- if val > G_max:
144
- G_max = val
145
- val -= G_min
146
- if val != 0.0:
147
- degree += 1
148
- J += val
149
- val = abs(val)
150
- J = -J / degree if degree > 0 else 0
131
+ val = G_func(nodes[n], nodes[m]) - G_min
132
+ if val <= epsilon:
133
+ continue
134
+ degree += 1
135
+ J += val
136
+ if degree > 0:
137
+ J = -J / degree
151
138
  degrees[n] = degree
152
139
  J_eff[n] = J
153
140
 
154
- return J_eff, degrees, G_max
141
+ return J_eff, degrees
155
142
 
156
143
  @njit
157
144
  def find_G_min(G_func, nodes, n_nodes):
@@ -169,13 +156,13 @@ def find_G_min(G_func, nodes, n_nodes):
169
156
 
170
157
  @njit
171
158
  def cpu_footer(shots, quality, n_qubits, G_min, G_func, nodes, is_spin_glass, anneal_t, anneal_h, repulsion_base):
172
- J_eff, degrees, max_edge = init_J_and_z(G_func, nodes, G_min)
159
+ J_eff, degrees = init_J_and_z(G_func, nodes, G_min, repulsion_base)
173
160
  hamming_prob = maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, anneal_t, anneal_h)
174
161
 
175
162
  degrees = None
176
- J_eff = repulsion_base ** J_eff
163
+ J_eff = None
177
164
 
178
- best_solution, best_value = sample_measurement(G_func, nodes, max_edge, shots, hamming_prob, J_eff, n_qubits, repulsion_base, is_spin_glass)
165
+ best_solution, best_value = sample_measurement(G_func, nodes, shots, hamming_prob, n_qubits, repulsion_base, is_spin_glass)
179
166
 
180
167
  bit_string, l, r = get_cut(best_solution, nodes, n_qubits)
181
168
 
@@ -223,7 +210,7 @@ def maxcut_tfim_streaming(
223
210
  anneal_h = 8.0
224
211
 
225
212
  if repulsion_base is None:
226
- repulsion_base = 8.0
213
+ repulsion_base = 2.0
227
214
 
228
215
  G_min = find_G_min(G_func, nodes, n_qubits)
229
216
 
@@ -393,18 +393,6 @@ def sample_mag(cum_prob):
393
393
 
394
394
  return m + 1
395
395
 
396
- @njit
397
- def init_bit_pick(weights, p, n):
398
- p *= np.random.rand()
399
- cum = 0.0
400
- node = 0
401
- for i in range(n):
402
- cum += weights[i]
403
- if p < cum:
404
- node = i
405
- break
406
-
407
- return node
408
396
 
409
397
  @njit
410
398
  def bit_pick(weights, used, n):
@@ -2,11 +2,9 @@ from .maxcut_tfim import maxcut_tfim
2
2
  from .maxcut_tfim_util import get_cut_base, make_G_m_buf, opencl_context, setup_opencl
3
3
  from .spin_glass_solver_util import get_cut_from_bit_array, int_to_bitstring
4
4
  import itertools
5
- import math
6
5
  import networkx as nx
7
6
  import numpy as np
8
7
  from numba import njit, prange
9
- import os
10
8
  import random
11
9
 
12
10
 
@@ -258,7 +256,7 @@ def spin_glass_solver(
258
256
 
259
257
  if reheat_round < reheat_tries:
260
258
  num_to_flip = int(np.round(np.log2(n_qubits)))
261
- bits_to_flip = random.sample(list(range(n_qubits)), num_to_flip)
259
+ bits_to_flip = random.sample(range(n_qubits), num_to_flip)
262
260
  for bit in bits_to_flip:
263
261
  reheat_theta[bit] = not reheat_theta[bit]
264
262
  reheat_min_energy = compute_energy(reheat_theta, G_m, n_qubits) if is_spin_glass else -evaluate_cut_edges(reheat_theta, G_m, n_qubits)
@@ -2,11 +2,9 @@ from .maxcut_tfim_sparse import maxcut_tfim_sparse
2
2
  from .maxcut_tfim_util import get_cut_base, make_G_m_csr_buf, opencl_context, setup_opencl
3
3
  from .spin_glass_solver_util import get_cut_from_bit_array, int_to_bitstring
4
4
  import itertools
5
- import math
6
5
  import networkx as nx
7
6
  import numpy as np
8
7
  from numba import njit, prange
9
- import os
10
8
  import random
11
9
  from scipy.sparse import lil_matrix, csr_matrix
12
10
 
@@ -280,7 +278,7 @@ def spin_glass_solver_sparse(
280
278
 
281
279
  if reheat_round < reheat_tries:
282
280
  num_to_flip = int(np.round(np.log2(n_qubits)))
283
- bits_to_flip = random.sample(list(range(n_qubits)), num_to_flip)
281
+ bits_to_flip = random.sample(range(n_qubits), num_to_flip)
284
282
  for bit in bits_to_flip:
285
283
  reheat_theta[bit] = not reheat_theta[bit]
286
284
  reheat_min_energy = compute_energy(reheat_theta, G_m.data, G_m.indptr, G_m.indices, n_qubits) if is_spin_glass else -evaluate_cut_edges(reheat_theta, G_m.data, G_m.indptr, G_m.indices, n_qubits)
@@ -5,7 +5,6 @@ import itertools
5
5
  import networkx as nx
6
6
  import numpy as np
7
7
  from numba import njit, prange
8
- import os
9
8
  import random
10
9
 
11
10
 
@@ -170,7 +169,7 @@ def spin_glass_solver_streaming(
170
169
 
171
170
  if reheat_round < reheat_tries:
172
171
  num_to_flip = int(np.round(np.log2(n_qubits)))
173
- bits_to_flip = random.sample(list(range(n_qubits)), num_to_flip)
172
+ bits_to_flip = random.sample(range(n_qubits), num_to_flip)
174
173
  for bit in bits_to_flip:
175
174
  reheat_theta[bit] = not reheat_theta[bit]
176
175
  reheat_min_energy = compute_energy(reheat_theta, G_func, nodes, n_qubits) if is_spin_glass else -evaluate_cut_edges(reheat_theta, G_func, nodes, n_qubits)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 8.14.7
3
+ Version: 8.14.9
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.7',
10
+ version='8.14.9',
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