Skip to content

Commit aafd73c

Browse files
doug-qss2165
andauthored
feat: Add monomorphization and constant folding to QSystemPass (#730)
Closes #729 Note that constant folding is disabled by default as it currently does not work on modules. --------- Co-authored-by: Seyon Sivarajah <seyon.sivarajah@cambridgequantum.com>
1 parent 95090a2 commit aafd73c

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

tket2-hseries/src/lib.rs

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
//! Provides a preparation and validation workflow for Hugrs targeting
22
//! Quantinuum H-series quantum computers.
3+
use std::mem;
4+
35
use derive_more::{Display, Error, From};
46
use hugr::{
57
algorithms::{
6-
force_order,
8+
const_fold::{ConstFoldError, ConstantFoldPass},
9+
force_order, monomorphize, remove_polyfuncs,
710
validation::{ValidatePassError, ValidationLevel},
811
},
9-
hugr::{hugrmut::HugrMut, HugrError},
12+
hugr::HugrError,
13+
Hugr, HugrView,
1014
};
1115
use tket2::Tk2Op;
1216

@@ -26,9 +30,21 @@ pub mod lazify_measure;
2630
/// Returns an error if this cannot be done.
2731
///
2832
/// To construct a `QSystemPass` use [Default::default].
29-
#[derive(Debug, Clone, Copy, Default)]
33+
#[derive(Debug, Clone, Copy)]
3034
pub struct QSystemPass {
3135
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+
}
3248
}
3349

3450
#[derive(Error, Debug, Display, From)]
@@ -43,12 +59,28 @@ pub enum QSystemPassError {
4359
ForceOrderError(HugrError),
4460
/// An error from the component [LowerTket2ToQSystemPass] pass.
4561
LowerTk2Error(LowerTk2Error),
62+
/// An error from the component [ConstantFoldPass] pass.
63+
ConstantFoldError(ConstFoldError),
4664
}
4765

4866
impl QSystemPass {
49-
/// Run `QSystemPass` on the given [HugrMut]. `registry` is used for
67+
/// Run `QSystemPass` on the given [Hugr]. `registry` is used for
5068
/// 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+
}
5284
self.lower_tk2().run(hugr)?;
5385
self.lazify_measure().run(hugr)?;
5486
self.validation_level.run_validated_pass(hugr, |hugr, _| {
@@ -77,11 +109,29 @@ impl QSystemPass {
77109
LazifyMeasurePass::default().with_validation_level(self.validation_level)
78110
}
79111

112+
fn constant_fold(&self) -> ConstantFoldPass {
113+
ConstantFoldPass::default().validation_level(self.validation_level)
114+
}
115+
80116
/// Returns a new `QSystemPass` with the given [ValidationLevel].
81117
pub fn with_validation_level(mut self, level: ValidationLevel) -> Self {
82118
self.validation_level = level;
83119
self
84120
}
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+
}
85135
}
86136

87137
#[cfg(test)]

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)