Skip to content

Commit

Permalink
feat!: new CircuitMatch struct (#55)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `CircuitMatcher` no longer implements `PortMatcher`. `find_matches`
and `find_rooted_matches` now return `CircuitMatch`es.
  • Loading branch information
lmondada authored Aug 29, 2023
1 parent 781074f commit 991c9dd
Show file tree
Hide file tree
Showing 7 changed files with 416 additions and 139 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ typetag = "0.2.8"
itertools = "0.11.0"
petgraph = { version = "0.6.3", default-features = false }
serde_yaml = "0.9.22"
portmatching = { version = "0.2.0", optional = true, features = ["serde"]}
# portmatching = { version = "0.2.0", optional = true, features = ["serde"]}
portmatching = { optional = true, git = "https://github.com/lmondada/portmatching", rev = "219f53d" }
derive_more = "0.99.17"
quantinuum-hugr = { workspace = true }
portgraph = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion pyrs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ tket-json-rs = { git = "https://github.com/CQCL/tket-json-rs", rev="619db15d3",
] }
quantinuum-hugr = { workspace = true }
portgraph = { workspace = true, features = ["pyo3"] }
pyo3 = { workspace = true, features = ["extension-module"] }
pyo3 = { workspace = true, features = ["extension-module"] }
24 changes: 11 additions & 13 deletions pyrs/test/test_portmatching.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@ def test_simple_matching():
assert len(matcher.find_matches(c)) == 2


# TODO: convexity
# def test_non_convex_pattern():
# """ two-qubit circuits can't match three-qb ones """
# p1 = patterns.CircuitPattern(Circuit(3).CX(0, 1).CX(1, 2))
# matcher = patterns.CircuitMatcher(iter([p1]))
def test_non_convex_pattern():
""" two-qubit circuits can't match three-qb ones """
p1 = patterns.CircuitPattern(Circuit(3).CX(0, 1).CX(1, 2))
matcher = patterns.CircuitMatcher(iter([p1]))

# c = Circuit(2).CX(0, 1).CX(1, 0)
# assert len(matcher.find_matches(c)) == 0
c = Circuit(2).CX(0, 1).CX(1, 0)
assert len(matcher.find_matches(c)) == 0

# c = Circuit(2).CX(0, 1).CX(1, 0).CX(1, 2)
# assert len(matcher.find_matches(c)) == 0
c = Circuit(3).CX(0, 1).CX(1, 0).CX(1, 2)
assert len(matcher.find_matches(c)) == 0

# c = Circuit(2).H(0).CX(0, 1).CX(1, 0).CX(0, 2)
# assert len(matcher.find_matches(c)) == 1
c = Circuit(3).H(0).CX(0, 1).CX(1, 0).CX(0, 2)
assert len(matcher.find_matches(c)) == 1


def test_larger_matching():
Expand All @@ -42,5 +41,4 @@ def test_larger_matching():

matcher = patterns.CircuitMatcher(iter([p1, p2, p3, p4]))

# TODO: convexity
assert len(matcher.find_matches(c)) == 8
assert len(matcher.find_matches(c)) == 6
9 changes: 8 additions & 1 deletion src/portmatching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

pub mod matcher;
mod optype;
pub mod pattern;
#[cfg(feature = "pyo3")]
mod pyo3;

pub use matcher::{CircuitMatcher, CircuitPattern};
pub use matcher::{CircuitMatch, CircuitMatcher, CircuitRewrite};
pub use pattern::CircuitPattern;

use hugr::Port;
use optype::MatchOp;

type PEdge = (Port, Port);
type PNode = MatchOp;
Loading

0 comments on commit 991c9dd

Please sign in to comment.