quantrs2-core 0.1.3

Core types and traits for the QuantRS2 quantum computing framework
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
// Quantum Amplitude Estimation (QAE)
//
// State-of-the-art amplitude estimation algorithms for quantum Monte Carlo,
// financial risk analysis, and machine learning applications.
//
// Implements multiple QAE variants:
// - Canonical QAE (quantum phase estimation based)
// - Maximum Likelihood Amplitude Estimation (MLAE)
// - Iterative Quantum Amplitude Estimation (IQAE)
// - Faster Amplitude Estimation (FAE)
//
// Reference: Brassard et al. (2002), Grinko et al. (2021)

use crate::error::QuantRS2Error;
use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::random::prelude::*;
use scirs2_core::Complex64;
use std::f64::consts::PI;

/// Amplitude to be estimated in a quantum state
///
/// For a state |ψ⟩ = √a|ψ_good⟩ + √(1-a)|ψ_bad⟩,
/// this trait defines how to prepare |ψ⟩ and recognize |ψ_good⟩
pub trait AmplitudeOracle {
    /// Prepare the quantum state |ψ⟩
    fn state_preparation(&self) -> Array1<Complex64>;

    /// Oracle that marks "good" states (applies phase flip to |ψ_good⟩)
    fn grover_oracle(&self, state: &mut Array1<Complex64>);

    /// Number of qubits required
    fn num_qubits(&self) -> usize;

    /// Check if a computational basis state is "good"
    fn is_good_state(&self, basis_index: usize) -> bool;
}

/// Grover operator for amplitude amplification
///
/// Q = -A S_0 A† S_χ where:
/// - A is the state preparation operator
/// - S_0 flips the sign of the |0⟩ state
/// - S_χ is the oracle marking good states
#[derive(Debug, Clone)]
pub struct GroverOperator {
    num_qubits: usize,
}

impl GroverOperator {
    /// Create a new Grover operator
    pub const fn new(num_qubits: usize) -> Self {
        Self { num_qubits }
    }

    /// Apply one Grover iteration to the state
    pub fn apply(
        &self,
        state: &mut Array1<Complex64>,
        oracle: &dyn AmplitudeOracle,
    ) -> Result<(), QuantRS2Error> {
        let dim = 1 << self.num_qubits;
        if state.len() != dim {
            return Err(QuantRS2Error::InvalidInput(format!(
                "State dimension {} doesn't match 2^{}",
                state.len(),
                self.num_qubits
            )));
        }

        // Step 1: Apply oracle S_χ (flip sign of good states)
        oracle.grover_oracle(state);

        // Step 2: Apply diffusion operator (reflection about average)
        self.apply_diffusion(state);

        Ok(())
    }

    /// Apply diffusion operator: 2|ψ⟩⟨ψ| - I
    fn apply_diffusion(&self, state: &mut Array1<Complex64>) {
        // Compute average amplitude
        let avg: Complex64 = state.iter().sum::<Complex64>() / (state.len() as f64);

        // Reflect about average: state -> 2*avg - state
        for amplitude in state.iter_mut() {
            *amplitude = Complex64::new(2.0, 0.0) * avg - *amplitude;
        }
    }
}

/// Canonical Quantum Amplitude Estimation using Quantum Phase Estimation
#[derive(Debug)]
pub struct CanonicalQAE {
    /// Number of evaluation qubits for QPE
    pub num_eval_qubits: usize,
    /// Grover operator
    grover_operator: GroverOperator,
}

impl CanonicalQAE {
    /// Create a new canonical QAE instance
    ///
    /// # Arguments
    /// * `num_eval_qubits` - Number of qubits for phase estimation (precision ~ 2^(-n))
    /// * `num_state_qubits` - Number of qubits in the state being estimated
    pub const fn new(num_eval_qubits: usize, num_state_qubits: usize) -> Self {
        Self {
            num_eval_qubits,
            grover_operator: GroverOperator::new(num_state_qubits),
        }
    }

    /// Estimate the amplitude using quantum phase estimation
    ///
    /// Returns (estimated_amplitude, confidence_interval)
    pub fn estimate(
        &self,
        oracle: &dyn AmplitudeOracle,
    ) -> Result<(f64, (f64, f64)), QuantRS2Error> {
        // Prepare initial state
        let mut state = oracle.state_preparation();

        // Apply Quantum Phase Estimation on the Grover operator
        let phase = self.quantum_phase_estimation(&state, oracle)?;

        // Convert phase to amplitude: a = sin²(θ/2) where θ = phase * π
        let theta = phase * PI;
        let amplitude = (theta / 2.0).sin().powi(2);

        // Compute confidence interval based on Heisenberg limit
        let precision = PI / (1 << self.num_eval_qubits) as f64;
        let lower_bound = ((theta - precision) / 2.0).sin().powi(2).max(0.0);
        let upper_bound = f64::midpoint(theta, precision).sin().powi(2).min(1.0);

        Ok((amplitude, (lower_bound, upper_bound)))
    }

    /// Quantum Phase Estimation for the Grover operator
    fn quantum_phase_estimation(
        &self,
        state: &Array1<Complex64>,
        oracle: &dyn AmplitudeOracle,
    ) -> Result<f64, QuantRS2Error> {
        // Simplified QPE: measure eigenvalue of Grover operator
        // In full implementation, would use controlled-Grover operations

        let num_measurements = 1 << self.num_eval_qubits;
        let mut phase_estimates = Vec::new();

        for k in 0..num_measurements {
            let mut temp_state = state.clone();

            // Apply Grover^k
            for _ in 0..k {
                self.grover_operator.apply(&mut temp_state, oracle)?;
            }

            // Measure phase (simplified)
            let measurement = self.measure_phase(&temp_state);
            phase_estimates.push(measurement);
        }

        // Average the phase estimates
        let avg_phase = phase_estimates.iter().sum::<f64>() / phase_estimates.len() as f64;

        Ok(avg_phase)
    }

    /// Measure the phase of a quantum state
    fn measure_phase(&self, state: &Array1<Complex64>) -> f64 {
        // Simplified: extract phase from dominant amplitude
        let mut max_amplitude = 0.0;
        let mut max_phase = 0.0;

        for amp in state {
            let magnitude = amp.norm();
            if magnitude > max_amplitude {
                max_amplitude = magnitude;
                max_phase = amp.arg();
            }
        }

        max_phase / (2.0 * PI)
    }
}

/// Maximum Likelihood Amplitude Estimation (MLAE)
///
/// Uses classical maximum likelihood estimation on measurement outcomes
/// to achieve optimal statistical efficiency.
///
/// Reference: Suzuki et al. (2020). "Amplitude estimation without phase estimation"
#[derive(Debug)]
pub struct MaximumLikelihoodAE {
    /// Number of Grover iterations to use
    pub schedule: Vec<usize>,
    /// Grover operator
    grover_operator: GroverOperator,
}

impl MaximumLikelihoodAE {
    /// Create a new MLAE instance with custom schedule
    pub const fn new(schedule: Vec<usize>, num_qubits: usize) -> Self {
        Self {
            schedule,
            grover_operator: GroverOperator::new(num_qubits),
        }
    }

    /// Create with exponential schedule: [0, 1, 2, 4, 8, ..., 2^k]
    pub fn with_exponential_schedule(max_power: usize, num_qubits: usize) -> Self {
        let schedule: Vec<usize> = (0..=max_power).map(|k| 1 << k).collect();
        Self::new(schedule, num_qubits)
    }

    /// Estimate amplitude using maximum likelihood
    pub fn estimate(
        &self,
        oracle: &dyn AmplitudeOracle,
        shots_per_iteration: usize,
    ) -> Result<(f64, f64), QuantRS2Error> {
        let mut observations = Vec::new();

        // Collect measurements for each number of Grover iterations
        for &num_grover in &self.schedule {
            let good_state_count =
                self.run_measurements(oracle, num_grover, shots_per_iteration)?;
            let success_probability = good_state_count as f64 / shots_per_iteration as f64;
            observations.push((num_grover, success_probability));
        }

        // Maximum likelihood estimation
        let (estimated_amplitude, fisher_info) = self.maximum_likelihood(&observations)?;

        // Compute standard deviation from Fisher information
        let std_dev = 1.0 / fisher_info.sqrt();

        Ok((estimated_amplitude, std_dev))
    }

    /// Run measurements for a specific number of Grover iterations
    fn run_measurements(
        &self,
        oracle: &dyn AmplitudeOracle,
        num_grover: usize,
        shots: usize,
    ) -> Result<usize, QuantRS2Error> {
        let mut good_count = 0;

        for _ in 0..shots {
            let mut state = oracle.state_preparation();

            // Apply Grover iterations
            for _ in 0..num_grover {
                self.grover_operator.apply(&mut state, oracle)?;
            }

            // Measure and check if in good state
            let measurement = self.measure_computational_basis(&state);
            if oracle.is_good_state(measurement) {
                good_count += 1;
            }
        }

        Ok(good_count)
    }

    /// Measure in computational basis
    fn measure_computational_basis(&self, state: &Array1<Complex64>) -> usize {
        let mut rng = thread_rng();
        let random: f64 = rng.random();

        let mut cumulative_prob = 0.0;
        for (idx, amp) in state.iter().enumerate() {
            cumulative_prob += amp.norm_sqr();
            if random <= cumulative_prob {
                return idx;
            }
        }

        state.len() - 1
    }

    /// Maximum likelihood estimation from observations
    fn maximum_likelihood(
        &self,
        observations: &[(usize, f64)],
    ) -> Result<(f64, f64), QuantRS2Error> {
        // Grid search for maximum likelihood
        let mut best_amplitude = 0.0;
        let mut best_likelihood = f64::NEG_INFINITY;

        const GRID_POINTS: usize = 1000;
        for i in 0..=GRID_POINTS {
            let a = i as f64 / GRID_POINTS as f64;
            let likelihood = self.compute_log_likelihood(a, observations);

            if likelihood > best_likelihood {
                best_likelihood = likelihood;
                best_amplitude = a;
            }
        }

        // Compute Fisher information at the MLE
        let fisher_info = self.compute_fisher_information(best_amplitude, observations);

        Ok((best_amplitude, fisher_info))
    }

    /// Compute log-likelihood for a given amplitude
    fn compute_log_likelihood(&self, amplitude: f64, observations: &[(usize, f64)]) -> f64 {
        let theta = (amplitude.sqrt()).asin() * 2.0;
        let mut log_likelihood = 0.0;

        for &(m, p_obs) in observations {
            // Probability of success after m Grover iterations
            let p_theory = (2.0f64.mul_add(m as f64, 1.0) * theta / 2.0).sin().powi(2);

            // Binomial log-likelihood (simplified)
            log_likelihood += p_obs.mul_add(p_theory.ln(), (1.0 - p_obs) * (1.0 - p_theory).ln());
        }

        log_likelihood
    }

    /// Compute Fisher information
    fn compute_fisher_information(&self, amplitude: f64, observations: &[(usize, f64)]) -> f64 {
        let theta = (amplitude.sqrt()).asin() * 2.0;
        let mut fisher_info = 0.0;

        for &(m, _) in observations {
            // Derivative of success probability w.r.t. theta
            let phase = 2.0f64.mul_add(m as f64, 1.0) * theta / 2.0;
            let derivative = 2.0f64.mul_add(m as f64, 1.0) * phase.sin() * phase.cos();

            let p = phase.sin().powi(2);
            fisher_info += derivative.powi(2) / (p * (1.0 - p)).max(1e-10);
        }

        fisher_info
    }
}

/// Iterative Quantum Amplitude Estimation (IQAE)
///
/// Adaptive algorithm that iteratively narrows the confidence interval
/// using Bayesian inference.
///
/// Reference: Grinko et al. (2021). "Iterative Quantum Amplitude Estimation"
#[derive(Debug)]
pub struct IterativeQAE {
    /// Target accuracy (epsilon)
    pub target_accuracy: f64,
    /// Confidence level (alpha)
    pub confidence_level: f64,
    /// Grover operator
    grover_operator: GroverOperator,
}

impl IterativeQAE {
    /// Create a new IQAE instance
    pub const fn new(target_accuracy: f64, confidence_level: f64, num_qubits: usize) -> Self {
        Self {
            target_accuracy,
            confidence_level,
            grover_operator: GroverOperator::new(num_qubits),
        }
    }

    /// Estimate amplitude iteratively
    pub fn estimate(&mut self, oracle: &dyn AmplitudeOracle) -> Result<IQAEResult, QuantRS2Error> {
        let mut lower_bound = 0.0;
        let mut upper_bound = 1.0;
        let mut num_oracle_calls = 0;
        let mut iteration = 0;

        while (upper_bound - lower_bound) > self.target_accuracy {
            // Choose number of Grover iterations based on current interval
            let k = self.choose_grover_iterations(lower_bound, upper_bound);

            // Run measurements
            let success_count = self.run_adaptive_measurements(oracle, k, 100)?;
            let success_rate = success_count as f64 / 100.0;
            num_oracle_calls += 100 * (k + 1);

            // Update interval using Bayesian inference
            (lower_bound, upper_bound) =
                self.update_interval(lower_bound, upper_bound, k, success_rate);

            iteration += 1;
        }

        let estimated_amplitude = f64::midpoint(lower_bound, upper_bound);

        Ok(IQAEResult {
            amplitude: estimated_amplitude,
            lower_bound,
            upper_bound,
            num_iterations: iteration,
            num_oracle_calls,
        })
    }

    /// Choose optimal number of Grover iterations
    fn choose_grover_iterations(&self, lower: f64, upper: f64) -> usize {
        let theta_lower = (lower.sqrt()).asin() * 2.0;
        let theta_upper = (upper.sqrt()).asin() * 2.0;
        let theta_mid = f64::midpoint(theta_lower, theta_upper);

        // Choose k such that (2k+1)θ ≈ π/2 for maximum discrimination

        ((PI / 2.0) / theta_mid - 0.5).max(0.0) as usize
    }

    /// Run measurements adaptively
    fn run_adaptive_measurements(
        &self,
        oracle: &dyn AmplitudeOracle,
        num_grover: usize,
        shots: usize,
    ) -> Result<usize, QuantRS2Error> {
        let mut success_count = 0;

        for _ in 0..shots {
            let mut state = oracle.state_preparation();

            for _ in 0..num_grover {
                self.grover_operator.apply(&mut state, oracle)?;
            }

            let measurement = self.measure_good_state(&state, oracle);
            if measurement {
                success_count += 1;
            }
        }

        Ok(success_count)
    }

    /// Measure whether state is in good subspace
    fn measure_good_state(&self, state: &Array1<Complex64>, oracle: &dyn AmplitudeOracle) -> bool {
        let mut rng = thread_rng();
        let random: f64 = rng.random();

        let mut cumulative_prob = 0.0;
        for (idx, amp) in state.iter().enumerate() {
            cumulative_prob += amp.norm_sqr();
            if random <= cumulative_prob {
                return oracle.is_good_state(idx);
            }
        }

        false
    }

    /// Update confidence interval using Bayesian inference
    fn update_interval(
        &self,
        lower: f64,
        upper: f64,
        k: usize,
        observed_success_rate: f64,
    ) -> (f64, f64) {
        // Simplified Bayesian update
        // In full implementation, would use likelihood-weighted sampling

        const GRID_SIZE: usize = 100;
        let mut likelihoods = vec![0.0; GRID_SIZE];
        let mut max_likelihood = f64::NEG_INFINITY;

        for i in 0..GRID_SIZE {
            let a = lower + (upper - lower) * i as f64 / (GRID_SIZE - 1) as f64;
            let theta = (a.sqrt()).asin() * 2.0;
            let p_theory = ((2 * k + 1) as f64 * theta / 2.0).sin().powi(2);

            // Binomial likelihood
            let likelihood = -((observed_success_rate - p_theory).powi(2));
            likelihoods[i] = likelihood;
            max_likelihood = max_likelihood.max(likelihood);
        }

        // Find credible interval
        let threshold = max_likelihood - 2.0; // Approximately 95% confidence
        let mut new_lower = lower;
        let mut new_upper = upper;

        for (i, &likelihood) in likelihoods.iter().enumerate() {
            if likelihood >= threshold {
                let a = lower + (upper - lower) * i as f64 / (GRID_SIZE - 1) as f64;
                if a < new_lower || new_lower == lower {
                    new_lower = a;
                }
                new_upper = a;
            }
        }

        (new_lower, new_upper)
    }
}

/// Result from Iterative QAE
#[derive(Debug, Clone)]
pub struct IQAEResult {
    /// Estimated amplitude
    pub amplitude: f64,
    /// Lower confidence bound
    pub lower_bound: f64,
    /// Upper confidence bound
    pub upper_bound: f64,
    /// Number of iterations performed
    pub num_iterations: usize,
    /// Total number of oracle calls
    pub num_oracle_calls: usize,
}

impl IQAEResult {
    /// Get confidence interval width
    pub fn interval_width(&self) -> f64 {
        self.upper_bound - self.lower_bound
    }

    /// Get relative error
    pub fn relative_error(&self) -> f64 {
        self.interval_width() / self.amplitude.max(1e-10)
    }
}

/// Example: Financial option pricing oracle
///
/// For European call option: payoff = max(S_T - K, 0)
/// We estimate the probability that S_T > K using QAE
pub struct OptionPricingOracle {
    num_qubits: usize,
    strike_price: f64,
    risk_free_rate: f64,
    volatility: f64,
    time_to_maturity: f64,
}

impl OptionPricingOracle {
    /// Create a new option pricing oracle
    pub const fn new(
        num_qubits: usize,
        strike_price: f64,
        risk_free_rate: f64,
        volatility: f64,
        time_to_maturity: f64,
    ) -> Self {
        Self {
            num_qubits,
            strike_price,
            risk_free_rate,
            volatility,
            time_to_maturity,
        }
    }

    /// Compute payoff for a given price index
    fn payoff(&self, price_index: usize) -> f64 {
        // Map index to price using log-normal distribution discretization
        let s_t = self.index_to_price(price_index);
        (s_t - self.strike_price).max(0.0)
    }

    /// Convert discrete index to continuous price
    fn index_to_price(&self, index: usize) -> f64 {
        let num_levels = 1 << self.num_qubits;
        let normalized = index as f64 / num_levels as f64;

        // Inverse CDF of log-normal distribution (simplified)
        let z = (normalized * 6.0) - 3.0; // Approximate normal quantile
        let s_0 = self.strike_price; // Assume ATM
        s_0 * 0.5f64
            .mul_add(-self.volatility.powi(2), self.risk_free_rate)
            .mul_add(
                self.time_to_maturity,
                self.volatility * self.time_to_maturity.sqrt() * z,
            )
            .exp()
    }
}

impl AmplitudeOracle for OptionPricingOracle {
    fn state_preparation(&self) -> Array1<Complex64> {
        let dim = 1 << self.num_qubits;
        let mut state = Array1::<Complex64>::zeros(dim);

        // Uniform superposition (simplified)
        let amplitude = Complex64::new(1.0 / (dim as f64).sqrt(), 0.0);
        state.fill(amplitude);

        state
    }

    fn grover_oracle(&self, state: &mut Array1<Complex64>) {
        for (idx, amplitude) in state.iter_mut().enumerate() {
            if self.is_good_state(idx) {
                *amplitude = -*amplitude; // Phase flip
            }
        }
    }

    fn num_qubits(&self) -> usize {
        self.num_qubits
    }

    fn is_good_state(&self, basis_index: usize) -> bool {
        self.payoff(basis_index) > 0.0
    }
}

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

    #[test]
    fn test_grover_operator() {
        let grover = GroverOperator::new(2);

        // Simple oracle for testing
        struct TestOracle;
        impl AmplitudeOracle for TestOracle {
            fn state_preparation(&self) -> Array1<Complex64> {
                Array1::from_vec(vec![
                    Complex64::new(0.5, 0.0),
                    Complex64::new(0.5, 0.0),
                    Complex64::new(0.5, 0.0),
                    Complex64::new(0.5, 0.0),
                ])
            }

            fn grover_oracle(&self, state: &mut Array1<Complex64>) {
                state[3] = -state[3]; // Mark state |11⟩
            }

            fn num_qubits(&self) -> usize {
                2
            }
            fn is_good_state(&self, basis_index: usize) -> bool {
                basis_index == 3
            }
        }

        let oracle = TestOracle;
        let mut state = oracle.state_preparation();

        grover
            .apply(&mut state, &oracle)
            .expect("Grover operator application should succeed");

        // After one Grover iteration, amplitude of |11⟩ should increase
        assert!(state[3].norm() > 0.5);
    }

    #[test]
    fn test_mlae_exponential_schedule() {
        let mlae = MaximumLikelihoodAE::with_exponential_schedule(3, 2);

        assert_eq!(mlae.schedule, vec![1, 2, 4, 8]);
    }

    #[test]
    fn test_iqae_interval_update() {
        let iqae = IterativeQAE::new(0.01, 0.95, 2);

        let (lower, upper) = iqae.update_interval(0.0, 1.0, 1, 0.5);

        // Interval should be narrowed
        assert!(upper - lower < 1.0);
        assert!(lower >= 0.0 && upper <= 1.0);
    }

    #[test]
    fn test_option_pricing_oracle() {
        let oracle = OptionPricingOracle::new(3, 100.0, 0.05, 0.2, 1.0);

        assert_eq!(oracle.num_qubits(), 3);

        let state = oracle.state_preparation();
        assert_eq!(state.len(), 8);

        // Check that state preparation creates valid quantum state
        let norm: f64 = state.iter().map(|c| c.norm_sqr()).sum();
        assert!((norm - 1.0).abs() < 1e-6);
    }
}