From b17736dcaf78552302884bc7cd131b03b4a0b5e9 Mon Sep 17 00:00:00 2001 From: Seyon Sivarajah Date: Fri, 29 Nov 2024 16:17:17 +0000 Subject: [PATCH] chore: update to pyo3 0.23.2 and tket-json-rs 0.7.1 (#709) --- Cargo.lock | 55 ++++++++++++++------------- Cargo.toml | 4 +- tket2-py/src/circuit.rs | 12 +++--- tket2-py/src/circuit/convert.rs | 4 +- tket2-py/src/circuit/tk2circuit.rs | 8 ++-- tket2-py/src/lib.rs | 2 +- tket2-py/src/ops.rs | 2 +- tket2-py/src/optimiser.rs | 2 +- tket2-py/src/passes.rs | 15 +++----- tket2-py/src/pattern.rs | 6 +-- tket2-py/src/pattern/portmatching.rs | 2 +- tket2-py/src/rewrite.rs | 2 +- tket2-py/src/types.rs | 2 +- tket2/src/serialize/pytket.rs | 29 ++++++++++++-- tket2/src/serialize/pytket/decoder.rs | 16 ++++---- tket2/src/serialize/pytket/encoder.rs | 26 +++++++------ tket2/src/serialize/pytket/tests.rs | 16 +++++--- 17 files changed, 114 insertions(+), 89 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 95e00df9..77e85b6b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -197,9 +197,9 @@ checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" [[package]] name = "cc" -version = "1.2.1" +version = "1.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd9de9f2205d5ef3fd67e685b0df337994ddd4495e2a28d185500d0e1edfea47" +checksum = "f34d93e62b03caf570cccc334cbc6c2fceca82f39211051345108adcba3eebdc" dependencies = [ "jobserver", "libc", @@ -648,12 +648,12 @@ dependencies = [ [[package]] name = "errno" -version = "0.3.9" +version = "0.3.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" +checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -858,7 +858,7 @@ dependencies = [ [[package]] name = "hugr" version = "0.13.3" -source = "git+https://github.com/CQCL/hugr.git#bcac68d3c147e960b921927f58b2fca8c86ccdc7" +source = "git+https://github.com/CQCL/hugr.git#163b211cfb8b87a7b0a6a12c8141217e0c052dd5" dependencies = [ "hugr-core", "hugr-passes", @@ -867,7 +867,7 @@ dependencies = [ [[package]] name = "hugr-cli" version = "0.13.3" -source = "git+https://github.com/CQCL/hugr.git#bcac68d3c147e960b921927f58b2fca8c86ccdc7" +source = "git+https://github.com/CQCL/hugr.git#163b211cfb8b87a7b0a6a12c8141217e0c052dd5" dependencies = [ "clap", "clap-verbosity-flag", @@ -882,7 +882,7 @@ dependencies = [ [[package]] name = "hugr-core" version = "0.13.3" -source = "git+https://github.com/CQCL/hugr.git#bcac68d3c147e960b921927f58b2fca8c86ccdc7" +source = "git+https://github.com/CQCL/hugr.git#163b211cfb8b87a7b0a6a12c8141217e0c052dd5" dependencies = [ "bitvec", "bumpalo", @@ -914,7 +914,7 @@ dependencies = [ [[package]] name = "hugr-passes" version = "0.13.3" -source = "git+https://github.com/CQCL/hugr.git#bcac68d3c147e960b921927f58b2fca8c86ccdc7" +source = "git+https://github.com/CQCL/hugr.git#163b211cfb8b87a7b0a6a12c8141217e0c052dd5" dependencies = [ "hugr-core", "itertools 0.13.0", @@ -1203,9 +1203,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" -version = "0.2.166" +version = "0.2.167" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2ccc108bbc0b1331bd061864e7cd823c0cab660bbe6970e66e2c0614decde36" +checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc" [[package]] name = "linux-raw-sys" @@ -1581,9 +1581,9 @@ dependencies = [ [[package]] name = "pyo3" -version = "0.22.6" +version = "0.23.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f402062616ab18202ae8319da13fa4279883a2b8a9d9f83f20dbade813ce1884" +checksum = "f54b3d09cbdd1f8c20650b28e7b09e338881482f4aa908a5f61a00c98fba2690" dependencies = [ "cfg-if", "indoc", @@ -1599,9 +1599,9 @@ dependencies = [ [[package]] name = "pyo3-build-config" -version = "0.22.6" +version = "0.23.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b14b5775b5ff446dd1056212d778012cbe8a0fbffd368029fd9e25b514479c38" +checksum = "3015cf985888fe66cfb63ce0e321c603706cd541b7aec7ddd35c281390af45d8" dependencies = [ "once_cell", "target-lexicon", @@ -1609,9 +1609,9 @@ dependencies = [ [[package]] name = "pyo3-ffi" -version = "0.22.6" +version = "0.23.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ab5bcf04a2cdcbb50c7d6105de943f543f9ed92af55818fd17b660390fc8636" +checksum = "6fca7cd8fd809b5ac4eefb89c1f98f7a7651d3739dfb341ca6980090f554c270" dependencies = [ "libc", "pyo3-build-config", @@ -1619,9 +1619,9 @@ dependencies = [ [[package]] name = "pyo3-macros" -version = "0.22.6" +version = "0.23.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fd24d897903a9e6d80b968368a34e1525aeb719d568dba8b3d4bfa5dc67d453" +checksum = "34e657fa5379a79151b6ff5328d9216a84f55dc93b17b08e7c3609a969b73aa0" dependencies = [ "proc-macro2", "pyo3-macros-backend", @@ -1631,9 +1631,9 @@ dependencies = [ [[package]] name = "pyo3-macros-backend" -version = "0.22.6" +version = "0.23.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36c011a03ba1e50152b4b394b479826cad97e7a21eb52df179cd91ac411cbfbe" +checksum = "295548d5ffd95fd1981d2d3cf4458831b21d60af046b729b6fd143b0ba7aee2f" dependencies = [ "heck", "proc-macro2", @@ -1644,9 +1644,9 @@ dependencies = [ [[package]] name = "pythonize" -version = "0.22.0" +version = "0.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90fcf491425978bd889015d5430f6473d91bdfa2097262f1e731aadcf6c2113e" +checksum = "91a6ee7a084f913f98d70cdc3ebec07e852b735ae3059a1500db2661265da9ff" dependencies = [ "pyo3", "serde", @@ -2133,10 +2133,11 @@ dependencies = [ [[package]] name = "tket-json-rs" -version = "0.6.2" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "45811ae5d7deb7c7e14bbf031e75b2ab7db2154ed56d1ed2eb9bb28a0a0df4b8" +checksum = "bbce51b8204122752da67c65d5cf13a9b8f09f10664d1fb40aa03f94bebdd1cb" dependencies = [ + "derive_more 1.0.0", "pyo3", "pythonize", "serde", @@ -2515,9 +2516,9 @@ dependencies = [ [[package]] name = "webbrowser" -version = "1.0.2" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e5f07fb9bc8de2ddfe6b24a71a75430673fd679e568c48b52716cef1cfae923" +checksum = "ea9fe1ebb156110ff855242c1101df158b822487e4957b0556d9ffce9db0f535" dependencies = [ "block2", "core-foundation", diff --git a/Cargo.toml b/Cargo.toml index 6a774821..e9f18c8a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,9 +37,9 @@ hugr = "0.13.3" hugr-core = "0.13.3" hugr-cli = "0.13.3" portgraph = "0.12" -pyo3 = "0.22.5" +pyo3 = "0.23.2" itertools = "0.13.0" -tket-json-rs = "0.6.2" +tket-json-rs = "0.7.1" tracing = "0.1.37" portmatching = "0.3.1" bytemuck = "1.20.0" diff --git a/tket2-py/src/circuit.rs b/tket2-py/src/circuit.rs index a70e3559..3c939a20 100644 --- a/tket2-py/src/circuit.rs +++ b/tket2-py/src/circuit.rs @@ -30,7 +30,7 @@ pub use tket2::{Pauli, Tk2Op}; /// The module definition pub fn module(py: Python<'_>) -> PyResult> { - let m = PyModule::new_bound(py, "circuit")?; + let m = PyModule::new(py, "circuit")?; m.add_class::()?; m.add_class::()?; m.add_class::()?; @@ -40,14 +40,14 @@ pub fn module(py: Python<'_>) -> PyResult> { m.add_function(wrap_pyfunction!(render_circuit_dot, &m)?)?; m.add_function(wrap_pyfunction!(render_circuit_mermaid, &m)?)?; - m.add("HugrError", py.get_type_bound::())?; - m.add("BuildError", py.get_type_bound::())?; - m.add("ValidationError", py.get_type_bound::())?; + m.add("HugrError", py.get_type::())?; + m.add("BuildError", py.get_type::())?; + m.add("ValidationError", py.get_type::())?; m.add( "HUGRSerializationError", - py.get_type_bound::(), + py.get_type::(), )?; - m.add("TK1ConvertError", py.get_type_bound::())?; + m.add("TK1ConvertError", py.get_type::())?; Ok(m) } diff --git a/tket2-py/src/circuit/convert.rs b/tket2-py/src/circuit/convert.rs index 00d5404f..93fb1a8f 100644 --- a/tket2-py/src/circuit/convert.rs +++ b/tket2-py/src/circuit/convert.rs @@ -11,8 +11,8 @@ use itertools::Itertools; use pyo3::exceptions::{PyAttributeError, PyValueError}; use pyo3::types::{PyAnyMethods, PyModule, PyString, PyTypeMethods}; use pyo3::{ - pyclass, pymethods, Bound, FromPyObject, PyAny, PyErr, PyObject, PyRefMut, PyResult, - PyTypeInfo, Python, ToPyObject, + pyclass, pymethods, Bound, FromPyObject, IntoPyObject, PyAny, PyErr, PyObject, PyRefMut, + PyResult, PyTypeInfo, Python, }; use derive_more::From; diff --git a/tket2-py/src/circuit/tk2circuit.rs b/tket2-py/src/circuit/tk2circuit.rs index 9a1bb283..d824356b 100644 --- a/tket2-py/src/circuit/tk2circuit.rs +++ b/tket2-py/src/circuit/tk2circuit.rs @@ -15,8 +15,8 @@ use itertools::Itertools; use pyo3::exceptions::{PyAttributeError, PyValueError}; use pyo3::types::{PyAnyMethods, PyModule, PyString, PyTypeMethods}; use pyo3::{ - pyclass, pymethods, Bound, FromPyObject, PyAny, PyErr, PyObject, PyRef, PyRefMut, PyResult, - PyTypeInfo, Python, ToPyObject, + pyclass, pymethods, Bound, FromPyObject, IntoPyObject, PyAny, PyErr, PyObject, PyRef, PyRefMut, + PyResult, PyTypeInfo, Python, }; use derive_more::From; @@ -177,9 +177,7 @@ impl Tk2Circuit { }; let tk2_py_op = PyTk2Op::from(tk2_op); let cost = cost_fn.call1((tk2_py_op,))?; - Ok(PyCircuitCost { - cost: cost.to_object(py), - }) + Ok(PyCircuitCost { cost: cost.into() }) }; let circ_cost = self.circ.circuit_cost(cost_fn)?; Ok(circ_cost.cost.into_bound(py)) diff --git a/tket2-py/src/lib.rs b/tket2-py/src/lib.rs index 1ebf3969..d86ec71b 100644 --- a/tket2-py/src/lib.rs +++ b/tket2-py/src/lib.rs @@ -32,7 +32,7 @@ fn add_submodule(py: Python, parent: &Bound, submodule: Bound) -> PyResult> { - let m = PyModule::new_bound(py, "ops")?; + let m = PyModule::new(py, "ops")?; m.add_class::()?; m.add_class::()?; m.add_class::()?; diff --git a/tket2-py/src/optimiser.rs b/tket2-py/src/optimiser.rs index a3005dc6..345a7545 100644 --- a/tket2-py/src/optimiser.rs +++ b/tket2-py/src/optimiser.rs @@ -13,7 +13,7 @@ use crate::circuit::update_circ; /// The module definition pub fn module(py: Python<'_>) -> PyResult> { - let m = PyModule::new_bound(py, "optimiser")?; + let m = PyModule::new(py, "optimiser")?; m.add_class::()?; Ok(m) } diff --git a/tket2-py/src/passes.rs b/tket2-py/src/passes.rs index c33fce64..eb7edb5f 100644 --- a/tket2-py/src/passes.rs +++ b/tket2-py/src/passes.rs @@ -20,16 +20,13 @@ use crate::{ /// /// This module is re-exported from the python module with the same name. pub fn module(py: Python<'_>) -> PyResult> { - let m = PyModule::new_bound(py, "passes")?; + let m = PyModule::new(py, "passes")?; m.add_function(wrap_pyfunction!(greedy_depth_reduce, &m)?)?; m.add_function(wrap_pyfunction!(lower_to_pytket, &m)?)?; m.add_function(wrap_pyfunction!(badger_optimise, &m)?)?; m.add_class::()?; m.add_function(wrap_pyfunction!(self::chunks::chunks, &m)?)?; - m.add( - "PullForwardError", - py.get_type_bound::(), - )?; + m.add("PullForwardError", py.get_type::())?; Ok(m) } @@ -66,11 +63,11 @@ fn greedy_depth_reduce<'py>(circ: &Bound<'py, PyAny>) -> PyResult<(Bound<'py, Py fn rebase_nam(circ: &Bound) -> PyResult<()> { let py = circ.py(); let auto_rebase = py - .import_bound("pytket.passes.auto_rebase")? + .import("pytket.passes.auto_rebase")? .getattr("auto_rebase_pass")?; - let optype = py.import_bound("pytket")?.getattr("OpType")?; - let locals = [("OpType", &optype)].into_py_dict_bound(py); - let op_set = py.eval_bound("{OpType.CX, OpType.Rz, OpType.H}", None, Some(&locals))?; + let optype = py.import("pytket")?.getattr("OpType")?; + let locals = [("OpType", &optype)].into_py_dict(py)?; + let op_set = py.eval(c"{OpType.CX, OpType.Rz, OpType.H}", None, Some(&locals))?; let rebase_pass = auto_rebase.call1((op_set,))?.getattr("apply")?; rebase_pass.call1((circ,)).map(|_| ()) } diff --git a/tket2-py/src/pattern.rs b/tket2-py/src/pattern.rs index 7ffa40be..4afb8520 100644 --- a/tket2-py/src/pattern.rs +++ b/tket2-py/src/pattern.rs @@ -13,7 +13,7 @@ use tket2::Circuit; /// The module definition pub fn module(py: Python<'_>) -> PyResult> { - let m = PyModule::new_bound(py, "pattern")?; + let m = PyModule::new(py, "pattern")?; m.add_class::()?; m.add_class::()?; m.add_class::()?; @@ -23,11 +23,11 @@ pub fn module(py: Python<'_>) -> PyResult> { m.add( "InvalidPatternError", - py.get_type_bound::(), + py.get_type::(), )?; m.add( "InvalidReplacementError", - py.get_type_bound::(), + py.get_type::(), )?; Ok(m) diff --git a/tket2-py/src/pattern/portmatching.rs b/tket2-py/src/pattern/portmatching.rs index 02747884..baae2002 100644 --- a/tket2-py/src/pattern/portmatching.rs +++ b/tket2-py/src/pattern/portmatching.rs @@ -64,7 +64,7 @@ impl PyPatternMatcher { pub fn py_from_patterns(patterns: &Bound) -> PyResult { Ok(PatternMatcher::from_patterns( patterns - .iter()? + .try_iter()? .map(|p| { let py_pattern = p?.extract::()?; Ok(py_pattern.pattern) diff --git a/tket2-py/src/rewrite.rs b/tket2-py/src/rewrite.rs index 18c34b41..24a9c626 100644 --- a/tket2-py/src/rewrite.rs +++ b/tket2-py/src/rewrite.rs @@ -10,7 +10,7 @@ use crate::circuit::{PyNode, Tk2Circuit}; /// The module definition pub fn module(py: Python<'_>) -> PyResult> { - let m = PyModule::new_bound(py, "rewrite")?; + let m = PyModule::new(py, "rewrite")?; m.add_class::()?; m.add_class::()?; m.add_class::()?; diff --git a/tket2-py/src/types.rs b/tket2-py/src/types.rs index 273f58b1..8ee997c0 100644 --- a/tket2-py/src/types.rs +++ b/tket2-py/src/types.rs @@ -9,7 +9,7 @@ use std::fmt; /// The module definition pub fn module(py: Python<'_>) -> PyResult> { - let m = PyModule::new_bound(py, "types")?; + let m = PyModule::new(py, "types")?; m.add_class::()?; m.add_class::()?; diff --git a/tket2/src/serialize/pytket.rs b/tket2/src/serialize/pytket.rs index 9013f77c..ad117d27 100644 --- a/tket2/src/serialize/pytket.rs +++ b/tket2/src/serialize/pytket.rs @@ -23,8 +23,9 @@ use std::{fs, io}; use hugr::ops::{NamedOp, OpType}; use derive_more::{Display, Error, From}; -use tket_json_rs::circuit_json::{self, SerialCircuit}; +use tket_json_rs::circuit_json::SerialCircuit; use tket_json_rs::optype::OpType as SerialOpType; +use tket_json_rs::register::{Bit, ElementId, Qubit}; use crate::circuit::Circuit; @@ -244,7 +245,7 @@ pub enum OpConvertError { /// The expected number of bits. expected_bits: usize, /// The given of parameters. - args: Vec, + args: Vec, }, } @@ -290,8 +291,28 @@ struct RegisterHash { hash: u64, } -impl From<&circuit_json::Register> for RegisterHash { - fn from(reg: &circuit_json::Register) -> Self { +impl From<&ElementId> for RegisterHash { + fn from(reg: &ElementId) -> Self { + let mut hasher = DefaultHasher::new(); + reg.hash(&mut hasher); + Self { + hash: hasher.finish(), + } + } +} + +impl From<&Qubit> for RegisterHash { + fn from(reg: &Qubit) -> Self { + let mut hasher = DefaultHasher::new(); + reg.hash(&mut hasher); + Self { + hash: hasher.finish(), + } + } +} + +impl From<&Bit> for RegisterHash { + fn from(reg: &Bit) -> Self { let mut hasher = DefaultHasher::new(); reg.hash(&mut hasher); Self { diff --git a/tket2/src/serialize/pytket/decoder.rs b/tket2/src/serialize/pytket/decoder.rs index 97cd7fc7..f4e46f40 100644 --- a/tket2/src/serialize/pytket/decoder.rs +++ b/tket2/src/serialize/pytket/decoder.rs @@ -17,6 +17,7 @@ use itertools::{EitherOrBoth, Itertools}; use serde_json::json; use tket_json_rs::circuit_json; use tket_json_rs::circuit_json::SerialCircuit; +use tket_json_rs::register; use super::op::Tk1Op; use super::param::decode::{parse_pytket_param, PytketParam}; @@ -80,18 +81,18 @@ impl Tk1Decoder { // named 2, at position 1 the register originally named 0, and so on. let mut output_qubits = Vec::with_capacity(serialcirc.qubits.len()); let mut output_bits = Vec::with_capacity(serialcirc.bits.len()); - let output_to_input: HashMap = serialcirc + let output_to_input: HashMap = serialcirc .implicit_permutation .iter() - .map(|p| (p.1.clone(), p.0.clone())) + .map(|p| (p.1.clone().id, p.0.clone().id)) .collect(); for qubit in &serialcirc.qubits { // For each output position, find the input register that should be there. - output_qubits.push(output_to_input.get(qubit).unwrap_or(qubit).clone()); + output_qubits.push(output_to_input.get(&qubit.id).unwrap_or(&qubit.id).clone()); } for bit in &serialcirc.bits { // For each output position, find the input register that should be there. - output_bits.push(output_to_input.get(bit).unwrap_or(bit).clone()); + output_bits.push(output_to_input.get(&bit.id).unwrap_or(&bit.id).clone()); } dfg.set_metadata(METADATA_Q_OUTPUT_REGISTERS, json!(output_qubits)); dfg.set_metadata(METADATA_B_OUTPUT_REGISTERS, json!(output_bits)); @@ -101,7 +102,8 @@ impl Tk1Decoder { let ordered_registers = serialcirc .qubits .iter() - .chain(&serialcirc.bits) + .map(|qb| &qb.id) + .chain(serialcirc.bits.iter().map(|bit| &bit.id)) .map(|reg| { check_register(reg)?; Ok(RegisterHash::from(reg)) @@ -192,7 +194,7 @@ impl Tk1Decoder { fn get_op_wires( &mut self, tk1op: &Tk1Op, - args: &[circuit_json::Register], + args: &[register::ElementId], params: Vec, ) -> Result<(Vec, Vec), OpConvertError> { // Arguments are always ordered with qubits first, and then bits. @@ -343,7 +345,7 @@ impl Tk1Decoder { } /// Only single-indexed registers are supported. -fn check_register(register: &circuit_json::Register) -> Result<(), TK1ConvertError> { +fn check_register(register: ®ister::ElementId) -> Result<(), TK1ConvertError> { if register.1.len() != 1 { Err(TK1ConvertError::MultiIndexedRegister { register: register.0.clone(), diff --git a/tket2/src/serialize/pytket/encoder.rs b/tket2/src/serialize/pytket/encoder.rs index e0258871..9cae5b27 100644 --- a/tket2/src/serialize/pytket/encoder.rs +++ b/tket2/src/serialize/pytket/encoder.rs @@ -9,8 +9,8 @@ use hugr::std_extensions::arithmetic::float_types::FLOAT64_TYPE; use hugr::types::Type; use hugr::{HugrView, Wire}; use itertools::Itertools; -use tket_json_rs::circuit_json::Register as RegisterUnit; use tket_json_rs::circuit_json::{self, SerialCircuit}; +use tket_json_rs::register::ElementId as RegisterUnit; use crate::circuit::command::{CircuitUnit, Command}; use crate::circuit::Circuit; @@ -221,15 +221,14 @@ impl Tk1Encoder { let mut implicit_permutation = qubits_permutation; implicit_permutation.append(&mut bits_permutation); - SerialCircuit { - name: self.name, - phase: self.phase, - commands: self.commands, - qubits, - bits, - implicit_permutation, - number_of_ws: None, - } + let mut ser = SerialCircuit::new(self.name, self.phase); + + ser.commands = self.commands; + ser.qubits = qubits.into_iter().map_into().collect(); + ser.bits = bits.into_iter().map_into().collect(); + ser.implicit_permutation = implicit_permutation; + ser.number_of_ws = None; + ser } /// Translate a linear [`CircuitUnit`] into a [`RegisterUnit`], if possible. @@ -355,7 +354,7 @@ impl QubitTracker { let permutation = outputs .into_iter() .zip(&self.inputs) - .map(|(out, inp)| circuit_json::ImplicitPermutation(inp.clone(), out)) + .map(|(out, inp)| circuit_json::ImplicitPermutation(inp.clone().into(), out.into())) .collect_vec(); (self.inputs, permutation) @@ -535,7 +534,10 @@ impl BitTracker { let permutation = original_permutation .into_iter() .map(|(inp, out)| { - circuit_json::ImplicitPermutation(inp, circuit_permutation.remove(&out).unwrap()) + circuit_json::ImplicitPermutation( + inp.into(), + circuit_permutation.remove(&out).unwrap().into(), + ) }) .collect_vec(); diff --git a/tket2/src/serialize/pytket/tests.rs b/tket2/src/serialize/pytket/tests.rs index 37d37f58..ce5ceb00 100644 --- a/tket2/src/serialize/pytket/tests.rs +++ b/tket2/src/serialize/pytket/tests.rs @@ -13,6 +13,7 @@ use hugr::HugrView; use rstest::{fixture, rstest}; use tket_json_rs::circuit_json::{self, SerialCircuit}; use tket_json_rs::optype; +use tket_json_rs::register; use super::{TKETDecode, METADATA_INPUT_PARAMETERS, METADATA_Q_OUTPUT_REGISTERS}; use crate::circuit::Circuit; @@ -77,21 +78,24 @@ fn validate_serial_circ(circ: &SerialCircuit) { for command in &circ.commands { for arg in &command.args { assert!( - circ.qubits.contains(arg) || circ.bits.contains(arg), + circ.qubits.contains(®ister::Qubit::from(arg.clone())) + || circ.bits.contains(®ister::Bit::from(arg.clone())), "Circuit command {command:?} has an invalid argument '{arg:?}'" ); } } // Check that the implicit permutation is valid. - let perm: HashMap = circ + let perm: HashMap = circ .implicit_permutation .iter() - .map(|p| (p.0.clone(), p.1.clone())) + .map(|p| (p.0.clone().id, p.1.clone().id)) .collect(); for (key, value) in &perm { - let valid_qubits = circ.qubits.contains(key) && circ.qubits.contains(value); - let valid_bits = circ.bits.contains(key) && circ.bits.contains(value); + let valid_qubits = circ.qubits.contains(®ister::Qubit::from(key.clone())) + && circ.qubits.contains(®ister::Qubit::from(value.clone())); + let valid_bits = circ.bits.contains(®ister::Bit::from(key.clone())) + && circ.bits.contains(®ister::Bit::from(value.clone())); assert!( valid_qubits || valid_bits, "Circuit has an invalid permutation '{key:?} -> {value:?}'" @@ -103,7 +107,7 @@ fn validate_serial_circ(circ: &SerialCircuit) { "Circuit has duplicate permutations", ); assert_eq!( - HashSet::<&circuit_json::Register>::from_iter(perm.values()).len(), + HashSet::<®ister::ElementId>::from_iter(perm.values()).len(), perm.len(), "Circuit has duplicate values in permutations" );