Skip to content

Commit

Permalink
feat: derive more traits on types (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadow53 authored Aug 11, 2022
1 parent db7b4c0 commit 552cf0c
Show file tree
Hide file tree
Showing 18 changed files with 44 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/configuration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl Configuration {
}
}

#[derive(Debug, Serialize)]
#[derive(Debug, Serialize, Copy, Clone, Eq, PartialEq)]
struct TokenRequest<'a> {
grant_type: &'static str,
client_id: &'a str,
Expand All @@ -154,7 +154,7 @@ impl<'a> TokenRequest<'a> {
}
}

#[derive(Deserialize, Debug)]
#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
struct TokenResponse {
refresh_token: String,
access_token: String,
Expand Down
6 changes: 3 additions & 3 deletions src/configuration/secrets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ mod describe_load {
}
}

#[derive(Deserialize, Debug, PartialEq, Serialize)]
#[derive(Deserialize, Debug, PartialEq, Serialize, Clone, Eq)]
pub(crate) struct Secrets {
pub credentials: HashMap<String, Credential>,
}
Expand All @@ -76,12 +76,12 @@ fn default_credentials() -> HashMap<String, Credential> {
map
}

#[derive(Deserialize, Debug, Default, PartialEq, Serialize)]
#[derive(Deserialize, Debug, Default, PartialEq, Serialize, Clone, Eq)]
pub(crate) struct Credential {
pub token_payload: Option<TokenPayload>,
}

#[derive(Deserialize, Debug, Default, PartialEq, Serialize)]
#[derive(Deserialize, Debug, Default, PartialEq, Serialize, Clone, Eq)]
pub(crate) struct TokenPayload {
pub refresh_token: Option<String>,
pub access_token: Option<String>,
Expand Down
10 changes: 5 additions & 5 deletions src/configuration/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ mod describe_load {
}
}

#[derive(Deserialize, Debug, PartialEq, Serialize)]
#[derive(Deserialize, Debug, PartialEq, Serialize, Clone, Eq)]
pub(crate) struct Settings {
/// Which profile to select settings from when none is specified.
pub default_profile_name: String,
Expand Down Expand Up @@ -89,7 +89,7 @@ fn default_auth_servers() -> HashMap<String, AuthServer> {
map
}

#[derive(Deserialize, Debug, PartialEq, Serialize)]
#[derive(Deserialize, Debug, PartialEq, Serialize, Clone, Eq)]
pub(crate) struct Profile {
/// URL of the QCS API to use for all API calls
pub api_url: String,
Expand All @@ -110,12 +110,12 @@ impl Default for Profile {
}
}

#[derive(Deserialize, Debug, Default, PartialEq, Serialize)]
#[derive(Deserialize, Debug, Default, PartialEq, Serialize, Clone, Eq)]
pub(crate) struct Applications {
pub pyquil: Pyquil,
}

#[derive(Deserialize, Debug, PartialEq, Serialize)]
#[derive(Deserialize, Debug, PartialEq, Serialize, Clone, Eq)]
pub(crate) struct Pyquil {
pub qvm_url: String,
pub quilc_url: String,
Expand All @@ -130,7 +130,7 @@ impl Default for Pyquil {
}
}

#[derive(Clone, Deserialize, Debug, PartialEq, Serialize)]
#[derive(Clone, Deserialize, Debug, PartialEq, Serialize, Eq)]
pub(crate) struct AuthServer {
pub(crate) client_id: String,
pub(crate) issuer: String,
Expand Down
3 changes: 2 additions & 1 deletion src/executable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ use crate::{qpu, qvm, ExecutionData};
/// You should be able to largely ignore these, just keep in mind that any borrowed data passed to
/// the methods most likely needs to live as long as this struct. Check individual methods for
/// specifics. If only using `'static` strings then everything should just work.
#[derive(Clone)]
pub struct Executable<'executable, 'execution> {
quil: Arc<str>,
shots: u16,
Expand Down Expand Up @@ -452,7 +453,7 @@ pub enum Error {
InvalidJobHandle,
}

#[derive(Debug)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
/// The external services that this SDK may connect to. Used to differentiate between networking
/// issues in [`Error::Connection`].
pub enum Service {
Expand Down
1 change: 1 addition & 0 deletions src/execution_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::RegisterData;
/// The result of executing an [`Executable`][crate::Executable] via
/// [`Executable::execute_on_qvm`][crate::Executable::execute_on_qvm] or
/// [`Executable::execute_on_qpu`][crate::Executable::execute_on_qpu].
#[derive(Debug, Clone, PartialEq)]
pub struct ExecutionData {
/// The data of all registers that were read from
/// (via [`Executable::read_from`][crate::Executable::read_from]). Key is the name of the
Expand Down
1 change: 1 addition & 0 deletions src/qpu/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use super::{
/// Contains all the info needed for a single run of an [`crate::Executable`] against a QPU. Can be
/// updated with fresh parameters in order to re-run the same program against the same QPU with the
/// same number of shots.
#[derive(Clone)]
pub(crate) struct Execution<'a> {
program: RewrittenProgram,
pub(crate) quantum_processor_id: &'a str,
Expand Down
2 changes: 1 addition & 1 deletion src/qpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn process_buffers(
.collect()
}

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Clone, Eq, Hash)]
struct BufferName {
register_name: String,
buffer_name: String,
Expand Down
2 changes: 1 addition & 1 deletion src/qpu/quilc/isa/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use super::operator::{
};

/// Represents a connection between two qubits.
#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, Clone, PartialEq)]
pub(crate) struct Edge {
#[serde(rename = "ids")]
id: EdgeId,
Expand Down
2 changes: 1 addition & 1 deletion src/qpu/quilc/isa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod operator;
mod qubit;

/// Restructuring of an [`InstructionSetArchitecture`] for sending to quilc
#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, Clone, PartialEq)]
pub(crate) struct CompilerIsa {
#[serde(rename = "1Q")]
qubits: HashMap<String, Qubit>,
Expand Down
4 changes: 2 additions & 2 deletions src/qpu/quilc/isa/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::{Serialize, Serializer};

/// Contains all the operators for a single Site ([`super::qubit::Qubit`] or [`super::edge::Edge`]) organized to allow
/// deduplication by name
#[derive(Debug)]
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct OperatorMap(HashSet<&'static str>, Vec<Operator>);

impl OperatorMap {
Expand Down Expand Up @@ -270,7 +270,7 @@ mod describe_parameters {
}
}

#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Arguments {
Int(i32),
Underscores,
Expand Down
3 changes: 2 additions & 1 deletion src/qpu/quilc/isa/qubit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use super::operator::{Arguments, OperatorMap, Parameters, PERFECT_DURATION, PERF
use super::Operator;

/// Represents a single Qubit on a QPU and its capabilities. Needed by quilc for optimization.
#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, Clone, PartialEq)]
pub(crate) struct Qubit {
id: i32,
#[serde(skip_serializing_if = "is_false")]
Expand Down Expand Up @@ -131,6 +131,7 @@ mod describe_qubit {
}
}

#[derive(Debug, Clone, PartialEq)]
pub(crate) struct FrbSim1q(Vec<Characteristic>);

impl TryFrom<Vec<Operation>> for FrbSim1q {
Expand Down
11 changes: 6 additions & 5 deletions src/qpu/quilc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ impl Error {

/// A wrapper around a [`String`] which indicates the string contains valid Native Quil. That is,
/// Quil which has been processed through `quilc`.
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct NativeQuil(String);

impl NativeQuil {
Expand All @@ -84,7 +85,7 @@ impl From<NativeQuil> for String {

/// A wrapper around [`Program`] which indicates it has been converted to `NativeQuil` (has been run
/// through `quilc` and therefore is suitable to use on QPUs.
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Clone)]
pub(super) struct NativeQuilProgram(Program);

impl TryFrom<NativeQuil> for NativeQuilProgram {
Expand All @@ -102,13 +103,13 @@ impl From<NativeQuilProgram> for Program {
}
}

#[derive(Deserialize)]
#[derive(Clone, Deserialize, Debug, Eq, PartialEq, Ord, PartialOrd)]
struct QuilcResponse {
pub quil: String,
}

/// The top level params that get passed to quilc
#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, Clone, PartialEq)]
struct QuilcParams {
protoquil: Option<bool>,
#[serde(rename = "*args")]
Expand All @@ -125,7 +126,7 @@ impl QuilcParams {
}

/// The expected request structure for sending Quil to quilc to be compiled
#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, Clone, PartialEq)]
#[serde(tag = "_type")]
struct NativeQuilRequest {
quil: String,
Expand All @@ -142,7 +143,7 @@ impl NativeQuilRequest {
}

/// Description of a device to compile for, part of [`NativeQuilRequest`]
#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, Clone, PartialEq)]
#[serde(tag = "_type")]
struct TargetDevice {
isa: CompilerIsa,
Expand Down
2 changes: 2 additions & 0 deletions src/qpu/rewrite_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,13 @@ fn substitution(expression: Expression, substitutions: &mut Substitutions) -> Ex
Expression::Address(reference)
}

#[derive(Debug, Clone, PartialEq)]
pub(crate) struct RewrittenProgram {
inner: Program,
pub(crate) substitutions: Substitutions,
}

#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub(crate) struct RewrittenQuil(String);

impl From<RewrittenQuil> for String {
Expand Down
1 change: 1 addition & 0 deletions src/qpu/rpcq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ impl<'params, T: Serialize> RPCRequest<'params, T> {
}

/// Credentials for connecting to RPCQ Server
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
struct Credentials<'a> {
pub client_secret_key: &'a str,
pub client_public_key: &'a str,
Expand Down
13 changes: 7 additions & 6 deletions src/qpu/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::executable::Parameters;
use super::rpcq::{Client, Error as RPCQError, RPCRequest};

/// The QCS Job ID. Useful for debugging or retrieving results later.
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub(crate) struct JobId(pub(crate) String);

/// Execute compiled program on a QPU.
Expand Down Expand Up @@ -63,7 +64,7 @@ impl From<RPCQError> for Error {
}
}

#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, Clone, PartialEq)]
struct QPUParams<'request> {
request: QPURequest<'request>,
priority: u8,
Expand All @@ -84,7 +85,7 @@ impl<'params> From<&'params QPUParams<'params>> for RPCRequest<'params, QPUParam
}
}

#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, PartialEq, Clone)]
#[serde(tag = "_type")]
struct QPURequest<'request> {
id: String,
Expand All @@ -102,7 +103,7 @@ impl<'request> QPURequest<'request> {
}
}

#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, Clone, Eq, PartialEq, Hash)]
struct GetExecutionResultsRequest {
job_id: String,
wait: bool,
Expand All @@ -122,7 +123,7 @@ impl<'request> From<&'request GetExecutionResultsRequest>
}
}

#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Clone, Eq, PartialEq)]
pub(crate) struct GetExecutionResultsResponse {
pub(crate) buffers: HashMap<String, Buffer>,
#[serde(default)]
Expand All @@ -133,14 +134,14 @@ pub(crate) struct GetExecutionResultsResponse {
///
/// Generally this should not be used directly, but converted into an appropriate
/// 2-D array.
#[derive(Deserialize, Debug, PartialEq)]
#[derive(Deserialize, Debug, PartialEq, Clone, Eq)]
pub(crate) struct Buffer {
shape: Vec<usize>,
data: ByteBuf,
dtype: DataType,
}

#[derive(Deserialize, Debug, PartialEq)]
#[derive(Deserialize, Debug, Copy, Clone, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub(crate) enum DataType {
Float64,
Expand Down
1 change: 1 addition & 0 deletions src/qvm/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use super::{Request, Response};

/// Contains all the info needed to execute on a QVM a single time, with the ability to be reused for
/// faster subsequent runs.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Execution {
program: Program,
}
Expand Down
10 changes: 5 additions & 5 deletions src/qvm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ use crate::RegisterData;

mod execution;

#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Clone, PartialEq)]
#[serde(untagged)]
pub(super) enum Response {
Success(Success),
Failure(Failure),
}

#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Clone, PartialEq)]
pub(super) struct Success {
#[serde(flatten)]
pub registers: HashMap<String, RegisterData>,
}

#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Clone, Eq, PartialEq)]
pub(super) struct Failure {
/// The message from QVM describing what went wrong.
pub status: String,
}

#[derive(Serialize)]
#[derive(Serialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
struct Request<'request> {
quil_instructions: String,
Expand All @@ -52,7 +52,7 @@ impl<'request> Request<'request> {
}
}

#[derive(Serialize)]
#[derive(Serialize, Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
enum RequestType {
Multishot,
Expand Down
2 changes: 1 addition & 1 deletion src/register_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::qpu::{DecodeError, Register};
/// Typically you will already know what type of data the `ExecutionResult` _should_ have, so you can
/// use the [`mod@enum_as_inner`] methods (e.g. [`ExecutionResult::into_i8`]) in order to
/// convert any variant type to its inner data.
#[derive(Debug, Deserialize, EnumAsInner, PartialEq, Serialize)]
#[derive(Clone, Debug, Deserialize, EnumAsInner, PartialEq, Serialize)]
#[serde(untagged)]
pub enum RegisterData {
/// Corresponds to the Quil `BIT` or `OCTET` types.
Expand Down

0 comments on commit 552cf0c

Please sign in to comment.