use std::sync::Arc;
use crate::serialize::pytket::extension::OpaqueTk1Op;
use crate::Tk2Op;
use hugr::extension::simple_op::MakeOpDef;
use hugr::extension::{
CustomSignatureFunc, ExtensionId, ExtensionRegistry, SignatureError, Version,
};
use hugr::hugr::IdentList;
use hugr::std_extensions::STD_REG;
use hugr::types::type_param::{TypeArg, TypeParam};
use hugr::types::{CustomType, PolyFuncType, PolyFuncTypeRV};
use hugr::Extension;
use lazy_static::lazy_static;
use smol_str::SmolStr;
pub mod bool;
pub mod debug;
pub mod rotation;
pub mod sympy;
use sympy::SympyOpDef;
pub const TKET1_EXTENSION_ID: ExtensionId = IdentList::new_unchecked("TKET1");
pub const TKET1_OP_NAME: SmolStr = SmolStr::new_inline("tk1op");
pub const TKET1_PAYLOAD_NAME: SmolStr = SmolStr::new_inline("TKET1-json-payload");
pub const TKET1_EXTENSION_VERSION: Version = Version::new(0, 1, 0);
lazy_static! {
pub static ref TKET1_OP_PAYLOAD : CustomType =
TKET1_EXTENSION.get_type(&TKET1_PAYLOAD_NAME).unwrap().instantiate([]).unwrap();
pub static ref TKET1_EXTENSION: Arc<Extension> = {
Extension::new_arc(TKET1_EXTENSION_ID, TKET1_EXTENSION_VERSION, |res, ext_ref| {
res.add_op(
TKET1_OP_NAME,
"An opaque TKET1 operation.".into(),
Tk1Signature([TypeParam::String]),
ext_ref
).unwrap();
})
};
pub(crate) static ref REGISTRY: ExtensionRegistry = ExtensionRegistry::new(
STD_REG.iter().map(|e| e.to_owned()).chain([
TKET1_EXTENSION.to_owned(),
TKET2_EXTENSION.to_owned(),
bool::BOOL_EXTENSION.to_owned(),
debug::DEBUG_EXTENSION.to_owned(),
rotation::ROTATION_EXTENSION.to_owned()
]));
}
struct Tk1Signature([TypeParam; 1]);
impl CustomSignatureFunc for Tk1Signature {
fn compute_signature<'o, 'a: 'o>(
&'a self,
arg_values: &[TypeArg],
_def: &'o hugr::extension::OpDef,
) -> Result<PolyFuncTypeRV, SignatureError> {
let [TypeArg::String { arg }] = arg_values else {
panic!("Wrong number of arguments");
};
let op: OpaqueTk1Op = serde_json::from_str(arg).unwrap(); let poly_func: PolyFuncType = op.signature().into();
Ok(poly_func.into())
}
fn static_params(&self) -> &[TypeParam] {
&self.0
}
}
pub const TKET2_EXTENSION_ID: ExtensionId = ExtensionId::new_unchecked("tket2.quantum");
pub const TKET2_EXTENSION_VERSION: Version = Version::new(0, 1, 1);
lazy_static! {
pub static ref TKET2_EXTENSION: Arc<Extension> = {
Extension::new_arc(TKET2_EXTENSION_ID, TKET2_EXTENSION_VERSION, |res, ext_ref| {
Tk2Op::load_all_ops(res, ext_ref).expect("add_fail");
SympyOpDef.add_to_extension(res, ext_ref).unwrap();
})
};
}