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

fix: Register the type parameters as custom types #101

Merged
merged 1 commit into from
Sep 13, 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
9 changes: 7 additions & 2 deletions src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,22 @@ pub const LINEAR_BIT_NAME: SmolStr = SmolStr::new_inline("LBit");
/// The name for opaque TKET1 operations.
pub const JSON_OP_NAME: SmolStr = SmolStr::new_inline("TKET1 Json Op");

/// The ID of an opaque TKET1 operation metadata.
pub const JSON_PAYLOAD_NAME: SmolStr = SmolStr::new_inline("TKET1 Json Payload");

lazy_static! {
/// A custom type for the encoded TKET1 operation
static ref TKET1_OP_PAYLOAD : CustomType = CustomType::new("TKET1 Json Op", vec![], TKET1_EXTENSION_ID, TypeBound::Eq);
static ref TKET1_OP_PAYLOAD : CustomType =
TKET1_EXTENSION.get_type(&JSON_PAYLOAD_NAME).unwrap().instantiate_concrete([]).unwrap();

/// The TKET1 extension, containing the opaque TKET1 operations.
pub static ref TKET1_EXTENSION: Extension = {
let mut res = Extension::new(TKET1_EXTENSION_ID);

res.add_type(LINEAR_BIT_NAME, vec![], "A linear bit.".into(), TypeBound::Any.into()).unwrap();

let json_op_payload = TypeParam::Opaque(TKET1_OP_PAYLOAD.clone());
let json_op_payload_def = res.add_type(JSON_PAYLOAD_NAME, vec![], "Opaque TKET1 operation metadata.".into(), TypeBound::Eq.into()).unwrap();
let json_op_payload = TypeParam::Opaque(json_op_payload_def.instantiate_concrete([]).unwrap());
res.add_op_custom_sig(
JSON_OP_NAME,
"An opaque TKET1 operation.".into(),
Expand Down
39 changes: 26 additions & 13 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,20 +179,14 @@ impl T2Op {
}
}

/// The type of the symbolic expression opaque type arg.
pub const SYM_EXPR_T: CustomType =
CustomType::new_simple(SmolStr::new_inline("SymExpr"), EXTENSION_ID, TypeBound::Eq);

const SYM_OP_ID: SmolStr = SmolStr::new_inline("symbolic_float");

/// Initialize a new custom symbolic expression constant op from a string.
pub fn symbolic_constant_op(s: &str) -> OpType {
let value: serde_yaml::Value = s.into();
let l: LeafOp = EXTENSION
.instantiate_extension_op(
&SYM_OP_ID,
vec![TypeArg::Opaque {
arg: CustomTypeArg::new(SYM_EXPR_T, value).unwrap(),
arg: CustomTypeArg::new(SYM_EXPR_T.clone(), value).unwrap(),
}],
)
.unwrap()
Expand Down Expand Up @@ -227,21 +221,40 @@ pub(crate) fn match_symb_const_op(op: &OpType) -> Option<&str> {
}
}

fn extension() -> Extension {
/// 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.
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 =
EXTENSION.get_type(&SYM_EXPR_NAME).unwrap().instantiate_concrete([]).unwrap();

pub static ref EXTENSION: Extension = {
let mut e = Extension::new(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_concrete([]).unwrap());

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

lazy_static! {
pub static ref EXTENSION: Extension = extension();
e
};
}

// From implementations could be made generic over SimpleOpEnum
Expand Down