Repository navigation
Expand file tree
/
Copy pathtest_null_net_receipts.py
More file actions
533 lines (440 loc) · 22.4 KB
/
Copy pathtest_null_net_receipts.py
File metadata and controls
533 lines (440 loc) · 22.4 KB
1
2
3
4
5
6
7
8
9
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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#!/usr/bin/env python3
"""Tests for the null-net receipt instrumentation (#503, #524)."""
from __future__ import annotations
import sys
import json
from fractions import Fraction
from pathlib import Path
import numpy as np
import pytest
HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE))
from null_net_receipts import ( # noqa: E402
hsm_compression_receipt,
instrument_null_net,
lie_closure_receipt,
mixed_gns_cauchy_receipt,
nti_receipt,
separating_modulus_receipt,
weak_additivity_receipt,
correlation_matrix,
bond_limit_error_bound,
embed,
modular_subspace_diagnostic,
momentum_profile,
_shape_fit,
)
def test_nti_and_weak_additivity_are_witnessed():
for n in (16, 32):
assert nti_receipt(n)["nontrivial"]
assert weak_additivity_receipt(n)["covers_ring"]
def test_separating_modulus_positive_at_every_stage():
r = separating_modulus_receipt(32)
assert r["strictly_faithful"]
# the gap is astronomically small but strictly positive: this is why
# the instrumentation needs extended precision
assert r["occupation_gap_log10"] < -10
def test_mixed_gns_cauchy():
r = mixed_gns_cauchy_receipt((16, 32, 64))
assert r["cauchy_decreasing"]
def test_hsm_compression_has_consistent_sign_asymmetry():
r32 = hsm_compression_receipt(32)
r64 = hsm_compression_receipt(64)
assert r32["asymmetry_ratio"] > 3.0
assert r64["asymmetry_ratio"] > 3.0
assert r32["lower_packet_leakage_sign"] == r64["lower_packet_leakage_sign"]
for r in (r32, r64):
assert r["max_subspace_leakage_plus"] > 0.98
assert r["max_subspace_leakage_plus"] == pytest.approx(
r["max_subspace_leakage_minus"], abs=1e-12)
assert not r["hsm_inclusion_certified"]
def test_hsm_real_packet_is_the_null_control():
# a real (non-chiral) packet is exactly time-reversal symmetric: the
# asymmetry must vanish. Chirality selects directional packet transport,
# not half-sided inclusion of the entire subspace.
import numpy as np
from scipy.linalg import expm
from modular_clock_instrumentation import arc_entanglement_hamiltonian
from null_net_receipts import embed
n, m_a, m_b = 32, 16, 8
h_a = arc_entanglement_hamiltonian(n, m_a)
h_b = embed(arc_entanglement_hamiltonian(n, m_b), m_a)
xs = np.arange(m_a)
psi = np.exp(-((xs - m_b / 2.0) ** 2) / (2 * (m_b / 6.0) ** 2)).astype(complex)
psi[m_b:] = 0.0
psi /= np.linalg.norm(psi)
def leakage(t):
out = expm(1j * h_a * t) @ expm(-1j * h_b * t) @ psi
return float(np.sum(np.abs(out[m_b:]) ** 2))
assert abs(leakage(0.12) - leakage(-0.12)) < 1e-12
def test_lie_closure_percent_level_and_resummation_control():
r = lie_closure_receipt(32)
assert r["relative_residual"] < 0.02
# the unresummed range-2 truncation is several times worse: the
# resummation is load-bearing
assert (r["relative_residual_unresummed_control"]
> 5.0 * r["relative_residual"])
def test_instrumented_null_net_verdicts():
report = instrument_null_net(rings=(16, 32))
w = report["receipts_witnessed"]
assert w["nti"] and w["weak_additivity"]
assert w["separating_faithfulness"] and w["single_bond_observable_limit"]
assert not w["mixed_gns_cauchy"]
assert not w["hsm_compression_one_particle"]
assert not w["modular_lie_closure_percent_level"]
assert report["verdicts"]["lie_closure_percent_level"]
# the rate clause must be recorded as open in the report
assert any("convergence rate" in p or "rate" in p
for p in report["receipts_pending"])
def test_full_ring_covariance_is_the_independent_fourier_projector():
n = 16
momenta = (2 * np.arange(n) + 1) * np.pi / n
occupied = momenta[np.cos(momenta) > 0]
modes = np.exp(1j * np.outer(np.arange(n), occupied)) / np.sqrt(n)
expected = modes @ modes.conj().T
actual = correlation_matrix(n)
np.testing.assert_allclose(actual, expected, atol=3e-15)
np.testing.assert_allclose(actual @ actual, actual, atol=3e-15)
def test_packet_asymmetry_cannot_witness_half_sided_inclusion():
from scipy.linalg import expm
from modular_clock_instrumentation import arc_entanglement_hamiltonian
h = arc_entanglement_hamiltonian(16, 8)
leakage = [np.linalg.norm(expm(1j * t * h)[4:, :4], 2) ** 2
for t in (0.12, -0.12)]
assert min(leakage) > 0.65
assert leakage[0] == pytest.approx(leakage[1], abs=1e-14)
assert not instrument_null_net((16,))["receipts_witnessed"][
"hsm_compression_one_particle"]
def test_single_stage_cannot_supply_mixed_gns_or_an_empty_closure_witness():
report = instrument_null_net((16,))
assert not report["verdicts"]["lie_closure_percent_level"]
assert not report["receipts_witnessed"]["mixed_gns_cauchy"]
def test_lie_fit_rejects_a_cutoff_that_erases_the_signal():
with pytest.raises(ValueError, match="cutoff|rmax"):
lie_closure_receipt(32, rmax=0)
@pytest.mark.parametrize("n", [8, 12, 16, 24, 32])
def test_covariance_spectrum_seam_and_arc_faithfulness(n):
# Separate discrete Fourier implementation, including unoccupied modes.
k = (2 * np.arange(n) + 1) * np.pi / n
basis = np.exp(1j * np.outer(np.arange(n), k)) / np.sqrt(n)
occupied, empty = basis[:, np.cos(k) > 0], basis[:, np.cos(k) < 0]
c = correlation_matrix(n)
np.testing.assert_allclose(c, occupied @ occupied.conj().T, atol=6e-15)
np.testing.assert_allclose(np.eye(n) - c, empty @ empty.conj().T, atol=6e-15)
np.testing.assert_allclose(c @ c, c, atol=2e-15)
assert np.trace(c) == n / 2
assert c[0, -1] == pytest.approx(-c[0, 1], abs=1e-16)
for m in (1, n // 4, n // 2):
# Gram rank checks the Vandermonde argument without subtracting tiny
# eigenvalues from one in binary64 (which fails by N=64).
assert np.linalg.matrix_rank(occupied[:m]) == m
assert np.linalg.matrix_rank(empty[:m]) == m
def test_precision_context_is_local():
import mpmath as mp
c = correlation_matrix(16)
expected = separating_modulus_receipt(32)
with mp.workdps(15):
np.testing.assert_array_equal(correlation_matrix(16), c)
assert separating_modulus_receipt(32) == expected
assert mp.mp.dps == 15
def test_bond_limit_against_independent_quadrature_and_fourier_sum():
from scipy.integrate import quad
from math import exp, pi
rings = (8, 12, 32, 64, 128)
report = mixed_gns_cauchy_receipt(rings)
f = lambda x: exp(-200 * (x - 0.25) ** 2)
limit, quadrature_error = quad(f, 0, 1, epsabs=1e-13, epsrel=1e-13)
limit *= 2 / pi
assert quadrature_error < 1e-12
assert report["limit_value"] == pytest.approx(limit, abs=1e-15)
for n, value, bound in zip(rings, report["values"], report["limit_error_bounds_exact"]):
momenta = (2 * np.arange(n) + 1) * pi / n
bond = np.sum(np.exp(1j * momenta[np.cos(momenta) > 0])).real / n
independent = 2 * bond * sum(f(j / n) for j in range(n - 1)) / n
assert value == pytest.approx(independent, abs=1e-15)
assert abs(value - limit) < Fraction(bound)
for difference, bound in zip(report["cauchy_differences"], report["pairwise_cauchy_bounds_exact"]):
assert difference < Fraction(bound)
# The bound supplies a cofinal rate for arbitrary large stages, without
# evaluating larger matrices or extrapolating from a finite fit.
for n in (8, 32, 128, 10**6, 10**20):
assert 0 < bond_limit_error_bound(n) < Fraction(3, n)
assert bond_limit_error_bound(2 * n) < bond_limit_error_bound(n)
assert not report["mixed_gns_certified"]
def test_exact_cyclic_counterexample_to_packet_inclusion():
import sympy as sp
# U e0=e1 stays inside B, but U e1=e2 escapes. Reverse flow sends
# e0 outside. Thus a perfect packet asymmetry coexists with maximal
# leakage of the entire subspace in BOTH directions.
u = sp.Matrix([[0, 0, 1], [1, 0, 0], [0, 1, 0]])
p = sp.diag(1, 1, 0)
q = sp.eye(3) - p
e0 = sp.eye(3)[:, 0]
assert q * u * e0 == sp.zeros(3, 1)
assert (q * u.T * e0).norm() == 1
for flow in (u, u.T):
block = q * flow * p
assert max((block.T * block).eigenvals()) == 1
# A Hermitian logarithm realizes this unitary as a finite continuous flow.
eigenvalues, eigenvectors = np.linalg.eig(np.array(u).astype(complex))
h = (eigenvectors * np.angle(eigenvalues)) @ eigenvectors.conj().T
r = modular_subspace_diagnostic(h, np.zeros((2, 2)), 1, np.array([1, 0]))
assert r["leakage_plus"] < 1e-28
assert r["leakage_minus"] == pytest.approx(1, abs=2e-14)
assert r["max_subspace_leakage_plus"] == pytest.approx(1, abs=2e-14)
assert r["max_subspace_leakage_minus"] == pytest.approx(1, abs=2e-14)
assert not r["hsm_inclusion_certified"]
@pytest.mark.parametrize("ma,mb", [(3, 1), (4, 3), (7, 2), (7, 5)])
def test_full_subspace_leakage_by_independent_complex_spectral_evolution(ma, mb):
rng = np.random.default_rng(70 + ma + mb)
a = rng.normal(size=(ma, ma)) + 1j * rng.normal(size=(ma, ma))
b = rng.normal(size=(mb, mb)) + 1j * rng.normal(size=(mb, mb))
ha, hb = a + a.conj().T, b + b.conj().T
packet = rng.normal(size=mb) + 1j * rng.normal(size=mb)
t = 0.31
report = modular_subspace_diagnostic(ha, hb, t, packet)
eig, basis = np.linalg.eigh(ha)
for sign, label in ((1, "plus"), (-1, "minus")):
va = (basis * np.exp(1j * sign * t * eig)) @ basis.conj().T
# No h_B in this calculation: it only rotates the input subspace.
block = va[mb:, :mb]
maximum = np.linalg.eigvalsh(block.conj().T @ block)[-1]
assert report[f"max_subspace_leakage_{label}"] == pytest.approx(maximum, abs=2e-14)
assert report[f"leakage_{label}"] <= maximum + 2e-14
assert report["max_subspace_sign_difference"] < 2e-14
def test_reducing_subspace_is_not_promoted_to_proper_inclusion():
r = modular_subspace_diagnostic(np.diag([1, 2, 3]), np.eye(2), 0.1, np.ones(2))
assert r["leakage_plus"] == r["leakage_minus"] == 0
assert r["max_subspace_leakage_plus"] == 0
assert r["asymmetry_ratio"] is None
assert r["lower_packet_leakage_sign"] is None
assert not r["proper_finite_subspace_inclusion_possible"]
json.dumps(r, allow_nan=False)
def test_imaginary_commutator_obstruction_is_exact():
import sympy as sp
x = sp.Matrix([[0, 1], [1, 0]])
z = sp.diag(1, -1)
comm = sp.I * (x * z - z * x)
a, b, c = sp.symbols("a b c", real=True)
candidate = sp.Matrix([[a, b], [b, c]])
squared = sp.trace((comm - candidate).adjoint() * (comm - candidate)).expand()
assert squared == 8 + a * a + 2 * b * b + c * c
assert sp.trace(comm.adjoint() * comm) == 8
r = lie_closure_receipt(32)
assert r["real_symmetric_span_relative_distance"] == pytest.approx(1, abs=1e-15)
assert not r["operator_lie_closure_certified"]
def test_even_envelope_has_an_exact_nonzero_invisible_commutator():
import sympy as sp
invisible = sp.zeros(6)
invisible[1, 3], invisible[3, 1] = 2, -2
invisible[0, 4], invisible[4, 0] = 1, -1
# Both entries have midpoint 2 and weighted sum 2 - 2*1 = 0.
diagonal = sp.diag(*range(6))
symmetric = sp.Matrix(6, 6, lambda i, j: invisible[i, j] / (i - j) if i != j else 0)
assert symmetric == symmetric.T
assert diagonal * symmetric - symmetric * diagonal == invisible
assert invisible.norm() ** 2 == 10
profile, tail = momentum_profile(np.array(invisible).astype(float))
np.testing.assert_array_equal(profile, np.zeros(4))
np.testing.assert_array_equal(tail, np.zeros(4))
base = np.zeros((6, 6))
base[1, 3], base[3, 1] = 1, -1
contaminated = base + 1e6 * np.array(invisible).astype(float)
np.testing.assert_array_equal(momentum_profile(base)[0], momentum_profile(contaminated)[0])
assert np.linalg.norm(contaminated - base) > 3e6
@pytest.mark.parametrize("cutoff", [2, 4, 8, 100, None])
def test_omitted_even_ranges_against_exact_entrywise_control(cutoff):
rng = np.random.default_rng(172)
entries = rng.integers(-5, 6, size=(14, 14))
comm = entries - entries.T
actual, tail = momentum_profile(comm, cutoff)
expected, omitted, full = [Fraction(0)] * 12, [Fraction(0)] * 12, [Fraction(0)] * 12
# Enumerate matrix entries instead of the producer's midpoint/range loop.
for i in range(14):
for k in range(i + 2, 14, 2):
r = k - i
j = (i + k) // 2 - 1
term = Fraction(r, 2) * (-1) ** ((r - 2) // 2) * int(comm[i, k])
full[j] += term
if cutoff is None or r <= cutoff:
expected[j] += term
else:
omitted[j] += abs(term)
np.testing.assert_array_equal(actual, np.array(expected, dtype=float))
np.testing.assert_array_equal(tail, np.array(omitted, dtype=float))
assert all(abs(a - b) <= t for a, b, t in zip(full, expected, omitted))
@pytest.mark.parametrize("cutoff", [-2, 0, 1, 3, True, 2.0, float("nan")])
def test_bad_cutoff_fails_before_fitting(cutoff):
with pytest.raises(ValueError, match="rmax"):
lie_closure_receipt(32, cutoff)
with pytest.raises(ValueError, match="rmax"):
momentum_profile(np.zeros((4, 4)), cutoff)
@pytest.mark.parametrize("rings", [(), (16, 16), (32, 16), (True,), (16.0,), (0,), (10,), (12,)])
def test_invalid_refinement_family_is_rejected(rings):
with pytest.raises(ValueError):
instrument_null_net(rings)
@pytest.mark.parametrize("m,g", [([], []), ([1], [1]), ([0, 0], [1, 1]),
([1, 1], [0, 0]), ([float("nan"), 1], [1, 2]),
([1, 2], [float("inf"), 1]), ([1j, 1], [1, 2])])
def test_unresolved_shape_data_cannot_pass(m, g):
with pytest.raises(ValueError):
_shape_fit(m, g)
def test_shape_fit_is_scale_stable_and_incorrect_shape_is_detected():
for scale in (1e-300, 1, 1e300):
r = _shape_fit(scale * np.array([1, 2, 3]), scale * np.array([2, 4, 6]))
assert r["relative_residual"] < 1e-15
assert r["normalization_alpha"] == pytest.approx(0.5)
assert _shape_fit([1, -1, 1], [1, 1, 1])["relative_residual"] > 0.9
def test_shape_fit_cannot_return_zero_coefficient_with_perfect_nonzero_fit():
with pytest.raises(ValueError, match="normalization"):
_shape_fit([1e-300, 2e-300], [1e300, 2e300])
with pytest.raises(ValueError, match="normalization"):
_shape_fit([1e300, 2e300], [1e-300, 2e-300])
# A genuine orthogonal signal still has the resolved coefficient zero.
r = _shape_fit([1e300, -1e300], [1e-300, 1e-300])
assert r["normalization_alpha"] == 0
assert r["relative_residual"] == 1
@pytest.mark.parametrize("time", [0, -0.1, True, np.bool_(True), "0.1", float("nan"), float("inf"), 1j])
def test_invalid_flow_time_is_rejected(time):
with pytest.raises(ValueError):
modular_subspace_diagnostic(np.eye(3), np.eye(2), time, np.ones(2))
@pytest.mark.parametrize("packet", [[0, 0], [1], [float("nan"), 1], [float("inf"), 1], [True, False]])
def test_invalid_flow_packet_is_rejected(packet):
with pytest.raises(ValueError):
modular_subspace_diagnostic(np.eye(3), np.eye(2), 0.1, packet)
def test_nonhermitian_flow_and_invalid_embedding_are_rejected():
with pytest.raises(ValueError, match="Hermitian"):
modular_subspace_diagnostic(np.array([[1, 2], [0, 1]]), np.ones((1, 1)), 1, [1])
with pytest.raises(ValueError, match="subspace"):
modular_subspace_diagnostic(np.eye(2), np.eye(2), 1, [1, 1])
for size, offset in ((1, 0), (4, -1), (3, 2), (True, 0)):
with pytest.raises(ValueError):
embed(np.eye(2), size, offset)
y = np.array([[0, -1j], [1j, 0]])
np.testing.assert_array_equal(embed(y, 4, 1)[1:3, 1:3], y)
def test_nonfinite_matrix_and_unresolved_spectrum_are_rejected(monkeypatch):
import mpmath as mp
import null_net_receipts as module
for matrix in (np.array([[float("nan")]]), np.array([[float("inf")]]),
np.array([1, 2]), np.zeros((2, 3)), np.zeros((0, 0))):
with pytest.raises(ValueError):
embed(matrix, 4)
monkeypatch.setattr(module.mp, "eigsy", lambda _: (mp.matrix([0, 1]), None))
with pytest.raises(ValueError, match="unresolved"):
separating_modulus_receipt(8)
def test_checked_report_replays_with_scoped_verdicts():
expected = json.loads((HERE / "runs" / "null_net_receipt_report.json").read_text(encoding="utf-8"))
actual = instrument_null_net()
def compare(a, b):
if isinstance(a, dict):
assert a.keys() == b.keys()
for key in a:
compare(a[key], b[key])
elif isinstance(a, list):
assert len(a) == len(b)
for x, y in zip(a, b):
compare(x, y)
elif type(a) is float:
assert b == pytest.approx(a, abs=2e-12, rel=2e-10)
else:
assert type(a) is type(b) and a == b
compare(expected, actual)
assert actual["schema_version"] == 2
assert actual["verdicts"]["lie_closure_residuals_decreasing"]
assert not actual["verdicts"]["lie_closure_rate_certified"]
assert all(r["all_even_ranges"] for r in actual["lie_closure"])
json.dumps(actual, allow_nan=False)
@pytest.mark.parametrize("scale", [1e-320, 1e-310])
def test_subnormal_complex_packet_preserves_its_direction(scale):
ha = np.array([[0., 0, 1], [0, 1, 0], [1, 0, 0]])
packet = np.array([scale, complex(0, scale)])
r = modular_subspace_diagnostic(ha, np.zeros((2, 2)), 0.4, packet)
# The first component alone couples to the third through a Pauli X block.
expected = np.sin(0.4) ** 2 / 2
assert np.isfinite(r["leakage_plus"])
assert r["leakage_plus"] == pytest.approx(expected, rel=1e-14, abs=0)
json.dumps(r, allow_nan=False)
def test_subnormal_complex_nonhermitian_input_cannot_bypass_validation():
from null_net_receipts import _matrix
h = np.array([[0, 1e-320], [0, 0]], dtype=complex)
with pytest.raises(ValueError, match="Hermitian"):
_matrix(h, hermitian=True)
@pytest.mark.parametrize("measured", [[2**53, 2**53+1], [float(2**60), 2**60+1]])
def test_shape_fit_rejects_lossy_input_promotion(measured):
with pytest.raises(ValueError, match="binary64"):
_shape_fit(measured, [1, 1])
def test_shape_fit_retains_a_resolved_tiny_residual():
r = _shape_fit([1, 1e-200], [1, 0])
assert r["normalization_alpha"] == 1
assert r["relative_residual"] == pytest.approx(1e-200, rel=1e-14, abs=0)
@pytest.mark.parametrize("scale", [np.nextafter(0., 1.), 1e-320, 1e-200, 1., 1e200, 1.7e308])
def test_complex_packet_magnitude_and_global_phase_do_not_change_leakage(scale):
ha = np.array([[0., 0, 1], [0, 1, 0], [1, 0, 0]])
packet = np.array([complex(scale, scale), complex(scale, -scale)])
r = modular_subspace_diagnostic(ha, np.zeros((2, 2)), 0.4, packet)
expected = np.sin(0.4) ** 2 / 2
assert r["leakage_plus"] == pytest.approx(expected, rel=1e-14, abs=0)
assert r["leakage_minus"] == pytest.approx(expected, rel=1e-14, abs=0)
json.dumps(r, allow_nan=False)
def test_subnormal_hermitian_generator_survives_reciprocal_time_scaling():
# The imaginary Pauli generator tests complex validation and flow, with
# a matrix scale below the reciprocal-overflow threshold.
h = np.array([[0, -1j * 1e-310], [1j * 1e-310, 0]])
r = modular_subspace_diagnostic(h, np.zeros((1, 1)), 1e308, [1])
expected = np.sin(1e-310 * 1e308) ** 2
assert r["max_subspace_leakage_plus"] == pytest.approx(expected, rel=1e-14, abs=0)
assert r["max_subspace_leakage_minus"] == pytest.approx(expected, rel=1e-14, abs=0)
def test_packet_with_unresolvable_component_range_is_rejected():
with pytest.raises(ValueError, match="dynamic range"):
modular_subspace_diagnostic(np.eye(3), np.eye(2), 1, [1e-300, 1e300])
@pytest.mark.parametrize("value", [2**53 + 1, True, float("inf"), float("nan")])
def test_numeric_components_cannot_hide_invalid_data_in_complex_arrays(value):
with pytest.raises(ValueError):
embed([[value, 1j], [-1j, 0]], 3)
with pytest.raises(ValueError):
modular_subspace_diagnostic(np.eye(3), np.eye(2), 1, [value, 1j])
def test_shape_result_replays_returned_coefficient_against_original_samples():
# Rounded subnormal alpha is not the mathematical minimizer. Its returned
# residual must include this representational error.
m, g = [1e-20, 2e-20], [1e300, 2e300]
r = _shape_fit(m, g)
error = sum((Fraction(x) - Fraction(r["normalization_alpha"])*Fraction(y))**2
for x, y in zip(m, g))
exact = error / sum(Fraction(x)**2 for x in m)
assert exact == Fraction(r["relative_residual_squared_exact"])
assert r["relative_residual"] > 1e-5
assert r["relative_residual"] == pytest.approx(float(exact)**0.5, rel=2e-15, abs=0)
def test_shape_fit_against_high_precision_independent_projection():
import mpmath as mp
rng = np.random.default_rng(1044)
with mp.workdps(400):
for exponent in (-300, -100, 0, 100, 300):
for _ in range(4):
m = rng.normal(size=9) * 10.**exponent
g = rng.normal(size=9)
r = _shape_fit(m, g)
mv, gv = mp.matrix(m.tolist()), mp.matrix(g.tolist())
alpha = float(mp.fdot(mv, gv) / mp.fdot(gv, gv))
error = mv - mp.mpf(alpha) * gv
residual = float(mp.norm(error) / mp.norm(mv))
assert r["normalization_alpha"] == alpha
assert r["relative_residual"] == pytest.approx(residual, rel=2e-15, abs=0)
def test_shape_display_does_not_supply_the_acceptance_boundary():
# Equal-scale subnormal normalizations are valid, and the exact replay
# remains available even when a displayed norm rounds at a threshold.
r = _shape_fit([1., np.nextafter(1., 2.)], [1., 1.])
assert Fraction(r["relative_residual_squared_exact"]) > 0
assert r["relative_residual"] > 0
with pytest.raises(ValueError):
_shape_fit([1e300, 1e-300], [1e300, 0])
def test_finite_trend_and_displayed_zero_cannot_manufacture_closure(monkeypatch):
import null_net_receipts as module
# Inject false-looking printed residuals. Assembly must consume the
# independently exact shape decision, and cannot promote any rate.
def fake_lie(n):
return {"n_ring": n, "relative_residual": 0.,
"shape_below_two_percent": False}
monkeypatch.setattr(module, "lie_closure_receipt", fake_lie)
r = instrument_null_net((32,))
assert not r["verdicts"]["lie_closure_percent_level"]
assert not r["verdicts"]["lie_closure_rate_certified"]