Skip to content

Commit

Permalink
Drop the lifetime from Circuit
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Sep 7, 2023
1 parent e6b9179 commit 772f1ba
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 31 deletions.
20 changes: 11 additions & 9 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ pub use hugr::{Node, Port, Wire};
// - Vertical slice iterator
// - Gate count map
// - Depth
pub trait Circuit<'circ>: HugrView {
pub trait Circuit: HugrView {
/// An iterator over the commands in the circuit.
type Commands: Iterator<Item = Command>;
type Commands<'a>: Iterator<Item = Command>
where
Self: 'a;

/// An iterator over the commands applied to an unit.
type UnitCommands: Iterator<Item = Command>;
Expand Down Expand Up @@ -65,10 +67,10 @@ pub trait Circuit<'circ>: HugrView {
/// Returns all the commands in the circuit, in some topological order.
///
/// Ignores the Input and Output nodes.
fn commands(&'circ self) -> Self::Commands;
fn commands(&self) -> Self::Commands<'_>;

/// Returns all the commands applied to the given unit, in order.
fn unit_commands(&'circ self) -> Self::UnitCommands;
fn unit_commands(&self) -> Self::UnitCommands;

/// Returns the [`NodeType`] of a command.
fn command_nodetype(&self, command: &Command) -> &NodeType {
Expand All @@ -84,11 +86,11 @@ pub trait Circuit<'circ>: HugrView {
fn num_gates(&self) -> usize;
}

impl<'circ, T> Circuit<'circ> for T
impl<T> Circuit for T
where
T: 'circ + HugrView,
T: HugrView,
{
type Commands = CommandIterator<'circ, T>;
type Commands<'a> = CommandIterator<'a, T> where Self: 'a;
type UnitCommands = std::iter::Empty<Command>;

#[inline]
Expand Down Expand Up @@ -126,12 +128,12 @@ where
}
}

fn commands(&'circ self) -> Self::Commands {
fn commands(&self) -> Self::Commands<'_> {
// Traverse the circuit in topological order.
CommandIterator::new(self)
}

fn unit_commands(&'circ self) -> Self::UnitCommands {
fn unit_commands(&self) -> Self::UnitCommands {
// TODO Can we associate linear i/o with the corresponding unit without
// doing the full toposort?
unimplemented!()
Expand Down
6 changes: 3 additions & 3 deletions src/circuit/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub struct CommandIterator<'circ, Circ> {

impl<'circ, Circ> CommandIterator<'circ, Circ>
where
Circ: Circuit<'circ>,
Circ: Circuit,
{
/// Create a new iterator over the commands of a circuit.
pub(super) fn new(circ: &'circ Circ) -> Self {
Expand Down Expand Up @@ -157,7 +157,7 @@ where

impl<'circ, Circ> Iterator for CommandIterator<'circ, Circ>
where
Circ: Circuit<'circ>,
Circ: Circuit,
{
type Item = Command;

Expand All @@ -181,7 +181,7 @@ where
}
}

impl<'circ, Circ> FusedIterator for CommandIterator<'circ, Circ> where Circ: Circuit<'circ> {}
impl<'circ, Circ> FusedIterator for CommandIterator<'circ, Circ> where Circ: Circuit {}

#[cfg(test)]
mod test {
Expand Down
4 changes: 2 additions & 2 deletions src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub trait TKETDecode: Sized {
/// Convert the serialized circuit to a [`Hugr`].
fn decode(self) -> Result<Hugr, Self::DecodeError>;
/// Convert a [`Hugr`] to a new serialized circuit.
fn encode<'circ>(circuit: &'circ impl Circuit<'circ>) -> Result<Self, Self::EncodeError>;
fn encode(circuit: &impl Circuit) -> Result<Self, Self::EncodeError>;
}

impl TKETDecode for SerialCircuit {
Expand All @@ -60,7 +60,7 @@ impl TKETDecode for SerialCircuit {
Ok(decoder.finish())
}

fn encode<'circ>(circ: &'circ impl Circuit<'circ>) -> Result<Self, Self::EncodeError> {
fn encode(circ: &impl Circuit) -> Result<Self, Self::EncodeError> {
let mut encoder = JsonEncoder::new(circ);
for com in circ.commands() {
let optype = circ.command_optype(&com);
Expand Down
2 changes: 1 addition & 1 deletion src/json/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub(super) struct JsonEncoder {

impl JsonEncoder {
/// Create a new [`JsonEncoder`] from a [`Circuit`].
pub fn new<'circ>(circ: &impl Circuit<'circ>) -> Self {
pub fn new(circ: &impl Circuit) -> Self {
let name = circ.name().map(str::to_string);

// Compute the linear qubit and bit registers. Each one have independent
Expand Down
16 changes: 8 additions & 8 deletions src/portmatching/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub struct CircuitMatch<'a, 'p, C> {
pub(super) root: Node,
}

impl<'a, 'p, C: Circuit<'a> + Clone> CircuitMatch<'a, 'p, C> {
impl<'a, 'p, C: Circuit + Clone> CircuitMatch<'a, 'p, C> {
/// Create a pattern match from the image of a pattern root.
///
/// This checks at construction time that the match is convex. This will
Expand Down Expand Up @@ -132,7 +132,7 @@ impl<'a, 'p, C: Circuit<'a> + Clone> CircuitMatch<'a, 'p, C> {
}
}

impl<'a, 'p, C: Circuit<'a>> Debug for CircuitMatch<'a, 'p, C> {
impl<'a, 'p, C: Circuit> Debug for CircuitMatch<'a, 'p, C> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CircuitMatch")
.field("root", &self.root)
Expand Down Expand Up @@ -187,7 +187,7 @@ impl CircuitMatcher {
}

/// Find all convex pattern matches in a circuit.
pub fn find_matches<'a, 'm, C: Circuit<'a> + Clone>(
pub fn find_matches<'a, 'm, C: Circuit + Clone>(
&'m self,
circuit: &'a C,
) -> Vec<CircuitMatch<'a, 'm, C>> {
Expand All @@ -199,7 +199,7 @@ impl CircuitMatcher {
}

/// Find all convex pattern matches in a circuit rooted at a given node.
fn find_rooted_matches<'a, 'm, C: Circuit<'a> + Clone>(
fn find_rooted_matches<'a, 'm, C: Circuit + Clone>(
&'m self,
circ: &'a C,
root: Node,
Expand Down Expand Up @@ -329,8 +329,8 @@ fn compatible_offsets((_, pout): &(Port, Port), (pin, _): &(Port, Port)) -> bool
}

/// Check if an edge `e` is valid in a portgraph `g` without weights.
pub(crate) fn validate_unweighted_edge<'circ>(
circ: &impl Circuit<'circ>,
pub(crate) fn validate_unweighted_edge(
circ: &impl Circuit,
) -> impl for<'a> Fn(Node, &'a PEdge) -> Option<Node> + '_ {
move |src, &(src_port, tgt_port)| {
let (next_node, _) = circ
Expand All @@ -341,8 +341,8 @@ pub(crate) fn validate_unweighted_edge<'circ>(
}

/// Check if a node `n` is valid in a weighted portgraph `g`.
pub(crate) fn validate_weighted_node<'circ>(
circ: &impl Circuit<'circ>,
pub(crate) fn validate_weighted_node(
circ: &impl Circuit,
) -> impl for<'a> Fn(Node, &PNode) -> bool + '_ {
move |v, prop| {
let v_weight = MatchOp::try_from(circ.get_optype(v).clone());
Expand Down
10 changes: 2 additions & 8 deletions src/portmatching/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ pub struct CircuitPattern {

impl CircuitPattern {
/// Construct a pattern from a circuit.
pub fn try_from_circuit<'circ, C: Circuit<'circ>>(
circuit: &'circ C,
) -> Result<Self, InvalidPattern> {
pub fn try_from_circuit<C: Circuit>(circuit: &C) -> Result<Self, InvalidPattern> {
if circuit.num_gates() == 0 {
return Err(InvalidPattern::EmptyCircuit);
}
Expand Down Expand Up @@ -74,11 +72,7 @@ impl CircuitPattern {
}

/// Compute the map from pattern nodes to circuit nodes in `circ`.
pub fn get_match_map<'a, C: Circuit<'a>>(
&self,
root: Node,
circ: &C,
) -> Option<HashMap<Node, Node>> {
pub fn get_match_map<C: Circuit>(&self, root: Node, circ: &C) -> Option<HashMap<Node, Node>> {
let single_matcher = SinglePatternMatcher::from_pattern(self.pattern.clone());
single_matcher
.get_match_map(
Expand Down

0 comments on commit 772f1ba

Please sign in to comment.