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
use qcs_api_client_openapi::models::InstructionSetArchitecture;
use rigetti_pyo3::{create_init_submodule, impl_repr, py_function_sync_async};
use std::convert::TryFrom;

use pyo3::prelude::*;

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

use crate::{
    compiler::{
        quilc::{
            self, CompilationResult, CompilerOpts, ConjugateByCliffordRequest,
            ConjugatePauliByCliffordRequest, ConjugatePauliByCliffordResponse, Error,
            GenerateRandomizedBenchmarkingSequenceResponse, NativeQuilMetadata, PauliTerm,
            RandomizedBenchmarkingRequest, TargetDevice, DEFAULT_COMPILER_TIMEOUT,
        },
        rpcq,
    },
    python::errors,
    qpu::isa::python::InstructionSetArchitecture as PyInstructionSetArchitecture,
};

create_init_submodule! {
    submodules: [ "quilc": pyquilc::init_submodule ],
}

mod pyquilc {
    #[allow(clippy::wildcard_imports)]
    use super::*;

    create_init_submodule! {
        classes: [
            CompilerOpts,
            CompilationResult,
            NativeQuilMetadata,
            TargetDevice,
            PauliTerm,
            ConjugateByCliffordRequest,
            ConjugatePauliByCliffordRequest,
            ConjugatePauliByCliffordResponse,
            RandomizedBenchmarkingRequest,
            GenerateRandomizedBenchmarkingSequenceResponse,
            PyQuilcClient
        ],
        consts: [ DEFAULT_COMPILER_TIMEOUT ],
        errors: [ errors::QuilcError ],
        funcs: [
            py_compile_program,
            py_compile_program_async,
            py_get_version_info,
            py_get_version_info_async,
            py_conjugate_pauli_by_clifford,
            py_conjugate_pauli_by_clifford_async,
            py_generate_randomized_benchmarking_sequence,
            py_generate_randomized_benchmarking_sequence_async
        ],
    }
}

impl_repr!(NativeQuilMetadata);

#[cfg_attr(feature = "stubs", gen_stub_pymethods)]
#[pymethods]
impl CompilerOpts {
    /// Create a new instance of `CompilerOpts`.
    #[new]
    #[pyo3(signature = (timeout = Some(DEFAULT_COMPILER_TIMEOUT), protoquil = None))]
    fn __new__(timeout: Option<f64>, protoquil: Option<bool>) -> Self {
        Self::new().with_timeout(timeout).with_protoquil(protoquil)
    }

    /// Create a new instance of `CompilerOpts` with default values.
    #[staticmethod]
    #[pyo3(name = "default")]
    fn py_default() -> Self {
        Self::default()
    }
}

#[cfg_attr(feature = "stubs", gen_stub_pymethods)]
#[pymethods]
impl TargetDevice {
    /// Create a [`TargetDevice`] based on an [`InstructionSetArchitecture`].
    ///
    /// # Errors
    ///
    /// Returns a [`QuilcError`] if the [`InstructionSetArchitecture`]
    /// cannot be converted into a format that Quilc understands.
    #[expect(clippy::result_large_err)]
    #[staticmethod]
    pub(crate) fn from_isa(isa: PyInstructionSetArchitecture) -> Result<Self, Error> {
        TargetDevice::try_from(InstructionSetArchitecture::from(isa))
    }

    /// Create a [`TargetDevice`] from a JSON string.
    ///
    /// # Errors
    ///
    /// Returns a [`QuilcError`] if the JSON is malformed.
    #[staticmethod]
    pub(crate) fn from_json(value: &str) -> PyResult<Self> {
        serde_json::from_str(value).map_err(|err| errors::QuilcError::new_err(err.to_string()))
    }
}

#[derive(Clone, Debug)]
pub(crate) enum QuilcClient {
    Rpcq(rpcq::Client),
    #[cfg(feature = "libquil")]
    LibquilSys(super::libquil::Client),
}

/// Client used to communicate with Quilc.
#[derive(Clone)]
#[cfg_attr(feature = "stubs", gen_stub_pyclass)]
#[pyclass(module = "qcs_sdk.compiler.quilc", name = "QuilcClient")]
pub(crate) struct PyQuilcClient {
    pub inner: QuilcClient,
}

impl QuilcClient {
    pub(crate) fn as_client(&self) -> &dyn quilc::Client {
        match self {
            QuilcClient::Rpcq(client) => client,
            #[cfg(feature = "libquil")]
            QuilcClient::LibquilSys(client) => client,
        }
    }
}

impl quilc::Client for QuilcClient {
    fn compile_program(
        &self,
        quil: &str,
        isa: TargetDevice,
        options: CompilerOpts,
    ) -> Result<CompilationResult, Error> {
        self.as_client().compile_program(quil, isa, options)
    }

    fn get_version_info(&self) -> Result<String, Error> {
        self.as_client().get_version_info()
    }

    fn conjugate_pauli_by_clifford(
        &self,
        request: ConjugateByCliffordRequest,
    ) -> Result<ConjugatePauliByCliffordResponse, Error> {
        self.as_client().conjugate_pauli_by_clifford(request)
    }

    fn generate_randomized_benchmarking_sequence(
        &self,
        request: RandomizedBenchmarkingRequest,
    ) -> Result<GenerateRandomizedBenchmarkingSequenceResponse, Error> {
        self.as_client()
            .generate_randomized_benchmarking_sequence(request)
    }
}

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

    /// Construct a `QuilcClient` that uses RPCQ to communicate with Quilc.
    #[staticmethod]
    fn new_rpcq(endpoint: &str) -> PyResult<Self> {
        Ok(Self {
            inner: QuilcClient::Rpcq(rpcq::Client::new(endpoint)?),
        })
    }
}

// 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 PyQuilcClient {
    /// Construct a QuilcClient that uses libquil.
    #[staticmethod]
    fn new_libquil() -> Self {
        let libquil_client = crate::compiler::libquil::Client {};
        Self {
            inner: QuilcClient::LibquilSys(libquil_client),
        }
    }
}

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

py_function_sync_async! {
    /// Compile a quil program for a target device.
    ///
    /// :param quil: The Quil program to compile.
    /// :param target: Architectural description of device to compile for.
    /// :param client: Client used to send compilation requests to Quilc.
    /// :param options: Optional compiler options. If ``None``, default values are used.
    ///
    /// :raises QuilcError: If compilation fails.
    #[cfg_attr(feature = "stubs", gen_stub_pyfunction(module = "qcs_sdk.compiler.quilc"))]
    #[pyfunction]
    #[pyo3(signature = (quil, target, client, options = None))]
    #[tracing::instrument(skip_all)]
    #[pyo3_opentelemetry::pypropagate(on_context_extraction_failure="ignore")]
    async fn compile_program(
        quil: String,
        target: TargetDevice,
        client: PyQuilcClient,
        options: Option<CompilerOpts>,
    ) -> PyResult<CompilationResult> {
        let client = client.inner.as_client();
        let options = options.unwrap_or_default();
        client.compile_program(&quil, target, options)
            .map_err(Into::into)
    }
}

#[cfg_attr(feature = "stubs", gen_stub_pymethods)]
#[pymethods]
impl NativeQuilMetadata {
    /// Construct a new `NativeQuilMetadata` from arguments.
    #[expect(clippy::too_many_arguments)]
    #[must_use]
    #[new]
    pub fn __new__(
        final_rewiring: Option<Vec<u64>>,
        gate_depth: Option<u64>,
        gate_volume: Option<u64>,
        multiqubit_gate_depth: Option<u64>,
        program_duration: Option<f64>,
        program_fidelity: Option<f64>,
        topological_swaps: Option<u64>,
        qpu_runtime_estimation: Option<f64>,
    ) -> NativeQuilMetadata {
        Self {
            final_rewiring: final_rewiring.unwrap_or_default(),
            gate_depth,
            gate_volume,
            multiqubit_gate_depth,
            program_duration,
            program_fidelity,
            topological_swaps,
            qpu_runtime_estimation,
        }
    }

    #[expect(clippy::type_complexity)]
    fn __getnewargs__(
        &self,
    ) -> (
        Option<Vec<u64>>,
        Option<u64>,
        Option<u64>,
        Option<u64>,
        Option<f64>,
        Option<f64>,
        Option<u64>,
        Option<f64>,
    ) {
        (
            if self.final_rewiring.is_empty() {
                None
            } else {
                Some(self.final_rewiring.clone())
            },
            self.gate_depth,
            self.gate_volume,
            self.multiqubit_gate_depth,
            self.program_duration,
            self.program_fidelity,
            self.topological_swaps,
            self.qpu_runtime_estimation,
        )
    }
}

py_function_sync_async! {
    /// Fetch the version information from the running Quilc service.
    ///
    /// :param client: Client used to send compilation requests to Quilc.
    ///
    /// :raises QuilcError: If there is a failure connecting to Quilc.
    #[cfg_attr(feature = "stubs", gen_stub_pyfunction(module = "qcs_sdk.compiler.quilc"))]
    #[pyfunction]
    async fn get_version_info(client: PyQuilcClient) -> PyResult<String> {
        client.inner.as_client().get_version_info().map_err(Into::into)
    }
}

#[cfg_attr(feature = "stubs", gen_stub_pymethods)]
#[pymethods]
impl PauliTerm {
    #[new]
    fn __new__(indices: Vec<u64>, symbols: Vec<String>) -> Self {
        Self { indices, symbols }
    }
}

#[cfg_attr(feature = "stubs", gen_stub_pymethods)]
#[pymethods]
impl ConjugateByCliffordRequest {
    #[new]
    fn __new__(pauli: PauliTerm, clifford: String) -> Self {
        Self { pauli, clifford }
    }
}

py_function_sync_async! {
    /// Given a circuit that consists only of elements of the Clifford group, return its action on a PauliTerm.
    /// In particular, for Clifford C, and Pauli P, this returns the PauliTerm representing CPC^{\dagger}.
    ///
    /// :param request: Pauli Term conjugation request.
    /// :param client: Client used to send compilation requests to Quilc.
    ///
    /// :raises QuilcError: If there is a failure connecting to Quilc or if the request is malformed.
    #[cfg_attr(feature = "stubs", gen_stub_pyfunction(module = "qcs_sdk.compiler.quilc"))]
    #[pyfunction]
    async fn conjugate_pauli_by_clifford(
        request: ConjugateByCliffordRequest,
        client: PyQuilcClient,
    ) -> PyResult<ConjugatePauliByCliffordResponse> {
        client.inner.as_client()
            .conjugate_pauli_by_clifford(request)
            .map_err(Into::into)
    }
}

#[cfg_attr(feature = "stubs", gen_stub_pymethods)]
#[pymethods]
impl RandomizedBenchmarkingRequest {
    #[new]
    fn __new__(
        depth: u64,
        qubits: u64,
        gateset: Vec<String>,
        seed: Option<u64>,
        interleaver: Option<String>,
    ) -> Self {
        Self {
            depth,
            qubits,
            gateset,
            seed,
            interleaver,
        }
    }
}

py_function_sync_async! {
    /// Construct a randomized benchmarking experiment on the given qubits, decomposing into
    /// gateset. If interleaver is not provided, the returned sequence will have the form
    ///
    /// ```ignore
    ///     C_1 C_2 ... C_(depth-1) C_inv ,
    /// ```
    ///
    /// where each C is a Clifford element drawn from gateset, C_{< depth} are randomly selected,
    /// and C_inv is selected so that the entire sequence composes to the identity. If an
    /// interleaver G (which must be a Clifford, and which will be decomposed into the native
    /// gateset) is provided, then the sequence instead takes the form
    ///
    /// ```ignore
    ///     C_1 G C_2 G ... C_(depth-1) G C_inv .
    /// ```
    ///
    /// :param request: Randomized benchmarking request.
    /// :param client: Client used to send compilation requests to Quilc.
    ///
    /// :raises QuilcError: If there is a failure connecting to Quilc or if the request is malformed.
    #[cfg_attr(feature = "stubs", gen_stub_pyfunction(module = "qcs_sdk.compiler.quilc"))]
    #[pyfunction]
    async fn generate_randomized_benchmarking_sequence(
        request: RandomizedBenchmarkingRequest,
        client: PyQuilcClient,
    ) -> PyResult<GenerateRandomizedBenchmarkingSequenceResponse> {
        client.inner
            .as_client()
            .generate_randomized_benchmarking_sequence(request)
            .map_err(Into::into)
    }
}