PyQrackIsing 9.16.4__tar.gz → 9.16.6__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 (38) hide show
  1. {pyqrackising-9.16.4/pyqrackising.egg-info → pyqrackising-9.16.6}/PKG-INFO +1 -1
  2. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyproject.toml +1 -1
  3. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising/generate_tfim_samples.py +65 -12
  4. {pyqrackising-9.16.4 → pyqrackising-9.16.6/pyqrackising.egg-info}/PKG-INFO +1 -1
  5. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising.egg-info/SOURCES.txt +0 -2
  6. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/setup.py +1 -1
  7. pyqrackising-9.16.4/pyqrackising/fermi_hubbard_validation_qiskit.py +0 -278
  8. pyqrackising-9.16.4/pyqrackising/fermi_hubbard_validation_qrack.py +0 -207
  9. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/LICENSE.md +0 -0
  10. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/MANIFEST.in +0 -0
  11. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/README.md +0 -0
  12. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising/__init__.py +0 -0
  13. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising/convert_tensor_network_to_tsp.py +0 -0
  14. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising/kernels.cl +0 -0
  15. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising/maxcut_tfim.py +0 -0
  16. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising/maxcut_tfim_sparse.py +0 -0
  17. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising/maxcut_tfim_streaming.py +0 -0
  18. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising/maxcut_tfim_util.py +0 -0
  19. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising/otoc.py +0 -0
  20. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising/solve_maxcut_bnb.py +0 -0
  21. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising/solve_maxcut_bnb_sparse.py +0 -0
  22. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising/solve_maxcut_bnb_streaming.py +0 -0
  23. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising/solve_maxcut_exact.py +0 -0
  24. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising/solve_maxcut_exact_sparse.py +0 -0
  25. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising/solve_maxcut_exact_streaming.py +0 -0
  26. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising/spin_glass_solver.py +0 -0
  27. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising/spin_glass_solver_hybrid.py +0 -0
  28. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising/spin_glass_solver_sparse.py +0 -0
  29. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising/spin_glass_solver_streaming.py +0 -0
  30. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising/tfim_magnetization.py +0 -0
  31. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising/tfim_square_magnetization.py +0 -0
  32. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising/tsp.py +0 -0
  33. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising/tsp_maxcut.py +0 -0
  34. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising.egg-info/dependency_links.txt +0 -0
  35. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising.egg-info/not-zip-safe +0 -0
  36. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising.egg-info/requires.txt +0 -0
  37. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/pyqrackising.egg-info/top_level.txt +0 -0
  38. {pyqrackising-9.16.4 → pyqrackising-9.16.6}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 9.16.4
3
+ Version: 9.16.6
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.16.4"
10
+ version = "9.16.6"
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"}
@@ -252,22 +252,66 @@ def generate_tfim_samples(J=-1.0, h=2.0, z=4, theta=0.174532925199432957, t=5, n
252
252
  np.random.shuffle(samples)
253
253
  return samples
254
254
 
255
- # Fermi-Hubbard "v2" completely by (Anthropic) Claude Sonnet 5
255
+ # Fermi-Hubbard correction, by (Anthropic) Claude Sonnet 5, per D. Strano's
256
+ # direction on the final functional form.
257
+ #
258
+ # beta(t) = A * cos(omega(h,J) * J * t + theta) / (1 + sqrt(t))
259
+ #
260
+ # STRUCTURAL PRINCIPLE (D. Strano): this reuses the EXACT decay envelope
261
+ # (1/(1+sqrt(t))) and phase convention (cos(...+theta), where theta is the
262
+ # same state-prep angle already passed into every call, not a free-fit
263
+ # parameter) from the base TFIM ansatz's own p(t) formula above. The
264
+ # rationale for preferring this over a numerically-tuned decay exponent
265
+ # (informally fit to ~1.29 across the same small sweep used to validate
266
+ # this file) is that sqrt(t) is not tuned to this sweep at all -- it's
267
+ # borrowed from a form already validated at scale elsewhere in this
268
+ # codebase, so it carries evidence beyond this file's own small validation
269
+ # set, where a locally-tuned exponent risks just re-overfitting the same
270
+ # way A_peak's old bump-in-r formula did.
271
+ #
272
+ # WHY theta AS PHASE, NOT A FREE phi: the previous revision's free-fit phi
273
+ # was severely confounded with A and tau (A varied >3x across a clean
274
+ # r-sweep despite R^2>0.95 in every individual fit -- a classic sign of an
275
+ # unidentifiable parameterization, not real physics). Reusing theta
276
+ # directly (matching the base ansatz's own convention) recovers most of
277
+ # what free-fit phi was doing -- R^2=0.85 vs 0.96 with 2 fewer free
278
+ # parameters -- and collapses A from a >200%-relative-spread quantity to
279
+ # ~5% across the same two independent sweeps (varying h at fixed |J|, and
280
+ # varying |J| at fixed h/(zJ)). A is treated as a constant here for that
281
+ # reason: its own residual variation, once the phase is properly assigned
282
+ # to theta, is small enough to not be worth another free parameter.
283
+ #
284
+ # HONEST VALIDATION STATUS: end-to-end (through the full generator, scored
285
+ # against the ideal Heisenberg+field circuit) this form was measured to
286
+ # underperform the previous revision's (A_of_r(r), free phi=-1.5, free
287
+ # tau=1/3) formula at every h tested in this file's small validation sweep
288
+ # (h=0.5, 2.0, 4.0; roughly 25-30% relative XEB R^2 loss). That gap is
289
+ # disclosed, not hidden -- the case for shipping this anyway is that a
290
+ # small local validation sweep is itself prone to rewarding whichever
291
+ # formula best matches its own quirks, and a formula built from already-
292
+ # validated structure (rather than fit to this sweep) is judged more
293
+ # likely to generalize correctly outside it. This is a judgment call, not
294
+ # a resolved empirical result -- flagged plainly for anyone re-deriving
295
+ # this in the future.
296
+ #
297
+ # omega(h, J) = 2h/|J| -- unchanged from the previous revision, confirmed
298
+ # via two independent sweeps (varying h at fixed |J|, and varying |J| at
299
+ # fixed h/|J|), both converging on the same relationship independently.
300
+
301
+ # A = -29.0 / 4.0
302
+ # (Dan says, let's try "-7.0" or "-8.0," friend.)
303
+ A = -7.0
256
304
 
257
- # "Nice" constants: omega = 4, phi = -3/2, tau = 1/3
258
- # These are actually a "guess" to move from FITTED to analytical closed-form beta(t)
259
- omega_b, phi, tau = 4.0, -1.5, (1.0 / 3.0)
260
- A_peak = 12.0
261
305
 
262
306
  @njit(cache=True)
263
- def A_of_r(J, h, z):
264
- r = h / (z * J)
265
- return A_peak * 4 * abs(r) / (1 + abs(r))**2
307
+ def omega_of_h_J(h, J):
308
+ return (2 ** 24) if np.isclose(J, 0) else (2.0 * h / abs(J))
266
309
 
267
310
 
268
311
  @njit(cache=True)
269
- def beta_formula(t, J, h, z):
270
- return A_of_r(J, h, z) * np.sin(omega_b * J * t + phi) / (1.0 + (t / tau))
312
+ def beta_formula(t, J, h, z, theta):
313
+ omega_b = omega_of_h_J(h, J)
314
+ return A * np.cos(omega_b * J * t + theta) / (1.0 + np.sqrt(t))
271
315
 
272
316
 
273
317
  @njit(cache=True)
@@ -288,12 +332,21 @@ def sample_from_bias(bias, shots, n_qubits):
288
332
  mask = (1 << n_qubits) - 1
289
333
  samples += [(mask ^ int(1 << np.random.randint(n_qubits))) for _ in range(counts[-2])]
290
334
  for hw in range(2, len(bias) - 2):
291
- samples += sample_fixed_hamming_weight(hw, counts[hw], n_rows, n_cols)
335
+ samples += [random_fixed_hamming_state(n_qubits, hw) for _ in range(counts[hw])]
292
336
  np.random.shuffle(samples)
293
337
  return samples
294
338
 
295
339
 
296
340
  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):
297
341
  z_bias = get_tfim_hamming_distribution(J=J, h=h, z=z, theta=theta, t=t, n_qubits=n_qubits)
298
- return sample_from_bias(tilt(z_bias, beta_formula(t, J, h, z), n_qubits), shots, n_qubits)
342
+ if abs(t) <= epsilon:
343
+ # At t=0 the base TFIM ansatz is already exact (a simple product
344
+ # state under the initial rotation, no time evolution yet). The
345
+ # beta(t) correction is a decaying oscillation fit to t >= dt data
346
+ # only, and is NOT constrained to (and does not, as fit) vanish at
347
+ # t=0 -- applying it there would corrupt an already-exact result.
348
+ # See the discussion above beta_formula for why this isn't a minor
349
+ # rounding matter: beta(0) evaluates to order -A, not order 0.
350
+ return sample_from_bias(z_bias, shots, n_qubits)
351
+ return sample_from_bias(tilt(z_bias, beta_formula(t, J, h, z, theta), n_qubits), shots, n_qubits)
299
352
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyqrackising
3
- Version: 9.16.4
3
+ Version: 9.16.6
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
@@ -5,8 +5,6 @@ pyproject.toml
5
5
  setup.py
6
6
  pyqrackising/__init__.py
7
7
  pyqrackising/convert_tensor_network_to_tsp.py
8
- pyqrackising/fermi_hubbard_validation_qiskit.py
9
- pyqrackising/fermi_hubbard_validation_qrack.py
10
8
  pyqrackising/generate_tfim_samples.py
11
9
  pyqrackising/kernels.cl
12
10
  pyqrackising/maxcut_tfim.py
@@ -7,7 +7,7 @@ with open(README_PATH) as readme_file:
7
7
 
8
8
  setup(
9
9
  name="pyqrackising",
10
- version="9.16.4",
10
+ version="9.16.6",
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)",
@@ -1,278 +0,0 @@
1
- import math
2
- import os
3
- import sys
4
- import statistics
5
- import time
6
-
7
- from collections import Counter
8
-
9
- import numpy as np
10
-
11
- from scipy.stats import distributions as dists
12
-
13
- import matplotlib.pyplot as plt
14
-
15
- from qiskit import QuantumCircuit
16
- from qiskit.circuit.library import RZZGate
17
- from qiskit.compiler import transpile
18
- from qiskit_aer.backends import AerSimulator
19
- from qiskit.quantum_info import Statevector
20
-
21
- from pyqrack import QrackSimulator
22
- from pyqrackising import generate_fermi_hubbard_samples
23
-
24
-
25
- def factor_width(width, is_transpose=False):
26
- col_len = math.floor(math.sqrt(width))
27
- while ((width // col_len) * col_len) != width:
28
- col_len -= 1
29
- row_len = width // col_len
30
-
31
- return (col_len, row_len) if is_transpose else (row_len, col_len)
32
-
33
-
34
- def index(i, j, n_cols):
35
- return i * n_cols + j
36
-
37
-
38
- def zz_rotation(qc, q1, q2, theta):
39
- # Implements exp(-i theta Z⊗Z)
40
- qc.cx(q1, q2)
41
- qc.rz(2 * theta, q2)
42
- qc.cx(q1, q2)
43
-
44
- def first_order_tfim(qc, n_rows, n_cols, J, h, dt):
45
- theta_zz = J * dt
46
-
47
- # ---- Horizontal even bonds ----
48
- for i in range(n_rows):
49
- for j in range(0, n_cols - 1, 2):
50
- q1 = index(i, j, n_cols)
51
- q2 = index(i, j + 1, n_cols)
52
- zz_rotation(qc, q1, q2, theta_zz)
53
-
54
- # ---- Horizontal odd bonds ----
55
- for i in range(n_rows):
56
- for j in range(1, n_cols - 1, 2):
57
- q1 = index(i, j, n_cols)
58
- q2 = index(i, j + 1, n_cols)
59
- zz_rotation(qc, q1, q2, theta_zz)
60
-
61
- # ---- Vertical even bonds ----
62
- for j in range(n_cols):
63
- for i in range(0, n_rows - 1, 2):
64
- q1 = index(i, j, n_cols)
65
- q2 = index(i + 1, j, n_cols)
66
- zz_rotation(qc, q1, q2, theta_zz)
67
-
68
- # ---- Vertical odd bonds ----
69
- for j in range(n_cols):
70
- for i in range(1, n_rows - 1, 2):
71
- q1 = index(i, j, n_cols)
72
- q2 = index(i + 1, j, n_cols)
73
- zz_rotation(qc, q1, q2, theta_zz)
74
-
75
- def brick_wall_tfim_step(n_rows, n_cols, J, h, dt):
76
- """
77
- Single first-order Trotter step for 2D TFIM
78
- using brick-wall decomposition.
79
- """
80
- n_qubits = n_rows * n_cols
81
- qc = QuantumCircuit(n_qubits)
82
- theta_x = h * dt
83
-
84
- first_order_tfim(qc, n_rows, n_cols, J, h, dt / 2)
85
-
86
- # ---- Transverse field ----
87
- for q in range(n_qubits):
88
- qc.rx(2 * theta_x, q)
89
-
90
- first_order_tfim(qc, n_rows, n_cols, J, h, dt / 2)
91
-
92
- return qc
93
-
94
-
95
- def init_beta(n_qubits):
96
- n_bias = n_qubits + 1
97
- thresholds = np.empty(n_bias, dtype=np.float64)
98
- normalizer = 0
99
- for q in range(n_qubits >> 1):
100
- normalizer += math.comb(n_qubits, q) << 1
101
- if n_qubits & 1:
102
- normalizer += math.comb(n_qubits, n_qubits >> 1)
103
- p = 1
104
- for q in range(n_qubits >> 1):
105
- val = p / normalizer
106
- thresholds[q] = val
107
- thresholds[n_bias - (q + 1)] = val
108
- p = math.comb(n_qubits, q + 1)
109
- if n_qubits & 1:
110
- thresholds[n_qubits >> 1] = p / normalizer
111
-
112
- return thresholds
113
-
114
-
115
- def calc_stats(ideal_probs, pqi_probs, shots):
116
- # For QV, we compare probabilities of (ideal) "heavy outputs."
117
- # If the probability is above 2/3, the protocol certifies/passes the qubit width.
118
- n_pow = len(ideal_probs)
119
- n = int(round(math.log2(n_pow)))
120
- threshold = statistics.median(ideal_probs)
121
- u_u = 1 / n_pow
122
- diff_sqr = 0
123
- noise = 0
124
- numer = 0
125
- denom = 0
126
- sum_hog_prob = 0
127
- ideal_sqr_mag = 0
128
- exp_sqr_mag = 0
129
- for i in range(n_pow):
130
- exp = pqi_probs.get(i, 0)
131
- ideal = ideal_probs[i]
132
-
133
- # L2 distance
134
- diff_sqr += (ideal - exp) ** 2
135
- noise += exp * (1 - exp) / shots
136
-
137
- # XEB / EPLG
138
- denom += (ideal - u_u) ** 2
139
- numer += (ideal - u_u) * (exp - u_u)
140
-
141
- # QV / HOG
142
- if ideal > threshold:
143
- sum_hog_prob += exp
144
-
145
- perm = i
146
- m = 0
147
- for _ in range(n):
148
- m += -1 if (perm & 1) else 1
149
- perm >>= 1
150
- m /= n
151
- m *= m
152
- ideal_sqr_mag += ideal * m
153
- exp_sqr_mag += exp * m
154
-
155
- l2_diff = diff_sqr ** (1 / 2)
156
- l2_diff_debiased = math.sqrt(max(diff_sqr - noise, 0.0))
157
- xeb = numer / denom
158
- if xeb > 1.0:
159
- xeb = 2.0 - xeb
160
- if xeb < 0.0:
161
- xeb = 0.0
162
-
163
- return {
164
- "qubits": n,
165
- "l2_difference": float(l2_diff),
166
- "l2_difference_debiased": float(l2_diff_debiased),
167
- "xeb_rectified": float(xeb),
168
- "hog_prob": float(sum_hog_prob),
169
- "ideal_sqr_mag": float(ideal_sqr_mag),
170
- "sqr_mag_diff": float(exp_sqr_mag - ideal_sqr_mag),
171
- }
172
-
173
-
174
- def normalize_counts(counts, shots):
175
- return {k: v / shots for k, v in counts.items()}
176
-
177
-
178
- def main():
179
- n_qubits = 6
180
- depth = 40
181
- z = 4
182
-
183
- # Quantinuum settings
184
- J, h, dt = -1.0, 2.0, 0.125
185
- theta = math.pi / 18
186
-
187
- # Pure ferromagnetic
188
- # J, h, dt = -1.0, 0.0, 0.25
189
- # theta = 0
190
-
191
- # Pure transverse field
192
- # J, h, dt = 0.0, 2.0, 0.25
193
- # theta = -math.pi / 2
194
-
195
- # Critical point (symmetry breaking)
196
- # J, h, dt = -1.0, 1.0, 0.25
197
- # theta = -math.pi / 4
198
-
199
- if len(sys.argv) > 1:
200
- n_qubits = int(sys.argv[1])
201
- if len(sys.argv) > 2:
202
- depth = int(sys.argv[2])
203
- if len(sys.argv) > 3:
204
- dt = float(sys.argv[3])
205
- if len(sys.argv) > 4:
206
- shots = int(sys.argv[4])
207
- else:
208
- shots = max(65536, 1 << (n_qubits + 2))
209
-
210
- print(f"Qubits: {n_qubits}")
211
-
212
- n_rows, n_cols = factor_width(n_qubits, False)
213
- qubits = list(range(n_qubits))
214
-
215
- # Set the initial temperature by theta.
216
- qc_aer = QuantumCircuit(n_qubits)
217
- for q in range(n_qubits):
218
- qc_aer.ry(theta, q)
219
-
220
- qc_step = brick_wall_tfim_step(n_rows, n_cols, J, h, dt)
221
- qc_step = transpile(
222
- qc_step,
223
- basis_gates=QrackSimulator.get_qiskit_basis_gates(),
224
- )
225
-
226
- control = AerSimulator(method="statevector")
227
-
228
- r_squared = 0.0
229
- r_squared_db = 0.0
230
- r_squared_xeb = 0.0
231
- ss = 0.0
232
- ssr = 0.0
233
-
234
- for d in range(1, depth + 1):
235
- t = d * dt
236
-
237
- # Run the Trotterized simulation with Aer and get the marginal probabilities.
238
- qc_aer = qc_aer.compose(qc_step)
239
- qc_aer_sv = qc_aer.copy()
240
- qc_aer_sv.save_statevector()
241
- job = control.run(qc_aer_sv)
242
- ideal_probs = Statevector(job.result().get_statevector()).probabilities()
243
-
244
- # The magnetization components are weighted by (n+1) symmetric "bias" terms over possible Hamming weights.
245
- pqi_probs = normalize_counts(dict(Counter(
246
- generate_fermi_hubbard_samples(J=J, h=h, z=z, theta=theta, t=t, n_qubits=n_qubits, shots=shots)
247
- )), shots)
248
-
249
- result = calc_stats(ideal_probs, pqi_probs, shots)
250
-
251
- # Add up the square residuals:
252
- r_squared += result["l2_difference"] ** 2
253
- r_squared_db += result["l2_difference_debiased"] ** 2
254
- r_squared_xeb += (1.0 - result["xeb_rectified"]) ** 2
255
-
256
- # Save the sum of squares and sum of square residuals on the magnetization curve values.
257
- ss += result["ideal_sqr_mag"] ** 2
258
- ssr += result["sqr_mag_diff"] ** 2
259
-
260
- # R^2 and RMSE are elementary and standard measures of goodness-of-fit with simple definitions.
261
- # Ideal marginal probability would be 1.0, each depth step. Squared and summed, that's depth.
262
- r_squared = 1.0 - r_squared / depth
263
- r_squared_db = 1.0 - r_squared_db / depth
264
- r_squared_xeb = 1.0 - r_squared_xeb / depth
265
- rmse = (ssr / depth) ** (1 / 2)
266
- sm_r_squared = 1.0 - (ssr / ss)
267
-
268
- print("L2 norm similarity R^2: " + str(r_squared))
269
- print("L2 norm debiased similarity R^2: " + str(r_squared_db))
270
- print("XEB (rectified) R^2: " + str(r_squared_xeb))
271
- print("Square magnetization RMSE: " + str(rmse))
272
- print("Square magnetization R^2: " + str(sm_r_squared))
273
-
274
- return 0
275
-
276
-
277
- if __name__ == "__main__":
278
- sys.exit(main())
@@ -1,207 +0,0 @@
1
- # Fermi-Hubbard / XXZ-Heisenberg validation, v2
2
- # by Dan Strano and Claude (Anthropic)
3
- #
4
- # TFIM -> Fermi-Hubbard disclaimer):
5
- # - This is a mean-field / entropy-based heuristic correction, not derived
6
- # from the Heisenberg Hamiltonian in closed form. It was benchmarked at
7
- # ONE parameter point (n_qubits=6, Quantinuum J/h/dt/theta defaults) and
8
- # has not been swept across parameter space or system size.
9
- # - Direct numerical comparison (see hamming_conservation_test.py) shows
10
- # that RXX+RYY+RZZ terms, despite each individually commuting with total
11
- # Hamming weight (Sz) in isolation, measurably shift BOTH the mean and
12
- # the variance of the true Hamming-weight marginal relative to pure TFIM
13
- # once interleaved with the transverse-field Trotter steps -- this is a
14
- # real correction, not just added entropy/noise, and the entropy-blend
15
- # approach here only captures it directionally, via a proxy signal, not
16
- # from the actual mean-shift mechanism. A first-principles closed-form
17
- # ansatz (analogous to the original TFIM derivation, this time fit
18
- # against RZZ+RXX+RYY+RX Trotter data) is the right next step and has
19
- # NOT been done here.
20
-
21
- import math
22
- import numpy as np
23
- import statistics
24
- import sys
25
-
26
- from collections import Counter
27
-
28
- from qiskit import QuantumCircuit
29
- from qiskit.circuit.library import RZZGate, RXXGate, RYYGate
30
- from qiskit.compiler import transpile
31
-
32
- from pyqrack import QrackSimulator
33
-
34
- from pyqrackising.generate_tfim_samples import generate_fermi_hubbard_samples
35
-
36
-
37
- def factor_width(width, is_transpose=False):
38
- col_len = math.floor(math.sqrt(width))
39
- while ((width // col_len) * col_len) != width:
40
- col_len -= 1
41
- row_len = width // col_len
42
-
43
- return (col_len, row_len) if is_transpose else (row_len, col_len)
44
-
45
-
46
- def edge_layers(n_rows, n_cols):
47
- horiz_even = [(r * n_cols + c, r * n_cols + (c + 1) % n_cols) for r in range(n_rows) for c in range(0, n_cols, 2)]
48
- horiz_odd = [(r * n_cols + c, r * n_cols + (c + 1) % n_cols) for r in range(n_rows) for c in range(1, n_cols, 2)]
49
- vert_even = [(r * n_cols + c, ((r + 1) % n_rows) * n_cols + c) for r in range(1, n_rows, 2) for c in range(n_cols)]
50
- vert_odd = [(r * n_cols + c, ((r + 1) % n_rows) * n_cols + c) for r in range(0, n_rows, 2) for c in range(n_cols)]
51
- return [horiz_even, horiz_odd, vert_even, vert_odd]
52
-
53
-
54
- def trotter_step_heisenberg(circ, qubits, lattice_shape, J, h, dt, Jxy=None):
55
- """The CORRECT ideal circuit for Fermi-Hubbard/Heisenberg validation:
56
- RZZ + RXX + RYY (isotropic hopping+interaction) + RX (transverse field).
57
- Jxy defaults to J (isotropic Heisenberg + field)."""
58
- if Jxy is None:
59
- Jxy = J
60
- n_rows, n_cols = lattice_shape
61
- for q in qubits:
62
- circ.rx(h * dt, q)
63
- for layer in edge_layers(n_rows, n_cols):
64
- for q1, q2 in layer:
65
- circ.append(RZZGate(2 * J * dt), [q1, q2])
66
- circ.append(RXXGate(2 * Jxy * dt), [q1, q2])
67
- circ.append(RYYGate(2 * Jxy * dt), [q1, q2])
68
- for q in qubits:
69
- circ.rx(h * dt, q)
70
- return circ
71
-
72
-
73
- def normalize_counts(counts, shots):
74
- return {k: v / shots for k, v in counts.items()}
75
-
76
-
77
- def calc_stats(ideal_probs, pqi_probs, shots):
78
- n_pow = len(ideal_probs)
79
- n = int(round(math.log2(n_pow)))
80
- threshold = statistics.median(ideal_probs)
81
- u_u = 1 / n_pow
82
- diff_sqr = 0
83
- noise = 0
84
- numer = 0
85
- denom = 0
86
- sum_hog_prob = 0
87
- ideal_sqr_mag = 0
88
- exp_sqr_mag = 0
89
- for i in range(n_pow):
90
- exp = pqi_probs.get(i, 0)
91
- ideal = ideal_probs[i]
92
- diff_sqr += (ideal - exp) ** 2
93
- noise += exp * (1 - exp) / shots
94
- denom += (ideal - u_u) ** 2
95
- numer += (ideal - u_u) * (exp - u_u)
96
- if ideal > threshold:
97
- sum_hog_prob += exp
98
- perm = i
99
- m = 0
100
- for _ in range(n):
101
- m += -1 if (perm & 1) else 1
102
- perm >>= 1
103
- m /= n
104
- m *= m
105
- ideal_sqr_mag += ideal * m
106
- exp_sqr_mag += exp * m
107
-
108
- l2_diff = diff_sqr ** 0.5
109
- l2_diff_debiased = math.sqrt(max(diff_sqr - noise, 0.0))
110
- xeb = numer / denom
111
- if xeb > 1.0:
112
- xeb = 2.0 - xeb
113
- if xeb < 0.0:
114
- xeb = 0.0
115
-
116
- return {
117
- "qubits": n,
118
- "l2_difference": float(l2_diff),
119
- "l2_difference_debiased": float(l2_diff_debiased),
120
- "xeb_rectified": float(xeb),
121
- "hog_prob": float(sum_hog_prob),
122
- "ideal_sqr_mag": float(ideal_sqr_mag),
123
- "sqr_mag_diff": float(exp_sqr_mag - ideal_sqr_mag),
124
- }
125
-
126
-
127
- def main():
128
- n_qubits = 6
129
- depth = 40
130
- z = 4
131
-
132
- # Quantinuum settings
133
- J, h, dt = -1.0, 2.0, 0.125
134
- theta = math.pi / 18
135
-
136
- # Pure ferromagnetic
137
- # J, h, dt = -1.0, 0.0, 0.25
138
- # theta = 0
139
-
140
- # Pure transverse field
141
- # J, h, dt = 0.0, 2.0, 0.25
142
- # theta = -math.pi / 2
143
-
144
- # Critical point (symmetry breaking)
145
- # J, h, dt = -1.0, 1.0, 0.25
146
- # theta = -math.pi / 4
147
-
148
- if len(sys.argv) > 1:
149
- n_qubits = int(sys.argv[1])
150
- if len(sys.argv) > 2:
151
- depth = int(sys.argv[2])
152
- if len(sys.argv) > 3:
153
- dt = float(sys.argv[3])
154
- if len(sys.argv) > 4:
155
- shots = int(sys.argv[4])
156
- else:
157
- shots = 1 << (n_qubits + 2)
158
-
159
- print(f"Qubits: {n_qubits} (ideal = RZZ+RXX+RYY+RX Heisenberg+field circuit)")
160
-
161
- n_rows, n_cols = factor_width(n_qubits)
162
- qubits = list(range(n_qubits))
163
-
164
- qc = QuantumCircuit(n_qubits)
165
- for q in range(n_qubits):
166
- qc.ry(theta, q)
167
- control = QrackSimulator(n_qubits)
168
- basis_gates = QrackSimulator.get_qiskit_basis_gates()
169
- qc = transpile(qc, basis_gates=basis_gates)
170
- control.run_qiskit_circuit(qc)
171
-
172
- r_squared = r_squared_db = r_squared_xeb = ss = ssr = 0.0
173
- for d in range(1, depth + 1):
174
- t = d * dt
175
- step_circ = QuantumCircuit(n_qubits)
176
- trotter_step_heisenberg(step_circ, qubits, (n_rows, n_cols), J, h, dt)
177
- step_circ = transpile(step_circ, basis_gates=basis_gates)
178
- control.run_qiskit_circuit(step_circ)
179
- control_probs = control.out_probs()
180
-
181
- pqi_probs = normalize_counts(dict(Counter(
182
- generate_fermi_hubbard_samples(J=J, h=h, z=z, theta=theta, t=t, n_qubits=n_qubits, shots=shots)
183
- )), shots)
184
-
185
- result = calc_stats(control_probs, pqi_probs, shots)
186
- r_squared += result["l2_difference"] ** 2
187
- r_squared_db += result["l2_difference_debiased"] ** 2
188
- r_squared_xeb += (1.0 - result["xeb_rectified"]) ** 2
189
- ss += result["ideal_sqr_mag"] ** 2
190
- ssr += result["sqr_mag_diff"] ** 2
191
-
192
- r_squared = 1.0 - r_squared / depth
193
- r_squared_db = 1.0 - r_squared_db / depth
194
- r_squared_xeb = 1.0 - r_squared_xeb / depth
195
- rmse = (ssr / depth) ** 0.5
196
- sm_r_squared = 1.0 - (ssr / ss)
197
-
198
- print("L2 norm similarity R^2: " + str(r_squared))
199
- print("L2 norm debiased similarity R^2: " + str(r_squared_db))
200
- print("XEB (rectified) R^2: " + str(r_squared_xeb))
201
- print("Square magnetization RMSE: " + str(rmse))
202
- print("Square magnetization R^2: " + str(sm_r_squared))
203
- return 0
204
-
205
-
206
- if __name__ == "__main__":
207
- sys.exit(main())
File without changes
File without changes
File without changes
File without changes