Skip to content
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

Merged
merged 8 commits into from
Sep 13, 2023
Merged

feat: Add RewriteStrategies #99

merged 8 commits into from
Sep 13, 2023

Conversation

lmondada
Copy link
Contributor

  • build: Update to latest Hugr rev bc9692b
  • feat: Add RewriteStrategies

@lmondada lmondada requested a review from aborgna-q September 13, 2023 11:48
Copy link
Collaborator

@aborgna-q aborgna-q left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

@@ -0,0 +1,212 @@
//! Rewriting strategies for circuit optimisation.
//!
//! This module contains the [`RewriteStrategy`] trait, which is implemented by
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something missing here?

Copy link
Contributor Author

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
Comment on lines 65 to 67
// Safety: pointer casting is allowed as Subcircuit is transparently
// just SiblingSubgrah.
unsafe { &*(self as *const SiblingSubgraph as *const Subcircuit) }
Copy link
Collaborator

@aborgna-q aborgna-q Sep 13, 2023

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)
}

Copy link
Contributor Author

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.
Copy link
Collaborator

@aborgna-q aborgna-q Sep 13, 2023

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.

) -> Vec<Hugr>;
}

/// A rewrite strategy that applies as many rewrites as possible on one circuit.
Copy link
Collaborator

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

Suggested change
/// 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.

Copy link
Contributor Author

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.

.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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
dbg!(old_count, new_count);

@lmondada lmondada merged commit 54a7ad1 into main Sep 13, 2023
7 checks passed
@lmondada lmondada deleted the feat/rewrite-strategy branch September 13, 2023 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants