Enum QasmSimError

Source
#[non_exhaustive]
pub enum QasmSimError<'src> { UnknownError(String), InvalidToken { source: &'src str, lineno: usize, startpos: usize, endpos: Option<usize>, token: Option<Tok>, expected: Vec<String>, }, UnexpectedEOF { source: &'src str, lineno: usize, startpos: usize, endpos: Option<usize>, token: Option<Tok>, expected: Vec<String>, }, UnexpectedToken { source: &'src str, lineno: usize, startpos: usize, endpos: Option<usize>, token: Option<Tok>, expected: Vec<String>, }, RedefinitionError { source: &'src str, symbol_name: String, lineno: usize, previous_lineno: usize, }, LibraryNotFound { source: &'src str, libpath: String, lineno: usize, }, IndexOutOfBounds { source: &'src str, lineno: usize, symbol_name: String, index: usize, size: usize, }, SymbolNotFound { source: &'src str, lineno: usize, symbol_name: String, expected: QasmType, }, WrongNumberOfParameters { source: &'src str, lineno: usize, symbol_name: String, are_registers: bool, given: usize, expected: usize, }, UndefinedGate { source: &'src str, lineno: usize, symbol_name: String, }, TypeMismatch { source: &'src str, lineno: usize, symbol_name: String, expected: QasmType, }, RegisterSizeMismatch { source: &'src str, lineno: usize, symbol_name: String, sizes: Vec<usize>, }, }
Expand description

Types of errors in QasmSim. QasmSim errors contain information about the error and the location in the source code where the error happens.

QasmSimError instances can be printed. They refer to the source code and try to provide contextual information for fixing the problem.

Conversion between ParseError, RuntimeError and LinkerError is possible thanks to the trait From is defined for the pair (&'source str, T) (see alias SrcAndErr) for all the errors listed above.

§Examples

The error type of simulate() is RuntimeError. RuntimeError is a sourceless error in the sense it does not relate with the concrete source code beyond the location in the AST at which the error happens.

You can use map_err for for capturing the error and converting it into a QasmSimError from its pairing with the source.

use qasmsim::{QasmSimError, parse_and_link, simulate};

let source = r#"
OPENQASM 2.0;
qreg q[2];
CX q[1], q[2]; // Notice we are indexing out of bounds here.
"#;
let program = parse_and_link(source)?;
let runtime_error = simulate(&program).expect_err("Index out of bounds");
let qasmsim_error = QasmSimError::from((source, runtime_error));

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

UnknownError(String)

A generic unknown error.

§

InvalidToken

Found an invalid token at some position.

Fields

§source: &'src str

Line source.

§lineno: usize

Line number.

§startpos: usize

Position inside the line (0-based) where the invalid token starts.

§endpos: Option<usize>

Position inside the line (0-based) where the invalid token ends.

§token: Option<Tok>

Token found.

§expected: Vec<String>

A list of expected tokens.

§

UnexpectedEOF

Found an unexpected end of file.

Fields

§source: &'src str

Line source.

§lineno: usize

Line number.

§startpos: usize

Position inside the line (0-based) where the invalid token starts.

§endpos: Option<usize>

Position inside the line (0-based) where the invalid token ends.

§token: Option<Tok>

Token found.

§expected: Vec<String>

A list of expected tokens.

§

UnexpectedToken

Found an unexpected token.

Fields

§source: &'src str

Line source.

§lineno: usize

Line number.

§startpos: usize

Position inside the line (0-based) where the invalid token starts.

§endpos: Option<usize>

Position inside the line (0-based) where the invalid token ends.

§token: Option<Tok>

Token found.

§expected: Vec<String>

A list of expected tokens.

§

RedefinitionError

Found a redefinition of a register.

Fields

§source: &'src str

Line source.

§symbol_name: String

Name of the register declared for second time.

§lineno: usize

Line number.

§previous_lineno: usize

Line number where the register was originally declared.

§

LibraryNotFound

The unability of linking a library.

Fields

§source: &'src str

Line source.

§libpath: String

Path to the library to be included.

§lineno: usize

Line number.

§

IndexOutOfBounds

Use of register index that does not fit the register size.

Fields

§source: &'src str

Line source.

§lineno: usize

Line number.

§symbol_name: String

Name of the register being indexed.

§index: usize

Index tried to access.

§size: usize

Size of the register.

§

SymbolNotFound

Use of an unknown/undeclared symbol.

Fields

§source: &'src str

Line source.

§lineno: usize

Line number.

§symbol_name: String

Name of the unknown symbol.

§expected: QasmType

The expected type.

§

WrongNumberOfParameters

The attempt of applying an operation passing the wrong number of parameters.

Fields

§source: &'src str

Line source.

§lineno: usize

Line number.

§symbol_name: String

Name of the operation.

§are_registers: bool

Indicate if the parameters are registers or real values.

§given: usize

The number of passed parameters.

§expected: usize

The number of expected parameters.

§

UndefinedGate

Use of a gate not previously defined.

Fields

§source: &'src str

Line source.

§lineno: usize

Line number.

§symbol_name: String

Name of the unknown gate.

§

TypeMismatch

Found an unexpected type of value.

Fields

§source: &'src str

Line source.

§lineno: usize

Line number.

§symbol_name: String

Name of the symbol with the incorrect type.

§expected: QasmType

Expected type.

§

RegisterSizeMismatch

Attempt of applying an operation to different sizes registers.

Fields

§source: &'src str

Line source.

§lineno: usize

Line number.

§symbol_name: String

Name of the operation.

§sizes: Vec<usize>

Sizes of the different registers involved.

Trait Implementations§

Source§

impl<'src> Clone for QasmSimError<'src>

Source§

fn clone(&self) -> QasmSimError<'src>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'src> Debug for QasmSimError<'src>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for QasmSimError<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for QasmSimError<'_>

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl<'src> From<(&'src str, LinkerError)> for QasmSimError<'src>

Source§

fn from(source_and_error: SrcAndErr<'src, LinkerError>) -> Self

Converts to this type from the input type.
Source§

impl<'src> From<(&'src str, ParseError<Location, Tok, LexicalError<Location>>)> for QasmSimError<'src>

Source§

fn from(src_and_err: SrcAndErr<'src, ParseError>) -> Self

Converts to this type from the input type.
Source§

impl<'src> From<(&'src str, RuntimeError)> for QasmSimError<'src>

Source§

fn from(source_and_error: SrcAndErr<'src, RuntimeError>) -> Self

Converts to this type from the input type.
Source§

impl From<String> for QasmSimError<'_>

Source§

fn from(err: String) -> Self

Converts to this type from the input type.
Source§

impl<'src> Hash for QasmSimError<'src>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'src> PartialEq for QasmSimError<'src>

Source§

fn eq(&self, other: &QasmSimError<'src>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'src> Eq for QasmSimError<'src>

Source§

impl<'src> StructuralPartialEq for QasmSimError<'src>

Auto Trait Implementations§

§

impl<'src> Freeze for QasmSimError<'src>

§

impl<'src> RefUnwindSafe for QasmSimError<'src>

§

impl<'src> Send for QasmSimError<'src>

§

impl<'src> Sync for QasmSimError<'src>

§

impl<'src> Unpin for QasmSimError<'src>

§

impl<'src> UnwindSafe for QasmSimError<'src>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V