PyQrackIsing 9.10.1__tar.gz → 9.11.1__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 (30) hide show
  1. {pyqrackising-9.10.1/pyqrackising.egg-info → pyqrackising-9.11.1}/PKG-INFO +1 -1
  2. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/README.md +1 -1
  3. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/pyproject.toml +1 -1
  4. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/pyqrackising/__init__.py +1 -1
  5. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/pyqrackising/convert_tensor_network_to_tsp.py +1 -5
  6. pyqrackising-9.11.1/pyqrackising/generate_tfim_samples.py +236 -0
  7. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/pyqrackising/maxcut_tfim.py +66 -12
  8. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/pyqrackising/maxcut_tfim_sparse.py +90 -14
  9. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/pyqrackising/maxcut_tfim_streaming.py +18 -5
  10. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/pyqrackising/maxcut_tfim_util.py +72 -37
  11. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/pyqrackising/otoc.py +20 -10
  12. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/pyqrackising/spin_glass_solver.py +125 -22
  13. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/pyqrackising/spin_glass_solver_sparse.py +184 -27
  14. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/pyqrackising/spin_glass_solver_streaming.py +25 -3
  15. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/pyqrackising/tsp.py +371 -183
  16. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/pyqrackising/tsp_maxcut.py +7 -7
  17. {pyqrackising-9.10.1 → pyqrackising-9.11.1/pyqrackising.egg-info}/PKG-INFO +1 -1
  18. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/setup.py +8 -8
  19. pyqrackising-9.10.1/pyqrackising/generate_tfim_samples.py +0 -195
  20. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/LICENSE.md +0 -0
  21. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/MANIFEST.in +0 -0
  22. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/pyqrackising/kernels.cl +0 -0
  23. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/pyqrackising/tfim_magnetization.py +0 -0
  24. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/pyqrackising/tfim_square_magnetization.py +0 -0
  25. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/pyqrackising.egg-info/SOURCES.txt +0 -0
  26. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/pyqrackising.egg-info/dependency_links.txt +0 -0
  27. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/pyqrackising.egg-info/not-zip-safe +0 -0
  28. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/pyqrackising.egg-info/requires.txt +0 -0
  29. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/pyqrackising.egg-info/top_level.txt +0 -0
  30. {pyqrackising-9.10.1 → pyqrackising-9.11.1}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 9.10.1
3
+ Version: 9.11.1
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
@@ -3,7 +3,7 @@ Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising
3
3
 
4
4
  (It's "the **Ising** on top.")
5
5
 
6
- [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.17635216.svg)](https://doi.org/10.5281/zenodo.17635216) [![PyPI Downloads](https://static.pepy.tech/badge/pyqrackising)](https://pepy.tech/projects/pyqrackising)
6
+ [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.18706638.svg)](https://doi.org/10.5281/zenodo.18706638) [![PyPI Downloads](https://static.pepy.tech/badge/pyqrackising)](https://pepy.tech/projects/pyqrackising)
7
7
 
8
8
  ## Copyright and license
9
9
  (c) Daniel Strano and the Qrack contributors 2025. All rights reserved.
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
7
7
 
8
8
  [project]
9
9
  name = "pyqrackising"
10
- version = "9.10.1"
10
+ version = "9.11.1"
11
11
  requires-python = ">=3.8"
12
12
  description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
13
13
  readme = {file = "README.txt", content-type = "text/markdown"}
@@ -1,4 +1,4 @@
1
- from .generate_tfim_samples import generate_tfim_samples, get_tfim_hamming_distribution
1
+ from .generate_tfim_samples import generate_tfim_samples, get_tfim_hamming_distribution, generate_fermi_hubbard_samples
2
2
  from .tfim_magnetization import tfim_magnetization
3
3
  from .tfim_square_magnetization import tfim_square_magnetization
4
4
  from .otoc import generate_otoc_samples, get_otoc_hamming_distribution
@@ -68,7 +68,6 @@ def convert_tensor_network_to_tsp(tensors, index_dims, dtype=np.float32):
68
68
  return dist_matrix, tensor_ids
69
69
 
70
70
 
71
-
72
71
  def convert_quimb_tree_to_tsp(tn, dtype=np.float32):
73
72
  """
74
73
  Converts a quimb TensorNetwork to a TSP distance matrix for contraction optimization.
@@ -86,10 +85,7 @@ def convert_quimb_tree_to_tsp(tn, dtype=np.float32):
86
85
  for tensor in tn.tensors:
87
86
  # Use a unique tag or fallback to name
88
87
  tensor_id = frozenset(tensor.tags) if tensor.tags else str(tensor.name)
89
- tensors.append({
90
- "id": tensor_id,
91
- "indices": set(tensor.inds)
92
- })
88
+ tensors.append({"id": tensor_id, "indices": set(tensor.inds)})
93
89
 
94
90
  for ind, dim in zip(tensor.inds, tensor.shape):
95
91
  index_dims[ind] = dim # capture dimension size
@@ -0,0 +1,236 @@
1
+ from .maxcut_tfim_util import probability_by_hamming_weight, sample_mag, opencl_context
2
+ import itertools
3
+ import math
4
+ import random
5
+ import sys
6
+ import numpy as np
7
+ from numba import njit
8
+
9
+ from collections import Counter
10
+
11
+
12
+ epsilon = opencl_context.epsilon
13
+
14
+
15
+ # Fixed-Hamming-weight Kawasaki swaps with acceptance probability, by Elara
16
+ def random_fixed_hamming_state(n_qubits, h):
17
+ bits = np.zeros(n_qubits, dtype=np.bool_)
18
+ bits[:h] = True
19
+ np.random.shuffle(bits)
20
+ state = 0
21
+ for b in bits:
22
+ state <<= 1
23
+ if b:
24
+ state |= 1
25
+
26
+ return state
27
+
28
+
29
+ def build_neighbors(n_rows, n_cols):
30
+ L = n_rows * n_cols
31
+ neighbors = [[] for _ in range(L)]
32
+
33
+ for i in range(n_rows):
34
+ for j in range(n_cols):
35
+ idx = i * n_cols + j
36
+
37
+ right = i * n_cols + ((j + 1) % n_cols)
38
+ left = i * n_cols + ((j - 1) % n_cols)
39
+ down = ((i + 1) % n_rows) * n_cols + j
40
+ up = ((i - 1) % n_rows) * n_cols + j
41
+
42
+ neighbors[idx] = [right, left, down, up]
43
+
44
+ return neighbors
45
+
46
+
47
+ def count_like_edges(state, neighbors):
48
+ like = 0
49
+ visited = set()
50
+
51
+ for i, nbrs in enumerate(neighbors):
52
+ si = (state >> i) & 1
53
+ for j in nbrs:
54
+ if (j, i) in visited:
55
+ continue
56
+ sj = (state >> j) & 1
57
+ if si == sj:
58
+ like += 1
59
+ visited.add((i, j))
60
+
61
+ return like
62
+
63
+
64
+ def delta_like_edges(state, i, j, neighbors):
65
+ si = (state >> i) & 1
66
+ sj = (state >> j) & 1
67
+
68
+ delta = 0
69
+
70
+ for idx in [i, j]:
71
+ s_old = (state >> idx) & 1
72
+
73
+ for nbr in neighbors[idx]:
74
+ if nbr == i or nbr == j:
75
+ continue
76
+
77
+ s_nbr = (state >> nbr) & 1
78
+
79
+ old_like = s_old == s_nbr
80
+ new_spin = sj if idx == i else si
81
+ new_like = new_spin == s_nbr
82
+
83
+ delta += int(new_like) - int(old_like)
84
+
85
+ # handle edge between i and j if neighbors
86
+ if j in neighbors[i]:
87
+ old_like = si == sj
88
+ new_like = sj == si
89
+ delta += int(new_like) - int(old_like)
90
+
91
+ return delta
92
+
93
+
94
+ def sample_fixed_hamming_weight(h_weight, count, n_rows, n_cols, burnin=10):
95
+ if count == 0:
96
+ return []
97
+
98
+ n_qubits = n_rows * n_cols
99
+ neighbors = build_neighbors(n_rows, n_cols)
100
+
101
+ like_edges = 0
102
+ while like_edges == 0:
103
+ state = random_fixed_hamming_state(n_qubits, h_weight)
104
+ like_edges = count_like_edges(state, neighbors)
105
+
106
+ samples = []
107
+
108
+ total_steps = burnin * count + count
109
+
110
+ for step in range(total_steps):
111
+ # choose random 1-bit and 0-bit
112
+ ones = [i for i in range(n_qubits) if (state >> i) & 1]
113
+ zeros = [i for i in range(n_qubits) if not (state >> i) & 1]
114
+
115
+ i = random.choice(ones)
116
+ j = random.choice(zeros)
117
+
118
+ delta = delta_like_edges(state, i, j, neighbors)
119
+ new_like = like_edges + delta
120
+
121
+ if new_like > 0:
122
+ accept_prob = new_like / like_edges
123
+ if np.random.random() < accept_prob:
124
+ state ^= 1 << i
125
+ state ^= 1 << j
126
+ like_edges = new_like
127
+
128
+ if step >= burnin * count:
129
+ samples.append(state)
130
+
131
+ return samples
132
+
133
+
134
+ @njit
135
+ def factor_width(width, is_transpose=False):
136
+ col_len = math.floor(math.sqrt(width))
137
+ while ((width // col_len) * col_len) != width:
138
+ col_len -= 1
139
+ row_len = width // col_len
140
+
141
+ return (col_len, row_len) if is_transpose else (row_len, col_len)
142
+
143
+
144
+ @njit
145
+ def comb(n, k):
146
+ if (k < 0) or (k > n):
147
+ return 0
148
+ if (k == 0) or (k == n):
149
+ return 1
150
+ res = 1
151
+ for i in range(1, k + 1):
152
+ res = res * (n - k + i) // i
153
+ return res
154
+
155
+
156
+ @njit
157
+ def fix_cdf(hamming_prob):
158
+ tot_prob = 0.0
159
+ n_bias = len(hamming_prob)
160
+ cum_prob = np.empty(n_bias, dtype=np.float64)
161
+ for i in range(n_bias):
162
+ tot_prob += hamming_prob[i]
163
+ cum_prob[i] = tot_prob
164
+ cum_prob[-1] = 1.0
165
+
166
+ return cum_prob
167
+
168
+
169
+ @njit
170
+ def get_tfim_hamming_distribution(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, omega=1.5 * np.pi):
171
+ if abs(t) <= epsilon:
172
+ p = (1.0 - np.cos(theta)) / 2.0
173
+ bias = np.empty(n_qubits + 1, dtype=np.float64)
174
+ for k in range(n_qubits + 1):
175
+ bias[k] = comb(n_qubits, k) * (p**k) * ((1.0 - p) ** (n_qubits - k))
176
+
177
+ return bias / bias.sum()
178
+
179
+ if abs(h) <= epsilon:
180
+ bias = np.empty(n_qubits + 1, dtype=np.float64)
181
+ if J > 0:
182
+ bias[-1] = 1.0
183
+ else:
184
+ bias[0] = 1.0
185
+ return bias
186
+
187
+ bias = probability_by_hamming_weight(J, h, z, theta, t, n_qubits + 1, normalized=True, omega=omega)
188
+
189
+ return bias / bias.sum()
190
+
191
+
192
+ def generate_tfim_samples(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, shots=100):
193
+ samples = []
194
+
195
+ if abs(t) <= epsilon:
196
+ prob = (1.0 - np.cos(theta)) / 2.0
197
+ for s in range(shots):
198
+ sample = 0
199
+ for q in range(n_qubits):
200
+ sample <<= 1
201
+ if np.random.random() < prob:
202
+ sample |= 1
203
+ samples.append(sample)
204
+
205
+ return samples
206
+
207
+ n_rows, n_cols = factor_width(n_qubits)
208
+
209
+ # First dimension: Hamming weight
210
+ bias = get_tfim_hamming_distribution(J=J, h=h, z=z, theta=theta, t=t, n_qubits=n_qubits)
211
+ counts = np.random.multinomial(shots, bias)
212
+
213
+ samples += [0] * counts[0]
214
+ samples += [(1 << n_qubits) - 1] * counts[-1]
215
+ if n_qubits > 1:
216
+ samples += [int(1 << np.random.randint(n_qubits)) for _ in range(counts[1])]
217
+ if n_qubits > 2:
218
+ mask = (1 << n_qubits) - 1
219
+ samples += [(mask ^ int(1 << np.random.randint(n_qubits))) for _ in range(counts[-2])]
220
+
221
+ # Second dimension: magnetic localization
222
+ for h_weight in range(2, len(bias) - 2):
223
+ samples += sample_fixed_hamming_weight(h_weight, counts[h_weight], n_rows, n_cols)
224
+
225
+ np.random.shuffle(samples)
226
+
227
+ return samples
228
+
229
+
230
+ def generate_fermi_hubbard_samples(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, shots=100, omega=1.5 * np.pi):
231
+ shots_x, shots_y, shots_z = np.random.multinomial(shots, [1 / 3, 1 / 3, 1 / 3])
232
+ return (
233
+ generate_tfim_samples(J=J, h=h, z=z, theta=theta, t=t, n_qubits=n_qubits, shots=shots_z)
234
+ + generate_tfim_samples(J=-h, h=-J, z=z, theta=theta + np.pi / 2, t=t, n_qubits=n_qubits, shots=shots_x)
235
+ + generate_tfim_samples(J=J, h=h, z=z, theta=theta + np.pi / 2, t=t, n_qubits=n_qubits, shots=shots_y)
236
+ )
@@ -3,7 +3,23 @@ import numpy as np
3
3
  from numba import njit, prange
4
4
  import os
5
5
 
6
- from .maxcut_tfim_util import compute_cut, compute_energy, compute_cut_diff_between, convert_bool_to_uint, get_cut, get_cut_base, heuristic_threshold, init_thresholds, make_G_m_buf, make_theta_buf, maxcut_hamming_cdf, opencl_context, sample_mag, setup_opencl, bit_pick
6
+ from .maxcut_tfim_util import (
7
+ compute_cut,
8
+ compute_energy,
9
+ compute_cut_diff_between,
10
+ convert_bool_to_uint,
11
+ get_cut,
12
+ get_cut_base,
13
+ heuristic_threshold,
14
+ init_thresholds,
15
+ make_G_m_buf,
16
+ make_theta_buf,
17
+ maxcut_hamming_cdf,
18
+ opencl_context,
19
+ sample_mag,
20
+ setup_opencl,
21
+ bit_pick,
22
+ )
7
23
 
8
24
  IS_OPENCL_AVAILABLE = True
9
25
  try:
@@ -41,7 +57,7 @@ def local_repulsion_choice(G_m, repulsion_base, n, m, s):
41
57
  - After choosing a node, its neighbors' probabilities are further reduced
42
58
  """
43
59
 
44
- used = np.zeros(n, dtype=np.bool_) # False = available, True = used
60
+ used = np.zeros(n, dtype=np.bool_) # False = available, True = used
45
61
 
46
62
  # First bit:
47
63
  node = s % n
@@ -133,7 +149,17 @@ def shot_loop(G_m, thresholds, repulsion_base, n, shots, solutions):
133
149
  solutions[s] = local_repulsion_choice(G_m, repulsion_base, n, m, s)
134
150
 
135
151
 
136
- def sample_for_opencl(G_m, G_m_buf, shots, thresholds, repulsion_base, is_spin_glass, is_segmented, segment_size, theta_segment_size):
152
+ def sample_for_opencl(
153
+ G_m,
154
+ G_m_buf,
155
+ shots,
156
+ thresholds,
157
+ repulsion_base,
158
+ is_spin_glass,
159
+ is_segmented,
160
+ segment_size,
161
+ theta_segment_size,
162
+ ):
137
163
  shots = ((max(1, shots >> 1) + 31) >> 5) << 5
138
164
  n = len(G_m)
139
165
 
@@ -142,7 +168,11 @@ def sample_for_opencl(G_m, G_m_buf, shots, thresholds, repulsion_base, is_spin_g
142
168
  best_solution = np.zeros(n, dtype=np.bool_)
143
169
  best_energy = compute_energy(best_solution, G_m, n) if is_spin_glass else 0.0
144
170
 
145
- opencl_args = setup_opencl(shots, shots, np.array([n, shots, is_spin_glass, segment_size, theta_segment_size], dtype=np.int32))
171
+ opencl_args = setup_opencl(
172
+ shots,
173
+ shots,
174
+ np.array([n, shots, is_spin_glass, segment_size, theta_segment_size], dtype=np.int32),
175
+ )
146
176
 
147
177
  improved = True
148
178
  while improved:
@@ -183,7 +213,21 @@ def init_J_and_z(G_m, repulsion_base):
183
213
  return J_eff, degrees
184
214
 
185
215
 
186
- def run_cut_opencl(best_energy, samples, G_m_buf, is_segmented, local_size, global_size, args_buf, local_energy_buf, local_index_buf, max_energy_host, max_index_host, max_energy_buf, max_index_buf):
216
+ def run_cut_opencl(
217
+ best_energy,
218
+ samples,
219
+ G_m_buf,
220
+ is_segmented,
221
+ local_size,
222
+ global_size,
223
+ args_buf,
224
+ local_energy_buf,
225
+ local_index_buf,
226
+ max_energy_host,
227
+ max_index_host,
228
+ max_energy_buf,
229
+ max_index_buf,
230
+ ):
187
231
  queue = opencl_context.queue
188
232
  calculate_cut_kernel = opencl_context.calculate_cut_segmented_kernel if is_segmented else opencl_context.calculate_cut_kernel
189
233
 
@@ -209,7 +253,7 @@ def run_cut_opencl(best_energy, samples, G_m_buf, is_segmented, local_size, glob
209
253
  max_energy_buf,
210
254
  max_index_buf,
211
255
  local_energy_buf,
212
- local_index_buf
256
+ local_index_buf,
213
257
  )
214
258
  else:
215
259
  calculate_cut_kernel.set_args(
@@ -219,7 +263,7 @@ def run_cut_opencl(best_energy, samples, G_m_buf, is_segmented, local_size, glob
219
263
  max_energy_buf,
220
264
  max_index_buf,
221
265
  local_energy_buf,
222
- local_index_buf
266
+ local_index_buf,
223
267
  )
224
268
 
225
269
  cl.enqueue_nd_range_kernel(queue, calculate_cut_kernel, (global_size,), (local_size,))
@@ -308,13 +352,13 @@ def maxcut_tfim(
308
352
  anneal_h=None,
309
353
  repulsion_base=None,
310
354
  is_maxcut_gpu=True,
311
- is_nested=False
355
+ is_nested=False,
312
356
  ):
313
357
  nodes = None
314
358
  G_m = None
315
359
  if isinstance(G, nx.Graph):
316
360
  nodes = list(G.nodes())
317
- G_m = nx.to_numpy_array(G, weight='weight', nonedge=0.0, dtype=dtype)
361
+ G_m = nx.to_numpy_array(G, weight="weight", nonedge=0.0, dtype=dtype)
318
362
  else:
319
363
  nodes = list(range(len(G)))
320
364
  G_m = G
@@ -327,7 +371,7 @@ def maxcut_tfim(
327
371
 
328
372
  if best_value < 0.0:
329
373
  # Best cut is trivial partition, all/empty
330
- return '0' * n_qubits, 0.0, (nodes, [])
374
+ return "0" * n_qubits, 0.0, (nodes, [])
331
375
 
332
376
  return bit_string, best_value, (l, r)
333
377
 
@@ -358,7 +402,17 @@ def maxcut_tfim(
358
402
  theta_segment_size = (((n_qubits + 31) >> 5) * (((shots + wgs - 1) // wgs) * wgs) + 3) >> 2
359
403
  is_segmented = ((G_m.nbytes << 1) > opencl_context.max_alloc) or ((theta_segment_size << 3) > opencl_context.max_alloc)
360
404
  G_m_buf = make_G_m_buf(G_m, is_segmented, segment_size)
361
- best_solution, best_value = sample_for_opencl(G_m, G_m_buf, shots, cum_prob, repulsion_base, is_spin_glass, is_segmented, segment_size, theta_segment_size)
405
+ best_solution, best_value = sample_for_opencl(
406
+ G_m,
407
+ G_m_buf,
408
+ shots,
409
+ cum_prob,
410
+ repulsion_base,
411
+ is_spin_glass,
412
+ is_segmented,
413
+ segment_size,
414
+ theta_segment_size,
415
+ )
362
416
  else:
363
417
  thread_count = os.cpu_count() ** 2
364
418
  best_solution, best_value = sample_measurement(G_m, shots, thread_count, cum_prob, repulsion_base, is_spin_glass)
@@ -367,6 +421,6 @@ def maxcut_tfim(
367
421
 
368
422
  if best_value < 0.0:
369
423
  # Best cut is trivial partition, all/empty
370
- return '0' * n_qubits, 0.0, (nodes, [])
424
+ return "0" * n_qubits, 0.0, (nodes, [])
371
425
 
372
426
  return bit_string, best_value, (l, r)
@@ -3,7 +3,24 @@ import numpy as np
3
3
  from numba import njit, prange
4
4
  import os
5
5
 
6
- from .maxcut_tfim_util import binary_search, compute_cut_sparse, compute_energy_sparse, convert_bool_to_uint, get_cut, get_cut_base, heuristic_threshold_sparse, init_thresholds, make_G_m_csr_buf, make_theta_buf, maxcut_hamming_cdf, opencl_context, sample_mag, setup_opencl, bit_pick, to_scipy_sparse_upper_triangular
6
+ from .maxcut_tfim_util import (
7
+ binary_search,
8
+ compute_cut_sparse,
9
+ compute_energy_sparse,
10
+ convert_bool_to_uint,
11
+ get_cut,
12
+ get_cut_base,
13
+ heuristic_threshold_sparse,
14
+ init_thresholds,
15
+ make_G_m_csr_buf,
16
+ make_theta_buf,
17
+ maxcut_hamming_cdf,
18
+ opencl_context,
19
+ sample_mag,
20
+ setup_opencl,
21
+ bit_pick,
22
+ to_scipy_sparse_upper_triangular,
23
+ )
7
24
 
8
25
  IS_OPENCL_AVAILABLE = True
9
26
  try:
@@ -51,7 +68,7 @@ def local_repulsion_choice(G_data, G_rows, G_cols, repulsion_base, n, m, s):
51
68
  - After choosing a node, its neighbors' probabilities are further reduced
52
69
  """
53
70
 
54
- used = np.zeros(n, dtype=np.bool_) # False = available, True = used
71
+ used = np.zeros(n, dtype=np.bool_) # False = available, True = used
55
72
 
56
73
  # First bit:
57
74
  node = s % n
@@ -140,7 +157,21 @@ def shot_loop(G_data, G_rows, G_cols, thresholds, repulsion_base, n, shots, solu
140
157
  solutions[s] = local_repulsion_choice(G_data, G_rows, G_cols, repulsion_base, n, m, s)
141
158
 
142
159
 
143
- 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):
160
+ def sample_for_opencl(
161
+ G_data,
162
+ G_rows,
163
+ G_cols,
164
+ G_data_buf,
165
+ G_rows_buf,
166
+ G_cols_buf,
167
+ shots,
168
+ thresholds,
169
+ repulsion_base,
170
+ is_spin_glass,
171
+ is_segmented,
172
+ segment_size,
173
+ theta_segment_size,
174
+ ):
144
175
  shots = ((max(1, shots >> 1) + 31) >> 5) << 5
145
176
  n = G_rows.shape[0] - 1
146
177
 
@@ -149,7 +180,11 @@ def sample_for_opencl(G_data, G_rows, G_cols, G_data_buf, G_rows_buf, G_cols_buf
149
180
  best_solution = np.zeros(n, dtype=np.bool_)
150
181
  best_energy = compute_energy(best_solution, G_m, n) if is_spin_glass else 0.0
151
182
 
152
- opencl_args = setup_opencl(shots, shots, np.array([n, shots, is_spin_glass, segment_size, theta_segment_size], dtype=np.int32))
183
+ opencl_args = setup_opencl(
184
+ shots,
185
+ shots,
186
+ np.array([n, shots, is_spin_glass, segment_size, theta_segment_size], dtype=np.int32),
187
+ )
153
188
 
154
189
  improved = True
155
190
  while improved:
@@ -162,7 +197,7 @@ def sample_for_opencl(G_data, G_rows, G_cols, G_data_buf, G_rows_buf, G_cols_buf
162
197
  improved = True
163
198
 
164
199
  if is_spin_glass:
165
- best_energy = compute_cut_sparse(best_solution, G_data, G_rows, G_cols, n)
200
+ best_energy = compute_cut_sparse(best_solution, G_data, G_rows, G_cols, n)
166
201
 
167
202
  return best_solution, float(best_energy)
168
203
 
@@ -190,7 +225,23 @@ def init_J_and_z(G_m, repulsion_base):
190
225
  return J_eff, degrees
191
226
 
192
227
 
193
- def run_cut_opencl(best_energy, samples, G_data_buf, G_rows_buf, G_cols_buf, is_segmented, local_size, global_size, args_buf, local_energy_buf, local_index_buf, max_energy_host, max_index_host, max_energy_buf, max_index_buf):
228
+ def run_cut_opencl(
229
+ best_energy,
230
+ samples,
231
+ G_data_buf,
232
+ G_rows_buf,
233
+ G_cols_buf,
234
+ is_segmented,
235
+ local_size,
236
+ global_size,
237
+ args_buf,
238
+ local_energy_buf,
239
+ local_index_buf,
240
+ max_energy_host,
241
+ max_index_host,
242
+ max_energy_buf,
243
+ max_index_buf,
244
+ ):
194
245
  queue = opencl_context.queue
195
246
  calculate_cut_kernel = opencl_context.calculate_cut_sparse_segmented_kernel if is_segmented else opencl_context.calculate_cut_sparse_kernel
196
247
 
@@ -218,7 +269,7 @@ def run_cut_opencl(best_energy, samples, G_data_buf, G_rows_buf, G_cols_buf, is_
218
269
  max_energy_buf,
219
270
  max_index_buf,
220
271
  local_energy_buf,
221
- local_index_buf
272
+ local_index_buf,
222
273
  )
223
274
  else:
224
275
  calculate_cut_kernel.set_args(
@@ -230,7 +281,7 @@ def run_cut_opencl(best_energy, samples, G_data_buf, G_rows_buf, G_cols_buf, is_
230
281
  max_energy_buf,
231
282
  max_index_buf,
232
283
  local_energy_buf,
233
- local_index_buf
284
+ local_index_buf,
234
285
  )
235
286
 
236
287
  cl.enqueue_nd_range_kernel(queue, calculate_cut_kernel, (global_size,), (local_size,))
@@ -329,7 +380,7 @@ def maxcut_tfim_sparse(
329
380
  anneal_h=None,
330
381
  repulsion_base=None,
331
382
  is_maxcut_gpu=True,
332
- is_nested=False
383
+ is_nested=False,
333
384
  ):
334
385
  wgs = opencl_context.work_group_size
335
386
  nodes = None
@@ -345,12 +396,14 @@ def maxcut_tfim_sparse(
345
396
  G_m = G
346
397
 
347
398
  if n_qubits < heuristic_threshold_sparse:
348
- best_solution, best_value = exact_spin_glass(G_m.data, G_m.indptr, G_m.indices) if is_spin_glass else exact_maxcut(G_m.data, G_m.indptr, G_m.indices)
399
+ best_solution, best_value = (
400
+ exact_spin_glass(G_m.data, G_m.indptr, G_m.indices) if is_spin_glass else exact_maxcut(G_m.data, G_m.indptr, G_m.indices)
401
+ )
349
402
  bit_string, l, r = get_cut(best_solution, nodes, n_qubits)
350
403
 
351
404
  if best_value < 0.0:
352
405
  # Best cut is trivial partition, all/empty
353
- return '0' * n_qubits, 0.0, (nodes, [])
406
+ return "0" * n_qubits, 0.0, (nodes, [])
354
407
 
355
408
  return bit_string, best_value, (l, r)
356
409
 
@@ -383,15 +436,38 @@ def maxcut_tfim_sparse(
383
436
  theta_segment_size = (((n_qubits + 31) >> 5) * (((shots + wgs - 1) // wgs) * wgs) + 3) >> 2
384
437
  is_segmented = (G_m.data.nbytes << 1) > opencl_context.max_alloc or ((theta_segment_size << 3) > opencl_context.max_alloc)
385
438
  G_data_buf, G_rows_buf, G_cols_buf = make_G_m_csr_buf(G_m, is_segmented, segment_size)
386
- 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, cum_prob, repulsion_base, is_spin_glass, is_segmented, segment_size, theta_segment_size)
439
+ best_solution, best_value = sample_for_opencl(
440
+ G_m.data,
441
+ G_m.indptr,
442
+ G_m.indices,
443
+ G_data_buf,
444
+ G_rows_buf,
445
+ G_cols_buf,
446
+ shots,
447
+ cum_prob,
448
+ repulsion_base,
449
+ is_spin_glass,
450
+ is_segmented,
451
+ segment_size,
452
+ theta_segment_size,
453
+ )
387
454
  else:
388
455
  thread_count = os.cpu_count() ** 2
389
- best_solution, best_value = sample_measurement(G_m.data, G_m.indptr, G_m.indices, shots, thread_count, cum_prob, repulsion_base, is_spin_glass)
456
+ best_solution, best_value = sample_measurement(
457
+ G_m.data,
458
+ G_m.indptr,
459
+ G_m.indices,
460
+ shots,
461
+ thread_count,
462
+ cum_prob,
463
+ repulsion_base,
464
+ is_spin_glass,
465
+ )
390
466
 
391
467
  bit_string, l, r = get_cut(best_solution, nodes, n_qubits)
392
468
 
393
469
  if best_value < 0.0:
394
470
  # Best cut is trivial partition, all/empty
395
- return '0' * n_qubits, 0.0, (nodes, [])
471
+ return "0" * n_qubits, 0.0, (nodes, [])
396
472
 
397
473
  return bit_string, best_value, (l, r)
@@ -3,7 +3,19 @@ import numpy as np
3
3
  from numba import njit, prange
4
4
  import os
5
5
 
6
- from .maxcut_tfim_util import compute_cut_streaming, compute_energy_streaming, compute_cut_diff_between_streaming, get_cut, get_cut_base, heuristic_threshold, init_thresholds, maxcut_hamming_cdf, opencl_context, sample_mag, bit_pick
6
+ from .maxcut_tfim_util import (
7
+ compute_cut_streaming,
8
+ compute_energy_streaming,
9
+ compute_cut_diff_between_streaming,
10
+ get_cut,
11
+ get_cut_base,
12
+ heuristic_threshold,
13
+ init_thresholds,
14
+ maxcut_hamming_cdf,
15
+ opencl_context,
16
+ sample_mag,
17
+ bit_pick,
18
+ )
7
19
 
8
20
 
9
21
  epsilon = opencl_context.epsilon
@@ -34,7 +46,7 @@ def local_repulsion_choice(G_func, nodes, repulsion_base, n, m, s):
34
46
  - After choosing a node, its neighbors' probabilities are further reduced
35
47
  """
36
48
 
37
- used = np.zeros(n, dtype=np.bool_) # False = available, True = used
49
+ used = np.zeros(n, dtype=np.bool_) # False = available, True = used
38
50
 
39
51
  # First bit:
40
52
  node = s % n
@@ -137,6 +149,7 @@ def init_J_and_z(G_func, nodes, G_min, repulsion_base):
137
149
 
138
150
  return J_eff, degrees
139
151
 
152
+
140
153
  @njit
141
154
  def find_G_min(G_func, nodes, n_nodes):
142
155
  G_min = float("inf")
@@ -211,7 +224,7 @@ def maxcut_tfim_streaming(
211
224
  is_spin_glass=False,
212
225
  anneal_t=None,
213
226
  anneal_h=None,
214
- repulsion_base=None
227
+ repulsion_base=None,
215
228
  ):
216
229
  wgs = opencl_context.work_group_size
217
230
  n_qubits = len(nodes)
@@ -222,7 +235,7 @@ def maxcut_tfim_streaming(
222
235
 
223
236
  if best_value < 0.0:
224
237
  # Best cut is trivial partition, all/empty
225
- return '0' * n_qubits, 0.0, (nodes, [])
238
+ return "0" * n_qubits, 0.0, (nodes, [])
226
239
 
227
240
  return bit_string, best_value, (l, r)
228
241
 
@@ -258,6 +271,6 @@ def maxcut_tfim_streaming(
258
271
 
259
272
  if best_value < 0.0:
260
273
  # Best cut is trivial partition, all/empty
261
- return '0' * n_qubits, 0.0, (nodes, [])
274
+ return "0" * n_qubits, 0.0, (nodes, [])
262
275
 
263
276
  return bit_string, best_value, (l, r)