quantrs2_sim/
optimized_simulator.rs

1//! Optimized quantum circuit simulator with automatic selection of best implementation
2//!
3//! This module provides a high-performance simulator implementation that automatically
4//! selects the most appropriate optimization strategy based on qubit count and hardware support.
5
6use quantrs2_circuit::builder::{Circuit, Simulator};
7use quantrs2_core::{error::QuantRS2Result, register::Register};
8
9/// An optimized simulator for quantum circuits that automatically selects the best
10/// implementation based on circuit size and hardware capabilities
11#[derive(Debug, Clone)]
12pub struct OptimizedSimulator {
13    /// Use SIMD acceleration when available
14    _use_simd: bool,
15    /// Use memory-efficient algorithms for large qubit counts
16    memory_efficient: bool,
17    /// Qubit count threshold for switching to memory-efficient implementation
18    memory_efficient_threshold: usize,
19}
20
21impl OptimizedSimulator {
22    /// Create a new optimized simulator with default settings
23    #[must_use]
24    pub const fn new() -> Self {
25        Self {
26            _use_simd: cfg!(feature = "simd"),
27            memory_efficient: cfg!(feature = "memory_efficient"),
28            memory_efficient_threshold: 25, // Switch to memory-efficient at 25+ qubits
29        }
30    }
31
32    /// Create a new optimized simulator with custom settings
33    #[must_use]
34    pub const fn with_options(
35        use_simd: bool,
36        memory_efficient: bool,
37        memory_efficient_threshold: usize,
38    ) -> Self {
39        Self {
40            _use_simd: use_simd,
41            memory_efficient,
42            memory_efficient_threshold,
43        }
44    }
45
46    /// Create a new simulator optimized for maximum performance
47    #[must_use]
48    pub const fn high_performance() -> Self {
49        Self {
50            _use_simd: true,
51            memory_efficient: true,
52            memory_efficient_threshold: 28, // Higher threshold favors performance over memory usage
53        }
54    }
55
56    /// Create a new simulator optimized for memory efficiency
57    #[must_use]
58    pub const fn memory_efficient() -> Self {
59        Self {
60            _use_simd: true,
61            memory_efficient: true,
62            memory_efficient_threshold: 20, // Lower threshold favors memory usage over performance
63        }
64    }
65
66    /// Check if SIMD is available on this system
67    #[must_use]
68    pub fn is_simd_available() -> bool {
69        use quantrs2_core::platform::PlatformCapabilities;
70        let platform = PlatformCapabilities::detect();
71        platform.cpu.simd.avx2 || platform.cpu.simd.avx512 || platform.cpu.simd.sse4_1
72    }
73}
74
75impl Default for OptimizedSimulator {
76    fn default() -> Self {
77        Self::new()
78    }
79}
80
81impl<const N: usize> Simulator<N> for OptimizedSimulator {
82    fn run(&self, circuit: &Circuit<N>) -> QuantRS2Result<Register<N>> {
83        // For extremely large circuits, memory efficiency is critical
84        if N >= 30 && self.memory_efficient {
85            // Defer to chunked implementation
86            let chunked_simulator =
87                crate::optimized_simulator_chunked::OptimizedSimulatorChunked::new();
88            return chunked_simulator.run(circuit);
89        }
90
91        // For large circuits, use memory-efficient implementation if enabled
92        if N >= self.memory_efficient_threshold && self.memory_efficient {
93            // Defer to chunked implementation
94            let chunked_simulator =
95                crate::optimized_simulator_chunked::OptimizedSimulatorChunked::new();
96            return chunked_simulator.run(circuit);
97        }
98
99        // For smaller circuits, use the simple optimized implementation
100        let standard_simulator = crate::optimized_simulator_simple::OptimizedSimulatorSimple::new();
101        standard_simulator.run(circuit)
102    }
103}