1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use pyo3::prelude::*;
use rigetti_pyo3::create_init_submodule;

pub mod expression;
pub mod instruction;
pub mod program;
pub mod validation;

create_init_submodule! {
    submodules: [
        "expression": expression::init_submodule,
        "instructions": instruction::init_submodule,
        "program": program::init_submodule,
        "validation": validation::init_submodule
    ],
}

#[pymodule]
fn quil(py: Python<'_>, m: &PyModule) -> PyResult<()> {
    init_submodule("quil", py, m)?;
    Ok(())
}

pub fn init_quil_submodule(name: &str, py: Python<'_>, m: &PyModule) -> PyResult<()> {
    init_submodule(name, py, m)?;
    Ok(())
}

/// Implement `to_quil` and `to_quil_or_debug` methods for wrapper types whose inner type
/// implements [`Quil`](quil_rs::quil::Quil).
#[macro_export]
macro_rules! impl_to_quil {
    ($name: ident) => {
        #[pyo3::pymethods]
        impl $name {
            pub fn to_quil(&self) -> pyo3::PyResult<String> {
                quil_rs::quil::Quil::to_quil(rigetti_pyo3::PyWrapper::as_inner(self))
                    .map_err(|e| pyo3::exceptions::PyValueError::new_err(e.to_string()))
            }

            pub fn to_quil_or_debug(&self) -> String {
                quil_rs::quil::Quil::to_quil_or_debug(rigetti_pyo3::PyWrapper::as_inner(self))
            }
        }
    };
}

#[macro_export]
macro_rules! impl_eq {
    ($name: ident) => {
        #[pyo3::pymethods]
        impl $name {
            pub fn __richcmp__(
                &self,
                py: pyo3::Python<'_>,
                other: &Self,
                op: pyo3::pyclass::CompareOp,
            ) -> pyo3::PyObject {
                use pyo3::IntoPy;
                match op {
                    pyo3::pyclass::CompareOp::Eq => (self == other).into_py(py),
                    pyo3::pyclass::CompareOp::Ne => (self != other).into_py(py),
                    _ => py.NotImplemented(),
                }
            }
        }
    };
}