Skip to main content

qcs_sdk/
lib.rs

1use std::sync::Mutex;
2
3use pyo3::prelude::*;
4use rigetti_pyo3::{create_init_submodule, py_sync};
5
6use executable::ExecutionError;
7use execution_data::RegisterMatrixConversionError;
8
9pub mod client;
10pub mod compiler;
11pub mod executable;
12pub mod execution_data;
13pub mod grpc;
14pub mod qpu;
15pub mod qvm;
16pub mod register_data;
17
18pub(crate) mod from_py;
19pub(crate) mod py_sync;
20
21create_init_submodule! {
22    classes: [
23        execution_data::PyExecutionData,
24        execution_data::PyResultData,
25        execution_data::PyRegisterMap,
26        execution_data::PyRegisterMatrix,
27        executable::PyExecutable,
28        executable::PyParameter,
29        executable::PyJobHandle,
30        executable::PyService,
31        register_data::PyRegisterData,
32        client::PyQcsClient
33    ],
34    errors: [
35        ExecutionError,
36        RegisterMatrixConversionError
37    ],
38    funcs: [ reset_logging, gather_diagnostics ],
39    submodules: [
40        "client": client::init_submodule,
41        "compiler": compiler::init_submodule,
42        "qpu": qpu::init_submodule,
43        "qvm": qvm::init_submodule
44    ],
45}
46
47static PY_RESET_LOGGING_HANDLE: once_cell::sync::Lazy<Mutex<Option<pyo3_log::ResetHandle>>> =
48    once_cell::sync::Lazy::new(|| Mutex::new(None));
49
50#[pymodule]
51fn qcs_sdk(py: Python<'_>, m: &PyModule) -> PyResult<()> {
52    match pyo3_log::try_init() {
53        Ok(reset_handle) => {
54            if let Ok(mut handle) = PY_RESET_LOGGING_HANDLE.lock() {
55                *handle = Some(reset_handle);
56            }
57        }
58        Err(e) => eprintln!("Failed to initialize the qcs_sdk logger: {e}"),
59    }
60    init_submodule("qcs_sdk", py, m)?;
61    pyo3_tracing_subscriber::add_submodule("qcs_sdk", "_tracing_subscriber", py, m)?;
62    m.add("__version__", env!("CARGO_PKG_VERSION"))
63}
64
65#[pyfunction]
66fn reset_logging() {
67    if let Ok(handle) = PY_RESET_LOGGING_HANDLE.lock() {
68        if let Some(handle) = handle.as_ref() {
69            handle.reset();
70        }
71    }
72}
73
74#[pyfunction]
75#[pyo3(name = "_gather_diagnostics")]
76fn gather_diagnostics(py: Python<'_>) -> PyResult<String> {
77    py_sync!(py, async { Ok(qcs::diagnostics::get_report().await) })
78}