1
1
//! Provides a preparation and validation workflow for Hugrs targeting
2
2
//! Quantinuum H-series quantum computers.
3
+ use std:: mem;
4
+
3
5
use derive_more:: { Display , Error , From } ;
4
6
use hugr:: {
5
7
algorithms:: {
6
- force_order,
8
+ const_fold:: { ConstFoldError , ConstantFoldPass } ,
9
+ force_order, monomorphize, remove_polyfuncs,
7
10
validation:: { ValidatePassError , ValidationLevel } ,
8
11
} ,
9
- hugr:: { hugrmut:: HugrMut , HugrError } ,
12
+ hugr:: HugrError ,
13
+ Hugr , HugrView ,
10
14
} ;
11
15
use tket2:: Tk2Op ;
12
16
@@ -26,9 +30,21 @@ pub mod lazify_measure;
26
30
/// Returns an error if this cannot be done.
27
31
///
28
32
/// To construct a `QSystemPass` use [Default::default].
29
- #[ derive( Debug , Clone , Copy , Default ) ]
33
+ #[ derive( Debug , Clone , Copy ) ]
30
34
pub struct QSystemPass {
31
35
validation_level : ValidationLevel ,
36
+ constant_fold : bool ,
37
+ monomorphize : bool ,
38
+ }
39
+
40
+ impl Default for QSystemPass {
41
+ fn default ( ) -> Self {
42
+ Self {
43
+ validation_level : ValidationLevel :: default ( ) ,
44
+ constant_fold : false ,
45
+ monomorphize : true ,
46
+ }
47
+ }
32
48
}
33
49
34
50
#[ derive( Error , Debug , Display , From ) ]
@@ -43,12 +59,28 @@ pub enum QSystemPassError {
43
59
ForceOrderError ( HugrError ) ,
44
60
/// An error from the component [LowerTket2ToQSystemPass] pass.
45
61
LowerTk2Error ( LowerTk2Error ) ,
62
+ /// An error from the component [ConstantFoldPass] pass.
63
+ ConstantFoldError ( ConstFoldError ) ,
46
64
}
47
65
48
66
impl QSystemPass {
49
- /// Run `QSystemPass` on the given [HugrMut ]. `registry` is used for
67
+ /// Run `QSystemPass` on the given [Hugr ]. `registry` is used for
50
68
/// validation, if enabled.
51
- pub fn run ( & self , hugr : & mut impl HugrMut ) -> Result < ( ) , QSystemPassError > {
69
+ pub fn run ( & self , hugr : & mut Hugr ) -> Result < ( ) , QSystemPassError > {
70
+ if self . monomorphize {
71
+ self . validation_level . run_validated_pass ( hugr, |hugr, _| {
72
+ let mut owned_hugr = Hugr :: default ( ) ;
73
+ mem:: swap ( & mut owned_hugr, hugr) ;
74
+ owned_hugr = remove_polyfuncs ( monomorphize ( owned_hugr) ) ;
75
+ mem:: swap ( & mut owned_hugr, hugr) ;
76
+
77
+ Ok :: < _ , QSystemPassError > ( ( ) )
78
+ } ) ?;
79
+ }
80
+
81
+ if self . constant_fold {
82
+ self . constant_fold ( ) . run ( hugr) ?;
83
+ }
52
84
self . lower_tk2 ( ) . run ( hugr) ?;
53
85
self . lazify_measure ( ) . run ( hugr) ?;
54
86
self . validation_level . run_validated_pass ( hugr, |hugr, _| {
@@ -77,11 +109,29 @@ impl QSystemPass {
77
109
LazifyMeasurePass :: default ( ) . with_validation_level ( self . validation_level )
78
110
}
79
111
112
+ fn constant_fold ( & self ) -> ConstantFoldPass {
113
+ ConstantFoldPass :: default ( ) . validation_level ( self . validation_level )
114
+ }
115
+
80
116
/// Returns a new `QSystemPass` with the given [ValidationLevel].
81
117
pub fn with_validation_level ( mut self , level : ValidationLevel ) -> Self {
82
118
self . validation_level = level;
83
119
self
84
120
}
121
+
122
+ /// Returns a new `QSystemPass` with constant folding enabled according to
123
+ /// `constant_fold`.
124
+ pub fn with_constant_fold ( mut self , constant_fold : bool ) -> Self {
125
+ self . constant_fold = constant_fold;
126
+ self
127
+ }
128
+
129
+ /// Returns a new `QSystemPass` with monomorphization enabled according to
130
+ /// `monomorphize`.
131
+ pub fn with_monormophize ( mut self , monomorphize : bool ) -> Self {
132
+ self . monomorphize = monomorphize;
133
+ self
134
+ }
85
135
}
86
136
87
137
#[ cfg( test) ]
0 commit comments