quantrs2_device/compiler_passes/
types.rs

1//! Platform types and compilation targets
2
3use std::collections::{HashMap, HashSet};
4use std::time::{Duration, SystemTime};
5
6use super::config::HardwareConstraints;
7use crate::backend_traits::BackendCapabilities;
8
9/// Multi-platform compilation target specifications
10#[derive(Debug, Clone, PartialEq)]
11pub enum CompilationTarget {
12    /// IBM Quantum platform with specific backend
13    IBMQuantum {
14        backend_name: String,
15        coupling_map: Vec<(usize, usize)>,
16        native_gates: HashSet<String>,
17        basis_gates: Vec<String>,
18        max_shots: usize,
19        simulator: bool,
20    },
21    /// AWS Braket platform
22    AWSBraket {
23        device_arn: String,
24        provider: BraketProvider,
25        supported_gates: HashSet<String>,
26        max_shots: usize,
27        cost_per_shot: f64,
28    },
29    /// Azure Quantum platform
30    AzureQuantum {
31        workspace: String,
32        target: String,
33        provider: AzureProvider,
34        supported_operations: HashSet<String>,
35        resource_estimation: bool,
36    },
37    /// IonQ platform
38    IonQ {
39        backend: String,
40        all_to_all: bool,
41        native_gates: HashSet<String>,
42        noise_model: Option<String>,
43    },
44    /// Google Quantum AI
45    GoogleQuantumAI {
46        processor_id: String,
47        gate_set: GoogleGateSet,
48        topology: GridTopology,
49    },
50    /// Rigetti QCS
51    Rigetti {
52        qpu_id: String,
53        lattice: RigettiLattice,
54        supported_gates: HashSet<String>,
55    },
56    /// Custom hardware platform
57    Custom {
58        name: String,
59        capabilities: BackendCapabilities,
60        constraints: HardwareConstraints,
61    },
62}
63
64/// AWS Braket provider types
65#[derive(Debug, Clone, PartialEq)]
66pub enum BraketProvider {
67    IonQ,
68    Rigetti,
69    OQC,
70    QuEra,
71    Simulator,
72}
73
74/// Azure Quantum provider types
75#[derive(Debug, Clone, PartialEq)]
76pub enum AzureProvider {
77    IonQ,
78    Quantinuum,
79    Pasqal,
80    Rigetti,
81    Microsoft,
82}
83
84/// Google Quantum AI gate sets
85#[derive(Debug, Clone, PartialEq)]
86pub enum GoogleGateSet {
87    Sycamore,
88    SqrtISwap,
89    SYC,
90}
91
92/// Grid topology for Google devices
93#[derive(Debug, Clone, PartialEq)]
94pub struct GridTopology {
95    pub rows: usize,
96    pub cols: usize,
97    pub connectivity: ConnectivityPattern,
98}
99
100/// Connectivity patterns
101#[derive(Debug, Clone, PartialEq)]
102pub enum ConnectivityPattern {
103    NearestNeighbor,
104    Square,
105    Hexagonal,
106    Custom(Vec<(usize, usize)>),
107}
108
109/// Rigetti lattice types
110#[derive(Debug, Clone, PartialEq)]
111pub enum RigettiLattice {
112    Aspen,
113    Ankaa,
114    Custom(Vec<(usize, usize)>),
115}
116
117/// Pass information with timing and metrics
118#[derive(Debug, Clone)]
119pub struct PassInfo {
120    /// Pass name
121    pub name: String,
122    /// Execution time
123    pub execution_time: Duration,
124    /// Number of gates modified
125    pub gates_modified: usize,
126    /// Improvement metric
127    pub improvement: f64,
128    /// Pass-specific metrics
129    pub metrics: HashMap<String, f64>,
130    /// Success status
131    pub success: bool,
132    /// Error message if failed
133    pub error_message: Option<String>,
134}
135
136/// Hardware allocation information
137#[derive(Debug, Clone)]
138pub struct HardwareAllocation {
139    /// Qubit mapping from logical to physical
140    pub qubit_mapping: HashMap<usize, usize>,
141    /// Allocated qubits
142    pub allocated_qubits: Vec<usize>,
143    /// Resource utilization
144    pub resource_utilization: f64,
145    /// Allocation strategy used
146    pub strategy: AllocationStrategy,
147    /// Allocation quality score
148    pub quality_score: f64,
149}
150
151/// Allocation strategies
152#[derive(Debug, Clone, PartialEq)]
153pub enum AllocationStrategy {
154    GreedyMapping,
155    OptimalMapping,
156    HeuristicMapping,
157    GraphBased,
158    Custom(String),
159}
160
161/// Performance prediction with confidence intervals
162#[derive(Debug, Clone)]
163pub struct PerformancePrediction {
164    /// Predicted execution time
165    pub execution_time: Duration,
166    /// Predicted fidelity
167    pub fidelity: f64,
168    /// Predicted error rate
169    pub error_rate: f64,
170    /// Success probability
171    pub success_probability: f64,
172    /// Confidence interval
173    pub confidence_interval: (f64, f64),
174    /// Prediction model used
175    pub model: String,
176}
177
178/// Advanced metrics for compilation analysis
179#[derive(Debug, Clone)]
180pub struct AdvancedMetrics {
181    /// Quantum volume
182    pub quantum_volume: usize,
183    /// Expressivity measure
184    pub expressivity: f64,
185    /// Entanglement entropy
186    pub entanglement_entropy: f64,
187    /// Circuit complexity
188    pub complexity_score: f64,
189    /// Resource efficiency
190    pub resource_efficiency: f64,
191    /// Error resilience
192    pub error_resilience: f64,
193    /// Platform compatibility score
194    pub compatibility_score: f64,
195}
196
197/// Optimization iteration information
198#[derive(Debug, Clone)]
199pub struct OptimizationIteration {
200    /// Iteration number
201    pub iteration: usize,
202    /// Objective function values
203    pub objective_values: Vec<f64>,
204    /// Applied transformations
205    pub transformations: Vec<String>,
206    /// Intermediate metrics
207    pub intermediate_metrics: HashMap<String, f64>,
208    /// Timestamp
209    pub timestamp: Duration,
210}
211
212/// Platform-specific optimization results
213#[derive(Debug, Clone)]
214pub struct PlatformSpecificResults {
215    /// Platform name
216    pub platform: String,
217    /// Platform-specific metrics
218    pub metrics: HashMap<String, f64>,
219    /// Applied transformations
220    pub transformations: Vec<String>,
221}
222
223/// Platform-specific constraints
224#[derive(Debug, Clone)]
225pub struct PlatformConstraints {
226    /// Maximum circuit depth
227    pub max_depth: Option<usize>,
228    /// Supported gate set
229    pub supported_gates: HashSet<String>,
230    /// Connectivity restrictions
231    pub connectivity: Vec<(usize, usize)>,
232    /// Timing constraints
233    pub timing_constraints: HashMap<String, f64>,
234}
235
236/// Verification and validation results
237#[derive(Debug, Clone)]
238pub struct VerificationResults {
239    /// Circuit equivalence verified
240    pub equivalence_verified: bool,
241    /// Constraint satisfaction verified
242    pub constraints_satisfied: bool,
243    /// Semantic correctness verified
244    pub semantic_correctness: bool,
245    /// Verification time
246    pub verification_time: Duration,
247    /// Detailed verification report
248    pub verification_report: String,
249}
250
251/// Constraint verification result
252#[derive(Debug, Clone)]
253pub struct ConstraintVerificationResult {
254    /// Whether constraints are satisfied
255    pub is_valid: bool,
256}
257
258/// Semantic verification result
259#[derive(Debug, Clone)]
260pub struct SemanticVerificationResult {
261    /// Whether semantics are correct
262    pub is_valid: bool,
263}
264
265/// Complexity metrics for circuit analysis
266#[derive(Debug, Clone)]
267pub struct ComplexityMetrics {
268    /// Circuit depth distribution
269    pub depth_distribution: Vec<usize>,
270    /// Gate type distribution
271    pub gate_distribution: HashMap<String, usize>,
272    /// Entanglement entropy
273    pub entanglement_entropy: f64,
274    /// Expressivity measure
275    pub expressivity_measure: f64,
276    /// Quantum volume
277    pub quantum_volume: usize,
278}
279
280/// Compilation result with comprehensive analysis
281#[derive(Debug, Clone)]
282pub struct CompilationResult {
283    /// Original circuit
284    pub original_circuit: String,
285    /// Optimized circuit
286    pub optimized_circuit: String,
287    /// Optimization statistics
288    pub optimization_stats: OptimizationStats,
289    /// Applied passes with detailed information
290    pub applied_passes: Vec<PassInfo>,
291    /// Hardware allocation and scheduling
292    pub hardware_allocation: HardwareAllocation,
293    /// Predicted performance with confidence intervals
294    pub predicted_performance: PerformancePrediction,
295    /// Compilation timing breakdown
296    pub compilation_time: Duration,
297    /// Advanced metrics and analysis
298    pub advanced_metrics: AdvancedMetrics,
299    /// Multi-pass optimization history
300    pub optimization_history: Vec<OptimizationIteration>,
301    /// Platform-specific results
302    pub platform_specific: PlatformSpecificResults,
303    /// Verification and validation results
304    pub verification_results: VerificationResults,
305}
306
307/// Optimization statistics
308#[derive(Debug, Clone)]
309pub struct OptimizationStats {
310    /// Original gate count
311    pub original_gate_count: usize,
312    /// Optimized gate count
313    pub optimized_gate_count: usize,
314    /// Original circuit depth
315    pub original_depth: usize,
316    /// Optimized circuit depth
317    pub optimized_depth: usize,
318    /// Predicted error rate improvement
319    pub error_improvement: f64,
320    /// Fidelity improvement
321    pub fidelity_improvement: f64,
322    /// Resource efficiency gain
323    pub efficiency_gain: f64,
324    /// Overall improvement score
325    pub overall_improvement: f64,
326}
327
328/// Performance anomaly detection
329#[derive(Debug, Clone)]
330pub struct PerformanceAnomaly {
331    /// Anomaly description
332    pub description: String,
333    /// Severity level
334    pub severity: AnomalySeverity,
335    /// Confidence score
336    pub confidence: f64,
337    /// Affected metrics
338    pub affected_metrics: Vec<String>,
339    /// Anomaly type
340    pub anomaly_type: String,
341    /// Recommended action
342    pub recommended_action: String,
343}
344
345/// Anomaly severity levels
346#[derive(Debug, Clone, PartialEq)]
347pub enum AnomalySeverity {
348    Low,
349    Medium,
350    High,
351    Critical,
352}
353
354/// Advanced optimization results
355#[derive(Debug, Clone)]
356pub struct AdvancedOptimizationResult {
357    /// Optimization method used
358    pub method: String,
359    /// Convergence achieved
360    pub converged: bool,
361    /// Final objective value
362    pub objective_value: f64,
363    /// Number of iterations
364    pub iterations: usize,
365    /// Parameter evolution
366    pub parameter_evolution: Vec<scirs2_core::ndarray::Array1<f64>>,
367    /// Whether optimization was successful
368    pub success: bool,
369    /// Optimized parameters
370    pub x: scirs2_core::ndarray::Array1<f64>,
371    /// Improvement achieved
372    pub improvement: f64,
373}
374
375/// Linear algebra optimization results
376#[derive(Debug, Clone)]
377pub struct LinalgOptimizationResult {
378    /// Matrix decomposition improvements
379    pub decomposition_improvements: HashMap<String, f64>,
380    /// Numerical stability metrics
381    pub stability_metrics: NumericalStabilityMetrics,
382    /// Eigenvalue analysis
383    pub eigenvalue_analysis: EigenvalueAnalysis,
384}
385
386/// Numerical stability metrics
387#[derive(Debug, Clone)]
388pub struct NumericalStabilityMetrics {
389    /// Condition number
390    pub condition_number: f64,
391    /// Numerical rank
392    pub numerical_rank: usize,
393    /// Spectral radius
394    pub spectral_radius: f64,
395}
396
397/// Eigenvalue analysis
398#[derive(Debug, Clone)]
399pub struct EigenvalueAnalysis {
400    /// Eigenvalue distribution
401    pub eigenvalue_distribution: Vec<scirs2_core::Complex64>,
402    /// Spectral gap
403    pub spectral_gap: f64,
404    /// Entanglement spectrum
405    pub entanglement_spectrum: Vec<f64>,
406}