-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: guppy → pytket conversion (#407)
Adds support for converting **flat** **pure** functions defined in guppy into pytket circuits. See the example in `test_guppy.py`. This PR just adds a `lower_to_pytket` pass that currently only runs the tuple erasure from #406.
- Loading branch information
Showing
10 changed files
with
165 additions
and
32 deletions.
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