Sequential Quantum Gate Decomposer  v1.9.6
Powerful decomposition of general unitarias into one- and two-qubit gates gates
qgd_math.h
Go to the documentation of this file.
1 /*
2 Copyright 2026
3 
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 */
10 
11 #ifndef QGD_MATH_H
12 #define QGD_MATH_H
13 
14 #include <cstddef>
15 #include <cmath>
16 #if !defined(_WIN32)
17 #include <math.h>
18 #endif
19 
20 // Provide standard C math constants when the platform headers do not.
21 #ifndef M_E
22 #define M_E 2.71828182845904523536
23 #endif
24 
25 #ifndef M_LOG2E
26 #define M_LOG2E 1.44269504088896340736
27 #endif
28 
29 #ifndef M_LOG10E
30 #define M_LOG10E 0.434294481903251827651
31 #endif
32 
33 #ifndef M_LN2
34 #define M_LN2 0.693147180559945309417
35 #endif
36 
37 #ifndef M_LN10
38 #define M_LN10 2.30258509299404568402
39 #endif
40 
41 #ifndef M_PI
42 #define M_PI 3.14159265358979323846
43 #endif
44 
45 #ifndef M_PI_2
46 #define M_PI_2 1.57079632679489661923
47 #endif
48 
49 #ifndef M_PI_4
50 #define M_PI_4 0.785398163397448309616
51 #endif
52 
53 #ifndef M_1_PI
54 #define M_1_PI 0.318309886183790671538
55 #endif
56 
57 #ifndef M_2_PI
58 #define M_2_PI 0.636619772367581343076
59 #endif
60 
61 #ifndef M_2_SQRTPI
62 #define M_2_SQRTPI 1.12837916709551257390
63 #endif
64 
65 #ifndef M_SQRT2
66 #define M_SQRT2 1.41421356237309504880
67 #endif
68 
69 #ifndef M_SQRT1_2
70 #define M_SQRT1_2 0.707106781186547524401
71 #endif
72 
73 template <typename T>
74 inline T qgd_sin(T x);
75 
76 template <typename T>
77 inline T qgd_cos(T x);
78 
79 template <typename T>
80 inline void qgd_sincos(T x, T* s, T* c);
81 
82 template <typename T>
83 inline void qgd_sincos_batch(const T* input, T* output, std::size_t count, int stride);
84 
85 template <>
86 inline float qgd_sin<float>(float x) {
87  return std::sin(x);
88 }
89 
90 template <>
91 inline double qgd_sin<double>(double x) {
92  return std::sin(x);
93 }
94 
95 template <>
96 inline float qgd_cos<float>(float x) {
97  return std::cos(x);
98 }
99 
100 template <>
101 inline double qgd_cos<double>(double x) {
102  return std::cos(x);
103 }
104 
105 template <>
106 inline void qgd_sincos<float>(float x, float* s, float* c) {
107 #if defined(_WIN32)
108  *s = std::sin(x);
109  *c = std::cos(x);
110 #elif defined(__APPLE__)
111  ::__sincosf(x, s, c);
112 #else
113  ::sincosf(x, s, c);
114 #endif
115 }
116 
117 template <>
118 inline void qgd_sincos<double>(double x, double* s, double* c) {
119 #if defined(_WIN32)
120  *s = std::sin(x);
121  *c = std::cos(x);
122 #elif defined(__APPLE__)
123  ::__sincos(x, s, c);
124 #else
125  ::sincos(x, s, c);
126 #endif
127 }
128 
129 template <>
130 inline void qgd_sincos_batch<float>(const float* input, float* output, std::size_t count, int stride) {
131  for (std::size_t idx = 0; idx < count; ++idx) {
132  const int offset = static_cast<int>(idx) * stride;
133  qgd_sincos<float>(input[idx], &output[offset + 0], &output[offset + 1]);
134  }
135 }
136 
137 template <>
138 inline void qgd_sincos_batch<double>(const double* input, double* output, std::size_t count, int stride) {
139  for (std::size_t idx = 0; idx < count; ++idx) {
140  const int offset = static_cast<int>(idx) * stride;
141  qgd_sincos<double>(input[idx], &output[offset + 0], &output[offset + 1]);
142  }
143 }
144 
145 #endif
void qgd_sincos< float >(float x, float *s, float *c)
Definition: qgd_math.h:106
void qgd_sincos< double >(double x, double *s, double *c)
Definition: qgd_math.h:118
T qgd_cos(T x)
double qgd_sin< double >(double x)
Definition: qgd_math.h:91
float qgd_cos< float >(float x)
Definition: qgd_math.h:96
void qgd_sincos_batch(const T *input, T *output, std::size_t count, int stride)
void qgd_sincos_batch< double >(const double *input, double *output, std::size_t count, int stride)
Definition: qgd_math.h:138
float qgd_sin< float >(float x)
Definition: qgd_math.h:86
T qgd_sin(T x)
double qgd_cos< double >(double x)
Definition: qgd_math.h:101
Definition: T.h:11
void qgd_sincos_batch< float >(const float *input, float *output, std::size_t count, int stride)
Definition: qgd_math.h:130
void qgd_sincos(T x, T *s, T *c)