Skip to content

Commit

Permalink
refactor: Replace circuit.queue with iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
stavros11 committed Jun 8, 2024
1 parent ac67ebd commit 90ed2ea
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion qibo-core-py/examples/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
backend = NumpyBackend()
result = backend.execute_circuit(c)
print()
print(result)
print(result)
4 changes: 2 additions & 2 deletions qibo-core-py/qibo_core/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def execute_circuit(self, circuit, initial_state=None, nshots=1000):
# cast to proper complex type
state = self.cast(initial_state)

for gate in circuit.queue:
for gate, targets in circuit:
state = gate.apply_density_matrix(self, state, nqubits)

else:
Expand All @@ -440,7 +440,7 @@ def execute_circuit(self, circuit, initial_state=None, nshots=1000):
# cast to proper complex type
state = self.cast(initial_state)

for gate, targets in zip(*circuit.queue):
for gate, targets in circuit:
# TODO: Handle measurements and ``CallbackGate``
state = self.apply_gate(gate, targets, state, nqubits)

Expand Down
25 changes: 16 additions & 9 deletions qibo-core-py/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ pub mod circuit {
use super::*;

#[pyclass]
struct Circuit(prelude::Circuit);
struct Circuit(prelude::Circuit, usize);

#[pymethods]
impl Circuit {
#[new]
fn new(elements: usize) -> Self {
Self(prelude::Circuit::new(elements))
Self(prelude::Circuit::new(elements), 0)
}

fn add(&mut self, gate: Gate, elements: Vec<usize>) {
Expand All @@ -32,14 +32,21 @@ pub mod circuit {
vec![]
}

#[getter]
fn queue(&self) -> (Vec<Gate>, Vec<Vec<usize>>) {
let queue = self.0.queue();
let mut pygates = vec![];
for &gate in queue.0.iter() {
pygates.push(Gate::to_python(gate));
fn __iter__(mut slf: PyRefMut<Self>) -> PyRefMut<Self> {
slf.1 = 0;
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;
Some((gate, targets))
} else {
None
}
(pygates, queue.1)
}

fn __str__(&self) -> String {
Expand Down
16 changes: 9 additions & 7 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,16 @@ impl Circuit {
wire.into_iter().rev().collect()
}

// Total number of elements in the circuit
pub fn n_elements(&self) -> usize {
self.ends.len()
}

// Total number of gates in the circuit
pub fn n_gates(&self) -> usize {
self.gates.len()
}

pub fn wires(&self) -> Vec<Vec<Gate>> {
(0..self.n_elements()).map(|i| self.wire(i)).collect()
}
Expand Down Expand Up @@ -132,13 +138,9 @@ impl Circuit {
.collect()
}

/// List of (gate, targeted elements) in the order given by the user
pub fn queue(&self) -> (&Vec<Gate>, Vec<Vec<usize>>) {
let mut elements = vec![];
for gid in 0..self.gates.len() {
elements.push(self.elements(gid));
}
(&self.gates, elements)
/// Gate in given position
pub fn gates(&self, gid: usize) -> Gate {
self.gates[gid]
}

pub fn draw(&self) -> String {
Expand Down

0 comments on commit 90ed2ea

Please sign in to comment.