From 58f5beb1bb119ea52028579c831e2d80b9dcd450 Mon Sep 17 00:00:00 2001 From: Kalan Date: Tue, 6 Jun 2023 14:30:59 -0700 Subject: [PATCH] feat: TranslationOptions owned by these crates rather than imported --- crates/lib/src/qpu/translation.rs | 15 ++--- crates/python/src/executable.rs | 17 ++--- crates/python/src/grpc/mod.rs | 8 --- crates/python/src/grpc/models/mod.rs | 9 --- crates/python/src/grpc/models/translation.rs | 66 -------------------- crates/python/src/lib.rs | 1 - crates/python/src/qpu/translation.rs | 36 ++++++++++- 7 files changed, 47 insertions(+), 105 deletions(-) delete mode 100644 crates/python/src/grpc/models/translation.rs diff --git a/crates/lib/src/qpu/translation.rs b/crates/lib/src/qpu/translation.rs index 7183c5455..ad22ae7d8 100644 --- a/crates/lib/src/qpu/translation.rs +++ b/crates/lib/src/qpu/translation.rs @@ -39,10 +39,10 @@ pub async fn translate( quil_program: &str, num_shots: u32, client: &Qcs, - translation_options: TO, + translation_options: Option, ) -> Result where - TO: Into>, + TO: Into, { #[cfg(feature = "tracing")] tracing::debug!( @@ -51,7 +51,7 @@ where quantum_processor_id, ); - let options = translation_options.into(); + let options = translation_options.map(Into::into); let request = TranslateQuilToEncryptedControllerJobRequest { quantum_processor_id: quantum_processor_id.to_owned(), @@ -112,10 +112,11 @@ pub async fn get_quilt_calibrations( } /// Options available for Quil program translation. -/// +/// /// This wraps [`RdmTranslationOptions`] in order to improve the user experience, /// because the structs auto-generated by `prost` can be clumsy to use directly. -#[derive(Debug, Default)] +#[allow(clippy::module_name_repetitions)] +#[derive(Clone, Debug, Default)] pub struct TranslationOptions { inner: RdmTranslationOptions, } @@ -124,13 +125,13 @@ impl TranslationOptions { /// Use the first-generation translation backend available on QCS since 2018. pub fn use_backend_v1(&mut self) { self.inner.translation_backend = - Some(TranslationBackend::V1(translation::BackendV1Options {})) + Some(TranslationBackend::V1(translation::BackendV1Options {})); } /// Use the second-generation translation backend available on QCS since 2023 pub fn use_backend_v2(&mut self) { self.inner.translation_backend = - Some(TranslationBackend::V2(translation::BackendV2Options {})) + Some(TranslationBackend::V2(translation::BackendV2Options {})); } } diff --git a/crates/python/src/executable.rs b/crates/python/src/executable.rs index 5610ff11f..20696e034 100644 --- a/crates/python/src/executable.rs +++ b/crates/python/src/executable.rs @@ -2,19 +2,18 @@ use std::{num::NonZeroU16, sync::Arc}; use pyo3::{pyclass, FromPyObject}; use qcs::{Error, Executable, ExecutionData, JobHandle, Service}; -use qcs_api_client_grpc::services::translation::TranslationOptions; use rigetti_pyo3::{ impl_as_mut_for_wrapper, py_wrap_error, py_wrap_simple_enum, py_wrap_type, pyo3::{exceptions::PyRuntimeError, pymethods, types::PyDict, Py, PyAny, PyResult, Python}, - wrap_error, PyTryFrom, PyWrapper, ToPython, ToPythonError, + wrap_error, PyWrapper, ToPython, ToPythonError, }; use tokio::sync::Mutex; use crate::{ compiler::quilc::PyCompilerOpts, execution_data::PyExecutionData, - grpc::models::translation::PyTranslationOptions, py_sync::{py_async, py_sync}, + qpu::translation::PyTranslationOptions, }; wrap_error!(RustExecutionError(Error)); @@ -151,8 +150,7 @@ impl PyExecutable { endpoint_id: Option, translation_options: Option, ) -> PyResult { - let translation_options = - Option::::py_try_from(py, &translation_options)?; + let translation_options = translation_options.map(|opts| opts.as_inner().clone().into()); match endpoint_id { Some(endpoint_id) => py_sync!( py, @@ -184,8 +182,7 @@ impl PyExecutable { endpoint_id: Option, translation_options: Option, ) -> PyResult<&PyAny> { - let translation_options = - Option::::py_try_from(py, &translation_options)?; + let translation_options = translation_options.map(|opts| opts.as_inner().clone().into()); match endpoint_id { Some(endpoint_id) => py_async!( py, @@ -217,8 +214,7 @@ impl PyExecutable { endpoint_id: Option, translation_options: Option, ) -> PyResult { - let translation_options = - Option::::py_try_from(py, &translation_options)?; + let translation_options = translation_options.map(|opts| opts.as_inner().clone().into()); match endpoint_id { Some(endpoint_id) => py_sync!( py, @@ -250,8 +246,7 @@ impl PyExecutable { endpoint_id: Option, translation_options: Option, ) -> PyResult<&PyAny> { - let translation_options = - Option::::py_try_from(py, &translation_options)?; + let translation_options = translation_options.map(|opts| opts.as_inner().clone().into()); match endpoint_id { Some(endpoint_id) => { py_async!( diff --git a/crates/python/src/grpc/mod.rs b/crates/python/src/grpc/mod.rs index 28364fb7d..c446ac883 100644 --- a/crates/python/src/grpc/mod.rs +++ b/crates/python/src/grpc/mod.rs @@ -1,9 +1 @@ -use rigetti_pyo3::create_init_submodule; - pub mod models; - -create_init_submodule! { - submodules: [ - "models": models::init_submodule - ], -} \ No newline at end of file diff --git a/crates/python/src/grpc/models/mod.rs b/crates/python/src/grpc/models/mod.rs index 7d4136c54..cb9e0ac5c 100644 --- a/crates/python/src/grpc/models/mod.rs +++ b/crates/python/src/grpc/models/mod.rs @@ -1,10 +1 @@ -use rigetti_pyo3::create_init_submodule; - pub mod controller; -pub mod translation; - -create_init_submodule! { - submodules: [ - "translation": translation::init_submodule - ], -} diff --git a/crates/python/src/grpc/models/translation.rs b/crates/python/src/grpc/models/translation.rs deleted file mode 100644 index 8e198a671..000000000 --- a/crates/python/src/grpc/models/translation.rs +++ /dev/null @@ -1,66 +0,0 @@ -use qcs_api_client_grpc::services::translation::{ - translation_options::TranslationBackend, BackendV1Options, BackendV2Options, TranslationOptions, -}; -use rigetti_pyo3::{ - create_init_submodule, py_wrap_data_struct, py_wrap_type, py_wrap_union_enum, - pyo3::{pymethods, PyResult}, -}; - -py_wrap_type! { - #[derive(Default)] - PyBackendV1Options(BackendV1Options) as "BackendV1Options"; -} - -#[pymethods] -impl PyBackendV1Options { - #[new] - fn __new__() -> PyResult { - Ok(Self::default()) - } -} - -py_wrap_type! { - #[derive(Default)] - PyBackendV2Options(BackendV2Options) as "BackendV2Options"; -} - -#[pymethods] -impl PyBackendV2Options { - #[new] - fn __new__() -> PyResult { - Ok(Self::default()) - } -} - -py_wrap_union_enum! { - PyTranslationBackend(TranslationBackend) as "TranslationBackend" { - v1: V1 => PyBackendV1Options, - v2: V2 => PyBackendV2Options - } -} - -py_wrap_data_struct! { - #[derive(Default)] - PyTranslationOptions(TranslationOptions) as "TranslationOptions" { - translation_backend: Option => Option - } -} - -#[pymethods] -impl PyTranslationOptions { - #[new] - fn __new__(translation_backend: Option) -> PyResult { - Ok(Self(TranslationOptions { - translation_backend: translation_backend.map(|b| b.0), - })) - } -} - -create_init_submodule! { - classes: [ - PyTranslationBackend, - PyTranslationOptions, - PyBackendV1Options, - PyBackendV2Options - ], -} diff --git a/crates/python/src/lib.rs b/crates/python/src/lib.rs index a338583fb..e1f970879 100644 --- a/crates/python/src/lib.rs +++ b/crates/python/src/lib.rs @@ -36,7 +36,6 @@ create_init_submodule! { submodules: [ "client": client::init_submodule, "compiler": compiler::init_submodule, - "grpc": grpc::init_submodule, "qpu": qpu::init_submodule, "qvm": qvm::init_submodule ], diff --git a/crates/python/src/qpu/translation.rs b/crates/python/src/qpu/translation.rs index 3a6425034..cba6a4ab7 100644 --- a/crates/python/src/qpu/translation.rs +++ b/crates/python/src/qpu/translation.rs @@ -1,20 +1,24 @@ //! Translating programs. use std::{collections::HashMap, time::Duration}; -use pyo3::{exceptions::PyRuntimeError, pyclass, pyfunction, types::PyString, Py, PyResult}; +use pyo3::{ + exceptions::PyRuntimeError, pyclass, pyfunction, pymethods, types::PyString, Py, PyResult, +}; use qcs::client::GrpcClientError; +use qcs::qpu::translation::TranslationOptions; use qcs_api_client_openapi::models::GetQuiltCalibrationsResponse; use rigetti_pyo3::{ - create_init_submodule, py_wrap_data_struct, py_wrap_error, wrap_error, PyWrapper, ToPythonError, + create_init_submodule, py_wrap_data_struct, py_wrap_error, wrap_error, ToPythonError, }; -use crate::{grpc::models::translation::PyTranslationOptions, py_sync::py_function_sync_async}; +use crate::py_sync::py_function_sync_async; use crate::client::PyQcsClient; create_init_submodule! { classes: [ PyQuiltCalibrations, + PyTranslationOptions, PyTranslationResult ], errors: [ @@ -83,6 +87,32 @@ py_wrap_error!( PyRuntimeError ); +#[derive(Clone, Default)] +#[pyclass] +pub struct PyTranslationOptions(TranslationOptions); + +impl PyTranslationOptions { + pub fn as_inner(&self) -> &TranslationOptions { + &self.0 + } +} + +#[pymethods] +impl PyTranslationOptions { + #[new] + fn __new__() -> PyResult { + Ok(Self(Default::default())) + } + + fn use_backend_v1(&mut self) { + self.0.use_backend_v1() + } + + fn use_backend_v2(&mut self) { + self.0.use_backend_v2() + } +} + /// The result of a call to [`translate`] which provides information about the /// translated program. #[pyclass]