Skip to main content

quil/instruction/
frame.rs

1use std::hash::Hash;
2
3use indexmap::IndexMap;
4
5use quil_rs::{
6    expression::Expression,
7    instruction::{
8        AttributeValue, Capture, FrameAttributes, FrameDefinition, FrameIdentifier,
9        MemoryReference, Pulse, Qubit, RawCapture, SetFrequency, SetPhase, SetScale,
10        ShiftFrequency, ShiftPhase, SwapPhases, WaveformInvocation,
11    },
12};
13use rigetti_pyo3::{
14    impl_hash, impl_repr, py_wrap_data_struct, py_wrap_union_enum,
15    pyo3::{
16        pymethods,
17        types::{PyBool, PyString},
18        Py, PyResult, Python,
19    },
20    PyTryFrom,
21};
22
23use super::PyQubit;
24use crate::{
25    expression::PyExpression,
26    impl_copy_for_instruction, impl_eq, impl_pickle_for_instruction, impl_to_quil,
27    instruction::{PyMemoryReference, PyWaveformInvocation},
28};
29
30py_wrap_union_enum! {
31    #[derive(Debug, PartialEq, Eq)]
32    #[pyo3(module = "quil.instructions")]
33    PyAttributeValue(AttributeValue) as "AttributeValue" {
34        string: String => Py<PyString>,
35        expression: Expression => PyExpression
36    }
37}
38impl_repr!(PyAttributeValue);
39impl_to_quil!(PyAttributeValue);
40impl_hash!(PyAttributeValue);
41impl_eq!(PyAttributeValue);
42
43pub type PyFrameAttributes = IndexMap<String, PyAttributeValue>;
44
45py_wrap_data_struct! {
46    #[derive(Debug, PartialEq, Eq)]
47    #[pyo3(subclass, module = "quil.instructions")]
48    PyFrameDefinition(FrameDefinition) as "FrameDefinition" {
49        identifier: FrameIdentifier => PyFrameIdentifier,
50        attributes: FrameAttributes => PyFrameAttributes
51    }
52}
53impl_repr!(PyFrameDefinition);
54impl_to_quil!(PyFrameDefinition);
55impl_copy_for_instruction!(PyFrameDefinition);
56impl_eq!(PyFrameDefinition);
57impl_pickle_for_instruction!(PyFrameDefinition);
58
59#[pymethods]
60impl PyFrameDefinition {
61    #[new]
62    pub fn new(
63        py: Python<'_>,
64        identifier: PyFrameIdentifier,
65        attributes: PyFrameAttributes,
66    ) -> PyResult<Self> {
67        Ok(Self(FrameDefinition::new(
68            FrameIdentifier::py_try_from(py, &identifier)?,
69            FrameAttributes::py_try_from(py, &attributes)?,
70        )))
71    }
72}
73
74py_wrap_data_struct! {
75    #[derive(Debug, PartialEq, Eq, Hash)]
76    #[pyo3(subclass, module = "quil.instructions")]
77    PyFrameIdentifier(FrameIdentifier) as "FrameIdentifier" {
78        name: String => Py<PyString>,
79        qubits: Vec<Qubit> => Vec<PyQubit>
80    }
81}
82impl_repr!(PyFrameIdentifier);
83impl_to_quil!(PyFrameIdentifier);
84impl_hash!(PyFrameIdentifier);
85impl_eq!(PyFrameIdentifier);
86
87#[pymethods]
88impl PyFrameIdentifier {
89    #[new]
90    pub fn new(py: Python<'_>, name: String, qubits: Vec<PyQubit>) -> PyResult<Self> {
91        Ok(Self(FrameIdentifier::new(
92            name,
93            Vec::<Qubit>::py_try_from(py, &qubits)?,
94        )))
95    }
96}
97
98py_wrap_data_struct! {
99    #[derive(Debug, PartialEq, Eq)]
100    #[pyo3(subclass, module = "quil.instructions")]
101    PyCapture(Capture) as "Capture" {
102        blocking: bool => Py<PyBool>,
103        frame: FrameIdentifier => PyFrameIdentifier,
104        memory_reference: MemoryReference => PyMemoryReference,
105        waveform: WaveformInvocation => PyWaveformInvocation
106    }
107}
108impl_repr!(PyCapture);
109impl_to_quil!(PyCapture);
110impl_copy_for_instruction!(PyCapture);
111impl_eq!(PyCapture);
112impl_pickle_for_instruction!(PyCapture);
113
114#[pymethods]
115impl PyCapture {
116    #[new]
117    pub fn new(
118        py: Python<'_>,
119        blocking: bool,
120        frame: PyFrameIdentifier,
121        memory_reference: PyMemoryReference,
122        waveform: PyWaveformInvocation,
123    ) -> PyResult<Self> {
124        Ok(Self(Capture::new(
125            blocking,
126            FrameIdentifier::py_try_from(py, &frame)?,
127            MemoryReference::py_try_from(py, &memory_reference)?,
128            WaveformInvocation::py_try_from(py, &waveform)?,
129        )))
130    }
131}
132
133py_wrap_data_struct! {
134    #[derive(Debug, PartialEq, Eq)]
135    #[pyo3(subclass, module = "quil.instructions")]
136    PyPulse(Pulse) as "Pulse" {
137        blocking: bool => Py<PyBool>,
138        frame: FrameIdentifier => PyFrameIdentifier,
139        waveform: WaveformInvocation => PyWaveformInvocation
140    }
141}
142impl_repr!(PyPulse);
143impl_to_quil!(PyPulse);
144impl_copy_for_instruction!(PyPulse);
145impl_eq!(PyPulse);
146impl_pickle_for_instruction!(PyPulse);
147
148#[pymethods]
149impl PyPulse {
150    #[new]
151    pub fn new(
152        py: Python<'_>,
153        blocking: bool,
154        frame: PyFrameIdentifier,
155        waveform: PyWaveformInvocation,
156    ) -> PyResult<Self> {
157        Ok(Self(Pulse::new(
158            blocking,
159            FrameIdentifier::py_try_from(py, &frame)?,
160            WaveformInvocation::py_try_from(py, &waveform)?,
161        )))
162    }
163}
164
165py_wrap_data_struct! {
166    #[derive(Debug, PartialEq, Eq)]
167    #[pyo3(subclass, module = "quil.instructions")]
168    PyRawCapture(RawCapture) as "RawCapture" {
169        blocking: bool => Py<PyBool>,
170        frame: FrameIdentifier => PyFrameIdentifier,
171        duration: Expression => PyExpression,
172        memory_reference: MemoryReference => PyMemoryReference
173    }
174}
175
176impl_repr!(PyRawCapture);
177impl_to_quil!(PyRawCapture);
178impl_copy_for_instruction!(PyRawCapture);
179impl_hash!(PyRawCapture);
180impl_eq!(PyRawCapture);
181impl_pickle_for_instruction!(PyRawCapture);
182
183#[pymethods]
184impl PyRawCapture {
185    #[new]
186    pub fn new(
187        py: Python<'_>,
188        blocking: bool,
189        frame: PyFrameIdentifier,
190        duration: PyExpression,
191        memory_reference: PyMemoryReference,
192    ) -> PyResult<Self> {
193        Ok(Self(RawCapture::new(
194            blocking,
195            FrameIdentifier::py_try_from(py, &frame)?,
196            Expression::py_try_from(py, &duration)?,
197            MemoryReference::py_try_from(py, &memory_reference)?,
198        )))
199    }
200}
201
202py_wrap_data_struct! {
203    #[derive(Debug, PartialEq, Eq)]
204    #[pyo3(subclass, module = "quil.instructions")]
205    PySetFrequency(SetFrequency) as "SetFrequency" {
206        frame: FrameIdentifier => PyFrameIdentifier,
207        frequency: Expression => PyExpression
208    }
209}
210impl_repr!(PySetFrequency);
211impl_to_quil!(PySetFrequency);
212impl_copy_for_instruction!(PySetFrequency);
213impl_hash!(PySetFrequency);
214impl_eq!(PySetFrequency);
215impl_pickle_for_instruction!(PySetFrequency);
216
217#[pymethods]
218impl PySetFrequency {
219    #[new]
220    pub fn new(
221        py: Python<'_>,
222        frame: PyFrameIdentifier,
223        frequency: PyExpression,
224    ) -> PyResult<Self> {
225        Ok(Self(SetFrequency::new(
226            FrameIdentifier::py_try_from(py, &frame)?,
227            Expression::py_try_from(py, &frequency)?,
228        )))
229    }
230}
231
232py_wrap_data_struct! {
233    #[derive(Debug, PartialEq, Eq)]
234    #[pyo3(subclass, module = "quil.instructions")]
235    PySetPhase(SetPhase) as "SetPhase" {
236        frame: FrameIdentifier => PyFrameIdentifier,
237        phase: Expression => PyExpression
238    }
239}
240impl_repr!(PySetPhase);
241impl_to_quil!(PySetPhase);
242impl_copy_for_instruction!(PySetPhase);
243impl_hash!(PySetPhase);
244impl_eq!(PySetPhase);
245impl_pickle_for_instruction!(PySetPhase);
246
247#[pymethods]
248impl PySetPhase {
249    #[new]
250    pub fn new(py: Python<'_>, frame: PyFrameIdentifier, phase: PyExpression) -> PyResult<Self> {
251        Ok(Self(SetPhase::new(
252            FrameIdentifier::py_try_from(py, &frame)?,
253            Expression::py_try_from(py, &phase)?,
254        )))
255    }
256}
257
258py_wrap_data_struct! {
259    #[derive(Debug, PartialEq, Eq)]
260    #[pyo3(subclass, module = "quil.instructions")]
261    PySetScale(SetScale) as "SetScale" {
262        frame: FrameIdentifier => PyFrameIdentifier,
263        scale: Expression => PyExpression
264    }
265}
266impl_repr!(PySetScale);
267impl_to_quil!(PySetScale);
268impl_copy_for_instruction!(PySetScale);
269impl_hash!(PySetScale);
270impl_eq!(PySetScale);
271impl_pickle_for_instruction!(PySetScale);
272
273#[pymethods]
274impl PySetScale {
275    #[new]
276    pub fn new(py: Python<'_>, frame: PyFrameIdentifier, scale: PyExpression) -> PyResult<Self> {
277        Ok(Self(SetScale::new(
278            FrameIdentifier::py_try_from(py, &frame)?,
279            Expression::py_try_from(py, &scale)?,
280        )))
281    }
282}
283
284py_wrap_data_struct! {
285    #[derive(Debug, PartialEq, Eq)]
286    #[pyo3(subclass, module = "quil.instructions")]
287    PyShiftFrequency(ShiftFrequency) as "ShiftFrequency" {
288        frame: FrameIdentifier => PyFrameIdentifier,
289        frequency: Expression => PyExpression
290    }
291}
292impl_repr!(PyShiftFrequency);
293impl_to_quil!(PyShiftFrequency);
294impl_copy_for_instruction!(PyShiftFrequency);
295impl_hash!(PyShiftFrequency);
296impl_eq!(PyShiftFrequency);
297impl_pickle_for_instruction!(PyShiftFrequency);
298
299#[pymethods]
300impl PyShiftFrequency {
301    #[new]
302    pub fn new(
303        py: Python<'_>,
304        frame: PyFrameIdentifier,
305        frequency: PyExpression,
306    ) -> PyResult<Self> {
307        Ok(Self(ShiftFrequency::new(
308            FrameIdentifier::py_try_from(py, &frame)?,
309            Expression::py_try_from(py, &frequency)?,
310        )))
311    }
312}
313
314py_wrap_data_struct! {
315    #[derive(Debug, PartialEq, Eq)]
316    #[pyo3(subclass, module = "quil.instructions")]
317    PyShiftPhase(ShiftPhase) as "ShiftPhase" {
318        frame: FrameIdentifier => PyFrameIdentifier,
319        phase: Expression => PyExpression
320    }
321}
322impl_repr!(PyShiftPhase);
323impl_to_quil!(PyShiftPhase);
324impl_copy_for_instruction!(PyShiftPhase);
325impl_hash!(PyShiftPhase);
326impl_eq!(PyShiftPhase);
327impl_pickle_for_instruction!(PyShiftPhase);
328
329#[pymethods]
330impl PyShiftPhase {
331    #[new]
332    pub fn new(py: Python<'_>, frame: PyFrameIdentifier, phase: PyExpression) -> PyResult<Self> {
333        Ok(Self(ShiftPhase::new(
334            FrameIdentifier::py_try_from(py, &frame)?,
335            Expression::py_try_from(py, &phase)?,
336        )))
337    }
338}
339
340py_wrap_data_struct! {
341    #[derive(Debug, PartialEq, Eq)]
342    #[pyo3(subclass, module = "quil.instructions")]
343    PySwapPhases(SwapPhases) as "SwapPhases" {
344        frame_1: FrameIdentifier => PyFrameIdentifier,
345        frame_2: FrameIdentifier => PyFrameIdentifier
346    }
347}
348impl_repr!(PySwapPhases);
349impl_to_quil!(PySwapPhases);
350impl_copy_for_instruction!(PySwapPhases);
351impl_hash!(PySwapPhases);
352impl_eq!(PySwapPhases);
353impl_pickle_for_instruction!(PySwapPhases);
354
355#[pymethods]
356impl PySwapPhases {
357    #[new]
358    pub fn new(
359        py: Python<'_>,
360        frame_1: PyFrameIdentifier,
361        frame_2: PyFrameIdentifier,
362    ) -> PyResult<Self> {
363        Ok(Self(SwapPhases::new(
364            FrameIdentifier::py_try_from(py, &frame_1)?,
365            FrameIdentifier::py_try_from(py, &frame_2)?,
366        )))
367    }
368}