Skip to content

Commit

Permalink
remove unnecessary ops
Browse files Browse the repository at this point in the history
  • Loading branch information
ss2165 committed Sep 13, 2024
1 parent 5327579 commit eb86c0d
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 550 deletions.
Binary file modified test_files/eccs/nam_4_2.rwr
Binary file not shown.
Binary file modified test_files/eccs/nam_6_3.rwr
Binary file not shown.
Binary file modified test_files/eccs/small_eccs.rwr
Binary file not shown.
Binary file modified tket2-eccs/src/tket2_eccs/data/nam_6_3.rwr
Binary file not shown.
21 changes: 14 additions & 7 deletions tket2-hseries/src/extension/hseries/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use hugr::{
extension::ExtensionRegistry,
hugr::{hugrmut::HugrMut, HugrError},
ops::{self, DataflowOpTrait},
std_extensions::arithmetic::float_types::ConstF64,
std_extensions::arithmetic::{float_ops::FloatOps, float_types::ConstF64},
types::Signature,
Hugr, HugrView, Node, Wire,
};
Expand Down Expand Up @@ -68,19 +68,19 @@ fn op_to_hugr(op: Tk2Op) -> Result<Hugr, LowerTk2Error> {
(Tk2Op::CY, [c, t]) => b.build_cy(*c, *t)?.into(),
(Tk2Op::CZ, [c, t]) => b.build_cz(*c, *t)?.into(),
(Tk2Op::Rx, [q, angle]) => {
let float = b.add_atorad(*angle)?;
let float = build_to_radians(&mut b, *angle)?;
vec![b.build_rx(*q, float)?]
}
(Tk2Op::Ry, [q, angle]) => {
let float = b.add_atorad(*angle)?;
let float = build_to_radians(&mut b, *angle)?;
vec![b.build_ry(*q, float)?]
}
(Tk2Op::Rz, [q, angle]) => {
let float = b.add_atorad(*angle)?;
let float = build_to_radians(&mut b, *angle)?;
vec![b.add_rz(*q, float)?]
}
(Tk2Op::CRz, [c, t, angle]) => {
let float = b.add_atorad(*angle)?;
let float = build_to_radians(&mut b, *angle)?;
b.build_crz(*c, *t, float)?.into()
}
(Tk2Op::Toffoli, [a, b_, c]) => b.build_toffoli(*a, *b_, *c)?.into(),
Expand All @@ -89,6 +89,13 @@ fn op_to_hugr(op: Tk2Op) -> Result<Hugr, LowerTk2Error> {
Ok(b.finish_hugr_with_outputs(outputs, &REGISTRY)?)
}

fn build_to_radians(b: &mut DFGBuilder<Hugr>, rotation: Wire) -> Result<Wire, BuildError> {
let turns = b.add_toturns(rotation)?;
let tau = pi_mul_f64(b, 2.0);
let float = b.add_dataflow_op(FloatOps::fmul, [turns, tau])?.out_wire(0);
Ok(float)
}

/// Lower `Tk2Op` operations to `HSeriesOp` operations.
pub fn lower_tk2_op(hugr: &mut impl HugrMut) -> Result<Vec<hugr::Node>, LowerTk2Error> {
let replaced_nodes = lower_direct(hugr)?;
Expand Down Expand Up @@ -264,8 +271,8 @@ mod test {

let lowered = lower_tk2_op(&mut h).unwrap();
assert_eq!(lowered.len(), 4);
// dfg, input, output, alloc, phasedx, rz, atorad, phasedx, free + 4x(float + load)
assert_eq!(h.node_count(), 17);
// dfg, input, output, alloc, phasedx, rz, toturns, fmul, phasedx, free + 5x(float + load)
assert_eq!(h.node_count(), 20);
assert_eq!(check_lowered(&h), Ok(()));
}
}
299 changes: 10 additions & 289 deletions tket2-py/tket2/extensions/_json_defs/tket2/rotation.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"extension": "tket2.rotation",
"name": "rotation",
"params": [],
"description": "rotation type expressed as dyadic rational multiples of ",
"description": "rotation type expressed as number of half turns",
"bound": {
"b": "Explicit",
"bound": "C"
Expand All @@ -16,122 +16,14 @@
},
"values": {},
"operations": {
"aadd": {
"fromturns": {
"extension": "tket2.rotation",
"name": "aadd",
"description": "addition of rotations",
"name": "fromturns",
"description": "Construct rotation from number of turns (would be multiples of π in radians).",
"signature": {
"params": [],
"body": {
"input": [
{
"t": "Opaque",
"extension": "tket2.rotation",
"id": "rotation",
"args": [],
"bound": "C"
},
{
"t": "Opaque",
"extension": "tket2.rotation",
"id": "rotation",
"args": [],
"bound": "C"
}
],
"output": [
{
"t": "Opaque",
"extension": "tket2.rotation",
"id": "rotation",
"args": [],
"bound": "C"
}
],
"extension_reqs": []
}
},
"binary": false
},
"adiv": {
"extension": "tket2.rotation",
"name": "adiv",
"description": "Divide rotation 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",
"signature": {
"params": [],
"body": {
"input": [
{
"t": "Opaque",
"extension": "tket2.rotation",
"id": "rotation",
"args": [],
"bound": "C"
},
{
"t": "I"
}
],
"output": [
{
"t": "Opaque",
"extension": "tket2.rotation",
"id": "rotation",
"args": [],
"bound": "C"
}
],
"extension_reqs": []
}
},
"binary": false
},
"aeq": {
"extension": "tket2.rotation",
"name": "aeq",
"description": "check rotation equality",
"signature": {
"params": [],
"body": {
"input": [
{
"t": "Opaque",
"extension": "tket2.rotation",
"id": "rotation",
"args": [],
"bound": "C"
},
{
"t": "Opaque",
"extension": "tket2.rotation",
"id": "rotation",
"args": [],
"bound": "C"
}
],
"output": [
{
"t": "Sum",
"s": "Unit",
"size": 2
}
],
"extension_reqs": []
}
},
"binary": false
},
"afromrad": {
"extension": "tket2.rotation",
"name": "afromrad",
"description": "construct rotation from radians, rounding given a log-denominator",
"signature": {
"params": [],
"body": {
"input": [
{
"t": "I"
},
{
"t": "Opaque",
"extension": "arithmetic.float.types",
Expand All @@ -154,148 +46,10 @@
},
"binary": false
},
"amul": {
"extension": "tket2.rotation",
"name": "amul",
"description": "multiply rotation by a scalar",
"signature": {
"params": [],
"body": {
"input": [
{
"t": "Opaque",
"extension": "tket2.rotation",
"id": "rotation",
"args": [],
"bound": "C"
},
{
"t": "I"
}
],
"output": [
{
"t": "Opaque",
"extension": "tket2.rotation",
"id": "rotation",
"args": [],
"bound": "C"
}
],
"extension_reqs": []
}
},
"binary": false
},
"aneg": {
"radd": {
"extension": "tket2.rotation",
"name": "aneg",
"description": "negation of a rotation",
"signature": {
"params": [],
"body": {
"input": [
{
"t": "Opaque",
"extension": "tket2.rotation",
"id": "rotation",
"args": [],
"bound": "C"
}
],
"output": [
{
"t": "Opaque",
"extension": "tket2.rotation",
"id": "rotation",
"args": [],
"bound": "C"
}
],
"extension_reqs": []
}
},
"binary": false
},
"anew": {
"extension": "tket2.rotation",
"name": "anew",
"description": "construct rotation from numerator and log-denominator, returning an error if invalid",
"signature": {
"params": [],
"body": {
"input": [
{
"t": "I"
},
{
"t": "I"
}
],
"output": [
{
"t": "Sum",
"s": "General",
"rows": [
[
{
"t": "Opaque",
"extension": "prelude",
"id": "error",
"args": [],
"bound": "C"
}
],
[
{
"t": "Opaque",
"extension": "tket2.rotation",
"id": "rotation",
"args": [],
"bound": "C"
}
]
]
}
],
"extension_reqs": []
}
},
"binary": false
},
"aparts": {
"extension": "tket2.rotation",
"name": "aparts",
"description": "decompose rotation into numerator and log-denominator",
"signature": {
"params": [],
"body": {
"input": [
{
"t": "Opaque",
"extension": "tket2.rotation",
"id": "rotation",
"args": [],
"bound": "C"
}
],
"output": [
{
"t": "I"
},
{
"t": "I"
}
],
"extension_reqs": []
}
},
"binary": false
},
"asub": {
"extension": "tket2.rotation",
"name": "asub",
"description": "subtraction of the second rotation from the first",
"name": "radd",
"description": "Add two angles together (experimental).",
"signature": {
"params": [],
"body": {
Expand Down Expand Up @@ -329,10 +83,10 @@
},
"binary": false
},
"atorad": {
"toturns": {
"extension": "tket2.rotation",
"name": "atorad",
"description": "convert rotation to radians",
"name": "toturns",
"description": "Convert rotation to number of turns (would be multiples of π in radians).",
"signature": {
"params": [],
"body": {
Expand All @@ -358,39 +112,6 @@
}
},
"binary": false
},
"atrunc": {
"extension": "tket2.rotation",
"name": "atrunc",
"description": "truncate a rotation to one with a lower log-denominator with the nearest value, rounding down in [0, 2π) if necessary",
"signature": {
"params": [],
"body": {
"input": [
{
"t": "Opaque",
"extension": "tket2.rotation",
"id": "rotation",
"args": [],
"bound": "C"
},
{
"t": "I"
}
],
"output": [
{
"t": "Opaque",
"extension": "tket2.rotation",
"id": "rotation",
"args": [],
"bound": "C"
}
],
"extension_reqs": []
}
},
"binary": false
}
}
}
Loading

0 comments on commit eb86c0d

Please sign in to comment.