-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy patharray.rs
272 lines (228 loc) · 8.55 KB
/
array.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
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
//! Fixed-length array type and operations extension.
mod array_op;
mod array_repeat;
mod array_scan;
use std::sync::Arc;
use itertools::Itertools as _;
use lazy_static::lazy_static;
use serde::{Deserialize, Serialize};
use std::hash::{Hash, Hasher};
use crate::extension::resolution::{
resolve_type_extensions, resolve_value_extensions, ExtensionResolutionError,
WeakExtensionRegistry,
};
use crate::extension::simple_op::{MakeOpDef, MakeRegisteredOp};
use crate::extension::{ExtensionId, ExtensionSet, SignatureError, TypeDef, TypeDefBound};
use crate::ops::constant::{maybe_hash_values, CustomConst, TryHash, ValueName};
use crate::ops::{ExtensionOp, OpName, Value};
use crate::types::type_param::{TypeArg, TypeParam};
use crate::types::{CustomCheckFailure, CustomType, Type, TypeBound, TypeName};
use crate::Extension;
pub use array_op::{ArrayOp, ArrayOpDef, ArrayOpDefIter};
pub use array_repeat::{ArrayRepeat, ArrayRepeatDef, ARRAY_REPEAT_OP_ID};
pub use array_scan::{ArrayScan, ArrayScanDef, ARRAY_SCAN_OP_ID};
/// Reported unique name of the array type.
pub const ARRAY_TYPENAME: TypeName = TypeName::new_inline("array");
/// Reported unique name of the extension
pub const EXTENSION_ID: ExtensionId = ExtensionId::new_unchecked("collections.array");
/// Extension version.
pub const VERSION: semver::Version = semver::Version::new(0, 1, 0);
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
/// Statically sized array of values, all of the same type.
pub struct ArrayValue {
values: Vec<Value>,
typ: Type,
}
impl ArrayValue {
/// Name of the constructor for creating constant arrays.
#[cfg_attr(not(feature = "model_unstable"), allow(dead_code))]
pub(crate) const CTR_NAME: &'static str = "collections.array.const";
/// Create a new [CustomConst] for an array of values of type `typ`.
/// That all values are of type `typ` is not checked here.
pub fn new(typ: Type, contents: impl IntoIterator<Item = Value>) -> Self {
Self {
values: contents.into_iter().collect_vec(),
typ,
}
}
/// Create a new [CustomConst] for an empty array of values of type `typ`.
pub fn new_empty(typ: Type) -> Self {
Self {
values: vec![],
typ,
}
}
/// Returns the type of the `[ArrayValue]` as a `[CustomType]`.`
pub fn custom_type(&self) -> CustomType {
array_custom_type(self.values.len() as u64, self.typ.clone())
}
/// Returns the type of values inside the `[ArrayValue]`.
pub fn get_element_type(&self) -> &Type {
&self.typ
}
/// Returns the values contained inside the `[ArrayValue]`.
pub fn get_contents(&self) -> &[Value] {
&self.values
}
}
impl TryHash for ArrayValue {
fn try_hash(&self, mut st: &mut dyn Hasher) -> bool {
maybe_hash_values(&self.values, &mut st) && {
self.typ.hash(&mut st);
true
}
}
}
#[typetag::serde]
impl CustomConst for ArrayValue {
fn name(&self) -> ValueName {
ValueName::new_inline("array")
}
fn get_type(&self) -> Type {
self.custom_type().into()
}
fn validate(&self) -> Result<(), CustomCheckFailure> {
let typ = self.custom_type();
EXTENSION
.get_type(&ARRAY_TYPENAME)
.unwrap()
.check_custom(&typ)
.map_err(|_| {
CustomCheckFailure::Message(format!(
"Custom typ {typ} is not a valid instantiation of array."
))
})?;
// constant can only hold classic type.
let ty = match typ.args() {
[TypeArg::BoundedNat { n }, TypeArg::Type { ty }]
if *n as usize == self.values.len() =>
{
ty
}
_ => {
return Err(CustomCheckFailure::Message(format!(
"Invalid array type arguments: {:?}",
typ.args()
)))
}
};
// check all values are instances of the element type
for v in &self.values {
if v.get_type() != *ty {
return Err(CustomCheckFailure::Message(format!(
"Array element {v:?} is not of expected type {ty}"
)));
}
}
Ok(())
}
fn equal_consts(&self, other: &dyn CustomConst) -> bool {
crate::ops::constant::downcast_equal_consts(self, other)
}
fn extension_reqs(&self) -> ExtensionSet {
ExtensionSet::union_over(self.values.iter().map(Value::extension_reqs))
.union(EXTENSION_ID.into())
}
fn update_extensions(
&mut self,
extensions: &WeakExtensionRegistry,
) -> Result<(), ExtensionResolutionError> {
for val in &mut self.values {
resolve_value_extensions(val, extensions)?;
}
resolve_type_extensions(&mut self.typ, extensions)
}
}
lazy_static! {
/// Extension for array operations.
pub static ref EXTENSION: Arc<Extension> = {
Extension::new_arc(EXTENSION_ID, VERSION, |extension, extension_ref| {
extension.add_type(
ARRAY_TYPENAME,
vec![ TypeParam::max_nat(), TypeBound::Any.into()],
"Fixed-length array".into(),
TypeDefBound::from_params(vec![1] ),
extension_ref,
)
.unwrap();
array_op::ArrayOpDef::load_all_ops(extension, extension_ref).unwrap();
array_repeat::ArrayRepeatDef.add_to_extension(extension, extension_ref).unwrap();
array_scan::ArrayScanDef.add_to_extension(extension, extension_ref).unwrap();
})
};
}
fn array_type_def() -> &'static TypeDef {
EXTENSION.get_type(&ARRAY_TYPENAME).unwrap()
}
/// Instantiate a new array type given a size argument and element type.
///
/// This method is equivalent to [`array_type_parametric`], but uses concrete
/// arguments types to ensure no errors are possible.
pub fn array_type(size: u64, element_ty: Type) -> Type {
array_custom_type(size, element_ty).into()
}
/// Instantiate a new array type given the size and element type parameters.
///
/// This is a generic version of [`array_type`].
pub fn array_type_parametric(
size: impl Into<TypeArg>,
element_ty: impl Into<TypeArg>,
) -> Result<Type, SignatureError> {
instantiate_array(array_type_def(), size, element_ty)
}
fn array_custom_type(size: impl Into<TypeArg>, element_ty: impl Into<TypeArg>) -> CustomType {
instantiate_array_custom(array_type_def(), size, element_ty)
.expect("array parameters are valid")
}
fn instantiate_array_custom(
array_def: &TypeDef,
size: impl Into<TypeArg>,
element_ty: impl Into<TypeArg>,
) -> Result<CustomType, SignatureError> {
array_def.instantiate(vec![size.into(), element_ty.into()])
}
fn instantiate_array(
array_def: &TypeDef,
size: impl Into<TypeArg>,
element_ty: impl Into<TypeArg>,
) -> Result<Type, SignatureError> {
instantiate_array_custom(array_def, size, element_ty).map(Into::into)
}
/// Name of the operation in the prelude for creating new arrays.
pub const NEW_ARRAY_OP_ID: OpName = OpName::new_inline("new_array");
/// Initialize a new array op of element type `element_ty` of length `size`
pub fn new_array_op(element_ty: Type, size: u64) -> ExtensionOp {
let op = array_op::ArrayOpDef::new_array.to_concrete(element_ty, size);
op.to_extension_op().unwrap()
}
#[cfg(test)]
mod test {
use crate::builder::{inout_sig, DFGBuilder, Dataflow, DataflowHugr};
use crate::extension::prelude::{qb_t, usize_t, ConstUsize};
use crate::ops::constant::CustomConst;
use crate::std_extensions::arithmetic::float_types::ConstF64;
use super::{array_type, new_array_op, ArrayValue};
#[test]
/// Test building a HUGR involving a new_array operation.
fn test_new_array() {
let mut b =
DFGBuilder::new(inout_sig(vec![qb_t(), qb_t()], array_type(2, qb_t()))).unwrap();
let [q1, q2] = b.input_wires_arr();
let op = new_array_op(qb_t(), 2);
let out = b.add_dataflow_op(op, [q1, q2]).unwrap();
b.finish_hugr_with_outputs(out.outputs()).unwrap();
}
#[test]
fn test_array_value() {
let array_value = ArrayValue {
values: vec![ConstUsize::new(3).into()],
typ: usize_t(),
};
array_value.validate().unwrap();
let wrong_array_value = ArrayValue {
values: vec![ConstF64::new(1.2).into()],
typ: usize_t(),
};
assert!(wrong_array_value.validate().is_err());
}
}