quantrs2_circuit/optimization/
mod.rs1pub mod analysis;
7pub mod cost_model;
8pub mod gate_properties;
9pub mod noise;
10pub mod pass_manager;
11pub mod passes;
12
13pub use analysis::{CircuitAnalyzer, CircuitMetrics, OptimizationReport};
14pub use cost_model::{AbstractCostModel, CostModel, HardwareCostModel};
15pub use gate_properties::{CommutationTable, GateCost, GateError, GateProperties};
16pub use noise::{
17 CoherenceOptimization, DecouplingSequence, DynamicalDecoupling, NoiseAwareCostModel,
18 NoiseAwareMapping, NoiseAwareOptimizer, NoiseModel,
19};
20pub use pass_manager::{OptimizationLevel, PassConfig, PassManager};
21pub use passes::{
22 CircuitRewriting, CostBasedOptimization, DecompositionOptimization, GateCancellation,
23 GateCommutation, GateMerging, OptimizationPass, PeepholeOptimization, RotationMerging,
24 TemplateMatching, TwoQubitOptimization,
25};
26
27use self::cost_model::CircuitCostExt;
28use crate::builder::Circuit;
29use quantrs2_core::error::QuantRS2Result;
30
31pub struct CircuitOptimizer2<const N: usize> {
33 pass_manager: PassManager,
34 cost_model: Box<dyn CostModel>,
35 analyzer: CircuitAnalyzer,
36}
37
38impl<const N: usize> CircuitOptimizer2<N> {
39 pub fn new() -> Self {
41 Self {
42 pass_manager: PassManager::default(),
43 cost_model: Box::new(AbstractCostModel::default()),
44 analyzer: CircuitAnalyzer::new(),
45 }
46 }
47
48 pub fn with_level(level: OptimizationLevel) -> Self {
50 Self {
51 pass_manager: PassManager::with_level(level),
52 cost_model: Box::new(AbstractCostModel::default()),
53 analyzer: CircuitAnalyzer::new(),
54 }
55 }
56
57 pub fn for_hardware(hardware: &str) -> Self {
59 Self {
60 pass_manager: PassManager::for_hardware(hardware),
61 cost_model: Box::new(HardwareCostModel::for_backend(hardware)),
62 analyzer: CircuitAnalyzer::new(),
63 }
64 }
65
66 pub fn optimize(&mut self, circuit: &Circuit<N>) -> QuantRS2Result<OptimizationReport> {
68 let initial_metrics = self.analyzer.analyze(circuit)?;
70
71 let optimized_circuit = self.pass_manager.run(circuit, &*self.cost_model)?;
73
74 let final_metrics = self.analyzer.analyze(&optimized_circuit)?;
76
77 Ok(OptimizationReport {
79 initial_metrics,
80 final_metrics,
81 applied_passes: self.pass_manager.get_applied_passes(),
82 })
83 }
84
85 pub fn add_pass(&mut self, pass: Box<dyn OptimizationPass>) {
87 self.pass_manager.add_pass(pass);
88 }
89
90 pub fn set_cost_model(&mut self, cost_model: Box<dyn CostModel>) {
92 self.cost_model = cost_model;
93 }
94
95 pub fn configure(&mut self, config: PassConfig) {
97 self.pass_manager.configure(config);
98 }
99}
100
101impl<const N: usize> Default for CircuitOptimizer2<N> {
102 fn default() -> Self {
103 Self::new()
104 }
105}