File size: 45,037 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 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 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 | /*
* hpc_graph.h β The Holographic Phase Graph
*
* The Devil's alternative to SVD.
*
* SVD reaches into the interior of a tensor and numerically discovers
* structure. O(nΒ³). Dense. Bulk-seeking.
*
* HPC works from the surface: entanglement is encoded as weighted phase
* edges in a graph. Amplitudes are computed on demand via O(N+E) graph
* traversal. The state vector is never materialized.
*
* Core formula:
* Ο(iβ,...,iβ) = [Ξ _k a_k(i_k)] Γ [Ξ _edges w_e(i_a, i_b)]
*
* For CZ edges: w_e(a,b) = Ο^(aΒ·b) β EXACT, fidelity = 1.0
* For general edges: w_e(a,b) = arbitrary 6Γ6 phase matrix β bounded fidelity
* For syntheme edges: w_e determined by Sβ syntheme projector β O(1) lookup
*
* This is an extension of magic_pointer.h that supports:
* - Weighted phase edges (not just CZ)
* - Syntheme metadata per edge
* - Fidelity tracking
* - On-demand marginal probabilities
*/
#ifndef HPC_GRAPH_H
#define HPC_GRAPH_H
#include "quhit_triality.h"
#include "s6_exotic.h"
#include "born_rule.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* CONSTANTS
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
#define HPC_D 6 /* Physical dimension per site */
#define HPC_INIT_EDGES 4096 /* Initial edge capacity (grows) */
#define HPC_INIT_LOG 8192 /* Initial gate log capacity (grows) */
/* Ο = exp(2Οi/6) roots of unity β precomputed */
static const double HPC_W6_RE[6] = {
1.0, 0.5, -0.5, -1.0, -0.5, 0.5
};
static const double HPC_W6_IM[6] = {
0.0, 0.866025403784438647, 0.866025403784438647,
0.0, -0.866025403784438647, -0.866025403784438647
};
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* EDGE TYPES β The Devil has more than one handshake
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
typedef enum {
HPC_EDGE_CZ, /* Exact CZ: w(a,b) = Ο^(aΒ·b), fidelity=1.0 */
HPC_EDGE_PHASE, /* General phase: w(a,b) = arbitrary 6Γ6 matrix */
HPC_EDGE_SYNTHEME /* Syntheme-projected: w from Sβ syntheme */
} HPCEdgeType;
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* WEIGHTED PHASE EDGE β One entangling interaction on the surface
*
* For CZ edges, only type + site indices are used.
* For general/syntheme edges, the full 6Γ6 phase matrix is stored.
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
typedef struct {
HPCEdgeType type;
uint64_t site_a; /* First site index */
uint64_t site_b; /* Second site index */
/* Phase matrix: w(a,b) β only used for PHASE and SYNTHEME types.
* For CZ: implicitly Ο^(aΒ·b), never stored.
* For PHASE: arbitrary complex 6Γ6 (36 complex entries, 576 bytes).
* For SYNTHEME: derived from syntheme projector. */
double w_re[HPC_D][HPC_D];
double w_im[HPC_D][HPC_D];
/* Syntheme metadata (only for SYNTHEME type) */
uint8_t syntheme_id; /* Which of 15 synthemes (0-14) */
uint8_t total_id; /* Which of 6 synthematic totals (0-5) */
/* Quality metric */
double fidelity; /* 1.0 = lossless, 0.0 = total loss */
} HPCEdge;
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* GATE LOG ENTRY β Recording what was applied
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
typedef enum {
HPC_GATE_LOCAL_DFT,
HPC_GATE_LOCAL_PHASE,
HPC_GATE_LOCAL_SHIFT,
HPC_GATE_LOCAL_UNITARY,
HPC_GATE_CZ,
HPC_GATE_GENERAL_2SITE,
HPC_GATE_INIT
} HPCGateType;
typedef struct {
HPCGateType type;
uint64_t site_a;
uint64_t site_b; /* Only for 2-site gates */
double params[12]; /* Gate-specific parameters */
double fidelity; /* Encoding fidelity for this gate */
} HPCGateEntry;
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* PER-SITE ADJACENCY LIST β O(degree) edge lookup
*
* Each site maintains a list of edge indices that touch it.
* This is the optimization that turns O(NΓE) β O(NΓdegree) = O(N).
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
#define HPC_ADJ_INIT 16 /* Initial adjacency list capacity per site */
typedef struct {
uint64_t *edge_ids; /* Indices into the graph's edge array */
uint64_t count; /* Number of edges touching this site */
uint64_t capacity; /* Allocated capacity */
} HPCAdjList;
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* HPC GRAPH β The Devil's state representation
*
* This struct IS the state. The 6^N state vector does not exist.
* Entanglement is a graph. Amplitudes are computed on demand.
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
typedef struct {
/* ββ Sites ββ */
uint64_t n_sites;
TrialityQuhit *locals; /* Per-site local states */
/* ββ Phase Graph ββ */
uint64_t n_edges;
uint64_t edge_cap;
HPCEdge *edges; /* Weighted phase edge list */
/* ββ Adjacency Lists ββ O(1) per-site edge lookup */
HPCAdjList *adj; /* Per-site adjacency lists */
/* ββ Gate Log ββ */
uint64_t n_log;
uint64_t log_cap;
HPCGateEntry *gate_log;
/* ββ Statistics ββ */
uint64_t amp_evals; /* Amplitude evaluations performed */
uint64_t prob_evals; /* Probability evaluations */
uint64_t measurements; /* Measurements performed */
uint64_t cz_edges; /* Number of exact CZ edges */
uint64_t phase_edges; /* Number of general phase edges */
uint64_t syntheme_edges; /* Number of syntheme-encoded edges */
double min_fidelity; /* Worst fidelity across all edges */
double avg_fidelity; /* Average fidelity */
} HPCGraph;
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* LIFECYCLE
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
static inline HPCGraph *hpc_create(uint64_t n_sites)
{
HPCGraph *g = (HPCGraph *)calloc(1, sizeof(HPCGraph));
if (!g) return NULL;
g->n_sites = n_sites;
g->locals = (TrialityQuhit *)calloc(n_sites, sizeof(TrialityQuhit));
if (!g->locals) { free(g); return NULL; }
for (uint64_t i = 0; i < n_sites; i++)
triality_init(&g->locals[i]);
g->edge_cap = (n_sites < HPC_INIT_EDGES) ? n_sites * 2 + 16 : HPC_INIT_EDGES;
g->edges = (HPCEdge *)calloc(g->edge_cap, sizeof(HPCEdge));
g->n_edges = 0;
/* Initialize per-site adjacency lists */
g->adj = (HPCAdjList *)calloc(n_sites, sizeof(HPCAdjList));
for (uint64_t i = 0; i < n_sites; i++) {
g->adj[i].capacity = HPC_ADJ_INIT;
g->adj[i].edge_ids = (uint64_t *)calloc(HPC_ADJ_INIT, sizeof(uint64_t));
g->adj[i].count = 0;
}
g->log_cap = HPC_INIT_LOG;
g->gate_log = (HPCGateEntry *)calloc(g->log_cap, sizeof(HPCGateEntry));
g->n_log = 0;
g->min_fidelity = 1.0;
g->avg_fidelity = 1.0;
return g;
}
static inline void hpc_destroy(HPCGraph *g)
{
if (!g) return;
if (g->adj) {
for (uint64_t i = 0; i < g->n_sites; i++)
free(g->adj[i].edge_ids);
free(g->adj);
}
free(g->locals);
free(g->edges);
free(g->gate_log);
free(g);
}
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* INTERNAL: grow arrays
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
static inline void hpc_grow_edges(HPCGraph *g)
{
if (g->n_edges < g->edge_cap) return;
g->edge_cap *= 2;
g->edges = (HPCEdge *)realloc(g->edges, g->edge_cap * sizeof(HPCEdge));
}
/* Grow the graph to accommodate new_n_sites total sites.
* Reallocates locals[] and adj[] arrays, initializes new entries.
* If new_n_sites <= g->n_sites, this is a no-op. */
static inline void hpc_grow_sites(HPCGraph *g, uint64_t new_n_sites)
{
if (new_n_sites <= g->n_sites) return;
g->locals = (TrialityQuhit *)realloc(g->locals,
new_n_sites * sizeof(TrialityQuhit));
g->adj = (HPCAdjList *)realloc(g->adj,
new_n_sites * sizeof(HPCAdjList));
/* Initialize the new sites */
for (uint64_t i = g->n_sites; i < new_n_sites; i++) {
triality_init(&g->locals[i]);
g->adj[i].capacity = HPC_ADJ_INIT;
g->adj[i].edge_ids = (uint64_t *)calloc(HPC_ADJ_INIT, sizeof(uint64_t));
g->adj[i].count = 0;
}
g->n_sites = new_n_sites;
}
static inline void hpc_grow_adj(HPCAdjList *a)
{
if (a->count < a->capacity) return;
a->capacity *= 2;
a->edge_ids = (uint64_t *)realloc(a->edge_ids,
a->capacity * sizeof(uint64_t));
}
static inline void hpc_adj_add(HPCGraph *g, uint64_t site, uint64_t edge_id)
{
HPCAdjList *a = &g->adj[site];
hpc_grow_adj(a);
a->edge_ids[a->count++] = edge_id;
}
static inline void hpc_adj_remove(HPCGraph *g, uint64_t site, uint64_t edge_id)
{
HPCAdjList *a = &g->adj[site];
for (uint64_t i = 0; i < a->count; i++) {
if (a->edge_ids[i] == edge_id) {
a->edge_ids[i] = a->edge_ids[--a->count];
return;
}
}
}
/* Replace one edge ID with another in a site's adjacency list */
static inline void hpc_adj_replace(HPCGraph *g, uint64_t site,
uint64_t old_id, uint64_t new_id)
{
HPCAdjList *a = &g->adj[site];
for (uint64_t i = 0; i < a->count; i++) {
if (a->edge_ids[i] == old_id) {
a->edge_ids[i] = new_id;
return;
}
}
}
static inline void hpc_grow_log(HPCGraph *g)
{
if (g->n_log < g->log_cap) return;
g->log_cap *= 2;
g->gate_log = (HPCGateEntry *)realloc(g->gate_log,
g->log_cap * sizeof(HPCGateEntry));
}
static inline void hpc_log_gate(HPCGraph *g, HPCGateEntry entry)
{
hpc_grow_log(g);
g->gate_log[g->n_log++] = entry;
}
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* INTERNAL: update fidelity statistics
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
static inline void hpc_update_fidelity_stats(HPCGraph *g)
{
if (g->n_edges == 0) {
g->min_fidelity = 1.0;
g->avg_fidelity = 1.0;
return;
}
double sum = 0.0;
double min_f = 1.0;
for (uint64_t e = 0; e < g->n_edges; e++) {
double f = g->edges[e].fidelity;
sum += f;
if (f < min_f) min_f = f;
}
g->min_fidelity = min_f;
g->avg_fidelity = sum / g->n_edges;
}
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* LOCAL GATES β Absorbed into the local quhit state
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
static inline void hpc_set_local(HPCGraph *g, uint64_t site,
const double re[6], const double im[6])
{
TrialityQuhit *q = &g->locals[site];
for (int i = 0; i < HPC_D; i++) {
q->edge_re[i] = re[i];
q->edge_im[i] = im[i];
}
q->primary = VIEW_EDGE;
q->dirty = DIRTY_VERTEX | DIRTY_DIAGONAL | DIRTY_FOLDED;
q->delta_valid = 0;
triality_update_mask(q);
HPCGateEntry entry = { .type = HPC_GATE_INIT, .site_a = site,
.fidelity = 1.0 };
for (int i = 0; i < 6; i++) entry.params[i] = re[i];
hpc_log_gate(g, entry);
}
static inline void hpc_dft(HPCGraph *g, uint64_t site)
{
triality_dft(&g->locals[site]);
HPCGateEntry entry = { .type = HPC_GATE_LOCAL_DFT, .site_a = site,
.fidelity = 1.0 };
hpc_log_gate(g, entry);
}
static inline void hpc_phase(HPCGraph *g, uint64_t site,
const double phi_re[6], const double phi_im[6])
{
triality_phase(&g->locals[site], phi_re, phi_im);
HPCGateEntry entry = { .type = HPC_GATE_LOCAL_PHASE, .site_a = site,
.fidelity = 1.0 };
for (int i = 0; i < 6; i++) entry.params[i] = phi_re[i];
hpc_log_gate(g, entry);
}
static inline void hpc_shift(HPCGraph *g, uint64_t site, int delta)
{
triality_shift(&g->locals[site], delta);
HPCGateEntry entry = { .type = HPC_GATE_LOCAL_SHIFT, .site_a = site,
.fidelity = 1.0 };
entry.params[0] = (double)delta;
hpc_log_gate(g, entry);
}
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* CZ GATE β The Devil's perfect handshake
*
* CZ is EXACT in HPC: no truncation, no approximation, no SVD.
* The entanglement is recorded as a phase edge: w(a,b) = Ο^(aΒ·b).
* Fidelity = 1.0. Always. This is the Devil at full power.
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
static inline void hpc_cz(HPCGraph *g, uint64_t site_a, uint64_t site_b)
{
hpc_grow_edges(g);
uint64_t eid = g->n_edges;
HPCEdge *e = &g->edges[eid];
memset(e, 0, sizeof(HPCEdge));
e->type = HPC_EDGE_CZ;
e->site_a = site_a;
e->site_b = site_b;
e->fidelity = 1.0;
/* Phase matrix not stored β implicitly Ο^(aΒ·b) */
g->n_edges++;
g->cz_edges++;
/* Maintain adjacency lists */
hpc_adj_add(g, site_a, eid);
hpc_adj_add(g, site_b, eid);
HPCGateEntry entry = {
.type = HPC_GATE_CZ,
.site_a = site_a, .site_b = site_b,
.fidelity = 1.0
};
hpc_log_gate(g, entry);
}
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* GENERAL 2-SITE GATE β Encoded as a weighted phase edge
*
* For a general 2-site gate G acting on sites (a,b):
* The gate creates entanglement that we encode as a phase matrix.
* G|Ο_aβ©|Ο_bβ© = Ξ£_{j,k} G_{(j,k),(m,n)} Ο_a(m) Ο_b(n) |jβ©|kβ©
*
* We decompose G into: (local on a) Γ (phase edge) Γ (local on b)
* The phase edge captures the entangling component.
*
* For CZ: this decomposition is EXACT (CZ is already in this form).
* For general gates: this is the syntheme approximation (lossy).
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
static inline void hpc_general_2site(HPCGraph *g, uint64_t site_a,
uint64_t site_b,
const double *G_re, const double *G_im)
{
/* G is a 36Γ36 matrix (DΒ²ΓDΒ² = 36Γ36) in row-major order.
* G[(j*D+k)*D*D + (m*D+n)] = G_{(j,k),(m,n)}
*
* Phase edge extraction:
* For each (j,k), compute the dominant phase of G_{(j,k),(j,k)}.
* This captures the diagonal (phase) part of the interaction.
* Off-diagonal terms are absorbed into local state updates. */
hpc_grow_edges(g);
uint64_t eid = g->n_edges;
HPCEdge *e = &g->edges[eid];
memset(e, 0, sizeof(HPCEdge));
e->type = HPC_EDGE_PHASE;
e->site_a = site_a;
e->site_b = site_b;
/* Extract diagonal phases: w(j,k) = G_{(j,k),(j,k)} / |G_{(j,k),(j,k)}| */
double max_mag = 0.0;
double fidelity_sum = 0.0;
int fidelity_count = 0;
for (int j = 0; j < HPC_D; j++) {
for (int k = 0; k < HPC_D; k++) {
int idx = (j * HPC_D + k) * HPC_D * HPC_D + (j * HPC_D + k);
double g_re = G_re[idx];
double g_im = G_im[idx];
double mag = sqrt(g_re * g_re + g_im * g_im);
if (mag > 1e-15) {
e->w_re[j][k] = g_re / mag;
e->w_im[j][k] = g_im / mag;
} else {
e->w_re[j][k] = 1.0;
e->w_im[j][k] = 0.0;
}
if (mag > max_mag) max_mag = mag;
double row_norm2 = 0.0;
for (int m = 0; m < HPC_D; m++) {
for (int n = 0; n < HPC_D; n++) {
int ridx = (j * HPC_D + k) * HPC_D * HPC_D + (m * HPC_D + n);
row_norm2 += G_re[ridx] * G_re[ridx] + G_im[ridx] * G_im[ridx];
}
}
if (row_norm2 > 1e-30) {
fidelity_sum += (g_re * g_re + g_im * g_im) / row_norm2;
fidelity_count++;
}
}
}
e->fidelity = (fidelity_count > 0) ? fidelity_sum / fidelity_count : 0.0;
g->n_edges++;
g->phase_edges++;
/* Maintain adjacency lists */
hpc_adj_add(g, site_a, eid);
hpc_adj_add(g, site_b, eid);
hpc_update_fidelity_stats(g);
HPCGateEntry entry = {
.type = HPC_GATE_GENERAL_2SITE,
.site_a = site_a, .site_b = site_b,
.fidelity = e->fidelity
};
hpc_log_gate(g, entry);
}
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* THE MAGIC: Amplitude Evaluation
*
* Ο(iβ,...,iβ) = [Ξ _k a_k(i_k)] Γ [Ξ _edges w_e(i_a, i_b)]
*
* Cost: O(N + E) β linear in sites + edges
* Memory: O(1) additional
*
* For CZ edges: w_e(a,b) = Ο^(aΒ·b) β precomputed lookup, no math
* For PHASE/SYNTHEME edges: w_e(a,b) from stored 6Γ6 matrix
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
static inline void hpc_amplitude(const HPCGraph *g,
const uint32_t *indices,
double *out_re, double *out_im)
{
double re = 1.0, im = 0.0;
/* Step 1: Product of local amplitudes β O(N) */
for (uint64_t k = 0; k < g->n_sites; k++) {
uint32_t idx = indices[k];
const TrialityQuhit *q = &g->locals[k];
double a_re = q->edge_re[idx];
double a_im = q->edge_im[idx];
double new_re = re * a_re - im * a_im;
double new_im = re * a_im + im * a_re;
re = new_re;
im = new_im;
}
/* Step 2: Phase edge accumulation β O(E) */
for (uint64_t e = 0; e < g->n_edges; e++) {
const HPCEdge *edge = &g->edges[e];
uint32_t ia = indices[edge->site_a];
uint32_t ib = indices[edge->site_b];
double w_re, w_im;
if (edge->type == HPC_EDGE_CZ) {
/* CZ: Ο^(iaΒ·ib) β precomputed, O(1) */
uint32_t phase_idx = (ia * ib) % HPC_D;
w_re = HPC_W6_RE[phase_idx];
w_im = HPC_W6_IM[phase_idx];
} else {
/* PHASE or SYNTHEME: lookup from stored matrix */
w_re = edge->w_re[ia][ib];
w_im = edge->w_im[ia][ib];
}
double new_re = re * w_re - im * w_im;
double new_im = re * w_im + im * w_re;
re = new_re;
im = new_im;
}
*out_re = re;
*out_im = im;
((HPCGraph *)g)->amp_evals++;
}
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* PROBABILITY β |Ο(iβ,...,iβ)|Β²
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
static inline double hpc_probability(const HPCGraph *g,
const uint32_t *indices)
{
double re, im;
hpc_amplitude(g, indices, &re, &im);
((HPCGraph *)g)->prob_evals++;
return re * re + im * im;
}
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* MARGINAL PROBABILITY β P(site_k = v)
*
* Uses per-site adjacency lists for O(degree) edge lookup.
* Only enumerates sites connected by edges to site k.
* Disconnected sites contribute 1.0 (they're normalized independently).
*
* OPTIMIZED: O(degree) edge lookup via adjacency list.
* Old version: O(E) scan β O(NΓE) = O(NΒ²) total.
* New version: O(degree) lookup β O(NΓdegree) = O(N) for bounded-degree lattices.
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
static inline double hpc_marginal(const HPCGraph *g,
uint64_t site, uint32_t value)
{
const HPCAdjList *adj = &g->adj[site];
/* Product state: no edges touching this site */
if (adj->count == 0) {
const TrialityQuhit *q = &g->locals[site];
return q->edge_re[value] * q->edge_re[value] +
q->edge_im[value] * q->edge_im[value];
}
/* Find unique connected sites via adjacency list β O(degree) */
uint64_t connected[128];
uint64_t conn_edge_ids[512]; /* Edge IDs in connected subsystem */
uint64_t n_connected = 0;
uint64_t n_conn_edges = 0;
for (uint64_t i = 0; i < adj->count; i++) {
uint64_t eid = adj->edge_ids[i];
const HPCEdge *edge = &g->edges[eid];
uint64_t partner = (edge->site_a == site) ? edge->site_b : edge->site_a;
/* Add edge to subsystem edge list */
if (n_conn_edges < 512)
conn_edge_ids[n_conn_edges++] = eid;
/* Add partner to connected list (dedup) */
int found = 0;
for (uint64_t c = 0; c < n_connected; c++)
if (connected[c] == partner) { found = 1; break; }
if (!found && n_connected < 128)
connected[n_connected++] = partner;
}
/* Also find edges between connected partners (not touching site)
* by scanning adjacency lists of connected sites β O(degreeΒ²) */
for (uint64_t c = 0; c < n_connected; c++) {
const HPCAdjList *padj = &g->adj[connected[c]];
for (uint64_t i = 0; i < padj->count; i++) {
uint64_t eid = padj->edge_ids[i];
const HPCEdge *edge = &g->edges[eid];
uint64_t sa = edge->site_a, sb = edge->site_b;
if (sa == site || sb == site) continue; /* Already counted */
/* Check if both ends are in connected set */
int a_in = 0, b_in = 0;
for (uint64_t c2 = 0; c2 < n_connected; c2++) {
if (connected[c2] == sa) a_in = 1;
if (connected[c2] == sb) b_in = 1;
}
if (a_in && b_in) {
/* Dedup edge */
int dup = 0;
for (uint64_t e2 = 0; e2 < n_conn_edges; e2++)
if (conn_edge_ids[e2] == eid) { dup = 1; break; }
if (!dup && n_conn_edges < 512)
conn_edge_ids[n_conn_edges++] = eid;
}
}
}
/* βββ Component 4: Ξ-Gated Fast Path βββ
* Instead of enumerating all D^n_connected configurations,
* only enumerate basis states that have nonzero amplitude
* (tracked by active_mask). For states confined to k of 6
* basis states, this reduces from 6^n to k^n configs.
*
* From the Faustian Pact: Ξβ0 states use fewer basis states,
* making this optimization most effective when it matters most. */
/* Build per-partner active state lists */
uint32_t partner_active[128][6];
uint32_t partner_active_count[128];
uint64_t n_configs = 1;
for (uint64_t c = 0; c < n_connected; c++) {
const TrialityQuhit *q_c = &g->locals[connected[c]];
uint8_t mask = q_c->active_mask ? q_c->active_mask : 0x3F;
int cnt = 0;
for (int k = 0; k < HPC_D; k++)
if (mask & (1 << k)) partner_active[c][cnt++] = k;
partner_active_count[c] = cnt;
n_configs *= cnt;
}
double total_prob = 0.0;
for (uint64_t cfg = 0; cfg < n_configs; cfg++) {
uint32_t partner_vals[128];
uint64_t tmp = cfg;
for (uint64_t c = 0; c < n_connected; c++) {
uint32_t idx_in_active = tmp % partner_active_count[c];
partner_vals[c] = partner_active[c][idx_in_active];
tmp /= partner_active_count[c];
}
/* Compute amplitude for this configuration */
const TrialityQuhit *q_site = &g->locals[site];
double amp_re = q_site->edge_re[value];
double amp_im = q_site->edge_im[value];
for (uint64_t c = 0; c < n_connected; c++) {
const TrialityQuhit *q_p = &g->locals[connected[c]];
uint32_t pv = partner_vals[c];
double p_re = q_p->edge_re[pv], p_im = q_p->edge_im[pv];
double new_re = amp_re * p_re - amp_im * p_im;
double new_im = amp_re * p_im + amp_im * p_re;
amp_re = new_re;
amp_im = new_im;
}
/* Phase contributions from edges in the connected subsystem only */
for (uint64_t ei = 0; ei < n_conn_edges; ei++) {
const HPCEdge *edge = &g->edges[conn_edge_ids[ei]];
uint64_t sa = edge->site_a;
uint64_t sb = edge->site_b;
uint32_t va = 0, vb = 0;
/* Resolve values for both endpoints */
if (sa == site) {
va = value;
for (uint64_t c = 0; c < n_connected; c++)
if (connected[c] == sb) { vb = partner_vals[c]; break; }
} else if (sb == site) {
vb = value;
for (uint64_t c = 0; c < n_connected; c++)
if (connected[c] == sa) { va = partner_vals[c]; break; }
} else {
for (uint64_t c = 0; c < n_connected; c++) {
if (connected[c] == sa) va = partner_vals[c];
if (connected[c] == sb) vb = partner_vals[c];
}
}
double w_re, w_im;
if (edge->type == HPC_EDGE_CZ) {
uint32_t phase_idx = (va * vb) % HPC_D;
w_re = HPC_W6_RE[phase_idx];
w_im = HPC_W6_IM[phase_idx];
} else {
w_re = edge->w_re[va][vb];
w_im = edge->w_im[va][vb];
}
double new_re = amp_re * w_re - amp_im * w_im;
double new_im = amp_re * w_im + amp_im * w_re;
amp_re = new_re;
amp_im = new_im;
}
total_prob += amp_re * amp_re + amp_im * amp_im;
}
return total_prob;
}
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* EDGE COMPACTION β Merge parallel CZ edges
*
* Multiple CZ edges between the same pair of sites can be merged:
* CZ Γ CZ = CZ with phase Ο^(2Β·aΒ·b) β equivalent to CZ^2
* n CZ edges β one edge with accumulated phase Ο^(nΒ·aΒ·b)
*
* For n β‘ 0 mod 6: the edge cancels (Ο^6 = 1) β remove entirely.
* For n β‘ 1 mod 6: standard CZ.
* For n β‘ 3 mod 6: anti-CZ (ΟΒ³ = -1).
*
* This preserves perfect phase coherence at any lattice scale.
* Without compaction, d-wave pairing bleeds out as parallel edges
* fragment the phase structure.
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
static inline void hpc_compact_edges(HPCGraph *g)
{
/* Count CZ edges between each pair, merge into accumulated phase.
* For bounded-degree lattices, this is O(E Γ degree) β O(E). */
for (uint64_t e = 0; e < g->n_edges; ) {
HPCEdge *edge = &g->edges[e];
if (edge->type != HPC_EDGE_CZ) { e++; continue; }
uint64_t sa = edge->site_a, sb = edge->site_b;
/* Count and remove duplicate CZ edges for this pair */
int cz_count = 1; /* This edge counts as 1 */
for (uint64_t e2 = e + 1; e2 < g->n_edges; ) {
HPCEdge *other = &g->edges[e2];
if (other->type == HPC_EDGE_CZ &&
((other->site_a == sa && other->site_b == sb) ||
(other->site_a == sb && other->site_b == sa))) {
cz_count++;
/* Remove adjacency entries for the duplicate */
hpc_adj_remove(g, other->site_a, e2);
hpc_adj_remove(g, other->site_b, e2);
/* Swap-remove the duplicate edge */
uint64_t last = g->n_edges - 1;
if (e2 != last) {
/* Update adjacency for the edge being swapped in */
hpc_adj_replace(g, g->edges[last].site_a, last, e2);
hpc_adj_replace(g, g->edges[last].site_b, last, e2);
g->edges[e2] = g->edges[last];
}
g->n_edges--;
g->cz_edges--;
} else {
e2++;
}
}
/* Reduce cz_count mod 6 */
int reduced = cz_count % 6;
if (reduced == 0) {
/* Complete cancellation: Ο^(6k) = 1 β remove edge entirely */
hpc_adj_remove(g, sa, e);
hpc_adj_remove(g, sb, e);
uint64_t last = g->n_edges - 1;
if (e != last) {
hpc_adj_replace(g, g->edges[last].site_a, last, e);
hpc_adj_replace(g, g->edges[last].site_b, last, e);
g->edges[e] = g->edges[last];
}
g->n_edges--;
g->cz_edges--;
} else if (reduced == 1) {
/* Standard CZ β already correct, just advance */
e++;
} else {
/* Convert to general phase edge with accumulated phase:
* w(a,b) = Ο^(reduced Β· a Β· b) */
edge->type = HPC_EDGE_PHASE;
edge->fidelity = 1.0; /* Still exact */
for (int a = 0; a < HPC_D; a++) {
for (int b = 0; b < HPC_D; b++) {
uint32_t phase_idx = (uint32_t)(reduced * a * b) % HPC_D;
edge->w_re[a][b] = HPC_W6_RE[phase_idx];
edge->w_im[a][b] = HPC_W6_IM[phase_idx];
}
}
g->cz_edges--;
g->phase_edges++;
e++;
}
}
}
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* BORN SAMPLING β Collapse site k
*
* Uses adjacency lists for O(degree) edge identification.
* Absorbs CZ phases into partners, removes resolved edges.
* This IS measurement-induced disentanglement.
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
static inline uint32_t hpc_measure(HPCGraph *g, uint64_t site,
double random_01)
{
/* Compute marginals */
double probs[HPC_D];
double total = 0.0;
for (int v = 0; v < HPC_D; v++) {
probs[v] = hpc_marginal(g, site, v);
total += probs[v];
}
if (total > 0) {
for (int v = 0; v < HPC_D; v++) probs[v] /= total;
}
/* Sample */
double cumul = 0.0;
uint32_t outcome = HPC_D - 1;
for (int v = 0; v < HPC_D; v++) {
cumul += probs[v];
if (random_01 <= cumul) { outcome = v; break; }
}
/* Collapse local state to |outcomeβ© */
for (int v = 0; v < HPC_D; v++) {
g->locals[site].edge_re[v] = (v == (int)outcome) ? 1.0 : 0.0;
g->locals[site].edge_im[v] = 0.0;
}
g->locals[site].primary = VIEW_EDGE;
g->locals[site].dirty = DIRTY_VERTEX | DIRTY_DIAGONAL | DIRTY_FOLDED;
g->locals[site].delta_valid = 0;
triality_update_mask(&g->locals[site]);
/* Collect edge IDs touching this site from adjacency list β O(degree) */
uint64_t edges_to_remove[512];
uint64_t n_remove = 0;
const HPCAdjList *adj = &g->adj[site];
for (uint64_t i = 0; i < adj->count && n_remove < 512; i++)
edges_to_remove[n_remove++] = adj->edge_ids[i];
/* Absorb phases and remove edges */
for (uint64_t r = 0; r < n_remove; r++) {
uint64_t eid = edges_to_remove[r];
if (eid >= g->n_edges) continue; /* Already removed by swap */
HPCEdge *edge = &g->edges[eid];
/* Verify this edge still touches our site (may have been swapped) */
if (edge->site_a != site && edge->site_b != site) continue;
uint64_t partner = (edge->site_a == site) ?
edge->site_b : edge->site_a;
TrialityQuhit *p = &g->locals[partner];
/* Absorb the phase: partner[k] *= w(outcome, k) or w(k, outcome) */
for (int k = 0; k < HPC_D; k++) {
double w_re, w_im;
if (edge->type == HPC_EDGE_CZ) {
uint32_t phase_idx = (outcome * k) % HPC_D;
w_re = HPC_W6_RE[phase_idx];
w_im = HPC_W6_IM[phase_idx];
} else if (edge->site_a == site) {
w_re = edge->w_re[outcome][k];
w_im = edge->w_im[outcome][k];
} else {
w_re = edge->w_re[k][outcome];
w_im = edge->w_im[k][outcome];
}
double old_re = p->edge_re[k], old_im = p->edge_im[k];
p->edge_re[k] = old_re * w_re - old_im * w_im;
p->edge_im[k] = old_re * w_im + old_im * w_re;
}
p->dirty = DIRTY_VERTEX | DIRTY_DIAGONAL | DIRTY_FOLDED;
p->delta_valid = 0;
/* Track edge type removal */
if (edge->type == HPC_EDGE_CZ) g->cz_edges--;
else if (edge->type == HPC_EDGE_PHASE) g->phase_edges--;
else g->syntheme_edges--;
/* Remove from adjacency lists */
hpc_adj_remove(g, site, eid);
hpc_adj_remove(g, partner, eid);
/* Swap-remove the edge */
uint64_t last = g->n_edges - 1;
if (eid != last) {
/* Update adjacency for the swapped-in edge */
hpc_adj_replace(g, g->edges[last].site_a, last, eid);
hpc_adj_replace(g, g->edges[last].site_b, last, eid);
g->edges[eid] = g->edges[last];
/* Update remaining removal targets that pointed to 'last' */
for (uint64_t r2 = r + 1; r2 < n_remove; r2++)
if (edges_to_remove[r2] == last)
edges_to_remove[r2] = eid;
}
g->n_edges--;
}
g->measurements++;
hpc_update_fidelity_stats(g);
return outcome;
}
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* NORMALIZATION CHECK β Ξ£ |Ο|Β² over ALL indices
*
* Cost: O(D^N Γ (N+E)) β small N only!
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
static inline double hpc_norm_sq(const HPCGraph *g)
{
if (g->n_sites > 8) {
fprintf(stderr, "hpc_norm_sq: N=%lu too large for brute force\n",
g->n_sites);
return -1.0;
}
uint64_t total_configs = 1;
for (uint64_t i = 0; i < g->n_sites; i++) total_configs *= HPC_D;
double norm = 0.0;
uint32_t indices[8];
for (uint64_t cfg = 0; cfg < total_configs; cfg++) {
uint64_t tmp = cfg;
for (uint64_t i = 0; i < g->n_sites; i++) {
indices[i] = tmp % HPC_D;
tmp /= HPC_D;
}
norm += hpc_probability(g, indices);
}
return norm;
}
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* EXOTIC INVARIANT β weighted Ξ across all sites
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
static inline double hpc_exotic_invariant(HPCGraph *g)
{
double total = 0.0;
for (uint64_t i = 0; i < g->n_sites; i++)
total += triality_exotic_invariant_cached(&g->locals[i]);
return total / g->n_sites;
}
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* ENTROPY ESTIMATE β across a bipartition cut
*
* CZ edges contribute exactly logβ(D) bits per crossing edge.
* General edges contribute fidelity-weighted logβ(D) bits.
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
static inline double hpc_entropy_cut(const HPCGraph *g, uint64_t cut_after)
{
double entropy = 0.0;
for (uint64_t e = 0; e < g->n_edges; e++) {
uint64_t sa = g->edges[e].site_a;
uint64_t sb = g->edges[e].site_b;
if ((sa <= cut_after && sb > cut_after) ||
(sb <= cut_after && sa > cut_after)) {
entropy += g->edges[e].fidelity * log2((double)HPC_D);
}
}
return entropy;
}
/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* DIAGNOSTICS
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
static inline void hpc_print_stats(const HPCGraph *g)
{
printf("βββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n");
printf("β Holographic Phase Graph Statistics β\n");
printf("β ββββββββββββββββββββββββββββββββββββββββββββββββββββββ£\n");
printf("β Sites: %10lu β\n", g->n_sites);
printf("β Total edges: %10lu β\n", g->n_edges);
printf("β CZ (exact): %10lu β\n", g->cz_edges);
printf("β Phase (lossy): %10lu β\n", g->phase_edges);
printf("β Syntheme: %10lu β\n", g->syntheme_edges);
printf("β Gate log: %10lu β\n", g->n_log);
printf("β Amp evals: %10lu β\n", g->amp_evals);
printf("β Measurements: %10lu β\n", g->measurements);
printf("β Min fidelity: %10.6f β\n", g->min_fidelity);
printf("β Avg fidelity: %10.6f β\n", g->avg_fidelity);
uint64_t mem_bytes = g->n_sites * sizeof(TrialityQuhit) +
g->n_edges * sizeof(HPCEdge) +
g->n_log * sizeof(HPCGateEntry) +
sizeof(HPCGraph);
printf("β Memory: %10lu bytes β\n", mem_bytes);
double full_sv_log = g->n_sites * log10(6.0) + log10(16.0);
printf("β Full SV: 10^%.1f bytes (impossible) β\n", full_sv_log);
printf("βββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n");
}
static inline void hpc_print_state(const HPCGraph *g, const char *label)
{
printf("ββ %s ββ\n", label);
printf(" Sites: %lu, Edges: %lu (CZ:%lu Phase:%lu Synth:%lu)\n",
g->n_sites, g->n_edges, g->cz_edges, g->phase_edges, g->syntheme_edges);
printf(" Fidelity: min=%.4f avg=%.4f\n", g->min_fidelity, g->avg_fidelity);
for (uint64_t i = 0; i < g->n_sites && i < 8; i++) {
printf(" Site %lu: [", i);
for (int j = 0; j < HPC_D; j++) {
printf("%.3f%+.3fi", g->locals[i].edge_re[j],
g->locals[i].edge_im[j]);
if (j < HPC_D - 1) printf(", ");
}
printf("]\n");
}
if (g->n_sites > 8) printf(" ... (%lu more sites)\n", g->n_sites - 8);
}
#endif /* HPC_GRAPH_H */
|