Skip to content

Commit

Permalink
fix!: update qcs-api deps to use a >=9 version of jsonwebtoken (#483)
Browse files Browse the repository at this point in the history
* chore: update qcs-api deps to use a >=9 version of jsonwebtoken

* chore: refactor to update to latest qcs-api-client

* chore: bump zerovec to fix yank crate error

* chore: fix lints

* chore: use default instead on unwrapping in new

* chore: make PyQcsClient load never async

* chore: update pyi definiteions

* chore: downgrade dirs to remove need for copyleft

* chore: fix test
  • Loading branch information
jselig-rigetti authored Jul 11, 2024
1 parent 7856072 commit 7a06117
Show file tree
Hide file tree
Showing 24 changed files with 382 additions and 171 deletions.
265 changes: 239 additions & 26 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ resolver = "2"

[workspace.dependencies]
qcs-api = "0.2.1"
qcs-api-client-common = "0.7.14"
qcs-api-client-grpc = "0.7.16"
qcs-api-client-openapi = "0.8.15"
qcs-api-client-common = "0.8.4"
qcs-api-client-grpc = "0.8.4"
qcs-api-client-openapi = "0.9.4"
serde_json = "1.0.86"
thiserror = "1.0.57"
tokio = "1.36.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/examples/delayed_job_retrieval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ MEASURE 0 ro[0]
const QUANTUM_PROCESSOR_ID: &str = "Aspen-M-3";

async fn quilc_client() -> rpcq::Client {
let qcs = Qcs::load().await;
let qcs = Qcs::load();
let endpoint = qcs.get_config().quilc_url();
rpcq::Client::new(endpoint).unwrap()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/examples/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ MEASURE 1 ro[1]
"#;

async fn quilc_client() -> rpcq::Client {
let qcs = Qcs::load().await;
let qcs = Qcs::load();
let endpoint = qcs.get_config().quilc_url();
rpcq::Client::new(endpoint).unwrap()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/examples/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ MEASURE 1 ro[1]
"#;

async fn quilc_client() -> rpcq::Client {
let qcs = Qcs::load().await;
let qcs = Qcs::load();
let endpoint = qcs.get_config().quilc_url();
rpcq::Client::new(endpoint).unwrap()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/examples/parametric_compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ RX(-pi / 2) 0
MEASURE 0 ro[0]
"#;
async fn quilc_client() -> rpcq::Client {
let qcs = Qcs::load().await;
let qcs = Qcs::load();
let endpoint = qcs.get_config().quilc_url();
rpcq::Client::new(endpoint).unwrap()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/examples/quil_t.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ DELAY 0 "rf" 1e-6
MEASURE 0 ro
"#;
async fn quilc_client() -> rpcq::Client {
let qcs = Qcs::load().await;
let qcs = Qcs::load();
let endpoint = qcs.get_config().quilc_url();
rpcq::Client::new(endpoint).unwrap()
}
Expand Down
46 changes: 27 additions & 19 deletions crates/lib/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
use std::time::Duration;

use qcs_api_client_common::configuration::{ClientConfiguration, RefreshError};
use qcs_api_client_common::configuration::{ClientConfiguration, TokenError};
#[cfg(feature = "grpc-web")]
use qcs_api_client_grpc::channel::{wrap_channel_with_grpc_web, GrpcWebWrapperLayerService};
use qcs_api_client_grpc::tonic::{wrap_channel_with_grpc_web, GrpcWebWrapperLayerService};
use qcs_api_client_grpc::{
channel::{
services::translation::translation_client::TranslationClient,
tonic::{
get_channel, parse_uri, wrap_channel_with, wrap_channel_with_retry, RefreshService,
RetryService,
},
services::translation::translation_client::TranslationClient,
};
use qcs_api_client_openapi::apis::configuration::Configuration as OpenApiConfiguration;
use tonic::transport::Channel;
use tonic::Status;

pub use qcs_api_client_common::configuration::LoadError;
pub use qcs_api_client_grpc::channel::Error as GrpcError;
pub use qcs_api_client_grpc::tonic::Error as GrpcError;
pub use qcs_api_client_openapi::apis::Error as OpenApiError;

const DEFAULT_MAX_MESSAGE_ENCODING_SIZE: usize = 50 * 1024 * 1024;
Expand All @@ -45,24 +45,24 @@ pub type GrpcConnection =
pub(crate) static DEFAULT_HTTP_API_TIMEOUT: Duration = Duration::from_secs(10);

/// A client providing helper functionality for accessing QCS APIs
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone)]
pub struct Qcs {
config: ClientConfiguration,
}

impl Qcs {
/// Create a [`Qcs`] and initialize it with the user's default [`ClientConfiguration`]
pub async fn load() -> Self {
let config = if let Ok(config) = ClientConfiguration::load_default().await {
config
#[must_use]
pub fn load() -> Self {
if let Ok(config) = ClientConfiguration::load_default() {
Self::with_config(config)
} else {
#[cfg(feature = "tracing")]
tracing::info!(
"No QCS client configuration found. QPU data and QCS will be inaccessible and only generic QVMs will be available for execution"
);
ClientConfiguration::default()
};
Self::with_config(config)
Self::default()
}
}

/// Create a [`Qcs`] and initialize it with the given [`ClientConfiguration`]
Expand All @@ -77,10 +77,8 @@ impl Qcs {
///
/// A [`LoadError`] will be returned if QCS credentials are
/// not correctly configured or the given profile is not defined.
pub async fn with_profile(profile: String) -> Result<Qcs, LoadError> {
ClientConfiguration::load_profile(profile)
.await
.map(Self::with_config)
pub fn with_profile(profile: String) -> Result<Qcs, LoadError> {
ClientConfiguration::load_profile(profile).map(Self::with_config)
}

/// Return a reference to the underlying [`ClientConfiguration`] with all settings parsed and resolved from configuration sources.
Expand All @@ -95,14 +93,14 @@ impl Qcs {

pub(crate) fn get_translation_client(
&self,
) -> Result<TranslationClient<GrpcConnection>, GrpcError<RefreshError>> {
) -> Result<TranslationClient<GrpcConnection>, GrpcError<TokenError>> {
self.get_translation_client_with_endpoint(self.get_config().grpc_api_url())
}

pub(crate) fn get_translation_client_with_endpoint(
&self,
translation_grpc_endpoint: &str,
) -> Result<TranslationClient<GrpcConnection>, GrpcError<RefreshError>> {
) -> Result<TranslationClient<GrpcConnection>, GrpcError<TokenError>> {
let uri = parse_uri(translation_grpc_endpoint)?;
let channel = get_channel(uri)?;
let service =
Expand All @@ -115,6 +113,16 @@ impl Qcs {
}
}

impl Default for Qcs {
fn default() -> Self {
Self::with_config(
ClientConfiguration::builder()
.build()
.expect("builder should be valid with all defaults"),
)
}
}

/// Errors that may occur while trying to use a `gRPC` client
#[derive(Debug, thiserror::Error)]
pub enum GrpcClientError {
Expand All @@ -128,7 +136,7 @@ pub enum GrpcClientError {

/// Error due to `gRPC` error
#[error("gRPC error: {0}")]
GrpcError(#[from] GrpcError<RefreshError>),
GrpcError(#[from] GrpcError<TokenError>),
}

/// Errors that may occur while trying to use an `OpenAPI` client
Expand Down
4 changes: 2 additions & 2 deletions crates/lib/src/compiler/quilc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ mod tests {
}

async fn rpcq_client() -> rpcq::Client {
let qcs = Qcs::load().await;
let qcs = Qcs::load();
let endpoint = qcs.get_config().quilc_url();
rpcq::Client::new(endpoint).unwrap()
}
Expand Down Expand Up @@ -387,7 +387,7 @@ MEASURE 1 ro[1]

#[tokio::test]
async fn run_compiled_bell_state_on_qvm() {
let client = Qcs::load().await;
let client = Qcs::load();
let client = qvm::http::HttpClient::from(&client);
let output = rpcq_client()
.await
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct Diagnostics {

impl Diagnostics {
async fn gather() -> Self {
let client = Qcs::load().await;
let client = Qcs::load();

let (qcs, qvm) = futures::future::join(
QcsApiDiagnostics::gather(&client),
Expand Down
20 changes: 10 additions & 10 deletions crates/lib/src/executable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use quil_rs::program::ProgramError;
/// async fn main() {
/// use std::num::NonZeroU16;
/// use qcs::qvm;
/// let qvm_client = qvm::http::HttpClient::from(&Qcs::load().await);
/// let qvm_client = qvm::http::HttpClient::from(&Qcs::load());
/// let mut result = Executable::from_quil(PROGRAM).with_qcs_client(Qcs::default()).with_shots(NonZeroU16::new(4).unwrap()).execute_on_qvm(&qvm_client).await.unwrap();
/// // "ro" is the only source read from by default if you don't specify a .read_from()
///
Expand Down Expand Up @@ -158,7 +158,7 @@ impl<'executable> Executable<'executable, '_> {
///
/// #[tokio::main]
/// async fn main() {
/// let qvm_client = qvm::http::HttpClient::from(&Qcs::load().await);
/// let qvm_client = qvm::http::HttpClient::from(&Qcs::load());
/// let mut result = Executable::from_quil(PROGRAM)
/// .with_qcs_client(Qcs::default()) // Unnecessary if you have ~/.qcs/settings.toml
/// .read_from("first")
Expand Down Expand Up @@ -228,7 +228,7 @@ impl<'executable> Executable<'executable, '_> {
///
/// #[tokio::main]
/// async fn main() {
/// let qvm_client = qvm::http::HttpClient::from(&Qcs::load().await);
/// let qvm_client = qvm::http::HttpClient::from(&Qcs::load());
/// let mut exe = Executable::from_quil(PROGRAM)
/// .with_qcs_client(Qcs::default()) // Unnecessary if you have ~/.qcs/settings.toml
/// .read_from("theta");
Expand Down Expand Up @@ -301,11 +301,11 @@ impl<'executable> Executable<'executable, '_> {
/// Get a reference to the [`Qcs`] client used by the executable.
///
/// If one has not been set, a default client is loaded, set, and returned.
pub async fn qcs_client(&mut self) -> Arc<Qcs> {
pub fn qcs_client(&mut self) -> Arc<Qcs> {
if let Some(client) = &self.qcs_client {
client.clone()
} else {
let client = Arc::new(Qcs::load().await);
let client = Arc::new(Qcs::load());
self.qcs_client = Some(client.clone());
client
}
Expand Down Expand Up @@ -413,7 +413,7 @@ impl<'execution> Executable<'_, 'execution> {
self.quil.clone(),
self.shots,
id,
self.qcs_client().await,
self.qcs_client(),
self.quilc_client.clone(),
self.compiler_options,
)
Expand Down Expand Up @@ -804,7 +804,7 @@ mod describe_get_config {
use crate::{compiler::rpcq, Executable};

async fn quilc_client() -> rpcq::Client {
let qcs = Qcs::load().await;
let qcs = Qcs::load();
let endpoint = qcs.get_config().quilc_url();
rpcq::Client::new(endpoint).unwrap()
}
Expand Down Expand Up @@ -835,7 +835,7 @@ mod describe_qpu_for_id {
use crate::{client::Qcs, Executable};

async fn quilc_client() -> rpcq::Client {
let qcs = Qcs::load().await;
let qcs = Qcs::load();
let endpoint = qcs.get_config().quilc_url();
rpcq::Client::new(endpoint).unwrap()
}
Expand All @@ -844,7 +844,7 @@ mod describe_qpu_for_id {
async fn it_refreshes_auth_token() {
// Default config has no auth, so it should try to refresh
let mut exe = Executable::from_quil("")
.with_qcs_client(Qcs::default())
.with_qcs_client(Qcs::load())
.with_quilc_client(Some(quilc_client().await));
let result = exe.qpu_for_id("blah").await;
let Err(err) = result else {
Expand All @@ -864,7 +864,7 @@ mod describe_qpu_for_id {
"".into(),
shots,
"Aspen-M-3".into(),
exe.qcs_client().await,
exe.qcs_client(),
exe.quilc_client.clone(),
CompilerOpts::default(),
)
Expand Down
10 changes: 5 additions & 5 deletions crates/lib/src/qpu/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ use std::{convert::TryFrom, fmt, time::Duration};

use cached::proc_macro::cached;
use derive_builder::Builder;
use qcs_api_client_common::configuration::RefreshError;
use qcs_api_client_common::configuration::TokenError;
#[cfg(feature = "grpc-web")]
use qcs_api_client_grpc::channel::wrap_channel_with_grpc_web;
pub use qcs_api_client_grpc::channel::Error as GrpcError;
use qcs_api_client_grpc::tonic::wrap_channel_with_grpc_web;
pub use qcs_api_client_grpc::tonic::Error as GrpcError;
use qcs_api_client_grpc::{
channel::{parse_uri, wrap_channel_with, wrap_channel_with_retry},
get_channel_with_timeout,
models::controller::{
controller_job_execution_result, data_value::Value, ControllerJobExecutionResult,
Expand All @@ -22,6 +21,7 @@ use qcs_api_client_grpc::{
CancelControllerJobsRequest, ExecuteControllerJobRequest,
ExecutionOptions as InnerApiExecutionOptions, GetControllerJobResultsRequest,
},
tonic::{parse_uri, wrap_channel_with, wrap_channel_with_retry},
};
pub use qcs_api_client_openapi::apis::Error as OpenApiError;
use qcs_api_client_openapi::apis::{
Expand Down Expand Up @@ -644,7 +644,7 @@ async fn get_default_endpoint(
pub enum QpuApiError {
/// Error due to a bad gRPC configuration
#[error("Error configuring gRPC request: {0}")]
GrpcError(#[from] GrpcError<RefreshError>),
GrpcError(#[from] GrpcError<TokenError>),

/// Error due to missing gRPC endpoint for endpoint ID
#[error("Missing gRPC endpoint for endpoint ID: {0}")]
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/src/qvm/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ mod describe_execution {
use crate::{client::Qcs, qvm};

async fn qvm_client() -> qvm::http::HttpClient {
let qcs = Qcs::load().await;
let qcs = Qcs::load();
qvm::http::HttpClient::from(&qcs)
}

Expand Down
6 changes: 3 additions & 3 deletions crates/lib/tests/basic_qvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ MEASURE 1 second
"##;

async fn quilc_client() -> rpcq::Client {
let qcs = Qcs::load().await;
let qcs = Qcs::load();
let endpoint = qcs.get_config().quilc_url();
rpcq::Client::new(endpoint).unwrap()
}

async fn qvm_client() -> qvm::http::HttpClient {
let qcs = Qcs::load().await;
let qcs = Qcs::load();
qvm::http::HttpClient::from(&qcs)
}

Expand All @@ -33,7 +33,7 @@ async fn test_bell_state() {

let data = Executable::from_quil(PROGRAM)
.with_quilc_client(Some(quilc_client().await))
.with_qcs_client(Qcs::load().await)
.with_qcs_client(Qcs::load())
.with_shots(shots)
.read_from("first")
.read_from("second")
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/tests/mocked_qpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async fn setup() {
}

async fn quilc_client() -> rpcq::Client {
let qcs = Qcs::load().await;
let qcs = Qcs::load();
let endpoint = qcs.get_config().quilc_url();
rpcq::Client::new(endpoint).unwrap()
}
Expand Down
4 changes: 2 additions & 2 deletions crates/lib/tests/parametric_compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ MEASURE 0 ro[0]
"#;

async fn quilc_client() -> rpcq::Client {
let qcs = Qcs::load().await;
let qcs = Qcs::load();
let endpoint = qcs.get_config().quilc_url();
rpcq::Client::new(endpoint).unwrap()
}

async fn qvm_client() -> qvm::http::HttpClient {
let qcs = Qcs::load().await;
let qcs = Qcs::load();
qvm::http::HttpClient::from(&qcs)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/lib/tests/qvm_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ MEASURE 1 ro[1]
"##;

async fn http_qvm_client() -> HttpClient {
let qcs_client = Qcs::load().await;
let qcs_client = Qcs::load();
HttpClient::from(&qcs_client)
}

Expand Down
Loading

0 comments on commit 7a06117

Please sign in to comment.