# How does one use NumericQ when NMinimize isn't being called in a function?

I have a value that is calculated based on the matric multiplication of 3 matrices, a projection in a hilbert space really. I then take the trace of this to calculate the probability

probM1 = Tr[
Flatten[Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p1}}],
1].upF.Flatten[
Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p1}}], 1]];


The matrix p1 is a 2x2 matrix with parameters undefined that I am trying to minimize

p1 = KroneckerProduct[{Cos[θ], Exp[I ϕ]*Sin[θ]},
Conjugate[{Cos[θ], Exp[I ϕ]*Sin[θ]}]];


I am calling NMinimize as follows:

NMinimize[{probM1[θ, ϕ], {θ ∈
Reals, θ <= 2*π, ϕ ∈ Reals,
0 <= ϕ <= 2*π}}, {θ, ϕ}]


Now as you might guess, the issue I get is the following: NMinimize::nnum: The function value (0.5 +2.53397*10^-49 I)[6.28319,5.22609] is not a number at {θ,ϕ} = {6.28319,5.22609}.

Ready a few posts, the problem seems to be with symbolic expression, and NumericQ seems to be a possible solution. However, it always seems to be called via an input to a created function, which I don't have here. Is there a way to utilize it just using the code I currently have, or will I need to specify a function for one of these 3 blocks?

I have tried

f[pro_?NumericQ] :=
Tr[Flatten[Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], pro}}],
1].upF.Flatten[
Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], pro}}], 1]]
NMinimize[{f[
p1], {θ ∈ Reals, θ <=
2*π, ϕ ∈ Reals,
0 <= ϕ <= 2*π}}, {θ, ϕ}]


But I get the same issue.

Edit:

Here is an example of upF

{{0.375, -0.225754 + i0.0998109,
0.125, -0.0752515 - i0.0998109}, {-0.225754 - i0.0998109,
0.375, -0.0752515 + i0.0998109,
0.125}, {0.125, -0.0752515 - i0.0998109,
0.125, -0.0752515 - i0.0998109}, {-0.0752515 + i0.0998109,
0.125, -0.0752515 + i0.0998109, 0.125}}

• You call probM1 like a function. However it is an expression: Tr[...] Feb 23 at 15:30
• I only did that after attempting it with just probM1, not probM1[]. It didn't work and a post with a simialr issue solved theres by calling it like that. Doing it with probM1 itself has the same issue. Feb 23 at 15:39
• What is the definition for upF? You prevent f from evaluating without a numeric argument; however, f doesn't use any numeric techniques and isn't affected by this. The NMinimize expression is the numeric technique and that is what needs to be defined as a function with appropriate parameters and stopped from trying to evaluate unless the parameters have numeric values. Feb 23 at 15:42
• upF is just a matrix of values. It doesn't have any indefined paramaters present. So created a function that passes in probM1 numerically and calculates NMinimize on it? Feb 23 at 15:50
• Defining a function that performs minimization on probM1 via input of probM1, using numericQ on the input, and then calling said function just returns the function with probM1 passed in. Feb 23 at 16:00

Clear["Global*"]

upF = {{0.375, -0.225754 + I*0.0998109,
0.125, -0.0752515 - I*0.0998109}, {-0.225754 - I*0.0998109,
0.375, -0.0752515 + I*0.0998109,
0.125}, {0.125, -0.0752515 - I*0.0998109,
0.125, -0.0752515 - I*0.0998109}, {-0.0752515 + I*0.0998109,
0.125, -0.0752515 + I*0.0998109, 0.125}} // Rationalize[#, 0] &;

p1 = KroneckerProduct[{Cos[θ], Exp[I ϕ]*Sin[θ]},
Conjugate[{Cos[θ], Exp[I ϕ]*Sin[θ]}]];

f[θ_, ϕ_] =
ComplexExpand[
Tr[Flatten[Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p1}}],
1] . upF .
Flatten[Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p1}}],
1]]] // Simplify

(* 1/2 - (602011 Cos[θ] Cos[ϕ] Sin[θ])/1000000 *)


Minimizing,

{min, arg} =
NMinimize[{f[θ, ϕ], 0 <= θ <= 2*π,
0 <= ϕ <= 2*π}, {θ, ϕ}]

(* {0.198995, {θ -> 5.49779, ϕ -> 3.14159}} *)

argmin = RootApproximant[({θ, ϕ} /. arg)/Pi]*Pi

(* {(7 π)/4, π} *)

minval = f @@ argmin

(* 397989/2000000 *)


EDIT: Using Minimize

{min, arg} =
Minimize[{f[θ, ϕ], 0 <= θ <= 2*π,
0 <= ϕ <= 2*π}, {θ, ϕ}] // Simplify

(* {397989/2000000, {θ -> (5 π)/4, ϕ -> 0}} *)

• Thank you Bob. So was my issue that I was passing a function taking in the matrix dependent on the parameters to minimize, as opposed to the parameters themselves? As in, it can't just take in the matrix dependent on the parameters? I also notice you side stepped the use of NumericQ? Would there still have been a way to achieve the result using it? Feb 23 at 18:20
• You only need NumericQ if the NMinimize contains an undefined parameter and you need to hold its evaluation until that parameter is provided. Then the NumericQ would be used with argument of the function containing the NMinimize. That is why it was impossible to understand fully your issue without the definition of upF. Also, min or max have no meaning with complex numbers, you need to ensure that values do not have any imaginary artifacts. That is why I converted everything to exact values and used ComplexExpand to get an explicitly real representation for f`. Feb 23 at 18:39