Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lib)!: clean up Error type variants, prefer error objects to strings #431

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
4 changes: 2 additions & 2 deletions crates/lib/src/compiler/libquil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,14 @@ mod test {
assert_eq!(output.program.to_quil_or_debug(), EXPECTED_H0_OUTPUT);
}

const BELL_STATE: &str = r##"DECLARE ro BIT[2]
const BELL_STATE: &str = r"DECLARE ro BIT[2]

H 0
CNOT 0 1

MEASURE 0 ro[0]
MEASURE 1 ro[1]
"##;
";

#[tokio::test]
async fn test_print_isa() {
Expand Down
39 changes: 3 additions & 36 deletions crates/lib/src/executable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ use std::borrow::Cow;
use std::collections::HashMap;
use std::num::NonZeroU16;
use std::sync::Arc;
use std::time::Duration;

use qcs_api_client_common::configuration::LoadError;
use quil_rs::quil::ToQuilError;

use crate::client::Qcs;
use crate::client::{GrpcClientError, Qcs};
use crate::compiler::quilc::{self, CompilerOpts};
use crate::execution_data::{self, ResultData};
use crate::qpu::api::{ExecutionOptions, JobId};
Expand Down Expand Up @@ -603,29 +601,9 @@ impl<'execution> Executable<'_, 'execution> {
/// [`Executable::execute_on_qvm`]..
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Communicating with QCS requires appropriate settings and secrets files. By default, these
/// should be `$HOME/.qcs/settings.toml` and `$HOME/.qcs/secrets.toml`, though those files can
/// be overridden by setting the `QCS_SETTINGS_FILE_PATH` and `QCS_SECRETS_FILE_PATH`
/// environment variables.
///
/// This error can occur when one of those files is required but missing or there is a problem
/// with the contents of those files.
#[error("There was a problem related to your QCS settings: {0}")]
Settings(String),
/// This error occurs when the SDK was unable to authenticate a request to QCS. This could mean
/// that your credentials are invalid or expired, or that you do not have access to the requested
/// QPU.
#[error("Could not authenticate a request to QCS for the requested QPU.")]
Authentication,
/// An API error occurred while connecting to the QPU.
#[error("An API error occurred while connecting to the QPU: {0}")]
QpuApiError(#[from] qpu::api::QpuApiError),
/// This happens when the QPU is down for maintenance and not accepting new jobs. If you receive
/// this error, internal compilation caches will have been cleared as programs should be recompiled
/// with new settings after a maintenance window. If you are mid-experiment, you might want to
/// start over.
#[error("QPU currently unavailable, retry after {} seconds", .0.as_secs())]
QpuUnavailable(Duration),
/// Indicates a problem connecting to an external service. Check your network connection and
/// ensure that any required local services (e.g., `qvm` or `quilc`) are running.
#[error("Error connecting to service {0:?}")]
Expand All @@ -643,29 +621,18 @@ pub enum Error {
Compilation(String),
/// There was a problem when translating the Quil program.
#[error("There was a problem translating the Quil program: {0}")]
Translation(String),
Translation(GrpcClientError),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many of the other errors look to be handled within QpuApiError now, which also has it's own variant with the same type QpuApiError::GrpcClientError(#[from] GrpcClientError)

/// There was a problem when rewriting parameter arithmetic in the Quil program.
#[error("There was a problem rewriting parameter arithmetic in the Quil program: {0}")]
RewriteArithmetic(#[from] rewrite_arithmetic::Error),
/// There was a problem when substituting parameters in the Quil program.
#[error("There was a problem substituting parameters in the Quil program: {0}")]
Substitution(String),
/// The Quil program is missing readout sources.
#[error("The Quil program is missing readout sources")]
MissingRoSources,
/// This error returns when a runtime check that _should_ always pass fails. This most likely
/// indicates a bug in the SDK and should be reported to
/// [GitHub](https://github.com/rigetti/qcs-sdk-rust/issues),
#[error("An unexpected error occurred, please open an issue on GitHub: {0:?}")]
Unexpected(String),
/// Occurs when [`Executable::retrieve_results`] is called with an invalid [`JobHandle`].
/// Calling functions on [`Executable`] between [`Executable::submit_to_qpu`] and
/// [`Executable::retrieve_results`] can invalidate the handle.
#[error("The job handle was not valid")]
InvalidJobHandle,
/// Occurs when failing to construct a [`Qcs`] client.
#[error("The QCS client configuration failed to load")]
QcsConfigLoadFailure(#[from] LoadError),
}

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -700,7 +667,7 @@ impl From<ExecutionError> for Error {
match err {
ExecutionError::Unexpected(inner) => Self::Unexpected(format!("{inner:?}")),
ExecutionError::Quilc { .. } => Self::Connection(Service::Quilc),
ExecutionError::QcsClient(v) => Self::Unexpected(format!("{v:?}")),
ExecutionError::Translation(e) => Self::Translation(e),
ExecutionError::Isa(v) => Self::Unexpected(format!("{v:?}")),
ExecutionError::ReadoutParse(v) => Self::Unexpected(format!("{v:?}")),
ExecutionError::Quil(e) => Self::Quil(e),
Expand Down
3 changes: 3 additions & 0 deletions crates/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ pub use execution_data::{
};
pub use register_data::RegisterData;

// Re-export tonic for consumers to use.
pub use tonic;

pub mod client;
pub mod compiler;
pub mod diagnostics;
Expand Down
4 changes: 2 additions & 2 deletions crates/lib/src/qpu/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ pub(crate) enum Error {
Unexpected(#[from] Unexpected),
#[error("Problem communicating with quilc at {uri}: {details}")]
Quilc { uri: String, details: String },
#[error("Problem using QCS API: {0}")]
QcsClient(#[from] GrpcClientError),
#[error("Problem translating using QCS API: {0}")]
Translation(#[from] GrpcClientError),
#[error("Problem fetching ISA: {0}")]
Isa(#[from] GetIsaError),
#[error("Problem parsing memory readout: {0}")]
Expand Down
4 changes: 2 additions & 2 deletions crates/lib/src/qpu/rewrite_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ impl RewrittenProgram {
}
}

pub(crate) type Substitutions = IndexSet<Expression>;

#[cfg(test)]
mod describe_rewrite_arithmetic {
use std::str::FromStr;
Expand Down Expand Up @@ -559,5 +561,3 @@ SHIFT-PHASE 0 "rf" __SUBST[0]
insta::assert_snapshot!(substitutions[0].to_quil_or_debug());
}
}

pub(crate) type Substitutions = IndexSet<Expression>;
3 changes: 3 additions & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ ignore = [
"RUSTSEC-2023-0052", # Introduced by transitive dependency `webpki`.
# `hyper-proxy`, then `qcs-api-client-rust` need to update in order to remove
# `webpki`.
"RUSTSEC-2024-0006" # Introduced by transitive dependency `shlex`.
# `bindgen`, then `libquil-sys` need to update in order to get a version
# of `shlex` >= 1.3.0.
]

# This section is considered when running `cargo deny check licenses`
Expand Down
Loading