Skip to content

Commit

Permalink
fix: Correctly detect custom ops by name (#281)
Browse files Browse the repository at this point in the history
This was causing the hard-coded AngleAdd encoder to not work for
OpaqueOps.
Now we compare ops by name instead.

drive-by: Nice errors in the encoder
  • Loading branch information
aborgna-q authored Jan 8, 2024
1 parent 345b918 commit d44ac89
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
23 changes: 16 additions & 7 deletions tket2/src/json/encoder.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! Intermediate structure for converting encoding [`Circuit`]s into [`SerialCircuit`]s.
use core::panic;
use std::collections::HashMap;

use hugr::extension::prelude::QB_T;
use hugr::ops::OpType;
use hugr::ops::{OpName, OpType};
use hugr::std_extensions::arithmetic::float_types::ConstF64;
use hugr::values::Value;
use hugr::Wire;
Expand Down Expand Up @@ -179,10 +180,14 @@ impl JsonEncoder {
})
.collect_vec();
if inputs.len() != command.input_count() {
debug_assert!(!matches!(
optype,
OpType::Const(_) | OpType::LoadConstant(_)
));
debug_assert!(
!matches!(optype, OpType::Const(_) | OpType::LoadConstant(_)),
"Found a {} with {} inputs, of which {} are non-linear. In node {:?}",
optype.name(),
command.input_count(),
inputs.len(),
command.node()
);
return false;
}

Expand Down Expand Up @@ -220,8 +225,12 @@ impl JsonEncoder {
};

for (unit, _, _) in command.outputs() {
if let CircuitUnit::Wire(wire) = unit {
self.add_parameter(wire, param.clone());
match unit {
CircuitUnit::Wire(wire) => self.add_parameter(wire, param.clone()),
CircuitUnit::Linear(_) => panic!(
"Found a non-wire output {unit:?} for a {} command.",
optype.name()
),
}
}
true
Expand Down
6 changes: 2 additions & 4 deletions tket2/src/ops.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::extension::{
SYM_EXPR_T, SYM_OP_ID, TKET2_EXTENSION as EXTENSION, TKET2_EXTENSION_ID as EXTENSION_ID,
};
use hugr::ops::OpName;
use hugr::{
extension::{
prelude::{BOOL_T, QB_T},
Expand Down Expand Up @@ -68,10 +69,7 @@ pub enum Tk2Op {

/// Whether an op is a given Tk2Op.
pub fn op_matches(op: &OpType, tk2op: Tk2Op) -> bool {
let Some(ext_op) = tk2op.to_extension_op() else {
return false;
};
op.as_leaf_op().and_then(|op| op.as_extension_op()) == Some(&ext_op)
op.name() == <Tk2Op as Into<OpType>>::into(tk2op).name()
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, EnumIter, Display, PartialEq, PartialOrd)]
Expand Down

0 comments on commit d44ac89

Please sign in to comment.