From 9d05e1b235dc69d704779d98be6dcf218d0961c6 Mon Sep 17 00:00:00 2001 From: Stavros Efthymiou <35475381+stavros11@users.noreply.github.com> Date: Sun, 9 Jun 2024 00:23:50 +0400 Subject: [PATCH] refactor: Explicit names in pycircuit struct --- qibo-core-py/src/circuit.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/qibo-core-py/src/circuit.rs b/qibo-core-py/src/circuit.rs index c925d42..17018fc 100644 --- a/qibo-core-py/src/circuit.rs +++ b/qibo-core-py/src/circuit.rs @@ -9,22 +9,25 @@ pub mod circuit { use super::*; #[pyclass] - struct Circuit(prelude::Circuit, usize); + struct Circuit { + circuit: prelude::Circuit, + iteration_index: usize // index for iterating circuit queue + } #[pymethods] impl Circuit { #[new] fn new(elements: usize) -> Self { - Self(prelude::Circuit::new(elements), 0) + Self { circuit: prelude::Circuit::new(elements), iteration_index: 0 } } fn add(&mut self, gate: Gate, elements: Vec) { - self.0.add(gate.to_rust(), elements); + self.circuit.add(gate.to_rust(), elements); } #[getter] fn n_elements(&self) -> usize { - self.0.n_elements() + self.circuit.n_elements() } #[getter] @@ -33,16 +36,16 @@ pub mod circuit { } fn __iter__(mut slf: PyRefMut) -> PyRefMut { - slf.1 = 0; + slf.iteration_index = 0; // reset iteration index slf } fn __next__(mut slf: PyRefMut) -> Option<(Gate, Vec)> { - let gid = slf.1; - if gid < slf.0.n_gates() { - let gate = Gate::to_python(slf.0.gates(gid)); - let targets = slf.0.elements(gid); - slf.1 += 1; + let gid = slf.iteration_index; + if gid < slf.circuit.n_gates() { + let gate = Gate::to_python(slf.circuit.gates(gid)); + let targets = slf.circuit.elements(gid); + slf.iteration_index += 1; Some((gate, targets)) } else { None @@ -50,7 +53,7 @@ pub mod circuit { } fn __str__(&self) -> String { - format!("{}", self.0) + format!("{}", self.circuit) } } }