PyQrackIsing 9.15.0__tar.gz → 9.15.2__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.
Files changed (35) hide show
  1. {pyqrackising-9.15.0/pyqrackising.egg-info → pyqrackising-9.15.2}/PKG-INFO +1 -1
  2. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyproject.toml +1 -1
  3. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising/generate_tfim_samples.py +62 -131
  4. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising/maxcut_tfim_util.py +51 -1
  5. {pyqrackising-9.15.0 → pyqrackising-9.15.2/pyqrackising.egg-info}/PKG-INFO +1 -1
  6. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/setup.py +1 -1
  7. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/LICENSE.md +0 -0
  8. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/MANIFEST.in +0 -0
  9. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/README.md +0 -0
  10. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising/__init__.py +0 -0
  11. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
  12. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising/kernels.cl +0 -0
  13. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising/maxcut_tfim.py +0 -0
  14. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising/maxcut_tfim_sparse.py +0 -0
  15. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising/maxcut_tfim_streaming.py +0 -0
  16. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising/otoc.py +0 -0
  17. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising/solve_maxcut_bnb.py +0 -0
  18. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising/solve_maxcut_bnb_sparse.py +0 -0
  19. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising/solve_maxcut_bnb_streaming.py +0 -0
  20. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising/solve_maxcut_exact.py +0 -0
  21. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising/solve_maxcut_exact_sparse.py +0 -0
  22. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising/solve_maxcut_exact_streaming.py +0 -0
  23. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising/spin_glass_solver.py +0 -0
  24. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising/spin_glass_solver_sparse.py +0 -0
  25. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising/spin_glass_solver_streaming.py +0 -0
  26. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising/tfim_magnetization.py +0 -0
  27. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising/tfim_square_magnetization.py +0 -0
  28. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising/tsp.py +0 -0
  29. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising/tsp_maxcut.py +0 -0
  30. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising.egg-info/SOURCES.txt +0 -0
  31. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising.egg-info/dependency_links.txt +0 -0
  32. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising.egg-info/not-zip-safe +0 -0
  33. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising.egg-info/requires.txt +0 -0
  34. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/pyqrackising.egg-info/top_level.txt +0 -0
  35. {pyqrackising-9.15.0 → pyqrackising-9.15.2}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 9.15.0
3
+ Version: 9.15.2
4
4
  Summary: Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)
5
5
  Home-page: https://github.com/vm6502q/PyQrackIsing
6
6
  Author: Dan Strano
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
7
7
 
8
8
  [project]
9
9
  name = "pyqrackising"
10
- version = "9.15.0"
10
+ version = "9.15.2"
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"}
@@ -49,7 +49,8 @@ from collections import Counter
49
49
  epsilon = opencl_context.epsilon
50
50
 
51
51
 
52
- # Fixed-Hamming-weight Kawasaki swaps with acceptance probability, by Elara
52
+ # ── Geometry ───────────────────────────────────────────────────────────────────
53
+
53
54
  def random_fixed_hamming_state(n_qubits, h):
54
55
  bits = np.zeros(n_qubits, dtype=np.bool_)
55
56
  bits[:h] = True
@@ -59,116 +60,100 @@ def random_fixed_hamming_state(n_qubits, h):
59
60
  state <<= 1
60
61
  if b:
61
62
  state |= 1
62
-
63
63
  return state
64
64
 
65
65
 
66
66
  def build_neighbors(n_rows, n_cols):
67
67
  L = n_rows * n_cols
68
68
  neighbors = [[] for _ in range(L)]
69
-
70
69
  for i in range(n_rows):
71
70
  for j in range(n_cols):
72
71
  idx = i * n_cols + j
73
-
74
72
  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
-
73
+ left = i * n_cols + ((j - 1) % n_cols)
74
+ down = ((i + 1) % n_rows) * n_cols + j
75
+ up = ((i - 1) % n_rows) * n_cols + j
79
76
  neighbors[idx] = [right, left, down, up]
80
-
81
77
  return neighbors
82
78
 
83
79
 
84
- def count_like_edges(state, neighbors):
85
- like = 0
80
+ def count_unlike_edges(state, neighbors):
81
+ unlike = 0
86
82
  visited = set()
87
-
88
83
  for i, nbrs in enumerate(neighbors):
89
84
  si = (state >> i) & 1
90
85
  for j in nbrs:
91
86
  if (j, i) in visited:
92
87
  continue
93
- sj = (state >> j) & 1
94
- if si == sj:
95
- like += 1
88
+ if si != ((state >> j) & 1):
89
+ unlike += 1
96
90
  visited.add((i, j))
97
-
98
- return like
91
+ return unlike
99
92
 
100
93
 
101
94
  def delta_like_edges(state, i, j, neighbors):
102
95
  si = (state >> i) & 1
103
96
  sj = (state >> j) & 1
104
-
105
97
  delta = 0
106
-
107
98
  for idx in [i, j]:
108
99
  s_old = (state >> idx) & 1
109
-
110
100
  for nbr in neighbors[idx]:
111
101
  if nbr == i or nbr == j:
112
102
  continue
113
-
114
- s_nbr = (state >> nbr) & 1
115
-
103
+ s_nbr = (state >> nbr) & 1
116
104
  old_like = s_old == s_nbr
117
105
  new_spin = sj if idx == i else si
118
106
  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
107
+ delta += int(new_like) - int(old_like)
123
108
  if j in neighbors[i]:
124
- old_like = si == sj
125
- new_like = sj == si
126
- delta += int(new_like) - int(old_like)
127
-
109
+ delta += int(sj == si) - int(si == sj)
128
110
  return delta
129
111
 
130
112
 
113
+ # ── Spatial sampler: repulsive Kawasaki ───────────────────────────────────────
114
+ # Electrons carry intrinsic magnetic dipole moments; antiparallel (unlike)
115
+ # neighbours have lower magnetic potential energy. The acceptance criterion
116
+ # therefore unconditionally favours unlike neighbours, independent of J.
117
+
131
118
  def sample_fixed_hamming_weight(h_weight, count, n_rows, n_cols, burnin=10):
132
119
  if count == 0:
133
120
  return []
134
121
 
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
122
+ n_qubits = n_rows * n_cols
123
+ neighbors = build_neighbors(n_rows, n_cols)
124
+ total_edges = 2 * n_qubits # 4 neighbours per site, each edge once → 2n
125
+
126
+ unlike_edges = 0
127
+ attempts = 0
128
+ while unlike_edges == 0:
129
+ state = random_fixed_hamming_state(n_qubits, h_weight)
130
+ unlike_edges = count_unlike_edges(state, neighbors)
131
+ attempts += 1
132
+ if attempts > 1000:
133
+ return [state] * count
134
+
135
+ samples = []
136
+ burn = burnin * n_qubits
146
137
  thinning = n_qubits
147
138
 
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])
139
+ ones = set(i for i in range(n_qubits) if (state >> i) & 1)
140
+ zeros = set(i for i in range(n_qubits) if not (state >> i) & 1)
150
141
 
151
142
  for step in range(burn + thinning * count):
152
- # choose random 1-bit and 0-bit
153
-
154
143
  i = random.choice(tuple(ones))
155
144
  j = random.choice(tuple(zeros))
156
145
 
157
- delta = delta_like_edges(state, i, j, neighbors)
158
- new_like = like_edges + delta
146
+ delta_like = delta_like_edges(state, i, j, neighbors)
147
+ new_unlike = unlike_edges - delta_like # unlike = total - like
159
148
 
160
- if new_like > 0:
161
- accept_prob = new_like / like_edges
162
- if np.random.random() < accept_prob:
149
+ if new_unlike > 0:
150
+ # Repulsive: accept proportional to new_unlike / unlike_edges
151
+ if np.random.random() < new_unlike / unlike_edges:
163
152
  state ^= 1 << i
164
- ones.remove(i)
165
- zeros.add(i)
166
-
153
+ ones.remove(i); zeros.add(i)
167
154
  state ^= 1 << j
168
- zeros.remove(j)
169
- ones.add(j)
170
-
171
- like_edges = new_like
155
+ zeros.remove(j); ones.add(j)
156
+ unlike_edges = new_unlike
172
157
 
173
158
  if step >= burn and (step - burn) % thinning == 0:
174
159
  samples.append(state)
@@ -176,90 +161,40 @@ def sample_fixed_hamming_weight(h_weight, count, n_rows, n_cols, burnin=10):
176
161
  return samples
177
162
 
178
163
 
179
- @njit
164
+ # ── Numba helpers ──────────────────────────────────────────────────────────────
165
+
166
+ @njit(cache=True)
180
167
  def factor_width(width, is_transpose=False):
181
168
  col_len = math.floor(math.sqrt(width))
182
169
  while ((width // col_len) * col_len) != width:
183
170
  col_len -= 1
184
171
  row_len = width // col_len
185
-
186
172
  return (col_len, row_len) if is_transpose else (row_len, col_len)
187
173
 
188
174
 
189
- @njit
175
+ @njit(cache=True)
190
176
  def comb(n, k):
191
- if (k < 0) or (k > n):
192
- return 0
193
- if (k == 0) or (k == n):
194
- return 1
177
+ if (k < 0) or (k > n): return 0
178
+ if (k == 0) or (k == n): return 1
195
179
  res = 1
196
180
  for i in range(1, k + 1):
197
181
  res = res * (n - k + i) // i
198
182
  return res
199
183
 
200
184
 
201
- @njit
185
+ @njit(cache=True)
202
186
  def fix_cdf(hamming_prob):
203
187
  tot_prob = 0.0
204
- n_bias = len(hamming_prob)
188
+ n_bias = len(hamming_prob)
205
189
  cum_prob = np.empty(n_bias, dtype=np.float64)
206
190
  for i in range(n_bias):
207
- tot_prob += hamming_prob[i]
208
- cum_prob[i] = tot_prob
191
+ tot_prob += hamming_prob[i]
192
+ cum_prob[i] = tot_prob
209
193
  cum_prob[-1] = 1.0
210
-
211
194
  return cum_prob
212
195
 
213
196
 
214
- @njit
215
- def _apply_time_envelope(bias, t, n_qubits):
216
- """
217
- Multiply bias[k] by exp(-|k - n/2| / max(t, epsilon)) for each k.
218
-
219
- This is the time-dependent Hamming weight envelope:
220
- - At small t: peaks sharply at half-filling k = n/2 (AFM sector).
221
- - At large t: envelope -> 1, recovering the original bias exactly.
222
-
223
- The differential effect is greatest at initialization (small t),
224
- which is where the ESD / adiabatic physics lives.
225
-
226
- alpha = 1.0 is hardcoded as the natural physical invariant.
227
- It is the least contrived value that produces the correct qualitative
228
- behaviour, and is left fixed rather than tuned empirically.
229
-
230
- Authors: Claude (Anthropic) and D. Strano
231
- """
232
- alpha = 1.0
233
- half = n_qubits / 2.0
234
- # Numba-safe floor of t for the epsilon guard
235
- t_eff = t if t > 1e-9 else 1e-9
236
-
237
- out = np.empty(n_qubits + 1, dtype=np.float64)
238
- tot = 0.0
239
- for k in range(n_qubits + 1):
240
- dist = k - half
241
- if dist < 0.0:
242
- dist = -dist
243
- envelope = math.exp(-alpha * dist / t_eff)
244
- val = bias[k] * envelope
245
- out[k] = val
246
- tot += val
247
-
248
- if tot <= 0.0:
249
- # Pathological underflow: collapse to half-filling delta
250
- for k in range(n_qubits + 1):
251
- out[k] = 0.0
252
- mid = n_qubits // 2
253
- out[mid] = 1.0
254
- return out
255
-
256
- for k in range(n_qubits + 1):
257
- out[k] /= tot
258
-
259
- return out
260
-
261
-
262
- @njit
197
+ @njit(cache=True)
263
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):
264
199
  if abs(t) <= epsilon:
265
200
  p = (1.0 - np.cos(theta)) / 2.0
@@ -277,12 +212,10 @@ def get_tfim_hamming_distribution(J=-1.0, h=2.0, z=4, theta=0.174532925199432957
277
212
  bias[0] = 1.0
278
213
  return bias
279
214
 
280
- bias = probability_by_hamming_weight(J, h, z, theta, t, n_qubits + 1, normalized=True, omega=omega)
215
+ return probability_by_hamming_weight(J, h, z, theta, t, n_qubits + 1, normalized=True, omega=omega)
281
216
 
282
- # Apply time-dependent envelope: maximum differential at small t,
283
- # recovers original distribution as t -> inf.
284
- return _apply_time_envelope(bias / bias.sum(), t, n_qubits)
285
217
 
218
+ # ── Public generators ──────────────────────────────────────────────────────────
286
219
 
287
220
  def generate_tfim_samples(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n_qubits=56, shots=100):
288
221
  samples = []
@@ -296,13 +229,12 @@ def generate_tfim_samples(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n
296
229
  if np.random.random() < prob:
297
230
  sample |= 1
298
231
  samples.append(sample)
299
-
300
232
  return samples
301
233
 
302
234
  n_rows, n_cols = factor_width(n_qubits)
303
235
 
304
- # First dimension: Hamming weight (envelope applied inside)
305
- bias = get_tfim_hamming_distribution(J=J, h=h, z=z, theta=theta, t=t, n_qubits=n_qubits)
236
+ # Dimension 1: Hamming weight with time-dependent envelope
237
+ bias = get_tfim_hamming_distribution(J=J, h=h, z=z, theta=theta, t=t, n_qubits=n_qubits)
306
238
  counts = np.random.multinomial(shots, bias)
307
239
 
308
240
  samples += [0] * counts[0]
@@ -310,22 +242,21 @@ def generate_tfim_samples(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n
310
242
  if n_qubits > 1:
311
243
  samples += [int(1 << np.random.randint(n_qubits)) for _ in range(counts[1])]
312
244
  if n_qubits > 2:
313
- mask = (1 << n_qubits) - 1
245
+ mask = (1 << n_qubits) - 1
314
246
  samples += [(mask ^ int(1 << np.random.randint(n_qubits))) for _ in range(counts[-2])]
315
247
 
316
- # Second dimension: magnetic localization
248
+ # Dimension 2: spatial magnetic localization (repulsive dipole coupling)
317
249
  for h_weight in range(2, len(bias) - 2):
318
250
  samples += sample_fixed_hamming_weight(h_weight, counts[h_weight], n_rows, n_cols)
319
251
 
320
252
  np.random.shuffle(samples)
321
-
322
253
  return samples
323
254
 
324
255
 
325
256
  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):
326
257
  shots_x, shots_y, shots_z = np.random.multinomial(shots, [1 / 3, 1 / 3, 1 / 3])
327
258
  return (
328
- generate_tfim_samples(J=J, h=h, z=z, theta=theta, t=t, n_qubits=n_qubits, shots=shots_z)
259
+ generate_tfim_samples(J=J, h=h, z=z, theta=theta, t=t, n_qubits=n_qubits, shots=shots_z)
329
260
  + generate_tfim_samples(J=-h, h=-J, z=z, theta=theta + np.pi / 2, t=t, n_qubits=n_qubits, shots=shots_x)
330
- + generate_tfim_samples(J=J, h=h, z=z, theta=theta + np.pi / 2, t=t, n_qubits=n_qubits, shots=shots_y)
261
+ + generate_tfim_samples(J=J, h=h, z=z, theta=theta + np.pi / 2, t=t, n_qubits=n_qubits, shots=shots_y)
331
262
  )
@@ -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
- return bias
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)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 9.15.0
3
+ Version: 9.15.2
4
4
  Summary: Fast MAXCUT, TSP, and sampling heuristics from near-ideal transverse field Ising model (TFIM)
5
5
  Home-page: https://github.com/vm6502q/PyQrackIsing
6
6
  Author: Dan Strano
@@ -7,7 +7,7 @@ with open(README_PATH) as readme_file:
7
7
 
8
8
  setup(
9
9
  name="pyqrackising",
10
- version="9.15.0",
10
+ version="9.15.2",
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