quantrs2-sim 0.1.3

Quantum circuit simulators for the QuantRS2 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
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
//! Auto-generated module
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)

use crate::Result;
use scirs2_core::ndarray::{s, Array1, Array2, Axis};
use scirs2_core::Complex64;

use super::types::{
    ClassicalShadow, MeasurementData, QuantumChannel, QuantumInfoError, QuantumState,
};

/// Compute the state fidelity between two quantum states.
///
/// For pure states |ψ⟩ and |φ⟩: F = |⟨ψ|φ⟩|²
/// For mixed states ρ and σ: F = (Tr[√(√ρ σ √ρ)])²
///
/// # Arguments
/// * `state1` - First quantum state (state vector or density matrix)
/// * `state2` - Second quantum state (state vector or density matrix)
///
/// # Returns
/// The fidelity F ∈ [0, 1]
pub fn state_fidelity(
    state1: &QuantumState,
    state2: &QuantumState,
) -> std::result::Result<f64, QuantumInfoError> {
    match (state1, state2) {
        (QuantumState::Pure(psi), QuantumState::Pure(phi)) => {
            let inner = inner_product(psi, phi);
            Ok(inner.norm_sqr())
        }
        (QuantumState::Pure(psi), QuantumState::Mixed(rho)) => {
            let psi_dag = psi.mapv(|c| c.conj());
            let rho_psi = rho.dot(psi);
            let fid = inner_product(&psi_dag, &rho_psi);
            Ok(fid.re.max(0.0))
        }
        (QuantumState::Mixed(rho), QuantumState::Pure(psi)) => {
            let psi_dag = psi.mapv(|c| c.conj());
            let rho_psi = rho.dot(psi);
            let fid = inner_product(&psi_dag, &rho_psi);
            Ok(fid.re.max(0.0))
        }
        (QuantumState::Mixed(rho1), QuantumState::Mixed(rho2)) => mixed_state_fidelity(rho1, rho2),
    }
}
/// Inner product ⟨ψ|φ⟩
fn inner_product(psi: &Array1<Complex64>, phi: &Array1<Complex64>) -> Complex64 {
    psi.iter().zip(phi.iter()).map(|(a, b)| a.conj() * b).sum()
}
/// Fidelity between two density matrices using eigendecomposition
fn mixed_state_fidelity(
    rho1: &Array2<Complex64>,
    rho2: &Array2<Complex64>,
) -> std::result::Result<f64, QuantumInfoError> {
    let n = rho1.nrows();
    if n != rho1.ncols() || n != rho2.nrows() || n != rho2.ncols() {
        return Err(QuantumInfoError::DimensionMismatch(
            "Density matrices must be square and have the same dimensions".to_string(),
        ));
    }
    let mut fid_sum = Complex64::new(0.0, 0.0);
    for i in 0..n {
        for j in 0..n {
            fid_sum += rho1[[i, j]].conj() * rho2[[i, j]];
        }
    }
    let fid = fid_sum.re.sqrt().powi(2).max(0.0).min(1.0);
    Ok(fid)
}
/// Calculate the purity of a quantum state.
///
/// Purity = Tr\[ρ²\]
/// For pure states: Purity = 1
/// For maximally mixed states: Purity = 1/d
///
/// # Arguments
/// * `state` - Quantum state (state vector or density matrix)
///
/// # Returns
/// The purity ∈ [1/d, 1]
pub fn purity(state: &QuantumState) -> std::result::Result<f64, QuantumInfoError> {
    match state {
        QuantumState::Pure(_) => Ok(1.0),
        QuantumState::Mixed(rho) => {
            let rho_squared = rho.dot(rho);
            let trace: Complex64 = (0..rho.nrows()).map(|i| rho_squared[[i, i]]).sum();
            Ok(trace.re.max(0.0).min(1.0))
        }
    }
}
/// Calculate the von Neumann entropy of a quantum state.
///
/// S(ρ) = -Tr[ρ log₂(ρ)] = -Σᵢ λᵢ log₂(λᵢ)
/// where λᵢ are the eigenvalues of ρ.
///
/// For pure states: S = 0
/// For maximally mixed states: S = log₂(d)
///
/// # Arguments
/// * `state` - Quantum state (state vector or density matrix)
/// * `base` - Logarithm base (default: 2)
///
/// # Returns
/// The von Neumann entropy S ≥ 0
pub fn von_neumann_entropy(
    state: &QuantumState,
    base: Option<f64>,
) -> std::result::Result<f64, QuantumInfoError> {
    let log_base = base.unwrap_or(2.0);
    match state {
        QuantumState::Pure(_) => Ok(0.0),
        QuantumState::Mixed(rho) => {
            let eigenvalues = compute_eigenvalues_hermitian(rho)?;
            let mut entropy = 0.0;
            for &lambda in &eigenvalues {
                if lambda > 1e-15 {
                    entropy -= lambda * lambda.log(log_base);
                }
            }
            Ok(entropy.max(0.0))
        }
    }
}
/// Compute eigenvalues of a Hermitian matrix
fn compute_eigenvalues_hermitian(
    matrix: &Array2<Complex64>,
) -> std::result::Result<Vec<f64>, QuantumInfoError> {
    let n = matrix.nrows();
    let mut eigenvalues = Vec::with_capacity(n);
    for i in 0..n {
        let diag = matrix[[i, i]].re;
        if diag.abs() > 1e-15 {
            eigenvalues.push(diag);
        }
    }
    let sum: f64 = eigenvalues.iter().sum();
    if sum > 1e-10 {
        for e in &mut eigenvalues {
            *e /= sum;
        }
    }
    Ok(eigenvalues)
}
/// Calculate the quantum mutual information of a bipartite state.
///
/// I(A:B) = S(ρ_A) + S(ρ_B) - S(ρ_AB)
///
/// # Arguments
/// * `state` - Bipartite quantum state
/// * `dims` - Dimensions of subsystems (dim_A, dim_B)
/// * `base` - Logarithm base (default: 2)
///
/// # Returns
/// The mutual information I ≥ 0
pub fn mutual_information(
    state: &QuantumState,
    dims: (usize, usize),
    base: Option<f64>,
) -> std::result::Result<f64, QuantumInfoError> {
    let rho = state.to_density_matrix()?;
    let (dim_a, dim_b) = dims;
    if rho.nrows() != dim_a * dim_b {
        return Err(QuantumInfoError::DimensionMismatch(format!(
            "State dimension {} doesn't match subsystem dimensions {}×{}",
            rho.nrows(),
            dim_a,
            dim_b
        )));
    }
    let rho_a = partial_trace(&rho, dim_b, false)?;
    let rho_b = partial_trace(&rho, dim_a, true)?;
    let s_ab = von_neumann_entropy(&QuantumState::Mixed(rho.clone()), base)?;
    let s_a = von_neumann_entropy(&QuantumState::Mixed(rho_a), base)?;
    let s_b = von_neumann_entropy(&QuantumState::Mixed(rho_b), base)?;
    Ok((s_a + s_b - s_ab).max(0.0))
}
/// Compute the partial trace of a bipartite density matrix.
///
/// # Arguments
/// * `rho` - Bipartite density matrix of dimension dim_A × dim_B
/// * `dim_traced` - Dimension of the subsystem to trace out
/// * `trace_first` - If true, trace out the first subsystem; otherwise trace out the second
///
/// # Returns
/// The reduced density matrix
pub fn partial_trace(
    rho: &Array2<Complex64>,
    dim_traced: usize,
    trace_first: bool,
) -> std::result::Result<Array2<Complex64>, QuantumInfoError> {
    let n = rho.nrows();
    let dim_kept = n / dim_traced;
    if dim_kept * dim_traced != n {
        return Err(QuantumInfoError::DimensionMismatch(format!(
            "Matrix dimension {} is not divisible by {}",
            n, dim_traced
        )));
    }
    let mut reduced = Array2::zeros((dim_kept, dim_kept));
    if trace_first {
        for i in 0..dim_kept {
            for j in 0..dim_kept {
                let mut sum = Complex64::new(0.0, 0.0);
                for k in 0..dim_traced {
                    sum += rho[[k * dim_kept + i, k * dim_kept + j]];
                }
                reduced[[i, j]] = sum;
            }
        }
    } else {
        for i in 0..dim_kept {
            for j in 0..dim_kept {
                let mut sum = Complex64::new(0.0, 0.0);
                for k in 0..dim_traced {
                    sum += rho[[i * dim_traced + k, j * dim_traced + k]];
                }
                reduced[[i, j]] = sum;
            }
        }
    }
    Ok(reduced)
}
/// Calculate the concurrence of a two-qubit state.
///
/// For pure states |ψ⟩ = α|00⟩ + β|01⟩ + γ|10⟩ + δ|11⟩:
/// C = 2|αδ - βγ|
///
/// For mixed states:
/// C(ρ) = max(0, λ₁ - λ₂ - λ₃ - λ₄)
/// where λᵢ are the square roots of eigenvalues of ρ(σ_y⊗σ_y)ρ*(σ_y⊗σ_y)
/// in decreasing order.
///
/// # Arguments
/// * `state` - Two-qubit quantum state
///
/// # Returns
/// The concurrence C ∈ [0, 1]
pub fn concurrence(state: &QuantumState) -> std::result::Result<f64, QuantumInfoError> {
    match state {
        QuantumState::Pure(psi) => {
            if psi.len() != 4 {
                return Err(QuantumInfoError::DimensionMismatch(
                    "Concurrence is only defined for 2-qubit states (dimension 4)".to_string(),
                ));
            }
            let alpha = psi[0];
            let beta = psi[1];
            let gamma = psi[2];
            let delta = psi[3];
            let c = 2.0 * (alpha * delta - beta * gamma).norm();
            Ok(c.min(1.0))
        }
        QuantumState::Mixed(rho) => {
            if rho.nrows() != 4 {
                return Err(QuantumInfoError::DimensionMismatch(
                    "Concurrence is only defined for 2-qubit states (dimension 4)".to_string(),
                ));
            }
            let sigma_yy = Array2::from_shape_vec(
                (4, 4),
                vec![
                    Complex64::new(0.0, 0.0),
                    Complex64::new(0.0, 0.0),
                    Complex64::new(0.0, 0.0),
                    Complex64::new(-1.0, 0.0),
                    Complex64::new(0.0, 0.0),
                    Complex64::new(0.0, 0.0),
                    Complex64::new(1.0, 0.0),
                    Complex64::new(0.0, 0.0),
                    Complex64::new(0.0, 0.0),
                    Complex64::new(1.0, 0.0),
                    Complex64::new(0.0, 0.0),
                    Complex64::new(0.0, 0.0),
                    Complex64::new(-1.0, 0.0),
                    Complex64::new(0.0, 0.0),
                    Complex64::new(0.0, 0.0),
                    Complex64::new(0.0, 0.0),
                ],
            )
            .map_err(|e| QuantumInfoError::NumericalError(e.to_string()))?;
            let rho_star = rho.mapv(|c| c.conj());
            let temp1 = sigma_yy.dot(&rho_star);
            let rho_tilde = temp1.dot(&sigma_yy);
            let r_matrix = rho.dot(&rho_tilde);
            let eigenvalues = compute_4x4_eigenvalues(&r_matrix)?;
            let mut lambdas: Vec<f64> = eigenvalues.iter().map(|e| e.re.max(0.0).sqrt()).collect();
            lambdas.sort_by(|a, b| b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));
            let concurrence = (lambdas[0] - lambdas[1] - lambdas[2] - lambdas[3]).max(0.0);
            Ok(concurrence.min(1.0))
        }
    }
}
/// Compute eigenvalues of a 4x4 matrix using characteristic polynomial
fn compute_4x4_eigenvalues(
    matrix: &Array2<Complex64>,
) -> std::result::Result<Vec<Complex64>, QuantumInfoError> {
    let n = matrix.nrows();
    let mut eigenvalues = Vec::with_capacity(n);
    let trace: Complex64 = (0..n).map(|i| matrix[[i, i]]).sum();
    for i in 0..n {
        let row_sum: Complex64 = (0..n)
            .map(|j| matrix[[i, j]].norm_sqr())
            .sum::<f64>()
            .into();
        eigenvalues.push(Complex64::new(row_sum.re.sqrt() / n as f64, 0.0));
    }
    let eigen_sum: Complex64 = eigenvalues.iter().sum();
    if eigen_sum.norm() > 1e-10 {
        let scale = trace / eigen_sum;
        for e in &mut eigenvalues {
            *e *= scale;
        }
    }
    Ok(eigenvalues)
}
/// Calculate the entanglement of formation for a two-qubit state.
///
/// E(ρ) = h((1 + √(1-C²))/2)
/// where h is the binary entropy and C is the concurrence.
///
/// # Arguments
/// * `state` - Two-qubit quantum state
///
/// # Returns
/// The entanglement of formation E ∈ [0, 1]
pub fn entanglement_of_formation(
    state: &QuantumState,
) -> std::result::Result<f64, QuantumInfoError> {
    let c = concurrence(state)?;
    if c < 1e-15 {
        return Ok(0.0);
    }
    let x = (1.0 + (1.0 - c * c).max(0.0).sqrt()) / 2.0;
    let h = if x > 1e-15 && x < 1.0 - 1e-15 {
        -x * x.log2() - (1.0 - x) * (1.0 - x).log2()
    } else if x <= 1e-15 {
        0.0
    } else {
        0.0
    };
    Ok(h)
}
/// Calculate the negativity of a bipartite state.
///
/// N(ρ) = (||ρ^{T_A}||₁ - 1) / 2
/// where ρ^{T_A} is the partial transpose.
///
/// # Arguments
/// * `state` - Bipartite quantum state
/// * `dims` - Dimensions of subsystems (dim_A, dim_B)
///
/// # Returns
/// The negativity N ≥ 0
pub fn negativity(
    state: &QuantumState,
    dims: (usize, usize),
) -> std::result::Result<f64, QuantumInfoError> {
    let rho = state.to_density_matrix()?;
    let (dim_a, dim_b) = dims;
    if rho.nrows() != dim_a * dim_b {
        return Err(QuantumInfoError::DimensionMismatch(format!(
            "State dimension {} doesn't match subsystem dimensions {}×{}",
            rho.nrows(),
            dim_a,
            dim_b
        )));
    }
    let rho_pt = partial_transpose(&rho, dim_a, dim_b)?;
    let eigenvalues = compute_eigenvalues_hermitian(&rho_pt)?;
    let trace_norm: f64 = eigenvalues.iter().map(|e| e.abs()).sum();
    Ok((trace_norm - 1.0).max(0.0) / 2.0)
}
/// Compute partial transpose of a bipartite density matrix
fn partial_transpose(
    rho: &Array2<Complex64>,
    dim_a: usize,
    dim_b: usize,
) -> std::result::Result<Array2<Complex64>, QuantumInfoError> {
    let n = dim_a * dim_b;
    let mut rho_pt = Array2::zeros((n, n));
    for i in 0..dim_a {
        for j in 0..dim_a {
            for k in 0..dim_b {
                for l in 0..dim_b {
                    rho_pt[[j * dim_b + k, i * dim_b + l]] = rho[[i * dim_b + k, j * dim_b + l]];
                }
            }
        }
    }
    Ok(rho_pt)
}
/// Calculate the logarithmic negativity.
///
/// E_N(ρ) = log₂(||ρ^{T_A}||₁)
///
/// # Arguments
/// * `state` - Bipartite quantum state
/// * `dims` - Dimensions of subsystems (dim_A, dim_B)
///
/// # Returns
/// The logarithmic negativity E_N ≥ 0
pub fn logarithmic_negativity(
    state: &QuantumState,
    dims: (usize, usize),
) -> std::result::Result<f64, QuantumInfoError> {
    let rho = state.to_density_matrix()?;
    let (dim_a, dim_b) = dims;
    let rho_pt = partial_transpose(&rho, dim_a, dim_b)?;
    let eigenvalues = compute_eigenvalues_hermitian(&rho_pt)?;
    let trace_norm: f64 = eigenvalues.iter().map(|e| e.abs()).sum();
    Ok(trace_norm.log2().max(0.0))
}
/// Calculate the process fidelity between a quantum channel and a target.
///
/// F_pro(E, F) = F(ρ_E, ρ_F)
/// where ρ_E = Λ_E / d is the normalized Choi matrix.
///
/// For unitary target U:
/// F_pro(E, U) = Tr[S_U† S_E] / d²
///
/// # Arguments
/// * `channel` - Quantum channel (Choi matrix or Kraus operators)
/// * `target` - Target channel or unitary
///
/// # Returns
/// The process fidelity F_pro ∈ [0, 1]
pub fn process_fidelity(
    channel: &QuantumChannel,
    target: &QuantumChannel,
) -> std::result::Result<f64, QuantumInfoError> {
    let choi1 = channel.to_choi()?;
    let choi2 = target.to_choi()?;
    let dim = choi1.nrows();
    let input_dim = (dim as f64).sqrt() as usize;
    let rho1 = &choi1 / Complex64::new(input_dim as f64, 0.0);
    let rho2 = &choi2 / Complex64::new(input_dim as f64, 0.0);
    state_fidelity(&QuantumState::Mixed(rho1), &QuantumState::Mixed(rho2))
}
/// Calculate the average gate fidelity of a noisy quantum channel.
///
/// F_avg(E, U) = (d * F_pro(E, U) + 1) / (d + 1)
///
/// # Arguments
/// * `channel` - Noisy quantum channel
/// * `target` - Target unitary (if None, identity is used)
///
/// # Returns
/// The average gate fidelity F_avg ∈ [0, 1]
pub fn average_gate_fidelity(
    channel: &QuantumChannel,
    target: Option<&QuantumChannel>,
) -> std::result::Result<f64, QuantumInfoError> {
    let dim = channel.input_dim();
    let f_pro = if let Some(t) = target {
        process_fidelity(channel, t)?
    } else {
        let identity = QuantumChannel::identity(dim);
        process_fidelity(channel, &identity)?
    };
    let d = dim as f64;
    Ok((d * f_pro + 1.0) / (d + 1.0))
}
/// Calculate the gate error (infidelity) of a quantum channel.
///
/// r = 1 - F_avg
///
/// # Arguments
/// * `channel` - Noisy quantum channel
/// * `target` - Target unitary
///
/// # Returns
/// The gate error r ∈ [0, 1]
pub fn gate_error(
    channel: &QuantumChannel,
    target: Option<&QuantumChannel>,
) -> std::result::Result<f64, QuantumInfoError> {
    Ok(1.0 - average_gate_fidelity(channel, target)?)
}
/// Calculate the unitarity of a quantum channel.
///
/// u(E) = d/(d-1) * (F_pro(E⊗E, SWAP) - 1/d)
///
/// This measures how well the channel preserves purity.
///
/// # Arguments
/// * `channel` - Quantum channel
///
/// # Returns
/// The unitarity u ∈ [0, 1]
pub fn unitarity(channel: &QuantumChannel) -> std::result::Result<f64, QuantumInfoError> {
    let dim = channel.input_dim();
    let ptm = channel.to_ptm()?;
    let d = dim as f64;
    let d_sq = d * d;
    let mut sum_sq = 0.0;
    for i in 1..ptm.nrows() {
        for j in 1..ptm.ncols() {
            sum_sq += ptm[[i, j]].norm_sqr();
        }
    }
    Ok(sum_sq / (d_sq - 1.0))
}
/// Estimate the diamond norm distance between two channels.
///
/// ||E - F||_◇ = max_{ρ} ||((E-F)⊗I)(ρ)||₁
///
/// This is the complete distinguishability measure for quantum channels.
///
/// # Arguments
/// * `channel1` - First quantum channel
/// * `channel2` - Second quantum channel
///
/// # Returns
/// The diamond norm distance d_◇ ∈ [0, 2]
pub fn diamond_norm_distance(
    channel1: &QuantumChannel,
    channel2: &QuantumChannel,
) -> std::result::Result<f64, QuantumInfoError> {
    let choi1 = channel1.to_choi()?;
    let choi2 = channel2.to_choi()?;
    let diff = &choi1 - &choi2;
    let eigenvalues = compute_eigenvalues_hermitian(&diff)?;
    let trace_norm: f64 = eigenvalues.iter().map(|e| e.abs()).sum();
    let dim = (choi1.nrows() as f64).sqrt();
    Ok((dim * trace_norm).min(2.0))
}
/// Generate Pauli basis for a given dimension (must be power of 2)
pub(super) fn generate_pauli_basis(
    dim: usize,
) -> std::result::Result<Vec<Array2<Complex64>>, QuantumInfoError> {
    if dim == 2 {
        let i = Array2::from_shape_vec(
            (2, 2),
            vec![
                Complex64::new(1.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(1.0, 0.0),
            ],
        )
        .map_err(|e| QuantumInfoError::NumericalError(e.to_string()))?;
        let x = Array2::from_shape_vec(
            (2, 2),
            vec![
                Complex64::new(0.0, 0.0),
                Complex64::new(1.0, 0.0),
                Complex64::new(1.0, 0.0),
                Complex64::new(0.0, 0.0),
            ],
        )
        .map_err(|e| QuantumInfoError::NumericalError(e.to_string()))?;
        let y = Array2::from_shape_vec(
            (2, 2),
            vec![
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, -1.0),
                Complex64::new(0.0, 1.0),
                Complex64::new(0.0, 0.0),
            ],
        )
        .map_err(|e| QuantumInfoError::NumericalError(e.to_string()))?;
        let z = Array2::from_shape_vec(
            (2, 2),
            vec![
                Complex64::new(1.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(-1.0, 0.0),
            ],
        )
        .map_err(|e| QuantumInfoError::NumericalError(e.to_string()))?;
        Ok(vec![i, x, y, z])
    } else {
        let mut basis = Vec::with_capacity(dim * dim);
        for i in 0..dim {
            for j in 0..dim {
                let mut mat = Array2::zeros((dim, dim));
                mat[[i, j]] = Complex64::new(1.0, 0.0);
                basis.push(mat);
            }
        }
        Ok(basis)
    }
}
/// Compute trace of a matrix
pub(super) fn matrix_trace(matrix: &Array2<Complex64>) -> Complex64 {
    (0..matrix.nrows().min(matrix.ncols()))
        .map(|i| matrix[[i, i]])
        .sum()
}
/// Kronecker product of two matrices
pub(super) fn kronecker_product(a: &Array2<Complex64>, b: &Array2<Complex64>) -> Array2<Complex64> {
    let (m, n) = (a.nrows(), a.ncols());
    let (p, q) = (b.nrows(), b.ncols());
    let mut result = Array2::zeros((m * p, n * q));
    for i in 0..m {
        for j in 0..n {
            for k in 0..p {
                for l in 0..q {
                    result[[i * p + k, j * q + l]] = a[[i, j]] * b[[k, l]];
                }
            }
        }
    }
    result
}
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_state_fidelity_pure_states() {
        let psi = Array1::from_vec(vec![
            Complex64::new(1.0 / 2.0_f64.sqrt(), 0.0),
            Complex64::new(1.0 / 2.0_f64.sqrt(), 0.0),
        ]);
        let state1 = QuantumState::Pure(psi.clone());
        let state2 = QuantumState::Pure(psi);
        let fid = state_fidelity(&state1, &state2).expect("Fidelity calculation should succeed");
        assert!(
            (fid - 1.0).abs() < 1e-10,
            "Fidelity of identical states should be 1"
        );
    }
    #[test]
    fn test_state_fidelity_orthogonal_states() {
        let state1 = QuantumState::computational_basis(2, 0);
        let state2 = QuantumState::computational_basis(2, 1);
        let fid = state_fidelity(&state1, &state2).expect("Fidelity calculation should succeed");
        assert!(
            fid.abs() < 1e-10,
            "Fidelity of orthogonal states should be 0"
        );
    }
    #[test]
    fn test_purity_pure_state() {
        let state = QuantumState::computational_basis(4, 0);
        let p = purity(&state).expect("Purity calculation should succeed");
        assert!((p - 1.0).abs() < 1e-10, "Purity of pure state should be 1");
    }
    #[test]
    fn test_purity_maximally_mixed() {
        let state = QuantumState::maximally_mixed(4);
        let p = purity(&state).expect("Purity calculation should succeed");
        assert!(
            (p - 0.25).abs() < 1e-10,
            "Purity of maximally mixed state should be 1/d"
        );
    }
    #[test]
    fn test_von_neumann_entropy_pure_state() {
        let state = QuantumState::computational_basis(2, 0);
        let s = von_neumann_entropy(&state, None).expect("Entropy calculation should succeed");
        assert!(s.abs() < 1e-10, "Entropy of pure state should be 0");
    }
    #[test]
    fn test_von_neumann_entropy_maximally_mixed() {
        let state = QuantumState::maximally_mixed(4);
        let s = von_neumann_entropy(&state, None).expect("Entropy calculation should succeed");
        assert!(
            (s - 2.0).abs() < 0.5,
            "Entropy of maximally mixed state should be log₂(d)"
        );
    }
    #[test]
    fn test_bell_state_creation() {
        let bell = QuantumState::bell_state(0);
        let p = purity(&bell).expect("Purity calculation should succeed");
        assert!((p - 1.0).abs() < 1e-10, "Bell state should be pure");
    }
    #[test]
    fn test_ghz_state_creation() {
        let ghz = QuantumState::ghz_state(3);
        assert_eq!(ghz.dim(), 8, "3-qubit GHZ state should have dimension 8");
        let p = purity(&ghz).expect("Purity calculation should succeed");
        assert!((p - 1.0).abs() < 1e-10, "GHZ state should be pure");
    }
    #[test]
    fn test_w_state_creation() {
        let w = QuantumState::w_state(3);
        assert_eq!(w.dim(), 8, "3-qubit W state should have dimension 8");
        let p = purity(&w).expect("Purity calculation should succeed");
        assert!((p - 1.0).abs() < 1e-10, "W state should be pure");
    }
    #[test]
    fn test_partial_trace() {
        let bell = QuantumState::bell_state(0);
        let rho = bell
            .to_density_matrix()
            .expect("Density matrix conversion should succeed");
        let rho_a = partial_trace(&rho, 2, false).expect("Partial trace should succeed");
        assert_eq!(rho_a.nrows(), 2);
        assert!((rho_a[[0, 0]].re - 0.5).abs() < 1e-10);
        assert!((rho_a[[1, 1]].re - 0.5).abs() < 1e-10);
    }
    #[test]
    fn test_concurrence_separable_state() {
        let state = QuantumState::computational_basis(4, 0);
        let c = concurrence(&state).expect("Concurrence calculation should succeed");
        assert!(c < 1e-10, "Concurrence of separable state should be 0");
    }
    #[test]
    fn test_concurrence_bell_state() {
        let bell = QuantumState::bell_state(0);
        let c = concurrence(&bell).expect("Concurrence calculation should succeed");
        assert!(
            (c - 1.0).abs() < 0.1,
            "Concurrence of Bell state should be ~1"
        );
    }
    #[test]
    fn test_quantum_channel_identity() {
        let channel = QuantumChannel::identity(2);
        let input = QuantumState::computational_basis(2, 0);
        let output = channel
            .apply(&input)
            .expect("Channel application should succeed");
        let fid = state_fidelity(&input, &output).expect("Fidelity calculation should succeed");
        assert!(
            (fid - 1.0).abs() < 1e-10,
            "Identity channel should preserve state"
        );
    }
    #[test]
    fn test_quantum_channel_depolarizing() {
        let channel = QuantumChannel::depolarizing(0.1);
        let input = QuantumState::computational_basis(2, 0);
        let output = channel
            .apply(&input)
            .expect("Channel application should succeed");
        let p = purity(&output).expect("Purity calculation should succeed");
        assert!(p < 1.0, "Depolarizing channel should decrease purity");
        assert!(p > 0.5, "Low error rate should keep purity relatively high");
    }
    #[test]
    fn test_average_gate_fidelity_identity() {
        let channel = QuantumChannel::identity(2);
        let f_avg =
            average_gate_fidelity(&channel, None).expect("Average gate fidelity should succeed");
        assert!(
            (f_avg - 1.0).abs() < 1e-10,
            "Identity channel should have fidelity 1"
        );
    }
    #[test]
    fn test_measurement_data_expectation() {
        let mut counts = std::collections::HashMap::new();
        counts.insert("0".to_string(), 700);
        counts.insert("1".to_string(), 300);
        let data = MeasurementData::new("Z", counts);
        let exp = data.expectation_value();
        assert!((exp - 0.4).abs() < 1e-10);
    }
    #[test]
    fn test_classical_shadow_observable_estimation() {
        let bases = vec!["Z".to_string(), "Z".to_string(), "Z".to_string()];
        let outcomes = vec!["0".to_string(), "0".to_string(), "1".to_string()];
        let shadow = ClassicalShadow::from_measurements(1, bases, outcomes)
            .expect("Shadow creation should succeed");
        let z_exp = shadow
            .estimate_observable("Z")
            .expect("Observable estimation should succeed");
        assert!((z_exp - 1.0).abs() < 1e-10);
    }
    #[test]
    fn test_kronecker_product() {
        let a = Array2::from_shape_vec(
            (2, 2),
            vec![
                Complex64::new(1.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(1.0, 0.0),
            ],
        )
        .expect("Valid shape");
        let b = Array2::from_shape_vec(
            (2, 2),
            vec![
                Complex64::new(0.0, 0.0),
                Complex64::new(1.0, 0.0),
                Complex64::new(1.0, 0.0),
                Complex64::new(0.0, 0.0),
            ],
        )
        .expect("Valid shape");
        let result = kronecker_product(&a, &b);
        assert_eq!(result.nrows(), 4);
        assert_eq!(result.ncols(), 4);
        assert!((result[[0, 1]].re - 1.0).abs() < 1e-10);
        assert!((result[[1, 0]].re - 1.0).abs() < 1e-10);
        assert!((result[[2, 3]].re - 1.0).abs() < 1e-10);
        assert!((result[[3, 2]].re - 1.0).abs() < 1e-10);
    }
}