Module cache

Module cache 

Source
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 eviction
  • ComplexEvalCache: Thread-safe cache for complex-valued evaluations (quantum amplitudes)
  • SimplificationCache: Cache for expression simplification results
  • BatchEvalCache: Optimized for VQE optimization loops with parameter sweeps
  • CachedEvaluator: Convenient wrapper with all caching features integrated
  • ExpressionCache: 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, &params).unwrap();

// Second evaluation retrieves from cache
let result2 = evaluator.eval(&expr, &params).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§

BatchEvalCache
Cache for batch evaluation results in VQE optimization loops.
CacheStats
Cache statistics
CachedEvaluator
An expression evaluator with integrated caching.
CombinedCacheStats
Combined statistics for all cache types
ComplexEvalCache
Thread-safe cache for complex number evaluation results.
EvalCache
Thread-safe cache for expression evaluation results with LRU eviction.
ExpressionCache
Hash consing for structural sharing of expressions
SimplificationCache
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