PyQrackIsing 9.17.0__tar.gz → 9.17.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.
- {pyqrackising-9.17.0/pyqrackising.egg-info → pyqrackising-9.17.1}/PKG-INFO +1 -1
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyproject.toml +1 -1
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/generate_tfim_samples.py +2 -4
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/local_search.py +2 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/maxcut_tfim_util.py +34 -5
- {pyqrackising-9.17.0 → pyqrackising-9.17.1/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/setup.py +1 -1
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/tests/test_pyqrackising.py +23 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/LICENSE.md +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/MANIFEST.in +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/README.md +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/__init__.py +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/maxcut_tfim.py +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/maxcut_tfim_sparse.py +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/maxcut_tfim_streaming.py +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/otoc.py +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/solve_maxcut_bnb.py +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/solve_maxcut_bnb_sparse.py +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/solve_maxcut_bnb_streaming.py +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/solve_maxcut_exact.py +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/solve_maxcut_exact_sparse.py +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/solve_maxcut_exact_streaming.py +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/solve_qubo.py +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/spin_glass_solver.py +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/spin_glass_solver_hybrid.py +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/spin_glass_solver_sparse.py +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/spin_glass_solver_streaming.py +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/tsp.py +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising/tsp_maxcut.py +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising.egg-info/requires.txt +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-9.17.0 → pyqrackising-9.17.1}/setup.cfg +0 -0
|
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
|
|
|
7
7
|
|
|
8
8
|
[project]
|
|
9
9
|
name = "pyqrackising"
|
|
10
|
-
version = "9.17.
|
|
10
|
+
version = "9.17.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.md", content-type = "text/markdown"}
|
|
@@ -35,7 +35,7 @@ thermalizing upward, rather than starting in the paramagnetic phase.
|
|
|
35
35
|
Authors: Claude (Anthropic), Elara (OpenAI custom GPT), and D. Strano
|
|
36
36
|
"""
|
|
37
37
|
|
|
38
|
-
from .maxcut_tfim_util import probability_by_hamming_weight, sample_mag, opencl_context
|
|
38
|
+
from .maxcut_tfim_util import binomial_pmf, probability_by_hamming_weight, sample_mag, opencl_context
|
|
39
39
|
import itertools
|
|
40
40
|
import math
|
|
41
41
|
import random
|
|
@@ -198,9 +198,7 @@ def fix_cdf(hamming_prob):
|
|
|
198
198
|
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):
|
|
199
199
|
if abs(t) <= epsilon:
|
|
200
200
|
p = (1.0 - np.cos(theta)) / 2.0
|
|
201
|
-
bias =
|
|
202
|
-
for k in range(n_qubits + 1):
|
|
203
|
-
bias[k] = comb(n_qubits, k) * (p**k) * ((1.0 - p) ** (n_qubits - k))
|
|
201
|
+
bias = binomial_pmf(n_qubits, p) # (int64 comb() wrapped around from n ~ 60)
|
|
204
202
|
|
|
205
203
|
return bias / bias.sum()
|
|
206
204
|
|
|
@@ -294,6 +294,8 @@ def gray_optimization(iterators, deltas, gray_iterations, is_spin_glass, A, data
|
|
|
294
294
|
for curr_idx in range(thread_iterations):
|
|
295
295
|
for block in range(blocks):
|
|
296
296
|
flip_bit = gray_code_next(iterator, curr_idx, block << 6)
|
|
297
|
+
if flip_bit < 0:
|
|
298
|
+
continue # past the end of a partial last block
|
|
297
299
|
# gain of the flip just made = -(gain of flipping it back)
|
|
298
300
|
d = -scale * flip_gain(flip_bit, iterator, A, data, rows, cols, G_func, nodes)
|
|
299
301
|
if d > 0.0:
|
|
@@ -603,10 +603,29 @@ def init_theta(h_mult, n_qubits, J_eff, degrees):
|
|
|
603
603
|
return theta
|
|
604
604
|
|
|
605
605
|
|
|
606
|
+
@njit(cache=True)
|
|
607
|
+
def binomial_pmf(n, p):
|
|
608
|
+
"""[C(n, k) p^k (1 - p)^(n - k) for k = 0..n], computed in log space so it
|
|
609
|
+
neither overflows (math.comb(n, k) exceeds float range from n ~ 1030) nor
|
|
610
|
+
wraps (int64 binomials, from n ~ 60). Underflowing tails become 0."""
|
|
611
|
+
out = np.zeros(n + 1, dtype=np.float64)
|
|
612
|
+
if p <= 0.0:
|
|
613
|
+
out[0] = 1.0
|
|
614
|
+
return out
|
|
615
|
+
if p >= 1.0:
|
|
616
|
+
out[n] = 1.0
|
|
617
|
+
return out
|
|
618
|
+
lp = math.log(p)
|
|
619
|
+
lq = math.log1p(-p)
|
|
620
|
+
lgn = math.lgamma(n + 1.0)
|
|
621
|
+
for k in range(n + 1):
|
|
622
|
+
out[k] = math.exp(lgn - math.lgamma(k + 1.0) - math.lgamma(n - k + 1.0) + k * lp + (n - k) * lq)
|
|
623
|
+
return out
|
|
624
|
+
|
|
625
|
+
|
|
606
626
|
def init_thresholds(n_qubits, theta=0.0):
|
|
607
|
-
n_bias = n_qubits + 1
|
|
608
627
|
p = (1.0 - np.sin(theta)) / 2.0
|
|
609
|
-
return
|
|
628
|
+
return binomial_pmf(int(n_qubits), float(p))
|
|
610
629
|
|
|
611
630
|
|
|
612
631
|
@njit(cache=True)
|
|
@@ -774,6 +793,10 @@ def gray_code_next(state, curr_idx, offset):
|
|
|
774
793
|
curr = curr ^ (curr >> 1)
|
|
775
794
|
diff = prev ^ curr
|
|
776
795
|
flip_bit = int(np.log2(diff)) + offset
|
|
796
|
+
if flip_bit >= state.shape[0]:
|
|
797
|
+
# Past the end of a partial last block (n not a multiple of 64):
|
|
798
|
+
# flip nothing. (This used to write out of bounds.)
|
|
799
|
+
return -1
|
|
777
800
|
state[flip_bit] = not state[flip_bit]
|
|
778
801
|
|
|
779
802
|
return flip_bit
|
|
@@ -781,11 +804,17 @@ def gray_code_next(state, curr_idx, offset):
|
|
|
781
804
|
|
|
782
805
|
@njit(cache=True)
|
|
783
806
|
def gray_mutation(index, seed_bits, offset):
|
|
784
|
-
"""Apply Gray-code-indexed bit flips to a seed bitstring
|
|
807
|
+
"""Apply Gray-code-indexed bit flips to a seed bitstring, within the
|
|
808
|
+
64-bit block at offset (counted from the end of the bitstring)."""
|
|
785
809
|
n = seed_bits.shape[0]
|
|
786
810
|
gray = index ^ (index >> 1)
|
|
787
811
|
bits = seed_bits.copy()
|
|
788
|
-
|
|
789
|
-
|
|
812
|
+
# Only the block's 64 bits: shifting by >= 64 is undefined in LLVM, and a
|
|
813
|
+
# negative index would wrap around to the other end of the array.
|
|
814
|
+
for i in range(64):
|
|
815
|
+
j = n - offset - 1 - i
|
|
816
|
+
if j < 0:
|
|
817
|
+
break
|
|
818
|
+
bits[j] ^= (gray >> i) & 1
|
|
790
819
|
|
|
791
820
|
return bits
|
|
@@ -7,7 +7,7 @@ with open(README_PATH) as readme_file:
|
|
|
7
7
|
|
|
8
8
|
setup(
|
|
9
9
|
name="pyqrackising",
|
|
10
|
-
version="9.17.
|
|
10
|
+
version="9.17.1",
|
|
11
11
|
author="Dan Strano",
|
|
12
12
|
author_email="stranoj@gmail.com",
|
|
13
13
|
description="Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)",
|
|
@@ -168,6 +168,29 @@ def test_small_spin_glass_exact_path_finds_the_optimum(solver):
|
|
|
168
168
|
assert compute_energy(th, D, 12) == pytest.approx(best)
|
|
169
169
|
|
|
170
170
|
|
|
171
|
+
def test_gray_helpers_stay_in_bounds_for_partial_last_block():
|
|
172
|
+
# n = 70: the last 64-bit block holds only 6 bits. gray_code_next used to
|
|
173
|
+
# flip index offset + log2(diff) >= n (an out-of-bounds write; it crashed
|
|
174
|
+
# spin_glass_solver_hybrid on G22), and gray_mutation shifted by >= 64.
|
|
175
|
+
from pyqrackising.maxcut_tfim_util import gray_code_next, gray_mutation
|
|
176
|
+
state = np.zeros(70, np.bool_)
|
|
177
|
+
assert gray_code_next(state, 63, 64) == -1 and not state.any() # would flip bit 70
|
|
178
|
+
assert gray_code_next(state, 0, 64) == 64 and state[64]
|
|
179
|
+
rng = np.random.default_rng(0)
|
|
180
|
+
seed_bits = rng.random(70) < 0.5
|
|
181
|
+
for idx in (0, 1, 5, 2**40 + 3, 2**62 - 1):
|
|
182
|
+
for offset in (0, 64):
|
|
183
|
+
got = gray_mutation(idx, seed_bits, offset)
|
|
184
|
+
ref = seed_bits.copy()
|
|
185
|
+
g = idx ^ (idx >> 1)
|
|
186
|
+
for i in range(64):
|
|
187
|
+
j = 70 - offset - 1 - i
|
|
188
|
+
if j < 0:
|
|
189
|
+
break
|
|
190
|
+
ref[j] ^= bool((g >> i) & 1)
|
|
191
|
+
assert np.array_equal(got, ref)
|
|
192
|
+
|
|
193
|
+
|
|
171
194
|
# --------------------------------------------------------------- gain-based sparse local search
|
|
172
195
|
|
|
173
196
|
@pytest.mark.parametrize("is_spin_glass", [False, True])
|
|
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
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|