Qrack  9.13
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 #define _USE_MATH_DEFINES
16 
17 #include "config.h"
18 
19 #include <complex>
20 #include <functional>
21 #include <limits>
22 #include <math.h>
23 #include <memory>
24 
25 #define IS_NORM_0(c) (norm(c) <= FP_NORM_EPSILON)
26 #define IS_SAME(c1, c2) (IS_NORM_0((c1) - (c2)))
27 #define IS_OPPOSITE(c1, c2) (IS_NORM_0((c1) + (c2)))
28 
29 #if ENABLE_CUDA
30 #include <cuda_runtime.h>
31 #endif
32 
33 #if (FPPOW < 5) && !defined(__arm__)
34 #include "half.hpp"
35 #endif
36 
37 #if QBCAPPOW < 8
38 #define bitLenInt uint8_t
39 #elif QBCAPPOW < 16
40 #define bitLenInt uint16_t
41 #elif QBCAPPOW < 32
42 #define bitLenInt uint32_t
43 #elif (QBCAPPOW < 64) || !defined(__SIZEOF_INT128__)
44 #define bitLenInt uint64_t
45 #else
46 #define bitLenInt unsigned __int128
47 #endif
48 
49 #if UINTPOW < 4
50 #define bitCapIntOcl uint8_t
51 #elif UINTPOW < 5
52 #define bitCapIntOcl uint16_t
53 #elif UINTPOW < 6
54 #define bitCapIntOcl uint32_t
55 #elif (UINTPOW < 7) || !defined(__SIZEOF_INT128__)
56 #define bitCapIntOcl uint64_t
57 #else
58 #define bitCapIntOcl unsigned __int128
59 #endif
60 
61 #if QBCAPPOW < 6
62 #define bitCapInt uint32_t
63 #define QRACK_MAX_QUBITS 32
64 #elif QBCAPPOW < 7
65 #define bitCapInt uint64_t
66 #define QRACK_MAX_QUBITS 64
67 #elif (QBCAPPOW < 8) && defined(__SIZEOF_INT128__)
68 #define bitCapInt unsigned __int128
69 #define QRACK_MAX_QUBITS 128
70 #else
71 #include "big_integer.hpp"
72 #define bitCapInt BigInteger
73 constexpr size_t QRACK_MAX_QUBITS = (1 << QBCAPPOW);
74 #endif
75 
76 #if FPPOW < 5
77 #ifdef __arm__
78 namespace Qrack {
79 typedef __fp16 real1;
80 typedef float real1_f;
81 typedef float real1_s;
82 #else
83 #if (CPP_STD >= 23) && __has_include(<stdfloat>)
84 #include <stdfloat>
85 #endif
86 #if defined(__STDCPP_FLOAT16_T__)
87 namespace Qrack {
88 typedef float16_t real1;
89 typedef float real1_f;
90 typedef float real1_s;
91 #else
92 #include "half.hpp"
93 namespace Qrack {
95 typedef float real1_f;
96 typedef float real1_s;
97 #endif
98 #endif
99 #elif FPPOW < 6
100 namespace Qrack {
101 typedef float real1;
102 typedef float real1_f;
103 typedef float real1_s;
104 #elif FPPOW < 7
105 namespace Qrack {
106 typedef double real1;
107 typedef double real1_f;
108 typedef double real1_s;
109 #else
110 #if (CPP_STD >= 23) && __has_include(<stdfloat>)
111 #include <stdfloat>
112 #endif
113 #if defined(__STDCPP_FLOAT128_T__)
114 namespace Qrack {
115 typedef float128_t real1;
116 typedef float128_t real1_f;
117 typedef double real1_s;
118 #else
119 #include <boost/multiprecision/float128.hpp>
120 #include <quadmath.h>
121 namespace Qrack {
122 typedef boost::multiprecision::float128 real1;
123 typedef boost::multiprecision::float128 real1_f;
124 typedef double real1_s;
125 #endif
126 #endif
127 
128 typedef std::complex<real1> complex;
129 const bitCapInt ONE_BCI = 1U;
131 constexpr bitLenInt bitsInCap = ((bitLenInt)1U) << ((bitLenInt)QBCAPPOW);
132 
133 typedef std::shared_ptr<complex> BitOp;
134 
135 // Called once per value between begin and end.
136 typedef std::function<void(const bitCapIntOcl&, const unsigned& cpu)> ParallelFunc;
137 typedef std::function<bitCapIntOcl(const bitCapIntOcl&)> IncrementFunc;
138 typedef std::function<bitCapInt(const bitCapInt&)> BdtFunc;
139 typedef std::function<void(const bitCapInt&, const unsigned& cpu)> ParallelFuncBdt;
140 
141 class StateVector;
142 class StateVectorArray;
143 class StateVectorSparse;
144 
145 typedef std::shared_ptr<StateVector> StateVectorPtr;
146 typedef std::shared_ptr<StateVectorArray> StateVectorArrayPtr;
147 typedef std::shared_ptr<StateVectorSparse> StateVectorSparsePtr;
148 
149 typedef std::function<void(void)> DispatchFn;
150 
151 class QEngine;
152 typedef std::shared_ptr<QEngine> QEnginePtr;
153 
154 #define bitsInByte 8U
155 #define qrack_rand_gen std::mt19937_64
156 #define qrack_rand_gen_ptr std::shared_ptr<qrack_rand_gen>
157 #define QRACK_ALIGN_SIZE 64U
158 
159 #if FPPOW < 6
160 #define ZERO_R1_F 0.0f
161 #define QUARTER_R1_F 0.25f
162 #define HALF_R1_F 0.5f
163 #define ONE_R1_F 1.0f
164 #else
165 #define ZERO_R1_F 0.0
166 #define QUARTER_R1_F 0.25
167 #define HALF_R1_F 0.5
168 #define ONE_R1_F 1.0
169 #endif
170 
171 #if (FPPOW > 4) || defined(__arm__)
172 #define QRACK_CONST constexpr
173 #else
174 #define QRACK_CONST const
175 #endif
176 
177 #define REAL1_DEFAULT_ARG -999.0f
181 
182 #if (FPPOW < 5) || (FPPOW > 6)
186 #elif FPPOW == 5
187 #define ZERO_R1 0.0f
188 #define QUARTER_R1 0.25f
189 #define HALF_R1 0.5f
190 #define ONE_R1 1.0f
191 #else
192 #define ZERO_R1 0.0
193 #define QUARTER_R1 0.25
194 #define HALF_R1 0.5
195 #define ONE_R1 1.0
196 #endif
197 
198 #if FPPOW < 5
199 // Half the probability in any single permutation of 20 maximally superposed qubits
201 #elif FPPOW < 6
202 // Half the probability in any single permutation of 48 maximally superposed qubits
203 #define REAL1_EPSILON 1.7763568394002505e-15f
204 #elif FPPOW < 7
205 // Half the probability in any single permutation of 96 maximally superposed qubits
206 #define REAL1_EPSILON 6.310887241768095e-30
207 #else
208 // Half the probability in any single permutation of 192 maximally superposed qubits
209 QRACK_CONST real1 REAL1_EPSILON = (real1)7.965459555662261e-59;
210 #endif
211 
212 #if ENABLE_CUDA
213 #if FPPOW < 5
214 #include <cuda_fp16.h>
215 #define qCudaReal1 __half
216 #define qCudaReal2 __half2
217 #define qCudaReal4 __half2*
218 #define qCudaCmplx __half2
219 #define qCudaCmplx2 __half2*
220 #define qCudaReal1_f float
221 #define make_qCudaCmplx make_half2
222 #define ZERO_R1_CUDA ((qCudaReal1)0.0f)
223 #define REAL1_EPSILON_CUDA ((qCudaReal1)0.000000477f)
224 #define PI_R1_CUDA M_PI
225 #elif FPPOW < 6
226 #define qCudaReal1 float
227 #define qCudaReal2 float2
228 #define qCudaReal4 float4
229 #define qCudaCmplx float2
230 #define qCudaCmplx2 float4
231 #define qCudaReal1_f float
232 #define make_qCudaCmplx make_float2
233 #define make_qCudaCmplx2 make_float4
234 #define ZERO_R1_CUDA 0.0f
235 #define REAL1_EPSILON_CUDA REAL1_EPSILON
236 #define PI_R1_CUDA PI_R1
237 #else
238 #define qCudaReal1 double
239 #define qCudaReal2 double2
240 #define qCudaReal4 double4
241 #define qCudaCmplx double2
242 #define qCudaCmplx2 double4
243 #define qCudaReal1_f double
244 #define make_qCudaCmplx make_double2
245 #define make_qCudaCmplx2 make_double4
246 #define ZERO_R1_CUDA 0.0
247 #define REAL1_EPSILON_CUDA REAL1_EPSILON
248 #define PI_R1_CUDA PI_R1
249 #endif
250 #endif
251 
258 QRACK_CONST real1 FP_NORM_EPSILON = (real1)(std::numeric_limits<real1>::epsilon() / 4);
261 const double FIDELITY_MIN = log((double)FP_NORM_EPSILON);
262 } // namespace Qrack
Abstract QEngine implementation, for all "Schroedinger method" engines.
Definition: qengine.hpp:31
Definition: statevector.hpp:72
Definition: statevector.hpp:38
Half-precision floating-point type.
Definition: half.hpp:2222
Main header file for half-precision functionality.
GLOSSARY: bitLenInt - "bit-length integer" - unsigned integer ID of qubit position in register bitCap...
Definition: complex16x2simd.hpp:25
QRACK_CONST real1_f FP_NORM_EPSILON_F
Definition: qrack_types.hpp:259
QRACK_CONST real1 SQRT1_2_R1
Definition: qrack_types.hpp:180
std::shared_ptr< QEngine > QEnginePtr
Definition: qrack_types.hpp:151
QRACK_CONST real1 SQRT2_R1
Definition: qrack_types.hpp:179
QRACK_CONST real1_f TRYDECOMPOSE_EPSILON
Definition: qrack_types.hpp:260
QRACK_CONST real1 HALF_R1
Definition: qrack_types.hpp:184
std::shared_ptr< complex > BitOp
Definition: qrack_types.hpp:133
std::function< bitCapIntOcl(const bitCapIntOcl &)> IncrementFunc
Definition: qrack_types.hpp:137
std::function< bitCapInt(const bitCapInt &)> BdtFunc
Definition: qrack_types.hpp:138
void U(quid sid, bitLenInt q, real1_f theta, real1_f phi, real1_f lambda)
(External API) 3-parameter unitary gate
Definition: wasm_api.cpp:1143
std::function< void(void)> DispatchFn
Definition: dispatchqueue.hpp:31
half_float::half real1
Definition: qrack_types.hpp:94
std::complex< real1 > complex
Definition: qrack_types.hpp:128
QRACK_CONST real1 FP_NORM_EPSILON
Definition: qrack_types.hpp:258
std::shared_ptr< StateVectorSparse > StateVectorSparsePtr
Definition: qrack_types.hpp:147
std::function< void(const bitCapInt &, const unsigned &cpu)> ParallelFuncBdt
Definition: qrack_types.hpp:139
QRACK_CONST real1 REAL1_EPSILON
Definition: qrack_types.hpp:200
QRACK_CONST complex ONE_CMPLX
Definition: qrack_types.hpp:252
QRACK_CONST real1 ONE_R1
Definition: qrack_types.hpp:185
QRACK_CONST real1 ZERO_R1
Definition: qrack_types.hpp:183
constexpr bitLenInt bitsInCap
Definition: qrack_types.hpp:131
float real1_f
Definition: qrack_types.hpp:95
std::shared_ptr< StateVectorArray > StateVectorArrayPtr
Definition: qrack_types.hpp:146
float real1_s
Definition: qrack_types.hpp:96
QRACK_CONST complex CMPLX_DEFAULT_ARG
Definition: qrack_types.hpp:257
std::function< void(const bitCapIntOcl &, const unsigned &cpu)> ParallelFunc
Definition: qrack_types.hpp:136
QRACK_CONST complex I_CMPLX
Definition: qrack_types.hpp:254
std::shared_ptr< StateVector > StateVectorPtr
Definition: qrack_types.hpp:143
QRACK_CONST complex HALF_I_HALF_CMPLX
Definition: qrack_types.hpp:255
QRACK_CONST complex ZERO_CMPLX
Definition: qrack_types.hpp:253
const double FIDELITY_MIN
Definition: qrack_types.hpp:261
QRACK_CONST real1 PI_R1
Definition: qrack_types.hpp:178
const bitCapInt ONE_BCI
Definition: qrack_types.hpp:129
const bitCapInt ZERO_BCI
Definition: qrack_types.hpp:130
QRACK_CONST complex HALF_NEG_I_HALF_CMPLX
Definition: qrack_types.hpp:256
half log(half arg)
Natural logarithm.
Definition: half.hpp:3318
#define REAL1_DEFAULT_ARG
Definition: qrack_types.hpp:177
#define QRACK_CONST
Definition: qrack_types.hpp:174
#define bitLenInt
Definition: qrack_types.hpp:38
#define bitCapInt
Definition: qrack_types.hpp:62
#define bitCapIntOcl
Definition: qrack_types.hpp:50
#define QRACK_MAX_QUBITS
Definition: qrack_types.hpp:63