45 }
else if (angle <= -
PI_R1) {
50 return toRet + (
pow((2 *
abs(angle) /
PI_R1), alpha) * (
PI_R1 / 2) * ((angle < 0) ? -1 : 1));
60 angle =
fmod(angle, PI_4);
63 }
else if (angle > PI_2) {
136 switch (activationFn) {
153 }
else if (activationFn ==
Sigmoid) {
154 #if (FPPOW < 5) || (FPPOW > 6)
156 std::unique_ptr<real1[]> _angles(
new real1[p]);
157 std::copy(angles, angles + p, _angles.get());
164 std::unique_ptr<real1[]> nAngles(
new real1[inputPower]);
165 switch (activationFn) {
167 std::transform(angles, angles + inputPower, nAngles.get(),
applyRelu);
170 std::transform(angles, angles + inputPower, nAngles.get(),
applyGelu);
174 angles, angles + inputPower, nAngles.get(), [&alpha](
real1_s a) { return applyAlpha(a, alpha); });
178 angles, angles + inputPower, nAngles.get(), [&alpha](
real1_s a) { return applyLeakyRelu(a, alpha); });
199 switch (activationFn) {
218 std::unique_ptr<real1[]> nAngles(
new real1[inputPower]);
219 switch (activationFn) {
221 std::transform(angles, angles + inputPower, nAngles.get(),
negApplyRelu);
225 std::transform(angles, angles + inputPower, nAngles.get(),
negApplyGelu);
230 angles, angles + inputPower, nAngles.get(), [&alpha](
real1_s a) { return -applyAlpha(a, alpha); });
234 std::transform(angles, angles + inputPower, nAngles.get(),
235 [&alpha](
real1_s a) { return -applyLeakyRelu(a, alpha); });
240 std::transform(angles, angles + inputPower, nAngles.get(), [](
real1_s a) { return -a; });
254 const real1_f result =
Predict(angles, expected,
false, activationFn, alpha);
255 Unpredict(angles, expected, activationFn, alpha);
267 void Learn(
real1_s* angles,
const real1_f& eta,
const bool& expected =
true,
const bool& resetInit =
true,
270 real1_f startProb =
Predict(angles, expected, resetInit, activationFn, alpha);
271 Unpredict(angles, expected, activationFn, alpha);
277 startProb =
LearnInternal(angles, expected, eta, perm, startProb, activationFn, alpha);
297 const real1_f startProb =
Predict(angles, expected, resetInit, activationFn, alpha);
298 Unpredict(angles, expected, activationFn, alpha);
316 const real1_s origAngle = angles[permOcl];
317 real1_s& angle = angles[permOcl];
327 angle = origAngle - delta;
330 if ((startProb >= plusProb) && (startProb >= minusProb)) {
337 if (plusProb > minusProb) {
338 angle = origAngle + delta;
Definition: qneuron.hpp:25
real1_f LearnInternal(real1_s *angles, const bool &expected, const real1_f &eta, const bitCapIntOcl &permOcl, const real1_f &startProb, const QNeuronActivationFn &activationFn=Sigmoid, const real1_f &alpha=ONE_R1_F)
Definition: qneuron.hpp:313
bitLenInt GetInputCount()
Definition: qneuron.hpp:116
static real1_f clampAngle(real1_f angle)
Definition: qneuron.hpp:55
QNeuron & operator=(QNeuron &toCopy)
Definition: qneuron.hpp:94
static real1_f applyAlpha(real1_f angle, const real1_f &alpha)
Definition: qneuron.hpp:39
static real1_f negApplyRelu(const real1_f &angle)
Definition: qneuron.hpp:33
QInterfacePtr qReg
Definition: qneuron.hpp:29
bitLenInt outputIndex
Definition: qneuron.hpp:27
QNeuron(const QNeuron &toCopy)
Create a new QNeuron which is an exact duplicate of another, including its learned state.
Definition: qneuron.hpp:89
static real1_f applyLeakyRelu(const real1_f &angle, const real1_f &alpha)
Definition: qneuron.hpp:53
void LearnPermutation(real1_s *angles, const real1_f &eta, const bool &expected=true, const bool &resetInit=true, const QNeuronActivationFn &activationFn=Sigmoid, const real1_f &alpha=ONE_R1_F)
Perform one learning iteration, measuring the entire QInterface and training the resulting permutatio...
Definition: qneuron.hpp:293
static real1_f applyRelu(const real1_f &angle)
Definition: qneuron.hpp:31
real1_f Unpredict(const real1_s *angles, const bool &expected=true, const QNeuronActivationFn &activationFn=Sigmoid, const real1_f &alpha=ONE_R1_F)
"Uncompute" the Predict() method
Definition: qneuron.hpp:194
void Learn(real1_s *angles, const real1_f &eta, const bool &expected=true, const bool &resetInit=true, const QNeuronActivationFn &activationFn=Sigmoid, const real1_f &alpha=ONE_R1_F)
Perform one learning iteration, training all parameters.
Definition: qneuron.hpp:267
std::vector< bitLenInt > inputIndices
Definition: qneuron.hpp:28
static real1_f negApplyGelu(const real1_f &angle)
Definition: qneuron.hpp:37
real1_f Predict(const real1_s *angles, const bool &expected=true, const bool &resetInit=true, const QNeuronActivationFn &activationFn=Sigmoid, const real1_f &alpha=ONE_R1_F)
Predict a binary classification.
Definition: qneuron.hpp:126
QInterfacePtr GetSimulator()
Retrieve the simulator.
Definition: qneuron.hpp:114
bitCapIntOcl GetInputPower()
Definition: qneuron.hpp:118
void SetSimulator(QInterfacePtr sim)
Replace the simulator.
Definition: qneuron.hpp:104
static real1_f applyGelu(const real1_f &angle)
Definition: qneuron.hpp:35
QNeuron(QInterfacePtr reg, const std::vector< bitLenInt > &inputIndcs, const bitLenInt &outputIndx)
"QNeuron" is a "Quantum neuron" or "quantum perceptron" class that can learn and predict in superposi...
Definition: qneuron.hpp:81
void SetIndices(const std::vector< bitLenInt > &inputIndcs, const bitLenInt &outputIndx)
Replace the input and output indices.
Definition: qneuron.hpp:107
real1_f LearnCycle(real1_s *angles, const bool &expected=true, const QNeuronActivationFn &activationFn=Sigmoid, const real1_f &alpha=ONE_R1_F)
Definition: qneuron.hpp:251
Half-precision floating-point type.
Definition: half.hpp:2222
GLOSSARY: bitLenInt - "bit-length integer" - unsigned integer ID of qubit position in register bitCap...
Definition: complex16x2simd.hpp:25
QRACK_CONST real1 SQRT1_2_R1
Definition: qrack_types.hpp:188
std::shared_ptr< QInterface > QInterfacePtr
Definition: qinterface.hpp:29
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:1199
QRACK_CONST real1 FP_NORM_EPSILON
Definition: qrack_types.hpp:268
QRACK_CONST real1 ONE_R1
Definition: qrack_types.hpp:193
QRACK_CONST real1 ZERO_R1
Definition: qrack_types.hpp:191
float real1_f
Definition: qrack_types.hpp:103
float real1_s
Definition: qrack_types.hpp:104
std::shared_ptr< QNeuron > QNeuronPtr
Definition: qneuron.hpp:22
QRACK_CONST real1 PI_R1
Definition: qrack_types.hpp:186
QNeuronActivationFn
Enumerated list of activation functions.
Definition: qneuron_activation_function.hpp:19
@ Sigmoid
Default.
Definition: qneuron_activation_function.hpp:21
@ ReLU
Rectified linear.
Definition: qneuron_activation_function.hpp:23
@ Generalized_Logistic
Version of (default) "Sigmoid" with tunable sharpness.
Definition: qneuron_activation_function.hpp:27
@ GeLU
Gaussian linear.
Definition: qneuron_activation_function.hpp:25
@ Leaky_ReLU
Leaky rectified linear.
Definition: qneuron_activation_function.hpp:29
bitCapIntOcl pow2Ocl(const bitLenInt &p)
Definition: qrack_functions.hpp:144
unsigned int erf(unsigned int arg)
Error function and postprocessing.
Definition: half.hpp:2092
HALF_CONSTEXPR half abs(half arg)
Absolute value.
Definition: half.hpp:2975
half fmod(half x, half y)
Remainder of division.
Definition: half.hpp:2983
half pow(half x, half y)
Power function.
Definition: half.hpp:3738
#define QRACK_CONST
Definition: qrack_types.hpp:182
#define bitLenInt
Definition: qrack_types.hpp:42
#define ZERO_R1_F
Definition: qrack_types.hpp:168
#define bitCapIntOcl
Definition: qrack_types.hpp:54
#define ONE_R1_F
Definition: qrack_types.hpp:171