quil-rs 0.37.1

Rust tooling for Quil (Quantum Instruction Language)
Documentation
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use num_complex::Complex64;
use numpy::{IntoPyArray as _, PyArray1};
use pyo3::{
    exceptions::{PyIndexError, PyStopIteration, PyValueError},
    prelude::*,
    types::{PySlice, PySliceIndices},
};

#[cfg(feature = "stubs")]
use pyo3_stub_gen::{
    derive::{
        gen_methods_from_python, gen_stub_pyclass, gen_stub_pyclass_complex_enum,
        gen_stub_pymethods,
    },
    PyStubType, TypeInfo,
};

use crate::waveform::sampling::*;

// The `init_submodule` occurs directly in `waveform/quilpy.rs`, as otherwise our linter is unhappy.

/// Register the relevant classes as subclasses of `collections.abc.*`.  Must be called exactly one;
/// ideally would be called from `init_submodule`, but we can't hook into that, so we expose this as
/// a `pub(crate)` function (to get warnings if it's unused) and then call it from the root
/// `#[pymodule]` function.
pub(crate) fn register_abcs<'py>(py: Python<'py>) -> PyResult<()> {
    pyo3::types::PySequence::register::<PyIqSamples>(py)?;
    Ok(())
}

// A duplication of [`IqSamples<Complex64>`], but nongeneric so it can be exposed to Python.  It
// also uses a named argument for the `Samples` constructor because that produces a nicer Python
// interface and avoids conflicting `__getitem__` definitions.  We don't mark this type as
// `#[pyclass(…, sequence)]`, because [`sequence` types have special
// behavior](https://pyo3.rs/main/class/protocols#mapping--sequence-types); this could be changed in
// the future if we're willing to handle that.
/// The result of sampling a waveform, representing a sequence of IQ value samples.
#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "stubs", gen_stub_pyclass_complex_enum)]
#[pyclass(
    module = "quil._quil.waveform.sampling",
    name = "IqSamples",
    eq,
    frozen,
    from_py_object
)]
pub enum PyIqSamples {
    /// A flat waveform, consisting of a single IQ value repeated some number of times.
    ///
    /// This is a more optimizable special-case representation for [`IqSamples::Samples(vec![iq;
    /// sample_count])`][Self::Samples], but is otherwise equivalent.
    Flat { iq: Complex64, sample_count: usize },

    /// A literal sequence of IQ samples.
    Samples { samples: Vec<Complex64> },
}

#[derive(Clone, Debug, derive_more::From, derive_more::Into)]
#[cfg_attr(feature = "stubs", gen_stub_pyclass)]
#[pyclass(
    module = "quil._quil.waveform.sampling",
    name = "IqSamplesIter",
    from_py_object
)]
pub struct PyIqSamplesIter(pub IntoIter<Complex64>);

#[derive(Clone, Debug, derive_more::From, derive_more::Into)]
#[cfg_attr(feature = "stubs", gen_stub_pyclass)]
#[pyclass(
    module = "quil._quil.waveform.sampling",
    name = "IqSamplesRevIter",
    from_py_object
)]
pub struct PyIqSamplesRevIter(pub std::iter::Rev<IntoIter<Complex64>>);

impl From<IqSamples<Complex64>> for PyIqSamples {
    fn from(value: IqSamples<Complex64>) -> Self {
        match value {
            IqSamples::Flat { iq, sample_count } => Self::Flat { iq, sample_count },
            IqSamples::Samples(samples) => Self::Samples { samples },
        }
    }
}

impl From<PyIqSamples> for IqSamples<Complex64> {
    fn from(value: PyIqSamples) -> Self {
        match value {
            PyIqSamples::Flat { iq, sample_count } => Self::Flat { iq, sample_count },
            PyIqSamples::Samples { samples } => Self::Samples(samples),
        }
    }
}

#[derive(Debug, FromPyObject)]
pub enum IqSamplesIndex<'py> {
    Int(isize),
    Slice(Bound<'py, PySlice>),
}

#[derive(Debug, IntoPyObject)]
pub enum IqSamplesIndexed {
    One(Complex64),
    Many(Vec<Complex64>),
}

#[cfg(feature = "stubs")]
impl PyStubType for IqSamplesIndex<'_> {
    fn type_output() -> TypeInfo {
        isize::type_output() | TypeInfo::builtin("slice[int | None]")
    }
}

#[cfg(feature = "stubs")]
impl PyStubType for IqSamplesIndexed {
    fn type_output() -> TypeInfo {
        Complex64::type_output() | TypeInfo::list_of::<Complex64>()
    }
}

#[cfg(feature = "stubs")]
pyo3::inventory::submit! {
    gen_methods_from_python! {r#"
        class PyIqSamples:
            @overload
            def get(self, index: int) -> typing.Optional[complex]: ...
            @overload
            def get(self, index: slice[typing.Optional[int]]) -> list[complex]: ...

            @overload
            def __getitem__(self, index: int) -> complex: ...
            @overload
            def __getitem__(self, index: slice[typing.Optional[int]]) -> list[complex]: ...
    "#}
}

#[cfg_attr(not(feature = "stubs"), optipy::strip_pyo3(only_stubs))]
#[cfg_attr(feature = "stubs", gen_stub_pymethods)]
#[pymethods]
impl PyIqSamples {
    /// The number of samples.  The same as `len`.
    // This is a reimplementation of [`IqSamples::sample_count`]
    #[getter(sample_count)]
    pub fn sample_count(&self) -> usize {
        match self {
            Self::Flat {
                sample_count,
                iq: _,
            } => *sample_count,

            Self::Samples { samples } => samples.len(),
        }
    }

    /// The number of samples.  The same as `sample_count`.
    #[pyo3(name = "__len__")]
    pub fn len(&self) -> usize {
        self.sample_count()
    }

    #[pyo3(name = "__length_hint__")]
    pub fn length_hint(&self) -> usize {
        self.sample_count()
    }

    #[pyo3(name = "__iter__")]
    pub fn iter(&self) -> PyIqSamplesIter {
        PyIqSamplesIter(IqSamples::from(self.clone()).into_iter())
    }

    #[pyo3(name = "__reversed__")]
    pub fn reversed(&self) -> PyIqSamplesRevIter {
        PyIqSamplesRevIter(self.iter().0.rev())
    }

    #[pyo3(name = "__contains__")]
    pub fn contains(&self, value: Bound<'_, PyAny>) -> bool {
        let Ok(value) = value.extract::<Complex64>() else {
            return false;
        };
        match self {
            Self::Flat { iq, sample_count } => *sample_count > 0 && *iq == value,
            Self::Samples { samples } => samples.contains(&value),
        }
    }

    /// Get the nth sample.  The same as indexing, but returns `None` instead of raising an error if
    /// the index is out of range.
    // This contains a reimplementation of [`IqSamples::get`], but handles Pythonic conventions as
    // well.
    #[pyo3(name = "get")]
    pub fn get(&self, index: IqSamplesIndex<'_>) -> PyResult<Option<IqSamplesIndexed>> {
        #[inline]
        fn get_one(this: &PyIqSamples, mut index: isize) -> Option<Complex64> {
            if index < 0 {
                index = index.checked_add_unsigned(this.sample_count())?;
            }
            let index = usize::try_from(index).ok()?;
            match this {
                PyIqSamples::Flat { iq, sample_count } => (index < *sample_count).then_some(*iq),
                PyIqSamples::Samples { samples } => samples.get(index).copied(),
            }
        }

        match index {
            IqSamplesIndex::Int(index) => Ok(get_one(self, index).map(IqSamplesIndexed::One)),

            IqSamplesIndex::Slice(indices) => {
                let sample_count = self.sample_count();
                let Ok(signed_sample_count) = isize::try_from(sample_count) else {
                    // Rust guarantees that allocated objects have size at most `isize::MAX`, so we
                    // must be in the `Flat` case and have more than `isize::MAX` samples.  However,
                    // `PyO3` only supports `isize` indices in `slice` (as of 0.29), and doesn't
                    // offer a way to get the raw values out, so we have to error here.
                    // Fortunately, this should happen approximately never.
                    //
                    // PyO3 docs: <https://pyo3.rs/main/doc/pyo3/types/struct.pyslice>

                    return Err(PyIndexError::new_err(format!(
                        "cannot index into an IqSamples.Flat object with >= isize::MAX ({}) \
                         samples using a slice",
                        isize::MAX
                    )));
                };

                let PySliceIndices {
                    start,
                    stop,
                    step,
                    slicelength,
                } = indices.indices(signed_sample_count)?;
                debug_assert!(step != 0);
                debug_assert!(slicelength <= sample_count);
                debug_assert!(start >= 0 || (start == -1 && step < 0));
                debug_assert!(stop >= 0 || (stop == -1 && step < 0));

                let sliced_samples = match self {
                    Self::Flat {
                        iq,
                        sample_count: _,
                    } => vec![*iq; slicelength],

                    Self::Samples { samples } => {
                        if let Ok(step) = usize::try_from(step) {
                            // Per the documentation of `PySliceIndices`
                            // (https://pyo3.rs/main/doc/pyo3/types/struct.pysliceindices), `start`
                            // and `stop` are nonnegative as long as the `step` is nonnegative.
                            let start = start as usize; // Guaranteed by documentation
                            let stop = stop as usize; // Guaranteed by documentation

                            if step == 1 {
                                samples[start..stop].to_owned()
                            } else {
                                let mut result = Vec::with_capacity(slicelength);
                                let mut i = start;
                                while i < stop {
                                    result.push(samples[i]);
                                    i += step;
                                }
                                result
                            }
                        } else {
                            let mut result = Vec::with_capacity(slicelength);
                            // Per the documentation of `PySliceIndices`
                            // (https://pyo3.rs/main/doc/pyo3/types/struct.pysliceindices), `start`
                            // and `stop` are no smaller than `-1`.  If `start` is `-1`, this loop
                            // will never iterate, as `stop` is also at least `-1`.  Moreover, `i`
                            // will never reach `-1` at all, for the same reason.  Thus, `i` will
                            // always be an in-range `usize`.
                            let mut i = start;
                            while i > stop {
                                result.push(samples[i as usize]);
                                i += step; // Counts down, since `step < 0`
                            }
                            result
                        }
                    }
                };

                Ok(Some(IqSamplesIndexed::Many(sliced_samples)))
            }
        }
    }

    /// Get the nth sample.
    #[pyo3(name = "__getitem__")]
    pub fn getitem(&self, index: IqSamplesIndex<'_>) -> PyResult<IqSamplesIndexed> {
        self.get(index)?
            .ok_or_else(|| PyIndexError::new_err("sample index out of range"))
    }

    /// Return the number of occurrences of the specified sample.
    ///
    /// Part of the `collections.abc.Sequence` interface.
    pub fn count(&self, value: Bound<'_, PyAny>) -> usize {
        let Ok(value) = value.extract::<Complex64>() else {
            return 0;
        };
        match self {
            Self::Flat { iq, sample_count } => {
                if *iq == value {
                    *sample_count
                } else {
                    0
                }
            }
            Self::Samples { samples } => samples.iter().filter(|sample| **sample == value).count(),
        }
    }

    /// Return the first occurrences of the specified sample.  Raises `ValueError` if the value is
    /// not present.
    ///
    /// Part of the `collections.abc.Sequence` interface.
    #[pyo3(signature = (value, start = 0, stop = None))]
    pub fn index(
        &self,
        value: Bound<'_, PyAny>,
        start: isize,
        stop: Option<isize>,
    ) -> PyResult<usize> {
        let error = || PyValueError::new_err(format!("{value:?} is not in samples"));

        let Ok(value) = value.extract::<Complex64>() else {
            return Err(error());
        };

        let sample_count = self.sample_count();

        // Indices that are "before the beginning" (more negative than `-sample_count`) or "after
        // the end" (greater than `sample_count`) are allowed, they're just equivalent to the
        // beginning/end, respectively.
        let (start, stop) = if let Ok(signed_sample_count) = isize::try_from(sample_count) {
            // It's panic-safe to negate `signed_sample_count` here, as it must be nonnegative.
            let signed_index = |i: isize| i.clamp(-signed_sample_count, signed_sample_count);
            (signed_index(start), stop.map(signed_index))
        } else {
            // We're in the `Flat` case and `sample_count > isize::MAX`, so every `isize` index is
            // valid.
            (start, stop)
        };

        // We have guaranteed that `-sample_count <= start, stop <= sample_count`, so this
        // arithmetic will always succeed at fixing our negative indices.
        let unsigned_index =
            |i: isize| usize::try_from(i).unwrap_or(sample_count.strict_add_signed(i));
        let start = unsigned_index(start);
        let stop = stop.map(unsigned_index).unwrap_or(sample_count);

        if start >= stop {
            // The range of indices is empty
            return Err(error());
        }

        // Since `start < stop`, and since we clamped `stop` to be at most `sample_count`, `start`
        // is a legal index and `start..stop` is a legal range of indices.

        match self {
            Self::Flat {
                iq,
                sample_count: _,
            } => {
                if *iq == value {
                    Ok(start)
                } else {
                    Err(error())
                }
            }
            Self::Samples { samples } => samples[start..stop]
                .iter()
                .position(|sample| *sample == value)
                .map(|i| i + start)
                .ok_or_else(error),
        }
    }

    /// Convert this sequence of samples into an explicit numpy array.
    ///
    /// The length of this array is [`self.sample_count()`][Self::sample_count], and its values are
    /// given by [`self[_]`][Self::get].
    pub fn iq_values<'py>(&self, py: Python<'py>) -> Bound<'py, PyArray1<Complex64>> {
        IqSamples::from(self.clone())
            .into_iq_values()
            .into_pyarray(py)
    }

    fn __repr__<'py>(&self, py: Python<'py>) -> PyResult<String> {
        // To get Python-like debug output, we have to ask Python to format our complex numbers
        let complex_repr = |c: &Complex64| c.into_pyobject(py)?.repr();

        match self {
            Self::Flat { iq, sample_count } => Ok(format!(
                "IqSamples.Flat(iq={iq}, sample_count={sample_count})",
                iq = complex_repr(iq)?.to_str()?,
            )),
            Self::Samples { samples } => {
                let mut output = "IqSamples.Samples([".to_owned();
                let mut first = true;
                for sample in samples {
                    if first {
                        first = false;
                    } else {
                        output.push_str(", ");
                    }
                    output.push_str(complex_repr(sample)?.to_str()?);
                }
                output.push_str("])");
                Ok(output)
            }
        }
    }
}

#[cfg_attr(feature = "stubs", gen_stub_pymethods)]
#[pymethods]
impl PyIqSamplesIter {
    #[pyo3(name = "__next__")]
    pub fn next(&mut self) -> PyResult<Complex64> {
        self.0.next().ok_or_else(|| PyStopIteration::new_err(()))
    }

    #[pyo3(name = "__iter__")]
    pub fn iter(slf: PyRef<'_, Self>) -> PyRef<'_, Self> {
        slf
    }

    fn __repr__(slf: PyRef<'_, Self>) -> String {
        format!("<IqSamplesIter object at {:?}>", slf.as_ptr())
    }
}

#[cfg_attr(feature = "stubs", gen_stub_pymethods)]
#[pymethods]
impl PyIqSamplesRevIter {
    #[pyo3(name = "__next__")]
    pub fn next(&mut self) -> PyResult<Complex64> {
        self.0.next().ok_or_else(|| PyStopIteration::new_err(()))
    }

    #[pyo3(name = "__iter__")]
    pub fn iter(slf: PyRef<'_, Self>) -> PyRef<'_, Self> {
        slf
    }

    fn __repr__(slf: PyRef<'_, Self>) -> String {
        format!("<IqSamplesRevIter object at {:?}>", slf.as_ptr())
    }
}