# How is Derivative causing NMaximize::nnum the function value is not a number

pauliX = PauliMatrix[1];
pauliY = PauliMatrix[2];
pauliZ = PauliMatrix[3];
spinRaise = pauliX + I pauliY;
spinLower = pauliX - I pauliY;
pauli0 = 1/2 (IdentityMatrix[2] - (pauliZ/(2 + 1)));
zero = {1, 0};
one = {0, 1};
excluded =
"ExcludedFunctions" /. ("DifferentiationOptions" /.
SystemOptions["DifferentiationOptions"])
SetSystemOptions[
"DifferentiationOptions" ->
"ExcludedFunctions" -> Union[excluded, {Conjugate}]]

(*state1={Cos[θ],Exp[i ϕ].Sin[θ]};
den1=KroneckerProduct[state1,state1];
state2={Sin[θ],-Exp[i ϕ].Cos[θ]};
den2=KroneckerProduct[state2,state2];*)

stateEvolved[exite_,state_, rValue_, tauValue_] := Module[{},

rho11 = zero.state.zero;
rho22 = one.state.one;
rho12 = zero.state.one;
rho21 = one.state.zero;
currR = rValue;
currTau = tauValue;
(*xi=(Exp[-Abs[eigen2]time]-R.Exp[-dissipation.time])/1-R;
xi2=(Exp[-Abs[eigen2]time]-2.R.Exp[-dissipation.time])/1-2.R;*)

xi = (Exp[-currR*currTau] - currR*Exp[-currTau])/(1 - currR);
xi2 = (Exp[-2*currR*currTau] - 2*currR*Exp[-currTau])/(1 - 2*currR);
(*eigen2=-2.phenDissipation(exciteNum+(1/2));*)

rhoT = 1/
2 (IdentityMatrix[
2] - (xi*(rho11 - rho22 + (1/((2*exciteNum) + 1))) -
1/((2*exciteNum) + 1))*
pauliZ + (xi2/2)*(rho12*spinRaise + rho21*spinLower));
Return[rhoT];

];

traceDistance[excite_densityState1_, densityState2_, rValue_, tauValue_] :=
Module[{},

evolvedState1 = stateEvolved[densityState1, rValue, tauValue];
evolvedState2 = stateEvolved[densityState2, rValue, tauValue];
stateDifference = evolvedState1 - evolvedState2;
trd = 1/
2 (Tr[Sqrt[
ConjugateTranspose[stateDifference].stateDifference]]);
Return[trd];
];

derTraceDistance[excite_,densityState1_, densityState2_, rValue_, tauValue_] :=
D[traceDistance[excite,densityState1, densityState2, rValue, tauValue],
tauValue];

nonMarkovMeasure[excite_, θ_, ϕ_, rValue_, tauValue_] :=
Module[{},

exciteNum = excite;
state1 = {Cos[θ], Exp[I ϕ]*Sin[θ]};
den1 = KroneckerProduct[state1, state1];
state2 = {Sin[θ], -Exp[I ϕ]*Cos[θ]};
den2 = KroneckerProduct[state2, state2];

rate = Derivative[0,0,0,0,1][traceDistance][excite,den1, den2, rValue, tauValue];
Return[rate]
];

maximiseNMM[excite_, rValue_, tauValue_] := Module[{},

result =
Maximize[
nonMarkovMeasure[excite, θ, ϕ, rValue,
tauValue], {θ, ϕ}];

Return[result];
];

markovMeasure = Table[maximiseNMM[0, 0.05, t], {t, 1, 5}];

Select[markovMeasure, # > 0&]



Everytime I run this code, I get an error like the one below:

NMaximize::nnum: The function value -2.56488 (-0.00118823+(0.146982 +0.128055 I) \!$$\*SubscriptBox[\(\[PartialD]$$, $$1$$]$$(\(\(0.14698239215186137$$\) - 0.12805494426804936\ I)\)\))-2.56488 (-0.00118823-(0.146982 +0.128055 I) (1/2 \!$$\*SubscriptBox[\(\[PartialD]$$, $$1$$]$$(\(-1.5526246457945858$$ - 0.3582826891743395\ I)\)\)+1/2 \!$$\*SubscriptBox[\(\[PartialD]$$, $$1$$]$$(\(\(1.258659861490863$$\) + 0.6143925777104382\ I)\)\))) is not a number at {\[Theta],\[Phi]} = {0.918621,0.716689}.


In a nutshell, the code takes two input states. From there, the trace distance between those two states is then computed. After this is done, the derivative with respect to $$\tau$$ is taken. I am attempting to maximise this value with respect to the parameters $$\theta$$ and $$\phi$$, which are the parameters that define the states, for different values of $$\tau$$.

From the comments below, it seems the issue is that the derivative cannot be computed due to tau being give a specific value, as opposed to simply being left as an undefined variable. I am not really sure how mathematica uses the derivative. But essentially I need to calculate is with respect to $$\tau$$, then maximise w.r.t $$\phi$$ and $$\theta$$, then pass in a value for $$\tau$$.

I have now changed it from

derTraceDistance[excite,densityState1_, densityState2_, rValue_, tauValue_] :=
D[traceDistance[excite,densityState1, densityState2, rValue, tauValue],
tauValue];



to

Derivative[0,0,0,0,1][traceDistance][excite,den1, den2, rValue, tauValue];


as I want to take the first derivative of traceDistance w.r.t it's fifth parameter, $$\tau$$ then I want to pass in the actual values for the variables.

Best I can tell, the reason may be due to

General::ivar: 1 is not a valid variable.


which I think is happening as the derivative is being taken with respect to the value being passed in, 1, instead of the variable tauValue.

NMaximize::nnum: The function value -0.99123 (-0.0145768-(0.146982 +0.128055 I) \!$$\*SubscriptBox[\(\[PartialD]$$, $$1$$]$$(\(-0.14698239215186137$$ + 0.12805494426804936\ I)\)\)+(0.350778 +0.305607 I) \!$$\*SubscriptBox[\(\[PartialD]$$, $$1$$]$$(\(\(0.35077827528107836$$\) - 0.3056073032554202\ I)\)\))-0.99123 (-0.0145768+(0.146982 +<<20>> I) \!$$\*SubscriptBox[\(\[PartialD]$$, $$<<1>>$$]$$(<<1>>)$$\)+(0.350778 +0.305607 I) \!$$\*SubscriptBox[\(\[PartialD]$$, $$1$$]$$(\(\(0.35077827528107836$$\) - 0.3056073032554202\ I)\)\)) is not a number at {\[Theta],\[Phi]} = {0.918621,0.716689}.


Which seems to be what the PartialD is stating in this error. However, I need to pass in a value for the variable, but I am unsure how I would force mathematica due take the derivative before the value is passed in.

• You need to fix this: General::ivar: 1 is not a valid variable.. You're using Block[{}, ...] and then setting variables up inside which will leak out. Try this: Block[{}, xyz=3];Print[xyz] and you'll see the problem. What you need is Module and all local variables must be in the list. Also you are maximizing over tauValue, but the tauValue is replaced by a number from the argument so it's not a variable anymore. Also you don't pattern test the arguments for NumericQ so that might affect evaluation order. – flinty Dec 28 '20 at 15:32
• Apologies, I don't see what you mean about block. I have been setting up variables in block functions for over a year now and this has never been a problem before. For example, I can create a variable in block, evaluate the function and then access said variable with no issues. – GaussStrife Dec 28 '20 at 18:15
• Also, I am not maximizing over tau, I maximise over theta and phi. – GaussStrife Dec 28 '20 at 18:16
• Then you've been using it wrong. As I said, in this example: Block[{}, xyz=3];Print[xyz] the xyz retains its value after the Block. Another problem or perhaps more bad style: in the function stateEvolved you refer to exciteNum but it hasn't been assigned a value either as a local variable or as an argument to the function. The main problem in your code is you are differentiating with respect to a number, not a variable. Finally # > 0 is wrong in the Select, it should be # > 0 & you can also get rid of that For loop with AppendTo and just use Table. – flinty Dec 28 '20 at 18:33
• @GaussStrife Suppose I have debugged code. What should I get as output? Now I got {}. – Alex Trounev Jan 9 at 19:33