Skip to content

Commit

Permalink
feat: more angle ops (construct, deconstruct, radians, equality) (#581)
Browse files Browse the repository at this point in the history
Co-authored-by: Mark Koch <48097969+mark-koch@users.noreply.github.com>
  • Loading branch information
ss2165 and mark-koch authored Sep 4, 2024
1 parent d14631f commit bf73511
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
2 changes: 2 additions & 0 deletions tket2/src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use hugr::extension::{
CustomSignatureFunc, ExtensionId, ExtensionRegistry, SignatureError, Version,
};
use hugr::hugr::IdentList;
use hugr::std_extensions::arithmetic::float_types::EXTENSION as FLOAT_TYPES;
use hugr::types::type_param::{TypeArg, TypeParam};
use hugr::types::{CustomType, PolyFuncType, PolyFuncTypeRV, Signature};
use hugr::{type_row, Extension};
Expand Down Expand Up @@ -56,6 +57,7 @@ pub static ref REGISTRY: ExtensionRegistry = ExtensionRegistry::try_new([
TKET1_EXTENSION.to_owned(),
PRELUDE.to_owned(),
TKET2_EXTENSION.to_owned(),
FLOAT_TYPES.to_owned(),
]).unwrap();


Expand Down
38 changes: 36 additions & 2 deletions tket2/src/extension/angle.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use hugr::extension::prelude::USIZE_T;
use hugr::extension::prelude::{sum_with_error, BOOL_T, USIZE_T};
use hugr::extension::simple_op::{MakeOpDef, MakeRegisteredOp};
use hugr::extension::ExtensionSet;
use hugr::ops::constant::{downcast_equal_consts, CustomConst};
use hugr::std_extensions::arithmetic::float_types::FLOAT64_TYPE;
use hugr::type_row;
use hugr::{
types::{ConstTypeError, CustomType, Signature, Type, TypeBound},
Expand Down Expand Up @@ -146,6 +147,20 @@ pub enum AngleOp {
asub,
/// Negation of an angle
aneg,
/// Construct angle from numerator and log-denominator
anew,
/// Decompose angle into numerator and log-denominator
aparts,
/// Construct angle from radians
afromrad,
/// Convert angle to radians
atorad,
/// Check angle equality
aeq,
/// Multiply angle by a scalar
amul,
/// Divide by scalar with rounding
adiv,
}

impl MakeOpDef for AngleOp {
Expand All @@ -167,6 +182,19 @@ impl MakeOpDef for AngleOp {
Signature::new(type_row![ANGLE_TYPE, ANGLE_TYPE], type_row![ANGLE_TYPE])
}
AngleOp::aneg => Signature::new_endo(type_row![ANGLE_TYPE]),
AngleOp::anew => Signature::new(
type_row![USIZE_T, USIZE_T],
vec![sum_with_error(ANGLE_TYPE).into()],
),
AngleOp::aparts => Signature::new(type_row![ANGLE_TYPE], type_row![USIZE_T, USIZE_T]),
AngleOp::afromrad => {
Signature::new(type_row![USIZE_T, FLOAT64_TYPE], type_row![ANGLE_TYPE])
}
AngleOp::atorad => Signature::new(type_row![ANGLE_TYPE], type_row![FLOAT64_TYPE]),
AngleOp::aeq => Signature::new(type_row![ANGLE_TYPE, ANGLE_TYPE], type_row![BOOL_T]),
AngleOp::amul | AngleOp::adiv => {
Signature::new(type_row![ANGLE_TYPE, USIZE_T], type_row![ANGLE_TYPE])
}
}
.into()
}
Expand All @@ -177,6 +205,13 @@ impl MakeOpDef for AngleOp {
AngleOp::aadd => "addition of angles",
AngleOp::asub => "subtraction of the second angle from the first",
AngleOp::aneg => "negation of an angle",
AngleOp::anew => "construct angle from numerator and log-denominator, returning an error if invalid",
AngleOp::aparts => "decompose angle into numerator and log-denominator",
AngleOp::afromrad => "construct angle from radians, rounding given a log-denominator",
AngleOp::atorad => "convert angle to radians",
AngleOp::aeq => "check angle equality",
AngleOp::amul => "multiply angle by a scalar",
AngleOp::adiv => "Divide angle by an integer. If the integer is not a power of 2, or if the resulting denominator would exceed 2^64, the result is rounded to the nearest multiple of 2 pi / 2^ 64",
}.to_owned()
}

Expand Down Expand Up @@ -251,7 +286,6 @@ mod test {
#[test]
fn test_ops() {
let ops = AngleOp::iter().collect::<Vec<_>>();
assert_eq!(ops.len(), 4);
for op in ops {
let optype: OpType = op.into();
assert_eq!(optype.cast(), Some(op));
Expand Down

0 comments on commit bf73511

Please sign in to comment.