PyQrackIsing 5.0.2__tar.gz → 5.1.0__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 (26) hide show
  1. {pyqrackising-5.0.2/pyqrackising.egg-info → pyqrackising-5.1.0}/PKG-INFO +1 -1
  2. {pyqrackising-5.0.2 → pyqrackising-5.1.0}/README.md +25 -0
  3. {pyqrackising-5.0.2 → pyqrackising-5.1.0}/pyproject.toml +1 -1
  4. {pyqrackising-5.0.2 → pyqrackising-5.1.0}/pyqrackising/__init__.py +2 -0
  5. {pyqrackising-5.0.2 → pyqrackising-5.1.0}/pyqrackising/maxcut_tfim_sparse.py +1 -1
  6. pyqrackising-5.1.0/pyqrackising/maxcut_tfim_streaming.py +363 -0
  7. pyqrackising-5.1.0/pyqrackising/spin_glass_solver_streaming.py +138 -0
  8. {pyqrackising-5.0.2 → pyqrackising-5.1.0/pyqrackising.egg-info}/PKG-INFO +1 -1
  9. {pyqrackising-5.0.2 → pyqrackising-5.1.0}/pyqrackising.egg-info/SOURCES.txt +2 -0
  10. {pyqrackising-5.0.2 → pyqrackising-5.1.0}/setup.py +1 -1
  11. {pyqrackising-5.0.2 → pyqrackising-5.1.0}/CMakeLists.txt +0 -0
  12. {pyqrackising-5.0.2 → pyqrackising-5.1.0}/LICENSE.md +0 -0
  13. {pyqrackising-5.0.2 → pyqrackising-5.1.0}/MANIFEST.in +0 -0
  14. {pyqrackising-5.0.2 → pyqrackising-5.1.0}/pyqrackising/_pyqrack_ising.cpp +0 -0
  15. {pyqrackising-5.0.2 → pyqrackising-5.1.0}/pyqrackising/generate_tfim_samples.py +0 -0
  16. {pyqrackising-5.0.2 → pyqrackising-5.1.0}/pyqrackising/kernels.cl +0 -0
  17. {pyqrackising-5.0.2 → pyqrackising-5.1.0}/pyqrackising/maxcut_tfim.py +0 -0
  18. {pyqrackising-5.0.2 → pyqrackising-5.1.0}/pyqrackising/spin_glass_solver.py +0 -0
  19. {pyqrackising-5.0.2 → pyqrackising-5.1.0}/pyqrackising/spin_glass_solver_sparse.py +0 -0
  20. {pyqrackising-5.0.2 → pyqrackising-5.1.0}/pyqrackising/tfim_magnetization.py +0 -0
  21. {pyqrackising-5.0.2 → pyqrackising-5.1.0}/pyqrackising/tfim_square_magnetization.py +0 -0
  22. {pyqrackising-5.0.2 → pyqrackising-5.1.0}/pyqrackising/tsp.py +0 -0
  23. {pyqrackising-5.0.2 → pyqrackising-5.1.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
  24. {pyqrackising-5.0.2 → pyqrackising-5.1.0}/pyqrackising.egg-info/not-zip-safe +0 -0
  25. {pyqrackising-5.0.2 → pyqrackising-5.1.0}/pyqrackising.egg-info/top_level.txt +0 -0
  26. {pyqrackising-5.0.2 → pyqrackising-5.1.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 5.0.2
3
+ Version: 5.1.0
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
@@ -114,6 +114,31 @@ print(f"Path length: {path_length}")
114
114
  ```
115
115
  We provide solvers for both the symmetric version of the TSP (i.e., the distance from "A" to "B" is considered the same as from "B" to "A") and asymmetric version (`tsp_asymmetric()`). `monte_carlo=True` switches out the MAXCUT-based heuristic for pure Monte Carlo recursive bipartitioning. `multi_start` controls how many stochastic repeats of MAXCUT are tried to select the best result, at every level of recursion. `k_neighbors` limits the count of nearest-neighbor connections considered for 3-opt.
116
116
 
117
+ If memory footprint of the graph or adjacency matrix is a concern, but the weights can be reconstructed by formula on demand, we offer `maxcut_tfim_streaming()` and `spin_glass_solver_streaming()`:
118
+ ```py
119
+ from pyqrackising import spin_glass_solver_streaming
120
+ # from pyqrackising import maxcut_tfim_streaming
121
+ from numba import njit
122
+
123
+
124
+ # This is a contrived example.
125
+ # The function must use numba NJIT.
126
+ # (In practice, even if you use other Python functionality like itertools,
127
+ # you can pre-calculate and load the data as a list through the arguments tuple.)
128
+ @njit
129
+ def G_func(node_pair, args_tuple):
130
+ i, j = min(node_pair), max(node_pair)
131
+ return ((j + 1) % (i + 1)) / args_tuple[0]
132
+
133
+
134
+ n_qubits = 64
135
+ nodes = list(range(n_qubits))
136
+ args_tuple = (n_qubits,)
137
+
138
+ solution_bit_string, cut_value, node_groups, energy = spin_glass_solver_streaming(G_func, nodes, G_func_args_tuple=args_tuple, quality=6, best_guess=None)
139
+ # solution_bit_string, cut_value, node_groups = maxcut_tfim_streaming(G_func, nodes, G_func_args_tuple=args_tuple)
140
+ ```
141
+
117
142
  ## About
118
143
  Transverse field Ising model (TFIM) is the basis of most claimed algorithmic "quantum advantage," circa 2025, with the notable exception of Shor's integer factoring algorithm.
119
144
 
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
12
12
 
13
13
  [project]
14
14
  name = "pyqrackising"
15
- version = "5.0.2"
15
+ version = "5.1.0"
16
16
  requires-python = ">=3.8"
17
17
  description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
18
18
  readme = {file = "README.txt", content-type = "text/markdown"}
@@ -3,6 +3,8 @@ from .tfim_magnetization import tfim_magnetization
3
3
  from .tfim_square_magnetization import tfim_square_magnetization
4
4
  from .maxcut_tfim import maxcut_tfim
5
5
  from .maxcut_tfim_sparse import maxcut_tfim_sparse
6
+ from .maxcut_tfim_streaming import maxcut_tfim_streaming
6
7
  from .spin_glass_solver import spin_glass_solver
7
8
  from .spin_glass_solver_sparse import spin_glass_solver_sparse
9
+ from .spin_glass_solver_streaming import spin_glass_solver_streaming
8
10
  from .tsp import tsp_symmetric, tsp_asymmetric
@@ -177,7 +177,7 @@ def local_repulsion_choice(G_cols, G_data, G_rows, max_weight, weights, n, m):
177
177
  if available[nbr]:
178
178
  weights[nbr] *= 0.5 ** (G_data[j] / max_weight) # tunable penalty factor
179
179
 
180
- for nbr in range(0, node):
180
+ for nbr in range(node):
181
181
  if not available[nbr]:
182
182
  continue
183
183
  start = G_rows[nbr]
@@ -0,0 +1,363 @@
1
+ import math
2
+ import networkx as nx
3
+ import numpy as np
4
+ import os
5
+ from numba import njit, prange
6
+
7
+
8
+ IS_OPENCL_AVAILABLE = True
9
+ ctx = None
10
+ queue = None
11
+ try:
12
+ import pyopencl as cl
13
+
14
+ # Pick a device (GPU if available)
15
+ ctx = cl.create_some_context()
16
+ queue = cl.CommandQueue(ctx)
17
+
18
+ # Load and build OpenCL kernels
19
+ kernel_src = open(os.path.dirname(os.path.abspath(__file__)) + "/kernels.cl").read()
20
+ program = cl.Program(ctx, kernel_src).build()
21
+ maxcut_hamming_cdf_kernel = program.maxcut_hamming_cdf
22
+ except ImportError:
23
+ IS_OPENCL_AVAILABLE = False
24
+
25
+
26
+ @njit
27
+ def probability_by_hamming_weight(J, h, z, theta, t, n_qubits):
28
+ bias = np.empty(n_qubits - 1, dtype=np.float64)
29
+
30
+ # critical angle
31
+ theta_c = np.arcsin(max(-1.0, min(1.0, abs(h) / (z * J))))
32
+
33
+ p = (
34
+ pow(2.0, abs(J / h) - 1.0)
35
+ * (1.0 + np.sin(theta - theta_c) * np.cos(1.5 * np.pi * J * t + theta) / (1.0 + np.sqrt(t)))
36
+ - 0.5
37
+ )
38
+
39
+ if (p * n_qubits) >= 1024:
40
+ return bias
41
+
42
+ tot_n = 1.0 + 1.0 / pow(2.0, p * n_qubits)
43
+ factor = pow(2.0, -p)
44
+ n = 1.0
45
+ for q in range(1, n_qubits):
46
+ n *= factor
47
+ bias[q - 1] = n
48
+ tot_n += n
49
+ bias /= tot_n
50
+
51
+ if J > 0.0:
52
+ return bias[::-1]
53
+
54
+ return bias
55
+
56
+
57
+ @njit(parallel=True)
58
+ def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, hamming_prob):
59
+ if n_qubits < 2:
60
+ hamming_prob.fill(0.0)
61
+ return
62
+
63
+ n_steps = 1 << quality
64
+ delta_t = 1.0 / n_steps
65
+ tot_t = 2.0 * n_steps * delta_t
66
+ h_mult = 2.0 / tot_t
67
+ n_bias = n_qubits - 1
68
+
69
+ theta = np.empty(n_qubits, dtype=np.float64)
70
+ for q in range(n_qubits):
71
+ J = J_func[q]
72
+ z = degrees[q]
73
+ theta[q] = np.arcsin(
74
+ max(
75
+ -1.0,
76
+ min(
77
+ 1.0,
78
+ (1.0 if J > 0.0 else -1.0) if np.isclose(abs(z * J), 0.0) else (abs(h_mult) / (z * J)),
79
+ ),
80
+ )
81
+ )
82
+
83
+ for qc in prange(n_qubits, n_steps * n_qubits):
84
+ step = qc // n_qubits
85
+ q = qc % n_qubits
86
+ J_eff = J_func[q]
87
+ if np.isclose(abs(J_eff), 0.0):
88
+ continue
89
+ z = degrees[q]
90
+ theta_eff = theta[q]
91
+ t = step * delta_t
92
+ tm1 = (step - 1) * delta_t
93
+ h_t = h_mult * (tot_t - t)
94
+ bias = probability_by_hamming_weight(J_eff, h_t, z, theta_eff, t, n_qubits)
95
+ last_bias = probability_by_hamming_weight(J_eff, h_t, z, theta_eff, tm1, n_qubits)
96
+ for i in range(n_bias):
97
+ hamming_prob[i] += bias[i] - last_bias[i]
98
+
99
+ tot_prob = hamming_prob.sum()
100
+ hamming_prob /= tot_prob
101
+
102
+ tot_prob = 0.0
103
+ for i in range(n_bias):
104
+ tot_prob += hamming_prob[i]
105
+ hamming_prob[i] = tot_prob
106
+ hamming_prob[-1] = 2.0
107
+
108
+
109
+ # Written by Elara (OpenAI custom GPT) and improved by Dan Strano
110
+ @njit
111
+ def local_repulsion_choice(G_func, G_func_args_tuple, nodes, max_weight, weights, n, m):
112
+ """
113
+ Pick m nodes out of n with repulsion bias:
114
+ - High-degree nodes are already less likely
115
+ - After choosing a node, its neighbors' probabilities are further reduced
116
+ adjacency_data, adjacency_rows: CSR-format sparse adjacency data
117
+ weights: float64 array of shape (n,)
118
+ """
119
+
120
+ weights = weights.copy()
121
+ chosen = np.zeros(m, dtype=np.int32) # store chosen indices
122
+ available = np.ones(n, dtype=np.bool_) # True = available, False = not
123
+ mask = np.zeros(n, dtype=np.bool_)
124
+ chosen_count = 0
125
+
126
+ for _ in range(m):
127
+ # Count available
128
+ total_w = 0.0
129
+ for i in range(n):
130
+ if available[i]:
131
+ total_w += weights[i]
132
+ if total_w <= 0:
133
+ break
134
+
135
+ # Normalize & sample
136
+ r = np.random.rand()
137
+ cum = 0.0
138
+ node = -1
139
+ for i in range(n):
140
+ if available[i]:
141
+ cum += weights[i] / total_w
142
+ if r < cum:
143
+ node = i
144
+ break
145
+
146
+ if node == -1:
147
+ continue
148
+
149
+ # Select node
150
+ chosen[chosen_count] = node
151
+ chosen_count += 1
152
+ available[node] = False
153
+ mask[node] = True
154
+
155
+ # Repulsion: penalize neighbors
156
+ for nbr in range(n):
157
+ if available[nbr]:
158
+ weights[nbr] *= 0.5 ** (G_func((nodes[node], nodes[nbr]), G_func_args_tuple) / max_weight) # tunable penalty factor
159
+
160
+ return mask
161
+
162
+
163
+ @njit
164
+ def compute_energy(sample, G_func, G_func_args_tuple, nodes, n_qubits):
165
+ energy = 0
166
+ for u in range(n_qubits):
167
+ for v in range(u + 1, n_qubits):
168
+ energy += G_func((nodes[u], nodes[v]), G_func_args_tuple) * (1 if sample[u] == sample[v] else -1)
169
+
170
+ return energy
171
+
172
+
173
+ @njit(parallel=True)
174
+ def sample_for_solution(G_func, G_func_args_tuple, nodes, max_weight, shots, thresholds, degrees_sum, J_eff, n):
175
+ weights = 1.0 / (1.0 + (2 ** -52) - J_eff)
176
+
177
+ solutions = np.empty((shots, n), dtype=np.bool_)
178
+ energies = np.empty(shots, dtype=np.float64)
179
+
180
+ for s in prange(shots):
181
+ # First dimension: Hamming weight
182
+ mag_prob = np.random.random()
183
+ m = 0
184
+ while thresholds[m] < mag_prob:
185
+ m += 1
186
+ m += 1
187
+
188
+ # Second dimension: permutation within Hamming weight
189
+ sample = local_repulsion_choice(G_func, G_func_args_tuple, nodes, max_weight, weights, n, m)
190
+
191
+ solutions[s] = sample
192
+ energies[s] = compute_energy(sample, G_func, G_func_args_tuple, nodes, n)
193
+
194
+ best_solution = solutions[np.argmin(energies)]
195
+
196
+ best_value = 0.0
197
+ for u in range(n):
198
+ for v in range(u + 1, n):
199
+ if best_solution[u] != best_solution[v]:
200
+ best_value += G_func((nodes[u], nodes[v]), G_func_args_tuple)
201
+
202
+ return best_solution, best_value
203
+
204
+
205
+ @njit
206
+ def init_J_and_z(G_func, G_func_args_tuple, nodes):
207
+ n_qubits = len(nodes)
208
+ degrees = np.empty(n_qubits, dtype=np.uint32)
209
+ J_eff = np.empty(n_qubits, dtype=np.float64)
210
+ J_max = -float("inf")
211
+ G_max = -float("inf")
212
+ for n in range(n_qubits):
213
+ degree = 0
214
+ J = 0.0
215
+ for m in range(n_qubits):
216
+ val = G_func((nodes[n], nodes[m]), G_func_args_tuple)
217
+ if val != 0.0:
218
+ degree += 1
219
+ J += val
220
+ if val > G_max:
221
+ G_max = val
222
+ J = -J / degree if degree > 0 else 0
223
+ degrees[n] = degree
224
+ J_eff[n] = J
225
+ J_abs = abs(J)
226
+ if J_abs > J_max:
227
+ J_max = J_abs
228
+ J_eff /= J_max
229
+
230
+ return J_eff, degrees, G_max
231
+
232
+
233
+ @njit
234
+ def init_thresholds(n_qubits):
235
+ n_bias = n_qubits - 1
236
+ thresholds = np.empty(n_bias, dtype=np.float64)
237
+ tot_prob = 0
238
+ p = 1.0
239
+ if n_qubits & 1:
240
+ q = n_qubits // 2
241
+ thresholds[q - 1] = p
242
+ tot_prob = p
243
+ p /= 2
244
+ for q in range(1, n_qubits // 2):
245
+ thresholds[q - 1] = p
246
+ thresholds[n_bias - q] = p
247
+ tot_prob += 2 * p
248
+ p /= 2
249
+ thresholds /= tot_prob
250
+
251
+ return thresholds
252
+
253
+
254
+ @njit
255
+ def init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees):
256
+ theta = np.empty(n_qubits, dtype=np.float64)
257
+ for q in range(n_qubits):
258
+ J = J_eff[q]
259
+ z = degrees[q]
260
+ theta[q] = np.arcsin(
261
+ max(
262
+ -1.0,
263
+ min(
264
+ 1.0,
265
+ (1.0 if J > 0.0 else -1.0) if np.isclose(abs(z * J), 0.0) else (abs(h_mult) / (z * J)),
266
+ ),
267
+ )
268
+ )
269
+
270
+ return theta
271
+
272
+
273
+ def maxcut_tfim_streaming(
274
+ G_func,
275
+ nodes,
276
+ G_func_args_tuple=None,
277
+ quality=None,
278
+ shots=None,
279
+ ):
280
+ n_qubits = len(nodes)
281
+
282
+ if n_qubits < 3:
283
+ if n_qubits == 0:
284
+ return "", 0, ([], [])
285
+
286
+ if n_qubits == 1:
287
+ return "0", 0, (nodes, [])
288
+
289
+ if n_qubits == 2:
290
+ weight = G_func((nodes[0], nodes[1]), G_func_args_tuple)
291
+ if weight < 0.0:
292
+ return "00", 0, (nodes, [])
293
+
294
+ return "01", weight, ([nodes[0]], [nodes[1]])
295
+
296
+ if quality is None:
297
+ quality = 8
298
+
299
+ if shots is None:
300
+ # Number of measurement shots
301
+ shots = n_qubits << quality
302
+
303
+ n_steps = 1 << quality
304
+ grid_size = n_steps * n_qubits
305
+
306
+ J_eff, degrees, G_max = init_J_and_z(G_func, G_func_args_tuple, nodes)
307
+ hamming_prob = init_thresholds(n_qubits)
308
+
309
+ if IS_OPENCL_AVAILABLE and grid_size >= 128:
310
+ delta_t = 1.0 / n_steps
311
+ tot_t = 2.0 * n_steps * delta_t
312
+ h_mult = 2.0 / tot_t
313
+ theta = init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees)
314
+ args = np.empty(3, dtype=np.float32)
315
+ args[0] = delta_t
316
+ args[1] = tot_t
317
+ args[2] = h_mult
318
+
319
+ # Warp size is 32:
320
+ group_size = n_qubits - 1
321
+ if group_size > 256:
322
+ group_size = 256
323
+ grid_dim = n_steps * n_qubits * group_size
324
+
325
+ # Move to GPU
326
+ mf = cl.mem_flags
327
+ args_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=args)
328
+ J_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=J_eff)
329
+ deg_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=degrees)
330
+ theta_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=theta)
331
+ ham_buf = cl.Buffer(ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=hamming_prob)
332
+
333
+ # Kernel execution
334
+ maxcut_hamming_cdf_kernel(
335
+ queue, (grid_dim,), (group_size,),
336
+ np.int32(n_qubits), deg_buf, args_buf, J_buf, theta_buf, ham_buf
337
+ )
338
+
339
+ # Fetch results
340
+ cl.enqueue_copy(queue, hamming_prob, ham_buf)
341
+
342
+ hamming_prob /= hamming_prob.sum()
343
+ tot_prob = 0.0
344
+ for i in range(n_qubits - 1):
345
+ tot_prob += hamming_prob[i]
346
+ hamming_prob[i] = tot_prob
347
+ hamming_prob[-1] = 2.0
348
+ else:
349
+ maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, hamming_prob)
350
+
351
+ best_solution, best_value = sample_for_solution(G_func, G_func_args_tuple, nodes, G_max, shots, hamming_prob, degrees.sum(), J_eff, n_qubits)
352
+
353
+ bit_string = ""
354
+ l, r = [], []
355
+ for i in range(len(best_solution)):
356
+ if best_solution[i]:
357
+ bit_string += "1"
358
+ r.append(nodes[i])
359
+ else:
360
+ bit_string += "0"
361
+ l.append(nodes[i])
362
+
363
+ return bit_string, best_value, (l, r)
@@ -0,0 +1,138 @@
1
+ from .maxcut_tfim_streaming import maxcut_tfim_streaming
2
+ import itertools
3
+ import networkx as nx
4
+ import numpy as np
5
+ from numba import njit, prange
6
+ import os
7
+
8
+
9
+ @njit
10
+ def evaluate_cut_edges(theta_bits, G_func, G_func_args_tuple, nodes):
11
+ n_qubits = len(nodes)
12
+ cut = 0
13
+ for u in range(n_qubits):
14
+ for v in range(u + 1, n_qubits):
15
+ if theta_bits[u] != theta_bits[v]:
16
+ cut += G_func((nodes[u], nodes[v]), G_func_args_tuple)
17
+
18
+ return cut
19
+
20
+
21
+ @njit
22
+ def compute_energy(theta_bits, G_func, G_func_args_tuple, nodes):
23
+ n_qubits = len(nodes)
24
+ energy = 0
25
+ for u in range(n_qubits):
26
+ for v in range(u + 1, n_qubits):
27
+ eigen = 1 if theta_bits[u] == theta_bits[v] else -1
28
+ energy += G_func((nodes[u], nodes[v]), G_func_args_tuple) * eigen
29
+
30
+ return energy
31
+
32
+
33
+ @njit
34
+ def bootstrap_worker(theta, G_func, G_func_args_tuple, nodes, indices):
35
+ local_theta = theta.copy()
36
+ for i in indices:
37
+ local_theta[i] = not local_theta[i]
38
+ energy = compute_energy(local_theta, G_func, G_func_args_tuple, nodes)
39
+
40
+ return energy
41
+
42
+
43
+ @njit(parallel=True)
44
+ def bootstrap(theta, G_func, G_func_args_tuple, nodes, k, indices_array):
45
+ n = len(indices_array) // k
46
+ energies = np.empty(n, dtype=np.float64)
47
+ for i in prange(n):
48
+ j = i * k
49
+ energies[i] = bootstrap_worker(theta, G_func, G_func_args_tuple, nodes, indices_array[j : j + k])
50
+
51
+ return energies
52
+
53
+
54
+ # By Gemini (Google Search AI)
55
+ def int_to_bitstring(integer, length):
56
+ return (bin(integer)[2:].zfill(length))[::-1]
57
+
58
+
59
+ def spin_glass_solver_streaming(
60
+ G_func,
61
+ nodes,
62
+ G_func_args_tuple=None,
63
+ quality=6,
64
+ shots=None,
65
+ best_guess=None
66
+ ):
67
+ n_qubits = len(nodes)
68
+
69
+ if n_qubits < 3:
70
+ if n_qubits == 0:
71
+ return "", 0, ([], []), 0
72
+
73
+ if n_qubits == 1:
74
+ return "0", 0, (nodes, []), 0
75
+
76
+ if n_qubits == 2:
77
+ weight = G_func((nodes[0], nodes[1]), G_func_args_tuple)
78
+ if weight < 0.0:
79
+ return "00", 0, (nodes, []), weight
80
+
81
+ return "01", weight, ([nodes[0]], [nodes[1]]), -weight
82
+
83
+ bitstring = ""
84
+ if isinstance(best_guess, str):
85
+ bitstring = best_guess
86
+ elif isinstance(best_guess, int):
87
+ bitstring = int_to_bitstring(best_guess, n_qubits)
88
+ elif isinstance(best_guess, list):
89
+ bitstring = "".join(["1" if b else "0" for b in best_guess])
90
+ else:
91
+ bitstring, _, _ = maxcut_tfim_streaming(G_func, nodes, G_func_args_tuple=G_func_args_tuple, quality=quality, shots=shots)
92
+ best_theta = [b == "1" for b in list(bitstring)]
93
+
94
+ min_energy = compute_energy(best_theta, G_func, G_func_args_tuple, nodes)
95
+ improved = True
96
+ correction_quality = 1
97
+ while improved:
98
+ improved = False
99
+ k = 1
100
+ while k <= correction_quality:
101
+ if n_qubits < k:
102
+ break
103
+
104
+ theta = best_theta.copy()
105
+
106
+ combos = list(
107
+ item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist
108
+ )
109
+ energies = bootstrap(theta, G_func, G_func_args_tuple, nodes, k, combos)
110
+
111
+ energy = energies.min()
112
+ if energy < min_energy:
113
+ index_match = np.random.choice(np.where(energies == energy)[0])
114
+ indices = combos[(index_match * k) : ((index_match + 1) * k)]
115
+ min_energy = energy
116
+ for i in indices:
117
+ best_theta[i] = not best_theta[i]
118
+ improved = True
119
+ if correction_quality < (k + 1):
120
+ correction_quality = k + 1
121
+ break
122
+
123
+ k = k + 1
124
+
125
+ bitstring = ""
126
+ l, r = [], []
127
+ for i in range(len(best_theta)):
128
+ b = best_theta[i]
129
+ if b:
130
+ bitstring += "1"
131
+ r.append(nodes[i])
132
+ else:
133
+ bitstring += "0"
134
+ l.append(nodes[i])
135
+
136
+ cut_value = evaluate_cut_edges(best_theta, G_func, G_func_args_tuple, nodes)
137
+
138
+ return bitstring, float(cut_value), (l, r), float(min_energy)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 5.0.2
3
+ Version: 5.1.0
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,8 +10,10 @@ pyqrackising/generate_tfim_samples.py
10
10
  pyqrackising/kernels.cl
11
11
  pyqrackising/maxcut_tfim.py
12
12
  pyqrackising/maxcut_tfim_sparse.py
13
+ pyqrackising/maxcut_tfim_streaming.py
13
14
  pyqrackising/spin_glass_solver.py
14
15
  pyqrackising/spin_glass_solver_sparse.py
16
+ pyqrackising/spin_glass_solver_streaming.py
15
17
  pyqrackising/tfim_magnetization.py
16
18
  pyqrackising/tfim_square_magnetization.py
17
19
  pyqrackising/tsp.py
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
51
51
 
52
52
  setup(
53
53
  name='pyqrackising',
54
- version='5.0.2',
54
+ version='5.1.0',
55
55
  author='Dan Strano',
56
56
  author_email='stranoj@gmail.com',
57
57
  description='Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)',
File without changes
File without changes
File without changes