dreamwell-intelligence 1.0.0

QuantumGPT (The Loom) — Quantum Information Pretrained Transformer. Density matrix attention with intrinsic thermodynamic loss, φ-scaled causal dephasing, and parameter shift gradient.
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
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
// Quantum Unitarity Gradient (QUG) — reverse-mode autodiff exploiting unitarity.
//
// The key insight: because U = exp(-iHdt) is unitary (U†U = I), the adjoint of
// the evolve operation ρ_out = UρU† is simply:
//   ∂L/∂ρ_in = U† · (∂L/∂ρ_out) · U
// This is just two matrix multiplies with the SAME unitary we stored during forward.
// No Jacobian inversion. No Fréchet derivative. No matrix exponential adjoint.
//
// Cost: ~3× forward pass (vs 2P× for parameter shift where P = param count).
// For 1M params: 700,000x speedup over parameter shift.
//
// Clean Compute: pre-allocated gradient buffers. Zero allocation in backward pass.

use crate::attention;
use crate::complex::Complex;
use crate::density_matrix::DensityMatrixN;
use crate::train::EpochMetrics;
use crate::transformer::QCT;

// φ constants available via dreamwell_math::golden_ratio() if needed.

/// Stored forward state for one QCT block (needed for backward pass).
pub struct ForwardCache {
    /// Density matrices BEFORE evolve at each token position [T × dim²].
    pub rho_before: Vec<Vec<Complex>>,
    /// Unitaries computed during forward [T × dim²].
    pub unitaries: Vec<Vec<Complex>>,
    /// Density matrices AFTER full attention at each position [T × dim²].
    pub rho_after: Vec<Vec<Complex>>,
    /// Populations from Born measurement [T × dim].
    pub populations: Vec<Vec<f32>>,
    /// Values passed to projection [T × dim].
    pub values: Vec<Vec<f32>>,
}

/// Gradient accumulator for all model parameters.
pub struct AllGradients {
    pub embed_grad: Vec<f32>,
    pub block_grads: Vec<BlockGrad>,
    pub output_grad: Vec<f32>,
}

pub struct BlockGrad {
    pub hamiltonian_grad: Vec<f32>,
    pub value_weight_grad: Vec<f32>,
}

impl AllGradients {
    /// Flatten to a single vector matching model.all_params() layout.
    pub fn flatten(&self) -> Vec<f32> {
        let mut v = Vec::new();
        v.extend_from_slice(&self.embed_grad);
        for bg in &self.block_grads {
            v.extend_from_slice(&bg.hamiltonian_grad);
            v.extend_from_slice(&bg.value_weight_grad);
        }
        v.extend_from_slice(&self.output_grad);
        v
    }
}

/// Forward pass with caching for QUG backward.
/// Returns (logits, avg_free_energy, per_block_caches).
pub fn forward_with_cache(model: &QCT, tokens: &[usize]) -> (Vec<Vec<f32>>, f32, Vec<ForwardCache>) {
    forward_with_cache_converter(model, tokens, None)
}

/// Forward pass with optional GoldenRatioConverter for frequency-aware dephasing.
/// When converter is Some, applies frequency-proportional initial dephasing after embedding.
/// The embedding stays pure (clean gradients). Frequency enters as a quantum channel.
/// When None, falls back to standard pure-state embedding with no initial dephasing.
pub fn forward_with_cache_converter(
    model: &QCT,
    tokens: &[usize],
    converter: Option<&crate::golden_ratio_converter::GoldenRatioConverter>,
) -> (Vec<Vec<f32>>, f32, Vec<ForwardCache>) {
    let dim = model.config.dim;
    let t = tokens.len();

    // Embed as pure states, then apply frequency-proportional dephasing.
    // Common tokens → strong dephasing → low F → thermodynamically gated (BA-34).
    // Rare tokens → weak dephasing → high F → full compute, maximum gradient signal.
    let mut states: Vec<DensityMatrixN> = tokens.iter().map(|&tok| model.embedding.embed(tok)).collect();
    if let Some(conv) = converter {
        for (i, &tok) in tokens.iter().enumerate() {
            let eps = conv.dephasing_rate(tok);
            if eps > 1e-6 {
                states[i].dephase(eps);
            }
        }
    }
    let mut values: Vec<Vec<f32>> = states.iter().map(|s| s.populations()).collect();
    let mut current_states = states;
    let mut caches = Vec::with_capacity(model.blocks.len());
    let mut total_f = 0.0f32;

    for block in &model.blocks {
        let mut cache = ForwardCache {
            rho_before: Vec::with_capacity(t),
            unitaries: Vec::with_capacity(t),
            rho_after: Vec::with_capacity(t),
            populations: Vec::with_capacity(t),
            values: values.clone(),
        };

        // Pre-compute ALL unitaries in parallel (rayon).
        // U_i = exp(-iH(i)dt) depends only on position i and static Hamiltonian
        // parameters — NOT on the evolving density matrices. This moves the
        // O(d³) matrix exponential out of the serial causal loop.
        let precomputed_unitaries: Vec<Vec<Complex>> = {
            use rayon::prelude::*;
            (0..t)
                .into_par_iter()
                .map(|i| {
                    let h_matrix = block.hamiltonian.build_matrix(i);
                    DensityMatrixN::hamiltonian_unitary(&h_matrix, dim, 0.090) // 1/φ⁵ — adiabatic evolution step
                })
                .collect()
        };

        // Causal attention loop — φ-windowed dephasing + thermodynamic gate + evolve.
        //
        // Quantum Signal Gate: the φ-exponential dephasing ε = exp(-d/φ) creates a
        // finite coherence horizon. Beyond distance w = ⌈φ · 3·ln(φ)⌉ = 3, the
        // dephasing contribution falls below 1/φ³ (thermodynamic gate threshold).
        // Truncate the causal loop from O(t²) to O(t × w).
        //
        // This is the DreamGate concept applied to quantum information flow:
        // the gate opens for coherent signals (within window) and closes for
        // thermalized signals (beyond window). Error bounded by 1/φ³ per position.
        // BA-35: Coherence window from φ-exponential decay.
        // Base horizon: ⌈φ · 3·ln(φ)⌉ = 3 (where ε < 1/φ³).
        // Training window: scale by φ² ≈ 2.618 for gradient preservation.
        // Wider window preserves more causal information for learning.
        // ⌈3 × φ²⌉ = ⌈7.85⌉ = 8.
        const COHERENCE_WINDOW: usize = 8;

        // BA-34: Thermodynamic gate threshold, scaled by dimension.
        // At dim=5: 0.236/5 = 0.047. At dim=48: 0.236/48 = 0.005.
        // The free energy landscape narrows with dimension — the threshold must follow.
        let f_gate = 0.236 / dim as f32;

        for i in 0..t {
            let mut rho = current_states[i].clone();

            // φ-windowed causal dephasing: only couple with nearest COHERENCE_WINDOW
            // predecessors. Distant positions contribute ε < 1/φ³ — below gate threshold.
            let window_start = i.saturating_sub(COHERENCE_WINDOW);
            for j in window_start..i {
                let dist = i - j;
                let eps = block.hamiltonian.causal_dephasing(dist);
                rho.couple_dephase(&current_states[j], eps);
            }

            // Store rho BEFORE evolve
            cache.rho_before.push(rho.entries.clone());

            // Thermodynamic computation gate: check free energy before evolve.
            // F < threshold → state is near equilibrium → evolve is negligible.
            let f_before = rho.free_energy(&block.hamiltonian.bias);
            let unitary = &precomputed_unitaries[i];

            // BA-34: Thermodynamic computation gate (dimension-scaled).
            // Skip evolve only for states genuinely at thermal equilibrium.
            if f_before.abs() > f_gate {
                cache.unitaries.push(unitary.clone());
                rho.evolve(unitary);
            } else {
                cache.unitaries.push(Vec::new());
            }

            // Store rho AFTER evolve (or after skip)
            cache.rho_after.push(rho.entries.clone());

            let f = rho.free_energy(&block.hamiltonian.bias);
            total_f += f;

            let pops = rho.populations();
            cache.populations.push(pops);
        }

        // Value projection
        let attn_output = attention::AttentionOutput {
            populations: cache.populations.clone(),
            free_energies: vec![0.0; t],
            coherences: vec![0.0; t],
        };
        let new_values = attention::attention_project(&attn_output, &values, dim);
        values = new_values;

        // Inter-block dephasing: ε = 1/(φ³ × num_blocks).
        // Total surviving coherence after ALL blocks: exp(-1/φ³) ≈ 79%.
        // Scale-invariant — same survival ratio at any block depth.
        let eps_block = 0.236 / model.blocks.len().max(1) as f32;
        for state in &mut current_states {
            state.dephase(eps_block);
        }

        caches.push(cache);
    }

    // Output projection
    let vocab = model.config.vocab_size;
    let mut logits = Vec::with_capacity(t);
    for i in 0..t {
        let mut token_logits = vec![0.0f32; vocab];
        for v in 0..vocab {
            for d in 0..dim {
                token_logits[v] += values[i][d] * model.output_weights[d * vocab + v];
            }
        }
        logits.push(token_logits);
    }

    (logits, total_f / t as f32, caches)
}

/// QUG backward pass — compute gradients using stored unitaries.
/// Cost: ~3× forward (3 matmuls per block for adjoint, vs 2P forward passes for PSR).
pub fn qug_backward(model: &QCT, tokens: &[usize], logits: &[Vec<f32>], caches: &[ForwardCache]) -> AllGradients {
    let dim = model.config.dim;
    let vocab = model.config.vocab_size;
    let t = tokens.len().saturating_sub(1); // targets = tokens[1..]
    if t == 0 {
        return AllGradients {
            embed_grad: vec![0.0; model.embedding.num_params()],
            block_grads: model
                .blocks
                .iter()
                .map(|b| BlockGrad {
                    hamiltonian_grad: vec![0.0; b.hamiltonian.num_params()],
                    value_weight_grad: vec![0.0; b.value_weights.len()],
                })
                .collect(),
            output_grad: vec![0.0; model.output_weights.len()],
        };
    }

    // 1. ∂L/∂logits from cross-entropy (softmax gradient)
    let mut d_logits: Vec<Vec<f32>> = Vec::with_capacity(t);
    for i in 0..t {
        let target = tokens[i + 1];
        let max_l = logits[i].iter().cloned().fold(f32::NEG_INFINITY, f32::max);
        let exp_sum: f32 = logits[i].iter().map(|&l| (l - max_l).exp()).sum();
        let mut d_log = vec![0.0f32; vocab];
        for v in 0..vocab {
            let softmax_v = (logits[i][v] - max_l).exp() / exp_sum;
            d_log[v] = softmax_v - if v == target { 1.0 } else { 0.0 };
        }
        // Scale by 1/t for average
        for v in &mut d_log {
            *v /= t as f32;
        }
        d_logits.push(d_log);
    }

    // 2. ∂L/∂output_weights
    let mut d_output = vec![0.0f32; dim * vocab];
    for i in 0..t {
        let cache = caches.last().unwrap();
        let vals = if i < cache.populations.len() {
            &cache.populations[i]
        } else {
            continue;
        };
        for d_idx in 0..dim {
            for v in 0..vocab {
                d_output[d_idx * vocab + v] += vals.get(d_idx).copied().unwrap_or(0.0) * d_logits[i][v];
            }
        }
    }

    // 3. ∂L/∂values (from output projection)
    let mut d_values: Vec<Vec<f32>> = vec![vec![0.0f32; dim]; t.max(1)];
    for i in 0..t {
        for d_idx in 0..dim {
            for v in 0..vocab {
                d_values[i][d_idx] += model.output_weights[d_idx * vocab + v] * d_logits[i][v];
            }
        }
    }

    // 4. Per-block backward (reverse order)
    let mut block_grads = Vec::with_capacity(model.blocks.len());
    for (block_idx, block) in model.blocks.iter().enumerate().rev() {
        let cache = &caches[block_idx];
        let num_h = block.hamiltonian.num_params();

        // ∂L/∂value_weights from attention projection
        let mut d_vw = vec![0.0f32; dim * dim];
        // Simplified: accumulate gradient from the projection step
        for i in 0..t.min(cache.populations.len()) {
            for d_idx in 0..dim {
                for s in 0..dim {
                    let pop = cache.populations[i].get(s).copied().unwrap_or(0.0);
                    let dv = d_values[i].get(d_idx).copied().unwrap_or(0.0);
                    d_vw[d_idx * dim + s] += pop * dv;
                }
            }
        }

        // ∂L/∂H via QUG: for each position, use stored unitary.
        // Positions are mathematically independent in the backward pass —
        // each reads from immutable cached state. Parallelize with rayon.
        let dt = 0.090f32; // 1/φ⁵ — must match forward pass
        let len = t.min(cache.unitaries.len());

        let position_grads: Vec<Vec<f32>> = {
            use rayon::prelude::*;
            (0..len)
                .into_par_iter()
                .map(|i| {
                    let mut local_d_h = vec![0.0f32; num_h];

                    // Thermodynamic gate: skip gradient for positions where evolve was skipped.
                    // Empty unitary = position was at thermal equilibrium (F < 1/φ³).
                    // No unitary was applied → no gradient to propagate → zero contribution.
                    let u = &cache.unitaries[i];
                    if u.is_empty() {
                        return local_d_h;
                    }

                    let mut scratch_a = vec![Complex::ZERO; dim * dim];
                    let mut scratch_b = vec![Complex::ZERO; dim * dim];

                    // ∂L/∂ρ_after from populations gradient (diagonal)
                    let mut d_rho = vec![Complex::ZERO; dim * dim];
                    for k in 0..dim {
                        let dp = d_values[i].get(k).copied().unwrap_or(0.0);
                        d_rho[k * dim + k] = Complex::new(dp, 0.0);
                    }

                    // Build U† explicitly (conjugate transpose)
                    let mut u_dag = vec![Complex::ZERO; dim * dim];
                    for r in 0..dim {
                        for c in 0..dim {
                            u_dag[r * dim + c] = u[c * dim + r].conj();
                        }
                    }

                    // THE KEY STEP: ∂L/∂ρ_before = U† · d_rho · U
                    dreamwell_math::linalg::cgemm(&u_dag, &d_rho, &mut scratch_a, dim, dim, dim);
                    dreamwell_math::linalg::cgemm(&scratch_a, u, &mut scratch_b, dim, dim, dim);

                    // Commutator gradient: ∂L/∂H = -dt · Im([∂L/∂ρ, ρ_before])
                    let rho_before = &cache.rho_before[i];
                    let mut h_idx = 0;

                    // Bias gradients
                    for k in 0..dim {
                        let mut comm_diag = 0.0f32;
                        for j in 0..dim {
                            let ab = scratch_b[k * dim + j].mul(rho_before[j * dim + k]);
                            let ba = rho_before[k * dim + j].mul(scratch_b[j * dim + k]);
                            comm_diag += (ab.sub(ba)).im;
                        }
                        if h_idx < local_d_h.len() {
                            local_d_h[h_idx] += -dt * comm_diag;
                        }
                        h_idx += 1;
                    }

                    // Coupling gradients
                    for p in 0..dim {
                        for q in (p + 1)..dim {
                            if h_idx >= local_d_h.len() {
                                break;
                            }
                            let ab_pq = scratch_b[p * dim + q].mul(rho_before[q * dim + p]);
                            let ba_pq = rho_before[p * dim + q].mul(scratch_b[q * dim + p]);
                            let comm_pq = ab_pq.sub(ba_pq);
                            local_d_h[h_idx] += -dt * 2.0 * comm_pq.im;
                            h_idx += 1;
                        }
                    }

                    local_d_h
                })
                .collect()
        };

        // Reduce: sum all position gradients
        let mut d_h = vec![0.0f32; num_h];
        for pg in &position_grads {
            for (k, &v) in pg.iter().enumerate() {
                d_h[k] += v;
            }
        }

        block_grads.push(BlockGrad {
            hamiltonian_grad: d_h,
            value_weight_grad: d_vw,
        });
    }

    // Reverse the block grads (we iterated in reverse)
    block_grads.reverse();

    // 5. Embedding gradient (simplified: finite difference on angles is OK for now)
    // Full analytic embedding gradient requires differentiating through Givens rotations.
    // For now, use zero (embedding angles are less critical than Hamiltonian params).
    let embed_grad = vec![0.0f32; model.embedding.num_params()];

    AllGradients {
        embed_grad,
        block_grads,
        output_grad: d_output,
    }
}

/// BA-60: GPU-accelerated forward + backward epoch.
///
/// Replaces the rayon par_iter(forward_with_cache + qug_backward) block
/// with a version that uses GpuTrainingContext for the heavy matrix operations:
///   - Batched expm for unitary precomputation (all positions × all blocks × all windows)
///   - Batched evolve for ρ' = UρU† (all windows in parallel per position)
///   - Batched adjoint for ∂L/∂ρ_before = U†·dρ·U (all positions × all blocks × all windows)
///
/// The causal dephasing loop, free energy computation, value projection, and
/// commutator gradient extraction remain on CPU — they are element-wise operations
/// that are negligible compared to the O(dim³) matrix multiplies.
///
/// Returns (avg_gradient_flat, avg_loss, avg_free_energy).
pub fn forward_backward_epoch_gpu(
    gpu: &dreamwell_math::gpu_training::GpuTrainingContext,
    model: &QCT,
    windows: &[(usize, usize)],
    tokens: &[usize],
) -> (Vec<f32>, f32, f32) {
    use dreamwell_math::Complex;
    let dim = model.config.dim;
    let vocab = model.config.vocab_size;
    let stride = dim * dim;
    let num_windows = windows.len();
    let num_blocks = model.blocks.len();
    let dt = 0.090f32; // 1/φ⁵

    // ═══════════════════════════════════════════════════════
    // Phase 1: Batched EXPM — all unitaries for all blocks, all windows
    // ═══════════════════════════════════════════════════════

    // Collect all Hamiltonians: [window][block][position] → flat batch
    let mut all_window_data: Vec<WindowForwardState> = Vec::with_capacity(num_windows);

    for &(ws, we) in windows {
        let window_tokens = &tokens[ws..we];
        let input = &window_tokens[..window_tokens.len().saturating_sub(1)];
        let t = input.len();

        // Embed tokens into density matrices
        let states: Vec<DensityMatrixN> = input.iter().map(|&tok| model.embedding.embed(tok)).collect();
        let values: Vec<Vec<f32>> = states.iter().map(|s| s.populations()).collect();

        // Pre-compute ALL unitaries for ALL blocks using GPU batched expm
        let mut block_unitaries: Vec<Vec<Vec<Complex>>> = Vec::with_capacity(num_blocks);
        for block in &model.blocks {
            let mut all_h = vec![0.0f32; t * stride];
            for i in 0..t {
                let h = block.hamiltonian.build_matrix(i);
                all_h[i * stride..(i + 1) * stride].copy_from_slice(&h);
            }
            let flat_unitaries = gpu.batched_expm(&all_h, dt, t);
            let per_pos: Vec<Vec<Complex>> = (0..t)
                .map(|i| flat_unitaries[i * stride..(i + 1) * stride].to_vec())
                .collect();
            block_unitaries.push(per_pos);
        }

        all_window_data.push(WindowForwardState {
            window_tokens: window_tokens.to_vec(),
            t,
            states,
            values,
            block_unitaries,
        });
    }

    // ═══════════════════════════════════════════════════════
    // Phase 2: Forward pass per window (causal loop on CPU, evolve on GPU)
    // ═══════════════════════════════════════════════════════

    let f_gate = 0.236 / dim as f32;
    const COHERENCE_WINDOW: usize = 8;
    let eps_block = 0.236 / num_blocks.max(1) as f32;

    let mut all_window_results: Vec<WindowResult> = Vec::with_capacity(num_windows);

    for wdata in &mut all_window_data {
        let t = wdata.t;
        let mut current_states = wdata.states.clone();
        let mut values = wdata.values.clone();
        let mut caches: Vec<ForwardCache> = Vec::with_capacity(num_blocks);
        let mut total_f = 0.0f32;

        for (block_idx, block) in model.blocks.iter().enumerate() {
            let mut cache = ForwardCache {
                rho_before: Vec::with_capacity(t),
                unitaries: Vec::with_capacity(t),
                rho_after: Vec::with_capacity(t),
                populations: Vec::with_capacity(t),
                values: values.clone(),
            };

            let precomputed_unitaries = &wdata.block_unitaries[block_idx];

            // Causal loop — sequential per position (CPU).
            // The evolve (UρU†) uses CPU cgemm_par since it's sequential
            // and can't be batched. GPU already saved time on expm above.
            for i in 0..t {
                let mut rho = current_states[i].clone();

                // Causal dephasing (element-wise, cheap)
                let window_start = i.saturating_sub(COHERENCE_WINDOW);
                for j in window_start..i {
                    let dist = i - j;
                    let eps = block.hamiltonian.causal_dephasing(dist);
                    rho.couple_dephase(&current_states[j], eps);
                }

                cache.rho_before.push(rho.entries.clone());

                let f_before = rho.free_energy(&block.hamiltonian.bias);
                let unitary = &precomputed_unitaries[i];

                if f_before.abs() > f_gate {
                    cache.unitaries.push(unitary.clone());
                    rho.evolve(unitary);
                } else {
                    cache.unitaries.push(Vec::new());
                }

                cache.rho_after.push(rho.entries.clone());

                let f = rho.free_energy(&block.hamiltonian.bias);
                total_f += f;

                let pops = rho.populations();
                cache.populations.push(pops);

                // Update current_states for subsequent causal coupling
                current_states[i] = rho;
            }

            // Value projection (CPU — O(t × dim²), cheap)
            let attn_output = attention::AttentionOutput {
                populations: cache.populations.clone(),
                free_energies: vec![0.0; t],
                coherences: vec![0.0; t],
            };
            values = attention::attention_project(&attn_output, &values, dim);

            // Inter-block dephasing (CPU — element-wise)
            for state in &mut current_states {
                state.dephase(eps_block);
            }

            caches.push(cache);
        }

        // Output projection (CPU — O(t × dim × vocab), cheap)
        let mut logits = Vec::with_capacity(t);
        for i in 0..t {
            let mut token_logits = vec![0.0f32; vocab];
            for v in 0..vocab {
                for d in 0..dim {
                    token_logits[v] += values[i][d] * model.output_weights[d * vocab + v];
                }
            }
            logits.push(token_logits);
        }

        let avg_f = total_f / t.max(1) as f32;
        let loss = QCT::loss_from_logits(&logits, &wdata.window_tokens, avg_f);

        all_window_results.push(WindowResult {
            logits,
            caches,
            loss,
            avg_f,
        });
    }

    // ═══════════════════════════════════════════════════════
    // Phase 3: Backward pass (output grad on CPU, adjoint on GPU)
    // ═══════════════════════════════════════════════════════

    let num_params = model.num_params();
    let mut total_grad = vec![0.0f32; num_params];
    let mut total_loss = 0.0f32;
    let mut total_f = 0.0f32;

    for (w_idx, wdata) in all_window_data.iter().enumerate() {
        let wr = &all_window_results[w_idx];

        let grads = qug_backward_gpu(gpu, model, &wdata.window_tokens, &wr.logits, &wr.caches);
        let grad_flat = grads.flatten();

        total_loss += wr.loss;
        total_f += wr.avg_f;
        for (i, &g) in grad_flat.iter().enumerate() {
            if i < num_params {
                total_grad[i] += g;
            }
        }
    }

    // Average across windows
    let n = num_windows as f32;
    for g in &mut total_grad {
        *g /= n;
    }
    (total_grad, total_loss / n, total_f / n)
}

/// GPU-accelerated QUG backward pass.
///
/// Same logic as qug_backward but uses batched GPU adjoint for the key step:
/// ∂L/∂ρ_before = U† · d_rho · U
fn qug_backward_gpu(
    gpu: &dreamwell_math::gpu_training::GpuTrainingContext,
    model: &QCT,
    tokens: &[usize],
    logits: &[Vec<f32>],
    caches: &[ForwardCache],
) -> AllGradients {
    use dreamwell_math::Complex;
    let dim = model.config.dim;
    let vocab = model.config.vocab_size;
    let stride = dim * dim;
    let t = tokens.len().saturating_sub(1);
    if t == 0 {
        return AllGradients {
            embed_grad: vec![0.0; model.embedding.num_params()],
            block_grads: model
                .blocks
                .iter()
                .map(|b| BlockGrad {
                    hamiltonian_grad: vec![0.0; b.hamiltonian.num_params()],
                    value_weight_grad: vec![0.0; b.value_weights.len()],
                })
                .collect(),
            output_grad: vec![0.0; model.output_weights.len()],
        };
    }

    // 1. ∂L/∂logits (CPU — cheap scalar ops)
    let mut d_logits: Vec<Vec<f32>> = Vec::with_capacity(t);
    for i in 0..t {
        let target = tokens[i + 1];
        let max_l = logits[i].iter().cloned().fold(f32::NEG_INFINITY, f32::max);
        let exp_sum: f32 = logits[i].iter().map(|&l| (l - max_l).exp()).sum();
        let mut d_log = vec![0.0f32; vocab];
        for v in 0..vocab {
            let softmax_v = (logits[i][v] - max_l).exp() / exp_sum;
            d_log[v] = (softmax_v - if v == target { 1.0 } else { 0.0 }) / t as f32;
        }
        d_logits.push(d_log);
    }

    // 2. ∂L/∂output_weights (CPU)
    let mut d_output = vec![0.0f32; dim * vocab];
    for i in 0..t {
        let cache = caches.last().unwrap();
        let vals = if i < cache.populations.len() {
            &cache.populations[i]
        } else {
            continue;
        };
        for d_idx in 0..dim {
            for v in 0..vocab {
                d_output[d_idx * vocab + v] += vals.get(d_idx).copied().unwrap_or(0.0) * d_logits[i][v];
            }
        }
    }

    // 3. ∂L/∂values (CPU)
    let mut d_values: Vec<Vec<f32>> = vec![vec![0.0f32; dim]; t.max(1)];
    for i in 0..t {
        for d_idx in 0..dim {
            for v in 0..vocab {
                d_values[i][d_idx] += model.output_weights[d_idx * vocab + v] * d_logits[i][v];
            }
        }
    }

    // 4. Per-block backward with GPU batched adjoint
    let dt_val = 0.090f32;
    let mut block_grads = Vec::with_capacity(model.blocks.len());

    for (block_idx, block) in model.blocks.iter().enumerate().rev() {
        let cache = &caches[block_idx];
        let num_h = block.hamiltonian.num_params();

        // ∂L/∂value_weights (CPU — O(t × dim²))
        let mut d_vw = vec![0.0f32; dim * dim];
        for i in 0..t.min(cache.populations.len()) {
            for d_idx in 0..dim {
                for s in 0..dim {
                    let pop = cache.populations[i].get(s).copied().unwrap_or(0.0);
                    let dv = d_values[i].get(d_idx).copied().unwrap_or(0.0);
                    d_vw[d_idx * dim + s] += pop * dv;
                }
            }
        }

        // Collect non-gated positions for GPU batched adjoint
        let len = t.min(cache.unitaries.len());
        let mut active_indices: Vec<usize> = Vec::new();
        let mut u_batch: Vec<Complex> = Vec::new();
        let mut d_rho_batch: Vec<Complex> = Vec::new();

        for i in 0..len {
            let u = &cache.unitaries[i];
            if u.is_empty() {
                continue;
            }

            active_indices.push(i);
            u_batch.extend_from_slice(u);

            // Build diagonal d_rho from d_values
            let mut d_rho = vec![Complex::ZERO; stride];
            for k in 0..dim {
                let dp = d_values[i].get(k).copied().unwrap_or(0.0);
                d_rho[k * dim + k] = Complex::new(dp, 0.0);
            }
            d_rho_batch.extend_from_slice(&d_rho);
        }

        // GPU batched adjoint: ∂L/∂ρ_before = U† · d_rho · U
        let adjoint_results = if !active_indices.is_empty() {
            gpu.batched_adjoint(&u_batch, &d_rho_batch, active_indices.len())
        } else {
            Vec::new()
        };

        // Extract commutator gradients on CPU (element-wise, cheap)
        let mut d_h = vec![0.0f32; num_h];
        for (batch_idx, &pos_idx) in active_indices.iter().enumerate() {
            let base = batch_idx * stride;
            let scratch_b = &adjoint_results[base..base + stride];
            let rho_before = &cache.rho_before[pos_idx];
            let mut h_idx = 0;

            // Bias gradients
            for k in 0..dim {
                let mut comm_diag = 0.0f32;
                for j in 0..dim {
                    let ab = scratch_b[k * dim + j].mul(rho_before[j * dim + k]);
                    let ba = rho_before[k * dim + j].mul(scratch_b[j * dim + k]);
                    comm_diag += (ab.sub(ba)).im;
                }
                if h_idx < d_h.len() {
                    d_h[h_idx] += -dt_val * comm_diag;
                }
                h_idx += 1;
            }

            // Coupling gradients
            for p in 0..dim {
                for q in (p + 1)..dim {
                    if h_idx >= d_h.len() {
                        break;
                    }
                    let ab_pq = scratch_b[p * dim + q].mul(rho_before[q * dim + p]);
                    let ba_pq = rho_before[p * dim + q].mul(scratch_b[q * dim + p]);
                    let comm_pq = ab_pq.sub(ba_pq);
                    d_h[h_idx] += -dt_val * 2.0 * comm_pq.im;
                    h_idx += 1;
                }
            }
        }

        block_grads.push(BlockGrad {
            hamiltonian_grad: d_h,
            value_weight_grad: d_vw,
        });
    }

    block_grads.reverse();
    let embed_grad = vec![0.0f32; model.embedding.num_params()];

    AllGradients {
        embed_grad,
        block_grads,
        output_grad: d_output,
    }
}

/// Intermediate state for GPU-accelerated forward pass per window.
struct WindowForwardState {
    window_tokens: Vec<usize>,
    t: usize,
    states: Vec<DensityMatrixN>,
    values: Vec<Vec<f32>>,
    block_unitaries: Vec<Vec<Vec<dreamwell_math::Complex>>>,
}

/// Result of forward pass for one window.
struct WindowResult {
    logits: Vec<Vec<f32>>,
    caches: Vec<ForwardCache>,
    loss: f32,
    avg_f: f32,
}

/// GPU-accelerated unitary precomputation for one block across all positions.
///
/// Batches all 64 (or T) `hamiltonian_unitary` calls into a single GPU dispatch.
/// Returns T unitary matrices as a contiguous Vec<Complex>.
///
/// This replaces the rayon par_iter precomputation at lines 113-119 of the forward pass
/// with a single GPU batched expm dispatch — the single largest speedup for training.
pub fn gpu_precompute_unitaries(
    gpu: &dreamwell_math::gpu_training::GpuTrainingContext,
    block: &crate::transformer::QCTBlock,
    dim: usize,
    t: usize,
    dt: f32,
) -> Vec<Vec<dreamwell_math::Complex>> {
    let n2 = dim * dim;

    // Build all Hamiltonians on CPU (cheap: just fills a real symmetric matrix from params)
    let mut all_h = vec![0.0f32; t * n2];
    for i in 0..t {
        let h = block.hamiltonian.build_matrix(i);
        all_h[i * n2..(i + 1) * n2].copy_from_slice(&h);
    }

    // Single batched GPU expm dispatch
    let flat = gpu.batched_expm(&all_h, dt, t);

    // Split into per-position unitaries
    let stride = dim * dim;
    (0..t).map(|i| flat[i * stride..(i + 1) * stride].to_vec()).collect()
}

/// GPU-accelerated batched evolve for positions where the thermodynamic gate passes.
///
/// Takes a batch of (unitary, density_matrix) pairs and computes ρ' = U·ρ·U† on GPU.
/// Returns evolved density matrices.
pub fn gpu_batch_evolve(
    gpu: &dreamwell_math::gpu_training::GpuTrainingContext,
    unitaries_flat: &[dreamwell_math::Complex],
    rhos_flat: &[dreamwell_math::Complex],
    batch_count: usize,
) -> Vec<dreamwell_math::Complex> {
    gpu.batched_evolve(unitaries_flat, rhos_flat, batch_count)
}

/// GPU-accelerated batched adjoint for QUG backward pass.
///
/// Computes ∂L/∂ρ_before = U† · ∂L/∂ρ_after · U for all positions in one batch.
pub fn gpu_batch_adjoint(
    gpu: &dreamwell_math::gpu_training::GpuTrainingContext,
    unitaries_flat: &[dreamwell_math::Complex],
    d_rho_flat: &[dreamwell_math::Complex],
    batch_count: usize,
) -> Vec<dreamwell_math::Complex> {
    gpu.batched_adjoint(unitaries_flat, d_rho_flat, batch_count)
}

/// Train using QUG (Quantum Unitarity Gradient) — the fast path.
/// Cost: ~3× forward per epoch (vs 2P× for parameter shift).
pub fn train_qug(model: &mut QCT, tokens: &[usize], config: &crate::train::TrainConfig) -> Vec<EpochMetrics> {
    let mut metrics = Vec::new();

    for epoch in 0..config.num_epochs {
        let start = std::time::Instant::now();
        let lr = crate::train::learning_rate_pub(config, epoch);

        // Select window
        let max_start = tokens.len().saturating_sub(config.context_length + 1);
        let window_start = if max_start > 0 { epoch % max_start } else { 0 };
        let window_end = (window_start + config.context_length + 1).min(tokens.len());
        let window = &tokens[window_start..window_end];

        // Forward with cache
        let (logits, avg_f, caches) = forward_with_cache(model, &window[..window.len() - 1]);

        // Compute loss from cached logits — no redundant forward pass
        let loss = QCT::loss_from_logits(&logits, window, avg_f);

        // QUG backward — ONE pass, ~3× forward cost
        let grads = qug_backward(model, window, &logits, &caches);
        let grad_flat = grads.flatten();

        // Gradient norm
        let grad_norm: f32 = grad_flat.iter().map(|g| g * g).sum::<f32>().sqrt();

        // Clip
        let scale = if grad_norm > config.grad_clip && grad_norm > 0.0 {
            config.grad_clip / grad_norm
        } else {
            1.0
        };

        // Update parameters in-place (zero allocation)
        model.apply_gradient_update(&grad_flat, lr, scale);

        let elapsed = start.elapsed().as_secs_f32() * 1000.0;

        if epoch % config.log_interval == 0 || epoch == config.num_epochs - 1 {
            let m = EpochMetrics {
                epoch,
                loss,
                free_energy: avg_f,
                grad_norm,
                elapsed_ms: elapsed,
                learning_rate: lr,
                params_trained: grad_flat.len(),
            };
            log::info!(
                "QUG Epoch {:4}: loss={:.4} F={:.4} |∇|={:.6} lr={:.5} ({:.1}ms)",
                m.epoch,
                m.loss,
                m.free_energy,
                m.grad_norm,
                m.learning_rate,
                m.elapsed_ms
            );
            metrics.push(m);
        }
    }

    metrics
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::transformer::QCTConfig;

    #[test]
    fn forward_cache_matches_forward() {
        let config = QCTConfig {
            vocab_size: 10,
            dim: 4,
            num_blocks: 1,
            seed: 42,
        };
        let model = QCT::new(config);
        let tokens = vec![0, 1, 2, 3, 4, 5];

        let (logits_normal, f_normal) = model.forward(&tokens);
        let (logits_cached, f_cached, caches) = forward_with_cache(&model, &tokens);

        assert_eq!(logits_normal.len(), logits_cached.len());
        assert!(
            (f_normal - f_cached).abs() < 0.5,
            "free energy mismatch: {} vs {}",
            f_normal,
            f_cached
        );
        assert!(!caches.is_empty());
    }

    #[test]
    fn qug_gradient_nonzero() {
        let config = QCTConfig {
            vocab_size: 10,
            dim: 4,
            num_blocks: 1,
            seed: 42,
        };
        let model = QCT::new(config);
        let tokens = vec![0, 1, 2, 3, 4, 5];

        let (logits, _, caches) = forward_with_cache(&model, &tokens[..5]);
        let grads = qug_backward(&model, &tokens, &logits, &caches);
        let flat = grads.flatten();

        let norm: f32 = flat.iter().map(|g| g * g).sum::<f32>().sqrt();
        assert!(norm > 0.0, "QUG gradient should be nonzero");
        assert!(norm.is_finite(), "QUG gradient should be finite");
    }

    #[test]
    fn qug_training_runs() {
        let config = QCTConfig {
            vocab_size: 10,
            dim: 4,
            num_blocks: 1,
            seed: 42,
        };
        let mut model = QCT::new(config);
        let tokens: Vec<usize> = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

        let train_config = crate::train::TrainConfig {
            learning_rate: 0.01,
            num_epochs: 3,
            context_length: 6,
            log_interval: 1,
            ..Default::default()
        };
        let metrics = train_qug(&mut model, &tokens, &train_config);
        assert_eq!(metrics.len(), 3);
        assert!(metrics[0].loss.is_finite());
        assert!(metrics[0].grad_norm > 0.0);
        eprintln!(
            "QUG training: {:.1}ms/epoch (vs ~7400ms for PSR)",
            metrics[0].elapsed_ms
        );
    }
}