Skip to content

Commit

Permalink
feat: Add lhs and rhs bindings to Rule (#440)
Browse files Browse the repository at this point in the history
Just a couple of helper methods I found useful for debugging.
  • Loading branch information
aborgna-q authored Jul 2, 2024
1 parent bc89180 commit 49b1c89
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
18 changes: 18 additions & 0 deletions tket2-py/src/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ impl Rule {

Ok(Rule([l.circ, r.circ]))
}

/// The left hand side of the rule.
///
/// This is the pattern that will be matched against the target circuit.
fn lhs(&self) -> Tk2Circuit {
Tk2Circuit {
circ: self.0[0].clone(),
}
}

/// The right hand side of the rule.
///
/// This is the replacement that will be applied to the target circuit.
fn rhs(&self) -> Tk2Circuit {
Tk2Circuit {
circ: self.0[1].clone(),
}
}
}
#[pyclass]
struct RuleMatcher {
Expand Down
20 changes: 16 additions & 4 deletions tket2-py/tket2/_tket2/pattern.pyi
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
from typing import Iterator
from .circuit import Node, Tk2Circuit
from .rewrite import CircuitRewrite
from pytket._tket.circuit import Circuit
from pytket._tket.circuit import Circuit as Tk1Circuit

class Rule:
"""A rewrite rule defined by a left hand side and right hand side of an equation."""

def __init__(
self,
l: Circuit | Tk2Circuit, # noqa: E741
r: Circuit | Tk2Circuit,
l: Tk1Circuit | Tk2Circuit, # noqa: E741
r: Tk1Circuit | Tk2Circuit,
) -> None:
"""Create a new rewrite rule."""

def lhs(self) -> Tk2Circuit:
"""Get the left hand side of the rule.
This is the pattern that is matched in the circuit.
"""

def rhs(self) -> Tk2Circuit:
"""Get the right hand side of the rule.
This is the pattern that is replaced in the circuit.
"""

class RuleMatcher:
"""A matcher for multiple rewrite rule."""

Expand All @@ -28,7 +40,7 @@ class RuleMatcher:
class CircuitPattern:
"""A pattern that matches a circuit exactly."""

def __init__(self, circ: Circuit | Tk2Circuit) -> None:
def __init__(self, circ: Tk1Circuit | Tk2Circuit) -> None:
"""Create a new circuit pattern."""

class PatternMatcher:
Expand Down

0 comments on commit 49b1c89

Please sign in to comment.