PyQrackIsing 1.14.13__tar.gz → 1.15.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.
- {pyqrackising-1.14.13/PyQrackIsing.egg-info → pyqrackising-1.15.0}/PKG-INFO +1 -1
- {pyqrackising-1.14.13 → pyqrackising-1.15.0}/PyQrackIsing/maxcut_tfim.py +59 -40
- {pyqrackising-1.14.13 → pyqrackising-1.15.0}/PyQrackIsing/tsp_symmetric.py +103 -7
- {pyqrackising-1.14.13 → pyqrackising-1.15.0/PyQrackIsing.egg-info}/PKG-INFO +1 -1
- {pyqrackising-1.14.13 → pyqrackising-1.15.0}/README.md +2 -2
- {pyqrackising-1.14.13 → pyqrackising-1.15.0}/pyproject.toml +1 -1
- {pyqrackising-1.14.13 → pyqrackising-1.15.0}/setup.py +1 -1
- {pyqrackising-1.14.13 → pyqrackising-1.15.0}/CMakeLists.txt +0 -0
- {pyqrackising-1.14.13 → pyqrackising-1.15.0}/LICENSE.md +0 -0
- {pyqrackising-1.14.13 → pyqrackising-1.15.0}/MANIFEST.in +0 -0
- {pyqrackising-1.14.13 → pyqrackising-1.15.0}/PyQrackIsing/__init__.py +0 -0
- {pyqrackising-1.14.13 → pyqrackising-1.15.0}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
- {pyqrackising-1.14.13 → pyqrackising-1.15.0}/PyQrackIsing/generate_tfim_samples.py +0 -0
- {pyqrackising-1.14.13 → pyqrackising-1.15.0}/PyQrackIsing/spin_glass_solver.py +0 -0
- {pyqrackising-1.14.13 → pyqrackising-1.15.0}/PyQrackIsing/tfim_magnetization.py +0 -0
- {pyqrackising-1.14.13 → pyqrackising-1.15.0}/PyQrackIsing/tfim_square_magnetization.py +0 -0
- {pyqrackising-1.14.13 → pyqrackising-1.15.0}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
- {pyqrackising-1.14.13 → pyqrackising-1.15.0}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
- {pyqrackising-1.14.13 → pyqrackising-1.15.0}/PyQrackIsing.egg-info/not-zip-safe +0 -0
- {pyqrackising-1.14.13 → pyqrackising-1.15.0}/PyQrackIsing.egg-info/top_level.txt +0 -0
- {pyqrackising-1.14.13 → pyqrackising-1.15.0}/setup.cfg +0 -0
|
@@ -257,6 +257,61 @@ def evaluate_cut_edges(samples, edge_keys, edge_values):
|
|
|
257
257
|
return best_solution, float(best_value)
|
|
258
258
|
|
|
259
259
|
|
|
260
|
+
@njit
|
|
261
|
+
def init_thresholds(n_qubits):
|
|
262
|
+
n_bias = n_qubits - 1
|
|
263
|
+
thresholds = np.zeros(n_bias, dtype=np.float64)
|
|
264
|
+
tot_prob = 0
|
|
265
|
+
p = 1.0
|
|
266
|
+
if n_qubits & 1:
|
|
267
|
+
q = n_qubits // 2
|
|
268
|
+
thresholds[q - 1] = p
|
|
269
|
+
tot_prob = p
|
|
270
|
+
p /= 2
|
|
271
|
+
for q in range(1, n_qubits // 2):
|
|
272
|
+
thresholds[q - 1] = p
|
|
273
|
+
thresholds[n_bias - q] = p
|
|
274
|
+
tot_prob += 2 * p
|
|
275
|
+
p /= 2
|
|
276
|
+
thresholds /= tot_prob
|
|
277
|
+
|
|
278
|
+
return thresholds
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
@njit
|
|
282
|
+
def init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees):
|
|
283
|
+
theta = np.zeros(n_qubits)
|
|
284
|
+
for q in range(n_qubits):
|
|
285
|
+
J = J_eff[q]
|
|
286
|
+
z = degrees[q]
|
|
287
|
+
theta[q] = np.arcsin(
|
|
288
|
+
max(
|
|
289
|
+
-1.0,
|
|
290
|
+
min(
|
|
291
|
+
1.0,
|
|
292
|
+
(1.0 if J > 0.0 else -1.0) if np.isclose(abs(z * J), 0.0) else (abs(h_mult) / (z * J)),
|
|
293
|
+
),
|
|
294
|
+
)
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
return theta
|
|
298
|
+
|
|
299
|
+
@njit
|
|
300
|
+
def compute_adjacency(G_m):
|
|
301
|
+
n_qubits = len(G_m)
|
|
302
|
+
adjacency = np.full((n_qubits, n_qubits), -1, dtype=np.int32)
|
|
303
|
+
for i in range(n_qubits):
|
|
304
|
+
k = 0
|
|
305
|
+
for j in range(n_qubits):
|
|
306
|
+
if i == j:
|
|
307
|
+
continue
|
|
308
|
+
if G_m[i, j] != 0.0:
|
|
309
|
+
adjacency[i, k] = j
|
|
310
|
+
k += 1
|
|
311
|
+
|
|
312
|
+
return adjacency
|
|
313
|
+
|
|
314
|
+
|
|
260
315
|
# By Gemini (Google Search AI)
|
|
261
316
|
def int_to_bitstring(integer, length):
|
|
262
317
|
return (bin(integer)[2:].zfill(length))[::-1]
|
|
@@ -309,40 +364,13 @@ def maxcut_tfim(
|
|
|
309
364
|
J_eff = np.array([-sum(G_m[n]) for n in range(n_qubits)], dtype=np.float64)
|
|
310
365
|
degrees = np.array([sum(abs(G_m[n])) for n in range(n_qubits)], dtype=np.float64)
|
|
311
366
|
|
|
312
|
-
|
|
313
|
-
thresholds = np.zeros(n_bias, dtype=np.float64)
|
|
314
|
-
tot_prob = 0
|
|
315
|
-
p = 1.0
|
|
316
|
-
if n_qubits & 1:
|
|
317
|
-
q = n_qubits // 2
|
|
318
|
-
thresholds[q - 1] = p
|
|
319
|
-
tot_prob = p
|
|
320
|
-
p /= 2
|
|
321
|
-
for q in range(1, n_qubits // 2):
|
|
322
|
-
thresholds[q - 1] = p
|
|
323
|
-
thresholds[n_bias - q] = p
|
|
324
|
-
tot_prob += 2 * p
|
|
325
|
-
p /= 2
|
|
326
|
-
thresholds /= tot_prob
|
|
367
|
+
thresholds = init_thresholds(n_qubits)
|
|
327
368
|
|
|
328
369
|
if IS_CUDA_AVAILABLE and cuda.is_available() and grid_size >= 128:
|
|
329
370
|
delta_t = 1.0 / n_steps
|
|
330
371
|
tot_t = n_steps * delta_t
|
|
331
372
|
h_mult = 32.0 / tot_t
|
|
332
|
-
|
|
333
|
-
theta = np.zeros(n_qubits)
|
|
334
|
-
for q in range(n_qubits):
|
|
335
|
-
J = J_eff[q]
|
|
336
|
-
z = degrees[q]
|
|
337
|
-
theta[q] = np.arcsin(
|
|
338
|
-
max(
|
|
339
|
-
-1.0,
|
|
340
|
-
min(
|
|
341
|
-
1.0,
|
|
342
|
-
(1.0 if J > 0.0 else -1.0) if np.isclose(abs(z * J), 0.0) else (abs(h_mult) / (z * J)),
|
|
343
|
-
),
|
|
344
|
-
)
|
|
345
|
-
)
|
|
373
|
+
theta = init_theta(delta_t, tot_t, h_mult, n_qubits, J_eff, degrees)
|
|
346
374
|
|
|
347
375
|
cuda_maxcut_hamming_cdf[grid_dims, group_size](delta_t, tot_t, h_mult, J_eff, degrees, theta, thresholds)
|
|
348
376
|
|
|
@@ -350,23 +378,14 @@ def maxcut_tfim(
|
|
|
350
378
|
thresholds /= tot_prob
|
|
351
379
|
|
|
352
380
|
tot_prob = 0.0
|
|
353
|
-
for i in range(
|
|
381
|
+
for i in range(n_qubits - 1):
|
|
354
382
|
tot_prob += thresholds[i]
|
|
355
383
|
thresholds[i] = tot_prob
|
|
356
384
|
thresholds[-1] = 2.0
|
|
357
385
|
else:
|
|
358
386
|
maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, thresholds)
|
|
359
387
|
|
|
360
|
-
adjacency =
|
|
361
|
-
for i in range(n_qubits):
|
|
362
|
-
k = 0
|
|
363
|
-
for j in range(n_qubits):
|
|
364
|
-
if i == j:
|
|
365
|
-
continue
|
|
366
|
-
if G_m[i, j] != 0.0:
|
|
367
|
-
adjacency[i, k] = j
|
|
368
|
-
k += 1
|
|
369
|
-
|
|
388
|
+
adjacency = compute_adjacency(G_m)
|
|
370
389
|
J_max = max(J_eff)
|
|
371
390
|
weights = 1.0 / (1.0 + (J_max - J_eff))
|
|
372
391
|
# We only need unique instances
|
|
@@ -1,9 +1,19 @@
|
|
|
1
1
|
from .spin_glass_solver import spin_glass_solver
|
|
2
2
|
import networkx as nx
|
|
3
|
+
from numba import njit
|
|
3
4
|
import numpy as np
|
|
4
5
|
|
|
5
6
|
|
|
6
|
-
# two_opt() written by Elara (OpenAI ChatGPT instance)
|
|
7
|
+
# two_opt() and targeted_three_opt() written by Elara (OpenAI ChatGPT instance)
|
|
8
|
+
@njit
|
|
9
|
+
def path_length(path, G_m):
|
|
10
|
+
tot_len = 0.0
|
|
11
|
+
for i in range(len(path)-1):
|
|
12
|
+
tot_len += G_m[path[i], path[i+1]]
|
|
13
|
+
|
|
14
|
+
return tot_len
|
|
15
|
+
|
|
16
|
+
@njit
|
|
7
17
|
def two_opt(path, G):
|
|
8
18
|
improved = True
|
|
9
19
|
best_path = path
|
|
@@ -24,10 +34,93 @@ def two_opt(path, G):
|
|
|
24
34
|
path = best_path
|
|
25
35
|
return best_path, best_dist
|
|
26
36
|
|
|
27
|
-
|
|
28
|
-
|
|
37
|
+
@njit
|
|
38
|
+
def targeted_three_opt(path, W, k_neighbors=20):
|
|
39
|
+
"""
|
|
40
|
+
Lin-Kernighan style 3-opt heuristic for TSP improvement.
|
|
41
|
+
|
|
42
|
+
path: list of node indices (tour)
|
|
43
|
+
W: adjacency matrix (numpy array)
|
|
44
|
+
k_neighbors: restrict 3-opt to nearest neighbors for efficiency
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
n = len(path)
|
|
48
|
+
best_path = path[:]
|
|
49
|
+
best_dist = path_length(best_path, W)
|
|
50
|
+
improved = True
|
|
51
|
+
|
|
52
|
+
# Precompute nearest neighbors for each node
|
|
53
|
+
neighbor_lists = [
|
|
54
|
+
np.argsort(W[i])[:k_neighbors] for i in range(n)
|
|
55
|
+
]
|
|
29
56
|
|
|
30
|
-
|
|
57
|
+
while improved:
|
|
58
|
+
improved = False
|
|
59
|
+
for i in range(n - 5):
|
|
60
|
+
for j in neighbor_lists[path[i]]:
|
|
61
|
+
if j <= i or j >= n-3:
|
|
62
|
+
continue
|
|
63
|
+
for k in neighbor_lists[path[j]]:
|
|
64
|
+
if k <= j or k >= n-1:
|
|
65
|
+
continue
|
|
66
|
+
|
|
67
|
+
# Extract indices
|
|
68
|
+
A, B, C, D, E, F = path[i], path[i+1], path[j], path[j+1], path[k], path[k+1]
|
|
69
|
+
|
|
70
|
+
# 7 unique cases (same as brute force, but restricted)
|
|
71
|
+
new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:]
|
|
72
|
+
dist = path_length(new_path, W)
|
|
73
|
+
if dist < best_dist:
|
|
74
|
+
best_path, best_dist, improved = new_path, dist, True
|
|
75
|
+
break
|
|
76
|
+
|
|
77
|
+
new_path = best_path[:j+1] + best_path[j+1:k+1][::-1] + best_path[k+1:]
|
|
78
|
+
dist = path_length(new_path, W)
|
|
79
|
+
if dist < best_dist:
|
|
80
|
+
best_path, best_dist, improved = new_path, dist, True
|
|
81
|
+
break
|
|
82
|
+
|
|
83
|
+
new_path = best_path[:i+1] + best_path[i+1:j+1][::-1] + best_path[j+1:k+1][::-1] + best_path[k+1:]
|
|
84
|
+
dist = path_length(new_path, W)
|
|
85
|
+
if dist < best_dist:
|
|
86
|
+
best_path, best_dist, improved = new_path, dist, True
|
|
87
|
+
break
|
|
88
|
+
|
|
89
|
+
new_path = best_path[:i+1] + best_path[j+1:k+1] + best_path[i+1:j+1] + best_path[k+1:]
|
|
90
|
+
dist = path_length(new_path, W)
|
|
91
|
+
if dist < best_dist:
|
|
92
|
+
best_path, best_dist, improved = new_path, dist, True
|
|
93
|
+
break
|
|
94
|
+
|
|
95
|
+
new_path = best_path[:i+1] + best_path[j+1:k+1][::-1] + best_path[i+1:j+1][::-1] + best_path[k+1:]
|
|
96
|
+
dist = path_length(new_path, W)
|
|
97
|
+
if dist < best_dist:
|
|
98
|
+
best_path, best_dist, improved = new_path, dist, True
|
|
99
|
+
break
|
|
100
|
+
|
|
101
|
+
new_path = best_path[:i+1] + best_path[j+1:k+1] + best_path[i+1:j+1][::-1] + best_path[k+1:]
|
|
102
|
+
dist = path_length(new_path, W)
|
|
103
|
+
if dist < best_dist:
|
|
104
|
+
best_path, best_dist, improved = new_path, dist, True
|
|
105
|
+
break
|
|
106
|
+
|
|
107
|
+
new_path = best_path[:i+1] + best_path[j+1:k+1][::-1] + best_path[i+1:j+1] + best_path[k+1:]
|
|
108
|
+
dist = path_length(new_path, W)
|
|
109
|
+
if dist < best_dist:
|
|
110
|
+
best_path, best_dist, improved = new_path, dist, True
|
|
111
|
+
break
|
|
112
|
+
|
|
113
|
+
if improved:
|
|
114
|
+
break
|
|
115
|
+
|
|
116
|
+
if improved:
|
|
117
|
+
break
|
|
118
|
+
|
|
119
|
+
path = best_path[:]
|
|
120
|
+
|
|
121
|
+
return best_path, best_dist
|
|
122
|
+
|
|
123
|
+
def tsp_symmetric(G, quality=0, shots=None, correction_quality=2, start_node=None, is_cyclic=True, is_3_opt=True, k_neighbors=20):
|
|
31
124
|
nodes = None
|
|
32
125
|
n_nodes = 0
|
|
33
126
|
G_m = None
|
|
@@ -75,8 +168,8 @@ def tsp_symmetric(G, quality=0, shots=None, correction_quality=2, is_cyclic=True
|
|
|
75
168
|
continue
|
|
76
169
|
G_b[i, j] = G_m[b[i], b[j]]
|
|
77
170
|
|
|
78
|
-
sol_a = tsp_symmetric(G_a, quality=quality, is_cyclic=False)
|
|
79
|
-
sol_b = tsp_symmetric(G_b, quality=quality, is_cyclic=False)
|
|
171
|
+
sol_a = tsp_symmetric(G_a, quality=quality, is_cyclic=False, is_3_opt=False)
|
|
172
|
+
sol_b = tsp_symmetric(G_b, quality=quality, is_cyclic=False, is_3_opt=False)
|
|
80
173
|
|
|
81
174
|
path_a = [a[x] for x in sol_a[0]]
|
|
82
175
|
path_b = [b[x] for x in sol_b[0]]
|
|
@@ -143,7 +236,10 @@ def tsp_symmetric(G, quality=0, shots=None, correction_quality=2, is_cyclic=True
|
|
|
143
236
|
path_a, path_b = path_b, path_a
|
|
144
237
|
terminals_a, terminals_b = terminals_b, terminals_a
|
|
145
238
|
|
|
146
|
-
|
|
239
|
+
best_path, best_weight = two_opt(best_path, G_m)
|
|
240
|
+
|
|
241
|
+
if is_3_opt:
|
|
242
|
+
best_path, best_weight = targeted_three_opt(best_path, G_m, k_neighbors)
|
|
147
243
|
|
|
148
244
|
if is_cyclic:
|
|
149
245
|
best_weight += G_m[best_path[-1], best_path[0]]
|
|
@@ -93,13 +93,13 @@ def generate_tsp_graph(n_nodes=64, seed=None):
|
|
|
93
93
|
|
|
94
94
|
n_nodes = 128
|
|
95
95
|
G = generate_tsp_graph(n_nodes=n_nodes, seed=42)
|
|
96
|
-
circuit, path_length = tsp_symmetric(G, quality=0, correction_quality=2, is_cyclic=True,
|
|
96
|
+
circuit, path_length = tsp_symmetric(G, quality=0, correction_quality=2, start_node=None, is_cyclic=True, is_3_opt=True, k_neighbors=20)
|
|
97
97
|
|
|
98
98
|
print(f"Node count: {n_nodes}")
|
|
99
99
|
print(f"Path: {circuit}")
|
|
100
100
|
print(f"Path length: {path_length}")
|
|
101
101
|
```
|
|
102
|
-
We only provide a solver for the symmetric version of the TSP (i.e., the distance from "A" to "B" is considered the same as from "B" to "A"). `is_cyclic` controls whether the path must ultimately complete a circuit, if `True`, or a one-way trip, if `False`. When the desired route is acyclic, a fixed starting point can optionally be specified via passing the node label to `start_node` (or, without this parameter, the solver will also attempt to pick an optimal starting point).
|
|
102
|
+
We only provide a solver for the symmetric version of the TSP (i.e., the distance from "A" to "B" is considered the same as from "B" to "A"). `is_cyclic` controls whether the path must ultimately complete a circuit, if `True`, or a one-way trip, if `False`. When the desired route is acyclic, a fixed starting point can optionally be specified via passing the node label to `start_node` (or, without this parameter, the solver will also attempt to pick an optimal starting point). Set `is_3_opt=False` or reduce the 3-opt `k_neighbors` to save time, with slightly lower-quality results.
|
|
103
103
|
|
|
104
104
|
## About
|
|
105
105
|
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.
|
|
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
|
|
|
12
12
|
|
|
13
13
|
[project]
|
|
14
14
|
name = "PyQrackIsing"
|
|
15
|
-
version = "1.
|
|
15
|
+
version = "1.15.0"
|
|
16
16
|
requires-python = ">=3.8"
|
|
17
17
|
description = "Near-ideal closed-form solutions for transverse field Ising model (TFIM)"
|
|
18
18
|
readme = {file = "README.txt", content-type = "text/markdown"}
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='PyQrackIsing',
|
|
54
|
-
version='1.
|
|
54
|
+
version='1.15.0',
|
|
55
55
|
author='Dan Strano',
|
|
56
56
|
author_email='stranoj@gmail.com',
|
|
57
57
|
description='Near-ideal closed-form solutions for transverse field Ising model (TFIM)',
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|