Download code/verify_dataset.py from PureOne/soryeixa-wasserstein-alignment-quantum-chirality: direct link, hf CLI and curl.
- Browser
- Download file 6.67 kB
-
https://huggingface.co/datasets/PureOne/soryeixa-wasserstein-alignment-quantum-chirality/resolve/main/code/verify_dataset.py
- Command line
-
hf download hf://datasets/PureOne/soryeixa-wasserstein-alignment-quantum-chirality/code/verify_dataset.py
-
curl -L -o verify_dataset.py https://huggingface.co/datasets/PureOne/soryeixa-wasserstein-alignment-quantum-chirality/resolve/main/code/verify_dataset.py
6.67 kB
| """Verify the shipped synthetic data, exact core, labels, and regression tables.""" | |
| from collections import defaultdict | |
| from fractions import Fraction | |
| import itertools | |
| import json | |
| import math | |
| from pathlib import Path | |
| import numpy as np | |
| from build_dataset import AXES, rational_core | |
| from frame_demo import estimate_frame | |
| ROOT = Path(__file__).resolve().parents[1] | |
| def load_json(path): | |
| return json.loads(path.read_text(encoding="utf-8")) | |
| def table(name): | |
| with (ROOT / "data" / f"{name}.jsonl").open(encoding="utf-8") as file: | |
| for line in file: | |
| yield json.loads(line) | |
| def check_bloch_pairs(): | |
| cases = {case["case_id"]: case for case in table("pair_cases")} | |
| assert len(cases) == 12 | |
| expected_cases = set(itertools.product((1, 3, 7), (1/8, 1/16), (1, -1))) | |
| assert {(c["frequency"], c["amplitude"], c["relative_determinant_sign"]) for c in cases.values()} == expected_cases | |
| clouds = defaultdict(list) | |
| for row in table("bloch_pairs"): | |
| assert row["case_id"] in cases and row["cloud"] in {"source", "target"} | |
| cloud = clouds[row["case_id"], row["cloud"]] | |
| assert row["sample_index"] == len(cloud) | |
| cloud.append([row[k] for k in ("x", "y", "z")]) | |
| assert len(clouds) == 24 | |
| baseline = load_json(ROOT / "results" / "bloch_pair_baseline.json") | |
| references = {row["case_id"]: row for row in baseline["observations"]} | |
| assert references.keys() == cases.keys() | |
| errors = [] | |
| correct = 0 | |
| for case_id, case in cases.items(): | |
| truth = np.asarray(case["relative_frame_row_major"]).reshape(3, 3) | |
| parity = case["relative_determinant_sign"] | |
| assert np.allclose(truth.T @ truth, np.eye(3), atol=1e-12, rtol=0) | |
| assert math.isclose(np.linalg.det(truth), parity, abs_tol=1e-12) | |
| estimates = [] | |
| for cloud in ("source", "target"): | |
| points = np.asarray(clouds[case_id, cloud]) | |
| assert points.shape == (case["points_per_cloud"], 3) == (8192, 3) | |
| assert np.isfinite(points).all() | |
| radii = np.linalg.norm(points, axis=1) | |
| assert ((radii >= .5-1e-12) & (radii <= 1+1e-12)).all() | |
| estimate, _ = estimate_frame(points, case["frequency"]) | |
| estimates.append(estimate) | |
| relative = estimates[1] @ estimates[0].T | |
| error = float(np.linalg.norm(relative - truth)) | |
| estimated_parity = int(np.linalg.slogdet(relative)[0]) | |
| assert estimated_parity == references[case_id]["estimated_determinant_sign"] | |
| assert math.isclose(error, references[case_id]["relative_frame_frobenius_error"], abs_tol=1e-10) | |
| correct += estimated_parity == parity | |
| errors.append(error) | |
| return {"pairs": len(cases), "point_rows": sum(map(len, clouds.values())), "baseline_correct_parity": correct, | |
| "baseline_median_frame_error": float(np.median(errors))} | |
| def check_fractal(): | |
| clouds = defaultdict(list) | |
| for row in table("fractal_cores"): | |
| group = clouds[row["cloud"]] | |
| assert row["point_index"] == len(group) | |
| point = tuple(Fraction(row[f"{axis}_num"], row["denominator"]) for axis in "xyz") | |
| for axis, exact in zip("xyz", point, strict=True): | |
| assert float(exact) == row[axis] | |
| group.append(point) | |
| assert set(clouds) == {"source", "mirror"} | |
| source, mirror = set(clouds["source"]), set(clouds["mirror"]) | |
| assert len(source) == len(mirror) == len(clouds["source"]) == len(clouds["mirror"]) == 36 | |
| assert source == set(rational_core()) | |
| assert mirror == {(x, -y, z) for x, y, z in source} | |
| for group in (source, mirror): | |
| for degree in (1, 2, 3): | |
| for axes in itertools.product(range(3), repeat=degree): | |
| moment = sum(math.prod(point[i] for i in axes) for point in group) / len(group) | |
| expected = Fraction(101, 300) if degree == 2 and axes[0] == axes[1] else 0 | |
| assert moment == expected | |
| # Clusters have within-cluster distances <= 1/5 and between-cluster distances > 1/2. | |
| # Their exact centroids force an orthogonal symmetry or mirror map to be a signed permutation. | |
| for group in (source, mirror): | |
| for axis in AXES: | |
| cluster = [point for point in group if sum((a-b)**2 for a, b in zip(point, axis)) <= Fraction(1, 100)] | |
| assert len(cluster) == 6 | |
| assert tuple(sum(column)/6 for column in zip(*cluster)) == axis | |
| stabilizers, mirror_parities = [], [] | |
| for perm in itertools.permutations(range(3)): | |
| inversions = sum(perm[i] > perm[j] for i in range(3) for j in range(i+1, 3)) | |
| for signs in itertools.product((1, -1), repeat=3): | |
| image = {tuple(signs[i]*point[perm[i]] for i in range(3)) for point in source} | |
| if image == source: | |
| stabilizers.append((perm, signs)) | |
| if image == mirror: | |
| mirror_parities.append((-1)**inversions * math.prod(signs)) | |
| assert stabilizers == [((0, 1, 2), (1, 1, 1))] | |
| assert mirror_parities == [-1] | |
| return {"point_rows": len(source)+len(mirror), "moment_orders_checked": 3, | |
| "stabilizer_size": len(stabilizers), "mirror_map_parities": mirror_parities} | |
| def check_reference_tables(): | |
| costs = {row["case_id"]: row for row in table("transport_regressions")} | |
| assert len(costs) == 3 and all(row["one_sided_squared_cost"] == 0 for row in costs.values()) | |
| assert costs["two-point"]["single_frame_squared_cost"] == 2 | |
| assert math.isclose(costs["square-to-octagon"]["single_frame_squared_cost"], 2-2*math.cos(math.pi/8)) | |
| assert costs["continuous-entropy"]["single_frame_squared_cost"] == 1/12 | |
| assert costs["continuous-entropy"]["one_sided_value_kind"] == "zero-regularization limit" | |
| thresholds = list(table("quantum_thresholds")) | |
| assert [row["h"] for row in thresholds] == list(range(1, 8)) | |
| for row in thresholds: | |
| h = row["h"] | |
| assert row["first_block_sizes"] == [2*h+1, 2*h+3, 2*h+5] | |
| assert row["threshold"] == sum(row["first_block_sizes"]) == 6*h+9 | |
| assert Fraction(row["determinant_without_amplitude_factor"]) == Fraction(3, 16)**(3*h+3) | |
| pauli = list(table("pauli_trials")) | |
| assert [row["true_determinant_sign"] for row in pauli] == [1, -1] | |
| assert all(row["true_determinant_sign"] == row["tomographic_determinant_sign"] for row in pauli) | |
| return {"transport_regressions": len(costs), "quantum_thresholds": len(thresholds), "pauli_trials": len(pauli)} | |
| if __name__ == "__main__": | |
| print(json.dumps({"bloch_pairs": check_bloch_pairs(), "fractal": check_fractal(), | |
| "reference_tables": check_reference_tables()}, indent=2)) | |