Skip to content

Commit 6e1af12

Browse files
tatiana-sacl-cqc
andauthored
feat: Add guppy extension with drop operation (#962)
Closes #946 --------- Co-authored-by: Alan Lawrence <alan.lawrence@cambridgequantum.com>
1 parent 6017450 commit 6e1af12

File tree

7 files changed

+107
-0
lines changed

7 files changed

+107
-0
lines changed

tket2-exts/src/tket2_exts/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ def debug() -> Extension:
2121
return load_extension("tket2.debug")
2222

2323

24+
@functools.cache
25+
def guppy() -> Extension:
26+
return load_extension("tket2.guppy")
27+
28+
2429
@functools.cache
2530
def rotation() -> Extension:
2631
return load_extension("tket2.rotation")
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"version": "0.1.0",
3+
"name": "tket2.guppy",
4+
"types": {},
5+
"operations": {
6+
"drop": {
7+
"extension": "tket2.guppy",
8+
"name": "drop",
9+
"description": "Drop the input wire. Applicable to guppy affine types only.",
10+
"signature": {
11+
"params": [
12+
{
13+
"tp": "Type",
14+
"b": "A"
15+
}
16+
],
17+
"body": {
18+
"input": [
19+
{
20+
"t": "V",
21+
"i": 0,
22+
"b": "A"
23+
}
24+
],
25+
"output": []
26+
}
27+
},
28+
"binary": false
29+
}
30+
}
31+
}

tket2-hseries/src/bin/tket2-hseries.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ fn main() -> Result<()> {
1313
tket2::extension::rotation::ROTATION_EXTENSION.to_owned(),
1414
tket2::extension::bool::BOOL_EXTENSION.to_owned(),
1515
tket2::extension::debug::DEBUG_EXTENSION.to_owned(),
16+
tket2::extension::guppy::GUPPY_EXTENSION.to_owned(),
1617
tket2_hseries::extension::qsystem::EXTENSION.to_owned(),
1718
tket2_hseries::extension::futures::EXTENSION.to_owned(),
1819
tket2_hseries::extension::random::EXTENSION.to_owned(),

tket2-py/test/test_exts.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from tket2.extensions import opaque_bool # noqa: F401
22
from tket2.extensions import debug # noqa: F401
33
from tket2.extensions import rotation # noqa: F401
4+
from tket2.extensions import guppy # noqa: F401
45
from tket2.extensions import futures # noqa: F401
56
from tket2.extensions import qsystem # noqa: F401
67
from tket2.extensions import qsystem_random # noqa: F401

tket2-py/tket2/extensions/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from tket2_exts import (
22
opaque_bool,
33
debug,
4+
guppy,
45
rotation,
56
futures,
67
qsystem,
@@ -15,6 +16,7 @@
1516
__all__ = [
1617
"debug",
1718
"opaque_bool",
19+
"guppy",
1820
"rotation",
1921
"futures",
2022
"qsystem",

tket2/src/extension.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ use smol_str::SmolStr;
2222
pub mod bool;
2323
/// Definition for debug ops.
2424
pub mod debug;
25+
/// Definition for ops used by Guppy.
26+
pub mod guppy;
2527
/// Definition for Angle ops and types.
2628
pub mod rotation;
2729
pub mod sympy;
@@ -64,6 +66,7 @@ pub(crate) static ref REGISTRY: ExtensionRegistry = ExtensionRegistry::new(
6466
TKET2_EXTENSION.to_owned(),
6567
bool::BOOL_EXTENSION.to_owned(),
6668
debug::DEBUG_EXTENSION.to_owned(),
69+
guppy::GUPPY_EXTENSION.to_owned(),
6770
rotation::ROTATION_EXTENSION.to_owned()
6871
]));
6972

tket2/src/extension/guppy.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//! This module defines a Hugr extension for operations to be used in Guppy.
2+
use std::sync::Arc;
3+
4+
use hugr::{
5+
extension::{ExtensionId, Version},
6+
hugr::IdentList,
7+
type_row,
8+
types::{FuncValueType, PolyFuncTypeRV, Type, TypeBound},
9+
Extension,
10+
};
11+
use lazy_static::lazy_static;
12+
use smol_str::SmolStr;
13+
14+
/// The ID of the `tket2.guppy` extension.
15+
pub const GUPPY_EXTENSION_ID: ExtensionId = IdentList::new_unchecked("tket2.guppy");
16+
/// The "tket2.guppy" extension version.
17+
pub const GUPPY_EXTENSION_VERSION: Version = Version::new(0, 1, 0);
18+
19+
/// The drop operation, used to handle affine types in Guppy.
20+
pub const DROP_OP_NAME: SmolStr = SmolStr::new_inline("drop");
21+
22+
lazy_static! {
23+
/// The "tket2.bool" extension.
24+
pub static ref GUPPY_EXTENSION: Arc<Extension> = {
25+
Extension::new_arc(GUPPY_EXTENSION_ID, GUPPY_EXTENSION_VERSION, |ext, ext_ref| {
26+
ext.add_op(DROP_OP_NAME,
27+
"Drop the input wire. Applicable to guppy affine types only.".into(),
28+
// drop<T: Any>(t: T) -> ()
29+
PolyFuncTypeRV::new(
30+
[TypeBound::Linear.into()],
31+
FuncValueType::new(
32+
vec![Type::new_var_use(0, TypeBound::Linear)],
33+
type_row![],
34+
)
35+
),
36+
ext_ref
37+
).unwrap();
38+
})
39+
};
40+
}
41+
42+
#[cfg(test)]
43+
mod test {
44+
45+
use hugr::{
46+
builder::{inout_sig, DFGBuilder, Dataflow, DataflowHugr},
47+
extension::prelude::usize_t,
48+
std_extensions::collections::array::array_type,
49+
};
50+
51+
use super::*;
52+
53+
#[test]
54+
fn test_drop() {
55+
let arr_type = array_type(2, usize_t());
56+
let drop_op = GUPPY_EXTENSION
57+
.instantiate_extension_op(&DROP_OP_NAME, [arr_type.clone().into()])
58+
.unwrap();
59+
let mut b = DFGBuilder::new(inout_sig(vec![arr_type], type_row![])).unwrap();
60+
let inp = b.input_wires();
61+
b.add_dataflow_op(drop_op, inp).unwrap();
62+
b.finish_hugr_with_outputs([]).unwrap();
63+
}
64+
}

0 commit comments

Comments
 (0)