File size: 1,861 Bytes
9425aed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
//! QATAAUM State-Vector Quantum Simulator
//!
//! A simple but correct state-vector simulator for quantum circuits.
//! Supports circuits up to ~10 qubits (limited by memory: 2^n complex amplitudes).
//!
//! Clean-room implementation based on standard quantum mechanics textbooks.

use num_complex::Complex64;
use qataaum_ir::{Gate, GateKind, GateProgram, GateSequenceId, GateTerminator, QubitId, BitId};
use std::collections::HashMap;
use std::f64::consts::PI;

pub mod state;
pub mod gates;
pub mod executor;

pub use state::*;
pub use gates::*;
pub use executor::*;

/// Result type for simulator operations
pub type SimResult<T> = Result<T, SimError>;

/// Simulator errors
#[derive(Debug, Clone)]
pub enum SimError {
    /// Invalid qubit index
    InvalidQubit(usize),
    
    /// Invalid classical bit index
    InvalidBit(usize),
    
    /// State not normalized
    NotNormalized,
    
    /// Unsupported gate
    UnsupportedGate(String),
    
    /// Too many qubits for simulator
    TooManyQubits(usize),
    
    /// Invalid gate parameters
    InvalidParameters(String),
}

impl std::fmt::Display for SimError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SimError::InvalidQubit(q) => write!(f, "Invalid qubit index: {}", q),
            SimError::InvalidBit(b) => write!(f, "Invalid bit index: {}", b),
            SimError::NotNormalized => write!(f, "State vector not normalized"),
            SimError::UnsupportedGate(g) => write!(f, "Unsupported gate: {}", g),
            SimError::TooManyQubits(n) => write!(f, "Too many qubits: {} (max ~20)", n),
            SimError::InvalidParameters(msg) => write!(f, "Invalid parameters: {}", msg),
        }
    }
}

impl std::error::Error for SimError {}

// Made with Bob