PyQrackIsing 9.14.0__tar.gz → 9.15.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.14.0/pyqrackising.egg-info → pyqrackising-9.15.1}/PKG-INFO +1 -1
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/README.md +5 -3
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyproject.toml +1 -1
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising/generate_tfim_samples.py +279 -244
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising/maxcut_tfim_util.py +51 -1
- {pyqrackising-9.14.0 → pyqrackising-9.15.1/pyqrackising.egg-info}/PKG-INFO +1 -1
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/setup.py +1 -1
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/LICENSE.md +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/MANIFEST.in +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising/__init__.py +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising/kernels.cl +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising/maxcut_tfim.py +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising/maxcut_tfim_sparse.py +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising/maxcut_tfim_streaming.py +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising/otoc.py +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising/solve_maxcut_bnb.py +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising/solve_maxcut_bnb_sparse.py +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising/solve_maxcut_bnb_streaming.py +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising/solve_maxcut_exact.py +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising/solve_maxcut_exact_sparse.py +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising/solve_maxcut_exact_streaming.py +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising/spin_glass_solver.py +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising/spin_glass_solver_sparse.py +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising/spin_glass_solver_streaming.py +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising/tfim_magnetization.py +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising/tfim_square_magnetization.py +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising/tsp.py +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising/tsp_maxcut.py +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising.egg-info/SOURCES.txt +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising.egg-info/dependency_links.txt +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising.egg-info/not-zip-safe +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising.egg-info/requires.txt +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/pyqrackising.egg-info/top_level.txt +0 -0
- {pyqrackising-9.14.0 → pyqrackising-9.15.1}/setup.cfg +0 -0
|
@@ -208,11 +208,13 @@ Transverse field Ising model (TFIM) is the basis of most claimed algorithmic "qu
|
|
|
208
208
|
|
|
209
209
|
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.)
|
|
210
210
|
|
|
211
|
-
**Special thanks to
|
|
211
|
+
**Special thanks to (Anthropic) Claude, for helping better connect the theory and implementation to theoretical treatment of Gibbs states!**
|
|
212
212
|
|
|
213
|
-
|
|
213
|
+
**Special thanks to (OpenAI GPT) "Elara," for help on the model and converting the original Python scripts to PyBind11, Numba, and PyOpenCL!**
|
|
214
214
|
|
|
215
|
-
|
|
215
|
+
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
|
+
|
|
217
|
+
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.)
|
|
216
218
|
|
|
217
219
|
**Elara has drafted this statement, and Dan Strano, as author, agrees with it, and will hold to it:**
|
|
218
220
|
|
|
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
|
|
|
7
7
|
|
|
8
8
|
[project]
|
|
9
9
|
name = "pyqrackising"
|
|
10
|
-
version = "9.
|
|
10
|
+
version = "9.15.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.txt", content-type = "text/markdown"}
|
|
@@ -1,244 +1,279 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
from
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
for
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
1
|
+
"""
|
|
2
|
+
generate_tfim_samples.py (Envelope variant)
|
|
3
|
+
==================================
|
|
4
|
+
Drop-in replacement for generate_tfim_samples with a time-dependent
|
|
5
|
+
exponential envelope applied to the Hamming weight marginal.
|
|
6
|
+
|
|
7
|
+
The key insight
|
|
8
|
+
---------------
|
|
9
|
+
The Hamming weight marginal from probability_by_hamming_weight is what
|
|
10
|
+
pins <O_bond> ~ +1.0 in the ESD sweep — not the spatial Kawasaki dynamics.
|
|
11
|
+
The marginal at h >> |J| is dominated by high-Hamming-weight (X-polarized)
|
|
12
|
+
states at all t, which produce positive correlations in all three bases.
|
|
13
|
+
|
|
14
|
+
The fix: multiply the marginal by an envelope that is *largest at small t*
|
|
15
|
+
and decays toward uniform as t grows:
|
|
16
|
+
|
|
17
|
+
bias[k] *= exp(-alpha * |k - n/2| / t)
|
|
18
|
+
|
|
19
|
+
At t -> 0: envelope peaks sharply at half-filling (k = n/2), the
|
|
20
|
+
antiferromagnetic ground state sector. The spatial dynamics
|
|
21
|
+
(repulsive Kawasaki) then has maximum leverage.
|
|
22
|
+
|
|
23
|
+
At t -> inf: envelope -> 1 everywhere; the physics-derived marginal
|
|
24
|
+
from probability_by_hamming_weight is recovered exactly.
|
|
25
|
+
|
|
26
|
+
The differential is greatest at initialization — exactly where the ESD
|
|
27
|
+
interesting physics lives — and fades as the system equilibrates toward
|
|
28
|
+
the Gibbs target. alpha controls the strength of the initial bias.
|
|
29
|
+
|
|
30
|
+
This is physically motivated: the AFM ground state of the XXZ model
|
|
31
|
+
is at half-filling on a bipartite lattice, so biasing toward k=n/2
|
|
32
|
+
at early times is consistent with starting near the ground state and
|
|
33
|
+
thermalizing upward, rather than starting in the paramagnetic phase.
|
|
34
|
+
|
|
35
|
+
Authors: Claude (Anthropic), Elara (OpenAI custom GPT), and D. Strano
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
from .maxcut_tfim_util import probability_by_hamming_weight, sample_mag, opencl_context
|
|
39
|
+
import itertools
|
|
40
|
+
import math
|
|
41
|
+
import random
|
|
42
|
+
import sys
|
|
43
|
+
import numpy as np
|
|
44
|
+
from numba import njit
|
|
45
|
+
|
|
46
|
+
from collections import Counter
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
epsilon = opencl_context.epsilon
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
# Fixed-Hamming-weight Kawasaki swaps with acceptance probability, by Elara
|
|
53
|
+
def random_fixed_hamming_state(n_qubits, h):
|
|
54
|
+
bits = np.zeros(n_qubits, dtype=np.bool_)
|
|
55
|
+
bits[:h] = True
|
|
56
|
+
np.random.shuffle(bits)
|
|
57
|
+
state = 0
|
|
58
|
+
for b in bits:
|
|
59
|
+
state <<= 1
|
|
60
|
+
if b:
|
|
61
|
+
state |= 1
|
|
62
|
+
|
|
63
|
+
return state
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def build_neighbors(n_rows, n_cols):
|
|
67
|
+
L = n_rows * n_cols
|
|
68
|
+
neighbors = [[] for _ in range(L)]
|
|
69
|
+
|
|
70
|
+
for i in range(n_rows):
|
|
71
|
+
for j in range(n_cols):
|
|
72
|
+
idx = i * n_cols + j
|
|
73
|
+
|
|
74
|
+
right = i * n_cols + ((j + 1) % n_cols)
|
|
75
|
+
left = i * n_cols + ((j - 1) % n_cols)
|
|
76
|
+
down = ((i + 1) % n_rows) * n_cols + j
|
|
77
|
+
up = ((i - 1) % n_rows) * n_cols + j
|
|
78
|
+
|
|
79
|
+
neighbors[idx] = [right, left, down, up]
|
|
80
|
+
|
|
81
|
+
return neighbors
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def count_like_edges(state, neighbors):
|
|
85
|
+
like = 0
|
|
86
|
+
visited = set()
|
|
87
|
+
|
|
88
|
+
for i, nbrs in enumerate(neighbors):
|
|
89
|
+
si = (state >> i) & 1
|
|
90
|
+
for j in nbrs:
|
|
91
|
+
if (j, i) in visited:
|
|
92
|
+
continue
|
|
93
|
+
sj = (state >> j) & 1
|
|
94
|
+
if si == sj:
|
|
95
|
+
like += 1
|
|
96
|
+
visited.add((i, j))
|
|
97
|
+
|
|
98
|
+
return like
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def delta_like_edges(state, i, j, neighbors):
|
|
102
|
+
si = (state >> i) & 1
|
|
103
|
+
sj = (state >> j) & 1
|
|
104
|
+
|
|
105
|
+
delta = 0
|
|
106
|
+
|
|
107
|
+
for idx in [i, j]:
|
|
108
|
+
s_old = (state >> idx) & 1
|
|
109
|
+
|
|
110
|
+
for nbr in neighbors[idx]:
|
|
111
|
+
if nbr == i or nbr == j:
|
|
112
|
+
continue
|
|
113
|
+
|
|
114
|
+
s_nbr = (state >> nbr) & 1
|
|
115
|
+
|
|
116
|
+
old_like = s_old == s_nbr
|
|
117
|
+
new_spin = sj if idx == i else si
|
|
118
|
+
new_like = new_spin == s_nbr
|
|
119
|
+
|
|
120
|
+
delta += int(new_like) - int(old_like)
|
|
121
|
+
|
|
122
|
+
# handle edge between i and j if neighbors
|
|
123
|
+
if j in neighbors[i]:
|
|
124
|
+
old_like = si == sj
|
|
125
|
+
new_like = sj == si
|
|
126
|
+
delta += int(new_like) - int(old_like)
|
|
127
|
+
|
|
128
|
+
return delta
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def sample_fixed_hamming_weight(h_weight, count, n_rows, n_cols, burnin=10):
|
|
132
|
+
if count == 0:
|
|
133
|
+
return []
|
|
134
|
+
|
|
135
|
+
n_qubits = n_rows * n_cols
|
|
136
|
+
neighbors = build_neighbors(n_rows, n_cols)
|
|
137
|
+
|
|
138
|
+
like_edges = 0
|
|
139
|
+
while like_edges == 0:
|
|
140
|
+
state = random_fixed_hamming_state(n_qubits, h_weight)
|
|
141
|
+
like_edges = count_like_edges(state, neighbors)
|
|
142
|
+
|
|
143
|
+
samples = []
|
|
144
|
+
|
|
145
|
+
burn = burnin * n_qubits
|
|
146
|
+
thinning = n_qubits
|
|
147
|
+
|
|
148
|
+
ones = set([i for i in range(n_qubits) if (state >> i) & 1])
|
|
149
|
+
zeros = set([i for i in range(n_qubits) if not (state >> i) & 1])
|
|
150
|
+
|
|
151
|
+
for step in range(burn + thinning * count):
|
|
152
|
+
# choose random 1-bit and 0-bit
|
|
153
|
+
|
|
154
|
+
i = random.choice(tuple(ones))
|
|
155
|
+
j = random.choice(tuple(zeros))
|
|
156
|
+
|
|
157
|
+
delta = delta_like_edges(state, i, j, neighbors)
|
|
158
|
+
new_like = like_edges + delta
|
|
159
|
+
|
|
160
|
+
if new_like > 0:
|
|
161
|
+
accept_prob = new_like / like_edges
|
|
162
|
+
if np.random.random() < accept_prob:
|
|
163
|
+
state ^= 1 << i
|
|
164
|
+
ones.remove(i)
|
|
165
|
+
zeros.add(i)
|
|
166
|
+
|
|
167
|
+
state ^= 1 << j
|
|
168
|
+
zeros.remove(j)
|
|
169
|
+
ones.add(j)
|
|
170
|
+
|
|
171
|
+
like_edges = new_like
|
|
172
|
+
|
|
173
|
+
if step >= burn and (step - burn) % thinning == 0:
|
|
174
|
+
samples.append(state)
|
|
175
|
+
|
|
176
|
+
return samples
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
@njit(cache=True)
|
|
180
|
+
def factor_width(width, is_transpose=False):
|
|
181
|
+
col_len = math.floor(math.sqrt(width))
|
|
182
|
+
while ((width // col_len) * col_len) != width:
|
|
183
|
+
col_len -= 1
|
|
184
|
+
row_len = width // col_len
|
|
185
|
+
|
|
186
|
+
return (col_len, row_len) if is_transpose else (row_len, col_len)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
@njit(cache=True)
|
|
190
|
+
def comb(n, k):
|
|
191
|
+
if (k < 0) or (k > n):
|
|
192
|
+
return 0
|
|
193
|
+
if (k == 0) or (k == n):
|
|
194
|
+
return 1
|
|
195
|
+
res = 1
|
|
196
|
+
for i in range(1, k + 1):
|
|
197
|
+
res = res * (n - k + i) // i
|
|
198
|
+
return res
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
@njit(cache=True)
|
|
202
|
+
def fix_cdf(hamming_prob):
|
|
203
|
+
tot_prob = 0.0
|
|
204
|
+
n_bias = len(hamming_prob)
|
|
205
|
+
cum_prob = np.empty(n_bias, dtype=np.float64)
|
|
206
|
+
for i in range(n_bias):
|
|
207
|
+
tot_prob += hamming_prob[i]
|
|
208
|
+
cum_prob[i] = tot_prob
|
|
209
|
+
cum_prob[-1] = 1.0
|
|
210
|
+
|
|
211
|
+
return cum_prob
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
@njit(cache=True)
|
|
215
|
+
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):
|
|
216
|
+
if abs(t) <= epsilon:
|
|
217
|
+
p = (1.0 - np.cos(theta)) / 2.0
|
|
218
|
+
bias = np.empty(n_qubits + 1, dtype=np.float64)
|
|
219
|
+
for k in range(n_qubits + 1):
|
|
220
|
+
bias[k] = comb(n_qubits, k) * (p**k) * ((1.0 - p) ** (n_qubits - k))
|
|
221
|
+
|
|
222
|
+
return bias / bias.sum()
|
|
223
|
+
|
|
224
|
+
if abs(h) <= epsilon:
|
|
225
|
+
bias = np.empty(n_qubits + 1, dtype=np.float64)
|
|
226
|
+
if J > 0:
|
|
227
|
+
bias[-1] = 1.0
|
|
228
|
+
else:
|
|
229
|
+
bias[0] = 1.0
|
|
230
|
+
return bias
|
|
231
|
+
|
|
232
|
+
return probability_by_hamming_weight(J, h, z, theta, t, n_qubits + 1, normalized=True, omega=omega)
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
def generate_tfim_samples(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, shots=100):
|
|
236
|
+
samples = []
|
|
237
|
+
|
|
238
|
+
if abs(t) <= epsilon:
|
|
239
|
+
prob = (1.0 - np.cos(theta)) / 2.0
|
|
240
|
+
for s in range(shots):
|
|
241
|
+
sample = 0
|
|
242
|
+
for q in range(n_qubits):
|
|
243
|
+
sample <<= 1
|
|
244
|
+
if np.random.random() < prob:
|
|
245
|
+
sample |= 1
|
|
246
|
+
samples.append(sample)
|
|
247
|
+
|
|
248
|
+
return samples
|
|
249
|
+
|
|
250
|
+
n_rows, n_cols = factor_width(n_qubits)
|
|
251
|
+
|
|
252
|
+
# First dimension: Hamming weight (envelope applied inside)
|
|
253
|
+
bias = get_tfim_hamming_distribution(J=J, h=h, z=z, theta=theta, t=t, n_qubits=n_qubits)
|
|
254
|
+
counts = np.random.multinomial(shots, bias)
|
|
255
|
+
|
|
256
|
+
samples += [0] * counts[0]
|
|
257
|
+
samples += [(1 << n_qubits) - 1] * counts[-1]
|
|
258
|
+
if n_qubits > 1:
|
|
259
|
+
samples += [int(1 << np.random.randint(n_qubits)) for _ in range(counts[1])]
|
|
260
|
+
if n_qubits > 2:
|
|
261
|
+
mask = (1 << n_qubits) - 1
|
|
262
|
+
samples += [(mask ^ int(1 << np.random.randint(n_qubits))) for _ in range(counts[-2])]
|
|
263
|
+
|
|
264
|
+
# Second dimension: magnetic localization
|
|
265
|
+
for h_weight in range(2, len(bias) - 2):
|
|
266
|
+
samples += sample_fixed_hamming_weight(h_weight, counts[h_weight], n_rows, n_cols)
|
|
267
|
+
|
|
268
|
+
np.random.shuffle(samples)
|
|
269
|
+
|
|
270
|
+
return samples
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def generate_fermi_hubbard_samples(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, shots=100, omega=1.5 * np.pi):
|
|
274
|
+
shots_x, shots_y, shots_z = np.random.multinomial(shots, [1 / 3, 1 / 3, 1 / 3])
|
|
275
|
+
return (
|
|
276
|
+
generate_tfim_samples(J=J, h=h, z=z, theta=theta, t=t, n_qubits=n_qubits, shots=shots_z)
|
|
277
|
+
+ generate_tfim_samples(J=-h, h=-J, z=z, theta=theta + np.pi / 2, t=t, n_qubits=n_qubits, shots=shots_x)
|
|
278
|
+
+ generate_tfim_samples(J=J, h=h, z=z, theta=theta + np.pi / 2, t=t, n_qubits=n_qubits, shots=shots_y)
|
|
279
|
+
)
|
|
@@ -617,6 +617,54 @@ def init_thresholds(n_qubits):
|
|
|
617
617
|
return thresholds
|
|
618
618
|
|
|
619
619
|
|
|
620
|
+
@njit(cache=True)
|
|
621
|
+
def _apply_time_envelope(bias, t, n_qubits):
|
|
622
|
+
"""
|
|
623
|
+
Multiply bias[k] by exp(-|k - n/2| / max(t, epsilon)) for each k.
|
|
624
|
+
|
|
625
|
+
This is the time-dependent Hamming weight envelope:
|
|
626
|
+
- At small t: peaks sharply at half-filling k = n/2 (AFM sector).
|
|
627
|
+
- At large t: envelope -> 1, recovering the original bias exactly.
|
|
628
|
+
|
|
629
|
+
The differential effect is greatest at initialization (small t),
|
|
630
|
+
which is where the ESD / adiabatic physics lives.
|
|
631
|
+
|
|
632
|
+
alpha = 1.0 is hardcoded as the natural physical invariant.
|
|
633
|
+
It is the least contrived value that produces the correct qualitative
|
|
634
|
+
behaviour, and is left fixed rather than tuned empirically.
|
|
635
|
+
|
|
636
|
+
Authors: Claude (Anthropic) and D. Strano
|
|
637
|
+
"""
|
|
638
|
+
alpha = 1.0
|
|
639
|
+
half = n_qubits / 2.0
|
|
640
|
+
# Numba-safe floor of t for the epsilon guard
|
|
641
|
+
t_eff = t if t > 1e-9 else 1e-9
|
|
642
|
+
|
|
643
|
+
out = np.empty(n_qubits + 1, dtype=np.float64)
|
|
644
|
+
tot = 0.0
|
|
645
|
+
for k in range(n_qubits + 1):
|
|
646
|
+
dist = k - half
|
|
647
|
+
if dist < 0.0:
|
|
648
|
+
dist = -dist
|
|
649
|
+
envelope = math.exp(-alpha * dist / t_eff)
|
|
650
|
+
val = bias[k] * envelope
|
|
651
|
+
out[k] = val
|
|
652
|
+
tot += val
|
|
653
|
+
|
|
654
|
+
if tot <= 0.0:
|
|
655
|
+
# Pathological underflow: collapse to half-filling delta
|
|
656
|
+
for k in range(n_qubits + 1):
|
|
657
|
+
out[k] = 0.0
|
|
658
|
+
mid = n_qubits // 2
|
|
659
|
+
out[mid] = 1.0
|
|
660
|
+
return out
|
|
661
|
+
|
|
662
|
+
for k in range(n_qubits + 1):
|
|
663
|
+
out[k] /= tot
|
|
664
|
+
|
|
665
|
+
return out
|
|
666
|
+
|
|
667
|
+
|
|
620
668
|
@njit(cache=True)
|
|
621
669
|
def probability_by_hamming_weight(J, h, z, theta, t, n_bias, normalized=True, omega=1.5 * np.pi):
|
|
622
670
|
zJ = z * J
|
|
@@ -640,7 +688,9 @@ def probability_by_hamming_weight(J, h, z, theta, t, n_bias, normalized=True, om
|
|
|
640
688
|
if normalized:
|
|
641
689
|
bias /= bias.sum()
|
|
642
690
|
|
|
643
|
-
|
|
691
|
+
# Apply time-dependent envelope: maximum differential at small t,
|
|
692
|
+
# recovers original distribution as t -> inf.
|
|
693
|
+
return _apply_time_envelope(bias / bias.sum(), t, n_bias - 1)
|
|
644
694
|
|
|
645
695
|
|
|
646
696
|
@njit(cache=True)
|
|
@@ -7,7 +7,7 @@ with open(README_PATH) as readme_file:
|
|
|
7
7
|
|
|
8
8
|
setup(
|
|
9
9
|
name="pyqrackising",
|
|
10
|
-
version="9.
|
|
10
|
+
version="9.15.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)",
|
|
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
|