Skip to content

Commit

Permalink
make loading more generic and use NotT2Op error mroe
Browse files Browse the repository at this point in the history
  • Loading branch information
ss2165 committed Sep 4, 2023
1 parent 16c8c52 commit 3f4cecd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
4 changes: 1 addition & 3 deletions src/json/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,7 @@ impl TryFrom<&OpType> for JsonOp {
T2Op::H => JsonOpType::H,
T2Op::Measure => JsonOpType::Measure,
T2Op::RzF64 => JsonOpType::RzF64,
_ => {
return Err(err());
}
_ => return Err(err()),
}
} else if let LeafOp::CustomOp(b) = leaf {
let ext = (*b).as_ref();
Expand Down
26 changes: 18 additions & 8 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,15 @@ pub struct NotT2Op;

// this trait could be implemented in Hugr
trait SimpleOpEnum: Into<&'static str> + FromStr + Copy + IntoEnumIterator {
type LoadError: std::error::Error;

fn signature(&self) -> FunctionType;
fn name(&self) -> &str {
(*self).into()
}
fn try_from_op_def(op_def: &OpDef) -> Result<Self, NotT2Op> {
from_extension_name(op_def.extension(), op_def.name())
fn from_extension_name(extension: &str, op_name: &str) -> Result<Self, Self::LoadError>;
fn try_from_op_def(op_def: &OpDef) -> Result<Self, Self::LoadError> {
Self::from_extension_name(op_def.extension(), op_def.name())
}
fn add_to_extension<'e>(
&self,
Expand All @@ -99,6 +102,7 @@ impl Pauli {
}
}
impl SimpleOpEnum for T2Op {
type LoadError = NotT2Op;
fn signature(&self) -> FunctionType {
use T2Op::*;
let one_qb_row = type_row![QB_T];
Expand Down Expand Up @@ -129,6 +133,13 @@ impl SimpleOpEnum for T2Op {
move |_: &_| Ok(FunctionType::new(input.clone(), output.clone())),
)
}

fn from_extension_name(extension: &str, op_name: &str) -> Result<Self, Self::LoadError> {
if extension != EXTENSION_ID {
return Err(NotT2Op);
}
Self::from_str(op_name).map_err(|_| NotT2Op)
}
}

impl T2Op {
Expand Down Expand Up @@ -175,25 +186,24 @@ impl From<T2Op> for OpType {
}

impl TryFrom<OpType> for T2Op {
type Error = &'static str;
type Error = NotT2Op;

fn try_from(op: OpType) -> Result<Self, Self::Error> {
let leaf: LeafOp = op.try_into().map_err(|_| "not a leaf.")?;
let leaf: LeafOp = op.try_into().map_err(|_| NotT2Op)?;
leaf.try_into()
}
}

impl TryFrom<LeafOp> for T2Op {
type Error = &'static str;
type Error = NotT2Op;

fn try_from(op: LeafOp) -> Result<Self, Self::Error> {
match op {
LeafOp::CustomOp(b) => match *b {
ExternalOp::Extension(e) => Self::try_from_op_def(e.def()),
ExternalOp::Opaque(o) => from_extension_name(o.extension(), o.name()),
}
.map_err(|_| "not a T2Op"),
_ => Err("not a custom."),
},
_ => Err(NotT2Op),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/portmatching/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use thiserror::Error;
#[cfg(feature = "pyo3")]
use pyo3::prelude::*;

use crate::{circuit::Circuit, T2Op};
use crate::{circuit::Circuit, ops::NotT2Op, T2Op};

/// Matchable operations in a circuit.
///
Expand All @@ -45,13 +45,13 @@ impl From<T2Op> for MatchOp {
}

impl TryFrom<OpType> for MatchOp {
type Error = &'static str;
type Error = NotT2Op;

fn try_from(value: OpType) -> Result<Self, Self::Error> {
match value {
OpType::LeafOp(op) => Ok(Self::Op(op.try_into()?)),
OpType::LoadConstant(_) => Ok(Self::LoadConstant),
_ => Err("Unsupported op type"),
_ => Err(NotT2Op),
}
}
}
Expand Down

0 comments on commit 3f4cecd

Please sign in to comment.