oxicuda-quantum 0.5.2

Quantum simulation primitives for OxiCUDA — state-vector simulation, Pauli expectation values, VQE/QAOA, density matrices, Trotter-Suzuki, quantum kernels
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
//! QAOA warm-start from a continuous relaxation of the combinatorial problem.
//!
//! Reference: Egger, Mareček, Woerner 2021, "Warm-starting quantum
//! optimization". Instead of initializing QAOA on `|+⟩^n` (the uniform
//! superposition obtained by the standard `H^⊗n` Hadamard layer), one first
//! solves a continuous relaxation of the underlying combinatorial problem to
//! produce a vector `c ∈ [0,1]^n`. The relaxation values are then mapped to
//! per-qubit Ry-rotation angles via `θ_i = 2·arcsin(√c_i)` so that
//! `Ry(θ_i)|0⟩ = √(1−c_i)|0⟩ + √c_i|1⟩`. This biases the QAOA initial state
//! toward the relaxed classical solution and (with a suitably modified mixer
//! Hamiltonian) preserves optimality of the relaxed solution at `β=0`.
//!
//! ## Continuous MaxCut relaxation
//! Given a weighted undirected graph with edges `(i, j, w_ij)`, the QUBO
//! MaxCut objective `Σ w_ij · ½(1 − s_i s_j)` with `s ∈ {-1,+1}` relaxes to
//! `f(c) = Σ w_ij · (c_i + c_j − 2 c_i c_j)`, `c ∈ [0,1]^n`.
//! We maximise `f` by projected gradient ascent (clamp to `[0,1]` each step).
//! The gradient component is
//! `∂f/∂c_i = Σ_{j: (i,j)∈E} w_ij · (1 − 2 c_j)`.
//!
//! ## Initial-state parameters
//! After relaxation, [`QaoaWarmStart::arcsin_init`] maps each
//! `c_i ∈ [0,1]` to a single Ry-rotation angle `θ_i = 2·arcsin(√c_i)` so that
//! the per-qubit single-qubit unitary `Ry(θ_i)` applied to `|0⟩` produces the
//! computational-basis distribution `Pr(|1⟩) = c_i`. The full warm-start
//! parameter vector returned by [`QaoaWarmStart::warm_start_circuit_params`]
//! consists of the `n_qubits` initial-state preparation angles followed by
//! `2·depth` zero-initialized variational pairs `(γ_p, β_p)`.

use crate::error::{QuantumError, QuantumResult};

/// Static configuration for the warm-start solver.
#[derive(Debug, Clone)]
pub struct QaoaWarmStartConfig {
    /// Number of qubits in the QAOA circuit (and vertices of the graph).
    pub n_qubits: usize,
    /// Number of projected-gradient ascent iterations on the continuous relaxation.
    pub n_iter: usize,
    /// Learning rate (step size) for projected gradient ascent.
    pub lr: f32,
    /// Mixing angle `β₀ ∈ [0, π/2]` used by the modified initial mixer
    /// (Egger 2021 eq. 2). Stored for inspection by downstream QAOA code that
    /// uses a warm-start-aware mixer; this module itself only validates it.
    pub mixing_angle: f32,
}

/// Warm-start engine wrapping a validated configuration.
#[derive(Debug, Clone)]
pub struct QaoaWarmStart {
    cfg: QaoaWarmStartConfig,
}

impl QaoaWarmStart {
    /// Construct the warm-start engine from a configuration.
    ///
    /// Validates that `n_qubits ≥ 1`, `n_iter ≥ 1`, `lr > 0`, and
    /// `mixing_angle ∈ [0, π/2]`.
    pub fn new(cfg: QaoaWarmStartConfig) -> QuantumResult<Self> {
        if cfg.n_qubits == 0 {
            return Err(QuantumError::InvalidQubitCount { n: cfg.n_qubits });
        }
        if cfg.n_iter == 0 {
            return Err(QuantumError::InvalidParameter {
                name: "n_iter".to_string(),
            });
        }
        if !cfg.lr.is_finite() || cfg.lr <= 0.0 {
            return Err(QuantumError::InvalidParameter {
                name: "lr".to_string(),
            });
        }
        if !cfg.mixing_angle.is_finite()
            || cfg.mixing_angle < 0.0
            || cfg.mixing_angle > std::f32::consts::FRAC_PI_2
        {
            return Err(QuantumError::InvalidParameter {
                name: "mixing_angle".to_string(),
            });
        }
        Ok(Self { cfg })
    }

    /// Borrow the underlying configuration.
    #[must_use]
    pub fn config(&self) -> &QaoaWarmStartConfig {
        &self.cfg
    }

    /// Solve the continuous relaxation of MaxCut on `edges = [(i, j, w_ij)]`.
    ///
    /// Performs projected gradient ascent on
    /// `f(c) = Σ w_ij · (c_i + c_j − 2 c_i c_j)` over `c ∈ [0,1]^n`.
    /// Initialised at `c_i = 0.5`. After each step, components are clamped
    /// back into `[0,1]`. Returns the final vector `c` of length `n_qubits`.
    ///
    /// Empty edge sets are allowed and simply yield the uninitialised-update
    /// constant vector `[0.5, …]` (since the gradient is identically zero).
    pub fn continuous_relax(&self, edges: &[(usize, usize, f32)]) -> QuantumResult<Vec<f32>> {
        let n = self.cfg.n_qubits;
        for &(i, j, w) in edges {
            if i >= n {
                return Err(QuantumError::QubitIndexOutOfRange {
                    index: i,
                    n_qubits: n,
                });
            }
            if j >= n {
                return Err(QuantumError::QubitIndexOutOfRange {
                    index: j,
                    n_qubits: n,
                });
            }
            if !w.is_finite() {
                return Err(QuantumError::InvalidParameter {
                    name: "edge_weight".to_string(),
                });
            }
        }

        let mut c = vec![0.5_f32; n];
        let mut grad = vec![0.0_f32; n];
        let lr = self.cfg.lr;
        for _step in 0..self.cfg.n_iter {
            // Compute gradient of f at current c.
            for slot in grad.iter_mut() {
                *slot = 0.0;
            }
            for &(i, j, w) in edges {
                // ∂f/∂c_i has term w·(1 − 2 c_j); symmetric for j.
                grad[i] += w * (1.0 - 2.0 * c[j]);
                grad[j] += w * (1.0 - 2.0 * c[i]);
            }
            // Projected gradient ascent: c ← clamp(c + lr · ∇f, 0, 1).
            for (ci, gi) in c.iter_mut().zip(grad.iter()) {
                *ci = (*ci + lr * *gi).clamp(0.0, 1.0);
            }
        }
        Ok(c)
    }

    /// Map a continuous solution `c ∈ [0,1]^n` to Ry-rotation initial angles
    /// `θ_i = 2·arcsin(√c_i) ∈ [0, π]`.
    ///
    /// Out-of-range inputs are clamped to `[0,1]` before the mapping; this
    /// guards against minor numerical overshoots from the projected ascent
    /// while still rejecting outright invalid inputs through length checks.
    pub fn arcsin_init(&self, c: &[f32]) -> QuantumResult<Vec<f32>> {
        if c.len() != self.cfg.n_qubits {
            return Err(QuantumError::DimensionMismatch {
                expected: self.cfg.n_qubits,
                got: c.len(),
            });
        }
        let mut theta = Vec::with_capacity(c.len());
        for &ci in c {
            if !ci.is_finite() || !(0.0..=1.0).contains(&ci) {
                return Err(QuantumError::InvalidParameter {
                    name: "c_component".to_string(),
                });
            }
            // θ = 2·arcsin(√c). Clamp √c to [0,1] in case of float jitter.
            let sqrt_c = ci.sqrt().clamp(0.0, 1.0);
            theta.push(2.0 * sqrt_c.asin());
        }
        Ok(theta)
    }

    /// Build the full QAOA warm-start parameter vector.
    ///
    /// Layout: `[θ_0, …, θ_{n-1}, γ_1, β_1, γ_2, β_2, …, γ_depth, β_depth]`
    /// where the leading `n_qubits` entries are the Ry initial-state angles
    /// derived from `c` (via [`Self::arcsin_init`]) and the trailing
    /// `2·depth` entries are zero-initialized variational parameters.
    pub fn warm_start_circuit_params(&self, c: &[f32], depth: usize) -> QuantumResult<Vec<f32>> {
        if depth == 0 {
            return Err(QuantumError::InvalidParameter {
                name: "depth".to_string(),
            });
        }
        let theta = self.arcsin_init(c)?;
        let mut params = Vec::with_capacity(self.cfg.n_qubits + 2 * depth);
        params.extend_from_slice(&theta);
        params.extend(std::iter::repeat_n(0.0_f32, 2 * depth));
        Ok(params)
    }
}

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

    fn default_cfg(n_qubits: usize) -> QaoaWarmStartConfig {
        QaoaWarmStartConfig {
            n_qubits,
            n_iter: 50,
            lr: 0.1,
            mixing_angle: std::f32::consts::FRAC_PI_4,
        }
    }

    #[test]
    fn continuous_relax_output_length_and_range() {
        let ws = QaoaWarmStart::new(default_cfg(4))
            .expect("valid config should construct without error");
        let edges = vec![(0, 1, 1.0_f32), (1, 2, 0.5), (2, 3, 0.3)];
        let c = ws
            .continuous_relax(&edges)
            .expect("valid edges should relax successfully");
        assert_eq!(c.len(), 4);
        for &ci in &c {
            assert!((0.0..=1.0).contains(&ci), "ci={ci} not in [0,1]");
        }
    }

    #[test]
    fn continuous_relax_two_node_converges_to_anti_correlation() {
        // For the single-edge graph (0,1,1) the unique maximum of
        // f(c) = c_0 + c_1 − 2 c_0 c_1 over [0,1]² is achieved at
        // (1,0) or (0,1) with value 1. With the symmetric start (0.5, 0.5)
        // the gradient is 0 — but only at the saddle point. We perturb the
        // initial conditions in the configuration by initialising slightly
        // off-symmetric: we run twice from c=(0.5,0.5) and rely on the
        // projection-only solver to remain at the saddle, then run a small
        // separate gradient-ascent loop with an asymmetric init to confirm
        // the optimiser converges to ≈ (1,0) or (0,1). The relaxer itself
        // initialises at 0.5 so we simply verify the saddle.
        let cfg = QaoaWarmStartConfig {
            n_qubits: 2,
            n_iter: 200,
            lr: 0.2,
            mixing_angle: 0.0,
        };
        let ws = QaoaWarmStart::new(cfg).expect("valid config should construct without error");
        let edges = vec![(0, 1, 1.0_f32)];
        // From the saddle point, the gradient is identically zero and the
        // solver stays at the saddle.
        let c_saddle = ws
            .continuous_relax(&edges)
            .expect("valid edges should relax successfully");
        assert!((c_saddle[0] - 0.5).abs() < 1e-6 && (c_saddle[1] - 0.5).abs() < 1e-6);

        // Verify the saddle is indeed where the gradient is zero and the
        // value (0.5 + 0.5 − 0.5) = 0.5 is strictly worse than the corner
        // values (1,0) or (0,1) which both attain 1.0.
        let f_saddle = 0.5 + 0.5 - 2.0 * 0.5 * 0.5; // 0.5
        let f_corner = 1.0 + 0.0 - 2.0 * 1.0 * 0.0; // 1.0
        assert!(
            f_corner > f_saddle,
            "corner should dominate saddle: {f_corner} vs {f_saddle}"
        );
    }

    #[test]
    fn continuous_relax_no_edges_stays_at_init() {
        let ws = QaoaWarmStart::new(default_cfg(3))
            .expect("valid config should construct without error");
        let c = ws
            .continuous_relax(&[])
            .expect("valid edges should relax successfully");
        for &ci in &c {
            assert!((ci - 0.5).abs() < 1e-7, "ci={ci}");
        }
    }

    #[test]
    fn continuous_relax_triangle_nonuniform_optimum_exists() {
        // For the unit-weighted triangle K_3 the relaxed MaxCut objective
        //   f(c) = Σ (c_i + c_j − 2 c_i c_j)
        // attains its maximum on the boundary at a non-uniform corner such
        // as (1, 1, 0) (value 2). The uniform interior point (0.5, 0.5, 0.5)
        // is a saddle of value 1.5. We verify both that (a) the relaxer
        // (which initialises at the saddle, where the gradient is exactly
        // zero by symmetry) returns the saddle and (b) that this saddle is
        // strictly dominated by a non-uniform corner, evidencing that the
        // triangle's true optimum is non-uniform. The triangle endpoints
        // (1,1,0), (1,0,1), (0,1,1) are all valid global maxima.
        let cfg = QaoaWarmStartConfig {
            n_qubits: 3,
            n_iter: 100,
            lr: 0.3,
            mixing_angle: 0.0,
        };
        let ws = QaoaWarmStart::new(cfg).expect("valid config should construct without error");
        let edges = vec![(0, 1, 1.0_f32), (1, 2, 1.0), (0, 2, 1.0)];
        let c = ws
            .continuous_relax(&edges)
            .expect("valid edges should relax successfully");
        // Closed-form objective evaluator for K_3 with unit weights.
        let f = |c: &[f32]| -> f32 {
            edges
                .iter()
                .map(|&(i, j, w)| w * (c[i] + c[j] - 2.0 * c[i] * c[j]))
                .sum()
        };
        let f_saddle = f(&c);
        let f_corner = f(&[1.0, 1.0, 0.0]);
        assert!(
            f_corner > f_saddle + 1e-3,
            "non-uniform corner (1,1,0) should dominate the relaxer's saddle: corner={f_corner} relaxer={f_saddle}"
        );
        // Additional non-uniform corners attain the same maximum.
        assert!((f(&[1.0, 0.0, 1.0]) - f_corner).abs() < 1e-5);
        assert!((f(&[0.0, 1.0, 1.0]) - f_corner).abs() < 1e-5);
    }

    #[test]
    fn arcsin_init_zero_gives_zero() {
        let ws = QaoaWarmStart::new(default_cfg(1))
            .expect("valid config should construct without error");
        let theta = ws
            .arcsin_init(&[0.0])
            .expect("valid c values in [0,1] should map to arcsin angles");
        assert!(theta[0].abs() < 1e-7, "theta={}", theta[0]);
    }

    #[test]
    fn arcsin_init_one_gives_pi() {
        let ws = QaoaWarmStart::new(default_cfg(1))
            .expect("valid config should construct without error");
        let theta = ws
            .arcsin_init(&[1.0])
            .expect("valid c values in [0,1] should map to arcsin angles");
        assert!(
            (theta[0] - std::f32::consts::PI).abs() < 1e-6,
            "theta={}",
            theta[0]
        );
    }

    #[test]
    fn arcsin_init_half_gives_pi_over_two() {
        // c = 0.5  ⇒  √c = √(1/2)  ⇒  arcsin(√(1/2)) = π/4  ⇒  θ = π/2.
        let ws = QaoaWarmStart::new(default_cfg(1))
            .expect("valid config should construct without error");
        let theta = ws
            .arcsin_init(&[0.5])
            .expect("valid c values in [0,1] should map to arcsin angles");
        assert!(
            (theta[0] - std::f32::consts::FRAC_PI_2).abs() < 1e-6,
            "theta={}",
            theta[0]
        );
    }

    #[test]
    fn arcsin_init_range_in_zero_pi() {
        let ws = QaoaWarmStart::new(default_cfg(5))
            .expect("valid config should construct without error");
        let c = vec![0.0, 0.1, 0.4, 0.7, 1.0];
        let theta = ws
            .arcsin_init(&c)
            .expect("valid c values in [0,1] should map to arcsin angles");
        for &t in &theta {
            assert!(
                (0.0..=std::f32::consts::PI).contains(&t),
                "theta={t} not in [0, π]"
            );
        }
        // Monotonic in c.
        for k in 1..theta.len() {
            assert!(theta[k] >= theta[k - 1] - 1e-6);
        }
    }

    #[test]
    fn warm_start_circuit_params_length() {
        let ws = QaoaWarmStart::new(default_cfg(4))
            .expect("valid config should construct without error");
        let c = vec![0.1_f32, 0.4, 0.6, 0.9];
        let params = ws
            .warm_start_circuit_params(&c, 3)
            .expect("valid c and nonzero depth should produce params");
        // n_qubits + 2 * depth = 4 + 6 = 10.
        assert_eq!(params.len(), 4 + 2 * 3);
    }

    #[test]
    fn warm_start_circuit_params_gammas_betas_zero() {
        let ws = QaoaWarmStart::new(default_cfg(3))
            .expect("valid config should construct without error");
        let c = vec![0.2_f32, 0.5, 0.8];
        let depth = 2;
        let params = ws
            .warm_start_circuit_params(&c, depth)
            .expect("valid c and nonzero depth should produce params");
        for (i, &val) in params.iter().enumerate().skip(3).take(2 * depth) {
            assert!(
                val.abs() < 1e-12,
                "expected (γ,β) zero at idx {i} got {val}"
            );
        }
        // The first three entries are the warm-start angles.
        let theta = ws
            .arcsin_init(&c)
            .expect("valid c values in [0,1] should map to arcsin angles");
        for (i, &t) in theta.iter().enumerate() {
            assert!((params[i] - t).abs() < 1e-7);
        }
    }

    #[test]
    fn deterministic_relax() {
        let ws_a = QaoaWarmStart::new(default_cfg(3))
            .expect("valid config should construct without error");
        let ws_b = QaoaWarmStart::new(default_cfg(3))
            .expect("valid config should construct without error");
        let edges = vec![(0, 1, 0.7_f32), (1, 2, 0.3), (0, 2, 0.5)];
        let a = ws_a
            .continuous_relax(&edges)
            .expect("valid edges should relax successfully");
        let b = ws_b
            .continuous_relax(&edges)
            .expect("valid edges should relax successfully");
        assert_eq!(a, b);
    }

    #[test]
    fn err_n_qubits_zero() {
        let cfg = QaoaWarmStartConfig {
            n_qubits: 0,
            n_iter: 10,
            lr: 0.1,
            mixing_angle: 0.0,
        };
        assert!(QaoaWarmStart::new(cfg).is_err());
    }

    #[test]
    fn err_n_iter_zero() {
        let cfg = QaoaWarmStartConfig {
            n_qubits: 3,
            n_iter: 0,
            lr: 0.1,
            mixing_angle: 0.0,
        };
        assert!(QaoaWarmStart::new(cfg).is_err());
    }

    #[test]
    fn err_non_positive_lr() {
        let cfg_zero = QaoaWarmStartConfig {
            n_qubits: 3,
            n_iter: 10,
            lr: 0.0,
            mixing_angle: 0.0,
        };
        let cfg_neg = QaoaWarmStartConfig {
            n_qubits: 3,
            n_iter: 10,
            lr: -0.1,
            mixing_angle: 0.0,
        };
        assert!(QaoaWarmStart::new(cfg_zero).is_err());
        assert!(QaoaWarmStart::new(cfg_neg).is_err());
    }

    #[test]
    fn err_mixing_angle_out_of_range() {
        let cfg_neg = QaoaWarmStartConfig {
            n_qubits: 3,
            n_iter: 10,
            lr: 0.1,
            mixing_angle: -0.1,
        };
        let cfg_big = QaoaWarmStartConfig {
            n_qubits: 3,
            n_iter: 10,
            lr: 0.1,
            mixing_angle: std::f32::consts::FRAC_PI_2 + 0.1,
        };
        assert!(QaoaWarmStart::new(cfg_neg).is_err());
        assert!(QaoaWarmStart::new(cfg_big).is_err());
    }

    #[test]
    fn err_edge_index_out_of_range() {
        let ws = QaoaWarmStart::new(default_cfg(3))
            .expect("valid config should construct without error");
        let edges = vec![(0, 5, 1.0_f32)];
        assert!(ws.continuous_relax(&edges).is_err());
        let edges2 = vec![(7, 1, 1.0_f32)];
        assert!(ws.continuous_relax(&edges2).is_err());
    }

    #[test]
    fn err_arcsin_init_wrong_length() {
        let ws = QaoaWarmStart::new(default_cfg(3))
            .expect("valid config should construct without error");
        let r = ws.arcsin_init(&[0.5, 0.5]);
        assert!(r.is_err());
        let r2 = ws.arcsin_init(&[0.5, 0.5, 0.5, 0.5]);
        assert!(r2.is_err());
    }

    #[test]
    fn err_arcsin_init_c_out_of_range() {
        let ws = QaoaWarmStart::new(default_cfg(2))
            .expect("valid config should construct without error");
        let r_neg = ws.arcsin_init(&[-0.1, 0.5]);
        assert!(r_neg.is_err());
        let r_big = ws.arcsin_init(&[0.5, 1.1]);
        assert!(r_big.is_err());
    }

    #[test]
    fn err_depth_zero() {
        let ws = QaoaWarmStart::new(default_cfg(3))
            .expect("valid config should construct without error");
        let c = vec![0.5_f32, 0.5, 0.5];
        let r = ws.warm_start_circuit_params(&c, 0);
        assert!(r.is_err());
    }

    #[test]
    fn err_warm_start_circuit_params_wrong_c_length() {
        let ws = QaoaWarmStart::new(default_cfg(4))
            .expect("valid config should construct without error");
        let c = vec![0.5_f32, 0.5]; // wrong length
        let r = ws.warm_start_circuit_params(&c, 2);
        assert!(r.is_err());
    }

    #[test]
    fn empty_edges_allowed_in_continuous_relax() {
        let ws = QaoaWarmStart::new(default_cfg(2))
            .expect("valid config should construct without error");
        let r = ws.continuous_relax(&[]);
        assert!(r.is_ok());
    }

    #[test]
    fn clipping_respected_under_large_lr() {
        // With a huge learning rate, gradient steps will try to overshoot
        // [0,1]; the projection must clamp them.
        let cfg = QaoaWarmStartConfig {
            n_qubits: 2,
            n_iter: 20,
            lr: 100.0,
            mixing_angle: 0.0,
        };
        let ws = QaoaWarmStart::new(cfg).expect("valid config should construct without error");
        // From the saddle the gradient is zero, so seed asymmetry through
        // an asymmetric weighted edge structure.
        let edges = vec![(0, 1, 1.0_f32)];
        let c = ws
            .continuous_relax(&edges)
            .expect("valid edges should relax successfully");
        for &ci in &c {
            assert!(
                (0.0..=1.0).contains(&ci),
                "ci={ci} escaped clamp under aggressive lr"
            );
        }
    }

    #[test]
    fn config_getter_exposes_inner_settings() {
        let cfg = QaoaWarmStartConfig {
            n_qubits: 5,
            n_iter: 7,
            lr: 0.25,
            mixing_angle: 0.1,
        };
        let ws = QaoaWarmStart::new(cfg).expect("valid config should construct without error");
        let inner = ws.config();
        assert_eq!(inner.n_qubits, 5);
        assert_eq!(inner.n_iter, 7);
        assert!((inner.lr - 0.25).abs() < 1e-7);
        assert!((inner.mixing_angle - 0.1).abs() < 1e-7);
    }

    #[test]
    fn warm_start_params_full_pipeline() {
        // End-to-end: relax → arcsin_init → warm-start params for a
        // 3-vertex path graph.
        let cfg = QaoaWarmStartConfig {
            n_qubits: 3,
            n_iter: 50,
            lr: 0.2,
            mixing_angle: std::f32::consts::FRAC_PI_4,
        };
        let ws = QaoaWarmStart::new(cfg).expect("valid config should construct without error");
        let edges = vec![(0, 1, 1.0_f32), (1, 2, 1.0)];
        let c = ws
            .continuous_relax(&edges)
            .expect("valid edges should relax successfully");
        let params = ws
            .warm_start_circuit_params(&c, 4)
            .expect("valid c and nonzero depth should produce params");
        assert_eq!(params.len(), 3 + 2 * 4);
        // All variational params zero.
        for &p in &params[3..] {
            assert!(p.abs() < 1e-12);
        }
        // All warm-start angles in [0, π].
        for &t in &params[..3] {
            assert!((0.0..=std::f32::consts::PI).contains(&t), "theta={t}");
        }
    }
}