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) {
138 switch (activationFn) {
155 }
else if (activationFn ==
Sigmoid) {
156 #if (FPPOW < 5) || (FPPOW > 6)
158 std::unique_ptr<real1[]> _angles(
new real1[p]);
159 std::copy(angles, angles + p, _angles.get());
166 std::unique_ptr<real1[]> nAngles(
new real1[inputPower]);
167 switch (activationFn) {
169 std::transform(angles, angles + inputPower, nAngles.get(),
applyRelu);
172 std::transform(angles, angles + inputPower, nAngles.get(),
applyGelu);
176 angles, angles + inputPower, nAngles.get(), [&alpha](
real1 a) { return applyAlpha(a, alpha); });
180 angles, angles + inputPower, nAngles.get(), [&alpha](
real1 a) { return applyLeakyRelu(a, alpha); });
201 switch (activationFn) {
220 std::unique_ptr<real1[]> nAngles(
new real1[inputPower]);
221 switch (activationFn) {
223 std::transform(angles, angles + inputPower, nAngles.get(),
negApplyRelu);
227 std::transform(angles, angles + inputPower, nAngles.get(),
negApplyGelu);
232 angles, angles + inputPower, nAngles.get(), [&alpha](
real1 a) { return -applyAlpha(a, alpha); });
236 std::transform(angles, angles + inputPower, nAngles.get(),
237 [&alpha](
real1 a) { return -applyLeakyRelu(a, alpha); });
242 std::transform(angles, angles + inputPower, nAngles.get(), [](
real1 a) { return -a; });
256 const real1_f result =
Predict(angles, expected,
false, activationFn, alpha);
257 Unpredict(angles, expected, activationFn, alpha);
269 void Learn(
real1* angles,
const real1_f& eta,
const bool& expected =
true,
const bool& resetInit =
true,
272 real1_f startProb =
Predict(angles, expected, resetInit, activationFn, alpha);
273 Unpredict(angles, expected, activationFn, alpha);
279 startProb =
LearnInternal(angles, expected, eta, perm, startProb, activationFn, alpha);
298 const real1_f startProb =
Predict(angles, expected, resetInit, activationFn, alpha);
299 Unpredict(angles, expected, activationFn, alpha);
317 const real1 origAngle = angles[permOcl];
318 real1& angle = angles[permOcl];
328 angle = origAngle - delta;
331 if ((startProb >= plusProb) && (startProb >= minusProb)) {
338 if (plusProb > minusProb) {
339 angle = origAngle + delta;
Definition: qneuron.hpp:25
bitLenInt GetInputCount()
Definition: qneuron.hpp:116
real1_f Predict(const real1 *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:128
static real1_f clampAngle(real1_f angle)
Definition: qneuron.hpp:55
QNeuron & operator=(QNeuron &toCopy)
Definition: qneuron.hpp:94
bitLenInt GetOutputIndex()
Definition: qneuron.hpp:120
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
real1_f Unpredict(const real1 *angles, const bool &expected=true, const QNeuronActivationFn &activationFn=Sigmoid, const real1_f &alpha=ONE_R1_F)
"Uncompute" the Predict() method
Definition: qneuron.hpp:196
void Learn(real1 *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:269
real1_f LearnInternal(real1 *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:314
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
real1_f LearnCycle(real1 *angles, const bool &expected=true, const QNeuronActivationFn &activationFn=Sigmoid, const real1_f &alpha=ONE_R1_F)
Definition: qneuron.hpp:253
static real1_f applyRelu(const real1_f &angle)
Definition: qneuron.hpp:31
std::vector< bitLenInt > inputIndices
Definition: qneuron.hpp:28
static real1_f negApplyGelu(const real1_f &angle)
Definition: qneuron.hpp:37
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
void LearnPermutation(real1 *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:295
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
Half-precision floating-point type.
Definition: half.hpp:2206
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:182
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
half_float::half real1
Definition: qrack_types.hpp:106
QRACK_CONST real1 FP_NORM_EPSILON
Definition: qrack_types.hpp:263
QRACK_CONST real1 ONE_R1
Definition: qrack_types.hpp:188
QRACK_CONST real1 ZERO_R1
Definition: qrack_types.hpp:186
float real1_f
Definition: qrack_types.hpp:107
float real1_s
Definition: qrack_types.hpp:108
std::shared_ptr< QNeuron > QNeuronPtr
Definition: qneuron.hpp:22
QRACK_CONST real1 PI_R1
Definition: qrack_types.hpp:180
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:157
unsigned int erf(unsigned int arg)
Error function and postprocessing.
Definition: half.hpp:2076
HALF_CONSTEXPR half abs(half arg)
Absolute value.
Definition: half.hpp:2958
half fmod(half x, half y)
Remainder of division.
Definition: half.hpp:2966
half pow(half x, half y)
Power function.
Definition: half.hpp:3721
#define QRACK_CONST
Definition: qrack_types.hpp:176
#define bitLenInt
Definition: qrack_types.hpp:41
#define ZERO_R1_F
Definition: qrack_types.hpp:162
#define bitCapIntOcl
Definition: qrack_types.hpp:53
#define ONE_R1_F
Definition: qrack_types.hpp:165