use quantrs2_circuit::builder::{Circuit, Simulator};
use quantrs2_core::{gate::GateOp, qubit::QubitId};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
use std::sync::{Arc, Barrier, Mutex, RwLock};
use std::time::{Duration, Instant};
use uuid::Uuid;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HardwareStrategy {
CacheOptimized,
SIMDOptimized,
NUMAAware,
GPUOffload,
Hybrid,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RecommendationType {
GateReordering,
CircuitDecomposition,
ResourceAllocation,
StrategyChange,
HardwareConfiguration,
}
#[derive(Debug, Clone)]
pub struct MLFeatures {
pub num_gates: usize,
pub num_qubits: usize,
pub circuit_depth: usize,
pub avg_connectivity: f64,
pub parallelism_factor: f64,
pub gate_distribution: HashMap<String, usize>,
pub entanglement_score: f64,
pub dependency_density: f64,
}
#[derive(Debug, Clone)]
pub struct HardwareCharacteristics {
pub num_cores: usize,
pub l1_cache_size: usize,
pub l2_cache_size: usize,
pub l3_cache_size: usize,
pub memory_bandwidth: f64,
pub num_numa_nodes: usize,
pub has_gpu: bool,
pub simd_width: usize,
}
#[derive(Debug, Clone)]
pub struct ParallelizationAnalysis {
pub tasks: Vec<ParallelTask>,
pub num_layers: usize,
pub efficiency: f64,
pub max_parallelism: usize,
pub critical_path_length: usize,
pub resource_utilization: ResourceUtilization,
pub recommendations: Vec<OptimizationRecommendation>,
}
#[derive(Debug, Clone)]
pub struct NodeCapacity {
pub cpu_cores: usize,
pub memory_gb: f64,
pub gpu_available: bool,
pub network_bandwidth_gbps: f64,
pub relative_performance: f64,
}
#[derive(Debug, Clone, Default)]
pub struct TaskCompletionStats {
pub total_tasks: usize,
pub average_duration: Duration,
pub success_rate: f64,
pub load_balance_effectiveness: f64,
}
pub struct LoadBalancer {
thread_loads: Vec<f64>,
task_queues: Vec<VecDeque<ParallelTask>>,
work_stealing_stats: WorkStealingStats,
}
impl LoadBalancer {
#[must_use]
pub fn new(num_threads: usize) -> Self {
Self {
thread_loads: vec![0.0; num_threads],
task_queues: vec![VecDeque::new(); num_threads],
work_stealing_stats: WorkStealingStats::default(),
}
}
pub fn balance_load(&mut self, tasks: Vec<ParallelTask>) -> Vec<Vec<ParallelTask>> {
let mut balanced_tasks = vec![Vec::new(); self.thread_loads.len()];
for (i, task) in tasks.into_iter().enumerate() {
let thread_index = i % self.thread_loads.len();
balanced_tasks[thread_index].push(task);
}
balanced_tasks
}
}
#[derive(Debug, Clone)]
pub struct ResourceUtilization {
pub cpu_utilization: Vec<f64>,
pub memory_usage: Vec<usize>,
pub load_balance_score: f64,
pub communication_overhead: f64,
}
#[derive(Debug, Clone, Default)]
pub struct WorkStealingStats {
pub steal_attempts: usize,
pub successful_steals: usize,
pub failed_steals: usize,
pub average_steal_latency: Duration,
}
#[derive(Debug, Clone)]
pub struct CircuitParallelResult {
pub circuit_size: usize,
pub num_qubits: usize,
pub analysis_time: Duration,
pub efficiency: f64,
pub max_parallelism: usize,
pub num_tasks: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum TaskPriority {
Low = 1,
Normal = 2,
High = 3,
Critical = 4,
}
#[derive(Debug, Clone)]
pub struct GateNode {
pub gate_index: usize,
pub gate: Arc<dyn GateOp + Send + Sync>,
pub qubits: HashSet<QubitId>,
pub layer: usize,
pub cost: f64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoadBalancingConfig {
pub enable_dynamic_balancing: bool,
pub work_stealing_strategy: WorkStealingStrategy,
pub monitoring_interval: Duration,
pub rebalancing_threshold: f64,
}
#[derive(Debug, Clone)]
pub struct DependencyGraph {
pub nodes: Vec<GateNode>,
pub edges: HashMap<usize, Vec<usize>>,
pub reverse_edges: HashMap<usize, Vec<usize>>,
pub layers: Vec<Vec<usize>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AutoParallelConfig {
pub max_threads: usize,
pub min_gates_for_parallel: usize,
pub strategy: ParallelizationStrategy,
pub resource_constraints: ResourceConstraints,
pub enable_inter_layer_parallel: bool,
pub enable_gate_fusion: bool,
pub scirs2_optimization_level: OptimizationLevel,
pub load_balancing: LoadBalancingConfig,
pub enable_analysis_caching: bool,
pub memory_budget: usize,
}
#[derive(Debug, Clone)]
pub struct ParallelTask {
pub id: Uuid,
pub gates: Vec<Arc<dyn GateOp + Send + Sync>>,
pub qubits: HashSet<QubitId>,
pub cost: f64,
pub memory_requirement: usize,
pub dependencies: HashSet<Uuid>,
pub priority: TaskPriority,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum RecommendationComplexity {
Low,
Medium,
High,
}
#[derive(Debug, Clone, Default)]
pub struct ParallelPerformanceStats {
pub circuits_processed: usize,
pub total_execution_time: Duration,
pub average_efficiency: f64,
pub cache_hit_rate: f64,
pub task_stats: TaskCompletionStats,
pub resource_history: Vec<ResourceSnapshot>,
}
#[derive(Debug, Clone)]
pub struct OptimizationRecommendation {
pub recommendation_type: RecommendationType,
pub description: String,
pub expected_improvement: f64,
pub complexity: RecommendationComplexity,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum WorkStealingStrategy {
Random,
CostAware,
LocalityAware,
Adaptive,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum OptimizationLevel {
None,
Basic,
Advanced,
Aggressive,
Custom,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceConstraints {
pub max_memory_per_thread: usize,
pub max_cpu_utilization: f64,
pub max_gates_per_thread: usize,
pub preferred_numa_node: Option<usize>,
}
#[derive(Debug, Clone)]
pub struct ResourceSnapshot {
pub timestamp: Instant,
pub cpu_utilization: Vec<f64>,
pub memory_usage: usize,
pub active_tasks: usize,
}
#[derive(Debug, Clone)]
pub struct AutoParallelBenchmarkResults {
pub total_time: Duration,
pub circuit_results: Vec<CircuitParallelResult>,
pub average_efficiency: f64,
pub average_parallelism: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ParallelizationStrategy {
DependencyAnalysis,
LayerBased,
QubitPartitioning,
Hybrid,
MLGuided,
HardwareAware,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MLPredictedStrategy {
HighParallelism,
BalancedParallelism,
ConservativeParallelism,
LayerOptimized,
}