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: add angle type to tket2 extension #231

Merged
merged 7 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ license-file = "LICENCE"
[workspace.dependencies]

tket2 = { path = "./tket2" }
quantinuum-hugr = { git = "https://github.com/CQCL-DEV/hugr", rev = "d0499ad" }
quantinuum-hugr = { git = "https://github.com/CQCL-DEV/hugr", rev = "b256c2b" }
portgraph = { version = "0.10" }
pyo3 = { version = "0.20" }
itertools = { version = "0.11.0" }
Expand Down
58 changes: 54 additions & 4 deletions tket2/src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@
use std::collections::HashMap;

use super::json::op::JsonOp;
use crate::ops::EXTENSION as T2EXTENSION;
use crate::ops::load_all_ops;
use crate::T2Op;
use hugr::extension::prelude::PRELUDE;
use hugr::extension::{ExtensionId, ExtensionRegistry, SignatureError};
use hugr::hugr::IdentList;
use hugr::ops::custom::{ExternalOp, OpaqueOp};
use hugr::ops::OpName;
use hugr::std_extensions::arithmetic::float_types::extension as float_extension;
use hugr::std_extensions::arithmetic::float_types::{extension as float_extension, FLOAT64_TYPE};
use hugr::types::type_param::{CustomTypeArg, TypeArg, TypeParam};
use hugr::types::{CustomType, FunctionType, Type, TypeBound};
use hugr::Extension;
use hugr::{type_row, Extension};
use lazy_static::lazy_static;
use smol_str::SmolStr;

/// Definition for Angle ops and types.
pub mod angle;

/// The ID of the TKET1 extension.
pub const TKET1_EXTENSION_ID: ExtensionId = IdentList::new_unchecked("TKET1");

Expand Down Expand Up @@ -68,7 +72,7 @@ pub static ref LINEAR_BIT: Type = {
pub static ref REGISTRY: ExtensionRegistry = ExtensionRegistry::from([
TKET1_EXTENSION.clone(),
PRELUDE.clone(),
T2EXTENSION.clone(),
TKET2_EXTENSION.clone(),
float_extension(),
]);

Expand Down Expand Up @@ -125,3 +129,49 @@ fn json_op_signature(args: &[TypeArg]) -> Result<FunctionType, SignatureError> {
let op: JsonOp = serde_yaml::from_value(arg.value.clone()).unwrap(); // TODO Errors!
Ok(op.signature())
}

/// Angle type with given log denominator.
pub fn angle_custom_type(log_denom: u8) -> CustomType {
angle::angle_custom_type(&TKET2_EXTENSION, angle::type_arg(log_denom))
}

/// Name of tket 2 extension.
pub const TKET2_EXTENSION_ID: ExtensionId = ExtensionId::new_unchecked("quantum.tket2");

/// The name of the symbolic expression opaque type arg.
pub const SYM_EXPR_NAME: SmolStr = SmolStr::new_inline("SymExpr");

/// The name of the symbolic expression opaque type arg.
pub const SYM_OP_ID: SmolStr = SmolStr::new_inline("symbolic_float");

lazy_static! {
/// The type of the symbolic expression opaque type arg.
pub static ref SYM_EXPR_T: CustomType =
TKET2_EXTENSION.get_type(&SYM_EXPR_NAME).unwrap().instantiate([]).unwrap();

/// The extension definition for TKET2 ops and types.
pub static ref TKET2_EXTENSION: Extension = {
let mut e = Extension::new(TKET2_EXTENSION_ID);
load_all_ops::<T2Op>(&mut e).expect("add fail");

let sym_expr_opdef = e.add_type(
SYM_EXPR_NAME,
vec![],
"Symbolic expression.".into(),
TypeBound::Eq.into(),
)
.unwrap();
let sym_expr_param = TypeParam::Opaque(sym_expr_opdef.instantiate([]).unwrap());

e.add_op_custom_sig_simple(
SYM_OP_ID,
"Store a sympy expression that can be evaluated to a float.".to_string(),
vec![sym_expr_param],
|_: &[TypeArg]| Ok(FunctionType::new(type_row![], type_row![FLOAT64_TYPE])),
)
.unwrap();

angle::add_to_extension(&mut e);
e
};
}
Loading