Sequential Quantum Gate Decomposer  v1.9.6
Powerful decomposition of general unitarias into one- and two-qubit gates gates
matrix_float_test.cpp
Go to the documentation of this file.
1 #include "matrix_float.h"
2 #include "matrix.h"
3 #include "QGDTypes.h"
4 #include <cassert>
5 #include <cmath>
6 #include <iostream>
7 
8 int main() {
9  // Basic instantiation
10  Matrix_float m(100, 100);
11  assert(m.get_data() != nullptr);
12 
13  // Memory size check
14  assert(sizeof(QGD_Complex8) == sizeof(QGD_Complex16) / 2);
15 
16  // Copy constructor and reference counting
17  Matrix_float m2(m);
18  assert(m2.get_data() == m.get_data());
19 
20  // Element access
21  m[0].real = 1.0f;
22  m[0].imag = 2.0f;
23  assert(m[0].real == 1.0f);
24 
25  // NaN propagation (requires IEEE 754 compliance, no -ffast-math)
26  m[1].real = NAN;
27  assert(m.isnan());
28 
29  // Conversion roundtrip
30  Matrix md(2, 2);
31  md[0].real = 3.14159265358979;
32  md[0].imag = 2.71828182845905;
33  Matrix_float mf = md.to_float32();
34  Matrix md2 = mf.to_float64();
35  assert(std::abs((md[0].real - md2[0].real) / md[0].real) < 2e-6);
36  assert(std::abs((md[0].imag - md2[0].imag) / md[0].imag) < 2e-6);
37 
38  // Overflow handling
39  Matrix large(1, 1);
40  large[0].real = 1e308;
41  Matrix_float overflow = large.to_float32();
42  assert(std::isinf(overflow[0].real));
43 
44  // Copy semantics
45  Matrix_float m3(mf);
46  assert(m3.get_data() != nullptr);
47 
48  // Alignment check
49  assert(reinterpret_cast<uintptr_t>(m.get_data()) % CACHELINE == 0);
50  m.ensure_aligned();
51  assert(reinterpret_cast<uintptr_t>(m.get_data()) % CACHELINE == 0);
52 
53  // Edge cases
54  Matrix_float zero_mat(0, 0);
55  assert(zero_mat.size() == 0);
56 
57  Matrix_float single(1, 1);
58  single[0].real = 42.0f;
59  assert(single[0].real == 42.0f);
60 
61  Matrix_float rect1(10, 100);
62  Matrix_float rect2(100, 10);
63  assert(rect1.size() == rect2.size());
64 
65  std::cout << "All tests passed!" << std::endl;
66  return 0;
67 }
Header file of single-precision complex array storage with automatic and thread safe reference counti...
Matrix_float to_float32() const
Convert to single precision.
Definition: matrix.cpp:32
Matrix to_float64() const
Convert to double precision.
Definition: matrix_float.cpp:8
Structure type representing single-precision complex numbers.
Definition: QGDTypes.h:46
int main()
void ensure_aligned()
scalar * get_data() const
Call to get the pointer to the stored data.
#define CACHELINE
Definition: QGDTypes.h:33
Custom types for the SQUANDER package.
Header file of complex array storage array with automatic and thread safe reference counting...
Structure type representing complex numbers in the SQUANDER package.
Definition: QGDTypes.h:38
Double-precision complex matrix (float64).
Definition: matrix.h:38
int size() const
Call to get the number of the allocated elements.
Single-precision complex matrix (float32).
Definition: matrix_float.h:41