Qrack  9.0
General classical-emulating-quantum development framework
qrack_types.hpp
Go to the documentation of this file.
1 //
3 // (C) Daniel Strano and the Qrack contributors 2017-2023. All rights reserved.
4 //
5 // This is a multithreaded, universal quantum register simulation, allowing
6 // (nonphysical) register cloning and direct measurement of probability and
7 // phase, to leverage what advantages classical emulation of qubits can have.
8 //
9 // Licensed under the GNU Lesser General Public License V3.
10 // See LICENSE.md in the project root or https://www.gnu.org/licenses/lgpl-3.0.en.html
11 // for details.
12 
13 #pragma once
14 
15 #include "config.h"
16 
17 #define _USE_MATH_DEFINES
18 
19 #include <cmath>
20 #include <complex>
21 #include <cstdint>
22 #include <functional>
23 #include <limits>
24 #include <math.h>
25 #include <memory>
26 
27 #define IS_NORM_0(c) (norm(c) <= FP_NORM_EPSILON)
28 #define IS_SAME(c1, c2) (IS_NORM_0((c1) - (c2)))
29 #define IS_OPPOSITE(c1, c2) (IS_NORM_0((c1) + (c2)))
30 
31 #if QBCAPPOW > 6 && defined(BOOST_AVAILABLE) || QBCAPPOW > 7
32 #include <boost/multiprecision/cpp_int.hpp>
33 #endif
34 
35 #if ENABLE_CUDA
36 #include <cuda_runtime.h>
37 #endif
38 
39 #if (FPPOW < 5) && !defined(__arm__)
40 #include "half.hpp"
41 #endif
42 
43 #if QBCAPPOW < 8
44 #define bitLenInt uint8_t
45 #elif QBCAPPOW < 16
46 #define bitLenInt uint16_t
47 #elif QBCAPPOW < 32
48 #define bitLenInt uint32_t
49 #else
50 #define bitLenInt uint64_t
51 #endif
52 
53 #if FPPOW < 5
54 namespace Qrack {
55 #ifdef __arm__
56 typedef std::complex<__fp16> complex;
57 typedef __fp16 real1;
58 typedef float real1_f;
59 typedef float real1_s;
60 #else
61 #include "half.hpp"
62 typedef std::complex<half_float::half> complex;
64 typedef float real1_f;
65 typedef float real1_s;
66 #endif
67 #elif FPPOW < 6
68 namespace Qrack {
69 typedef std::complex<float> complex;
70 typedef float real1;
71 typedef float real1_f;
72 typedef float real1_s;
73 #elif FPPOW < 7
74 namespace Qrack {
75 typedef std::complex<double> complex;
76 typedef double real1;
77 typedef double real1_f;
78 typedef double real1_s;
79 #else
80 #include <boost/multiprecision/float128.hpp>
81 #include <quadmath.h>
82 namespace Qrack {
83 typedef std::complex<boost::multiprecision::float128> complex;
84 typedef boost::multiprecision::float128 real1;
85 typedef boost::multiprecision::float128 real1_f;
86 typedef double real1_s;
87 #endif
88 
89 #if UINTPOW < 4
90 constexpr uint8_t ONE_BCI = 1U;
91 #define bitCapIntOcl uint8_t
92 #elif UINTPOW < 5
93 constexpr uint16_t ONE_BCI = 1U;
94 #define bitCapIntOcl uint16_t
95 #elif UINTPOW < 6
96 #define ONE_BCI 1U
97 #define bitCapIntOcl uint32_t
98 #else
99 #define ONE_BCI 1UL
100 #define bitCapIntOcl uint64_t
101 #endif
102 
103 #if QBCAPPOW < 6
104 #define bitsInCap 32
105 #define bitCapInt uint32_t
106 #elif QBCAPPOW < 7
107 #define bitsInCap 64
108 #define bitCapInt uint64_t
109 #elif QBCAPPOW < 8
110 #define bitsInCap 128
111 #ifdef BOOST_AVAILABLE
112 #define bitCapInt boost::multiprecision::uint128_t
113 #else
114 #define bitCapInt __uint128_t
115 #endif
116 #else
117 constexpr bitLenInt bitsInCap = ((bitLenInt)1U) << (QBCAPPOW + 3U);
118 #define bitCapInt \
119  boost::multiprecision::number<boost::multiprecision::cpp_int_backend<1ULL << QBCAPPOW, 1ULL << QBCAPPOW, \
120  boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>
121 #endif
122 
123 typedef std::shared_ptr<complex> BitOp;
124 
125 // Called once per value between begin and end.
126 typedef std::function<void(const bitCapIntOcl&, const unsigned& cpu)> ParallelFunc;
127 typedef std::function<bitCapIntOcl(const bitCapIntOcl&)> IncrementFunc;
128 typedef std::function<bitCapInt(const bitCapInt&)> BdtFunc;
129 typedef std::function<void(const bitCapInt&, const unsigned& cpu)> ParallelFuncBdt;
130 
131 class StateVector;
132 class StateVectorArray;
133 class StateVectorSparse;
134 
135 typedef std::shared_ptr<StateVector> StateVectorPtr;
136 typedef std::shared_ptr<StateVectorArray> StateVectorArrayPtr;
137 typedef std::shared_ptr<StateVectorSparse> StateVectorSparsePtr;
138 
139 typedef std::function<void(void)> DispatchFn;
140 
141 class QEngine;
142 typedef std::shared_ptr<QEngine> QEnginePtr;
143 
144 #define bitsInByte 8U
145 #define qrack_rand_gen std::mt19937_64
146 #define qrack_rand_gen_ptr std::shared_ptr<qrack_rand_gen>
147 #define QRACK_ALIGN_SIZE 64U
148 
149 #if FPPOW < 5
150 #define QRACK_CONST const
151 const real1 ZERO_R1 = (real1)0.0f;
152 constexpr real1_f ZERO_R1_F = 0.0f;
153 const real1 ONE_R1 = (real1)1.0f;
154 constexpr real1_f ONE_R1_F = 1.0f;
155 const real1 REAL1_DEFAULT_ARG = (real1)-999.0f;
156 // Half of the probability of 16 maximally superposed qubits in any permutation
157 const real1 REAL1_EPSILON = (real1)0.00000762939f;
158 const real1 PI_R1 = (real1)M_PI;
159 const real1 SQRT2_R1 = (real1)M_SQRT2;
160 const real1 SQRT1_2_R1 = (real1)M_SQRT1_2;
161 #elif FPPOW < 6
162 #define QRACK_CONST constexpr
163 #define ZERO_R1 0.0f
164 #define ZERO_R1_F 0.0f
165 #define ONE_R1 1.0f
166 #define ONE_R1_F 1.0f
167 constexpr real1 PI_R1 = (real1)M_PI;
168 constexpr real1 SQRT2_R1 = (real1)M_SQRT2;
169 constexpr real1 SQRT1_2_R1 = (real1)M_SQRT1_2;
170 #define REAL1_DEFAULT_ARG -999.0f
171 // Half of the probability of 32 maximally superposed qubits in any permutation
172 #define REAL1_EPSILON 1.1641532e-10
173 #elif FPPOW < 7
174 #define QRACK_CONST constexpr
175 #define ZERO_R1 0.0
176 #define ZERO_R1_F 0.0
177 #define ONE_R1 1.0
178 #define ONE_R1_F 1.0
179 #define PI_R1 M_PI
180 #define SQRT2_R1 M_SQRT2
181 #define SQRT1_2_R1 M_SQRT1_2
182 #define REAL1_DEFAULT_ARG -999.0
183 // Half of the probability of 64 maximally superposed qubits in any permutation
184 #define REAL1_EPSILON 2.7105054e-20
185 #else
186 #define QRACK_CONST constexpr
187 constexpr real1 ZERO_R1 = (real1)0.0;
188 #define ZERO_R1_F 0.0
189 constexpr real1 ONE_R1 = (real1)1.0;
190 #define ONE_R1_F 1.0
191 constexpr real1_f PI_R1 = (real1_f)M_PI;
192 constexpr real1_f SQRT2_R1 = (real1_f)M_SQRT2;
193 constexpr real1_f SQRT1_2_R1 = (real1_f)M_SQRT1_2;
194 #define REAL1_DEFAULT_ARG -999.0
195 // Half of the probability of 64 maximally superposed qubits in any permutation
196 #define REAL1_EPSILON 1.4693679e-39
197 #endif
198 
199 #if ENABLE_CUDA
200 #if FPPOW < 5
201 #include <cuda_fp16.h>
202 #define qCudaReal1 __half
203 #define qCudaReal2 __half2
204 #define qCudaReal4 __half2*
205 #define qCudaCmplx __half2
206 #define qCudaCmplx2 __half2*
207 #define qCudaReal1_f float
208 #define make_qCudaCmplx make_half2
209 #define ZERO_R1_CUDA ((qCudaReal1)0.0f)
210 #define REAL1_EPSILON_CUDA 0.00000762939f
211 #define PI_R1_CUDA M_PI
212 #elif FPPOW < 6
213 #define qCudaReal1 float
214 #define qCudaReal2 float2
215 #define qCudaReal4 float4
216 #define qCudaCmplx float2
217 #define qCudaCmplx2 float4
218 #define qCudaReal1_f float
219 #define make_qCudaCmplx make_float2
220 #define make_qCudaCmplx2 make_float4
221 #define ZERO_R1_CUDA 0.0f
222 #define REAL1_EPSILON_CUDA REAL1_EPSILON
223 #define PI_R1_CUDA PI_R1
224 #else
225 #define qCudaReal1 double
226 #define qCudaReal2 double2
227 #define qCudaReal4 double4
228 #define qCudaCmplx double2
229 #define qCudaCmplx2 double4
230 #define qCudaReal1_f double
231 #define make_qCudaCmplx make_double2
232 #define make_qCudaCmplx2 make_double4
233 #define ZERO_R1_CUDA 0.0
234 #define REAL1_EPSILON_CUDA REAL1_EPSILON
235 #define PI_R1_CUDA PI_R1
236 #endif
237 #endif
238 
243 QRACK_CONST real1 FP_NORM_EPSILON = std::numeric_limits<real1>::epsilon();
245 constexpr real1_f FP_NORM_EPSILON_F = std::numeric_limits<real1_f>::epsilon();
246 } // namespace Qrack
Abstract QEngine implementation, for all "Schroedinger method" engines.
Definition: qengine.hpp:31
Definition: statevector.hpp:83
Definition: statevector.hpp:211
Definition: statevector.hpp:45
Half-precision floating-point type.
Definition: half.hpp:2222
Main header file for half-precision functionality.
Definition: complex16x2simd.hpp:25
constexpr uint8_t ONE_BCI
Definition: qrack_types.hpp:90
std::complex< half_float::half > complex
Definition: qrack_types.hpp:62
std::shared_ptr< QEngine > QEnginePtr
Definition: qrack_types.hpp:141
QRACK_CONST real1_f TRYDECOMPOSE_EPSILON
Definition: qrack_types.hpp:244
constexpr real1_f ZERO_R1_F
Definition: qrack_types.hpp:152
constexpr real1_f FP_NORM_EPSILON_F
Definition: qrack_types.hpp:245
std::shared_ptr< complex > BitOp
Definition: qrack_types.hpp:123
const real1 ONE_R1
Definition: qrack_types.hpp:153
std::function< bitCapIntOcl(const bitCapIntOcl &)> IncrementFunc
Definition: qrack_types.hpp:127
constexpr real1_f ONE_R1_F
Definition: qrack_types.hpp:154
std::function< bitCapInt(const bitCapInt &)> BdtFunc
Definition: qrack_types.hpp:128
std::function< void(void)> DispatchFn
Definition: dispatchqueue.hpp:31
half_float::half real1
Definition: qrack_types.hpp:63
QRACK_CONST real1 FP_NORM_EPSILON
Definition: qrack_types.hpp:243
const real1 SQRT2_R1
Definition: qrack_types.hpp:159
std::shared_ptr< StateVectorSparse > StateVectorSparsePtr
Definition: qrack_types.hpp:137
std::function< void(const bitCapInt &, const unsigned &cpu)> ParallelFuncBdt
Definition: qrack_types.hpp:129
const real1 REAL1_DEFAULT_ARG
Definition: qrack_types.hpp:155
const real1 PI_R1
Definition: qrack_types.hpp:158
QRACK_CONST complex ONE_CMPLX
Definition: qrack_types.hpp:239
const real1 ZERO_R1
Definition: qrack_types.hpp:151
float real1_f
Definition: qrack_types.hpp:64
std::shared_ptr< StateVectorArray > StateVectorArrayPtr
Definition: qrack_types.hpp:136
float real1_s
Definition: qrack_types.hpp:65
QRACK_CONST complex CMPLX_DEFAULT_ARG
Definition: qrack_types.hpp:242
std::function< void(const bitCapIntOcl &, const unsigned &cpu)> ParallelFunc
Definition: qrack_types.hpp:126
QRACK_CONST complex I_CMPLX
Definition: qrack_types.hpp:241
std::shared_ptr< StateVector > StateVectorPtr
Definition: qrack_types.hpp:133
const real1 REAL1_EPSILON
Definition: qrack_types.hpp:157
QRACK_CONST complex ZERO_CMPLX
Definition: qrack_types.hpp:240
const real1 SQRT1_2_R1
Definition: qrack_types.hpp:160
#define QRACK_CONST
Definition: qrack_types.hpp:150
#define bitLenInt
Definition: qrack_types.hpp:44
#define bitCapInt
Definition: qrack_types.hpp:105
#define bitCapIntOcl
Definition: qrack_types.hpp:91
#define bitsInCap
Definition: qrack_types.hpp:104