racah 0.1.1

Racah-Wigner calculus for compact Lie groups: exact SU(2) recoupling, and runtime Clebsch-Gordan / F- / R-coefficient generation for SU(N), SO(N), and Sp(2N)
Documentation
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
//! Family-generic F- and R-symbol core: the four-CGC contraction and the
//! pentagon/hexagon/F-unitarity gates, shared by the SU(N) (`crate::sun::fr`)
//! and B/C/D (`crate::bcd::fr`) surfaces.
//!
//! # Why one core, two callers
//!
//! The F-symbol is the contraction of four Clebsch–Gordan tensors over their
//! magnetic indices, leaving four outer multiplicity indices `[μ, ν, κ, λ]`
//! (SUNRepresentations.jl `sector.jl:_Fsymbol`, `:58-89`; the contraction wiring
//! and axis order match TensorKitSectors `sectors.jl:Fsymbol_from_fusiontensor`,
//! `:406-418`). The PR #17 review established that this logic depends on nothing
//! SU(N)-specific: only on an irrep label type, a fusion-multiplicity source
//! `N^c_{ab}`, a CGC provider returning **sparse real** `(m1, m2, m3, mu, value)`
//! entries, and `f64` scalars. Real-valuedness holds for B/C/D CGC too (the sweep
//! produces real orthogonal isometries), so conjugation is the identity and is
//! elided on both surfaces — value-identical to the reference. This module is
//! that logic, parameterized by the [`Family`] seam.
//!
//! # The `&mut self` seam (why it does not force interior mutability on SU(N))
//!
//! [`Family`] takes `&mut self`. That is the superset both callers satisfy: the
//! B/C/D provider is a `&mut CanonicalCatalog` (append-only generator
//! materialization is genuinely mutating), while the SU(N) provider is a
//! stateless zero-sized type whose methods delegate to the process-global CGC
//! cache via free functions and simply ignore the `&mut`. A `&mut` to a local ZST
//! is always available, so SU(N) pays no lock and grows no interior-mutability
//! cell — the mutability is real for one impl and vacuous for the other.
//!
//! # Contraction strategy: sparse key-matching accumulation
//!
//! CGC are sparse, and each F/R element is a sum over matched magnetic-index
//! keys, so the contraction is a sequence of sparse joins over shared magnetic
//! indices — data-structure code, not a dense numeric kernel (issue #16:
//! "sparse-aware assembly is acceptable and likely optimal"; a densified GEMM
//! would be mostly zeros and pay a densify+GEMM+scatter tax the join avoids).

use std::collections::HashMap;
use std::hash::Hash;

/// CGC entries grouped by one magnetic index → `(idx_i, idx_j, mult, value)`.
type GroupBy4 = HashMap<u32, Vec<(u32, u32, u32, f64)>>;

/// A partial-contraction result keyed by the three shared magnetic indices
/// `(ma, mb, mc)` → list of `(mult_p, mult_q, value)`.
type PairGroup = HashMap<(u32, u32, u32), Vec<(u32, u32, f64)>>;

/// Verification-gate tolerances. Not reference constants: sized well above the
/// f64 round-off floor of the sparse contractions (a handful of products and
/// sums of `O(1)` coefficients, `~1e-13`) and far below any structural error
/// (a wrong sign/index is `O(1)`), so a genuine defect trips them while
/// faithful round-off does not. The pentagon/hexagon budgets are looser because
/// they compose several F/R blocks. Shared by both families: the B/C/D CGC come
/// from an SVD sweep and carry the same `f64` round-off character as the SU(N)
/// SVD/QR pipeline, so the same sizing rationale holds.
pub(crate) const TOL_F_UNITARY: f64 = 1.0e-9;
pub(crate) const TOL_PENTAGON: f64 = 1.0e-8;
pub(crate) const TOL_HEXAGON: f64 = 1.0e-8;

/// One nonzero entry of a sparse m-basis CGC tensor: `value` at magnetic indices
/// `(m1, m2, m3)` and outer-multiplicity `mu`. The family-neutral form the core
/// consumes; each [`Family`] adapts its native CGC representation to this.
#[derive(Clone, Copy, Debug)]
pub(crate) struct MEntry {
    /// 0-based magnetic index in the left factor.
    pub m1: u32,
    /// 0-based magnetic index in the right factor.
    pub m2: u32,
    /// 0-based magnetic index in the coupled irrep.
    pub m3: u32,
    /// 0-based outer-multiplicity index (trailing axis).
    pub mu: u32,
    /// The (real, standard-gauge) coefficient.
    pub value: f64,
}

/// The seam between the generic core and a representation family (SU(N) or
/// B/C/D). Exactly two implementors; not a public extension point.
pub(crate) trait Family {
    /// The irrep label type (used as a magnetic-block cache/memo key).
    type Irrep: Clone + Eq + Hash;
    /// The family's error type, surfaced unchanged by the core.
    type Error;

    /// `N^c_{ab}`, the fusion multiplicity of `a ⊗ b → c`. Errors on an
    /// ill-posed pairing (mixed rank/series/group) per the family's contract.
    fn mult(
        &mut self,
        a: &Self::Irrep,
        b: &Self::Irrep,
        c: &Self::Irrep,
    ) -> Result<usize, Self::Error>;

    /// The sparse real CGC entries coupling `a ⊗ b → c`. Called only for a
    /// non-empty channel (`mult > 0`), which the core checks first.
    fn cgc_entries(
        &mut self,
        a: &Self::Irrep,
        b: &Self::Irrep,
        c: &Self::Irrep,
    ) -> Result<Vec<MEntry>, Self::Error>;

    /// The fusion outputs of $a \otimes b$ (the irreps `c` with $N^c_{ab} > 0$), in a
    /// deterministic order.
    fn products(
        &mut self,
        a: &Self::Irrep,
        b: &Self::Irrep,
    ) -> Result<Vec<Self::Irrep>, Self::Error>;
}

// ---------------------------------------------------------------------------
// Dense F/R blocks (family-neutral: just dims + row-major f64 data).
// ---------------------------------------------------------------------------

/// A dense F-symbol block $F^{abc}_d[e, f]$, a rank-4 array over the outer
/// multiplicity indices $[\mu, \nu, \kappa, \lambda]$ in **row-major** order.
///
/// The axis lengths are $[N^e_{ab}, N^d_{ec}, N^f_{bc}, N^d_{af}]$
/// ($\mu, \nu, \kappa, \lambda$), matching the TensorKitSectors `GenericFusion` convention
/// (`sectors.jl:Fsymbol_from_fusiontensor`). For a multiplicity-free family
/// (e.g. SU(2)) every axis is length 1, so the block holds the single scalar.
#[derive(Clone, Debug, PartialEq)]
pub struct FBlock {
    dims: [usize; 4],
    /// Row-major over `[μ, ν, κ, λ]`.
    data: Vec<f64>,
}

impl FBlock {
    fn zeros(dims: [usize; 4]) -> Self {
        FBlock {
            dims,
            data: vec![0.0; dims[0] * dims[1] * dims[2] * dims[3]],
        }
    }

    #[inline]
    fn flat(dims: [usize; 4], mu: usize, nu: usize, kappa: usize, lambda: usize) -> usize {
        ((mu * dims[1] + nu) * dims[2] + kappa) * dims[3] + lambda
    }

    /// The axis lengths $[N^e_{ab}, N^d_{ec}, N^f_{bc}, N^d_{af}]$ ($\mu,\nu,\kappa,\lambda$).
    pub fn dims(&self) -> [usize; 4] {
        self.dims
    }

    /// The row-major `[μ, ν, κ, λ]` coefficient data.
    pub fn data(&self) -> &[f64] {
        &self.data
    }

    /// The coefficient at multiplicity indices `(μ, ν, κ, λ)`.
    ///
    /// # Panics
    ///
    /// Panics if any index is out of range for [`FBlock::dims`].
    pub fn at(&self, mu: usize, nu: usize, kappa: usize, lambda: usize) -> f64 {
        assert!(
            mu < self.dims[0] && nu < self.dims[1] && kappa < self.dims[2] && lambda < self.dims[3],
            "FBlock index out of range"
        );
        self.data[Self::flat(self.dims, mu, nu, kappa, lambda)]
    }
}

/// A dense R-symbol block $R^{ab}_c$, an $N^c_{ab} \times N^c_{ba}$ matrix in
/// **row-major** order (rows = $\mu$, cols = $\nu$).
///
/// $N^c_{ab} = N^c_{ba}$ (fusion multiplicities are symmetric), so the matrix
/// is square. For a multiplicity-free family it is the single braiding phase.
#[derive(Clone, Debug, PartialEq)]
pub struct RBlock {
    n: usize,
    /// Row-major `n × n`.
    data: Vec<f64>,
}

impl RBlock {
    fn zeros(n: usize) -> Self {
        RBlock {
            n,
            data: vec![0.0; n * n],
        }
    }

    /// The multiplicity `N^c_{ab}` (both the row and column count).
    pub fn dim(&self) -> usize {
        self.n
    }

    /// The row-major coefficient data (`N × N`, rows `μ`, cols `ν`).
    pub fn data(&self) -> &[f64] {
        &self.data
    }

    /// The braiding coefficient at `(μ, ν)`.
    ///
    /// # Panics
    ///
    /// Panics if `mu` or `nu` is `>= dim()`.
    pub fn at(&self, mu: usize, nu: usize) -> f64 {
        assert!(mu < self.n && nu < self.n, "RBlock index out of range");
        self.data[mu * self.n + nu]
    }
}

// ---------------------------------------------------------------------------
// F-symbol raw contraction (sector.jl:_Fsymbol, :58-89).
// ---------------------------------------------------------------------------

/// The raw F contraction with **reference** empty-vertex semantics (an all-zero
/// block, not an error): the reference pentagon/hexagon loops feed empty blocks
/// through harmlessly. The per-family `f_symbol` wrappers convert empty vertices
/// to a typed error before calling this.
///
/// With `A = CGC(a,b,e)`, `B = CGC(e,c,d)`, `C = CGC(b,c,f)`, `D = CGC(a,f,d)`
/// (the last two conjugated in the reference; real, so elided), and `B`/`D`
/// sliced at the first `d`-basis index (`m_d = 0`; the F-symbol is independent of
/// which `d`-state is fixed — the reference "first diagonal element"):
///
/// ```text
/// F[μ,ν,κ,λ] = Σ  D[ma,mf,λ] · C[mb,mc,mf,κ] · A[ma,mb,me,μ] · B[me,mc,ν]
///            ma,mb,me,mc,mf
/// ```
#[allow(clippy::too_many_arguments)]
pub(crate) fn f_block_raw<F: Family>(
    fam: &mut F,
    a: &F::Irrep,
    b: &F::Irrep,
    c: &F::Irrep,
    d: &F::Irrep,
    e: &F::Irrep,
    f: &F::Irrep,
) -> Result<FBlock, F::Error> {
    let n1 = fam.mult(a, b, e)?;
    let n2 = fam.mult(e, c, d)?;
    let n3 = fam.mult(b, c, f)?;
    let n4 = fam.mult(a, f, d)?;
    let dims = [n1, n2, n3, n4];
    if n1 == 0 || n2 == 0 || n3 == 0 || n4 == 0 {
        return Ok(FBlock::zeros(dims));
    }

    let cab = fam.cgc_entries(a, b, e)?; // A[ma,mb,me,μ]
    let cecd = fam.cgc_entries(e, c, d)?; // B slice at m_d=0: [me,mc,ν]
    let cbcf = fam.cgc_entries(b, c, f)?; // C[mb,mc,mf,κ]
    let cafd = fam.cgc_entries(a, f, d)?; // D slice at m_d=0: [ma,mf,λ]

    // Step 1: AB[(ma,mb,mc), (μ,ν)] = Σ_me A[ma,mb,me,μ] · B[me,mc,ν].
    // Group A and B by the shared magnetic index me, then cross each group.
    let mut a_by_me: GroupBy4 = HashMap::new();
    for x in &cab {
        a_by_me
            .entry(x.m3)
            .or_default()
            .push((x.m1, x.m2, x.mu, x.value)); // (ma, mb, μ, vA)
    }
    // B is CGC(e,c,d)[:, :, m_d=0, :]: keep only m3 == 0.
    let mut b_by_me: HashMap<u32, Vec<(u32, u32, f64)>> = HashMap::new();
    for x in &cecd {
        if x.m3 == 0 {
            b_by_me.entry(x.m1).or_default().push((x.m2, x.mu, x.value)); // (mc, ν, vB)
        }
    }
    // AB keyed by (ma,mb,mc) -> Vec<(μ, ν, value)>.
    let mut ab: PairGroup = HashMap::new();
    for (me, alist) in &a_by_me {
        let Some(blist) = b_by_me.get(me) else {
            continue;
        };
        for &(ma, mb, mu, va) in alist {
            for &(mc, nu, vb) in blist {
                ab.entry((ma, mb, mc)).or_default().push((mu, nu, va * vb));
            }
        }
    }

    // Step 2: CD[(ma,mb,mc), (κ,λ)] = Σ_mf C[mb,mc,mf,κ] · D[ma,mf,λ].
    let mut c_by_mf: GroupBy4 = HashMap::new();
    for x in &cbcf {
        c_by_mf
            .entry(x.m3)
            .or_default()
            .push((x.m1, x.m2, x.mu, x.value)); // (mb, mc, κ, vC)
    }
    // D is CGC(a,f,d)[:, :, m_d=0, :]: keep only m3 == 0.
    let mut d_by_mf: HashMap<u32, Vec<(u32, u32, f64)>> = HashMap::new();
    for x in &cafd {
        if x.m3 == 0 {
            d_by_mf.entry(x.m2).or_default().push((x.m1, x.mu, x.value)); // (ma, λ, vD)
        }
    }
    let mut cd: PairGroup = HashMap::new();
    for (mf, clist) in &c_by_mf {
        let Some(dlist) = d_by_mf.get(mf) else {
            continue;
        };
        for &(mb, mc, kappa, vc) in clist {
            for &(ma, lambda, vd) in dlist {
                cd.entry((ma, mb, mc))
                    .or_default()
                    .push((kappa, lambda, vc * vd));
            }
        }
    }

    // Step 3: F[μ,ν,κ,λ] = Σ_{ma,mb,mc} AB[(ma,mb,mc),(μ,ν)] · CD[(ma,mb,mc),(κ,λ)].
    let mut block = FBlock::zeros(dims);
    for (key, ablist) in &ab {
        let Some(cdlist) = cd.get(key) else {
            continue;
        };
        for &(mu, nu, vab) in ablist {
            for &(kappa, lambda, vcd) in cdlist {
                let idx = FBlock::flat(
                    dims,
                    mu as usize,
                    nu as usize,
                    kappa as usize,
                    lambda as usize,
                );
                block.data[idx] += vab * vcd;
            }
        }
    }
    Ok(block)
}

// ---------------------------------------------------------------------------
// R-symbol raw contraction (sector.jl:_Rsymbol, :91-110).
// ---------------------------------------------------------------------------

/// The raw R contraction with reference empty-vertex semantics (zeros, not an
/// error); the gates feed empty blocks through harmlessly.
///
/// With `A = CGC(a,b,c)`, `B = CGC(b,a,c)` sliced at `m_c = 0`:
///
/// ```text
/// R[μ, ν] = Σ  A[ma, mb, μ] · B[mb, ma, ν]      (B conjugated; real, elided)
///          ma,mb
/// ```
pub(crate) fn r_block_raw<F: Family>(
    fam: &mut F,
    a: &F::Irrep,
    b: &F::Irrep,
    c: &F::Irrep,
) -> Result<RBlock, F::Error> {
    let n1 = fam.mult(a, b, c)?; // rows μ
    let n2 = fam.mult(b, a, c)?; // cols ν  (== n1: fusion multiplicities are symmetric)
    if n1 == 0 || n2 == 0 {
        return Ok(RBlock::zeros(n1.max(n2)));
    }
    debug_assert_eq!(n1, n2, "N^c_ab == N^c_ba");

    let cab = fam.cgc_entries(a, b, c)?; // A[ma,mb,mc,μ]
    let cba = fam.cgc_entries(b, a, c)?; // B[mb,ma,mc,ν]

    // A slice at m_c = 0, keyed by (ma, mb) -> Vec<(μ, value)>.
    let mut a_map: HashMap<(u32, u32), Vec<(u32, f64)>> = HashMap::new();
    for x in &cab {
        if x.m3 == 0 {
            a_map.entry((x.m1, x.m2)).or_default().push((x.mu, x.value));
        }
    }
    // B slice at m_c = 0, keyed by (ma, mb) = (B.m2, B.m1) -> Vec<(ν, value)>.
    let mut b_map: HashMap<(u32, u32), Vec<(u32, f64)>> = HashMap::new();
    for x in &cba {
        if x.m3 == 0 {
            b_map.entry((x.m2, x.m1)).or_default().push((x.mu, x.value));
        }
    }

    let mut block = RBlock::zeros(n1);
    for (key, alist) in &a_map {
        let Some(blist) = b_map.get(key) else {
            continue;
        };
        for &(mu, va) in alist {
            for &(nu, vb) in blist {
                block.data[mu as usize * n1 + nu as usize] += va * vb;
            }
        }
    }
    Ok(block)
}

// ---------------------------------------------------------------------------
// Fusion-set helper for the gates.
// ---------------------------------------------------------------------------

/// `a ⊗ b ∩ c ⊗ d` (the pentagon/hexagon `intersect(⊗(...), ⊗(...))`).
fn intersect_products<F: Family>(
    fam: &mut F,
    a: &F::Irrep,
    b: &F::Irrep,
    c: &F::Irrep,
    d: &F::Irrep,
) -> Result<Vec<F::Irrep>, F::Error> {
    let left = fam.products(a, b)?;
    let right = fam.products(c, d)?;
    Ok(left.into_iter().filter(|k| right.contains(k)).collect())
}

/// Per-gate-call memo for F/R blocks.
///
/// A pentagon/hexagon gate references the *same* F/R block from many index
/// combinations; without memoization each reference recomputes a four-CGC
/// contraction, which for OM≥2 families is the difference between seconds and
/// many minutes. The blocks are tiny, so the memo clones them out cheaply. It is
/// *not* a process-global cache: gates use the raw zeros-for-`N=0` semantics,
/// which would pollute a public value tier with blocks `f_symbol` never stores.
struct BlockMemo<I: Clone + Eq + Hash> {
    f: HashMap<[I; 6], FBlock>,
    r: HashMap<[I; 3], RBlock>,
}

impl<I: Clone + Eq + Hash> Default for BlockMemo<I> {
    fn default() -> Self {
        BlockMemo {
            f: HashMap::new(),
            r: HashMap::new(),
        }
    }
}

impl<I: Clone + Eq + Hash> BlockMemo<I> {
    #[allow(clippy::too_many_arguments)]
    fn f_block<F: Family<Irrep = I>>(
        &mut self,
        fam: &mut F,
        a: &I,
        b: &I,
        c: &I,
        d: &I,
        e: &I,
        f: &I,
    ) -> Result<FBlock, F::Error> {
        let key = [
            a.clone(),
            b.clone(),
            c.clone(),
            d.clone(),
            e.clone(),
            f.clone(),
        ];
        if let Some(bl) = self.f.get(&key) {
            return Ok(bl.clone());
        }
        let bl = f_block_raw(fam, a, b, c, d, e, f)?;
        self.f.insert(key, bl.clone());
        Ok(bl)
    }

    fn r_block<F: Family<Irrep = I>>(
        &mut self,
        fam: &mut F,
        a: &I,
        b: &I,
        c: &I,
    ) -> Result<RBlock, F::Error> {
        let key = [a.clone(), b.clone(), c.clone()];
        if let Some(bl) = self.r.get(&key) {
            return Ok(bl.clone());
        }
        let bl = r_block_raw(fam, a, b, c)?;
        self.r.insert(key, bl.clone());
        Ok(bl)
    }
}

// ---------------------------------------------------------------------------
// Gate 1: F-move unitarity — worst |(M Mᵀ - I)_{ij}|.
// ---------------------------------------------------------------------------

/// The worst F-move unitarity residual for fixed outer labels `(a, b, c, d)`.
///
/// The F-symbols form a square matrix `M` with rows `(e, μ, ν)`
/// (`e ∈ a⊗b`, `μ ∈ [0,N^e_{ab})`, `ν ∈ [0,N^d_{ec})`) and columns `(f, κ, λ)`
/// (`f ∈ b⊗c`, `κ ∈ [0,N^f_{bc})`, `λ ∈ [0,N^d_{af})`). The two associativity
/// bases are orthonormal, so `M` is real-orthogonal (`M Mᵀ = I`); this returns
/// the worst `|(M Mᵀ - I)_{ij}|`. The per-family wrapper compares it to
/// [`TOL_F_UNITARY`] and raises the family's typed error.
pub(crate) fn f_unitarity_residual<F: Family>(
    fam: &mut F,
    a: &F::Irrep,
    b: &F::Irrep,
    c: &F::Irrep,
    d: &F::Irrep,
) -> Result<f64, F::Error> {
    // Rows: (e, μ, ν). Columns: (f, κ, λ).
    let mut rows: Vec<(F::Irrep, usize, usize)> = Vec::new();
    for e in fam.products(a, b)? {
        let n_ab_e = fam.mult(a, b, &e)?;
        let n_ec_d = fam.mult(&e, c, d)?;
        for mu in 0..n_ab_e {
            for nu in 0..n_ec_d {
                rows.push((e.clone(), mu, nu));
            }
        }
    }
    let mut cols: Vec<(F::Irrep, usize, usize)> = Vec::new();
    for f in fam.products(b, c)? {
        let n_bc_f = fam.mult(b, c, &f)?;
        let n_af_d = fam.mult(a, &f, d)?;
        for kappa in 0..n_bc_f {
            for lambda in 0..n_af_d {
                cols.push((f.clone(), kappa, lambda));
            }
        }
    }

    // M[row, col].
    let nr = rows.len();
    let nc = cols.len();
    let mut m = vec![0.0f64; nr * nc];
    let mut memo = BlockMemo::default();
    for (ri, (e, mu, nu)) in rows.iter().enumerate() {
        for (ci, (f, kappa, lambda)) in cols.iter().enumerate() {
            let block = memo.f_block(fam, a, b, c, d, e, f)?;
            m[ri * nc + ci] = block.at(*mu, *nu, *kappa, *lambda);
        }
    }

    // worst |(M Mᵀ - I)_{ij}|.
    let mut worst = 0.0f64;
    for i in 0..nr {
        for j in 0..nr {
            let mut dot = 0.0;
            for k in 0..nc {
                dot += m[i * nc + k] * m[j * nc + k];
            }
            let target = if i == j { 1.0 } else { 0.0 };
            worst = worst.max((dot - target).abs());
        }
    }
    Ok(worst)
}

// ---------------------------------------------------------------------------
// Gate 2: pentagon (TensorKitSectors sectors.jl:pentagon_equation, :786-819).
// ---------------------------------------------------------------------------

/// The worst pentagon residual for the quadruple `(a, b, c, d)` (GenericFusion
/// branch of `TensorKitSectors/sectors.jl:pentagon_equation`). For every
/// `f ∈ a⊗b`, `h ∈ c⊗d`, `g ∈ f⊗c`, `i ∈ b⊗h`, `e ∈ (g⊗d) ∩ (a⊗i)`:
///
/// ```text
/// p1[λμν κρσ] = Σ_τ F(f,c,d,e,g,h)[λ,μ,ν,τ] · F(a,b,h,e,f,i)[κ,τ,ρ,σ]
/// p2[λμν κρσ] = Σ_{j∈b⊗c, α,β,τ}
///                 F(a,b,c,g,f,j)[κ,λ,α,β] · F(a,j,d,e,g,i)[β,μ,τ,σ]
///                 · F(b,c,d,i,j,h)[α,τ,ν,ρ]
/// ```
///
/// returns `max |p1 - p2|`.
pub(crate) fn pentagon_residual<F: Family>(
    fam: &mut F,
    a: &F::Irrep,
    b: &F::Irrep,
    c: &F::Irrep,
    d: &F::Irrep,
) -> Result<f64, F::Error> {
    let mut worst = 0.0f64;
    let mut memo = BlockMemo::default();

    for f in fam.products(a, b)? {
        for h in fam.products(c, d)? {
            for g in fam.products(&f, c)? {
                for i in fam.products(b, &h)? {
                    for e in intersect_products(fam, &g, d, a, &i)? {
                        // Free-index dims: λ=N_fcg, μ=N_gde, ν=N_cdh, κ=N_abf,
                        // ρ=N_bhi, σ=N_aie.
                        let n_lambda = fam.mult(&f, c, &g)?;
                        let n_mu = fam.mult(&g, d, &e)?;
                        let n_nu = fam.mult(c, d, &h)?;
                        let n_kappa = fam.mult(a, b, &f)?;
                        let n_rho = fam.mult(b, &h, &i)?;
                        let n_sigma = fam.mult(a, &i, &e)?;
                        if [n_lambda, n_mu, n_nu, n_kappa, n_rho, n_sigma].contains(&0) {
                            continue; // empty output family -> vacuous
                        }

                        // p1: F1[λ,μ,ν,τ] · F2[κ,τ,ρ,σ], sum over τ (= N_fhe).
                        let f1 = memo.f_block(fam, &f, c, d, &e, &g, &h)?; // [λ,μ,ν,τ]
                        let f2 = memo.f_block(fam, a, b, &h, &e, &f, &i)?; // [κ,τ,ρ,σ]
                        let n_tau = f1.dims()[3];

                        // p2 factors, summed over j ∈ b⊗c and α,β,τ'.
                        let mut p2_terms: Vec<(FBlock, FBlock, FBlock)> = Vec::new();
                        for j in fam.products(b, c)? {
                            let g1 = memo.f_block(fam, a, b, c, &g, &f, &j)?; // [κ,λ,α,β]
                            let g2 = memo.f_block(fam, a, &j, d, &e, &g, &i)?; // [β,μ,τ',σ]
                            let g3 = memo.f_block(fam, b, c, d, &i, &j, &h)?; // [α,τ',ν,ρ]
                            p2_terms.push((g1, g2, g3));
                        }

                        for lambda in 0..n_lambda {
                            for mu in 0..n_mu {
                                for nu in 0..n_nu {
                                    for kappa in 0..n_kappa {
                                        for rho in 0..n_rho {
                                            for sigma in 0..n_sigma {
                                                let mut p1 = 0.0;
                                                for tau in 0..n_tau {
                                                    p1 += f1.at(lambda, mu, nu, tau)
                                                        * f2.at(kappa, tau, rho, sigma);
                                                }
                                                let mut p2 = 0.0;
                                                for (g1, g2, g3) in &p2_terms {
                                                    // dims: α=g1[2], β=g1[3], τ'=g2[2]
                                                    let n_alpha = g1.dims()[2];
                                                    let n_beta = g1.dims()[3];
                                                    let n_taup = g2.dims()[2];
                                                    for alpha in 0..n_alpha {
                                                        for beta in 0..n_beta {
                                                            for taup in 0..n_taup {
                                                                p2 += g1
                                                                    .at(kappa, lambda, alpha, beta)
                                                                    * g2.at(beta, mu, taup, sigma)
                                                                    * g3.at(alpha, taup, nu, rho);
                                                            }
                                                        }
                                                    }
                                                }
                                                worst = worst.max((p1 - p2).abs());
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    Ok(worst)
}

// ---------------------------------------------------------------------------
// Gate 3: hexagon (TensorKitSectors sectors.jl:hexagon_equation, :834-871).
// ---------------------------------------------------------------------------

/// The worst residual of both hexagon identities for the triple `(a, b, c)`
/// (GenericFusion branch of `TensorKitSectors/sectors.jl:hexagon_equation`).
/// For every `e ∈ c⊗a`, `f ∈ c⊗b`, `d ∈ (e⊗b) ∩ (a⊗f)`, with
/// `F ≡ F(a,c,b,d,e,f)[λ,β,γ,ν]`:
///
/// ```text
/// RFR1[α,β,μ,ν] = Σ_{λ,γ} R(c,a,e)[α,λ] · F[λ,β,γ,ν] · R(c,b,f)[γ,μ]
/// RFR2[α,β,μ,ν] = Σ_{λ,γ} R(a,c,e)[α,λ] · F[λ,β,γ,ν] · R(b,c,f)[γ,μ]   (conj; real)
/// FRF1[α,β,μ,ν] = Σ_{g∈a⊗b, δ,σ,ψ}
///                   F(c,a,b,d,e,g)[α,β,δ,σ] · R(c,g,d)[σ,ψ] · F(a,b,c,d,g,f)[δ,ψ,μ,ν]
/// FRF2[α,β,μ,ν] = Σ ... R(g,c,d)[σ,ψ] ...   (conj; real)
/// ```
///
/// returns `max(|RFR1 - FRF1|, |RFR2 - FRF2|)`. R is real, so the two hexagons
/// differ only in which R replaces which (`conj` is the identity).
pub(crate) fn hexagon_residual<F: Family>(
    fam: &mut F,
    a: &F::Irrep,
    b: &F::Irrep,
    c: &F::Irrep,
) -> Result<f64, F::Error> {
    let mut worst = 0.0f64;
    let mut memo = BlockMemo::default();

    for e in fam.products(c, a)? {
        let rcae = memo.r_block(fam, c, a, &e)?; // [α,λ]
        let race = memo.r_block(fam, a, c, &e)?; // [α,λ]
        for f in fam.products(c, b)? {
            let rcbf = memo.r_block(fam, c, b, &f)?; // [γ,μ]
            let rbcf = memo.r_block(fam, b, c, &f)?; // [γ,μ]
            for d in intersect_products(fam, &e, b, a, &f)? {
                // free dims: α=N_cae, β=N_ebd, μ=N_bcf, ν=N_afd.
                let n_alpha = fam.mult(c, a, &e)?;
                let n_beta = fam.mult(&e, b, &d)?;
                let n_mu = fam.mult(b, c, &f)?;
                let n_nu = fam.mult(a, &f, &d)?;
                if [n_alpha, n_beta, n_mu, n_nu].contains(&0) {
                    continue;
                }
                let facb = memo.f_block(fam, a, c, b, &d, &e, &f)?; // [λ,β,γ,ν]
                let n_lam = facb.dims()[0]; // N_ace = N_cae
                let n_gam = facb.dims()[2]; // N_cbf

                // FRF factors over g ∈ a⊗b.
                let mut frf_terms: Vec<(FBlock, RBlock, RBlock, FBlock)> = Vec::new();
                for g in fam.products(a, b)? {
                    let rcgd = memo.r_block(fam, c, &g, &d)?;
                    let rgcd = memo.r_block(fam, &g, c, &d)?;
                    let fcab = memo.f_block(fam, c, a, b, &d, &e, &g)?; // [α,β,δ,σ]
                    let fabc = memo.f_block(fam, a, b, c, &d, &g, &f)?; // [δ,ψ,μ,ν]
                    frf_terms.push((fcab, rcgd, rgcd, fabc));
                }

                for alpha in 0..n_alpha {
                    for beta in 0..n_beta {
                        for mu in 0..n_mu {
                            for nu in 0..n_nu {
                                // RFR1 / RFR2.
                                let mut rfr1 = 0.0;
                                let mut rfr2 = 0.0;
                                for lam in 0..n_lam {
                                    for gam in 0..n_gam {
                                        let fv = facb.at(lam, beta, gam, nu);
                                        rfr1 += rcae.at(alpha, lam) * fv * rcbf.at(gam, mu);
                                        rfr2 += race.at(alpha, lam) * fv * rbcf.at(gam, mu);
                                    }
                                }
                                // FRF1 / FRF2.
                                let mut frf1 = 0.0;
                                let mut frf2 = 0.0;
                                for (fcab, rcgd, rgcd, fabc) in &frf_terms {
                                    let n_delta = fcab.dims()[2]; // N_abg
                                    let n_sigma = fcab.dims()[3]; // N_cgd
                                    let n_psi = rcgd.dim(); // N_gcd (= N_cgd)
                                    for delta in 0..n_delta {
                                        for sigma in 0..n_sigma {
                                            let fc = fcab.at(alpha, beta, delta, sigma);
                                            for psi in 0..n_psi {
                                                let fa = fabc.at(delta, psi, mu, nu);
                                                frf1 += fc * rcgd.at(sigma, psi) * fa;
                                                frf2 += fc * rgcd.at(sigma, psi) * fa;
                                            }
                                        }
                                    }
                                }
                                worst = worst.max((rfr1 - frf1).abs());
                                worst = worst.max((rfr2 - frf2).abs());
                            }
                        }
                    }
                }
            }
        }
    }
    Ok(worst)
}