Sequential Quantum Gate Decomposer  v1.9.6
Powerful decomposition of general unitarias into one- and two-qubit gates gates
matrix_test.cpp
Go to the documentation of this file.
1 #include "matrix.h"
2 #include "QGDTypes.h"
3 #include <cassert>
4 #include <cmath>
5 #include <iostream>
6 
7 int main() {
8  // Basic instantiation
9  Matrix m1(3, 3);
10  assert(m1.get_data() != nullptr);
11 
12  // Size check
13  assert(sizeof(Matrix) == CACHELINE);
14 
15  // Initialize data
16  for (int i = 0; i < 3; i++) {
17  for (int j = 0; j < 3; j++) {
18  int idx = i * m1.stride + j;
19  m1.data[idx].real = i + j;
20  m1.data[idx].imag = i - j;
21  }
22  }
23 
24  // Copy constructor (shallow copy - reference counted)
25  Matrix m2(m1);
26  assert(m2.get_data() == m1.get_data());
27 
28  // Deep copy
29  Matrix m3 = m1.copy();
30  assert(m3.get_data() != m1.get_data());
31  assert(m3.rows == m1.rows && m3.cols == m1.cols);
32 
33  // Verify data is copied
34  for (int i = 0; i < m1.rows * m1.cols; i++) {
35  assert(m3.data[i].real == m1.data[i].real);
36  assert(m3.data[i].imag == m1.data[i].imag);
37  }
38 
39  // Move constructor
40  Matrix m4(std::move(m3));
41  assert(m4.rows == m1.rows && m4.cols == m1.cols);
42 
43  // Assignment operator (reference counted)
44  Matrix m5(2, 2);
45  m5 = m1;
46  assert(m5.get_data() == m1.get_data());
47 
48  // NaN detection
49  Matrix m6(2, 2);
50  m6[0].real = NAN;
51  assert(m6.isnan());
52 
53  // Alignment check
54  assert(reinterpret_cast<uintptr_t>(m1.get_data()) % CACHELINE == 0);
55  m1.ensure_aligned();
56  assert(reinterpret_cast<uintptr_t>(m1.get_data()) % CACHELINE == 0);
57 
58  // Edge cases
59  Matrix zero_mat(0, 0);
60  assert(zero_mat.size() == 0);
61 
62  Matrix single(1, 1);
63  single[0].real = 42.0;
64  assert(single[0].real == 42.0);
65 
66  Matrix rect1(10, 100);
67  Matrix rect2(100, 10);
68  assert(rect1.size() == rect2.size());
69 
70  std::cout << "All tests passed!" << std::endl;
71  return 0;
72 }
int stride
The column stride of the array. (The array elements in one row are a_0, a_1, ... a_{cols-1}, 0, 0, 0, 0. The number of zeros is stride-cols)
Definition: matrix_base.hpp:46
scalar * data
pointer to the stored data
Definition: matrix_base.hpp:48
void ensure_aligned()
scalar * get_data() const
Call to get the pointer to the stored data.
#define CACHELINE
Definition: QGDTypes.h:33
int rows
The number of rows.
Definition: matrix_base.hpp:42
int cols
The number of columns.
Definition: matrix_base.hpp:44
Custom types for the SQUANDER package.
Header file of complex array storage array with automatic and thread safe reference counting...
Matrix copy() const
Call to create a copy of the matrix.
Definition: matrix.h:57
Double-precision complex matrix (float64).
Definition: matrix.h:38
int size() const
Call to get the number of the allocated elements.
double real
the real part of a complex number
Definition: QGDTypes.h:40
int main()
Definition: matrix_test.cpp:7
double imag
the imaginary part of a complex number
Definition: QGDTypes.h:42