File size: 20,947 Bytes
07b428c | 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 | /*
* quhit_triality.h β The Triality Quhit
*
* A new quantum primitive based on the CMY geometric principle:
* three mutually-defining views (Edge/Vertex/Diagonal) where each
* view's structure IS the other views' structure in a different role.
*
* Edge of A = Vertex of B = Diagonal of C (cyclic)
*
* The triality quhit stores state in all three views with lazy
* conversion. Gates automatically execute in their cheapest view:
* Phase gates β Edge view O(D)
* Shift gates β Vertex view O(D)
* Conjugate ops β Diagonal view O(D)
* General β any view O(DΒ²)
*
* Average gate cost: O(12) instead of O(36). 3Γ free speedup.
*
*/
#ifndef QUHIT_TRIALITY_H
#define QUHIT_TRIALITY_H
#include <stdint.h>
#include "s6_exotic.h"
#define TRI_D 6
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* VIEW IDENTIFIERS
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
#define VIEW_EDGE 0 /* Computational basis β Yellow square */
#define VIEW_VERTEX 1 /* Fourier basis (DFTβ) β Cyan square */
#define VIEW_DIAGONAL 2 /* Conjugate Fourier (DFTβΒ²) β Magenta square */
#define VIEW_FOLDED 3 /* Antipodal fold: Stage 1 of factored DFTβ */
#define VIEW_EXOTIC 4 /* Exotic fold: syntheme-parameterized (outer automorphism) */
#define VIEW_TETRA 5 /* Tetrahedral eigenbasis: DFTβ eigenspace decomposition */
/* Dirty bitmask: bit 0-5 for each view */
#define DIRTY_EDGE 0x01
#define DIRTY_VERTEX 0x02
#define DIRTY_DIAGONAL 0x04
#define DIRTY_FOLDED 0x08
#define DIRTY_EXOTIC 0x10
#define DIRTY_TETRA 0x20
#define DIRTY_ALL 0x3F
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* THE TRIALITY QUHIT
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
typedef struct {
/* Three views of the same quantum state */
double edge_re[TRI_D], edge_im[TRI_D]; /* |Οβ© in computational basis */
double vertex_re[TRI_D], vertex_im[TRI_D]; /* |Οβ© in Fourier basis */
double diag_re[TRI_D], diag_im[TRI_D]; /* |Οβ© in conjugate basis */
double folded_re[TRI_D], folded_im[TRI_D]; /* Antipodal fold intermediate */
double exotic_re[TRI_D], exotic_im[TRI_D]; /* Exotic fold (alt syntheme) */
double tetra_re[TRI_D], tetra_im[TRI_D]; /* DFTβ eigenbasis coefficients */
int exotic_syntheme; /* Which syntheme to use for exotic view */
uint8_t dirty; /* Which views are stale (bits 0-3) */
uint8_t primary; /* Which view was last written (0/1/2/3) */
/* ββ Enhancement flags ββ */
int8_t eigenstate_class; /* -1=unknown, 0..3=DFTβ eigenvalue {1,-1,i,-i} */
uint8_t active_mask; /* Bitmask of non-zero basis states (6 bits) */
uint8_t active_count; /* popcount(active_mask), 1..6 */
uint8_t real_valued; /* 1 if all imaginary parts are zero */
/* ββ Exotic invariant cache (Fix #5) ββ */
double cached_delta; /* Cached exotic invariant Ξ */
double cached_fingerprint[11];/* Cached conjugacy-class deltas */
uint8_t delta_valid; /* 1 if cached values are up-to-date */
} TrialityQuhit;
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* TRIALITY PAIR β Two entangled triality quhits
* Each partner contributes a different view to the joint state.
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
typedef struct {
double joint_re[TRI_D * TRI_D];
double joint_im[TRI_D * TRI_D];
int view_a; /* which view partner A contributes */
int view_b; /* which view partner B contributes */
} TrialityPair;
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* LIFECYCLE
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
/* Initialize to |0β© with all views clean */
void triality_init(TrialityQuhit *q);
/* Initialize to basis state |kβ© */
void triality_init_basis(TrialityQuhit *q, int k);
/* Copy */
void triality_copy(TrialityQuhit *dst, const TrialityQuhit *src);
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* VIEW MANAGEMENT β Lazy DFTβ conversion
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
/* Ensure a specific view is up-to-date (converts from primary if dirty) */
void triality_ensure_view(TrialityQuhit *q, int view);
/* Force recompute all views from primary */
void triality_sync_all(TrialityQuhit *q);
/* Get read-only access to a view (ensures it first) */
const double *triality_view_re(TrialityQuhit *q, int view);
const double *triality_view_im(TrialityQuhit *q, int view);
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* OPTIMAL-VIEW GATES β O(D) when gate matches view
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
/* Phase gate: |kβ© β e^{iΟβ}|kβ© β diagonal in EDGE view, O(D) */
void triality_phase(TrialityQuhit *q, const double *phi_re, const double *phi_im);
/* Single-phase: |kβ© β e^{iΟ}|kβ©, all others unchanged β O(1) */
void triality_phase_single(TrialityQuhit *q, int k, double phi_re, double phi_im);
/* Z gate: |kβ© β Ο^k |kβ© β diagonal in EDGE view, O(D) */
void triality_z(TrialityQuhit *q);
/* Shift gate: |kβ© β |k+Ξ΄ mod Dβ© β diagonal in VERTEX view, O(D) */
void triality_shift(TrialityQuhit *q, int delta);
/* X gate: |kβ© β |k+1 mod Dβ© β diagonal in VERTEX view, O(D) */
void triality_x(TrialityQuhit *q);
/* DFTβ: rotates edgeβvertexβdiagonalβedge β view rotation, O(DΒ²) once */
void triality_dft(TrialityQuhit *q);
/* Inverse DFTβ */
void triality_idft(TrialityQuhit *q);
/* General unitary in a specific view β O(DΒ²) */
void triality_unitary(TrialityQuhit *q, int view,
const double *U_re, const double *U_im);
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* CZ GATE β O(D) in edge view (diagonal)
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
void triality_cz(TrialityQuhit *a, TrialityQuhit *b);
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* MEASUREMENT β O(D) via cached view
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
/* Measure in a specific view basis. Returns outcome 0..D-1. Collapses state. */
int triality_measure(TrialityQuhit *q, int view, uint64_t *rng_state);
/* Probability distribution in a view β O(D), no collapse */
void triality_probabilities(TrialityQuhit *q, int view, double *probs);
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* TRIALITY ROTATION β The geometric heart
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
/* Rotate the role assignment: EdgeβVertexβDiagonalβEdge
* This is a FREE operation β it just relabels which view is which.
* No amplitudes are modified. O(1). */
void triality_rotate(TrialityQuhit *q);
/* Inverse rotation: DiagonalβVertexβEdgeβDiagonal. O(1). */
void triality_rotate_inv(TrialityQuhit *q);
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* Sβ OUTER AUTOMORPHISM β Exotic Extensions
*
* Sβ is the ONLY symmetric group with a non-trivial outer automorphism.
* These functions exploit this D=6-unique structure.
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
/* Initialize the exotic engine (builds Ο table). Call once at startup. */
void triality_exotic_init(void);
/* Set which syntheme the exotic view uses (default: 0 = {(01),(23),(45)}) */
void triality_set_exotic_syntheme(TrialityQuhit *q, int syntheme_idx);
/* Fold using any of the 15 synthemes instead of the default antipodal */
void triality_fold_syntheme(TrialityQuhit *q, int syntheme_idx);
void triality_unfold_syntheme(TrialityQuhit *q, int syntheme_idx);
/* Apply exotic gate: uses Ο(Ο) instead of Ο. O(D). */
void triality_exotic_gate(TrialityQuhit *q, S6Perm sigma);
/* Dual CZ: standard CZ + exotic channel information. Returns the
* statistical distance between standard and exotic channels. */
double triality_cz_dual(TrialityQuhit *a, TrialityQuhit *b);
/* Measure in the exotic fold basis. Returns outcome 0..D-1. */
int triality_measure_exotic(TrialityQuhit *q, int syntheme_idx, uint64_t *rng_state);
/* Dual measurement: returns both standard and exotic outcomes.
* Exotic outcome is in *exotic_outcome. Standard is returned. */
int triality_measure_dual(TrialityQuhit *q, int view, int exotic_syntheme,
uint64_t *rng_state, int *exotic_outcome);
/* 6-fold rotation: cycles through all 6 synthematic views.
* Standard rotate: EdgeβVertexβDiagonalβEdge (3-cycle, views 0β1β2β0)
* Exotic rotate: Also cycles the exotic syntheme through its total.
* This accesses the full Aut(Sβ) β
Sβ β Zβ structure. */
void triality_rotate_exotic(TrialityQuhit *q);
/* Probabilities in both standard and exotic bases β no collapse */
void triality_dual_probabilities(TrialityQuhit *q, int view,
double *probs_std, double *probs_exo);
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* GEOMETRIC COSMOLOGY ENHANCEMENTS
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
/* ββ Enhancement 1: Folded View ββ */
/* Fold: pair antipodal vertices (0β3, 1β4, 2β5) via Hadamard.
* This is Stage 1 of the factored DFTβ (Cooley-Tukey 6=2Γ3).
* vesica[k] = (Ο[k] + Ο[k+3]) / β2 (k=0,1,2)
* wave[k] = (Ο[k] - Ο[k+3]) / β2 (k=0,1,2) */
void triality_fold(TrialityQuhit *q);
void triality_unfold(TrialityQuhit *q);
/* Convert EdgeβVertex via the folded intermediate (O(18) vs O(36)) */
void triality_ensure_view_via_fold(TrialityQuhit *q, int target_view);
/* ββ Enhancement 5: Tetrahedral Eigenbasis ββ */
/* Decompose state into DFTβ eigenspaces {Ξ»=1(Γ2), Ξ»=-1(Γ2), Ξ»=i, Ξ»=-i}.
* Once cached, all view conversions and DFT/IDFT gates become O(D). */
void triality_ensure_tetra(TrialityQuhit *q);
/* Convert from tetra cache to any standard view β O(DΒ²) but avoids
* needing a clean standard view as starting point */
void triality_tetra_to_view(TrialityQuhit *q, int target_view);
/* DFTβ via tetra: multiply each eigencomponent by Ξ» β O(D) */
void triality_dft_via_tetra(TrialityQuhit *q);
void triality_idft_via_tetra(TrialityQuhit *q);
/* Cached exotic invariant β returns Ξ without recomputing if state is unchanged */
double triality_exotic_invariant_cached(TrialityQuhit *q);
void triality_exotic_fingerprint_cached(TrialityQuhit *q, double *deltas);
/* Invalidate exotic cache (called internally after state-modifying operations) */
void triality_invalidate_exotic_cache(TrialityQuhit *q);
/* ββ Enhancement 2: Eigenstate Detection ββ */
/* Detect if state is a DFTβ eigenstate. Sets eigenstate_class.
* Returns eigenstate_class (0..3) or -1 if not an eigenstate. */
int triality_detect_eigenstate(TrialityQuhit *q);
/* Clear eigenstate flag (call when non-diagonal gate is applied) */
void triality_clear_eigenstate(TrialityQuhit *q);
/* ββ Enhancement 3: Subspace Confinement ββ */
/* Recompute active_mask and active_count from current edge amplitudes */
void triality_update_mask(TrialityQuhit *q);
/* ββ Enhancement 4: Real-Valued Detection ββ */
/* Detect and set real_valued flag from current edge amplitudes */
void triality_detect_real(TrialityQuhit *q);
/* ββ Combined: refresh all enhancement flags ββ */
void triality_refresh_flags(TrialityQuhit *q);
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* DIAGNOSTICS
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
/* Print state in all three views */
void triality_print(TrialityQuhit *q, const char *label);
/* View conversion count (for benchmarking) */
typedef struct {
uint64_t edge_to_vertex;
uint64_t edge_to_diag;
uint64_t vertex_to_edge;
uint64_t vertex_to_diag;
uint64_t diag_to_edge;
uint64_t diag_to_vertex;
uint64_t edge_to_folded;
uint64_t folded_to_vertex;
uint64_t gates_edge; /* gates executed in edge view */
uint64_t gates_vertex; /* gates executed in vertex view */
uint64_t gates_diag; /* gates executed in diagonal view */
uint64_t rotations; /* O(1) triality rotations */
uint64_t eigenstate_skips; /* view conversions skipped by eigenstate flag */
uint64_t mask_skips; /* operations skipped by active_mask */
uint64_t real_fast_path; /* operations using real-valued fast path */
uint64_t exotic_folds; /* exotic syntheme fold operations */
uint64_t exotic_gates; /* exotic-automorphism gate applications */
uint64_t dual_measurements; /* dual standard+exotic measurements */
uint64_t tetra_conversions; /* view conversions via tetrahedral eigenbasis */
uint64_t tetra_dft_skips; /* DFT/IDFT operations done via tetra O(D) path */
} TrialityStats;
extern TrialityStats triality_stats;
void triality_stats_reset(void);
void triality_stats_print(void);
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* LAZY TRIALITY QUHIT β Heisenberg Picture
*
* Amplitudes are NEVER touched until measurement.
* Gates accumulate as diagonal phase vectors.
* DFTs accumulate as a counter between segments.
*
* Chain: state β F^pre0 Β· D0 β F^pre1 Β· D1 β ... β F^trailing
* Fβ΄ = I, so each count is mod 4. Pure DFT sequences cancel.
* Same-view consecutive gates fuse into one D. O(D) per gate.
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
typedef struct {
/* The frozen initial state β set once at init */
double state_re[TRI_D], state_im[TRI_D];
/* Transformation chain: array of segments.
* Each segment has a pre_dfts count (0-3 DFTs before its diagonal)
* and a diagonal phase vector applied in edge view. */
#define MAX_LAZY_SEGMENTS 64
struct {
double diag_re[TRI_D]; /* Diagonal phase vector */
double diag_im[TRI_D];
int pre_dfts; /* 0-3 DFTs to apply BEFORE this diagonal (F^4=I) */
} segments[MAX_LAZY_SEGMENTS];
int n_segments;
int trailing_dfts; /* DFTs after the last segment (accumulated) */
/* Oracle: cross-batch composite matrix.
* When segments overflow, instead of materializing, the Oracle
* compiles the chain into a 6Γ6 matrix and absorbs it here.
* At final materialize: state = oracle_M Β· initial_state, then
* any remaining segments are applied on top. */
double oracle_M_re[TRI_D][TRI_D];
double oracle_M_im[TRI_D][TRI_D];
int oracle_active; /* 1 if oracle_M contains data */
/* Stats */
uint64_t gates_fused; /* Gates absorbed into existing segment */
uint64_t segments_created; /* New segments started */
uint64_t materializations; /* Times state was materialized */
} LazyTrialityQuhit;
/* Lifecycle */
void ltri_init(LazyTrialityQuhit *q);
void ltri_init_basis(LazyTrialityQuhit *q, int k);
/* Gates β O(D) each, zero view conversions */
void ltri_z(LazyTrialityQuhit *q);
void ltri_x(LazyTrialityQuhit *q);
void ltri_shift(LazyTrialityQuhit *q, int delta);
void ltri_dft(LazyTrialityQuhit *q);
void ltri_idft(LazyTrialityQuhit *q);
void ltri_phase(LazyTrialityQuhit *q, const double *phi_re, const double *phi_im);
/* Materialize β apply accumulated transform, return edge-view amplitudes */
void ltri_materialize(LazyTrialityQuhit *q, double *out_re, double *out_im);
/* Force materialize β compile oracle + apply chain, producing a TrialityQuhit.
* Use this when a two-body operation (CZ) needs actual amplitudes. */
void ltri_force_materialize(LazyTrialityQuhit *q, TrialityQuhit *out);
/* Measure β materialize + Born sample */
int ltri_measure(LazyTrialityQuhit *q, int view, uint64_t *rng_state);
/* Stats */
void ltri_stats_print(const LazyTrialityQuhit *q);
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* HEXAGRAM INTERCONVERSION
* Convert between triality (vertex model) and hexagram (edge model).
* Requires quhit_hexagram.h and hexagram_init_tables() called first.
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
struct HexagramQuhit; /* forward declaration */
/* Convert triality quhit β hexagram quhit via Hβ transform */
void triality_to_hexagram(TrialityQuhit *src, struct HexagramQuhit *dst);
/* Convert hexagram quhit β triality quhit via Hββ transform */
void hexagram_to_triality(struct HexagramQuhit *src, TrialityQuhit *dst);
#endif /* QUHIT_TRIALITY_H */
|