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 #[must_use]
41 pub fn new() -> Self {
42 Self {
43 pass_manager: PassManager::default(),
44 cost_model: Box::new(AbstractCostModel::default()),
45 analyzer: CircuitAnalyzer::new(),
46 }
47 }
48
49 #[must_use]
51 pub fn with_level(level: OptimizationLevel) -> Self {
52 Self {
53 pass_manager: PassManager::with_level(level),
54 cost_model: Box::new(AbstractCostModel::default()),
55 analyzer: CircuitAnalyzer::new(),
56 }
57 }
58
59 #[must_use]
61 pub fn for_hardware(hardware: &str) -> Self {
62 Self {
63 pass_manager: PassManager::for_hardware(hardware),
64 cost_model: Box::new(HardwareCostModel::for_backend(hardware)),
65 analyzer: CircuitAnalyzer::new(),
66 }
67 }
68
69 pub fn optimize(&mut self, circuit: &Circuit<N>) -> QuantRS2Result<OptimizationReport> {
71 let initial_metrics = self.analyzer.analyze(circuit)?;
73
74 let optimized_circuit = self.pass_manager.run(circuit, &*self.cost_model)?;
76
77 let final_metrics = self.analyzer.analyze(&optimized_circuit)?;
79
80 Ok(OptimizationReport {
82 initial_metrics,
83 final_metrics,
84 applied_passes: self.pass_manager.get_applied_passes(),
85 })
86 }
87
88 pub fn add_pass(&mut self, pass: Box<dyn OptimizationPass>) {
90 self.pass_manager.add_pass(pass);
91 }
92
93 pub fn set_cost_model(&mut self, cost_model: Box<dyn CostModel>) {
95 self.cost_model = cost_model;
96 }
97
98 pub fn configure(&mut self, config: PassConfig) {
100 self.pass_manager.configure(config);
101 }
102}
103
104impl<const N: usize> Default for CircuitOptimizer2<N> {
105 fn default() -> Self {
106 Self::new()
107 }
108}