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    pub fn new() -> Self {
24        Self {
25            _use_simd: cfg!(feature = "simd"),
26            memory_efficient: cfg!(feature = "memory_efficient"),
27            memory_efficient_threshold: 25, // Switch to memory-efficient at 25+ qubits
28        }
29    }
30
31    /// Create a new optimized simulator with custom settings
32    pub fn with_options(
33        use_simd: bool,
34        memory_efficient: bool,
35        memory_efficient_threshold: usize,
36    ) -> Self {
37        Self {
38            _use_simd: use_simd,
39            memory_efficient,
40            memory_efficient_threshold,
41        }
42    }
43
44    /// Create a new simulator optimized for maximum performance
45    pub fn high_performance() -> Self {
46        Self {
47            _use_simd: true,
48            memory_efficient: true,
49            memory_efficient_threshold: 28, // Higher threshold favors performance over memory usage
50        }
51    }
52
53    /// Create a new simulator optimized for memory efficiency
54    pub fn memory_efficient() -> Self {
55        Self {
56            _use_simd: true,
57            memory_efficient: true,
58            memory_efficient_threshold: 20, // Lower threshold favors memory usage over performance
59        }
60    }
61
62    /// Check if SIMD is available on this system
63    pub fn is_simd_available() -> bool {
64        use quantrs2_core::platform::PlatformCapabilities;
65        let platform = PlatformCapabilities::detect();
66        platform.cpu.simd.avx2 || platform.cpu.simd.avx512 || platform.cpu.simd.sse4_1
67    }
68}
69
70impl Default for OptimizedSimulator {
71    fn default() -> Self {
72        Self::new()
73    }
74}
75
76impl<const N: usize> Simulator<N> for OptimizedSimulator {
77    fn run(&self, circuit: &Circuit<N>) -> QuantRS2Result<Register<N>> {
78        // For extremely large circuits, memory efficiency is critical
79        if N >= 30 && self.memory_efficient {
80            // Defer to chunked implementation
81            let chunked_simulator =
82                crate::optimized_simulator_chunked::OptimizedSimulatorChunked::new();
83            return chunked_simulator.run(circuit);
84        }
85
86        // For large circuits, use memory-efficient implementation if enabled
87        if N >= self.memory_efficient_threshold && self.memory_efficient {
88            // Defer to chunked implementation
89            let chunked_simulator =
90                crate::optimized_simulator_chunked::OptimizedSimulatorChunked::new();
91            return chunked_simulator.run(circuit);
92        }
93
94        // For smaller circuits, use the simple optimized implementation
95        let standard_simulator = crate::optimized_simulator_simple::OptimizedSimulatorSimple::new();
96        standard_simulator.run(circuit)
97    }
98}