From dc3041ea3dcd64322532daccfa5379552d9f2a38 Mon Sep 17 00:00:00 2001 From: Raynel Sanchez <87539502+raynelfss@users.noreply.github.com> Date: Fri, 28 Jun 2024 13:45:43 -0400 Subject: [PATCH] Remove: Default initialization methods from custom datatypes. - Use `__getnewargs__ instead. --- crates/circuit/src/equivalence.rs | 69 ++++++++++++++++--------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/crates/circuit/src/equivalence.rs b/crates/circuit/src/equivalence.rs index 58fbdd4265cf..e30c846d34ea 100644 --- a/crates/circuit/src/equivalence.rs +++ b/crates/circuit/src/equivalence.rs @@ -17,7 +17,7 @@ use rustworkx_core::petgraph::csr::IndexType; use rustworkx_core::petgraph::stable_graph::StableDiGraph; use rustworkx_core::petgraph::visit::IntoEdgeReferences; -use smallvec::{smallvec, SmallVec}; +use smallvec::SmallVec; use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; use std::{error::Error, fmt::Display}; @@ -42,7 +42,8 @@ mod exceptions { import_exception_bound! {qiskit.circuit.exceptions, CircuitError} } pub static PYDIGRAPH: ImportOnceCell = ImportOnceCell::new("rustworkx", "PyDiGraph"); -pub static QUANTUMCIRCUIT: ImportOnceCell = ImportOnceCell::new("qiskit.circuit", "QuantumCircuit"); +pub static QUANTUMCIRCUIT: ImportOnceCell = + ImportOnceCell::new("qiskit.circuit.quantumcircuit", "QuantumCircuit"); // Custom Structs @@ -58,7 +59,7 @@ pub struct Key { #[pymethods] impl Key { #[new] - #[pyo3(signature = (name="".to_string(), num_qubits=0))] + #[pyo3(signature = (name, num_qubits))] fn new(name: String, num_qubits: u32) -> Self { Self { name, num_qubits } } @@ -81,6 +82,10 @@ impl Key { (slf.name.clone(), slf.num_qubits) } + fn __getnewargs__(slf: PyRef) -> (String, u32) { + Key::__getstate__(slf) + } + fn __setstate__(mut slf: PyRefMut, state: (String, u32)) { slf.name = state.0; slf.num_qubits = state.1; @@ -97,17 +102,8 @@ impl Display for Key { } } -impl Default for Key { - fn default() -> Self { - Self { - name: "".to_string(), - num_qubits: 0, - } - } -} - #[pyclass(sequence, module = "qiskit._accelerate.circuit.equivalence")] -#[derive(Debug, Clone, PartialEq, Default)] +#[derive(Debug, Clone, PartialEq)] pub struct Equivalence { #[pyo3(get)] pub params: SmallVec<[Param; 3]>, @@ -118,7 +114,7 @@ pub struct Equivalence { #[pymethods] impl Equivalence { #[new] - #[pyo3(signature = (params=smallvec![], circuit=CircuitRep::default()))] + #[pyo3(signature = (params, circuit))] fn new(params: SmallVec<[Param; 3]>, circuit: CircuitRep) -> Self { Self { circuit, params } } @@ -131,8 +127,12 @@ impl Equivalence { self.eq(&other) } - fn __getstate__(slf: PyRef) -> (SmallVec<[Param; 3]>, CircuitRep) { - (slf.params.clone(), slf.circuit.clone()) + fn __getstate__(&self, py: Python) -> (PyObject, PyObject) { + (self.params.to_object(py), self.circuit.object.clone_ref(py)) + } + + fn __getnewargs__(&self, py: Python) -> (PyObject, PyObject) { + self.__getstate__(py) } fn __setstate__(mut slf: PyRefMut, state: (SmallVec<[Param; 3]>, CircuitRep)) { @@ -153,7 +153,7 @@ impl Display for Equivalence { } #[pyclass(sequence, module = "qiskit._accelerate.circuit.equivalence")] -#[derive(Debug, Clone, PartialEq, Default)] +#[derive(Debug, Clone, PartialEq)] pub struct NodeData { #[pyo3(get)] key: Key, @@ -164,7 +164,7 @@ pub struct NodeData { #[pymethods] impl NodeData { #[new] - #[pyo3(signature = (key=Key::default(), equivs=vec![]))] + #[pyo3(signature = (key, equivs))] fn new(key: Key, equivs: Vec) -> Self { Self { key, equivs } } @@ -177,8 +177,12 @@ impl NodeData { self.eq(&other) } - fn __getstate__(slf: PyRef) -> (Key, Vec) { - (slf.key.clone(), slf.equivs.clone()) + fn __getstate__(&self) -> (Key, Vec) { + (self.key.clone(), self.equivs.clone()) + } + + fn __getnewargs__(&self) -> (Key, Vec) { + self.__getstate__() } fn __setstate__(mut slf: PyRefMut, state: (Key, Vec)) { @@ -199,7 +203,7 @@ impl Display for NodeData { } #[pyclass(sequence, module = "qiskit._accelerate.circuit.equivalence")] -#[derive(Debug, Clone, PartialEq, Default)] +#[derive(Debug, Clone, PartialEq)] pub struct EdgeData { #[pyo3(get)] pub index: usize, @@ -214,7 +218,7 @@ pub struct EdgeData { #[pymethods] impl EdgeData { #[new] - #[pyo3(signature = (index=0, num_gates=0, rule=Equivalence::default(), source=Key::default()))] + #[pyo3(signature = (index, num_gates, rule, source))] fn new(index: usize, num_gates: usize, rule: Equivalence, source: Key) -> Self { Self { index, @@ -241,6 +245,15 @@ impl EdgeData { ) } + fn __getnewargs__(slf: PyRef) -> (usize, usize, Equivalence, Key) { + ( + slf.index, + slf.num_gates, + slf.rule.clone(), + slf.source.clone(), + ) + } + fn __setstate__(mut slf: PyRefMut, state: (usize, usize, Equivalence, Key)) { slf.index = state.0; slf.num_gates = state.1; @@ -366,18 +379,6 @@ impl IntoPy for CircuitRep { } } -impl Default for CircuitRep { - fn default() -> Self { - Self { - object: Python::with_gil(|py| py.None()), - num_qubits: 0, - num_clbits: 0, - params: None, - data: None, - } - } -} - // Custom Types type GraphType = StableDiGraph; type KTIType = HashMap;