quantrs2-symengine-pure 0.2.0

Pure Rust symbolic mathematics for quantum computing - a replacement for C++-based symengine
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
//! Symbolic matrix operations.
//!
//! This module provides a proper matrix type where each element is a symbolic expression.
//! This is essential for quantum computing where we work with parameterized gates and
//! symbolic Hamiltonians.

use std::fmt;

use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::Complex64;

use crate::error::{SymEngineError, SymEngineResult};
use crate::expr::Expression;

/// A symbolic matrix where each element is an Expression.
///
/// This is useful for representing parameterized quantum gates,
/// symbolic Hamiltonians, and other matrix expressions.
#[derive(Clone, Debug)]
pub struct SymbolicMatrix {
    /// The matrix elements (row-major order)
    elements: Vec<Expression>,
    /// Number of rows
    rows: usize,
    /// Number of columns
    cols: usize,
}

impl SymbolicMatrix {
    /// Create a new symbolic matrix from a 2D array of expressions.
    ///
    /// # Errors
    /// Returns error if the input is empty
    pub fn new(elements: Vec<Vec<Expression>>) -> SymEngineResult<Self> {
        if elements.is_empty() {
            return Err(SymEngineError::dimension("Matrix cannot be empty"));
        }

        let rows = elements.len();
        let cols = elements[0].len();

        // Verify all rows have the same length
        for (i, row) in elements.iter().enumerate() {
            if row.len() != cols {
                return Err(SymEngineError::dimension(format!(
                    "Row {i} has {} columns, expected {cols}",
                    row.len()
                )));
            }
        }

        let flat: Vec<Expression> = elements.into_iter().flatten().collect();

        Ok(Self {
            elements: flat,
            rows,
            cols,
        })
    }

    /// Create a matrix from a flat vector with specified dimensions.
    ///
    /// # Errors
    /// Returns error if the dimensions don't match the vector length
    pub fn from_flat(elements: Vec<Expression>, rows: usize, cols: usize) -> SymEngineResult<Self> {
        if elements.len() != rows * cols {
            return Err(SymEngineError::dimension(format!(
                "Expected {} elements for {}x{} matrix, got {}",
                rows * cols,
                rows,
                cols,
                elements.len()
            )));
        }

        Ok(Self {
            elements,
            rows,
            cols,
        })
    }

    /// Create a zero matrix.
    #[must_use]
    pub fn zeros(rows: usize, cols: usize) -> Self {
        Self {
            elements: vec![Expression::zero(); rows * cols],
            rows,
            cols,
        }
    }

    /// Create an identity matrix.
    #[must_use]
    pub fn identity(n: usize) -> Self {
        let mut elements = vec![Expression::zero(); n * n];
        for i in 0..n {
            elements[i * n + i] = Expression::one();
        }
        Self {
            elements,
            rows: n,
            cols: n,
        }
    }

    /// Create a diagonal matrix from a vector of diagonal elements.
    #[must_use]
    pub fn diagonal(diag: Vec<Expression>) -> Self {
        let n = diag.len();
        let mut elements = vec![Expression::zero(); n * n];
        for (i, d) in diag.into_iter().enumerate() {
            elements[i * n + i] = d;
        }
        Self {
            elements,
            rows: n,
            cols: n,
        }
    }

    /// Create a matrix from a numeric Array2.
    #[must_use]
    pub fn from_array(arr: &Array2<f64>) -> Self {
        let rows = arr.nrows();
        let cols = arr.ncols();
        let elements: Vec<Expression> = arr
            .iter()
            .map(|&v| Expression::float_unchecked(v))
            .collect();
        Self {
            elements,
            rows,
            cols,
        }
    }

    /// Create a matrix from a complex Array2.
    #[must_use]
    pub fn from_complex_array(arr: &Array2<Complex64>) -> Self {
        let rows = arr.nrows();
        let cols = arr.ncols();
        let elements: Vec<Expression> =
            arr.iter().map(|&c| Expression::from_complex64(c)).collect();
        Self {
            elements,
            rows,
            cols,
        }
    }

    // =========================================================================
    // Accessors
    // =========================================================================

    /// Get the number of rows.
    #[must_use]
    pub const fn nrows(&self) -> usize {
        self.rows
    }

    /// Get the number of columns.
    #[must_use]
    pub const fn ncols(&self) -> usize {
        self.cols
    }

    /// Get the dimensions as (rows, cols).
    #[must_use]
    pub const fn shape(&self) -> (usize, usize) {
        (self.rows, self.cols)
    }

    /// Check if the matrix is square.
    #[must_use]
    pub const fn is_square(&self) -> bool {
        self.rows == self.cols
    }

    /// Get element at (i, j).
    ///
    /// # Panics
    /// Panics if indices are out of bounds.
    #[must_use]
    pub fn get(&self, i: usize, j: usize) -> &Expression {
        assert!(i < self.rows && j < self.cols, "Index out of bounds");
        &self.elements[i * self.cols + j]
    }

    /// Get mutable reference to element at (i, j).
    ///
    /// # Panics
    /// Panics if indices are out of bounds.
    pub fn get_mut(&mut self, i: usize, j: usize) -> &mut Expression {
        assert!(i < self.rows && j < self.cols, "Index out of bounds");
        &mut self.elements[i * self.cols + j]
    }

    /// Set element at (i, j).
    ///
    /// # Panics
    /// Panics if indices are out of bounds.
    pub fn set(&mut self, i: usize, j: usize, value: Expression) {
        assert!(i < self.rows && j < self.cols, "Index out of bounds");
        self.elements[i * self.cols + j] = value;
    }

    /// Get a row as a vector of expressions.
    #[must_use]
    pub fn row(&self, i: usize) -> Vec<Expression> {
        assert!(i < self.rows, "Row index out of bounds");
        let start = i * self.cols;
        self.elements[start..start + self.cols].to_vec()
    }

    /// Get a column as a vector of expressions.
    #[must_use]
    pub fn col(&self, j: usize) -> Vec<Expression> {
        assert!(j < self.cols, "Column index out of bounds");
        (0..self.rows).map(|i| self.get(i, j).clone()).collect()
    }

    // =========================================================================
    // Matrix Operations
    // =========================================================================

    /// Matrix transpose.
    #[must_use]
    pub fn transpose(&self) -> Self {
        let mut elements = Vec::with_capacity(self.rows * self.cols);
        for j in 0..self.cols {
            for i in 0..self.rows {
                elements.push(self.get(i, j).clone());
            }
        }
        Self {
            elements,
            rows: self.cols,
            cols: self.rows,
        }
    }

    /// Complex conjugate of all elements.
    #[must_use]
    pub fn conjugate(&self) -> Self {
        Self {
            elements: self.elements.iter().map(Expression::conjugate).collect(),
            rows: self.rows,
            cols: self.cols,
        }
    }

    /// Hermitian conjugate (conjugate transpose).
    #[must_use]
    pub fn dagger(&self) -> Self {
        self.transpose().conjugate()
    }

    /// Matrix addition.
    ///
    /// # Errors
    /// Returns error if dimensions don't match.
    pub fn add(&self, other: &Self) -> SymEngineResult<Self> {
        if self.rows != other.rows || self.cols != other.cols {
            return Err(SymEngineError::dimension(format!(
                "Cannot add {}x{} matrix with {}x{} matrix",
                self.rows, self.cols, other.rows, other.cols
            )));
        }

        let elements: Vec<Expression> = self
            .elements
            .iter()
            .zip(other.elements.iter())
            .map(|(a, b)| a.clone() + b.clone())
            .collect();

        Ok(Self {
            elements,
            rows: self.rows,
            cols: self.cols,
        })
    }

    /// Matrix subtraction.
    ///
    /// # Errors
    /// Returns error if dimensions don't match.
    pub fn sub(&self, other: &Self) -> SymEngineResult<Self> {
        if self.rows != other.rows || self.cols != other.cols {
            return Err(SymEngineError::dimension(format!(
                "Cannot subtract {}x{} matrix from {}x{} matrix",
                other.rows, other.cols, self.rows, self.cols
            )));
        }

        let elements: Vec<Expression> = self
            .elements
            .iter()
            .zip(other.elements.iter())
            .map(|(a, b)| a.clone() - b.clone())
            .collect();

        Ok(Self {
            elements,
            rows: self.rows,
            cols: self.cols,
        })
    }

    /// Matrix multiplication.
    ///
    /// # Errors
    /// Returns error if inner dimensions don't match.
    pub fn matmul(&self, other: &Self) -> SymEngineResult<Self> {
        if self.cols != other.rows {
            return Err(SymEngineError::dimension(format!(
                "Cannot multiply {}x{} matrix with {}x{} matrix",
                self.rows, self.cols, other.rows, other.cols
            )));
        }

        let mut elements = Vec::with_capacity(self.rows * other.cols);

        for i in 0..self.rows {
            for j in 0..other.cols {
                let mut sum = Expression::zero();
                for k in 0..self.cols {
                    sum = sum + self.get(i, k).clone() * other.get(k, j).clone();
                }
                elements.push(sum);
            }
        }

        Ok(Self {
            elements,
            rows: self.rows,
            cols: other.cols,
        })
    }

    /// Scalar multiplication.
    #[must_use]
    pub fn scale(&self, scalar: &Expression) -> Self {
        Self {
            elements: self
                .elements
                .iter()
                .map(|e| e.clone() * scalar.clone())
                .collect(),
            rows: self.rows,
            cols: self.cols,
        }
    }

    /// Kronecker (tensor) product.
    #[must_use]
    pub fn kron(&self, other: &Self) -> Self {
        let new_rows = self.rows * other.rows;
        let new_cols = self.cols * other.cols;
        let mut elements = Vec::with_capacity(new_rows * new_cols);

        for i1 in 0..self.rows {
            for i2 in 0..other.rows {
                for j1 in 0..self.cols {
                    for j2 in 0..other.cols {
                        let a = self.get(i1, j1).clone();
                        let b = other.get(i2, j2).clone();
                        elements.push(a * b);
                    }
                }
            }
        }

        Self {
            elements,
            rows: new_rows,
            cols: new_cols,
        }
    }

    /// Matrix trace (sum of diagonal elements).
    ///
    /// # Errors
    /// Returns error if matrix is not square.
    pub fn trace(&self) -> SymEngineResult<Expression> {
        if !self.is_square() {
            return Err(SymEngineError::dimension(
                "Trace is only defined for square matrices",
            ));
        }

        let mut sum = Expression::zero();
        for i in 0..self.rows {
            sum = sum + self.get(i, i).clone();
        }
        Ok(sum)
    }

    /// Commutator [A, B] = AB - BA.
    ///
    /// # Errors
    /// Returns error if dimensions don't match or matrices are not square.
    pub fn commutator(&self, other: &Self) -> SymEngineResult<Self> {
        let ab = self.matmul(other)?;
        let ba = other.matmul(self)?;
        ab.sub(&ba)
    }

    /// Anticommutator {A, B} = AB + BA.
    ///
    /// # Errors
    /// Returns error if dimensions don't match.
    pub fn anticommutator(&self, other: &Self) -> SymEngineResult<Self> {
        let ab = self.matmul(other)?;
        let ba = other.matmul(self)?;
        ab.add(&ba)
    }

    // =========================================================================
    // Simplification
    // =========================================================================

    /// Simplify all matrix elements.
    #[must_use]
    pub fn simplify(&self) -> Self {
        Self {
            elements: self.elements.iter().map(Expression::simplify).collect(),
            rows: self.rows,
            cols: self.cols,
        }
    }

    /// Expand all matrix elements.
    #[must_use]
    pub fn expand(&self) -> Self {
        Self {
            elements: self.elements.iter().map(Expression::expand).collect(),
            rows: self.rows,
            cols: self.cols,
        }
    }

    // =========================================================================
    // Evaluation
    // =========================================================================

    /// Evaluate all matrix elements with given variable values.
    ///
    /// # Errors
    /// Returns error if any element evaluation fails.
    pub fn eval(
        &self,
        values: &std::collections::HashMap<String, f64>,
    ) -> SymEngineResult<Array2<f64>> {
        let mut result = Array2::zeros((self.rows, self.cols));
        for i in 0..self.rows {
            for j in 0..self.cols {
                result[[i, j]] = self.get(i, j).eval(values)?;
            }
        }
        Ok(result)
    }

    /// Substitute a variable with an expression in all elements.
    #[must_use]
    pub fn substitute(&self, var: &Expression, value: &Expression) -> Self {
        Self {
            elements: self
                .elements
                .iter()
                .map(|e| e.substitute(var, value))
                .collect(),
            rows: self.rows,
            cols: self.cols,
        }
    }

    // =========================================================================
    // Differentiation
    // =========================================================================

    /// Compute the derivative of all elements with respect to a variable.
    #[must_use]
    pub fn diff(&self, var: &Expression) -> Self {
        Self {
            elements: self.elements.iter().map(|e| e.diff(var)).collect(),
            rows: self.rows,
            cols: self.cols,
        }
    }
}

impl fmt::Display for SymbolicMatrix {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "[")?;
        for i in 0..self.rows {
            write!(f, "  [")?;
            for j in 0..self.cols {
                if j > 0 {
                    write!(f, ", ")?;
                }
                write!(f, "{}", self.get(i, j))?;
            }
            writeln!(f, "]")?;
        }
        write!(f, "]")
    }
}

impl std::ops::Index<(usize, usize)> for SymbolicMatrix {
    type Output = Expression;

    fn index(&self, index: (usize, usize)) -> &Self::Output {
        self.get(index.0, index.1)
    }
}

impl std::ops::IndexMut<(usize, usize)> for SymbolicMatrix {
    fn index_mut(&mut self, index: (usize, usize)) -> &mut Self::Output {
        self.get_mut(index.0, index.1)
    }
}

// =========================================================================
// Quantum Gate Matrices
// =========================================================================

/// Create the Pauli X gate matrix.
#[must_use]
pub fn pauli_x() -> SymbolicMatrix {
    SymbolicMatrix::from_flat(
        vec![
            Expression::zero(),
            Expression::one(),
            Expression::one(),
            Expression::zero(),
        ],
        2,
        2,
    )
    .expect("valid 2x2 matrix")
}

/// Create the Pauli Y gate matrix.
#[must_use]
pub fn pauli_y() -> SymbolicMatrix {
    let i = Expression::i();
    SymbolicMatrix::from_flat(
        vec![Expression::zero(), i.clone().neg(), i, Expression::zero()],
        2,
        2,
    )
    .expect("valid 2x2 matrix")
}

/// Create the Pauli Z gate matrix.
#[must_use]
pub fn pauli_z() -> SymbolicMatrix {
    SymbolicMatrix::from_flat(
        vec![
            Expression::one(),
            Expression::zero(),
            Expression::zero(),
            Expression::one().neg(),
        ],
        2,
        2,
    )
    .expect("valid 2x2 matrix")
}

/// Create the Hadamard gate matrix.
#[must_use]
pub fn hadamard() -> SymbolicMatrix {
    let sqrt2_inv = Expression::one() / crate::ops::trig::sqrt(&Expression::int(2));
    SymbolicMatrix::from_flat(
        vec![
            sqrt2_inv.clone(),
            sqrt2_inv.clone(),
            sqrt2_inv.clone(),
            sqrt2_inv.neg(),
        ],
        2,
        2,
    )
    .expect("valid 2x2 matrix")
}

/// Create the S (phase) gate matrix.
#[must_use]
pub fn s_gate() -> SymbolicMatrix {
    SymbolicMatrix::from_flat(
        vec![
            Expression::one(),
            Expression::zero(),
            Expression::zero(),
            Expression::i(),
        ],
        2,
        2,
    )
    .expect("valid 2x2 matrix")
}

/// Create the T gate matrix.
#[must_use]
pub fn t_gate() -> SymbolicMatrix {
    let exp_i_pi_4 =
        crate::ops::trig::exp(&(Expression::i() * Expression::pi() / Expression::int(4)));
    SymbolicMatrix::from_flat(
        vec![
            Expression::one(),
            Expression::zero(),
            Expression::zero(),
            exp_i_pi_4,
        ],
        2,
        2,
    )
    .expect("valid 2x2 matrix")
}

/// Create a rotation gate Rx(θ) around the X axis.
#[must_use]
pub fn rx(theta: &Expression) -> SymbolicMatrix {
    let half = Expression::float_unchecked(0.5);
    let half_theta = theta.clone() * half;
    let cos_half = crate::ops::trig::cos(&half_theta);
    let sin_half = crate::ops::trig::sin(&half_theta);
    let i = Expression::i();

    SymbolicMatrix::from_flat(
        vec![
            cos_half.clone(),
            i.clone().neg() * sin_half.clone(),
            i.neg() * sin_half,
            cos_half,
        ],
        2,
        2,
    )
    .expect("valid 2x2 matrix")
}

/// Create a rotation gate Ry(θ) around the Y axis.
#[must_use]
pub fn ry(theta: &Expression) -> SymbolicMatrix {
    let half = Expression::float_unchecked(0.5);
    let half_theta = theta.clone() * half;
    let cos_half = crate::ops::trig::cos(&half_theta);
    let sin_half = crate::ops::trig::sin(&half_theta);

    SymbolicMatrix::from_flat(
        vec![cos_half.clone(), sin_half.clone().neg(), sin_half, cos_half],
        2,
        2,
    )
    .expect("valid 2x2 matrix")
}

/// Create a rotation gate Rz(θ) around the Z axis.
#[must_use]
pub fn rz(theta: &Expression) -> SymbolicMatrix {
    let half = Expression::float_unchecked(0.5);
    let i = Expression::i();
    let half_theta = theta.clone() * half;
    let exp_neg = crate::ops::trig::exp(&(i.neg() * half_theta.clone()));
    let exp_pos = crate::ops::trig::exp(&(Expression::i() * half_theta));

    SymbolicMatrix::from_flat(
        vec![exp_neg, Expression::zero(), Expression::zero(), exp_pos],
        2,
        2,
    )
    .expect("valid 2x2 matrix")
}

/// Create the CNOT (CX) gate matrix.
#[must_use]
pub fn cnot() -> SymbolicMatrix {
    SymbolicMatrix::from_flat(
        vec![
            Expression::one(),
            Expression::zero(),
            Expression::zero(),
            Expression::zero(),
            Expression::zero(),
            Expression::one(),
            Expression::zero(),
            Expression::zero(),
            Expression::zero(),
            Expression::zero(),
            Expression::zero(),
            Expression::one(),
            Expression::zero(),
            Expression::zero(),
            Expression::one(),
            Expression::zero(),
        ],
        4,
        4,
    )
    .expect("valid 4x4 matrix")
}

/// Create the SWAP gate matrix.
#[must_use]
pub fn swap() -> SymbolicMatrix {
    SymbolicMatrix::from_flat(
        vec![
            Expression::one(),
            Expression::zero(),
            Expression::zero(),
            Expression::zero(),
            Expression::zero(),
            Expression::zero(),
            Expression::one(),
            Expression::zero(),
            Expression::zero(),
            Expression::one(),
            Expression::zero(),
            Expression::zero(),
            Expression::zero(),
            Expression::zero(),
            Expression::zero(),
            Expression::one(),
        ],
        4,
        4,
    )
    .expect("valid 4x4 matrix")
}

/// Create a controlled-U gate matrix.
#[must_use]
pub fn controlled(u: &SymbolicMatrix) -> SymbolicMatrix {
    assert!(u.is_square() && u.nrows() == 2, "U must be a 2x2 matrix");

    let n = 4;
    let mut elements = vec![Expression::zero(); n * n];

    // Top-left 2x2 block is identity (control = |0⟩)
    elements[0] = Expression::one();
    elements[5] = Expression::one();

    // Bottom-right 2x2 block is U (control = |1⟩)
    elements[10] = u.get(0, 0).clone();
    elements[11] = u.get(0, 1).clone();
    elements[14] = u.get(1, 0).clone();
    elements[15] = u.get(1, 1).clone();

    SymbolicMatrix::from_flat(elements, n, n).expect("valid 4x4 matrix")
}

#[cfg(test)]
#[allow(clippy::redundant_clone)]
mod tests {
    use super::*;
    use std::collections::HashMap;

    #[test]
    fn test_matrix_creation() {
        let m = SymbolicMatrix::identity(2);
        assert_eq!(m.nrows(), 2);
        assert_eq!(m.ncols(), 2);
        assert!(m.get(0, 0).is_one());
        assert!(m.get(0, 1).is_zero());
        assert!(m.get(1, 0).is_zero());
        assert!(m.get(1, 1).is_one());
    }

    #[test]
    fn test_matrix_transpose() {
        let x = Expression::symbol("x");
        let y = Expression::symbol("y");
        let z = Expression::symbol("z");
        let w = Expression::symbol("w");

        let m = SymbolicMatrix::new(vec![vec![x.clone(), y.clone()], vec![z.clone(), w.clone()]])
            .expect("valid matrix");

        let mt = m.transpose();
        assert_eq!(mt.get(0, 0).as_symbol(), Some("x"));
        assert_eq!(mt.get(0, 1).as_symbol(), Some("z"));
        assert_eq!(mt.get(1, 0).as_symbol(), Some("y"));
        assert_eq!(mt.get(1, 1).as_symbol(), Some("w"));
    }

    #[test]
    fn test_matrix_multiplication() {
        // Test with identity
        let i = SymbolicMatrix::identity(2);
        let x = Expression::symbol("x");
        let m = SymbolicMatrix::new(vec![
            vec![x.clone(), Expression::zero()],
            vec![Expression::zero(), x.clone()],
        ])
        .expect("valid matrix");

        let result = i.matmul(&m).expect("valid matmul");

        // I * M = M - verify by evaluation
        let mut values = HashMap::new();
        values.insert("x".to_string(), 5.0);

        // result[0,0] should evaluate to x = 5.0
        let r00 = result.get(0, 0).eval(&values).expect("valid eval");
        assert!((r00 - 5.0).abs() < 1e-10);

        // result[0,1] should evaluate to 0
        let r01 = result.get(0, 1).eval(&values).expect("valid eval");
        assert!(r01.abs() < 1e-10);

        // result[1,1] should evaluate to x = 5.0
        let r11 = result.get(1, 1).eval(&values).expect("valid eval");
        assert!((r11 - 5.0).abs() < 1e-10);
    }

    #[test]
    fn test_kronecker_product() {
        let x = pauli_x();
        let z = pauli_z();

        let xz = x.kron(&z);
        assert_eq!(xz.nrows(), 4);
        assert_eq!(xz.ncols(), 4);
    }

    #[test]
    fn test_trace() {
        let theta = Expression::symbol("theta");
        let m = SymbolicMatrix::diagonal(vec![theta.clone(), theta.clone()]);
        let tr = m.trace().expect("valid trace");

        // tr(diag(θ, θ)) = 2θ
        let mut values = HashMap::new();
        values.insert("theta".to_string(), 3.0);
        let result = tr.eval(&values).expect("valid eval");
        assert!((result - 6.0).abs() < 1e-10);
    }

    #[test]
    fn test_rotation_gates() {
        let theta = Expression::symbol("theta");

        // Rx(θ) should be unitary: Rx†Rx = I
        let rx_gate = rx(&theta);
        let rx_dag = rx_gate.dagger();

        // Just verify structure - full verification would require complex eval
        assert_eq!(rx_gate.nrows(), 2);
        assert_eq!(rx_dag.nrows(), 2);
    }

    #[test]
    fn test_pauli_commutation() {
        let x = pauli_x();
        let y = pauli_y();

        // [X, Y] = 2iZ
        let comm = x.commutator(&y).expect("valid commutator");
        assert_eq!(comm.nrows(), 2);
    }

    #[test]
    fn test_matrix_diff() {
        let theta = Expression::symbol("theta");
        let m = SymbolicMatrix::diagonal(vec![
            crate::ops::trig::sin(&theta),
            crate::ops::trig::cos(&theta),
        ]);

        let dm = m.diff(&theta);

        // d/dθ sin(θ) = cos(θ), d/dθ cos(θ) = -sin(θ)
        let mut values = HashMap::new();
        values.insert("theta".to_string(), 0.0);

        // At θ=0: d/dθ sin(θ)|θ=0 = cos(0) = 1
        let result = dm.eval(&values).expect("valid eval");
        assert!((result[[0, 0]] - 1.0).abs() < 1e-10);
        // d/dθ cos(θ)|θ=0 = -sin(0) = 0
        assert!(result[[1, 1]].abs() < 1e-10);
    }
}