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
//! Quantum unit testing framework
//!
//! This module provides tools for testing quantum circuits, states, and operations
//! with proper handling of quantum-specific properties like phase and entanglement.

use crate::complex_ext::QuantumComplexExt;
use crate::error::QuantRS2Error;
use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::Complex64;
use std::fmt;

/// Tolerance for quantum state comparisons
pub const DEFAULT_TOLERANCE: f64 = 1e-10;

/// Result of a quantum test
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TestResult {
    /// Test passed
    Pass,
    /// Test failed with reason
    Fail(String),
    /// Test skipped
    Skip(String),
}

impl TestResult {
    pub const fn is_pass(&self) -> bool {
        matches!(self, Self::Pass)
    }
}

/// Quantum state assertion helper
pub struct QuantumAssert {
    tolerance: f64,
}

impl Default for QuantumAssert {
    fn default() -> Self {
        Self {
            tolerance: DEFAULT_TOLERANCE,
        }
    }
}

impl QuantumAssert {
    /// Create with custom tolerance
    pub const fn with_tolerance(tolerance: f64) -> Self {
        Self { tolerance }
    }

    /// Assert two quantum states are equal (up to global phase)
    pub fn states_equal(
        &self,
        state1: &Array1<Complex64>,
        state2: &Array1<Complex64>,
    ) -> TestResult {
        if state1.len() != state2.len() {
            return TestResult::Fail(format!(
                "State dimensions mismatch: {} vs {}",
                state1.len(),
                state2.len()
            ));
        }

        // Find first non-zero amplitude to determine global phase
        let mut phase_factor = None;
        for i in 0..state1.len() {
            if state1[i].norm() > self.tolerance && state2[i].norm() > self.tolerance {
                phase_factor = Some(state2[i] / state1[i]);
                break;
            }
        }

        let phase = phase_factor.unwrap_or(Complex64::new(1.0, 0.0));

        // Check all amplitudes match after phase correction
        for i in 0..state1.len() {
            let expected = state1[i] * phase;
            if (expected - state2[i]).norm() > self.tolerance {
                return TestResult::Fail(format!(
                    "States differ at index {}: expected {}, got {}",
                    i, expected, state2[i]
                ));
            }
        }

        TestResult::Pass
    }

    /// Assert a state is normalized
    pub fn state_normalized(&self, state: &Array1<Complex64>) -> TestResult {
        let norm_squared: f64 = state.iter().map(|c| c.norm_sqr()).sum();

        if (norm_squared - 1.0).abs() > self.tolerance {
            TestResult::Fail(format!(
                "State not normalized: norm^2 = {norm_squared} (expected 1.0)"
            ))
        } else {
            TestResult::Pass
        }
    }

    /// Assert two states are orthogonal
    pub fn states_orthogonal(
        &self,
        state1: &Array1<Complex64>,
        state2: &Array1<Complex64>,
    ) -> TestResult {
        if state1.len() != state2.len() {
            return TestResult::Fail("State dimensions mismatch".to_string());
        }

        let inner_product: Complex64 = state1
            .iter()
            .zip(state2.iter())
            .map(|(a, b)| a.conj() * b)
            .sum();

        if inner_product.norm() > self.tolerance {
            TestResult::Fail(format!(
                "States not orthogonal: inner product = {inner_product}"
            ))
        } else {
            TestResult::Pass
        }
    }

    /// Assert a matrix is unitary
    pub fn matrix_unitary(&self, matrix: &Array2<Complex64>) -> TestResult {
        let (rows, cols) = matrix.dim();
        if rows != cols {
            return TestResult::Fail(format!("Matrix not square: {rows}x{cols}"));
        }

        // Compute U† U
        let conjugate_transpose = matrix.t().mapv(|c| c.conj());
        let product = conjugate_transpose.dot(matrix);

        // Check if it's identity
        for i in 0..rows {
            for j in 0..cols {
                let expected = if i == j {
                    Complex64::new(1.0, 0.0)
                } else {
                    Complex64::new(0.0, 0.0)
                };

                if (product[[i, j]] - expected).norm() > self.tolerance {
                    return TestResult::Fail(format!(
                        "U†U not identity at ({},{}): got {}",
                        i,
                        j,
                        product[[i, j]]
                    ));
                }
            }
        }

        TestResult::Pass
    }

    /// Assert a state has specific measurement probabilities
    pub fn measurement_probabilities(
        &self,
        state: &Array1<Complex64>,
        expected_probs: &[(usize, f64)],
    ) -> TestResult {
        for &(index, expected_prob) in expected_probs {
            if index >= state.len() {
                return TestResult::Fail(format!(
                    "Index {} out of bounds for state of length {}",
                    index,
                    state.len()
                ));
            }

            let actual_prob = state[index].probability();
            if (actual_prob - expected_prob).abs() > self.tolerance {
                return TestResult::Fail(format!(
                    "Probability mismatch at index {index}: expected {expected_prob}, got {actual_prob}"
                ));
            }
        }

        TestResult::Pass
    }

    /// Assert entanglement properties
    pub fn is_entangled(&self, state: &Array1<Complex64>, qubit_indices: &[usize]) -> TestResult {
        // For a 2-qubit system, check if state can be written as |ψ⟩ = |a⟩ ⊗ |b⟩
        if qubit_indices.len() != 2 {
            return TestResult::Skip(
                "Entanglement check only implemented for 2-qubit subsystems".to_string(),
            );
        }

        let n_qubits = (state.len() as f64).log2() as usize;
        if qubit_indices.iter().any(|&i| i >= n_qubits) {
            return TestResult::Fail("Qubit index out of bounds".to_string());
        }

        // Simplified check: for computational basis states
        // A separable state has rank-1 reduced density matrix
        // This is a placeholder - full implementation would compute partial trace

        TestResult::Skip("Full entanglement check not yet implemented".to_string())
    }
}

/// Quantum circuit test builder
pub struct QuantumTest {
    name: String,
    setup: Option<Box<dyn Fn() -> Result<(), QuantRS2Error>>>,
    test: Box<dyn Fn() -> TestResult>,
    teardown: Option<Box<dyn Fn()>>,
}

impl QuantumTest {
    /// Create a new quantum test
    pub fn new(name: impl Into<String>, test: impl Fn() -> TestResult + 'static) -> Self {
        Self {
            name: name.into(),
            setup: None,
            test: Box::new(test),
            teardown: None,
        }
    }

    /// Add setup function
    #[must_use]
    pub fn with_setup(mut self, setup: impl Fn() -> Result<(), QuantRS2Error> + 'static) -> Self {
        self.setup = Some(Box::new(setup));
        self
    }

    /// Add teardown function
    #[must_use]
    pub fn with_teardown(mut self, teardown: impl Fn() + 'static) -> Self {
        self.teardown = Some(Box::new(teardown));
        self
    }

    /// Run the test
    pub fn run(&self) -> TestResult {
        // Run setup
        if let Some(setup) = &self.setup {
            if let Err(e) = setup() {
                return TestResult::Fail(format!("Setup failed: {e}"));
            }
        }

        // Run test
        let result = (self.test)();

        // Run teardown
        if let Some(teardown) = &self.teardown {
            teardown();
        }

        result
    }
}

/// Test suite for organizing multiple quantum tests
pub struct QuantumTestSuite {
    name: String,
    tests: Vec<QuantumTest>,
}

impl QuantumTestSuite {
    /// Create a new test suite
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            tests: Vec::new(),
        }
    }

    /// Add a test to the suite
    pub fn add_test(&mut self, test: QuantumTest) {
        self.tests.push(test);
    }

    /// Run all tests in the suite
    pub fn run(&self) -> TestSuiteResult {
        let mut results = Vec::new();

        for test in &self.tests {
            let result = test.run();
            results.push((test.name.clone(), result));
        }

        TestSuiteResult {
            suite_name: self.name.clone(),
            results,
        }
    }
}

/// Results from running a test suite
pub struct TestSuiteResult {
    suite_name: String,
    results: Vec<(String, TestResult)>,
}

impl TestSuiteResult {
    /// Get number of passed tests
    pub fn passed(&self) -> usize {
        self.results.iter().filter(|(_, r)| r.is_pass()).count()
    }

    /// Get number of failed tests
    pub fn failed(&self) -> usize {
        self.results
            .iter()
            .filter(|(_, r)| matches!(r, TestResult::Fail(_)))
            .count()
    }

    /// Get number of skipped tests
    pub fn skipped(&self) -> usize {
        self.results
            .iter()
            .filter(|(_, r)| matches!(r, TestResult::Skip(_)))
            .count()
    }

    /// Get total number of tests
    pub fn total(&self) -> usize {
        self.results.len()
    }

    /// Check if all tests passed
    pub fn all_passed(&self) -> bool {
        self.failed() == 0
    }
}

impl fmt::Display for TestSuiteResult {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "\n{} Test Results:", self.suite_name)?;
        writeln!(f, "{}", "=".repeat(50))?;

        for (name, result) in &self.results {
            let status = match result {
                TestResult::Pass => "✓ PASS",
                TestResult::Fail(_) => "✗ FAIL",
                TestResult::Skip(_) => "⊙ SKIP",
            };

            writeln!(f, "{status:<6} {name}")?;

            if let TestResult::Fail(reason) = result {
                writeln!(f, "       Reason: {reason}")?;
            } else if let TestResult::Skip(reason) = result {
                writeln!(f, "       Reason: {reason}")?;
            }
        }

        writeln!(f, "{}", "=".repeat(50))?;
        writeln!(
            f,
            "Total: {} | Passed: {} | Failed: {} | Skipped: {}",
            self.total(),
            self.passed(),
            self.failed(),
            self.skipped()
        )?;

        Ok(())
    }
}

/// Macros for quantum testing
#[macro_export]
macro_rules! quantum_test {
    ($name:expr, $test:expr) => {
        QuantumTest::new($name, $test)
    };
}

#[macro_export]
macro_rules! assert_states_equal {
    ($state1:expr, $state2:expr) => {{
        let assert = QuantumAssert::default();
        assert.states_equal($state1, $state2)
    }};
    ($state1:expr, $state2:expr, $tolerance:expr) => {{
        let assert = QuantumAssert::with_tolerance($tolerance);
        assert.states_equal($state1, $state2)
    }};
}

#[macro_export]
macro_rules! assert_unitary {
    ($matrix:expr) => {{
        let assert = QuantumAssert::default();
        assert.matrix_unitary($matrix)
    }};
}

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

    #[test]
    fn test_quantum_assert_states_equal() {
        let assert = QuantumAssert::default();

        // Test equal states
        let state1 = array![Complex64::new(1.0, 0.0), Complex64::new(0.0, 0.0)];
        let state2 = state1.clone();
        assert!(assert.states_equal(&state1, &state2).is_pass());

        // Test states with global phase
        let state3 = array![Complex64::new(0.0, 1.0), Complex64::new(0.0, 0.0)]; // i|0⟩
        assert!(assert.states_equal(&state1, &state3).is_pass());

        // Test different states
        let state4 = array![Complex64::new(0.0, 0.0), Complex64::new(1.0, 0.0)];
        assert!(!assert.states_equal(&state1, &state4).is_pass());
    }

    #[test]
    fn test_quantum_assert_normalized() {
        let assert = QuantumAssert::default();

        // Normalized state
        let state1 = array![
            Complex64::new(1.0 / 2.0_f64.sqrt(), 0.0),
            Complex64::new(1.0 / 2.0_f64.sqrt(), 0.0)
        ];
        assert!(assert.state_normalized(&state1).is_pass());

        // Not normalized
        let state2 = array![Complex64::new(1.0, 0.0), Complex64::new(1.0, 0.0)];
        assert!(!assert.state_normalized(&state2).is_pass());
    }

    #[test]
    fn test_quantum_test_suite() {
        let mut suite = QuantumTestSuite::new("Example Suite");

        suite.add_test(QuantumTest::new("Test 1", || TestResult::Pass));
        suite.add_test(QuantumTest::new("Test 2", || {
            TestResult::Fail("Expected failure".to_string())
        }));
        suite.add_test(QuantumTest::new("Test 3", || {
            TestResult::Skip("Not implemented".to_string())
        }));

        let results = suite.run();
        assert_eq!(results.total(), 3);
        assert_eq!(results.passed(), 1);
        assert_eq!(results.failed(), 1);
        assert_eq!(results.skipped(), 1);
    }
}