Skip to content

Commit

Permalink
refactor: Explicit names in pycircuit struct
Browse files Browse the repository at this point in the history
  • Loading branch information
stavros11 committed Jun 8, 2024
1 parent 90ed2ea commit 9d05e1b
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions qibo-core-py/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize>) {
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]
Expand All @@ -33,24 +36,24 @@ pub mod circuit {
}

fn __iter__(mut slf: PyRefMut<Self>) -> PyRefMut<Self> {
slf.1 = 0;
slf.iteration_index = 0; // reset iteration index
slf
}

fn __next__(mut slf: PyRefMut<Self>) -> Option<(Gate, Vec<usize>)> {
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
}
}

fn __str__(&self) -> String {
format!("{}", self.0)
format!("{}", self.circuit)
}
}
}

0 comments on commit 9d05e1b

Please sign in to comment.