Skip to main content

quil/instruction/
classical.rs

1use quil_rs::instruction::{
2    Arithmetic, ArithmeticOperand, ArithmeticOperator, BinaryLogic, BinaryOperand, BinaryOperator,
3    Comparison, ComparisonOperand, ComparisonOperator, Convert, Exchange, MemoryReference, Move,
4    UnaryLogic, UnaryOperator,
5};
6
7use rigetti_pyo3::{
8    impl_hash, impl_repr, py_wrap_data_struct, py_wrap_simple_enum, py_wrap_union_enum,
9    pyo3::{
10        pymethods,
11        types::{PyFloat, PyInt},
12        Py, PyResult, Python,
13    },
14    PyTryFrom,
15};
16
17use super::PyMemoryReference;
18use crate::{impl_copy_for_instruction, impl_eq, impl_pickle_for_instruction, impl_to_quil};
19
20py_wrap_data_struct! {
21    #[derive(Debug, PartialEq)]
22    #[pyo3(subclass, module = "quil.instructions")]
23    PyArithmetic(Arithmetic) as "Arithmetic" {
24        operator: ArithmeticOperator => PyArithmeticOperator,
25        destination: MemoryReference => PyMemoryReference,
26        source: ArithmeticOperand => PyArithmeticOperand
27    }
28}
29impl_repr!(PyArithmetic);
30impl_to_quil!(PyArithmetic);
31impl_copy_for_instruction!(PyArithmetic);
32impl_hash!(PyArithmetic);
33impl_eq!(PyArithmetic);
34impl_pickle_for_instruction!(PyArithmetic);
35
36#[pymethods]
37impl PyArithmetic {
38    #[new]
39    pub fn new(
40        py: Python<'_>,
41        operator: PyArithmeticOperator,
42        destination: PyMemoryReference,
43        source: PyArithmeticOperand,
44    ) -> PyResult<Self> {
45        Ok(PyArithmetic(Arithmetic::new(
46            ArithmeticOperator::py_try_from(py, &operator)?,
47            MemoryReference::py_try_from(py, &destination)?,
48            ArithmeticOperand::py_try_from(py, &source)?,
49        )))
50    }
51}
52
53py_wrap_union_enum! {
54    #[derive(Debug, PartialEq)]
55    PyArithmeticOperand(ArithmeticOperand) as "ArithmeticOperand" {
56        literal_integer: LiteralInteger => Py<PyInt>,
57        literal_real: LiteralReal => Py<PyFloat>,
58        memory_reference: MemoryReference => PyMemoryReference
59    }
60}
61impl_repr!(PyArithmeticOperand);
62impl_to_quil!(PyArithmeticOperand);
63impl_hash!(PyArithmeticOperand);
64impl_eq!(PyArithmeticOperand);
65
66py_wrap_simple_enum! {
67    #[derive(Debug, PartialEq)]
68    PyArithmeticOperator(ArithmeticOperator) as "ArithmeticOperator" {
69        Add,
70        Subtract,
71        Divide,
72        Multiply
73    }
74}
75impl_repr!(PyArithmeticOperator);
76impl_to_quil!(PyArithmeticOperator);
77impl_hash!(PyArithmeticOperator);
78impl_eq!(PyArithmeticOperator);
79
80py_wrap_union_enum! {
81    #[derive(Debug, PartialEq, Eq)]
82    #[pyo3(subclass)]
83    PyBinaryOperand(BinaryOperand) as "BinaryOperand" {
84        literal_integer: LiteralInteger => Py<PyInt>,
85        memory_reference: MemoryReference => PyMemoryReference
86    }
87}
88impl_repr!(PyBinaryOperand);
89impl_to_quil!(PyBinaryOperand);
90impl_hash!(PyBinaryOperand);
91impl_eq!(PyBinaryOperand);
92
93py_wrap_simple_enum! {
94    #[derive(Debug, PartialEq, Eq)]
95    PyBinaryOperator(BinaryOperator) as "BinaryOperator" {
96        And,
97        Ior,
98        Xor
99    }
100}
101impl_repr!(PyBinaryOperator);
102impl_to_quil!(PyBinaryOperator);
103impl_hash!(PyBinaryOperator);
104impl_eq!(PyBinaryOperator);
105
106py_wrap_data_struct! {
107    #[derive(Debug, PartialEq, Eq)]
108    #[pyo3(subclass, module = "quil.instructions")]
109    PyBinaryLogic(BinaryLogic) as "BinaryLogic" {
110        operator: BinaryOperator => PyBinaryOperator,
111        destination: MemoryReference => PyMemoryReference,
112        source: BinaryOperand => PyBinaryOperand
113    }
114}
115impl_repr!(PyBinaryLogic);
116impl_to_quil!(PyBinaryLogic);
117impl_copy_for_instruction!(PyBinaryLogic);
118impl_eq!(PyBinaryLogic);
119impl_pickle_for_instruction!(PyBinaryLogic);
120
121#[pymethods]
122impl PyBinaryLogic {
123    #[new]
124    pub fn new(
125        py: Python<'_>,
126        operator: PyBinaryOperator,
127        destination: PyMemoryReference,
128        source: PyBinaryOperand,
129    ) -> PyResult<Self> {
130        Ok(PyBinaryLogic(BinaryLogic::new(
131            BinaryOperator::py_try_from(py, &operator)?,
132            MemoryReference::py_try_from(py, &destination)?,
133            BinaryOperand::py_try_from(py, &source)?,
134        )))
135    }
136}
137
138py_wrap_data_struct! {
139    #[derive(Debug, PartialEq, Eq)]
140    #[pyo3(subclass, module = "quil.instructions")]
141    PyConvert(Convert) as "Convert" {
142        destination: MemoryReference => PyMemoryReference,
143        source: MemoryReference => PyMemoryReference
144    }
145}
146impl_repr!(PyConvert);
147impl_to_quil!(PyConvert);
148impl_copy_for_instruction!(PyConvert);
149impl_hash!(PyConvert);
150impl_eq!(PyConvert);
151impl_pickle_for_instruction!(PyConvert);
152
153#[pymethods]
154impl PyConvert {
155    #[new]
156    fn new(
157        py: Python<'_>,
158        destination: PyMemoryReference,
159        source: PyMemoryReference,
160    ) -> PyResult<Self> {
161        Ok(Self(Convert::new(
162            MemoryReference::py_try_from(py, &destination)?,
163            MemoryReference::py_try_from(py, &source)?,
164        )))
165    }
166}
167
168py_wrap_data_struct! {
169    #[derive(Debug, PartialEq)]
170    #[pyo3(subclass, module = "quil.instructions")]
171    PyMove(Move) as "Move" {
172        destination: MemoryReference => PyMemoryReference,
173        source: ArithmeticOperand => PyArithmeticOperand
174    }
175}
176impl_repr!(PyMove);
177impl_to_quil!(PyMove);
178impl_copy_for_instruction!(PyMove);
179impl_hash!(PyMove);
180impl_eq!(PyMove);
181impl_pickle_for_instruction!(PyMove);
182
183#[pymethods]
184impl PyMove {
185    #[new]
186    fn new(
187        py: Python<'_>,
188        destination: PyMemoryReference,
189        source: PyArithmeticOperand,
190    ) -> PyResult<Self> {
191        Ok(Self(Move::new(
192            MemoryReference::py_try_from(py, &destination)?,
193            ArithmeticOperand::py_try_from(py, &source)?,
194        )))
195    }
196}
197
198py_wrap_data_struct! {
199    #[derive(Debug, PartialEq)]
200    #[pyo3(subclass, module = "quil.instructions")]
201    PyExchange(Exchange) as "Exchange" {
202        left: MemoryReference => PyMemoryReference,
203        right: MemoryReference => PyMemoryReference
204    }
205}
206impl_repr!(PyExchange);
207impl_to_quil!(PyExchange);
208impl_copy_for_instruction!(PyExchange);
209impl_hash!(PyExchange);
210impl_eq!(PyExchange);
211impl_pickle_for_instruction!(PyExchange);
212
213#[pymethods]
214impl PyExchange {
215    #[new]
216    pub fn new(
217        py: Python<'_>,
218        left: PyMemoryReference,
219        right: PyMemoryReference,
220    ) -> PyResult<Self> {
221        Ok(Self(Exchange::new(
222            MemoryReference::py_try_from(py, &left)?,
223            MemoryReference::py_try_from(py, &right)?,
224        )))
225    }
226}
227
228py_wrap_union_enum! {
229    #[derive(Debug, PartialEq)]
230    #[pyo3(subclass)]
231    PyComparisonOperand(ComparisonOperand) as "ComparisonOperand" {
232        literal_integer: LiteralInteger => Py<PyInt>,
233        literal_real: LiteralReal => Py<PyFloat>,
234        memory_reference: MemoryReference => PyMemoryReference
235    }
236}
237impl_repr!(PyComparisonOperand);
238impl_to_quil!(PyComparisonOperand);
239impl_hash!(PyComparisonOperand);
240
241py_wrap_simple_enum! {
242    #[derive(Debug, PartialEq, Eq)]
243    PyComparisonOperator(ComparisonOperator) as "ComparisonOperator" {
244        Equal,
245        GreaterThanOrEqual,
246        GreaterThan,
247        LessThanOrEqual,
248        LessThan
249    }
250}
251
252py_wrap_data_struct! {
253    #[derive(Debug, PartialEq)]
254    #[pyo3(subclass, module = "quil.instructions")]
255    PyComparison(Comparison) as "Comparison" {
256        operator: ComparisonOperator => PyComparisonOperator,
257        destination: MemoryReference => PyMemoryReference,
258        lhs: MemoryReference => PyMemoryReference,
259        rhs: ComparisonOperand => PyComparisonOperand
260    }
261}
262impl_repr!(PyComparison);
263impl_to_quil!(PyComparison);
264impl_copy_for_instruction!(PyComparison);
265impl_hash!(PyComparison);
266impl_eq!(PyComparison);
267impl_pickle_for_instruction!(PyComparison);
268
269#[pymethods]
270impl PyComparison {
271    #[new]
272    pub fn new(
273        py: Python<'_>,
274        operator: PyComparisonOperator,
275        destination: PyMemoryReference,
276        lhs: PyMemoryReference,
277        rhs: PyComparisonOperand,
278    ) -> PyResult<Self> {
279        Ok(Self(Comparison::new(
280            ComparisonOperator::py_try_from(py, &operator)?,
281            MemoryReference::py_try_from(py, &destination)?,
282            MemoryReference::py_try_from(py, &lhs)?,
283            ComparisonOperand::py_try_from(py, &rhs)?,
284        )))
285    }
286}
287
288py_wrap_simple_enum! {
289    #[derive(Debug, PartialEq, Eq)]
290    PyUnaryOperator(UnaryOperator) as "UnaryOperator" {
291        Neg,
292        Not
293    }
294}
295impl_repr!(PyUnaryOperator);
296impl_to_quil!(PyUnaryOperator);
297impl_hash!(PyUnaryOperator);
298
299py_wrap_data_struct! {
300    #[derive(Debug, PartialEq, Eq)]
301    #[pyo3(subclass, module = "quil.instructions")]
302    PyUnaryLogic(UnaryLogic) as "UnaryLogic" {
303        operator: UnaryOperator => PyUnaryOperator,
304        operand: MemoryReference => PyMemoryReference
305    }
306}
307impl_repr!(PyUnaryLogic);
308impl_to_quil!(PyUnaryLogic);
309impl_copy_for_instruction!(PyUnaryLogic);
310impl_hash!(PyUnaryLogic);
311impl_eq!(PyUnaryLogic);
312impl_pickle_for_instruction!(PyUnaryLogic);
313
314#[pymethods]
315impl PyUnaryLogic {
316    #[new]
317    pub fn new(
318        py: Python<'_>,
319        operator: PyUnaryOperator,
320        operand: PyMemoryReference,
321    ) -> PyResult<Self> {
322        Ok(Self(UnaryLogic::new(
323            UnaryOperator::py_try_from(py, &operator)?,
324            MemoryReference::py_try_from(py, &operand)?,
325        )))
326    }
327}