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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
use serde::{Deserialize, Serialize};
use crate::index::{IndexDirection, TensorIndex};
use std::ops::Add;
/// Represents the shape of a tensor as it will be generated.
///
/// While tensors can conceptually have rank larger than four, even infinite,
/// tensors in the OpenQudit Expression library are generated into a buffer
/// indexed by 0, 1, 2, 3, or 4 physical dimensions.
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub enum GenerationShape {
/// A 0-dimensional tensor (a single value).
Scalar,
/// A 1-dimensional tensor with `nelems` elements.
Vector(usize),
/// A 2-dimensional tensor (matrix) with `nrows` rows and `ncols` columns.
Matrix(usize, usize),
/// A 3-dimensional tensor with `nmats` matrices, each of `nrows` rows and `ncols` columns.
Tensor3D(usize, usize, usize),
/// A 4-dimensional tensor usually for derivatives (ntens, nmats, nrows, ncols)
Tensor4D(usize, usize, usize, usize),
}
impl std::fmt::Debug for GenerationShape {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
GenerationShape::Scalar => write!(f, "Scalar"),
GenerationShape::Vector(nelems) => write!(f, "Vector({})", nelems),
GenerationShape::Matrix(nrows, ncols) => write!(f, "Matrix({}, {})", nrows, ncols),
GenerationShape::Tensor3D(nmats, nrows, ncols) => {
write!(f, "Tensor3D({}, {}, {})", nmats, nrows, ncols)
}
GenerationShape::Tensor4D(ntens, nmats, nrows, ncols) => {
write!(f, "Tensor4D({}, {}, {}, {})", ntens, nmats, nrows, ncols)
}
}
}
}
impl GenerationShape {
/// Calculates the total number of elements in a tensor with this shape.
///
/// # Returns
/// The total number of elements as `usize`.
pub fn num_elements(&self) -> usize {
match self {
GenerationShape::Scalar => 1,
GenerationShape::Vector(nelems) => *nelems,
GenerationShape::Matrix(nrows, ncols) => nrows * ncols,
GenerationShape::Tensor3D(nmats, nrows, ncols) => nmats * nrows * ncols,
GenerationShape::Tensor4D(ntens, nmats, nrows, ncols) => ntens * nmats * nrows * ncols,
}
}
/// Determines the shape of the derivative of a tensor with respect to `num_params` parameters.
///
/// This method effectively prepends `num_params` to the current tensor's dimensions.
/// For example, the derivative of a `Scalar` with respect to `num_params` becomes a `Vector(num_params)`.
/// The derivative of a `Matrix(R, C)` becomes a `Tensor3D(num_params, R, C)`.
///
/// # Arguments
/// * `num_params` - The number of parameters in the gradient.
///
/// # Returns
/// A new `GenerationShape` representing the shape of the gradient.
///
/// # See Also
/// - `[hessian_shape]` For the shape of a hessian tensor.
pub fn gradient_shape(&self, num_params: usize) -> Self {
match self {
GenerationShape::Scalar => GenerationShape::Vector(num_params),
GenerationShape::Vector(nelems) => GenerationShape::Matrix(num_params, *nelems),
GenerationShape::Matrix(nrows, ncols) => {
GenerationShape::Tensor3D(num_params, *nrows, *ncols)
}
GenerationShape::Tensor3D(nmats, nrows, ncols) => {
GenerationShape::Tensor4D(num_params, *nmats, *nrows, *ncols)
}
GenerationShape::Tensor4D(_, _, _, _) => {
panic!("Unable to find shape for gradient of 4D tensor.")
}
}
}
/// Determine the hessian shape of a tensor with this shape that has `num_params` parameters.
///
/// # Arguments
/// * `num_params` - The number of parameters in the hessian.
///
/// # Returns
/// A new `GenerationShape` representing the shape of the hessian.
///
/// # See Also
/// - `[gradient_shape]` For the shape of a gradient tensor.
pub fn hessian_shape(&self, num_params: usize) -> Self {
let sym_sq_size = num_params * (num_params - 1) / 2; // TODO: Is it +1 or -1
match self {
GenerationShape::Scalar => GenerationShape::Vector(sym_sq_size),
GenerationShape::Vector(nelems) => GenerationShape::Matrix(sym_sq_size, *nelems),
GenerationShape::Matrix(nrows, ncols) => {
GenerationShape::Tensor3D(sym_sq_size, *nrows, *ncols)
}
GenerationShape::Tensor3D(nmats, nrows, ncols) => {
GenerationShape::Tensor4D(sym_sq_size, *nmats, *nrows, *ncols)
}
GenerationShape::Tensor4D(_, _, _, _) => {
panic!("Unable to find shape for Hessian of 4D tensor.")
}
}
}
/// Converts the tensor shape object to a vector of integers.
///
/// # Returns
///
/// A `Vec<usize>` containing the dimensions of the shape.
///
/// # Examples
/// ```
/// use qudit_expr::GenerationShape;
///
/// let scalar_shape = GenerationShape::Scalar;
/// assert_eq!(scalar_shape.to_vec(), Vec::<usize>::new());
///
/// let vector_shape = GenerationShape::Vector(5);
/// assert_eq!(vector_shape.to_vec(), vec![5]);
///
/// let matrix_shape = GenerationShape::Matrix(2, 3);
/// assert_eq!(matrix_shape.to_vec(), vec![2, 3]);
/// ```
pub fn to_vec(&self) -> Vec<usize> {
match self {
GenerationShape::Scalar => vec![],
GenerationShape::Vector(nelems) => vec![*nelems],
GenerationShape::Matrix(nrows, ncols) => vec![*nrows, *ncols],
GenerationShape::Tensor3D(nmats, nrows, ncols) => vec![*nmats, *nrows, *ncols],
GenerationShape::Tensor4D(ntens, nmats, nrows, ncols) => {
vec![*ntens, *nmats, *nrows, *ncols]
}
}
}
/// Checks if the current `GenerationShape` is strictly a scalar variant.
pub fn is_scalar(&self) -> bool {
matches!(self, GenerationShape::Scalar)
}
/// Checks if the current `GenerationShape` is strictly a vector variant.
pub fn is_vector(&self) -> bool {
matches!(self, GenerationShape::Vector(_))
}
/// Checks if the current `GenerationShape` is strictly a matrix variant.
pub fn is_matrix(&self) -> bool {
matches!(self, GenerationShape::Matrix(_, _))
}
/// Checks if the current `GenerationShape` is strictly a tensor3D variant.
pub fn is_tensor3d(&self) -> bool {
matches!(self, GenerationShape::Tensor3D(_, _, _))
}
/// Checks if the current `GenerationShape` is strictly a tensor4D variant.
pub fn is_tensor4d(&self) -> bool {
matches!(self, GenerationShape::Tensor4D(_, _, _, _))
}
/// Check if there is only one element.
///
/// # Returns
/// `true` if the shape can be treated as a scalar, `false` otherwise.
///
/// # Examples
/// ```
/// use qudit_expr::GenerationShape;
///
/// let test_scalar = GenerationShape::Scalar;
/// let test_vector = GenerationShape::Vector(1);
/// let test_matrix = GenerationShape::Matrix(1, 1);
/// let test_tensor3d = GenerationShape::Tensor3D(1, 1, 1);
///
/// let test_vector_2 = GenerationShape::Vector(9);
/// let test_matrix_2 = GenerationShape::Matrix(9, 9);
/// let test_tensor3d_2 = GenerationShape::Tensor3D(1, 9, 9);
///
/// assert!(test_scalar.is_0d());
/// assert!(test_vector.is_0d());
/// assert!(test_matrix.is_0d());
/// assert!(test_tensor3d.is_0d());
///
/// assert_eq!(test_vector_2.is_0d(), false);
/// assert_eq!(test_matrix_2.is_0d(), false);
/// assert_eq!(test_tensor3d_2.is_0d(), false);
/// ```
pub fn is_0d(&self) -> bool {
self.num_elements() == 1
}
/// Check if the shape can be conceptually treated as a 1d tensor.
///
/// # Returns
/// `true` if the shape has exactly one dimension with 1 or more elements.
///
/// # Examples
/// ```
/// use qudit_expr::GenerationShape;
///
/// let test_vector = GenerationShape::Vector(9);
/// let test_matrix = GenerationShape::Matrix(1, 9);
/// let test_tensor3d = GenerationShape::Tensor3D(1, 1, 9);
///
/// let test_scalar = GenerationShape::Scalar;
/// let test_matrix_2 = GenerationShape::Matrix(9, 9);
/// let test_tensor3d_2 = GenerationShape::Tensor3D(1, 9, 9);
///
/// assert!(test_vector.is_1d());
/// assert!(test_matrix.is_1d());
/// assert!(test_tensor3d.is_1d());
///
/// assert_eq!(test_scalar.is_1d(), false);
/// assert_eq!(test_matrix_2.is_1d(), false);
/// assert_eq!(test_tensor3d_2.is_1d(), false);
/// ```
pub fn is_1d(&self) -> bool {
match self {
GenerationShape::Scalar => false,
GenerationShape::Vector(_) => true,
GenerationShape::Matrix(nrows, ncols) => *nrows == 1 || *ncols == 1,
GenerationShape::Tensor3D(nmats, nrows, ncols) => {
let non_one_count = [*nmats, *nrows, *ncols].iter().filter(|&&d| d > 1).count();
non_one_count == 1
}
GenerationShape::Tensor4D(ntens, nmats, nrows, ncols) => {
let non_one_count = [*ntens, *nmats, *nrows, *ncols]
.iter()
.filter(|&&d| d > 1)
.count();
non_one_count == 1
}
}
}
/// Checks if the current `GenerationShape` can be conceptually treated as a 2-dimensional matrix.
/// This is true for `GenerationShape` variants with a dimensionality of at least 2, with
/// any additional dimensions having size 1.
///
/// # Returns
/// `true` if the shape can be treated as a matrix, `false` otherwise.
///
/// # Examples
/// ```
/// use qudit_expr::GenerationShape;
///
/// let test_scalar = GenerationShape::Scalar;
/// let test_vector = GenerationShape::Vector(1);
/// let test_tensor3d_2 = GenerationShape::Tensor3D(9, 9, 9);
/// let test_tensor_nd_2 = GenerationShape::Tensor4D(1, 9, 9, 9);
///
/// let test_matrix = GenerationShape::Matrix(9, 9);
/// let test_tensor3d = GenerationShape::Tensor3D(1, 9, 9);
/// let test_tensor_nd = GenerationShape::Tensor4D(1, 1, 9, 9);
///
/// assert_eq!(test_scalar.is_2d(), false);
/// assert_eq!(test_vector.is_2d(), false);
/// assert_eq!(test_tensor3d_2.is_2d(), false);
/// assert_eq!(test_tensor_nd_2.is_2d(), false);
///
/// assert_eq!(test_matrix.is_2d(), true);
/// assert_eq!(test_tensor3d.is_2d(), true);
/// assert_eq!(test_tensor_nd.is_2d(), true);
/// ```
pub fn is_2d(&self) -> bool {
match self {
GenerationShape::Scalar => false,
GenerationShape::Vector(_) => false,
GenerationShape::Matrix(_, _) => true,
// A Tensor3D can be seen as a matrix if it's essentially a stack of column vectors,
// or perhaps if it represents a single matrix (nmats=1).
// The current implementation checks if ncols is 1, implying it's a stack of column vectors.
GenerationShape::Tensor3D(nmats, _, _) => *nmats == 1,
GenerationShape::Tensor4D(ntens, nmats, _, _) => *ntens == 1 && *nmats == 1,
}
}
/// Checks if the current `GenerationShape` can be conceptually treated as a 3D tensor.
/// This is true for `GenerationShape` variants with a dimensionality of at least 3, with
/// any additional dimensions having size 1.
///
/// # Returns
/// `true` if the shape can be treated as a 3D tensor, `false` otherwise.
///
/// # Examples
/// ```
/// use qudit_expr::GenerationShape;
///
/// let test_scalar = GenerationShape::Scalar;
/// let test_vector = GenerationShape::Vector(1);
/// let test_matrix = GenerationShape::Matrix(1, 1);
/// let test_tensor_nd_2 = GenerationShape::Tensor4D(9, 1, 9, 9);
///
/// let test_tensor3d = GenerationShape::Tensor3D(9, 9, 9);
/// let test_tensor_nd = GenerationShape::Tensor4D(1, 9, 9, 9);
///
/// assert_eq!(test_scalar.is_3d(), false);
/// assert_eq!(test_vector.is_3d(), false);
/// assert_eq!(test_matrix.is_3d(), false);
/// assert_eq!(test_tensor_nd_2.is_3d(), false);
///
/// assert_eq!(test_tensor3d.is_3d(), true);
/// assert_eq!(test_tensor_nd.is_3d(), true);
/// ```
pub fn is_3d(&self) -> bool {
match self {
GenerationShape::Scalar => false,
GenerationShape::Vector(_) => false,
GenerationShape::Matrix(_, _) => false,
GenerationShape::Tensor3D(_, _, _) => true,
GenerationShape::Tensor4D(ntens, _, _, _) => *ntens == 1,
}
}
/// Checks if the current `GenerationShape` can be conceptually treated as a 4D tensor.
/// This is true for `GenerationShape` variants with a dimensionality of at least 4, with
/// any additional dimensions having size 1.
///
/// # Returns
/// `true` if the shape can be treated as a 4D tensor, `false` otherwise.
///
/// # Examples
/// ```
/// use qudit_expr::GenerationShape;
///
/// let test_scalar = GenerationShape::Scalar;
/// let test_vector = GenerationShape::Vector(1);
/// let test_matrix = GenerationShape::Matrix(1, 1);
/// let test_tensor3d = GenerationShape::Tensor3D(1, 1, 1);
///
/// let test_tensor4d = GenerationShape::Tensor4D(9, 9, 9, 9);
/// let test_tensor_nd = GenerationShape::Tensor4D(1, 9, 9, 9);
///
/// assert_eq!(test_scalar.is_4d(), false);
/// assert_eq!(test_vector.is_4d(), false);
/// assert_eq!(test_matrix.is_4d(), false);
/// assert_eq!(test_tensor3d.is_4d(), false);
///
/// assert_eq!(test_tensor4d.is_4d(), true);
/// assert_eq!(test_tensor_nd.is_4d(), true);
/// ```
pub fn is_4d(&self) -> bool {
match self {
GenerationShape::Scalar => false,
GenerationShape::Vector(_) => false,
GenerationShape::Matrix(_, _) => false,
GenerationShape::Tensor3D(_, _, _) => false,
GenerationShape::Tensor4D(_, _, _, _) => true,
}
}
/// Returns the number of columns for the current shape.
///
/// # Returns
/// The number of columns.
///
/// # Examples
/// ```
/// use qudit_expr::GenerationShape;
/// let matrix_shape = GenerationShape::Matrix(2, 3);
/// assert_eq!(matrix_shape.ncols(), 3);
/// ```
pub fn ncols(&self) -> usize {
match self {
GenerationShape::Scalar => 1,
GenerationShape::Vector(ncols) => *ncols,
GenerationShape::Matrix(_, ncols) => *ncols,
GenerationShape::Tensor3D(_, _, ncols) => *ncols,
GenerationShape::Tensor4D(_, _, _, ncols) => *ncols,
}
}
/// Returns the number of rows for the current shape.
///
/// # Returns
/// The number of rows.
///
/// # Examples
/// ```
/// use qudit_expr::GenerationShape;
/// let matrix_shape = GenerationShape::Matrix(2, 3);
/// assert_eq!(matrix_shape.nrows(), 2);
/// ```
pub fn nrows(&self) -> usize {
match self {
GenerationShape::Scalar => 1,
GenerationShape::Vector(_) => 1,
GenerationShape::Matrix(nrows, _) => *nrows,
GenerationShape::Tensor3D(_, nrows, _) => *nrows,
GenerationShape::Tensor4D(_, _, nrows, _) => *nrows,
}
}
/// Returns the number of matrices for the current shape.
///
/// # Returns
/// The number of matrices.
///
/// # Examples
/// ```
/// use qudit_expr::GenerationShape;
/// let tensor3d_shape = GenerationShape::Tensor3D(5, 2, 3);
/// assert_eq!(tensor3d_shape.nmats(), 5);
/// ```
pub fn nmats(&self) -> usize {
match self {
GenerationShape::Scalar => 1,
GenerationShape::Vector(_) => 1,
GenerationShape::Matrix(_, _) => 1,
GenerationShape::Tensor3D(nmats, _, _) => *nmats,
GenerationShape::Tensor4D(_, nmats, _, _) => *nmats,
}
}
/// Returns the number of tensors (in the first dimension) for the current shape.
///
/// # Returns
/// The number of tensors.
///
/// # Examples
/// ```
/// use qudit_expr::GenerationShape;
/// let tensor4d_shape = GenerationShape::Tensor4D(7, 5, 2, 3);
/// assert_eq!(tensor4d_shape.ntens(), 7);
/// ```
pub fn ntens(&self) -> usize {
match self {
GenerationShape::Scalar => 1,
GenerationShape::Vector(_) => 1,
GenerationShape::Matrix(_, _) => 1,
GenerationShape::Tensor3D(_, _, _) => 1,
GenerationShape::Tensor4D(ntens, _, _, _) => *ntens,
}
}
pub fn calculate_directions(&self, index_sizes: &[usize]) -> Vec<IndexDirection> {
match self {
GenerationShape::Scalar => vec![],
GenerationShape::Vector(_) => vec![IndexDirection::Input; index_sizes.len()],
GenerationShape::Matrix(nrows, _) => {
let mut index_size_acm = 1usize;
let mut index_iter = 0;
let mut index_directions = vec![];
while index_iter < index_sizes.len() && index_size_acm < *nrows {
index_size_acm *= index_sizes[index_iter];
index_directions.push(IndexDirection::Output);
index_iter += 1;
}
while index_iter < index_sizes.len() {
index_directions.push(IndexDirection::Input);
index_iter += 1;
}
index_directions
}
GenerationShape::Tensor3D(nmats, nrows, _) => {
let mut index_size_acm = 1usize;
let mut index_iter = 0;
let mut index_directions = vec![];
while index_iter < index_sizes.len() && index_size_acm < *nmats {
index_size_acm *= index_sizes[index_iter];
index_directions.push(IndexDirection::Batch);
index_iter += 1;
}
index_size_acm = 1usize; // Reset to calculate for nrows
while index_iter < index_sizes.len() && index_size_acm < *nrows {
index_size_acm *= index_sizes[index_iter];
index_directions.push(IndexDirection::Output);
index_iter += 1;
}
while index_iter < index_sizes.len() {
index_directions.push(IndexDirection::Input);
index_iter += 1;
}
index_directions
}
GenerationShape::Tensor4D(_, _, _, _) => {
todo!()
}
}
}
}
// impl From<Vec<TensorIndex>> for GenerationShape {
// fn from(indices: Vec<TensorIndex>) -> Self {
// GenerationShape::from(indices.as_slice())
// }
// }
impl<I: AsRef<[TensorIndex]>> From<I> for GenerationShape {
fn from(indices: I) -> Self {
let indices = indices.as_ref();
let mut dimensions = [1, 1, 1, 1];
for index in indices.iter() {
match index.direction() {
IndexDirection::Derivative => dimensions[0] *= index.index_size(),
IndexDirection::Batch => dimensions[1] *= index.index_size(),
IndexDirection::Output => dimensions[2] *= index.index_size(),
IndexDirection::Input => dimensions[3] *= index.index_size(),
}
}
match dimensions {
[1, 1, 1, 1] => GenerationShape::Scalar,
[1, 1, 1, nelems] => GenerationShape::Vector(nelems),
[1, 1, nrows, ncols] => GenerationShape::Matrix(nrows, ncols),
[1, nmats, nrows, ncols] => GenerationShape::Tensor3D(nmats, nrows, ncols),
[ntens, nmats, nrows, ncols] => GenerationShape::Tensor4D(ntens, nmats, nrows, ncols),
}
}
}
impl Add for GenerationShape {
type Output = Self;
fn add(self, other: Self) -> Self::Output {
match (self, other) {
// TODO: re-evaluate this...
(GenerationShape::Scalar, other_shape) => other_shape,
(other_shape, GenerationShape::Scalar) => other_shape,
(GenerationShape::Vector(s_nelems), GenerationShape::Vector(o_nelems)) => {
GenerationShape::Vector(s_nelems + o_nelems)
}
(
GenerationShape::Matrix(s_nrows, s_ncols),
GenerationShape::Matrix(o_nrows, o_ncols),
) => GenerationShape::Matrix(s_nrows + o_nrows, s_ncols + o_ncols),
(
GenerationShape::Tensor3D(s_nmats, s_nrows, s_ncols),
GenerationShape::Tensor3D(o_nmats, o_nrows, o_ncols),
) => GenerationShape::Tensor3D(s_nmats + o_nmats, s_nrows + o_nrows, s_ncols + o_ncols),
(
GenerationShape::Tensor4D(s_ntens, s_nmats, s_nrows, s_ncols),
GenerationShape::Tensor4D(o_ntens, o_nmats, o_nrows, o_ncols),
) => GenerationShape::Tensor4D(
s_ntens + o_ntens,
s_nmats + o_nmats,
s_nrows + o_nrows,
s_ncols + o_ncols,
),
_ => {
panic!("Cannot add tensors of different fundamental shapes or incompatible ranks.")
}
}
}
}