quantrs2-ml 0.1.3

Quantum Machine Learning module for QuantRS2
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
//! TorchQuantum Tensor Network Backend
//!
//! This module provides tensor network simulation backend for TorchQuantum circuits,
//! enabling efficient simulation of large circuits with limited entanglement.
//!
//! ## Key Features
//!
//! - **MPS Backend**: Matrix Product State representation for 1D circuits
//! - **PEPS Backend**: Projected Entangled Pair States for 2D circuits
//! - **Automatic Bond Dimension Management**: Adaptive truncation based on fidelity
//! - **Gradient Support**: Tensor network compatible gradient computation

use crate::error::{MLError, Result};
use scirs2_core::ndarray::{Array1, Array2, Array3, Axis};
use scirs2_core::Complex64;
use std::collections::HashMap;

use super::{CType, TQDevice, TQModule, TQParameter};

// ============================================================================
// Tensor Network Configuration
// ============================================================================

/// Configuration for tensor network simulation
#[derive(Debug, Clone)]
pub struct TensorNetworkConfig {
    /// Maximum bond dimension for truncation
    pub max_bond_dim: usize,
    /// Truncation threshold (singular values below this are discarded)
    pub truncation_threshold: f64,
    /// Whether to use canonical form
    pub use_canonical_form: bool,
    /// Compression method
    pub compression: CompressionMethod,
}

impl Default for TensorNetworkConfig {
    fn default() -> Self {
        Self {
            max_bond_dim: 64,
            truncation_threshold: 1e-12,
            use_canonical_form: true,
            compression: CompressionMethod::SVD,
        }
    }
}

/// Compression methods for tensor network
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompressionMethod {
    /// Singular Value Decomposition
    SVD,
    /// QR decomposition
    QR,
    /// Variational compression
    Variational,
}

// ============================================================================
// MPS Tensor (Single Site)
// ============================================================================

/// Single site tensor in MPS representation
///
/// Shape: (bond_left, physical_dim, bond_right)
#[derive(Debug, Clone)]
pub struct MPSTensor {
    /// Tensor data: shape (bond_left, physical_dim, bond_right)
    pub data: Array3<CType>,
    /// Site index
    pub site: usize,
}

impl MPSTensor {
    /// Create new MPS tensor
    pub fn new(data: Array3<CType>, site: usize) -> Self {
        Self { data, site }
    }

    /// Get bond dimensions
    pub fn bond_dims(&self) -> (usize, usize) {
        let shape = self.data.shape();
        (shape[0], shape[2])
    }

    /// Get physical dimension
    pub fn physical_dim(&self) -> usize {
        self.data.shape()[1]
    }

    /// Contract with another tensor (right multiplication)
    pub fn contract_right(&self, other: &MPSTensor) -> Array3<CType> {
        let (d_left, phys_a, d_mid) = (
            self.data.shape()[0],
            self.data.shape()[1],
            self.data.shape()[2],
        );
        let (_d_mid2, phys_b, d_right) = (
            other.data.shape()[0],
            other.data.shape()[1],
            other.data.shape()[2],
        );

        // Contract over the shared bond dimension
        let mut result = Array3::<CType>::zeros((d_left, phys_a * phys_b, d_right));

        for i in 0..d_left {
            for j in 0..phys_a {
                for k in 0..d_mid {
                    for l in 0..phys_b {
                        for m in 0..d_right {
                            let combined_phys = j * phys_b + l;
                            result[[i, combined_phys, m]] +=
                                self.data[[i, j, k]] * other.data[[k, l, m]];
                        }
                    }
                }
            }
        }

        result
    }
}

// ============================================================================
// Matrix Product State
// ============================================================================

/// Matrix Product State representation of quantum state
#[derive(Debug, Clone)]
pub struct MatrixProductState {
    /// MPS tensors for each site
    pub tensors: Vec<MPSTensor>,
    /// Number of qubits
    pub n_qubits: usize,
    /// Configuration
    pub config: TensorNetworkConfig,
    /// Normalization factor
    pub norm: f64,
}

impl MatrixProductState {
    /// Create MPS from computational basis state (e.g., |00...0>)
    pub fn from_computational_basis(n_qubits: usize, state: usize) -> Self {
        let config = TensorNetworkConfig::default();
        let mut tensors = Vec::with_capacity(n_qubits);

        for site in 0..n_qubits {
            // Each tensor is (1, 2, 1) for product states
            let mut data = Array3::<CType>::zeros((1, 2, 1));
            let bit = (state >> (n_qubits - 1 - site)) & 1;
            data[[0, bit, 0]] = Complex64::new(1.0, 0.0);
            tensors.push(MPSTensor::new(data, site));
        }

        Self {
            tensors,
            n_qubits,
            config,
            norm: 1.0,
        }
    }

    /// Create MPS from TQDevice state
    pub fn from_tq_device(qdev: &TQDevice) -> Result<Self> {
        // Get state vector
        let states = qdev.get_states_1d();
        let state_vec: Vec<CType> = states.row(0).iter().cloned().collect();

        Self::from_state_vector(&state_vec, qdev.n_wires)
    }

    /// Create MPS from state vector using SVD decomposition
    pub fn from_state_vector(state_vec: &[CType], n_qubits: usize) -> Result<Self> {
        let config = TensorNetworkConfig::default();
        let dim = 1 << n_qubits;

        if state_vec.len() != dim {
            return Err(MLError::InvalidConfiguration(format!(
                "State vector size {} doesn't match 2^{} = {}",
                state_vec.len(),
                n_qubits,
                dim
            )));
        }

        // For simplicity, use direct tensor construction for small systems
        // For larger systems, use SVD decomposition
        let mut tensors = Vec::with_capacity(n_qubits);

        if n_qubits <= 4 {
            // Direct construction for small systems
            for site in 0..n_qubits {
                let bond_left = 1.min(1 << site);
                let bond_right = 1.min(1 << (n_qubits - site - 1));
                let mut data = Array3::<CType>::zeros((bond_left, 2, bond_right));

                // Fill tensor based on state amplitudes
                for idx in 0..dim {
                    let bit = (idx >> (n_qubits - 1 - site)) & 1;
                    let left_idx = (idx >> (n_qubits - site)) % bond_left;
                    let right_idx = idx % bond_right;
                    data[[left_idx, bit, right_idx]] += state_vec[idx];
                }

                tensors.push(MPSTensor::new(data, site));
            }
        } else {
            // SVD-based construction for larger systems
            let mut remaining = Array2::<CType>::from_shape_vec((1, dim), state_vec.to_vec())
                .map_err(|e| MLError::InvalidConfiguration(e.to_string()))?;

            for site in 0..n_qubits {
                let rows = remaining.nrows();
                let cols = remaining.ncols();
                let new_cols = cols / 2;

                // Clone and reshape to separate the physical index
                let reshaped = remaining
                    .clone()
                    .into_shape_with_order((rows * 2, new_cols))
                    .map_err(|e| MLError::InvalidConfiguration(e.to_string()))?;

                // For last site, no SVD needed
                if site == n_qubits - 1 {
                    let mut data = Array3::<CType>::zeros((rows, 2, 1));
                    for i in 0..rows {
                        for j in 0..2 {
                            data[[i, j, 0]] = reshaped[[i * 2 + j, 0]];
                        }
                    }
                    tensors.push(MPSTensor::new(data, site));
                } else {
                    // Simple truncation (for production, use proper SVD)
                    let bond_dim = (rows * 2).min(config.max_bond_dim).min(new_cols);
                    let mut data = Array3::<CType>::zeros((rows, 2, bond_dim));

                    for i in 0..rows {
                        for j in 0..2 {
                            for k in 0..bond_dim {
                                if i * 2 + j < rows * 2 && k < new_cols {
                                    data[[i, j, k]] = reshaped[[i * 2 + j, k]];
                                }
                            }
                        }
                    }

                    tensors.push(MPSTensor::new(data, site));

                    // Prepare for next iteration
                    remaining = Array2::<CType>::zeros((bond_dim, new_cols));
                    for i in 0..bond_dim.min(rows * 2) {
                        for j in 0..new_cols {
                            remaining[[i.min(bond_dim - 1), j]] = reshaped[[i, j]];
                        }
                    }
                }
            }
        }

        Ok(Self {
            tensors,
            n_qubits,
            config,
            norm: 1.0,
        })
    }

    /// Apply single-qubit gate to MPS
    pub fn apply_single_qubit_gate(&mut self, site: usize, gate: &Array2<CType>) -> Result<()> {
        if site >= self.n_qubits {
            return Err(MLError::InvalidConfiguration(format!(
                "Site {} out of range for {} qubits",
                site, self.n_qubits
            )));
        }

        let tensor = &mut self.tensors[site];
        let (d_left, _phys, d_right) = (
            tensor.data.shape()[0],
            tensor.data.shape()[1],
            tensor.data.shape()[2],
        );

        let mut new_data = Array3::<CType>::zeros((d_left, 2, d_right));

        for i in 0..d_left {
            for k in 0..d_right {
                let old_0 = tensor.data[[i, 0, k]];
                let old_1 = tensor.data[[i, 1, k]];
                new_data[[i, 0, k]] = gate[[0, 0]] * old_0 + gate[[0, 1]] * old_1;
                new_data[[i, 1, k]] = gate[[1, 0]] * old_0 + gate[[1, 1]] * old_1;
            }
        }

        tensor.data = new_data;
        Ok(())
    }

    /// Apply two-qubit gate to MPS (with truncation)
    pub fn apply_two_qubit_gate(
        &mut self,
        site1: usize,
        site2: usize,
        gate: &Array2<CType>,
    ) -> Result<()> {
        // Ensure sites are adjacent for efficient application
        if site1.abs_diff(site2) != 1 {
            return Err(MLError::InvalidConfiguration(
                "Two-qubit gates on non-adjacent sites require SWAP operations".to_string(),
            ));
        }

        let (left_site, right_site) = if site1 < site2 {
            (site1, site2)
        } else {
            (site2, site1)
        };

        // Contract the two tensors
        let left_tensor = &self.tensors[left_site];
        let right_tensor = &self.tensors[right_site];

        let d_left = left_tensor.data.shape()[0];
        let d_mid = left_tensor.data.shape()[2];
        let d_right = right_tensor.data.shape()[2];

        // Contract and apply gate
        let mut contracted = Array3::<CType>::zeros((d_left, 4, d_right));

        for i in 0..d_left {
            for k in 0..d_mid {
                for m in 0..d_right {
                    for j1 in 0..2 {
                        for j2 in 0..2 {
                            let combined = j1 * 2 + j2;
                            contracted[[i, combined, m]] +=
                                left_tensor.data[[i, j1, k]] * right_tensor.data[[k, j2, m]];
                        }
                    }
                }
            }
        }

        // Apply gate
        let mut gated = Array3::<CType>::zeros((d_left, 4, d_right));
        for i in 0..d_left {
            for m in 0..d_right {
                for out_idx in 0..4 {
                    for in_idx in 0..4 {
                        gated[[i, out_idx, m]] +=
                            gate[[out_idx, in_idx]] * contracted[[i, in_idx, m]];
                    }
                }
            }
        }

        // Split back into two tensors (simplified truncation)
        let new_bond = d_mid.min(self.config.max_bond_dim);

        let mut new_left = Array3::<CType>::zeros((d_left, 2, new_bond));
        let mut new_right = Array3::<CType>::zeros((new_bond, 2, d_right));

        // Simple split (for production, use SVD)
        for i in 0..d_left {
            for j1 in 0..2 {
                for k in 0..new_bond {
                    for j2 in 0..2 {
                        for m in 0..d_right {
                            let combined = j1 * 2 + j2;
                            // Distribute amplitude
                            new_left[[i, j1, k]] += gated[[i, combined, m]]
                                * Complex64::new(1.0 / (new_bond * d_right) as f64, 0.0);
                            new_right[[k, j2, m]] += gated[[i, combined, m]]
                                * Complex64::new(1.0 / (d_left * 2) as f64, 0.0);
                        }
                    }
                }
            }
        }

        self.tensors[left_site] = MPSTensor::new(new_left, left_site);
        self.tensors[right_site] = MPSTensor::new(new_right, right_site);

        Ok(())
    }

    /// Get the state vector representation
    pub fn to_state_vector(&self) -> Result<Vec<CType>> {
        let dim = 1 << self.n_qubits;
        let mut state = vec![Complex64::new(0.0, 0.0); dim];

        // Contract all tensors
        for idx in 0..dim {
            let mut amp = Complex64::new(1.0, 0.0);

            for site in 0..self.n_qubits {
                let bit = (idx >> (self.n_qubits - 1 - site)) & 1;
                // For product states, just multiply the diagonal elements
                amp *= self.tensors[site].data[[0, bit, 0]];
            }

            state[idx] = amp;
        }

        Ok(state)
    }

    /// Compute overlap with another MPS
    pub fn overlap(&self, other: &MatrixProductState) -> Result<CType> {
        if self.n_qubits != other.n_qubits {
            return Err(MLError::InvalidConfiguration(
                "MPS qubit counts don't match".to_string(),
            ));
        }

        // Contract from left to right
        let mut transfer = Array2::<CType>::eye(1);

        for site in 0..self.n_qubits {
            let t1 = &self.tensors[site];
            let t2 = &other.tensors[site];

            let d1_left = t1.data.shape()[0];
            let d1_right = t1.data.shape()[2];
            let d2_left = t2.data.shape()[0];
            let d2_right = t2.data.shape()[2];

            let mut new_transfer = Array2::<CType>::zeros((d1_right, d2_right));

            for i1 in 0..d1_left {
                for i2 in 0..d2_left {
                    for j in 0..2 {
                        for k1 in 0..d1_right {
                            for k2 in 0..d2_right {
                                new_transfer[[k1, k2]] += transfer
                                    [[i1.min(transfer.nrows() - 1), i2.min(transfer.ncols() - 1)]]
                                    * t1.data[[i1, j, k1]].conj()
                                    * t2.data[[i2, j, k2]];
                            }
                        }
                    }
                }
            }

            transfer = new_transfer;
        }

        Ok(transfer[[0, 0]])
    }

    /// Get total bond dimension (max across all bonds)
    pub fn max_bond_dim(&self) -> usize {
        self.tensors
            .iter()
            .map(|t| t.bond_dims().1)
            .max()
            .unwrap_or(1)
    }
}

// ============================================================================
// TQ Tensor Network Backend
// ============================================================================

/// TorchQuantum Tensor Network Backend
///
/// Provides MPS/PEPS simulation backend for TorchQuantum circuits.
#[derive(Debug, Clone)]
pub struct TQTensorNetworkBackend {
    /// MPS representation of the state
    pub mps: Option<MatrixProductState>,
    /// Number of qubits
    pub n_wires: usize,
    /// Configuration
    pub config: TensorNetworkConfig,
    /// Static mode flag
    pub static_mode: bool,
    /// Gate cache for static mode
    pub gate_cache: HashMap<String, Array2<CType>>,
}

impl TQTensorNetworkBackend {
    /// Create new tensor network backend
    pub fn new(n_wires: usize) -> Self {
        Self {
            mps: Some(MatrixProductState::from_computational_basis(n_wires, 0)),
            n_wires,
            config: TensorNetworkConfig::default(),
            static_mode: false,
            gate_cache: HashMap::new(),
        }
    }

    /// Create with custom configuration
    pub fn with_config(n_wires: usize, config: TensorNetworkConfig) -> Self {
        let mut mps = MatrixProductState::from_computational_basis(n_wires, 0);
        mps.config = config.clone();

        Self {
            mps: Some(mps),
            n_wires,
            config,
            static_mode: false,
            gate_cache: HashMap::new(),
        }
    }

    /// Reset to |0...0> state
    pub fn reset(&mut self) {
        self.mps = Some(MatrixProductState::from_computational_basis(
            self.n_wires,
            0,
        ));
        self.mps.as_mut().map(|m| m.config = self.config.clone());
    }

    /// Apply single-qubit gate
    pub fn apply_gate(&mut self, site: usize, gate: &Array2<CType>) -> Result<()> {
        if let Some(ref mut mps) = self.mps {
            mps.apply_single_qubit_gate(site, gate)
        } else {
            Err(MLError::InvalidConfiguration(
                "MPS not initialized".to_string(),
            ))
        }
    }

    /// Apply two-qubit gate
    pub fn apply_two_qubit_gate(
        &mut self,
        site1: usize,
        site2: usize,
        gate: &Array2<CType>,
    ) -> Result<()> {
        if let Some(ref mut mps) = self.mps {
            mps.apply_two_qubit_gate(site1, site2, gate)
        } else {
            Err(MLError::InvalidConfiguration(
                "MPS not initialized".to_string(),
            ))
        }
    }

    /// Get state vector (contracts MPS)
    pub fn get_state_vector(&self) -> Result<Vec<CType>> {
        if let Some(ref mps) = self.mps {
            mps.to_state_vector()
        } else {
            Err(MLError::InvalidConfiguration(
                "MPS not initialized".to_string(),
            ))
        }
    }

    /// Get expectation value of observable
    pub fn expectation_value(&self, observable: &Array2<CType>, sites: &[usize]) -> Result<f64> {
        // For single-qubit observables, contract efficiently
        if sites.len() == 1 && observable.nrows() == 2 {
            if let Some(ref mps) = self.mps {
                let site = sites[0];
                let tensor = &mps.tensors[site];

                // <O> = sum_{ij} O_{ij} * rho_{ji}
                // where rho is the reduced density matrix at this site
                let mut exp_val = Complex64::new(0.0, 0.0);

                for i in 0..2 {
                    for j in 0..2 {
                        // Simplified: assume product state for now
                        let rho_ji = tensor.data[[0, j, 0]].conj() * tensor.data[[0, i, 0]];
                        exp_val += observable[[i, j]] * rho_ji;
                    }
                }

                return Ok(exp_val.re);
            }
        }

        Err(MLError::NotSupported(
            "Multi-site observables not yet implemented for MPS".to_string(),
        ))
    }

    /// Get current bond dimension
    pub fn bond_dimension(&self) -> usize {
        self.mps.as_ref().map(|m| m.max_bond_dim()).unwrap_or(0)
    }

    /// Convert to TQDevice for compatibility
    pub fn to_tq_device(&self) -> Result<TQDevice> {
        let state_vec = self.get_state_vector()?;
        let mut qdev = TQDevice::new(self.n_wires);

        // Set state from vector
        use scirs2_core::ndarray::{ArrayD, IxDyn};
        let mut shape = vec![1usize]; // batch size 1
        shape.extend(vec![2; self.n_wires]);

        let states = ArrayD::from_shape_vec(IxDyn(&shape), state_vec)
            .map_err(|e| MLError::InvalidConfiguration(e.to_string()))?;
        qdev.set_states(states);

        Ok(qdev)
    }

    /// Create from TQDevice
    pub fn from_tq_device(qdev: &TQDevice) -> Result<Self> {
        let mps = MatrixProductState::from_tq_device(qdev)?;
        Ok(Self {
            n_wires: qdev.n_wires,
            mps: Some(mps),
            config: TensorNetworkConfig::default(),
            static_mode: false,
            gate_cache: HashMap::new(),
        })
    }
}

impl TQModule for TQTensorNetworkBackend {
    fn forward(&mut self, _qdev: &mut TQDevice) -> Result<()> {
        // Backend doesn't have a forward pass - it IS the state
        Ok(())
    }

    fn parameters(&self) -> Vec<TQParameter> {
        Vec::new()
    }

    fn n_wires(&self) -> Option<usize> {
        Some(self.n_wires)
    }

    fn set_n_wires(&mut self, n_wires: usize) {
        self.n_wires = n_wires;
        self.reset();
    }

    fn is_static_mode(&self) -> bool {
        self.static_mode
    }

    fn static_on(&mut self) {
        self.static_mode = true;
    }

    fn static_off(&mut self) {
        self.static_mode = false;
        self.gate_cache.clear();
    }

    fn name(&self) -> &str {
        "TQTensorNetworkBackend"
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_mps_creation() {
        let mps = MatrixProductState::from_computational_basis(4, 0);
        assert_eq!(mps.n_qubits, 4);
        assert_eq!(mps.tensors.len(), 4);
    }

    #[test]
    fn test_mps_state_vector() {
        let mps = MatrixProductState::from_computational_basis(2, 0);
        let state = mps.to_state_vector().expect("Should succeed");
        assert_eq!(state.len(), 4);
        assert!((state[0].re - 1.0).abs() < 1e-10);
        for i in 1..4 {
            assert!(state[i].norm() < 1e-10);
        }
    }

    #[test]
    fn test_tensor_network_backend() {
        let backend = TQTensorNetworkBackend::new(3);
        assert_eq!(backend.n_wires, 3);
        assert!(backend.mps.is_some());
    }

    #[test]
    fn test_single_qubit_gate_application() {
        let mut backend = TQTensorNetworkBackend::new(2);

        // Apply X gate
        let x_gate = 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("Should create matrix");

        backend.apply_gate(0, &x_gate).expect("Should apply gate");

        let state = backend.get_state_vector().expect("Should get state");
        // |10> state
        assert!(state[0].norm() < 1e-10);
        assert!(state[1].norm() < 1e-10);
        assert!((state[2].re - 1.0).abs() < 1e-10);
        assert!(state[3].norm() < 1e-10);
    }

    #[test]
    fn test_config_defaults() {
        let config = TensorNetworkConfig::default();
        assert_eq!(config.max_bond_dim, 64);
        assert_eq!(config.compression, CompressionMethod::SVD);
    }
}