PyQrackIsing 1.14.6__tar.gz → 1.14.8__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.6/PyQrackIsing.egg-info → pyqrackising-1.14.8}/PKG-INFO +1 -1
- {pyqrackising-1.14.6 → pyqrackising-1.14.8}/PyQrackIsing/maxcut_tfim.py +76 -43
- {pyqrackising-1.14.6 → pyqrackising-1.14.8}/PyQrackIsing/spin_glass_solver.py +5 -3
- {pyqrackising-1.14.6 → pyqrackising-1.14.8}/PyQrackIsing/tsp_symmetric.py +1 -1
- {pyqrackising-1.14.6 → pyqrackising-1.14.8/PyQrackIsing.egg-info}/PKG-INFO +1 -1
- {pyqrackising-1.14.6 → pyqrackising-1.14.8}/README.md +4 -4
- {pyqrackising-1.14.6 → pyqrackising-1.14.8}/pyproject.toml +1 -1
- {pyqrackising-1.14.6 → pyqrackising-1.14.8}/setup.py +1 -1
- {pyqrackising-1.14.6 → pyqrackising-1.14.8}/CMakeLists.txt +0 -0
- {pyqrackising-1.14.6 → pyqrackising-1.14.8}/LICENSE.md +0 -0
- {pyqrackising-1.14.6 → pyqrackising-1.14.8}/MANIFEST.in +0 -0
- {pyqrackising-1.14.6 → pyqrackising-1.14.8}/PyQrackIsing/__init__.py +0 -0
- {pyqrackising-1.14.6 → pyqrackising-1.14.8}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
- {pyqrackising-1.14.6 → pyqrackising-1.14.8}/PyQrackIsing/generate_tfim_samples.py +0 -0
- {pyqrackising-1.14.6 → pyqrackising-1.14.8}/PyQrackIsing/tfim_magnetization.py +0 -0
- {pyqrackising-1.14.6 → pyqrackising-1.14.8}/PyQrackIsing/tfim_square_magnetization.py +0 -0
- {pyqrackising-1.14.6 → pyqrackising-1.14.8}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
- {pyqrackising-1.14.6 → pyqrackising-1.14.8}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
- {pyqrackising-1.14.6 → pyqrackising-1.14.8}/PyQrackIsing.egg-info/not-zip-safe +0 -0
- {pyqrackising-1.14.6 → pyqrackising-1.14.8}/PyQrackIsing.egg-info/top_level.txt +0 -0
- {pyqrackising-1.14.6 → pyqrackising-1.14.8}/setup.cfg +0 -0
|
@@ -145,51 +145,87 @@ def maxcut_hamming_cdf(n_qubits, J_func, degrees, quality, hamming_prob):
|
|
|
145
145
|
|
|
146
146
|
|
|
147
147
|
# Written by Elara (OpenAI custom GPT)
|
|
148
|
-
|
|
148
|
+
@njit(parallel=True)
|
|
149
|
+
def local_repulsion_choice(adjacency, degrees, weights, n, m):
|
|
149
150
|
"""
|
|
150
|
-
Pick m nodes
|
|
151
|
+
Pick m nodes out of n with repulsion bias:
|
|
151
152
|
- High-degree nodes are already less likely
|
|
152
153
|
- After choosing a node, its neighbors' probabilities are further reduced
|
|
154
|
+
adjacency: 2D int array (n x max_deg), padded with -1
|
|
155
|
+
degrees: int array of shape (n,)
|
|
156
|
+
weights: float64 array of shape (n,)
|
|
153
157
|
"""
|
|
154
158
|
|
|
155
|
-
# Base weights: inverse degree
|
|
156
|
-
# degrees = np.array([len(adjacency.get(i, [])) for i in range(n)], dtype=np.float64)
|
|
157
|
-
# weights = 1.0 / (degrees + 1.0)
|
|
158
159
|
weights = weights.copy()
|
|
160
|
+
chosen = np.zeros(m, dtype=np.int32) # store chosen indices
|
|
161
|
+
available = np.ones(n, dtype=np.int32) # 1 = available, 0 = not
|
|
162
|
+
mask = np.zeros(n, dtype=np.bool_)
|
|
163
|
+
chosen_count = 0
|
|
164
|
+
|
|
165
|
+
for _ in prange(m):
|
|
166
|
+
# Count available
|
|
167
|
+
total_w = 0.0
|
|
168
|
+
for i in range(n):
|
|
169
|
+
if available[i] == 1:
|
|
170
|
+
total_w += weights[i]
|
|
171
|
+
if total_w <= 0:
|
|
172
|
+
break
|
|
159
173
|
|
|
160
|
-
|
|
161
|
-
|
|
174
|
+
# Normalize & sample
|
|
175
|
+
r = np.random.rand()
|
|
176
|
+
cum = 0.0
|
|
177
|
+
node = -1
|
|
178
|
+
for i in range(n):
|
|
179
|
+
if available[i] == 1:
|
|
180
|
+
cum += weights[i] / total_w
|
|
181
|
+
if r < cum:
|
|
182
|
+
node = i
|
|
183
|
+
break
|
|
184
|
+
|
|
185
|
+
if node == -1:
|
|
186
|
+
continue
|
|
162
187
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
188
|
+
# Select node
|
|
189
|
+
chosen[chosen_count] = node
|
|
190
|
+
chosen_count += 1
|
|
191
|
+
available[node] = 0
|
|
192
|
+
mask[node] = True
|
|
166
193
|
|
|
167
|
-
#
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
194
|
+
# Repulsion: penalize neighbors
|
|
195
|
+
deg = degrees[node]
|
|
196
|
+
for j in range(deg):
|
|
197
|
+
nbr = adjacency[node, j]
|
|
198
|
+
if nbr >= 0 and available[nbr] == 1:
|
|
199
|
+
weights[nbr] *= 0.5 # tunable penalty factor
|
|
171
200
|
|
|
172
|
-
|
|
173
|
-
idx = np.random.choice(len(sub_nodes), p=sub_weights)
|
|
174
|
-
node = sub_nodes[idx]
|
|
175
|
-
chosen.append(node)
|
|
201
|
+
return mask
|
|
176
202
|
|
|
177
|
-
# Remove node from available
|
|
178
|
-
available.remove(node)
|
|
179
203
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
idx = nodes.index(nbr)
|
|
183
|
-
if idx in available:
|
|
184
|
-
weights[idx] *= 0.5 # halve neighbor's weight (tunable!)
|
|
204
|
+
def local_repulsion_choice_sample(shots, thresholds, adjacency, degrees, weights, n):
|
|
205
|
+
samples = []
|
|
185
206
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
207
|
+
for s in range(shots):
|
|
208
|
+
# First dimension: Hamming weight
|
|
209
|
+
mag_prob = np.random.random()
|
|
210
|
+
m = 0
|
|
211
|
+
while thresholds[m] < mag_prob:
|
|
212
|
+
m += 1
|
|
213
|
+
m += 1
|
|
190
214
|
|
|
191
|
-
|
|
215
|
+
# Second dimension: permutation within Hamming weight
|
|
216
|
+
samples.append(mask_array_to_python_int(local_repulsion_choice(adjacency, degrees, weights, n, m)))
|
|
192
217
|
|
|
218
|
+
return samples
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def mask_array_to_python_int(mask):
|
|
222
|
+
sample = 0
|
|
223
|
+
for b in reversed(mask):
|
|
224
|
+
sample <<= 1
|
|
225
|
+
if b:
|
|
226
|
+
sample |= 1
|
|
227
|
+
|
|
228
|
+
return sample
|
|
193
229
|
|
|
194
230
|
def evaluate_cut_edges(samples, edge_keys, edge_values):
|
|
195
231
|
best_value = float("-inf")
|
|
@@ -237,7 +273,7 @@ def maxcut_tfim(
|
|
|
237
273
|
shared_size = (n_qubits - 1) * 8
|
|
238
274
|
|
|
239
275
|
if quality is None:
|
|
240
|
-
quality =
|
|
276
|
+
quality = 10
|
|
241
277
|
|
|
242
278
|
if shots is None:
|
|
243
279
|
# Number of measurement shots
|
|
@@ -308,22 +344,19 @@ def maxcut_tfim(
|
|
|
308
344
|
thresholds[-1] = 1.0
|
|
309
345
|
else:
|
|
310
346
|
maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, thresholds)
|
|
347
|
+
|
|
311
348
|
G_dict = nx.to_dict_of_lists(G)
|
|
349
|
+
max_degree = max(len(x) for x in G_dict.values())
|
|
350
|
+
adjacency = np.full((len(nodes), max_degree), -1, dtype=np.int32)
|
|
351
|
+
for i in range(len(nodes)):
|
|
352
|
+
adj = G_dict.get(nodes[i], [])
|
|
353
|
+
for j in range(len(adj)):
|
|
354
|
+
adjacency[i, j] = adj[j]
|
|
355
|
+
|
|
312
356
|
J_max = max(J_eff)
|
|
313
357
|
weights = 1.0 / (1.0 + (J_max - J_eff))
|
|
314
|
-
samples = []
|
|
315
|
-
for s in range(shots):
|
|
316
|
-
# First dimension: Hamming weight
|
|
317
|
-
mag_prob = np.random.random()
|
|
318
|
-
m = 0
|
|
319
|
-
while thresholds[m] < mag_prob:
|
|
320
|
-
m += 1
|
|
321
|
-
m += 1
|
|
322
|
-
# Second dimension: permutation within Hamming weight
|
|
323
|
-
samples.append(local_repulsion_choice(nodes, G_dict, degrees, weights, n_qubits, m))
|
|
324
|
-
|
|
325
358
|
# We only need unique instances
|
|
326
|
-
samples = list(set(
|
|
359
|
+
samples = list(set(local_repulsion_choice_sample(shots, thresholds, adjacency, degrees, weights, n_qubits)))
|
|
327
360
|
|
|
328
361
|
edge_keys = []
|
|
329
362
|
edge_values = []
|
|
@@ -56,7 +56,7 @@ def int_to_bitstring(integer, length):
|
|
|
56
56
|
return (bin(integer)[2:].zfill(length))[::-1]
|
|
57
57
|
|
|
58
58
|
|
|
59
|
-
def spin_glass_solver(G, quality=None, shots=None, correction_quality=
|
|
59
|
+
def spin_glass_solver(G, quality=None, shots=None, correction_quality=None, best_guess=None):
|
|
60
60
|
nodes = list(G.nodes())
|
|
61
61
|
n_qubits = len(nodes)
|
|
62
62
|
|
|
@@ -66,8 +66,10 @@ def spin_glass_solver(G, quality=None, shots=None, correction_quality=2, best_gu
|
|
|
66
66
|
if n_qubits == 1:
|
|
67
67
|
return "0", 0, ([nodes[0]], [])
|
|
68
68
|
|
|
69
|
-
if
|
|
70
|
-
|
|
69
|
+
if correction_quality is None:
|
|
70
|
+
# maxcut_tfim(G) scales roughly like n^4,
|
|
71
|
+
# so its match order of overhead.
|
|
72
|
+
correction_quality = 4
|
|
71
73
|
|
|
72
74
|
bitstring = ""
|
|
73
75
|
if isinstance(best_guess, str):
|
|
@@ -21,7 +21,7 @@ def get_best_stitch(adjacency, terminals_a, terminals_b, is_cyclic):
|
|
|
21
21
|
return best_weight, best_edge
|
|
22
22
|
|
|
23
23
|
|
|
24
|
-
def tsp_symmetric(G, quality=
|
|
24
|
+
def tsp_symmetric(G, quality=0, shots=None, correction_quality=2, is_cyclic=True, start_node=None):
|
|
25
25
|
nodes = list(G.nodes())
|
|
26
26
|
n_nodes = len(nodes)
|
|
27
27
|
|
|
@@ -49,7 +49,7 @@ G = nx.petersen_graph()
|
|
|
49
49
|
best_solution_bit_string, best_cut_value, best_node_groups = maxcut_tfim(G, quality=8)
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
-
The (integer) `quality` setting is optional, with a default value of `
|
|
52
|
+
The (integer) `quality` setting is optional, with a default value of `10`, but you can turn it up for higher-quality results, or turn it down to save time. (You can also optionally specify the number of measurement `shots` as an argument, if you want specific fine-grained control over resource usage.) If you want to run MAXCUT on a graph with non-uniform edge weights, specify them as the `weight` attribute of each edge, with `networkx`. (If any `weight` attribute is not defined, the solver assumes it's `1.0` for that edge.)
|
|
53
53
|
|
|
54
54
|
Based on a combination of the TFIM-inspired MAXCUT solver and another technique for finding ground-state energy in quantum chemistry that we call the _"binary Clifford eigensolver,"_ we also provide an (approximate) spin glass ground-state solver:
|
|
55
55
|
```py
|
|
@@ -72,7 +72,7 @@ G = generate_spin_glass_graph(n_nodes=64, seed=42)
|
|
|
72
72
|
solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, quality=5, correction_quality=2, best_guess=None)
|
|
73
73
|
# solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, best_guess=maxcut_tfim(G, quality=8)[0])
|
|
74
74
|
```
|
|
75
|
-
The (integer) `quality` setting is the same as `maxcut_tfim`. `correction_quality` controls an additional convex optimization procedure on top of `best_guess` and defaults to
|
|
75
|
+
The (integer) `quality` setting is the same as `maxcut_tfim`. `correction_quality` controls an additional convex optimization procedure on top of `best_guess` and defaults to 4. `best_guess` gives the option to seed the algorithm with a best guess as to the maximal cut (as an integer, binary string, or list of booleans). By default, `spin_glass_solver()` uses `maxcut_tfim(G)` with passed-through `quality` as `best_guess`, which typically works well, but it could be seeded with higher `maxcut_tfim()` `quality` or Goemans-Williamson, for example. This function is designed with a sign convention for weights such that it can immediately be used as a MAXCUT solver itself: you might need to reverse the sign convention on your weights for spin glass graphs, but this is only convention.
|
|
76
76
|
|
|
77
77
|
From the `spin_glass_solver()`, we provide a (recursive) Traveling Salesman Problem (TSP) solver:
|
|
78
78
|
```py
|
|
@@ -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=
|
|
96
|
+
circuit, path_length = tsp_symmetric(G, quality=0, correction_quality=2, is_cyclic=True, start_node=None)
|
|
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
|
-
|
|
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).
|
|
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.14.
|
|
15
|
+
version = "1.14.8"
|
|
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.14.
|
|
54
|
+
version='1.14.8',
|
|
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
|