| /* | |
| * 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. | |
| * | |
| */ | |
| /* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| * VIEW IDENTIFIERS | |
| * βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */ | |
| /* Dirty bitmask: bit 0-5 for each view */ | |
| /* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| * 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. */ | |
| 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); | |