qcs 0.26.2-rc.0

High level interface for running Quil on a QPU
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
//! Python bindings for the QVM module.

#![expect(
    clippy::needless_pass_by_value,
    reason = "PyO3 types often require it."
)]

use rigetti_pyo3::{create_init_submodule, impl_repr, py_function_sync_async};
use std::collections::HashMap;
use std::time::Duration;

use pyo3::{prelude::*, types::PyList, Py};

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

use crate::{
    python::{errors, NonZeroU16},
    qvm::{
        self,
        http::{
            AddressRequest, ExpectationRequest, HttpClient, MultishotMeasureRequest,
            MultishotRequest, MultishotResponse, WavefunctionRequest,
        },
        Error, QvmOptions, QvmResultData,
    },
    register_data::RegisterData,
    RegisterMap, RegisterMatrixConversionError,
};

create_init_submodule! {
    classes: [
        PyQvmClient,
        QvmOptions,
        QvmResultData,
        RawQvmReadoutData
    ],
    errors: [ errors::QVMError ],
    funcs: [ py_run, py_run_async ],
    submodules: [ "api": api::init_submodule ],
}

impl_repr!(QvmOptions);
impl_repr!(QvmResultData);
impl_repr!(RawQvmReadoutData);

/// Client used to communicate with QVM.
#[derive(Clone, Debug)]
pub enum QvmClient {
    /// A client which uses HTTP to communicate with a QVM service.
    Http(HttpClient),

    /// A client which uses libquil to communicate with a QVM service.
    #[cfg(feature = "libquil")]
    Libquil(qvm::libquil::Client),
}

/// Client used to communicate with QVM.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "stubs", gen_stub_pyclass)]
#[pyclass(name = "QVMClient", module = "qcs_sdk.qvm")]
pub struct PyQvmClient {
    inner: QvmClient,
}

/// Encapsulates raw data returned from the QVM after executing a program.
#[derive(Debug)]
#[cfg_attr(feature = "stubs", gen_stub_pyclass)]
#[pyo3::pyclass(name = "RawQVMReadoutData", module = "qcs_sdk.qvm", frozen, get_all)]
pub struct RawQvmReadoutData {
    /// The mapping of register names (ie. "ro") to a 2-d list containing the
    /// values for that register.
    memory: HashMap<String, Py<PyList>>,
}

impl PyQvmClient {
    /// Access the underlying QVM client.
    #[must_use]
    pub fn as_client(&self) -> &(dyn qvm::Client + Send + Sync) {
        match &self.inner {
            QvmClient::Http(client) => client,
            #[cfg(feature = "libquil")]
            QvmClient::Libquil(client) => client,
        }
    }
}

#[cfg_attr(feature = "stubs", gen_stub_pymethods)]
#[pymethods]
impl PyQvmClient {
    #[new]
    fn new() -> PyResult<Self> {
        Err(errors::QuilcError::new_err(
            #[cfg(not(feature = "libquil"))]
            "QVMClient cannot be instantiated directly. Use QVMClient.new_http() instead.",
            #[cfg(feature = "libquil")]
            "QVMClient cannot be instantiated directly. Use QVMClient.new_http() and QVMClient.new_libquil().",
        ))
    }

    /// Construct a new QVM client which uses HTTP to communicate with a QVM service.
    #[staticmethod]
    fn new_http(endpoint: String) -> Self {
        let http_client = HttpClient::new(endpoint);
        Self {
            inner: QvmClient::Http(http_client),
        }
    }

    /// Return the address of the client.
    #[getter]
    fn qvm_url(&self) -> String {
        match &self.inner {
            QvmClient::Http(client) => client.qvm_url.clone(),
            #[cfg(feature = "libquil")]
            QvmClient::Libquil(_) => "".into(),
        }
    }
}

// These are pulled out separately so that the feature flag won't confuse the stub generator.
#[cfg(feature = "libquil")]
#[cfg_attr(feature = "stubs", gen_stub_pymethods)]
#[pymethods]
impl PyQvmClient {
    /// Construct a new QVM client which uses libquil.
    #[staticmethod]
    fn new_libquil() -> Self {
        Self {
            inner: QvmClient::Libquil(qvm::libquil::Client {}),
        }
    }
}

#[cfg(not(feature = "libquil"))]
#[cfg_attr(feature = "stubs", gen_stub_pymethods)]
#[pymethods]
impl PyQvmClient {
    #[staticmethod]
    #[pyo3(warn(
        message = "The installed version of qcs_sdk was built without the libquil feature. Use QVMClient.new_http() instead.",
    ))]
    fn new_libquil() -> PyResult<Self> {
        Err(errors::QVMError::new_err(
            "Cannot create a libquil QVMClient. Use QVMClient.new_http() instead.",
        ))
    }
}

#[async_trait::async_trait]
impl qvm::Client for PyQvmClient {
    /// The QVM version string. Not guaranteed to comply to the semver spec.
    async fn get_version_info(&self, options: &QvmOptions) -> Result<String, Error> {
        self.as_client().get_version_info(options).await
    }

    /// Execute a program on the QVM.
    async fn run(
        &self,
        request: &MultishotRequest,
        options: &QvmOptions,
    ) -> Result<MultishotResponse, Error> {
        self.as_client().run(request, options).await
    }

    /// Execute a program on the QVM.
    ///
    /// The behavior of this method is different to that of [`Self::run`]
    /// in that [`Self::run_and_measure`] will execute the program a single
    /// time; the resulting wavefunction is then sampled some number of times
    /// (specified in [`MultishotMeasureRequest`]).
    ///
    /// This can be useful if the program is expensive to execute and does
    /// not change per "shot".
    async fn run_and_measure(
        &self,
        request: &MultishotMeasureRequest,
        options: &QvmOptions,
    ) -> Result<Vec<Vec<i64>>, Error> {
        self.as_client().run_and_measure(request, options).await
    }

    /// Measure the expectation value of a program
    async fn measure_expectation(
        &self,
        request: &ExpectationRequest,
        options: &QvmOptions,
    ) -> Result<Vec<f64>, Error> {
        self.as_client().measure_expectation(request, options).await
    }

    /// Get the wavefunction produced by a program
    async fn get_wavefunction(
        &self,
        request: &WavefunctionRequest,
        options: &QvmOptions,
    ) -> Result<Vec<u8>, Error> {
        self.as_client().get_wavefunction(request, options).await
    }
}

#[cfg_attr(feature = "stubs", gen_stub_pymethods)]
#[pymethods]
impl QvmOptions {
    #[new]
    #[pyo3(signature = (timeout_seconds = None))]
    fn __new__(timeout_seconds: Option<f64>) -> Self {
        Self {
            timeout: timeout_seconds.map(Duration::from_secs_f64),
        }
    }

    /// The timeout used for requests to the QVM. If set to None, there is no timeout.
    #[getter]
    #[must_use]
    pub fn timeout(&self) -> Option<f32> {
        self.timeout.map(|duration| duration.as_secs_f32())
    }

    /// The timeout used for requests to the QVM. If set to None, there is no timeout.
    #[setter]
    pub fn set_timeout(&mut self, timeout_seconds: Option<f64>) {
        self.timeout = timeout_seconds.map(Duration::from_secs_f64);
    }

    /// Get the default set of ``QVMOptions`` used for QVM requests.
    ///
    /// Settings:
    ///     timeout: 30.0 seconds
    #[staticmethod]
    #[pyo3(name = "default")]
    fn py_default() -> Self {
        Self::default()
    }
}

#[cfg_attr(feature = "stubs", gen_stub_pymethods)]
#[pymethods]
impl QvmResultData {
    /// Build a ``QVMResultData`` from a mapping of register names to a ``RegisterData`` matrix.
    #[new]
    fn __new__(memory: HashMap<String, RegisterData>) -> Self {
        QvmResultData::from_memory_map(memory)
    }

    fn __getnewargs__(&self) -> (HashMap<String, RegisterData>,) {
        (self.memory().clone(),)
    }

    #[expect(clippy::missing_errors_doc)]
    /// Get a copy of this result data flattened into a ``RawQVMReadoutData``.
    pub fn to_raw_readout_data(&self, py: Python<'_>) -> PyResult<RawQvmReadoutData> {
        let memory = self
            .memory()
            .iter()
            .map(|(register, data)| {
                (match data {
                    RegisterData::I8(matrix) => PyList::new(py, matrix),
                    RegisterData::F64(matrix) => PyList::new(py, matrix),
                    RegisterData::I16(matrix) => PyList::new(py, matrix),
                    RegisterData::Complex32(matrix) => PyList::new(py, matrix),
                })
                .map(|list| (register.clone(), list.unbind()))
            })
            .collect::<PyResult<_>>()?;

        Ok(RawQvmReadoutData { memory })
    }

    /// Convert into a [`RegisterMap`].
    ///
    /// The [`RegisterMatrix`] for each register will be
    /// constructed such that each row contains all the final values in the register for a single shot.
    ///
    /// # Errors
    ///
    /// Returns a [`RegisterMatrixConversionError`] if the inner execution data for any of the
    /// registers would result in a jagged matrix.
    /// This is often the case in programs that use mid-circuit measurement or dynamic control flow,
    /// where measurements to the same memory reference might occur multiple times in a shot, or be
    /// skipped conditionally. In these cases, building a rectangular [`RegisterMatrix`] would
    /// necessitate making assumptions about the data that could skew the data in undesirable ways.
    /// Instead, it's recommended to manually build a matrix from [`QpuResultData`] that accurately
    /// selects the last value per-shot based on the program that was run.
    fn to_register_map(&self) -> Result<RegisterMap, RegisterMatrixConversionError> {
        RegisterMap::from_qvm_result_data(self)
    }
}

#[cfg_attr(feature = "stubs", pyo3_stub_gen::derive::gen_stub_pymethods)]
#[pymethods]
impl MultishotRequest {
    /// Creates a new `MultishotRequest` with the given parameters.
    #[new]
    #[pyo3(signature = (program, shots, addresses, measurement_noise=None, gate_noise=None, rng_seed=None))]
    fn __new__(
        program: String,
        shots: NonZeroU16,
        addresses: HashMap<String, AddressRequest>,
        measurement_noise: Option<(f64, f64, f64)>,
        gate_noise: Option<(f64, f64, f64)>,
        rng_seed: Option<i64>,
    ) -> Self {
        Self::new(
            program,
            shots.0,
            addresses,
            measurement_noise,
            gate_noise,
            rng_seed,
        )
    }

    #[getter]
    fn trials(&self) -> NonZeroU16 {
        NonZeroU16(self.trials)
    }

    #[setter]
    fn set_trials(&mut self, trials: NonZeroU16) {
        self.trials = trials.0;
    }
}

#[cfg_attr(not(feature = "stubs"), optipy::strip_pyo3(only_stubs))]
#[cfg_attr(feature = "stubs", pyo3_stub_gen::derive::gen_stub_pymethods)]
#[pymethods]
impl MultishotMeasureRequest {
    /// Construct a new `MultishotMeasureRequest` using the given parameters.
    #[new]
    #[pyo3(signature = (compiled_quil, trials, qubits, measurement_noise=None, gate_noise=None, rng_seed=None))]
    fn __new__(
        compiled_quil: String,
        trials: NonZeroU16,
        #[gen_stub(override_type(type_repr = "builtins.list[builtins.int]"))] qubits: Vec<u64>,
        measurement_noise: Option<(f64, f64, f64)>,
        gate_noise: Option<(f64, f64, f64)>,
        rng_seed: Option<i64>,
    ) -> Self {
        Self::new(
            compiled_quil,
            trials.0,
            &qubits,
            measurement_noise,
            gate_noise,
            rng_seed,
        )
    }

    #[getter]
    fn trials(&self) -> NonZeroU16 {
        NonZeroU16(self.trials)
    }

    #[setter]
    fn set_trials(&mut self, trials: NonZeroU16) {
        self.trials = trials.0;
    }
}

#[cfg_attr(feature = "stubs", pyo3_stub_gen::derive::gen_stub_pymethods)]
#[pymethods]
impl ExpectationRequest {
    /// Creates a new `ExpectationRequest` using the given parameters.
    #[new]
    #[pyo3(signature = (state_preparation, operators, rng_seed=None))]
    fn __new__(state_preparation: String, operators: Vec<String>, rng_seed: Option<i64>) -> Self {
        ExpectationRequest::new(state_preparation, &operators, rng_seed)
    }
}

#[cfg_attr(feature = "stubs", pyo3_stub_gen::derive::gen_stub_pymethods)]
#[pymethods]
impl WavefunctionRequest {
    /// Create a new `WavefunctionRequest` with the given parameters.
    #[new]
    #[pyo3(signature = (compiled_quil, measurement_noise=None, gate_noise=None, rng_seed=None))]
    fn __new__(
        compiled_quil: String,
        measurement_noise: Option<(f64, f64, f64)>,
        gate_noise: Option<(f64, f64, f64)>,
        rng_seed: Option<i64>,
    ) -> Self {
        WavefunctionRequest::new(compiled_quil, measurement_noise, gate_noise, rng_seed)
    }
}

py_function_sync_async! {
    #[cfg_attr(feature = "stubs", gen_stub_pyfunction(module = "qcs_sdk.qvm"))]
    #[pyfunction(signature = (
        quil, shots, addresses, params, client,
        measurement_noise=None, gate_noise=None, rng_seed=None, options=None
    ))]
    #[tracing::instrument(skip_all)]
    #[pyo3_opentelemetry::pypropagate(on_context_extraction_failure="ignore")]
    /// Runs the given program on the QVM.
    ///
    /// :param quil: A quil program as a string.
    /// :param shots: The number of times to run the program. Should be a value greater than zero.
    /// :param addresses: A mapping of memory region names to an ``AddressRequest`` describing what data to get back for that memory region from the QVM at the end of execution.
    /// :param params: A mapping of memory region names to their desired values.
    /// :param client: An optional ``QCSClient`` to use. If unset, creates one using the environemnt configuration (see https://docs.rigetti.com/qcs/references/qcs-client-configuration).
    /// :param options: An optional ``QVMOptions`` to use. If unset, uses ``QVMOptions.default()`` for the request.
    ///
    /// :returns: A ``QVMResultData`` containing the final state of of memory for the requested readouts after the program finished running.
    ///
    /// :raises QVMError: If one of the parameters is invalid, or if there was a problem communicating with the QVM server.
    async fn run(
        quil: String,
        shots: NonZeroU16,
        addresses: HashMap<String, AddressRequest>,
        params: HashMap<String, Vec<f64>>,
        client: PyQvmClient,
        measurement_noise: Option<(f64, f64, f64)>,
        gate_noise: Option<(f64, f64, f64)>,
        rng_seed: Option<i64>,
        options: Option<QvmOptions>,
    ) -> PyResult<QvmResultData> {
        let params = params
            .into_iter()
            .map(|(key, value)| (key.into_boxed_str(), value))
            .collect();

        let options = options.unwrap_or_default();

        qvm::run(
            &quil,
            shots.0,
            addresses,
            &params,
            measurement_noise,
            gate_noise,
            rng_seed,
            &client,
            &options,
        )
        .await
        .map_err(Into::into)
    }
}

mod api {
    use pyo3::prelude::*;
    use rigetti_pyo3::{create_init_submodule, py_function_sync_async};

    #[cfg(feature = "stubs")]
    use pyo3_stub_gen::derive::gen_stub_pyfunction;

    use crate::qvm::{
        http::{
            AddressRequest, ExpectationRequest, MultishotMeasureRequest, MultishotRequest,
            MultishotResponse, WavefunctionRequest,
        },
        python::PyQvmClient,
        Client, QvmOptions,
    };

    create_init_submodule! {
        classes: [
            ExpectationRequest,
            MultishotMeasureRequest,
            MultishotRequest,
            MultishotResponse,
            WavefunctionRequest
        ],
        complex_enums: [ AddressRequest ],
        funcs: [
            py_get_version_info,
            py_get_version_info_async,
            py_run,
            py_run_async,
            py_run_and_measure,
            py_run_and_measure_async,
            py_measure_expectation,
            py_measure_expectation_async,
            py_get_wavefunction,
            py_get_wavefunction_async
        ],
    }

    py_function_sync_async! {
        #[cfg_attr(feature = "stubs", gen_stub_pyfunction(module = "qcs_sdk.qvm.api"))]
        #[pyfunction]
        #[pyo3(signature = (client, options = None))]
        async fn get_version_info(client: PyQvmClient, options: Option<QvmOptions>) -> PyResult<String> {
            client
                .get_version_info(&options.unwrap_or_default())
                .await
                .map_err(Into::into)
        }
    }

    py_function_sync_async! {
        #[cfg_attr(feature = "stubs", gen_stub_pyfunction(module = "qcs_sdk.qvm.api"))]
        #[pyfunction]
        #[pyo3(signature = (request, client, options = None))]
        #[tracing::instrument(skip_all)]
        #[pyo3_opentelemetry::pypropagate(on_context_extraction_failure="ignore")]
        async fn get_wavefunction(
            request: WavefunctionRequest,
            client: PyQvmClient,
            options: Option<QvmOptions>
        ) -> PyResult<Vec<u8>> {
            client
                .get_wavefunction(&request, &options.unwrap_or_default())
                .await
                .map_err(Into::into)
        }
    }

    py_function_sync_async! {
        #[cfg_attr(feature = "stubs", gen_stub_pyfunction(module = "qcs_sdk.qvm.api"))]
        #[pyfunction]
        #[pyo3(signature = (request, client, options = None))]
        #[tracing::instrument(skip_all)]
        #[pyo3_opentelemetry::pypropagate(on_context_extraction_failure="ignore")]
        async fn measure_expectation(
            request: ExpectationRequest,
            client: PyQvmClient,
            options: Option<QvmOptions>) -> PyResult<Vec<f64>> {
            client
                .measure_expectation(&request, &options.unwrap_or_default())
                .await
                .map_err(Into::into)
        }
    }

    py_function_sync_async! {
        #[cfg_attr(feature = "stubs", gen_stub_pyfunction(module = "qcs_sdk.qvm.api"))]
        #[pyfunction]
        #[pyo3(signature = (request, client, options = None))]
        #[tracing::instrument(skip_all)]
        #[pyo3_opentelemetry::pypropagate(on_context_extraction_failure="ignore")]
        async fn run_and_measure(
            request: MultishotMeasureRequest,
            client: PyQvmClient,
            options: Option<QvmOptions>) -> PyResult<Vec<Vec<i64>>> {
            client
                .run_and_measure(&request, &options.unwrap_or_default())
                .await
                .map_err(Into::into)
        }
    }

    py_function_sync_async! {
        #[cfg_attr(feature = "stubs", gen_stub_pyfunction(module = "qcs_sdk.qvm.api"))]
        #[pyfunction]
        #[pyo3(signature = (request, client, options = None))]
        #[tracing::instrument(skip_all)]
        #[pyo3_opentelemetry::pypropagate(on_context_extraction_failure="ignore")]
        async fn run(
            request: MultishotRequest,
            client: PyQvmClient,
            options: Option<QvmOptions>,
        ) -> PyResult<MultishotResponse> {
            client
                .run(&request, &options.unwrap_or_default())
                .await
                .map_err(Into::into)
        }
    }
}