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
//! Stable Quantum Computing Cache System
//!
//! High-performance caching for quantum gate matrices, decompositions, and results
//! using only stable Rust features and standard library components.

use crate::error::QuantRS2Result;
use scirs2_core::Complex64;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, OnceLock, RwLock};
use std::time::{Duration, Instant};

/// Cache key for quantum computations
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CacheKey {
    pub operation: String,
    pub parameters: Vec<u64>, // Quantized parameters for consistent hashing
    pub qubit_count: usize,
}

impl CacheKey {
    /// Create a new cache key
    pub fn new(operation: &str, params: Vec<f64>, qubit_count: usize) -> Self {
        // Quantize floating point parameters for consistent hashing
        let quantized_params: Vec<u64> = params
            .into_iter()
            .map(|p| {
                // Quantize to avoid floating point precision issues
                (p * 1_000_000.0).round() as u64
            })
            .collect();

        Self {
            operation: operation.to_string(),
            parameters: quantized_params,
            qubit_count,
        }
    }
}

/// Cached quantum computation results
#[derive(Debug, Clone)]
pub enum CachedResult {
    Matrix(Vec<Complex64>),
    StateVector(Vec<Complex64>),
    Probability(Vec<f64>),
    Scalar(Complex64),
    Decomposition(Vec<String>),
}

/// Cache entry with metadata
#[derive(Debug, Clone)]
struct CacheEntry {
    result: CachedResult,
    created_at: Instant,
    access_count: u64,
    last_accessed: Instant,
    /// Monotonic sequence number for LRU tracking (higher = more recently accessed)
    access_sequence: u64,
}

/// High-performance quantum cache with LRU eviction
pub struct StableQuantumCache {
    entries: Arc<RwLock<HashMap<CacheKey, CacheEntry>>>,
    max_size: usize,
    max_age: Duration,
    stats: Arc<RwLock<CacheStatistics>>,
    /// Monotonic counter for access sequence tracking
    access_counter: AtomicU64,
}

/// Cache performance statistics
#[derive(Debug, Clone, Default)]
pub struct CacheStatistics {
    pub hits: u64,
    pub misses: u64,
    pub evictions: u64,
    pub total_size: usize,
    pub average_access_count: f64,
    pub oldest_entry_age: Duration,
}

impl StableQuantumCache {
    /// Create a new quantum cache
    pub fn new(max_size: usize, max_age_seconds: u64) -> Self {
        Self {
            entries: Arc::new(RwLock::new(HashMap::new())),
            max_size,
            max_age: Duration::from_secs(max_age_seconds),
            stats: Arc::new(RwLock::new(CacheStatistics::default())),
            access_counter: AtomicU64::new(0),
        }
    }

    /// Insert a result into the cache
    pub fn insert(&self, key: CacheKey, result: CachedResult) {
        let now = Instant::now();
        let seq = self.access_counter.fetch_add(1, Ordering::Relaxed);
        let entry = CacheEntry {
            result,
            created_at: now,
            access_count: 0,
            last_accessed: now,
            access_sequence: seq,
        };

        {
            let mut entries = self.entries.write().expect("Cache entries lock poisoned");
            entries.insert(key, entry);

            // Perform maintenance if needed
            if entries.len() > self.max_size {
                self.evict_lru(&mut entries);
            }
        }

        // Update statistics
        let mut stats = self.stats.write().expect("Cache stats lock poisoned");
        stats.total_size += 1;
    }

    /// Get a result from the cache
    pub fn get(&self, key: &CacheKey) -> Option<CachedResult> {
        let now = Instant::now();

        // Check if entry exists and is not expired
        let result = {
            let mut entries = self.entries.write().expect("Cache entries lock poisoned");
            if let Some(entry) = entries.get_mut(key) {
                // Check if entry is expired
                if now.duration_since(entry.created_at) > self.max_age {
                    entries.remove(key);
                    let mut stats = self.stats.write().expect("Cache stats lock poisoned");
                    stats.misses += 1;
                    stats.evictions += 1;
                    return None;
                }

                // Update access statistics
                entry.access_count += 1;
                entry.last_accessed = now;
                entry.access_sequence = self.access_counter.fetch_add(1, Ordering::Relaxed);

                let mut stats = self.stats.write().expect("Cache stats lock poisoned");
                stats.hits += 1;

                Some(entry.result.clone())
            } else {
                let mut stats = self.stats.write().expect("Cache stats lock poisoned");
                stats.misses += 1;
                None
            }
        };

        result
    }

    /// Check if a key exists in the cache
    pub fn contains(&self, key: &CacheKey) -> bool {
        let entries = self.entries.read().expect("Cache entries lock poisoned");
        entries.contains_key(key)
    }

    /// Clear all cache entries
    pub fn clear(&self) {
        let mut entries = self.entries.write().expect("Cache entries lock poisoned");
        entries.clear();

        let mut stats = self.stats.write().expect("Cache stats lock poisoned");
        *stats = CacheStatistics::default();
    }

    /// Perform LRU eviction
    fn evict_lru(&self, entries: &mut HashMap<CacheKey, CacheEntry>) {
        // Find the least recently used entry using min_by_key on access_sequence
        // The entry with the smallest access_sequence is the least recently accessed
        if let Some(oldest_key) = entries
            .iter()
            .min_by_key(|(_, entry)| entry.access_sequence)
            .map(|(key, _)| key.clone())
        {
            entries.remove(&oldest_key);
            let mut stats = self.stats.write().expect("Cache stats lock poisoned");
            stats.evictions += 1;
        }
    }

    /// Remove expired entries
    pub fn cleanup_expired(&self) {
        let now = Instant::now();
        let mut entries = self.entries.write().expect("Cache entries lock poisoned");
        let mut expired_keys = Vec::new();

        for (key, entry) in entries.iter() {
            if now.duration_since(entry.created_at) > self.max_age {
                expired_keys.push(key.clone());
            }
        }

        let expired_count = expired_keys.len();
        for key in expired_keys {
            entries.remove(&key);
        }

        let mut stats = self.stats.write().expect("Cache stats lock poisoned");
        stats.evictions += expired_count as u64;
    }

    /// Get cache statistics
    pub fn get_statistics(&self) -> CacheStatistics {
        let entries = self.entries.read().expect("Cache entries lock poisoned");
        let mut stats = self
            .stats
            .read()
            .expect("Cache stats lock poisoned")
            .clone();

        // Update computed statistics
        stats.total_size = entries.len();

        if !entries.is_empty() {
            let total_accesses: u64 = entries.values().map(|e| e.access_count).sum();
            stats.average_access_count = total_accesses as f64 / entries.len() as f64;

            if let Some(oldest_entry) = entries.values().min_by_key(|e| e.created_at) {
                stats.oldest_entry_age = Instant::now().duration_since(oldest_entry.created_at);
            }
        }

        stats
    }

    /// Get cache hit ratio
    pub fn hit_ratio(&self) -> f64 {
        let stats = self.stats.read().expect("Cache stats lock poisoned");
        if stats.hits + stats.misses == 0 {
            0.0
        } else {
            stats.hits as f64 / (stats.hits + stats.misses) as f64
        }
    }

    /// Get memory usage estimate in bytes
    pub fn estimated_memory_usage(&self) -> usize {
        let entries = self.entries.read().expect("Cache entries lock poisoned");
        let mut total_size = 0;

        for (key, entry) in entries.iter() {
            // Estimate key size
            total_size += key.operation.len();
            total_size += key.parameters.len() * 8; // u64 = 8 bytes
            total_size += 8; // qubit_count

            // Estimate result size
            total_size += match &entry.result {
                CachedResult::Matrix(m) => m.len() * 16, // Complex64 = 16 bytes
                CachedResult::StateVector(s) => s.len() * 16,
                CachedResult::Probability(p) => p.len() * 8, // f64 = 8 bytes
                CachedResult::Scalar(_) => 16,
                CachedResult::Decomposition(d) => d.iter().map(|s| s.len()).sum(),
            };

            // Metadata size
            total_size += 32; // Approximate size of CacheEntry metadata
        }

        total_size
    }
}

/// Global quantum cache instance
static GLOBAL_CACHE: OnceLock<StableQuantumCache> = OnceLock::new();

/// Get the global quantum cache
pub fn get_global_cache() -> &'static StableQuantumCache {
    GLOBAL_CACHE.get_or_init(|| {
        StableQuantumCache::new(
            4096, // 4K entries
            3600, // 1 hour TTL
        )
    })
}

/// Macro for easy caching of quantum computations
#[macro_export]
macro_rules! cached_quantum_computation {
    ($operation:expr, $params:expr, $qubits:expr, $compute:expr) => {{
        let cache = $crate::optimizations_stable::quantum_cache::get_global_cache();
        let key = $crate::optimizations_stable::quantum_cache::CacheKey::new(
            $operation, $params, $qubits,
        );

        if let Some(result) = cache.get(&key) {
            result
        } else {
            let computed_result = $compute;
            cache.insert(key, computed_result.clone());
            computed_result
        }
    }};
}

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

    #[test]
    fn test_cache_basic_operations() {
        let cache = StableQuantumCache::new(100, 60);

        let key = CacheKey::new("test_op", vec![1.0], 2);
        let result = CachedResult::Scalar(Complex64::new(1.0, 0.0));

        // Test insertion and retrieval
        cache.insert(key.clone(), result.clone());
        let retrieved = cache
            .get(&key)
            .expect("Cache should contain the inserted key");

        match (&result, &retrieved) {
            (CachedResult::Scalar(a), CachedResult::Scalar(b)) => {
                assert!((a - b).norm() < 1e-10);
            }
            _ => panic!("Wrong result type"),
        }

        // Test statistics
        let stats = cache.get_statistics();
        assert_eq!(stats.hits, 1);
        assert_eq!(stats.misses, 0);
    }

    #[test]
    fn test_cache_key_quantization() {
        let key1 = CacheKey::new("rx", vec![std::f64::consts::PI], 1);
        let key2 = CacheKey::new("rx", vec![std::f64::consts::PI + 1e-10], 1);

        // Should be the same after quantization
        assert_eq!(key1, key2);
    }

    #[test]
    fn test_cache_lru_eviction() {
        let cache = StableQuantumCache::new(2, 60); // Small cache for testing

        let key1 = CacheKey::new("op1", vec![], 1);
        let key2 = CacheKey::new("op2", vec![], 1);
        let key3 = CacheKey::new("op3", vec![], 1);

        let result = CachedResult::Scalar(Complex64::new(1.0, 0.0));

        // Fill cache to capacity
        cache.insert(key1.clone(), result.clone());
        cache.insert(key2.clone(), result.clone());

        // Access key1 to make it more recently used
        let _ = cache.get(&key1);

        // Insert key3, should evict key2 (least recently used)
        cache.insert(key3.clone(), result.clone());

        assert!(cache.contains(&key1)); // Should still exist
        assert!(!cache.contains(&key2)); // Should be evicted
        assert!(cache.contains(&key3)); // Should exist
    }

    #[test]
    fn test_memory_usage_estimation() {
        let cache = StableQuantumCache::new(100, 60);

        // Insert a matrix result
        let key = CacheKey::new("matrix_op", vec![1.0], 2);
        let matrix = vec![Complex64::new(1.0, 0.0); 16]; // 4x4 matrix
        let result = CachedResult::Matrix(matrix);

        cache.insert(key, result);

        let memory_usage = cache.estimated_memory_usage();
        assert!(memory_usage > 0);

        // Should include matrix data (16 * 16 bytes) plus metadata
        assert!(memory_usage >= 256);
    }

    #[test]
    fn test_hit_ratio_calculation() {
        let cache = StableQuantumCache::new(100, 60);

        let key1 = CacheKey::new("op1", vec![], 1);
        let key2 = CacheKey::new("op2", vec![], 1);
        let result = CachedResult::Scalar(Complex64::new(1.0, 0.0));

        // No operations yet - hit ratio should be 0
        assert_eq!(cache.hit_ratio(), 0.0);

        // Insert and hit once
        cache.insert(key1.clone(), result);
        let _ = cache.get(&key1); // Hit
        let _ = cache.get(&key2); // Miss

        // Should be 50% hit ratio
        assert!((cache.hit_ratio() - 0.5).abs() < 1e-10);
    }
}