diff --git a/tket2-py/src/circuit/tk2circuit.rs b/tket2-py/src/circuit/tk2circuit.rs index 65cfe7e1..9305d269 100644 --- a/tket2-py/src/circuit/tk2circuit.rs +++ b/tket2-py/src/circuit/tk2circuit.rs @@ -98,6 +98,15 @@ impl Tk2Circuit { Ok(Tk2Circuit { circ: hugr.into() }) } + /// Load a function from a compiled guppy module, encoded as a json string. + #[staticmethod] + pub fn from_guppy_json(json: &str, function: &str) -> PyResult { + let circ = tket2::serialize::load_guppy_json_str(json, function).map_err(|e| { + PyErr::new::(format!("Invalid encoded circuit: {e}")) + })?; + Ok(Tk2Circuit { circ }) + } + /// Encode the circuit as a tket1 json string. pub fn to_tket1_json(&self) -> PyResult { Ok(serde_json::to_string(&SerialCircuit::encode(&self.circ).convert_pyerrs()?).unwrap()) @@ -106,11 +115,10 @@ impl Tk2Circuit { /// Decode a tket1 json string to a circuit. #[staticmethod] pub fn from_tket1_json(json: &str) -> PyResult { - let tk1: SerialCircuit = serde_json::from_str(json) - .map_err(|e| PyErr::new::(format!("Invalid encoded HUGR: {e}")))?; - Ok(Tk2Circuit { - circ: tk1.decode().convert_pyerrs()?, - }) + let circ = tket2::serialize::load_tk1_json_str(json).map_err(|e| { + PyErr::new::(format!("Could not load pytket circuit: {e}")) + })?; + Ok(Tk2Circuit { circ }) } /// Compute the cost of the circuit based on a per-operation cost function. diff --git a/tket2-py/tket2/_tket2/circuit.pyi b/tket2-py/tket2/_tket2/circuit.pyi index 42667fd1..9fb5982a 100644 --- a/tket2-py/tket2/_tket2/circuit.pyi +++ b/tket2-py/tket2/_tket2/circuit.pyi @@ -55,6 +55,10 @@ class Tk2Circuit: def to_tket1_json(self) -> str: """Encode the circuit as a pytket json string.""" + @staticmethod + def from_guppy_json(json: str, function: str) -> Tk2Circuit: + """Load a function from a compiled guppy module, encoded as a json string.""" + @staticmethod def from_tket1_json(json: str) -> Tk2Circuit: """Decode a pytket json string to a Tk2Circuit."""