-
Notifications
You must be signed in to change notification settings - Fork 6
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
Changes from 3 commits
865afdb
ea37aa8
6f30b43
a4bab6e
649356f
e23b055
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from typing import no_type_check | ||
import pytket.circuit | ||
from tket2.circuit import Tk2Circuit | ||
|
||
import math | ||
|
@@ -9,8 +10,46 @@ | |
from guppylang.prelude.builtins import py | ||
from guppylang.prelude.quantum import measure, phased_x, qubit, rz, zz_max | ||
|
||
import pytket | ||
|
||
def test_load_compiled_module(): | ||
|
||
def test_load_pure_circuit(): | ||
module = GuppyModule("test") | ||
module.load(quantum) | ||
|
||
@guppy(module) | ||
@no_type_check | ||
def my_func( | ||
q0: qubit, | ||
q1: qubit, | ||
) -> tuple[qubit, qubit]: # pragma: no cover | ||
q0 = phased_x(q0, py(math.pi / 2), py(-math.pi / 2)) | ||
q0 = rz(q0, py(math.pi)) | ||
q1 = phased_x(q1, py(math.pi / 2), py(-math.pi / 2)) | ||
q1 = rz(q1, py(math.pi)) | ||
q0, q1 = zz_max(q0, q1) | ||
q0 = rz(q0, py(math.pi)) | ||
q1 = rz(q1, py(math.pi)) | ||
return (q0, q1) | ||
|
||
# Compile the module | ||
hugr = module.compile() | ||
json = hugr.to_raw().to_json() | ||
|
||
# Load it from the JSON string | ||
circ = Tk2Circuit.from_guppy_json(json, "my_func") | ||
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. these three lines probably belong in a function, in _circuit.py orsomething 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. There's a couple reasons for keeping this explicit for the moment: Without CQCL/guppylang#246, we need to keep an explicit reference to the hugr if we want to create multiple circuits from the same module. from_guppy(my_func: guppy.Definition) -> Tk2Circuit But more importantly, 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 added a utility functions for testing, but we won't be able to expose it in the tket2 api. 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. makes sense, thanks for the explanation |
||
assert circ.num_operations() == 7 | ||
|
||
# Convert into a pytket Circuit | ||
tk1 = circ.to_tket1() | ||
assert tk1.n_gates == 7 | ||
assert tk1.n_qubits == 2 | ||
|
||
gates = list(tk1) | ||
assert gates[4].op.type == pytket.circuit.OpType.ZZMax | ||
|
||
|
||
def test_load_hybrid_circuit(): | ||
module = GuppyModule("test") | ||
module.load(quantum) | ||
|
||
|
@@ -19,7 +58,7 @@ def test_load_compiled_module(): | |
def my_func( | ||
q0: qubit, | ||
q1: qubit, | ||
) -> tuple[bool,]: | ||
) -> tuple[bool,]: # pragma: no cover | ||
q0 = phased_x(q0, py(math.pi / 2), py(-math.pi / 2)) | ||
q0 = rz(q0, py(math.pi)) | ||
q1 = phased_x(q1, py(math.pi / 2), py(-math.pi / 2)) | ||
|
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, | ||
} |
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.