Skip to content

Commit

Permalink
feat: TranslationOptions owned by these crates rather than imported
Browse files Browse the repository at this point in the history
  • Loading branch information
kalzoo committed Jun 6, 2023
1 parent 6ce0078 commit 58f5beb
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 105 deletions.
15 changes: 8 additions & 7 deletions crates/lib/src/qpu/translation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ pub async fn translate<TO>(
quil_program: &str,
num_shots: u32,
client: &Qcs,
translation_options: TO,
translation_options: Option<TO>,
) -> Result<EncryptedTranslationResult, GrpcClientError>
where
TO: Into<Option<RdmTranslationOptions>>,
TO: Into<RdmTranslationOptions>,
{
#[cfg(feature = "tracing")]
tracing::debug!(
Expand All @@ -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(),
Expand Down Expand Up @@ -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,
}
Expand All @@ -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 {}));
}
}

Expand Down
17 changes: 6 additions & 11 deletions crates/python/src/executable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -151,8 +150,7 @@ impl PyExecutable {
endpoint_id: Option<String>,
translation_options: Option<PyTranslationOptions>,
) -> PyResult<PyExecutionData> {
let translation_options =
Option::<TranslationOptions>::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,
Expand Down Expand Up @@ -184,8 +182,7 @@ impl PyExecutable {
endpoint_id: Option<String>,
translation_options: Option<PyTranslationOptions>,
) -> PyResult<&PyAny> {
let translation_options =
Option::<TranslationOptions>::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,
Expand Down Expand Up @@ -217,8 +214,7 @@ impl PyExecutable {
endpoint_id: Option<String>,
translation_options: Option<PyTranslationOptions>,
) -> PyResult<PyJobHandle> {
let translation_options =
Option::<TranslationOptions>::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,
Expand Down Expand Up @@ -250,8 +246,7 @@ impl PyExecutable {
endpoint_id: Option<String>,
translation_options: Option<PyTranslationOptions>,
) -> PyResult<&PyAny> {
let translation_options =
Option::<TranslationOptions>::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!(
Expand Down
8 changes: 0 additions & 8 deletions crates/python/src/grpc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
use rigetti_pyo3::create_init_submodule;

pub mod models;

create_init_submodule! {
submodules: [
"models": models::init_submodule
],
}
9 changes: 0 additions & 9 deletions crates/python/src/grpc/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
use rigetti_pyo3::create_init_submodule;

pub mod controller;
pub mod translation;

create_init_submodule! {
submodules: [
"translation": translation::init_submodule
],
}
66 changes: 0 additions & 66 deletions crates/python/src/grpc/models/translation.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
],
Expand Down
36 changes: 33 additions & 3 deletions crates/python/src/qpu/translation.rs
Original file line number Diff line number Diff line change
@@ -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: [
Expand Down Expand Up @@ -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<Self> {
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]
Expand Down

0 comments on commit 58f5beb

Please sign in to comment.