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: Use tket1 and tket2 circuits interchangeably everywhere #243

Merged
merged 5 commits into from
Nov 15, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update docs
aborgna-q committed Nov 15, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 214e775cd1becb206b6b6efb7a698323017cb75c
20 changes: 16 additions & 4 deletions tket2-py/src/circuit/convert.rs
Original file line number Diff line number Diff line change
@@ -42,6 +42,8 @@ impl Tk2Circuit {
}

/// Encode the circuit as a HUGR json string.
//
// TODO: Bind a messagepack encoder/decoder too.
pub fn to_hugr_json(&self) -> PyResult<String> {
Ok(serde_json::to_string(&self.hugr).unwrap())
Copy link
Member

Choose a reason for hiding this comment

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

a note that previously we have used messagepack (and the behaviour between json and messagepack is not always identical), but I'm ok avoiding the extra python dependency here for now - at least until the serialized format gets a bit more stable (hopefully soon)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added a TODO

}
@@ -100,7 +102,9 @@ impl CircuitType {
}
}

/// Apply a fallible function expecting a hugr on a pytket circuit.
/// Apply a fallible function expecting a hugr on a python circuit.
///
/// This method supports both `pytket.Circuit` and `Tk2Circuit` python objects.
pub fn try_with_hugr<T, E, F>(circ: &PyAny, f: F) -> PyResult<T>
where
E: Into<PyErr>,
@@ -118,15 +122,20 @@ where
(f)(hugr, typ).map_err(|e| e.into())
}

/// Apply a function expecting a hugr on a pytket circuit.
/// Apply a function expecting a hugr on a python circuit.
///
/// This method supports both `pytket.Circuit` and `Tk2Circuit` python objects.
pub fn with_hugr<T, F>(circ: &PyAny, f: F) -> PyResult<T>
where
F: FnOnce(Hugr, CircuitType) -> T,
{
try_with_hugr(circ, |hugr, typ| Ok::<T, PyErr>((f)(hugr, typ)))
}

/// Apply a hugr-to-hugr function on a pytket circuit, and return the modified circuit.
/// Apply a fallible hugr-to-hugr function on a python circuit, and return the modified circuit.
///
/// This method supports both `pytket.Circuit` and `Tk2Circuit` python objects.
/// The returned Hugr is converted to the matching python object.
pub fn try_update_hugr<E, F>(circ: &PyAny, f: F) -> PyResult<&PyAny>
where
E: Into<PyErr>,
@@ -139,7 +148,10 @@ where
})
}

/// Apply a hugr-to-hugr function on a pytket circuit, and return the modified circuit.
/// Apply a hugr-to-hugr function on a python circuit, and return the modified circuit.
///
/// This method supports both `pytket.Circuit` and `Tk2Circuit` python objects.
/// The returned Hugr is converted to the matching python object.
pub fn update_hugr<F>(circ: &PyAny, f: F) -> PyResult<&PyAny>
where
F: FnOnce(Hugr, CircuitType) -> Hugr,