-
Notifications
You must be signed in to change notification settings - Fork 11
/
noise_models.rs
154 lines (142 loc) · 6.22 KB
/
noise_models.rs
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
// Copyright © 2021-2022 HQS Quantum Simulations GmbH. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing permissions and
// limitations under the License.
use quote::quote;
use syn::{parse_macro_input, ItemImpl};
pub fn noise_model_wrapper_def(
_metadata: proc_macro::TokenStream,
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let parsed_input = parse_macro_input!(input as ItemImpl);
let ident = parsed_input.self_ty;
let items = parsed_input.items;
let q = quote! {
#[pymethods]
impl #ident {
#(#items)*
/// Returns a copy of the device (copy here produces a deepcopy).
///
/// Returns:
/// A deep copy of self.
///
pub fn __copy__(&self) -> Self {
self.clone()
}
/// Creates deep copy of Noise-Model.
///
/// Returns:
/// A deep copy of self.
///
pub fn __deepcopy__(&self, _memodict: Py<PyAny>) -> Self {
self.clone()
}
/// Return the bincode representation of the Noise-Model using the bincode crate.
///
/// Returns:
/// ByteArray: The serialized Noise-Model (in bincode form).
///
/// Raises:
/// ValueError: Cannot serialize Noise-Model to bytes.
///
pub fn to_bincode(&self) -> PyResult<Py<pyo3::types::PyByteArray>> {
let noise_model = NoiseModel::from(self.internal.clone());
let serialized = bincode::serialize(&noise_model)
.map_err(|_| pyo3::exceptions::PyValueError::new_err("Cannot serialize Noise-Model to bytes"))?;
let b: Py<pyo3::types::PyByteArray> = Python::with_gil(|py| -> Py<pyo3::types::PyByteArray> {
pyo3::types::PyByteArray::new(py, &serialized[..]).into()
});
Ok(b)
}
/// Return the json representation of the Noise-Model.
///
/// Returns:
/// str: The serialized form of Noise-Model.
///
/// Raises:
/// ValueError: Cannot serialize Noise-Model to json.
///
pub fn to_json(&self) -> PyResult<String> {
let noise_model = NoiseModel::from(self.internal.clone());
let serialized = serde_json::to_string(&noise_model)
.map_err(|_| pyo3::exceptions::PyValueError::new_err("Cannot serialize Noise-Model to json"))?;
Ok(serialized)
}
#[cfg(feature = "json_schema")]
/// Returns the current version of the qoqo library .
///
/// Returns:
/// str: The current version of the library.
#[staticmethod]
pub fn current_version() -> String {
ROQOQO_VERSION.to_string()
}
#[cfg(feature = "json_schema")]
/// Return the minimum version of qoqo that supports this object.
///
/// Returns:
/// str: The minimum version of the qoqo library to deserialize this object.
pub fn min_supported_version(&self) -> String {
let min_version: (u32, u32, u32) =
NoiseModel::minimum_supported_roqoqo_version(&NoiseModel::from(self.internal.clone()));
format!("{}.{}.{}", min_version.0, min_version.1, min_version.2)
}
/// Return the __richcmp__ magic method to perform rich comparison operations on mixed system.
///
/// Args:
/// other: The object to compare self to.
/// op: Whether they should be equal or not.
///
/// Returns:
/// bool
///
/// Raises:
/// NotImplementedError: Other comparison not implemented.
///
fn __richcmp__(&self, other: Py<PyAny>, op: pyo3::class::basic::CompareOp) -> PyResult<bool> {
let other = #ident::from_pyany(other);
match op {
pyo3::class::basic::CompareOp::Eq => match other {
Ok(osystem) => Ok(NoiseModel::from(self.internal.clone()) == osystem),
_ => Ok(false),
},
pyo3::class::basic::CompareOp::Ne => match other {
Ok(osystem) => Ok(NoiseModel::from(self.internal.clone()) != osystem),
_ => Ok(true),
},
_ => Err(pyo3::exceptions::PyNotImplementedError::new_err(
"Other comparison not implemented",
)),
}
}
}
impl #ident {
/// Fallible conversion of generic python object..
pub fn from_pyany(input: Py<PyAny>) -> PyResult<NoiseModel> {
Python::with_gil(|py| -> PyResult<NoiseModel> {
let input = input.as_ref(py);
if let Ok(try_downcast) = input.extract::<#ident>() {
Ok(try_downcast.internal.into())
} else {
let get_bytes = input.call_method0("to_bincode")?;
let bytes = get_bytes.extract::<Vec<u8>>()?;
bincode::deserialize(&bytes[..]).map_err(|err| {
pyo3::exceptions::PyValueError::new_err(format!(
"Cannot treat input as NoiseModel: {}",
err
))
})
}
})
}
}
};
q.into()
}