-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add RewriteStrategies #99
Conversation
lmondada
commented
Sep 12, 2023
- build: Update to latest Hugr rev bc9692b
- feat: Add RewriteStrategies
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome!
src/rewrite/strategy.rs
Outdated
@@ -0,0 +1,212 @@ | |||
//! Rewriting strategies for circuit optimisation. | |||
//! | |||
//! This module contains the [`RewriteStrategy`] trait, which is implemented by |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something missing here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed!
src/rewrite.rs
Outdated
// Safety: pointer casting is allowed as Subcircuit is transparently | ||
// just SiblingSubgrah. | ||
unsafe { &*(self as *const SiblingSubgraph as *const Subcircuit) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uhm. This is ok, but I'd propose using bytemuck
instead (it does essentially the same, but we sideload the unsafe to a really well tested crate that does extra checks).
#[derive(TransparentWrapper)]
#[repr(transparent)]
pub struct Subcircuit {...}
fn as_ref(&self) -> &Subcircuit {
Subcircuit::wrap_ref(self)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, I quickly googled for a crate that would do this but missed it, thanks for the pointer!
/// to a circuit according to a strategy, returning a set of possible | ||
/// optimised circuits. | ||
pub trait RewriteStrategy { | ||
/// Apply a set of rewrites to a circuit. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clarify that each returned Hugr
may have multiple non-overlapping rewrites
elements applied to it.
src/rewrite/strategy.rs
Outdated
) -> Vec<Hugr>; | ||
} | ||
|
||
/// A rewrite strategy that applies as many rewrites as possible on one circuit. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd add something like
/// A rewrite strategy that applies as many rewrites as possible on one circuit. | |
/// A rewrite strategy that applies as many non-overlapping rewrites as possible | |
/// on a single instance of the circuit. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've expanded the docs a bit, hopefully it is clearer now.
src/rewrite/strategy.rs
Outdated
.filter(|rw| { | ||
let old_count = rw.subcircuit().node_count() as f64; | ||
let new_count = rw.replacement().num_gates() as f64; | ||
dbg!(old_count, new_count); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dbg!(old_count, new_count); |