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: bindings for circuit cost and hash #252

Merged
merged 11 commits into from
Nov 23, 2023
Prev Previous commit
Next Next commit
feat: Add Tk2Circuit::__copy__ and deepcopy
aborgna-q committed Nov 20, 2023
commit 60d9c12bed615d984fc01a4b7b4df3a80a832748
12 changes: 11 additions & 1 deletion tket2-py/src/circuit/convert.rs
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@ impl Tk2Circuit {
}

/// Apply a rewrite on the circuit.
pub fn apply_match(&mut self, rw: PyCircuitRewrite) {
pub fn apply_rewrite(&mut self, rw: PyCircuitRewrite) {
rw.rewrite.apply(&mut self.hugr).expect("Apply error.");
}

@@ -107,6 +107,16 @@ impl Tk2Circuit {
pub fn __hash__(&self) -> isize {
self.hash() as isize
}

/// Copy the circuit.
pub fn __copy__(&self) -> PyResult<Self> {
Ok(self.clone())
Copy link
Member

Choose a reason for hiding this comment

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

OOI is it even possible to do something like the python shallow copy here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not really, unless you add Arcs everywhere.

}

/// Copy the circuit.
pub fn __deepcopy__(&self, _memo: Py<PyAny>) -> PyResult<Self> {
Ok(self.clone())
}
}
impl Tk2Circuit {
/// Tries to extract a Tk2Circuit from a python object.
4 changes: 2 additions & 2 deletions tket2-py/test/test_pass.py
Original file line number Diff line number Diff line change
@@ -58,7 +58,7 @@ def test_cx_rule():

mtch = matcher.find_match(c)

c.apply_match(mtch)
c.apply_rewrite(mtch)

out = c.to_tket1()

@@ -75,7 +75,7 @@ def test_multiple_rules():
match_count = 0
while match := matcher.find_match(circ):
match_count += 1
circ.apply_match(match)
circ.apply_rewrite(match)

assert match_count == 3