1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#![deny(missing_docs)]
#![warn(private_intra_doc_links)]
#![warn(missing_crate_level_docs)]
#![warn(missing_doc_code_examples)]
#![warn(private_doc_tests)]
#![deny(missing_debug_implementations)]
pub use qoqo_calculator::Calculator;
use qoqo_calculator::CalculatorError;
pub use qoqo_calculator::CalculatorFloat;
use std::str::FromStr;
use thiserror::Error;
pub const ROQOQO_VERSION: &str = env!("CARGO_PKG_VERSION");
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Default)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", serde(try_from = "RoqoqoVersionSerializable"))]
#[cfg_attr(feature = "serialize", serde(into = "RoqoqoVersionSerializable"))]
struct RoqoqoVersion;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Default)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
struct RoqoqoVersionSerializable {
major_version: u32,
minor_version: u32,
}
impl TryFrom<RoqoqoVersionSerializable> for RoqoqoVersion {
type Error = RoqoqoError;
fn try_from(value: RoqoqoVersionSerializable) -> Result<Self, Self::Error> {
let mut rsplit = ROQOQO_VERSION.split('.').take(2);
let major_version = u32::from_str(
rsplit
.next()
.expect("Internal error: Version not conforming to semver"),
)
.expect("Internal error: Major version is not unsigned integer.");
let minor_version = u32::from_str(
rsplit
.next()
.expect("Internal error: Version not conforming to semver"),
)
.expect("Internal error: Minor version is not unsigned integer.");
if major_version != value.major_version {
return Err(RoqoqoError::VersionMissmatch {
library_major_version: major_version,
library_minor_version: minor_version,
data_major_version: value.major_version,
data_minor_version: value.minor_version,
});
}
if major_version == 0 {
if minor_version != value.minor_version {
return Err(RoqoqoError::VersionMissmatch {
library_major_version: major_version,
library_minor_version: minor_version,
data_major_version: value.major_version,
data_minor_version: value.minor_version,
});
}
} else if minor_version < value.minor_version {
return Err(RoqoqoError::VersionMissmatch {
library_major_version: major_version,
library_minor_version: minor_version,
data_major_version: value.major_version,
data_minor_version: value.minor_version,
});
}
Ok(RoqoqoVersion)
}
}
impl From<RoqoqoVersion> for RoqoqoVersionSerializable {
fn from(_: RoqoqoVersion) -> Self {
let mut rsplit = ROQOQO_VERSION.split('.').take(2);
let major_version = u32::from_str(
rsplit
.next()
.expect("Internal error: Version not conforming to semver"),
)
.expect("Internal error: Major version is not unsigned integer.");
let minor_version = u32::from_str(
rsplit
.next()
.expect("Internal error: Version not conforming to semver"),
)
.expect("Internal error: Minor version is not unsigned integer.");
RoqoqoVersionSerializable {
major_version,
minor_version,
}
}
}
#[derive(Error, Debug, PartialEq)]
pub enum RoqoqoError {
#[error("Resulting gate matrix is not unitary. Please check values of alpha and beta: alpha_r: {alpha_r:?}, alpha_i: {alpha_i:?}, beta_r: {beta_r:?}, beta_i: {beta_i:?}, norm: {norm:?}.")]
UnitaryMatrixErrror {
alpha_r: f64,
alpha_i: f64,
beta_r: f64,
beta_i: f64,
norm: f64,
},
#[error("Mapping of qubit {qubit:?} failed")]
QubitMappingError {
qubit: usize,
},
#[error("Conversion from {start_type} to {end_type} failed")]
ConversionError {
start_type: &'static str,
end_type: &'static str,
},
#[error("TryFrom conversion failed")]
TryFromError,
#[error("Qubits {squbit} and {oqubit} incompatible. Gates acting on different qubits can not be multiplied.")]
MultiplicationIncompatibleQubits {
squbit: usize,
oqubit: usize,
},
#[error("Pauli product involves qubit {pp_qubit} but number qubits is lower {number_qubits}.")]
PauliProductExceedsQubits {
pp_qubit: usize,
number_qubits: usize,
},
#[error(
"Index of operator {index:?} exceeds Hilbert space dimension of {number_qubits} qubits."
)]
MismatchedOperatorDimension {
index: (usize, usize),
number_qubits: usize,
},
#[error(
"Dimension of register {dim:?} exceeds Hilbert space dimension of {number_qubits} qubits."
)]
MismatchedRegisterDimension {
dim: usize,
number_qubits: usize,
},
#[error("Name {name} of expectation value already taken.")]
ExpValUsedTwice {
name: String,
},
#[error("OutputRegister {name} is missing.")]
MissingRegister {
name: String,
},
#[error("Error occured in basis rotation measurement. {msg}")]
BasisRotationMeasurementError {
msg: String,
},
#[error("An error occured serializing a roqoqo object: {msg} ")]
SerializationError {
msg: String,
},
#[error("An error occured in roqoqo: {msg} ")]
GenericError {
msg: String,
},
#[error("Trying to deserialize data created with incompatible version of roqoqo Library version: {library_major_version}.{library_minor_version} Data version: {data_major_version}.{data_minor_version}. Try to convert data with roqoqo data conversion tool.")]
VersionMissmatch {
library_major_version: u32,
library_minor_version: u32,
data_major_version: u32,
data_minor_version: u32,
},
#[error(transparent)]
CalculatorError(#[from] CalculatorError),
}
#[derive(Error, Debug, PartialEq)]
pub enum RoqoqoBackendError {
#[error("Operation {hqslang} not supported by backend {hqslang}: ")]
OperationNotInBackend {
backend: &'static str,
hqslang: &'static str,
},
#[error("Backend authentification information is missing: {msg} ")]
MissingAuthentification {
msg: String,
},
#[error("NetworkError communicating with: {msg} ")]
NetworkError {
msg: String,
},
#[error("Backend timed out: {msg} ")]
Timeout {
msg: String,
},
#[error("The file at this location already exists: {path} ")]
FileAlreadyExists {
path: String,
},
#[error("An error occured in the backend: {msg} ")]
GenericError {
msg: String,
},
#[error(transparent)]
RoqoqoError(#[from] RoqoqoError),
#[error(transparent)]
CalculatorError(#[from] CalculatorError),
}
#[doc(hidden)]
mod circuit;
pub mod operations;
pub mod prelude;
pub use circuit::*;
pub mod backends;
pub mod devices;
pub mod measurements;
mod quantum_program;
pub mod registers;
pub use quantum_program::QuantumProgram;