Skip to content

Commit

Permalink
get benchmark working
Browse files Browse the repository at this point in the history
  • Loading branch information
MarquessV committed Mar 24, 2024
1 parent b0f2f08 commit 80853a6
Show file tree
Hide file tree
Showing 4 changed files with 9,984 additions and 4 deletions.
1 change: 1 addition & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fn pyquil(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
initalize_submodule(py, "instruction", m, &instruction::init_module(py)?)?;
initalize_submodule(py, "expression", m, &expression::init_module(py)?)?;
initalize_submodule(py, "primitive", m, &primitive::init_module(py)?)?;
initalize_submodule(py, "program", m, &program::init_module(py)?)?;
Ok(())
}

Expand Down
53 changes: 52 additions & 1 deletion rust/src/program.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
use std::str::FromStr;
use std::{str::FromStr, sync::Arc};

use indexmap::IndexMap;
use pyo3::{exceptions::PyValueError, prelude::*};
use quil_rs::quil::Quil;

use crate::instruction::{Declare, DefCalibration, DefMeasureCalibration, Instruction};

pub fn init_module(_py: Python<'_>) -> PyResult<Bound<'_, PyModule>> {
let m = PyModule::new_bound(_py, "program")?;
m.add_class::<Program>()?;
Ok(m)
}

#[pyclass]
#[derive(Clone, Debug)]
pub struct Program {
Expand All @@ -13,6 +20,23 @@ pub struct Program {
num_shots: u64,
}

#[pyclass]
pub struct ProgramIter {
// inner: std::vec::IntoIter<Instruction>,
inner: Box<dyn Iterator<Item = Instruction> + Send>,
}

#[pymethods]
impl ProgramIter {
fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> {
slf
}

fn __next__(mut slf: PyRefMut<'_, Self>) -> Option<Instruction> {
slf.inner.next()
}
}

#[derive(FromPyObject, Clone, Debug)]
pub enum InstructionDesignator {
Instruction(Instruction),
Expand Down Expand Up @@ -108,6 +132,19 @@ impl Program {
)
.collect()
}

fn out(&self) -> PyResult<String> {
self.inner
.to_quil()
.map_err(|e| PyValueError::new_err(format!("Could not serialize program to Quil: {e}")))
}

fn __iter__(&self, py: Python<'_>) -> PyResult<Py<ProgramIter>> {
let iter = ProgramIter {
inner: Box::new(self.clone().iter_instructions()),
};
Py::new(py, iter)
}
}

impl Program {
Expand All @@ -120,6 +157,20 @@ impl Program {
quil_rs::instruction::Declaration::new(name, descriptor.size, descriptor.sharing)
})
}

fn iter_instructions(self) -> impl Iterator<Item = Instruction> {
self.iter_declarations()
.map(|declaration| {
Instruction::from_quil_rs(quil_rs::instruction::Instruction::Declaration(
declaration,
))
})
.chain(
self.inner
.into_body_instructions()
.map(Instruction::from_quil_rs),
)
}
}

#[pymodule]
Expand Down
Loading

0 comments on commit 80853a6

Please sign in to comment.