-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: Cleanup tket1 serialized op structures #419
Changes from 2 commits
18c48fb
385e881
720c275
b38d3e5
4814cfb
6dd44e8
3e8f817
22de8c8
58b5966
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -90,9 +90,12 @@ pub enum Pauli { | |||
Z, | ||||
} | ||||
|
||||
#[derive(Debug, Error, PartialEq, Clone, Copy)] | ||||
#[error("Not a Tk2Op.")] | ||||
pub struct NotTk2Op; | ||||
#[derive(Debug, Error, PartialEq, Clone)] | ||||
#[error("{} is not a Tk2Op.", op.name())] | ||||
pub struct NotTk2Op { | ||||
/// The offending operation. | ||||
pub op: OpType, | ||||
} | ||||
|
||||
impl Pauli { | ||||
/// Check if this pauli commutes with another. | ||||
|
@@ -226,29 +229,34 @@ impl TryFrom<&OpType> for Tk2Op { | |||
type Error = NotTk2Op; | ||||
|
||||
fn try_from(op: &OpType) -> Result<Self, Self::Error> { | ||||
let OpType::CustomOp(custom_op) = op else { | ||||
return Err(NotTk2Op); | ||||
}; | ||||
|
||||
match custom_op { | ||||
CustomOp::Extension(ext) => Tk2Op::from_extension_op(ext), | ||||
CustomOp::Opaque(opaque) => { | ||||
if opaque.extension() != &EXTENSION_ID { | ||||
return Err(NotTk2Op); | ||||
} | ||||
try_from_name(opaque.name()) | ||||
} | ||||
} | ||||
.map_err(|_| NotTk2Op) | ||||
optype_to_tk2op(op).ok_or_else(|| NotTk2Op { op: op.clone() }) | ||||
} | ||||
} | ||||
|
||||
impl TryFrom<OpType> for Tk2Op { | ||||
type Error = NotTk2Op; | ||||
|
||||
fn try_from(op: OpType) -> Result<Self, Self::Error> { | ||||
Self::try_from(&op) | ||||
optype_to_tk2op(&op).ok_or_else(|| NotTk2Op { op }) | ||||
} | ||||
} | ||||
|
||||
// Internal implementation for `TryFrom<Optype> for Tk2Op` that doesn't copy the `OpType` when it errors. | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, the original function that returned an |
||||
fn optype_to_tk2op(op: &OpType) -> Option<Tk2Op> { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both callsites create the same error anyway, I would prefer to do it once here. Not a big deal though. |
||||
let OpType::CustomOp(custom_op) = op else { | ||||
return None; | ||||
}; | ||||
|
||||
match custom_op { | ||||
CustomOp::Extension(ext) => Tk2Op::from_extension_op(ext), | ||||
CustomOp::Opaque(opaque) => { | ||||
if opaque.extension() != &EXTENSION_ID { | ||||
return None; | ||||
} | ||||
try_from_name(opaque.name()) | ||||
} | ||||
} | ||||
.ok() | ||||
} | ||||
|
||||
#[cfg(test)] | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we rename
JsonOpSignature
?