Skip to content

Commit

Permalink
refactor!: Simplify tket1 conversion errors (#408)
Browse files Browse the repository at this point in the history
Cleanups the repeated and unused variants in `TK1ConvertError` and
`OpConvertError`.

BREAKING CHANGE: Replaced `tket2.circuit.OpConvertError` with
`tket2.circuit.TK1ConvertError` in the python lib.
  • Loading branch information
aborgna-q authored Jun 13, 2024
1 parent 39e29d4 commit b0b8aff
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 51 deletions.
6 changes: 3 additions & 3 deletions tket2-py/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn module(py: Python<'_>) -> PyResult<Bound<'_, PyModule>> {
"HUGRSerializationError",
py.get_type_bound::<PyHUGRSerializationError>(),
)?;
m.add("OpConvertError", py.get_type_bound::<PyOpConvertError>())?;
m.add("TK1ConvertError", py.get_type_bound::<PyTK1ConvertError>())?;

Ok(m)
}
Expand Down Expand Up @@ -83,8 +83,8 @@ create_py_exception!(
);

create_py_exception!(
tket2::serialize::pytket::OpConvertError,
PyOpConvertError,
tket2::serialize::pytket::TK1ConvertError,
PyTK1ConvertError,
"Error type for the conversion between tket2 and tket1 operations."
);

Expand Down
2 changes: 1 addition & 1 deletion tket2-py/tket2/_tket2/circuit.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,4 @@ class HugrError(Exception): ...
class BuildError(Exception): ...
class ValidationError(Exception): ...
class HUGRSerializationError(Exception): ...
class OpConvertError(Exception): ...
class TK1ConvertError(Exception): ...
4 changes: 2 additions & 2 deletions tket2-py/tket2/circuit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
BuildError,
ValidationError,
HUGRSerializationError,
OpConvertError,
TK1ConvertError,
)

from .build import CircBuild, Command
Expand All @@ -39,5 +39,5 @@
"BuildError",
"ValidationError",
"HUGRSerializationError",
"OpConvertError",
"TK1ConvertError",
]
70 changes: 25 additions & 45 deletions tket2/src/serialize/pytket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod decoder;
mod encoder;
mod op;

use hugr::types::Type;
pub(crate) use op::JsonOp;

#[cfg(test)]
Expand Down Expand Up @@ -52,8 +53,8 @@ pub trait TKETDecode: Sized {
}

impl TKETDecode for SerialCircuit {
type DecodeError = OpConvertError;
type EncodeError = OpConvertError;
type DecodeError = TK1ConvertError;
type EncodeError = TK1ConvertError;

fn decode(self) -> Result<Circuit, Self::DecodeError> {
let mut decoder = JsonDecoder::new(&self);
Expand All @@ -73,11 +74,12 @@ impl TKETDecode for SerialCircuit {
fn encode(circ: &Circuit) -> Result<Self, Self::EncodeError> {
let mut encoder = JsonEncoder::new(circ);
let f64_inputs = circ.units().filter_map(|(wire, _, t)| match (wire, t) {
(CircuitUnit::Wire(wire), t) if t == FLOAT64_TYPE => Some(wire),
(CircuitUnit::Wire(wire), t) if t == FLOAT64_TYPE => Some(Ok(wire)),
(CircuitUnit::Linear(_), _) => None,
_ => unimplemented!("Non-float64 input wires not supported"),
(_, typ) => Some(Err(TK1ConvertError::NonSerializableInputs { typ })),
});
for (i, wire) in f64_inputs.enumerate() {
let wire = wire?;
let param = format!("f{i}");
encoder.add_parameter(wire, param);
}
Expand All @@ -93,14 +95,11 @@ impl TKETDecode for SerialCircuit {
#[derive(Debug, Error)]
pub enum OpConvertError {
/// The serialized operation is not supported.
#[error("Unsupported serialized operation: {0:?}")]
#[error("Unsupported serialized pytket operation: {0:?}")]
UnsupportedSerializedOp(JsonOpType),
/// The serialized operation is not supported.
#[error("Cannot serialize operation: {0:?}")]
#[error("Cannot serialize tket2 operation: {0:?}")]
UnsupportedOpSerialization(OpType),
/// The serialized operation is not supported.
#[error("Cannot serialize operation: {0:?}")]
NonSerializableInputs(OpType),
}

/// Load a TKET1 circuit from a JSON file.
Expand Down Expand Up @@ -142,49 +141,30 @@ pub fn save_tk1_json_str(circ: &Circuit) -> Result<String, TK1ConvertError> {
let mut buf = io::BufWriter::new(Vec::new());
save_tk1_json_writer(circ, &mut buf)?;
let bytes = buf.into_inner().unwrap();
String::from_utf8(bytes).map_err(|_| TK1ConvertError::InvalidJson)
Ok(String::from_utf8(bytes)?)
}

/// Error type for conversion between `Op` and `OpType`.
#[derive(Debug, Error)]
pub enum TK1ConvertError {
/// The serialized operation is not supported.
#[error("unsupported serialized operation: {0:?}")]
UnsupportedSerializedOp(JsonOpType),
/// The serialized operation is not supported.
#[error("cannot serialize operation: {0:?}")]
UnsupportedOpSerialization(OpType),
/// The serialized operation is not supported.
#[error("cannot serialize operation: {0:?}")]
NonSerializableInputs(OpType),
/// Operation conversion error.
#[error("{0}")]
OpConversionError(#[from] OpConvertError),
/// The circuit has non-serializable inputs.
#[error("Circuit contains non-serializable input of type {typ}.")]
NonSerializableInputs {
/// The unsupported type.
typ: Type,
},
/// Invalid JSON,
#[error("Invalid pytket JSON. {0}")]
InvalidJson(#[from] serde_json::Error),
/// Invalid JSON,
#[error("invalid JSON")]
InvalidJson,
#[error("Invalid JSON encoding. {0}")]
InvalidJsonEncoding(#[from] std::string::FromUtf8Error),
/// File not found.,
#[error("unable to load file")]
FileLoadError,
}

impl From<serde_json::Error> for TK1ConvertError {
fn from(_: serde_json::Error) -> Self {
Self::InvalidJson
}
}

impl From<io::Error> for TK1ConvertError {
fn from(_: io::Error) -> Self {
Self::FileLoadError
}
}

impl From<OpConvertError> for TK1ConvertError {
fn from(value: OpConvertError) -> Self {
match value {
OpConvertError::UnsupportedSerializedOp(op) => Self::UnsupportedSerializedOp(op),
OpConvertError::UnsupportedOpSerialization(op) => Self::UnsupportedOpSerialization(op),
OpConvertError::NonSerializableInputs(op) => Self::NonSerializableInputs(op),
}
}
#[error("Unable to load pytket json file. {0}")]
FileLoadError(#[from] io::Error),
}

#[inline]
Expand Down

0 comments on commit b0b8aff

Please sign in to comment.