Skip to content

Commit

Permalink
feat!: remove Tk2Op::AngleAdd (#567)
Browse files Browse the repository at this point in the history
Closes #566


BREAKING CHANGE: `Tk2Op::AngleAdd` removed, use `fadd` from the hugr
float ops standard extension

---------

Co-authored-by: Douglas Wilson <141026920+doug-q@users.noreply.github.com>
  • Loading branch information
ss2165 and doug-q authored Sep 3, 2024
1 parent 258a7c5 commit 42cc82f
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 31 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.
1 change: 0 additions & 1 deletion tket2-py/tket2/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class Tk2Op(Enum):
RxF64 = auto()
PhasedX = auto()
ZZPhase = auto()
AngleAdd = auto()
CZ = auto()
TK1 = auto()
QAlloc = auto()
Expand Down
14 changes: 9 additions & 5 deletions tket2/src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use hugr::extension::{
CustomSignatureFunc, ExtensionId, ExtensionRegistry, SignatureError, Version,
};
use hugr::hugr::IdentList;
use hugr::std_extensions::arithmetic::float_types::{EXTENSION as FLOAT_EXTENSION, FLOAT64_TYPE};
use hugr::std_extensions::arithmetic::{
float_ops::EXTENSION as FLOAT_OPS_EXTENSION,
float_types::{EXTENSION as FLOAT_TYPES_EXTENSION, FLOAT64_TYPE},
};
use hugr::types::type_param::{TypeArg, TypeParam};
use hugr::types::{CustomType, PolyFuncType, PolyFuncTypeRV, Signature};
use hugr::{type_row, Extension};
Expand Down Expand Up @@ -53,10 +56,11 @@ pub static ref TKET1_EXTENSION: Extension = {

/// Extension registry including the prelude, TKET1 and Tk2Ops extensions.
pub static ref REGISTRY: ExtensionRegistry = ExtensionRegistry::try_new([
TKET1_EXTENSION.clone(),
PRELUDE.clone(),
TKET2_EXTENSION.clone(),
FLOAT_EXTENSION.clone(),
TKET1_EXTENSION.to_owned(),
PRELUDE.to_owned(),
TKET2_EXTENSION.to_owned(),
FLOAT_TYPES_EXTENSION.to_owned(),
FLOAT_OPS_EXTENSION.to_owned(),
]).unwrap();


Expand Down
7 changes: 1 addition & 6 deletions tket2/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ pub enum Tk2Op {
RxF64,
PhasedX,
ZZPhase,
AngleAdd,
CZ,
TK1,
QAlloc,
Expand Down Expand Up @@ -121,10 +120,6 @@ impl MakeOpDef for Tk2Op {
Measure => Signature::new(one_qb_row, type_row![QB_T, BOOL_T]),
RzF64 | RxF64 => Signature::new(type_row![QB_T, FLOAT64_TYPE], one_qb_row),
PhasedX => Signature::new(type_row![QB_T, FLOAT64_TYPE, FLOAT64_TYPE], one_qb_row),
AngleAdd => Signature::new(
type_row![FLOAT64_TYPE, FLOAT64_TYPE],
type_row![FLOAT64_TYPE],
),
TK1 => Signature::new(
type_row![QB_T, FLOAT64_TYPE, FLOAT64_TYPE, FLOAT64_TYPE],
one_qb_row,
Expand Down Expand Up @@ -182,7 +177,7 @@ impl Tk2Op {
match self {
H | CX | T | S | X | Y | Z | Tdg | Sdg | ZZMax | RzF64 | RxF64 | PhasedX | ZZPhase
| CZ | TK1 => true,
AngleAdd | Measure | QAlloc | QFree | Reset => false,
Measure | QAlloc | QFree | Reset => false,
}
}
}
Expand Down
21 changes: 13 additions & 8 deletions tket2/src/optimiser/badger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,13 +555,6 @@ mod tests {

use super::{BadgerOptimiser, DefaultBadgerOptimiser};

/// Simplified description of the circuit's commands.
fn gates(circ: &Circuit) -> Vec<Tk2Op> {
circ.commands()
.map(|cmd| cmd.optype().try_into().unwrap())
.collect()
}

#[fixture]
fn rz_rz() -> Circuit {
let input_t = vec![QB_T, FLOAT64_TYPE, FLOAT64_TYPE];
Expand Down Expand Up @@ -630,15 +623,27 @@ mod tests {
#[case::compiled(badger_opt_compiled())]
#[case::json(badger_opt_json())]
fn rz_rz_cancellation(rz_rz: Circuit, #[case] badger_opt: DefaultBadgerOptimiser) {
use hugr::{ops::OpType, std_extensions::arithmetic::float_ops::FloatOps};

use crate::op_matches;

let opt_rz = badger_opt.optimise(
&rz_rz,
BadgerOptions {
queue_size: 4,
..Default::default()
},
);
let [op1, op2]: [&OpType; 2] = opt_rz
.commands()
.map(|cmd| cmd.optype())
.collect::<Vec<_>>()
.try_into()
.unwrap();

// Rzs combined into a single one.
assert_eq!(gates(&opt_rz), vec![Tk2Op::AngleAdd, Tk2Op::RzF64]);
assert_eq!(op1.cast::<FloatOps>(), Some(FloatOps::fadd));
assert!(op_matches(op2, Tk2Op::RzF64));
}

#[rstest]
Expand Down
4 changes: 3 additions & 1 deletion tket2/src/optimiser/badger/qtz_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ struct RepCircData {
}

fn map_op(opstr: &str) -> Op {
if opstr == "add" {
return hugr::std_extensions::arithmetic::float_ops::FloatOps::fadd.into();
}
// TODO, more
match opstr {
"h" => Tk2Op::H,
Expand All @@ -52,7 +55,6 @@ fn map_op(opstr: &str) -> Op {
"tdg" => Tk2Op::Tdg,
"sdg" => Tk2Op::Sdg,
"rz" => Tk2Op::RzF64,
"add" => Tk2Op::AngleAdd,
x => panic!("unknown op {x}"),
}
.into()
Expand Down
5 changes: 3 additions & 2 deletions tket2/src/serialize/pytket/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::collections::{HashMap, HashSet, VecDeque};

use hugr::extension::prelude::{BOOL_T, QB_T};
use hugr::ops::{OpTrait, OpType};
use hugr::std_extensions::arithmetic::float_ops::FloatOps;
use hugr::std_extensions::arithmetic::float_types::FLOAT64_TYPE;
use hugr::{HugrView, Wire};
use itertools::Itertools;
Expand All @@ -13,7 +14,7 @@ use tket_json_rs::circuit_json::{self, SerialCircuit};

use crate::circuit::command::{CircuitUnit, Command};
use crate::circuit::Circuit;
use crate::ops::{match_symb_const_op, op_matches};
use crate::ops::match_symb_const_op;
use crate::serialize::pytket::RegisterHash;
use crate::Tk2Op;

Expand Down Expand Up @@ -619,7 +620,7 @@ impl ParameterTracker {
// Re-use the parameter from the input.
inputs[0].clone()
}
op if op_matches(op, Tk2Op::AngleAdd) => {
OpType::ExtensionOp(_) if optype.cast::<FloatOps>() == Some(FloatOps::fadd) => {
format!("{} + {}", inputs[0], inputs[1])
}
_ => {
Expand Down
5 changes: 0 additions & 5 deletions tket2/src/serialize/pytket/op/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ impl NativeOp {
Tk2Op::CZ => Tk1OpType::CZ,
Tk2Op::Reset => Tk1OpType::Reset,
Tk2Op::Measure => Tk1OpType::Measure,
Tk2Op::AngleAdd => {
// These operations should be folded into constant before serialisation,
// or replaced by pytket logic expressions.
return Some(Self::new(tk2op.into(), None));
}
// These operations do not have a direct pytket counterpart.
Tk2Op::QAlloc | Tk2Op::QFree => {
// These operations are implicitly supported by the encoding,
Expand Down
9 changes: 6 additions & 3 deletions tket2/src/serialize/pytket/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use hugr::builder::{DFGBuilder, Dataflow, DataflowHugr};
use hugr::extension::prelude::{BOOL_T, QB_T};

use hugr::hugr::hugrmut::HugrMut;
use hugr::std_extensions::arithmetic::float_types::{ConstF64, FLOAT64_TYPE};
use hugr::std_extensions::arithmetic::{
float_ops::FloatOps,
float_types::{ConstF64, FLOAT64_TYPE},
};
use hugr::types::Signature;
use hugr::HugrView;
use rstest::{fixture, rstest};
Expand Down Expand Up @@ -194,7 +197,7 @@ fn circ_add_angles_symbolic() -> Circuit {

let [qb, f1, f2] = h.input_wires_arr();
let [f12] = h
.add_dataflow_op(Tk2Op::AngleAdd, [f1, f2])
.add_dataflow_op(FloatOps::fadd, [f1, f2])
.unwrap()
.outputs_arr();
let [qb] = h
Expand All @@ -215,7 +218,7 @@ fn circ_add_angles_constants() -> Circuit {
let point2 = h.add_load_value(ConstF64::new(0.2 * std::f64::consts::PI));
let point3 = h.add_load_value(ConstF64::new(0.3 * std::f64::consts::PI));
let point5 = h
.add_dataflow_op(Tk2Op::AngleAdd, [point2, point3])
.add_dataflow_op(FloatOps::fadd, [point2, point3])
.unwrap()
.out_wire(0);

Expand Down

0 comments on commit 42cc82f

Please sign in to comment.