-
Notifications
You must be signed in to change notification settings - Fork 7
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: guppy → pytket conversion #407
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
865afdb
feat: convert guppy programs into pytket circuits
aborgna-q ea37aa8
Ignore coverage in guppy function
aborgna-q 6f30b43
Test alternative no-coverage marker
aborgna-q a4bab6e
Convert between radians and half-turns when encoding pytket circs
aborgna-q 649356f
`guppy_to_circuit` test helper
aborgna-q e23b055
Fix mypy errors
aborgna-q File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from guppylang.definition.function import RawFunctionDef | ||
|
||
from tket2.circuit import Tk2Circuit | ||
|
||
|
||
def guppy_to_circuit(func_def: RawFunctionDef) -> Tk2Circuit: | ||
"""Convert a Guppy function definition to a `Tk2Circuit`.""" | ||
module = func_def.id.module | ||
assert module is not None, "Function definition must belong to a module" | ||
|
||
hugr = module.compile() | ||
assert hugr is not None, "Module must be compilable" | ||
|
||
json = hugr.to_raw().to_json() | ||
return Tk2Circuit.from_guppy_json(json, func_def.name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//! This module contains routines needed for normalizing a circuit | ||
//! into a form that can be encoded as a pytket legacy circuit. | ||
//! | ||
//! This is a best-effort attempt, and may not always succeed. | ||
|
||
use itertools::Itertools; | ||
|
||
use crate::serialize::pytket::OpConvertError; | ||
use crate::Circuit; | ||
|
||
use super::find_tuple_unpack_rewrites; | ||
|
||
/// Try to lower a circuit to a form that can be encoded as a pytket legacy circuit. | ||
pub fn lower_to_pytket(circ: &Circuit) -> Result<Circuit, PytketLoweringError> { | ||
let mut circ = circ | ||
.extract_dfg() | ||
.map_err(|_| PytketLoweringError::NonLocalOperations)?; | ||
|
||
// Remove sequences of tuple pack-unpack operations, | ||
// typically generated by guppy. | ||
let rewrites = find_tuple_unpack_rewrites(&circ).collect_vec(); | ||
for rewrite in rewrites { | ||
rewrite.apply(&mut circ).unwrap(); | ||
} | ||
|
||
Ok(circ) | ||
} | ||
|
||
/// Errors that can occur during the lowering process. | ||
#[derive(Clone, PartialEq, Debug, thiserror::Error)] | ||
pub enum PytketLoweringError { | ||
/// An error occurred during the conversion of an operation. | ||
#[error("operation conversion error: {0}")] | ||
OpConversionError(#[from] OpConvertError), | ||
/// The circuit is not fully-contained in a region. | ||
/// Function calls are not supported. | ||
#[error("Non-local operations found. Function calls are not supported.")] | ||
NonLocalOperations, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,8 +108,8 @@ fn circ_add_angles_constants() -> Circuit { | |
|
||
let qb = h.input_wires().next().unwrap(); | ||
|
||
let point2 = h.add_load_value(ConstF64::new(0.2)); | ||
let point3 = h.add_load_value(ConstF64::new(0.3)); | ||
let point2 = h.add_load_value(ConstF64::new(0.2 * std::f64::consts::PI)); | ||
let point3 = h.add_load_value(ConstF64::new(0.3 * std::f64::consts::PI)); | ||
Comment on lines
+111
to
+112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was surprised this didn't cause precision errors. |
||
let point5 = h | ||
.add_dataflow_op(Tk2Op::AngleAdd, [point2, point3]) | ||
.unwrap() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tket1 has angles in fractions of pi, are these angles converted correctly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. That was missing in the encoder/decoder.
Now we translate between radians and half-turns when converting hugrs to pytket circuits.