Expand description
Expression caching and memoization.
This module provides caching mechanisms for expensive operations like evaluation, simplification, and complex number operations.
§Features
EvalCache: Thread-safe cache for real-valued evaluation results with LRU evictionComplexEvalCache: Thread-safe cache for complex-valued evaluations (quantum amplitudes)SimplificationCache: Cache for expression simplification resultsBatchEvalCache: Optimized for VQE optimization loops with parameter sweepsCachedEvaluator: Convenient wrapper with all caching features integratedExpressionCache: Hash consing for structural sharing of expressions
§Performance Benefits
Expression caching is critical for quantum computing applications:
- VQE/QAOA loops: Same expressions evaluated thousands of times with different parameters
- Gradient computation: Derivatives computed repeatedly during optimization
- Circuit simulation: Gate matrices cached after first computation
§Example
ⓘ
use quantrs2_symengine_pure::cache::{CachedEvaluator, hash_params};
use quantrs2_symengine_pure::Expression;
use std::collections::HashMap;
let evaluator = CachedEvaluator::new();
let expr = Expression::symbol("x").sin();
// First evaluation computes the result
let mut params = HashMap::new();
params.insert("x".to_string(), 0.5);
let result1 = evaluator.eval(&expr, ¶ms).unwrap();
// Second evaluation retrieves from cache
let result2 = evaluator.eval(&expr, ¶ms).unwrap();
assert!((result1 - result2).abs() < 1e-10);
// Check hit rate
let stats = evaluator.stats();
println!("Cache hit rate: {:.1}%", stats.overall_hit_rate() * 100.0);Structs§
- Batch
Eval Cache - Cache for batch evaluation results in VQE optimization loops.
- Cache
Stats - Cache statistics
- Cached
Evaluator - An expression evaluator with integrated caching.
- Combined
Cache Stats - Combined statistics for all cache types
- Complex
Eval Cache - Thread-safe cache for complex number evaluation results.
- Eval
Cache - Thread-safe cache for expression evaluation results with LRU eviction.
- Expression
Cache - Hash consing for structural sharing of expressions
- Simplification
Cache - Thread-safe cache for expression simplification results.
Constants§
- DEFAULT_
MAX_ CACHE_ SIZE - Default maximum cache size (entries)
Functions§
- compute_
hash - Compute a hash for an expression
- hash_
complex_ params - Compute a hash for complex parameters
- hash_
param_ array - Compute a hash for a parameter array (for batch operations)
- hash_
params - Compute a hash for a set of real parameters