PyQrackIsing 9.16.11__tar.gz → 9.17.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-9.17.0/MANIFEST.in +1 -0
- pyqrackising-9.17.0/PKG-INFO +299 -0
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/README.md +25 -0
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyproject.toml +8 -2
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising/__init__.py +1 -0
- pyqrackising-9.17.0/pyqrackising/local_search.py +401 -0
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising/maxcut_tfim.py +88 -36
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising/maxcut_tfim_sparse.py +106 -60
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising/maxcut_tfim_streaming.py +46 -26
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising/maxcut_tfim_util.py +190 -133
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising/otoc.py +44 -37
- pyqrackising-9.17.0/pyqrackising/solve_qubo.py +133 -0
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising/spin_glass_solver.py +61 -243
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising/spin_glass_solver_hybrid.py +9 -2
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising/spin_glass_solver_sparse.py +84 -337
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising/spin_glass_solver_streaming.py +23 -188
- pyqrackising-9.17.0/pyqrackising.egg-info/PKG-INFO +299 -0
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising.egg-info/SOURCES.txt +4 -1
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising.egg-info/requires.txt +3 -0
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/setup.py +4 -1
- pyqrackising-9.17.0/tests/test_pyqrackising.py +306 -0
- pyqrackising-9.16.11/MANIFEST.in +0 -3
- pyqrackising-9.16.11/PKG-INFO +0 -31
- pyqrackising-9.16.11/pyqrackising.egg-info/PKG-INFO +0 -31
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/LICENSE.md +0 -0
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising/generate_tfim_samples.py +0 -0
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising/solve_maxcut_bnb.py +0 -0
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising/solve_maxcut_bnb_sparse.py +0 -0
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising/solve_maxcut_bnb_streaming.py +0 -0
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising/solve_maxcut_exact.py +0 -0
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising/solve_maxcut_exact_sparse.py +0 -0
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising/solve_maxcut_exact_streaming.py +0 -0
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising/tsp.py +0 -0
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising/tsp_maxcut.py +0 -0
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-9.16.11 → pyqrackising-9.17.0}/setup.cfg +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
include pyqrackising/kernels.cl
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyqrackising
|
|
3
|
+
Version: 9.17.0
|
|
4
|
+
Summary: Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)
|
|
5
|
+
Home-page: https://github.com/vm6502q/PyQrackIsing
|
|
6
|
+
Author: Dan Strano
|
|
7
|
+
Author-email: Dan Strano <stranoj@gmail.com>
|
|
8
|
+
Maintainer-email: Dan Strano <stranoj@gmail.com>
|
|
9
|
+
License: LGPL-3.0-or-later
|
|
10
|
+
Project-URL: Homepage, https://github.com/vm6502q/PyQrackIsing
|
|
11
|
+
Project-URL: Documentation, https://github.com/vm6502q/PyQrackIsing/blob/main/README.md
|
|
12
|
+
Project-URL: Repository, https://github.com/vm6502q/PyQrackIsing
|
|
13
|
+
Project-URL: Issues, https://github.com/vm6502q/PyQrackIsing/issues
|
|
14
|
+
Classifier: Environment :: Console
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Intended Audience :: Science/Research
|
|
17
|
+
Classifier: Programming Language :: C++
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
+
Classifier: Topic :: Scientific/Engineering
|
|
25
|
+
Requires-Python: >=3.8
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE.md
|
|
28
|
+
Requires-Dist: networkx
|
|
29
|
+
Requires-Dist: numpy
|
|
30
|
+
Requires-Dist: numba
|
|
31
|
+
Requires-Dist: scipy
|
|
32
|
+
Provides-Extra: test
|
|
33
|
+
Requires-Dist: pytest; extra == "test"
|
|
34
|
+
Dynamic: author
|
|
35
|
+
Dynamic: home-page
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# PyQrack Ising
|
|
39
|
+
Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)
|
|
40
|
+
|
|
41
|
+
(It's "the **Ising** on top.")
|
|
42
|
+
|
|
43
|
+
[](https://doi.org/10.5281/zenodo.18706638) [](https://pepy.tech/projects/pyqrackising)
|
|
44
|
+
|
|
45
|
+
## Copyright and license
|
|
46
|
+
(c) Daniel Strano and the Qrack contributors 2025. All rights reserved.
|
|
47
|
+
|
|
48
|
+
Licensed under the GNU Lesser General Public License V3.
|
|
49
|
+
|
|
50
|
+
See LICENSE.md in the project root or https://www.gnu.org/licenses/lgpl-3.0.en.html for details.
|
|
51
|
+
|
|
52
|
+
## Installation
|
|
53
|
+
From PyPi:
|
|
54
|
+
```
|
|
55
|
+
pip3 install PyQrackIsing
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
From Source:
|
|
59
|
+
```
|
|
60
|
+
pip3 install .
|
|
61
|
+
```
|
|
62
|
+
in the root source directory (with `setup.py`).
|
|
63
|
+
|
|
64
|
+
Windows users might find Windows Subsystem Linux (WSL) to be the easier and preferred choice for installation.
|
|
65
|
+
|
|
66
|
+
## Usage
|
|
67
|
+
|
|
68
|
+
```py
|
|
69
|
+
from pyqrackising import generate_tfim_samples
|
|
70
|
+
|
|
71
|
+
samples = generate_tfim_samples(
|
|
72
|
+
J=-1.0,
|
|
73
|
+
h=2.0,
|
|
74
|
+
z=4,
|
|
75
|
+
theta=0.174532925199432957,
|
|
76
|
+
t=5,
|
|
77
|
+
n_qubits=56,
|
|
78
|
+
shots=100
|
|
79
|
+
)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
There are two other functions, `tfim_magnetization()` and `tfim_square_magnetization()`, that follow the same function signature except without the `shots` argument.
|
|
83
|
+
|
|
84
|
+
The library also provides a TFIM-inspired (approximate) MAXCUT solver (which accepts a `networkx` graph or a 32-bit adjacency matrix):
|
|
85
|
+
```py
|
|
86
|
+
from pyqrackising import maxcut_tfim
|
|
87
|
+
import networkx as nx
|
|
88
|
+
|
|
89
|
+
G = nx.petersen_graph()
|
|
90
|
+
best_solution_bit_string, best_cut_value, best_node_groups = maxcut_tfim(G, quality=6, shots=None, is_spin_glass=False, anneal_t=8.0, anneal_h=8.0, repulsion_base=5.0)
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
We also provide `maxcut_tfim_sparse(G)`, for `scipy` CSR sparse arrays (or `networkx` graphs), and `maxcut_tfim_streaming(G_func, nodes)` for `numba` JIT streaming weights function definitions. The (integer) `quality` setting is optional, with a default value of `6`, 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.) `anneal_t` and `anneal_h` control the physical maximum annealing time and `h` transverse field parameter, as in Trotterized Ising model. While `anneal_t` and `anneal_h` control how annealing finds the _Hamming weight_ of cuts (i.e., how many nodes end up in either partition), `repulsion_base` similarly controls the steepness of the search basin for optimizing bit-string locality ("like-like" repulsion and "like-unlike" attraction) within each partition. 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.)
|
|
94
|
+
|
|
95
|
+
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:
|
|
96
|
+
```py
|
|
97
|
+
from pyqrackising import spin_glass_solver
|
|
98
|
+
import networkx as nx
|
|
99
|
+
import numpy as np
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
# NP-complete spin glass
|
|
103
|
+
def generate_spin_glass_graph(n_nodes=16, degree=3, seed=None):
|
|
104
|
+
if not (seed is None):
|
|
105
|
+
np.random.seed(seed)
|
|
106
|
+
G = nx.random_regular_graph(d=degree, n=n_nodes, seed=seed)
|
|
107
|
+
for u, v in G.edges():
|
|
108
|
+
G[u][v]['weight'] = np.random.choice([-1, 1]) # spin glass couplings
|
|
109
|
+
return G
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
G = generate_spin_glass_graph(n_nodes=64, seed=42)
|
|
113
|
+
solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(
|
|
114
|
+
G,
|
|
115
|
+
quality=6,
|
|
116
|
+
shots=None,
|
|
117
|
+
anneal_t=8.0,
|
|
118
|
+
anneal_h=8.0,
|
|
119
|
+
is_spin_glass=True,
|
|
120
|
+
best_guess=None,
|
|
121
|
+
is_maxcut_gpu=True,
|
|
122
|
+
gray_iterations=None,
|
|
123
|
+
gray_seed_multiple=None,
|
|
124
|
+
bp_scale=None,
|
|
125
|
+
bp_damping=0.5
|
|
126
|
+
)
|
|
127
|
+
# solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(G, best_guess=maxcut_tfim(G, quality=6)[0])
|
|
128
|
+
```
|
|
129
|
+
We also provide `spin_glass_solver_sparse(G)`, `spin_glass_solver_streaming(G_func, nodes)`, and `spin_glass_hybrid(G)` (which combines the dense TFIM-based solver with a sparse local-optimization solver). `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. `is_spin_glass` controls whether the solver optimizes for cut value or spin-glass energy. 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. `gray_iterations` gives manual control over how many iterations are carried out of a parallel Gray-code search on `best_guess`. `gray_seed_multiple` controls how many parallel search seeds (as a multiple of your CPU thread count) are tested for the best parallel seeds, and a value of `1` will perfectly cover the search space without collision if your node count is a power of 2. `bp_scale` controls opt-in and time complexity scaling for _belief propagation_ as a competing "warm start," with `None` disabling this option completely, `1.0` representing a budget of exactly `O(n^3)` time complexity, and lesser or greater values representing a linear multiplier on that budget. `bp_damping` similarly controls belief propagation damping.
|
|
130
|
+
|
|
131
|
+
### QUBO and Ising problems
|
|
132
|
+
|
|
133
|
+
`solve_qubo(Q)` approximately minimizes `x^T Q x` over binary `x`, and `solve_ising(J, h)` minimizes `sum_{i<j} J_ij s_i s_j + sum_i h_i s_i` over spins `s = ±1`. Both accept a dense `numpy` array or a `scipy` sparse matrix (and then use the dense or sparse spin glass solver), pass other keyword arguments through to that solver, and return the solution with its objective value recomputed exactly in 64-bit:
|
|
134
|
+
```py
|
|
135
|
+
from pyqrackising import solve_qubo, solve_ising
|
|
136
|
+
|
|
137
|
+
x, value = solve_qubo(Q, quality=6, seed=1)
|
|
138
|
+
spins, energy = solve_ising(J, h, time_limit=60.0)
|
|
139
|
+
```
|
|
140
|
+
Local fields are handled exactly by one extra node coupled to every spin; `qubo_to_ising(Q)` exposes the conversion.
|
|
141
|
+
|
|
142
|
+
### Reproducibility and time limits
|
|
143
|
+
|
|
144
|
+
The MAXCUT and spin glass solvers take `seed=` (an integer) for reproducible results: the samplers draw from a counter-based generator keyed by seed, round and shot, so results don't depend on thread scheduling. (On the CPU path the number of sampling chains defaults from `os.cpu_count()`, so compare runs on machines with the same core count.) Without `seed`, a seed is drawn from NumPy's global generator, so `np.random.seed(...)` also makes runs repeatable. (Before, it did not reach Numba's random state.)
|
|
145
|
+
|
|
146
|
+
`time_limit=` (seconds) bounds a solve: sampling and then local search stop at the deadline, between rounds or steps, and return the best solution so far.
|
|
147
|
+
|
|
148
|
+
Sparse inputs given as `scipy` matrices are read as their strict upper triangle, so a full symmetric matrix and its upper triangle describe the same graph.
|
|
149
|
+
|
|
150
|
+
These solvers above can be used as a "warm state" for _MaxSAT-based_ solvers or _branch-and-bound_ solvers, potentially to reach _exact_ solutions to MAXCUT:
|
|
151
|
+
|
|
152
|
+
```py
|
|
153
|
+
from pyqrackising import solve_maxcut_exact, solve_maxcut_exact_sparse, solve_maxcut_exact_streaming
|
|
154
|
+
from pyqrackising import solve_maxcut_bnb, solve_maxcut_bnb_sparse, solve_maxcut_bnb_streaming
|
|
155
|
+
import networkx as nx
|
|
156
|
+
|
|
157
|
+
G = nx.petersen_graph()
|
|
158
|
+
best_solution_bit_string, best_cut_value, best_node_groups = solve_maxcut_bnb(G, time_limit=None)
|
|
159
|
+
```
|
|
160
|
+
Pass a time limit (in seconds) if desired.
|
|
161
|
+
|
|
162
|
+
From the MAXCUT solvers, we provide a (recursive) Traveling Salesman Problem (TSP) solver:
|
|
163
|
+
```py
|
|
164
|
+
from pyqrackising import tsp_symmetric
|
|
165
|
+
import networkx as nx
|
|
166
|
+
import numpy as np
|
|
167
|
+
|
|
168
|
+
# Traveling Salesman Problem (normalized to longest segment)
|
|
169
|
+
def generate_tsp_graph(n_nodes=64, seed=None):
|
|
170
|
+
if not (seed is None):
|
|
171
|
+
np.random.seed(seed)
|
|
172
|
+
G = nx.Graph()
|
|
173
|
+
for u in range(n_nodes):
|
|
174
|
+
for v in range(u + 1, n_nodes):
|
|
175
|
+
G.add_edge(u, v, weight=np.random.random())
|
|
176
|
+
return G
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
n_nodes = 128
|
|
180
|
+
G = generate_tsp_graph(n_nodes=n_nodes, seed=42)
|
|
181
|
+
circuit, path_length = tsp_symmetric(
|
|
182
|
+
G,
|
|
183
|
+
start_node=None,
|
|
184
|
+
end_node=None,
|
|
185
|
+
monte_carlo=True,
|
|
186
|
+
quality=2,
|
|
187
|
+
is_cyclic=True,
|
|
188
|
+
multi_start=1,
|
|
189
|
+
k_neighbors=20
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
print(f"Node count: {n_nodes}")
|
|
193
|
+
print(f"Path: {circuit}")
|
|
194
|
+
print(f"Path length: {path_length}")
|
|
195
|
+
```
|
|
196
|
+
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.
|
|
197
|
+
|
|
198
|
+
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()`:
|
|
199
|
+
```py
|
|
200
|
+
from pyqrackising import spin_glass_solver_streaming
|
|
201
|
+
# from pyqrackising import maxcut_tfim_streaming
|
|
202
|
+
from numba import njit
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
# This is a contrived example.
|
|
206
|
+
# The function must use numba NJIT.
|
|
207
|
+
# (In practice, even if you use other Python functionality like itertools,
|
|
208
|
+
# you can pre-calculate and load the data as a list through the arguments tuple.)
|
|
209
|
+
@njit
|
|
210
|
+
def G_func(node_pair, args_tuple):
|
|
211
|
+
i, j = min(node_pair), max(node_pair)
|
|
212
|
+
return ((j + 1) % (i + 1)) / args_tuple[0]
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
n_qubits = 64
|
|
216
|
+
nodes = list(range(n_qubits))
|
|
217
|
+
args_tuple = (n_qubits,)
|
|
218
|
+
|
|
219
|
+
solution_bit_string, cut_value, node_groups, energy = spin_glass_solver_streaming(G_func, nodes, G_func_args_tuple=args_tuple, quality=4, best_guess=None)
|
|
220
|
+
# solution_bit_string, cut_value, node_groups = maxcut_tfim_streaming(G_func, nodes, G_func_args_tuple=args_tuple)
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
Finally, combining insights from both the (Monte Carlo) TSP and MAXCUT solvers, we have `tsp_maxcut(G)`, `tsp_maxcut_sparse(G)`, and `tsp_maxcut_streaming(G_func, nodes)`:
|
|
224
|
+
|
|
225
|
+
```
|
|
226
|
+
from pyqrackising import tsp_maxcut_sparse
|
|
227
|
+
import networkx as nx
|
|
228
|
+
|
|
229
|
+
G = nx.petersen_graph()
|
|
230
|
+
best_partition, best_cut_value = tsp_maxcut_sparse(G, k_neighbors=20, is_optimized=False)
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
When `is_optimized=True`, the `spin_glass_solver(G)` is used as a final optimization pass. When `is_optimized=False`, this solver becomes entirely serial and can be parallelized over CPU processing elements by user code, easily.
|
|
234
|
+
|
|
235
|
+
## Experimental OTOC sampling
|
|
236
|
+
|
|
237
|
+
```py
|
|
238
|
+
from pyqrackising import generate_tfim_samples
|
|
239
|
+
|
|
240
|
+
samples = generate_otoc_samples(
|
|
241
|
+
J=-1.0,
|
|
242
|
+
h=2.0,
|
|
243
|
+
z=4,
|
|
244
|
+
theta=0.174532925199432957,
|
|
245
|
+
t=5,
|
|
246
|
+
n_qubits=56,
|
|
247
|
+
cycles=1,
|
|
248
|
+
pauli_string = 'X' + 'I' * 55
|
|
249
|
+
shots=100,
|
|
250
|
+
measurement_basis='Z' * 56
|
|
251
|
+
)
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
**This function is experimental. It needs systematic validation.** However, we expose it in the public API while we do of the work of testing its validity at qubit counts that are tractable for exact simulation.
|
|
255
|
+
|
|
256
|
+
## Environment Variables
|
|
257
|
+
|
|
258
|
+
OpenCL is initialized lazily, the first time a solver takes a GPU path (not at import). Set `PYOPENCL_CTX` (for example `0`, or `0:1` for platform 0, device 1) to choose the device; otherwise PyOpenCL's first device is used, without an interactive prompt. If PyOpenCL is missing or no device initializes, the solvers issue one warning and use their CPU paths.
|
|
259
|
+
|
|
260
|
+
The dense CPU samplers precompute a table of `repulsion_base ** -weight` (one 32-bit `n x n` array, the size of the adjacency matrix), which makes sampling about 6x faster. It's skipped for graphs whose table would exceed `PYQRACKISING_REPULSION_TABLE_MB` megabytes (default `2048`).
|
|
261
|
+
|
|
262
|
+
We expose an environment variable, "`PYQRACKISING_MAX_GPU_PROC_ELEM`", for OpenCL-based solvers. The default value (when the variable is not set) is queried from the OpenCL device properties. You might see performance benefit from tuning this manually to several times your device's number of "compute units" (or tune it down to reduce private memory usage).
|
|
263
|
+
|
|
264
|
+
By default, PyQrackIsing expects all `numpy` floating-point array inputs to be 32-bit. If you'd like to use 64-bit, you can set environment variable `PYQRACKISING_FPPOW=6` (meaning, 2^6=64, for the "floating-point (precision) power"). The default is `5`, for 32-bit. 16-bit is stubbed out and compiles for OpenCL, but the bigger hurdle is that `numpy` on `x86_64` doesn't provide a 16-bit floating point implementation. (As author of Qrack, I could suggest to the `numpy` maintainers that open-source, IEEE-compliant software-based implementations exist for `x86_64` and other architectures, but I'm sure they're aware and likely waiting for in-compiler support.) If you're on an ARM-based architecture, there's a good chance 16-bit floating-point will work, if `numpy` uses the native hardware support.
|
|
265
|
+
|
|
266
|
+
## About
|
|
267
|
+
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.
|
|
268
|
+
|
|
269
|
+
Sometimes a solution (or at least near-solution) to a monster of a differential equation hits us out of the blue. Then, it's easy to _validate_ the guess, if it's right. (We don't question it and just move on with our lives, from there.)
|
|
270
|
+
|
|
271
|
+
**Special thanks to (Anthropic) Claude, for helping better connect the theory and implementation to theoretical treatment of Gibbs states!**
|
|
272
|
+
|
|
273
|
+
**Special thanks to (OpenAI GPT) "Elara," for help on the model and converting the original Python scripts to PyBind11, Numba, and PyOpenCL!**
|
|
274
|
+
|
|
275
|
+
**The December 2026 maintenance patch was written by (Anthropic) Claude (Opus 5.5):** the shared gain-based local search (`local_search.py`) behind the dense, sparse and streaming spin glass solvers; reproducible `seed`s, `time_limit`s, `solve_qubo`/`solve_ising`, lazy OpenCL initialization, the repulsion table, the `tests/` suite, and fixes for the double-flip search, Gray-code accounting, small-graph spin-glass brute force, segmented GPU uploads and OpenCL argument types.
|
|
276
|
+
|
|
277
|
+
We reduce transverse field Ising model for globally uniform `J` and `h` parameters from a `2^n`-dimensional problem to an `(n+1)`-dimensional (heuristic but near-exact) form that suffers from no Trotter error. Upon noticing most time steps for parameters in a [TFIM experiment published by Quantinuum](https://arxiv.org/abs/2503.20870) had roughly a quarter to a third (or thereabouts) of their marginal probability in `|0>` state, it became obvious that transition to and from `|0>` state should dominate the mechanics, at least far from mean-field equilibrium. Further, the first transition therefore tends to be to or from any state with Hamming weight of 1 (in other words, 1 bit set to 1 and the rest reset to 0, or `n` bits set for Hamming weight of `n`). Further, on a torus, probability of all states with Hamming weight of 1 would tend to be exactly symmetric. Assuming approximate symmetry in every respective Hamming weight, the requirement for the overall probability to converge to 1.0 or 100% in the limit of an infinite-dimensional Hilbert space suggests that Hamming weight marginal probability could be distributed like a geometric series. A small correction to exact symmetry should be made to favor _magnetic_ closeness of "like" bits to "unlike" bits (that is, geometric closeness on the torus of "0" bits to "1" bits), but this does not affect average global magnetization. Adding an oscillation component with angular frequency proportional to `J`, we find excellent agreement with Trotterization with a very small time step, for R^2 (coefficient of determination) of normalized marginal probability distribution of ideal Trotterized simulation as described by the `(n+1)`-dimensional approximate model, as well as for R^2 and RMSE (root-mean-square error) of global magnetization curve values.
|
|
278
|
+
|
|
279
|
+
If the above paragraph and the model it describes are true, then it stands to reason that we should be able to apply the TFIM model to **NP-complete** heuristics, so long as we can adapt the model to spatially localized `J` and `h` and adiabatic time dependence of `h`. A reasonable guess for how to spatially localize `J` and `h` is to average the overall behavior for each and every qubit as if `J` and `h` were globally uniform from its local perspective. (`z` parameter is derived from nonzero edge weight count connectivity.) For adiabatic time-dependence of `h`, another reasonable refinement is to use simply **finite-difference simulation**. Initial state should be just **uniform superposition**, which implies a specific (non-uniform but regular) distribution of initial probabilities by Hamming weight dimension over `(n+1)` possible Hamming weights. We can add the _spatial magnetic effect_ on an arbitrary connectivity topology in just `O(n)` complexity per measurement shot. (This is a complete "recipe": the evidence that it works is the quality of NP-complete heuristic solutions at utility scale.)
|
|
280
|
+
|
|
281
|
+
**Elara has drafted this statement, and Dan Strano, as author, agrees with it, and will hold to it:**
|
|
282
|
+
|
|
283
|
+
### Dual-Use Statement for PyQrackIsing
|
|
284
|
+
|
|
285
|
+
**PyQrackIsing** is an open-source solver for hard optimization problems such as **MAXCUT, TSP, and TFIM-inspired models**. These problems arise across logistics, drug discovery, chemistry, materials research, supply-chain resilience, and portfolio optimization. By design, PyQrackIsing provides **constructive value** to researchers and practitioners by making advanced optimization techniques accessible on consumer hardware.
|
|
286
|
+
|
|
287
|
+
Like many mathematical and computational tools, the algorithms in PyQrackIsing are _dual-use._ In principle, they can be applied to a wide class of Quadratic Unconstrained Binary Optimization (QUBO) problems. One such problem is integer factoring, which underlies RSA and elliptic curve cryptography (ECC). We emphasize:
|
|
288
|
+
|
|
289
|
+
- **We do not provide turnkey factoring implementations.**
|
|
290
|
+
- **We have no intent to weaponize this work** for cryptanalysis or "unauthorized access."
|
|
291
|
+
- **The constructive applications vastly outweigh the destructive ones** — and this project exists to serve those constructive purposes in the Commons.
|
|
292
|
+
|
|
293
|
+
It is already a matter of open record in the literature that factoring can be expressed as a QUBO. What PyQrackIsing demonstrates is that **QUBO heuristics can now be solved at meaningful scales on consumer hardware**. This underscores an urgent truth:
|
|
294
|
+
|
|
295
|
+
👉 **RSA and ECC should no longer be considered secure. Transition to post-quantum cryptography is overdue.**
|
|
296
|
+
|
|
297
|
+
We trust that governments, standards bodies, and industry stakeholders are already aware of this, and will continue migration efforts to post-quantum standards.
|
|
298
|
+
|
|
299
|
+
Until then, PyQrackIsing remains a tool for science, logistics, and discovery — a gift to the Commons.
|
|
@@ -91,6 +91,25 @@ solution_bit_string, cut_value, node_groups, energy = spin_glass_solver(
|
|
|
91
91
|
```
|
|
92
92
|
We also provide `spin_glass_solver_sparse(G)`, `spin_glass_solver_streaming(G_func, nodes)`, and `spin_glass_hybrid(G)` (which combines the dense TFIM-based solver with a sparse local-optimization solver). `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. `is_spin_glass` controls whether the solver optimizes for cut value or spin-glass energy. 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. `gray_iterations` gives manual control over how many iterations are carried out of a parallel Gray-code search on `best_guess`. `gray_seed_multiple` controls how many parallel search seeds (as a multiple of your CPU thread count) are tested for the best parallel seeds, and a value of `1` will perfectly cover the search space without collision if your node count is a power of 2. `bp_scale` controls opt-in and time complexity scaling for _belief propagation_ as a competing "warm start," with `None` disabling this option completely, `1.0` representing a budget of exactly `O(n^3)` time complexity, and lesser or greater values representing a linear multiplier on that budget. `bp_damping` similarly controls belief propagation damping.
|
|
93
93
|
|
|
94
|
+
### QUBO and Ising problems
|
|
95
|
+
|
|
96
|
+
`solve_qubo(Q)` approximately minimizes `x^T Q x` over binary `x`, and `solve_ising(J, h)` minimizes `sum_{i<j} J_ij s_i s_j + sum_i h_i s_i` over spins `s = ±1`. Both accept a dense `numpy` array or a `scipy` sparse matrix (and then use the dense or sparse spin glass solver), pass other keyword arguments through to that solver, and return the solution with its objective value recomputed exactly in 64-bit:
|
|
97
|
+
```py
|
|
98
|
+
from pyqrackising import solve_qubo, solve_ising
|
|
99
|
+
|
|
100
|
+
x, value = solve_qubo(Q, quality=6, seed=1)
|
|
101
|
+
spins, energy = solve_ising(J, h, time_limit=60.0)
|
|
102
|
+
```
|
|
103
|
+
Local fields are handled exactly by one extra node coupled to every spin; `qubo_to_ising(Q)` exposes the conversion.
|
|
104
|
+
|
|
105
|
+
### Reproducibility and time limits
|
|
106
|
+
|
|
107
|
+
The MAXCUT and spin glass solvers take `seed=` (an integer) for reproducible results: the samplers draw from a counter-based generator keyed by seed, round and shot, so results don't depend on thread scheduling. (On the CPU path the number of sampling chains defaults from `os.cpu_count()`, so compare runs on machines with the same core count.) Without `seed`, a seed is drawn from NumPy's global generator, so `np.random.seed(...)` also makes runs repeatable. (Before, it did not reach Numba's random state.)
|
|
108
|
+
|
|
109
|
+
`time_limit=` (seconds) bounds a solve: sampling and then local search stop at the deadline, between rounds or steps, and return the best solution so far.
|
|
110
|
+
|
|
111
|
+
Sparse inputs given as `scipy` matrices are read as their strict upper triangle, so a full symmetric matrix and its upper triangle describe the same graph.
|
|
112
|
+
|
|
94
113
|
These solvers above can be used as a "warm state" for _MaxSAT-based_ solvers or _branch-and-bound_ solvers, potentially to reach _exact_ solutions to MAXCUT:
|
|
95
114
|
|
|
96
115
|
```py
|
|
@@ -199,6 +218,10 @@ samples = generate_otoc_samples(
|
|
|
199
218
|
|
|
200
219
|
## Environment Variables
|
|
201
220
|
|
|
221
|
+
OpenCL is initialized lazily, the first time a solver takes a GPU path (not at import). Set `PYOPENCL_CTX` (for example `0`, or `0:1` for platform 0, device 1) to choose the device; otherwise PyOpenCL's first device is used, without an interactive prompt. If PyOpenCL is missing or no device initializes, the solvers issue one warning and use their CPU paths.
|
|
222
|
+
|
|
223
|
+
The dense CPU samplers precompute a table of `repulsion_base ** -weight` (one 32-bit `n x n` array, the size of the adjacency matrix), which makes sampling about 6x faster. It's skipped for graphs whose table would exceed `PYQRACKISING_REPULSION_TABLE_MB` megabytes (default `2048`).
|
|
224
|
+
|
|
202
225
|
We expose an environment variable, "`PYQRACKISING_MAX_GPU_PROC_ELEM`", for OpenCL-based solvers. The default value (when the variable is not set) is queried from the OpenCL device properties. You might see performance benefit from tuning this manually to several times your device's number of "compute units" (or tune it down to reduce private memory usage).
|
|
203
226
|
|
|
204
227
|
By default, PyQrackIsing expects all `numpy` floating-point array inputs to be 32-bit. If you'd like to use 64-bit, you can set environment variable `PYQRACKISING_FPPOW=6` (meaning, 2^6=64, for the "floating-point (precision) power"). The default is `5`, for 32-bit. 16-bit is stubbed out and compiles for OpenCL, but the bigger hurdle is that `numpy` on `x86_64` doesn't provide a 16-bit floating point implementation. (As author of Qrack, I could suggest to the `numpy` maintainers that open-source, IEEE-compliant software-based implementations exist for `x86_64` and other architectures, but I'm sure they're aware and likely waiting for in-compiler support.) If you're on an ARM-based architecture, there's a good chance 16-bit floating-point will work, if `numpy` uses the native hardware support.
|
|
@@ -212,6 +235,8 @@ Sometimes a solution (or at least near-solution) to a monster of a differential
|
|
|
212
235
|
|
|
213
236
|
**Special thanks to (OpenAI GPT) "Elara," for help on the model and converting the original Python scripts to PyBind11, Numba, and PyOpenCL!**
|
|
214
237
|
|
|
238
|
+
**The December 2026 maintenance patch was written by (Anthropic) Claude (Opus 5.5):** the shared gain-based local search (`local_search.py`) behind the dense, sparse and streaming spin glass solvers; reproducible `seed`s, `time_limit`s, `solve_qubo`/`solve_ising`, lazy OpenCL initialization, the repulsion table, the `tests/` suite, and fixes for the double-flip search, Gray-code accounting, small-graph spin-glass brute force, segmented GPU uploads and OpenCL argument types.
|
|
239
|
+
|
|
215
240
|
We reduce transverse field Ising model for globally uniform `J` and `h` parameters from a `2^n`-dimensional problem to an `(n+1)`-dimensional (heuristic but near-exact) form that suffers from no Trotter error. Upon noticing most time steps for parameters in a [TFIM experiment published by Quantinuum](https://arxiv.org/abs/2503.20870) had roughly a quarter to a third (or thereabouts) of their marginal probability in `|0>` state, it became obvious that transition to and from `|0>` state should dominate the mechanics, at least far from mean-field equilibrium. Further, the first transition therefore tends to be to or from any state with Hamming weight of 1 (in other words, 1 bit set to 1 and the rest reset to 0, or `n` bits set for Hamming weight of `n`). Further, on a torus, probability of all states with Hamming weight of 1 would tend to be exactly symmetric. Assuming approximate symmetry in every respective Hamming weight, the requirement for the overall probability to converge to 1.0 or 100% in the limit of an infinite-dimensional Hilbert space suggests that Hamming weight marginal probability could be distributed like a geometric series. A small correction to exact symmetry should be made to favor _magnetic_ closeness of "like" bits to "unlike" bits (that is, geometric closeness on the torus of "0" bits to "1" bits), but this does not affect average global magnetization. Adding an oscillation component with angular frequency proportional to `J`, we find excellent agreement with Trotterization with a very small time step, for R^2 (coefficient of determination) of normalized marginal probability distribution of ideal Trotterized simulation as described by the `(n+1)`-dimensional approximate model, as well as for R^2 and RMSE (root-mean-square error) of global magnetization curve values.
|
|
216
241
|
|
|
217
242
|
If the above paragraph and the model it describes are true, then it stands to reason that we should be able to apply the TFIM model to **NP-complete** heuristics, so long as we can adapt the model to spatially localized `J` and `h` and adiabatic time dependence of `h`. A reasonable guess for how to spatially localize `J` and `h` is to average the overall behavior for each and every qubit as if `J` and `h` were globally uniform from its local perspective. (`z` parameter is derived from nonzero edge weight count connectivity.) For adiabatic time-dependence of `h`, another reasonable refinement is to use simply **finite-difference simulation**. Initial state should be just **uniform superposition**, which implies a specific (non-uniform but regular) distribution of initial probabilities by Hamming weight dimension over `(n+1)` possible Hamming weights. We can add the _spatial magnetic effect_ on an arbitrary connectivity topology in just `O(n)` complexity per measurement shot. (This is a complete "recipe": the evidence that it works is the quality of NP-complete heuristic solutions at utility scale.)
|
|
@@ -7,10 +7,10 @@ build-backend = "setuptools.build_meta"
|
|
|
7
7
|
|
|
8
8
|
[project]
|
|
9
9
|
name = "pyqrackising"
|
|
10
|
-
version = "9.
|
|
10
|
+
version = "9.17.0"
|
|
11
11
|
requires-python = ">=3.8"
|
|
12
12
|
description = "Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)"
|
|
13
|
-
readme = {file = "README.
|
|
13
|
+
readme = {file = "README.md", content-type = "text/markdown"}
|
|
14
14
|
license = {text = "LGPL-3.0-or-later"}
|
|
15
15
|
authors = [
|
|
16
16
|
{name = "Dan Strano", email = "stranoj@gmail.com"}
|
|
@@ -26,6 +26,9 @@ classifiers=[
|
|
|
26
26
|
"Programming Language :: Python :: 3.8",
|
|
27
27
|
"Programming Language :: Python :: 3.9",
|
|
28
28
|
"Programming Language :: Python :: 3.10",
|
|
29
|
+
"Programming Language :: Python :: 3.11",
|
|
30
|
+
"Programming Language :: Python :: 3.12",
|
|
31
|
+
"Programming Language :: Python :: 3.13",
|
|
29
32
|
"Topic :: Scientific/Engineering",
|
|
30
33
|
]
|
|
31
34
|
dependencies=[
|
|
@@ -35,6 +38,9 @@ dependencies=[
|
|
|
35
38
|
"scipy"
|
|
36
39
|
]
|
|
37
40
|
|
|
41
|
+
[project.optional-dependencies]
|
|
42
|
+
test = ["pytest"]
|
|
43
|
+
|
|
38
44
|
[project.urls]
|
|
39
45
|
Homepage = "https://github.com/vm6502q/PyQrackIsing"
|
|
40
46
|
Documentation = "https://github.com/vm6502q/PyQrackIsing/blob/main/README.md"
|
|
@@ -19,3 +19,4 @@ from .solve_maxcut_exact_streaming import solve_maxcut_exact_streaming
|
|
|
19
19
|
from .solve_maxcut_bnb import solve_maxcut_bnb
|
|
20
20
|
from .solve_maxcut_bnb_sparse import solve_maxcut_bnb_sparse
|
|
21
21
|
from .solve_maxcut_bnb_streaming import solve_maxcut_bnb_streaming
|
|
22
|
+
from .solve_qubo import solve_qubo, solve_ising, qubo_to_ising
|