0
$\begingroup$
upF = {{0.375, -0.225754 + 0.0998109 I, 
  0.125, -0.0752515 - 0.0998109 I}, {-0.225754 - 0.0998109 I, 
  0.375, -0.0752515 + 0.0998109 I, 
  0.125}, {0.125, -0.0752515 - 0.0998109 I, 
  0.125, -0.0752515 - 0.0998109 I}, {-0.0752515 + 0.0998109 I, 
  0.125, -0.0752515 + 0.0998109 I, 0.125}}

p1 = KroneckerProduct[{Cos[\[Theta]], Exp[I \[Phi]]*Sin[\[Theta]]}, 
   Conjugate[{Cos[\[Theta]], Exp[I \[Phi]]*Sin[\[Theta]]}]];
p2 = KroneckerProduct[{Exp[-I \[Phi]]*Sin[\[Theta]], -Cos[\[Theta]]}, 
   Conjugate[{Exp[-I \[Phi]]*Sin[\[Theta]], -Cos[\[Theta]]}]];
dTraceSystem[upF, {2}, 2]
upF1 = dTraceSystem[
   Flatten[Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p1}}], 
     1].upF.Flatten[
     Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p1}}], 1], {2},
    2];
upF2 = dTraceSystem[
   Flatten[Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p2}}], 
     1].upF.Flatten[
     Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p2}}], 1], {2},
    2];
probM1 = Simplify[
   ComplexExpand[
    Tr[Flatten[
       Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p1}}], 
       1].upF.Flatten[
       Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p1}}], 
       1]]]];
probM2 = Simplify[
   ComplexExpand[
    Tr[Flatten[
       Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p2}}], 
       1].upF.Flatten[
       Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p2}}], 
       1]]]];
test1 = upF1/probM1
test2 = upF2/probM2
oop = Eigenvalues[test1 + 10^-15 IdentityMatrix[Length[test1]]];
entropyOop = 
  ComplexExpand[-Sum[
     oop[[j]] Log[2, oop[[j]]], {j, 1, Length[oop]}]];
ComplexExpand[entropyOop[[1]]]
oop1 = Eigenvalues[test2 + 10^-15 IdentityMatrix[Length[test2]]];
entropyOop1 = 
  ComplexExpand[-Sum[
     oop1[[j]] Log[2, oop1[[j]]], {j, 1, Length[oop1]}]];

NMinimize[{probM1*entropyOop + 
   probM2*entropyOop1, {\[Theta] \[Element] Reals, \[Theta] <= 
    2*\[Pi], \[Phi] \[Element] Reals, 
   0 <= \[Phi] <= 2*\[Pi]}}, {\[Theta], \[Phi]}]

So after this post, I thought I had a handle on how to utilize the min and max functions. However, I am still running into the problem of non real numbers, despite having complex expanded the eigenvalue calculations.

Using numericQ shouldn't help as, from my understanding, that is only employed to halt the evaluation of a function containing nMinimize until parameters have been passed to it. I tried it anyway, no change. I attempted to set up the eigenvalue calculations and matrix calculations of test1 and test2 as functions as well. However, this also hasn't solved the problem. Honestly, at this point I clearly don't understand how nMinimize actually works and I am just randomly trying things to get it to work.

Edit: To clarify, probM1 and probM2 minimize just fine. The issue seemsto come from the introduction of entropyOop and entropOop1.

Edit2 :Edit: So after having updated the code to the above, and attempting both Re and Chop to handle the imaginary parts, I am still getting either not a number or not a scalar value

Edit3:

Here is test1:

test1 = {{1/(0.5 - 
    0.602012 Cos[θ] Cos[ϕ] Sin[θ]) (Conjugate[
       Cos[θ]] Cos[θ] ((0. + 0. I) + 
        0.375 Conjugate[
          Cos[θ]] Cos[θ] - (0.225754 + 
           0.0998109 I) E^(-I Conjugate[ϕ])
          Conjugate[Sin[θ]] Cos[θ]) + 
     E^(I ϕ)
       Conjugate[
       Cos[θ]] ((0. + 
          0. I) - (0.225754 - 0.0998109 I) Conjugate[
          Cos[θ]] Cos[θ] + 
        0.375 E^(-I Conjugate[ϕ])
          Conjugate[Sin[θ]] Cos[θ]) Sin[θ] + 
     E^(-I Conjugate[ϕ])
       Conjugate[
       Sin[θ]] Cos[θ] ((0. + 0. I) + 
        0.375 E^(I ϕ)
          Conjugate[
          Cos[θ]] Sin[θ] - (0.225754 + 0.0998109 I) E^(
         I ϕ - I Conjugate[ϕ])
          Conjugate[Sin[θ]] Sin[θ]) + 
     E^(I ϕ - I Conjugate[ϕ])
       Conjugate[
       Sin[θ]] Sin[θ] ((0. + 
          0. I) - (0.225754 - 0.0998109 I) E^(I ϕ)
          Conjugate[Cos[θ]] Sin[θ] + 
        0.375 E^(I ϕ - I Conjugate[ϕ])
          Conjugate[Sin[θ]] Sin[θ])), 
  1/(0.5 - 0.602012 Cos[θ] Cos[ϕ] Sin[θ]) \
(Conjugate[
       Cos[θ]] Cos[θ] ((0. + 0. I) + 
        0.125 Conjugate[
          Cos[θ]] Cos[θ] - (0.0752515 - 
           0.0998109 I) E^(-I Conjugate[ϕ])
          Conjugate[Sin[θ]] Cos[θ]) + 
     E^(I ϕ)
       Conjugate[
       Cos[θ]] ((0. + 
          0. I) - (0.0752515 + 0.0998109 I) Conjugate[
          Cos[θ]] Cos[θ] + 
        0.125 E^(-I Conjugate[ϕ])
          Conjugate[Sin[θ]] Cos[θ]) Sin[θ] + 
     E^(-I Conjugate[ϕ])
       Conjugate[
       Sin[θ]] Cos[θ] ((0. + 0. I) + 
        0.125 E^(I ϕ)
          Conjugate[
          Cos[θ]] Sin[θ] - (0.0752515 - 0.0998109 I) E^(
         I ϕ - I Conjugate[ϕ])
          Conjugate[Sin[θ]] Sin[θ]) + 
     E^(I ϕ - I Conjugate[ϕ])
       Conjugate[
       Sin[θ]] Sin[θ] ((0. + 
          0. I) - (0.0752515 + 0.0998109 I) E^(I ϕ)
          Conjugate[Cos[θ]] Sin[θ] + 
        0.125 E^(I ϕ - I Conjugate[ϕ])
          Conjugate[Sin[θ]] Sin[θ]))}, {1/(
   0.5 - 0.602012 Cos[θ] Cos[ϕ] Sin[θ]) (Conjugate[
       Cos[θ]] Cos[θ] ((0. + 0. I) + 
        0.125 Conjugate[
          Cos[θ]] Cos[θ] - (0.0752515 - 
           0.0998109 I) E^(-I Conjugate[ϕ])
          Conjugate[Sin[θ]] Cos[θ]) + 
     E^(I ϕ)
       Conjugate[
       Cos[θ]] ((0. + 
          0. I) - (0.0752515 + 0.0998109 I) Conjugate[
          Cos[θ]] Cos[θ] + 
        0.125 E^(-I Conjugate[ϕ])
          Conjugate[Sin[θ]] Cos[θ]) Sin[θ] + 
     E^(-I Conjugate[ϕ])
       Conjugate[
       Sin[θ]] Cos[θ] ((0. + 0. I) + 
        0.125 E^(I ϕ)
          Conjugate[
          Cos[θ]] Sin[θ] - (0.0752515 - 0.0998109 I) E^(
         I ϕ - I Conjugate[ϕ])
          Conjugate[Sin[θ]] Sin[θ]) + 
     E^(I ϕ - I Conjugate[ϕ])
       Conjugate[
       Sin[θ]] Sin[θ] ((0. + 
          0. I) - (0.0752515 + 0.0998109 I) E^(I ϕ)
          Conjugate[Cos[θ]] Sin[θ] + 
        0.125 E^(I ϕ - I Conjugate[ϕ])
          Conjugate[Sin[θ]] Sin[θ])), 
  1/(0.5 - 0.602012 Cos[θ] Cos[ϕ] Sin[θ]) \
(Conjugate[
       Cos[θ]] Cos[θ] ((0. + 0. I) + 
        0.125 Conjugate[
          Cos[θ]] Cos[θ] - (0.0752515 - 
           0.0998109 I) E^(-I Conjugate[ϕ])
          Conjugate[Sin[θ]] Cos[θ]) + 
     E^(I ϕ)
       Conjugate[
       Cos[θ]] ((0. + 
          0. I) - (0.0752515 + 0.0998109 I) Conjugate[
          Cos[θ]] Cos[θ] + 
        0.125 E^(-I Conjugate[ϕ])
          Conjugate[Sin[θ]] Cos[θ]) Sin[θ] + 
     E^(-I Conjugate[ϕ])
       Conjugate[
       Sin[θ]] Cos[θ] ((0. + 0. I) + 
        0.125 E^(I ϕ)
          Conjugate[
          Cos[θ]] Sin[θ] - (0.0752515 - 0.0998109 I) E^(
         I ϕ - I Conjugate[ϕ])
          Conjugate[Sin[θ]] Sin[θ]) + 
     E^(I ϕ - I Conjugate[ϕ])
       Conjugate[
       Sin[θ]] Sin[θ] ((0. + 
          0. I) - (0.0752515 + 0.0998109 I) E^(I ϕ)
          Conjugate[Cos[θ]] Sin[θ] + 
        0.125 E^(I ϕ - I Conjugate[ϕ])
          Conjugate[Sin[θ]] Sin[θ]))}};

Here is test 2:

test2 = {{1/(0.5 + 
    0.602012 Cos[θ] Cos[ϕ] Sin[θ]) (Conjugate[
       Cos[θ]] Cos[θ] ((0. + 0. I) + 
        0.375 Conjugate[
          Cos[θ]] Cos[θ] + (0.225754 - 0.0998109 I) E^(
         I Conjugate[ϕ])
          Conjugate[Sin[θ]] Cos[θ]) - 
     E^(-I ϕ)
       Conjugate[
       Cos[θ]] ((0. + 
          0. I) - (0.225754 + 0.0998109 I) Conjugate[
          Cos[θ]] Cos[θ] - 
        0.375 E^(I Conjugate[ϕ])
          Conjugate[Sin[θ]] Cos[θ]) Sin[θ] - 
     E^(I Conjugate[ϕ])
       Conjugate[
       Sin[θ]] Cos[θ] ((0. + 0. I) - 
        0.375 E^(-I ϕ)
          Conjugate[
          Cos[θ]] Sin[θ] - (0.225754 - 
           0.0998109 I) E^(-I ϕ + I Conjugate[ϕ])
          Conjugate[Sin[θ]] Sin[θ]) + 
     E^(-I ϕ + I Conjugate[ϕ])
       Conjugate[
       Sin[θ]] Sin[θ] ((0. + 
          0. I) + (0.225754 + 0.0998109 I) E^(-I ϕ)
          Conjugate[Cos[θ]] Sin[θ] + 
        0.375 E^(-I ϕ + I Conjugate[ϕ])
          Conjugate[Sin[θ]] Sin[θ])), 
  1/(0.5 + 0.602012 Cos[θ] Cos[ϕ] Sin[θ]) \
(Conjugate[
       Cos[θ]] Cos[θ] ((0. + 0. I) + 
        0.125 Conjugate[
          Cos[θ]] Cos[θ] + (0.0752515 + 0.0998109 I) E^(
         I Conjugate[ϕ])
          Conjugate[Sin[θ]] Cos[θ]) - 
     E^(-I ϕ)
       Conjugate[
       Cos[θ]] ((0. + 
          0. I) - (0.0752515 - 0.0998109 I) Conjugate[
          Cos[θ]] Cos[θ] - 
        0.125 E^(I Conjugate[ϕ])
          Conjugate[Sin[θ]] Cos[θ]) Sin[θ] - 
     E^(I Conjugate[ϕ])
       Conjugate[
       Sin[θ]] Cos[θ] ((0. + 0. I) - 
        0.125 E^(-I ϕ)
          Conjugate[
          Cos[θ]] Sin[θ] - (0.0752515 + 
           0.0998109 I) E^(-I ϕ + I Conjugate[ϕ])
          Conjugate[Sin[θ]] Sin[θ]) + 
     E^(-I ϕ + I Conjugate[ϕ])
       Conjugate[
       Sin[θ]] Sin[θ] ((0. + 
          0. I) + (0.0752515 - 0.0998109 I) E^(-I ϕ)
          Conjugate[Cos[θ]] Sin[θ] + 
        0.125 E^(-I ϕ + I Conjugate[ϕ])
          Conjugate[Sin[θ]] Sin[θ]))}, {1/(
   0.5 + 0.602012 Cos[θ] Cos[ϕ] Sin[θ]) (Conjugate[
       Cos[θ]] Cos[θ] ((0. + 0. I) + 
        0.125 Conjugate[
          Cos[θ]] Cos[θ] + (0.0752515 + 0.0998109 I) E^(
         I Conjugate[ϕ])
          Conjugate[Sin[θ]] Cos[θ]) - 
     E^(-I ϕ)
       Conjugate[
       Cos[θ]] ((0. + 
          0. I) - (0.0752515 - 0.0998109 I) Conjugate[
          Cos[θ]] Cos[θ] - 
        0.125 E^(I Conjugate[ϕ])
          Conjugate[Sin[θ]] Cos[θ]) Sin[θ] - 
     E^(I Conjugate[ϕ])
       Conjugate[
       Sin[θ]] Cos[θ] ((0. + 0. I) - 
        0.125 E^(-I ϕ)
          Conjugate[
          Cos[θ]] Sin[θ] - (0.0752515 + 
           0.0998109 I) E^(-I ϕ + I Conjugate[ϕ])
          Conjugate[Sin[θ]] Sin[θ]) + 
     E^(-I ϕ + I Conjugate[ϕ])
       Conjugate[
       Sin[θ]] Sin[θ] ((0. + 
          0. I) + (0.0752515 - 0.0998109 I) E^(-I ϕ)
          Conjugate[Cos[θ]] Sin[θ] + 
        0.125 E^(-I ϕ + I Conjugate[ϕ])
          Conjugate[Sin[θ]] Sin[θ])), 
  1/(0.5 + 0.602012 Cos[θ] Cos[ϕ] Sin[θ]) \
(Conjugate[
       Cos[θ]] Cos[θ] ((0. + 0. I) + 
        0.125 Conjugate[
          Cos[θ]] Cos[θ] + (0.0752515 + 0.0998109 I) E^(
         I Conjugate[ϕ])
          Conjugate[Sin[θ]] Cos[θ]) - 
     E^(-I ϕ)
       Conjugate[
       Cos[θ]] ((0. + 
          0. I) - (0.0752515 - 0.0998109 I) Conjugate[
          Cos[θ]] Cos[θ] - 
        0.125 E^(I Conjugate[ϕ])
          Conjugate[Sin[θ]] Cos[θ]) Sin[θ] - 
     E^(I Conjugate[ϕ])
       Conjugate[
       Sin[θ]] Cos[θ] ((0. + 0. I) - 
        0.125 E^(-I ϕ)
          Conjugate[
          Cos[θ]] Sin[θ] - (0.0752515 + 
           0.0998109 I) E^(-I ϕ + I Conjugate[ϕ])
          Conjugate[Sin[θ]] Sin[θ]) + 
     E^(-I ϕ + I Conjugate[ϕ])
       Conjugate[
       Sin[θ]] Sin[θ] ((0. + 
          0. I) + (0.0752515 - 0.0998109 I) E^(-I ϕ)
          Conjugate[Cos[θ]] Sin[θ] + 
        0.125 E^(-I ϕ + I Conjugate[ϕ])
          Conjugate[Sin[θ]] Sin[θ]))}};

Here is the system for oop2:

{{0.75, 0.25}, {0.25, 0.25}}

Edit 5: Using the suggestion of mszynisz, I have rewrote them as expressions as opposed to functions.

$\endgroup$
8
  • $\begingroup$ dTraceSystem is undefined. $\endgroup$
    – Michael E2
    Mar 7, 2022 at 15:14
  • $\begingroup$ Yeah I just added in the actual matrices that it calculates and uses for the eigenvalue functions that NMinimize is having trouble with. $\endgroup$ Mar 7, 2022 at 15:25
  • $\begingroup$ It's still impossible to help without the value of entropyOop2, or perhaps oop2, or as a last resort, the definition of dTraceSystem. $\endgroup$
    – Michael E2
    Mar 7, 2022 at 15:52
  • $\begingroup$ Added in the system that is passed in for oop2. I've used the dTraceSystem code a long time, the matrices it creates are obeying the properties density matrices need to possess. $\endgroup$ Mar 7, 2022 at 18:43
  • $\begingroup$ Actually those values don't help. They aren't involved in the minimization. $\endgroup$ Mar 8, 2022 at 12:56

2 Answers 2

1
$\begingroup$

I include all code just to be clear with unused parts commented out:

test1 = {{1/(0.5 - 
        0.602012 Cos[θ] Cos[ϕ] Sin[θ]) (Conjugate[
         Cos[θ]] Cos[θ] ((0. + 0. I) + 
          0.375 Conjugate[
            Cos[θ]] Cos[θ] - (0.225754 + 
             0.0998109 I) E^(-I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Cos[θ]) + 
       E^(I ϕ) Conjugate[
         Cos[θ]] ((0. + 
            0. I) - (0.225754 - 0.0998109 I) Conjugate[
            Cos[θ]] Cos[θ] + 
          0.375 E^(-I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Cos[θ]) Sin[θ] + 
       E^(-I Conjugate[ϕ]) Conjugate[
         Sin[θ]] Cos[θ] ((0. + 0. I) + 
          0.375 E^(I ϕ) Conjugate[
            Cos[θ]] Sin[θ] - (0.225754 + 
             0.0998109 I) E^(I ϕ - 
              I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Sin[θ]) + 
       E^(I ϕ - I Conjugate[ϕ]) Conjugate[
         Sin[θ]] Sin[θ] ((0. + 
            0. I) - (0.225754 - 0.0998109 I) E^(I ϕ) Conjugate[
            Cos[θ]] Sin[θ] + 
          0.375 E^(I ϕ - I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Sin[θ])), 
    1/(0.5 - 
        0.602012 Cos[θ] Cos[ϕ] Sin[θ]) (Conjugate[
         Cos[θ]] Cos[θ] ((0. + 0. I) + 
          0.125 Conjugate[
            Cos[θ]] Cos[θ] - (0.0752515 - 
             0.0998109 I) E^(-I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Cos[θ]) + 
       E^(I ϕ) Conjugate[
         Cos[θ]] ((0. + 
            0. I) - (0.0752515 + 0.0998109 I) Conjugate[
            Cos[θ]] Cos[θ] + 
          0.125 E^(-I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Cos[θ]) Sin[θ] + 
       E^(-I Conjugate[ϕ]) Conjugate[
         Sin[θ]] Cos[θ] ((0. + 0. I) + 
          0.125 E^(I ϕ) Conjugate[
            Cos[θ]] Sin[θ] - (0.0752515 - 
             0.0998109 I) E^(I ϕ - 
              I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Sin[θ]) + 
       E^(I ϕ - I Conjugate[ϕ]) Conjugate[
         Sin[θ]] Sin[θ] ((0. + 
            0. I) - (0.0752515 + 0.0998109 I) E^(I ϕ) Conjugate[
            Cos[θ]] Sin[θ] + 
          0.125 E^(I ϕ - I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Sin[θ]))}, {1/(0.5 - 
        0.602012 Cos[θ] Cos[ϕ] Sin[θ]) (Conjugate[
         Cos[θ]] Cos[θ] ((0. + 0. I) + 
          0.125 Conjugate[
            Cos[θ]] Cos[θ] - (0.0752515 - 
             0.0998109 I) E^(-I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Cos[θ]) + 
       E^(I ϕ) Conjugate[
         Cos[θ]] ((0. + 
            0. I) - (0.0752515 + 0.0998109 I) Conjugate[
            Cos[θ]] Cos[θ] + 
          0.125 E^(-I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Cos[θ]) Sin[θ] + 
       E^(-I Conjugate[ϕ]) Conjugate[
         Sin[θ]] Cos[θ] ((0. + 0. I) + 
          0.125 E^(I ϕ) Conjugate[
            Cos[θ]] Sin[θ] - (0.0752515 - 
             0.0998109 I) E^(I ϕ - 
              I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Sin[θ]) + 
       E^(I ϕ - I Conjugate[ϕ]) Conjugate[
         Sin[θ]] Sin[θ] ((0. + 
            0. I) - (0.0752515 + 0.0998109 I) E^(I ϕ) Conjugate[
            Cos[θ]] Sin[θ] + 
          0.125 E^(I ϕ - I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Sin[θ])), 
    1/(0.5 - 
        0.602012 Cos[θ] Cos[ϕ] Sin[θ]) (Conjugate[
         Cos[θ]] Cos[θ] ((0. + 0. I) + 
          0.125 Conjugate[
            Cos[θ]] Cos[θ] - (0.0752515 - 
             0.0998109 I) E^(-I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Cos[θ]) + 
       E^(I ϕ) Conjugate[
         Cos[θ]] ((0. + 
            0. I) - (0.0752515 + 0.0998109 I) Conjugate[
            Cos[θ]] Cos[θ] + 
          0.125 E^(-I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Cos[θ]) Sin[θ] + 
       E^(-I Conjugate[ϕ]) Conjugate[
         Sin[θ]] Cos[θ] ((0. + 0. I) + 
          0.125 E^(I ϕ) Conjugate[
            Cos[θ]] Sin[θ] - (0.0752515 - 
             0.0998109 I) E^(I ϕ - 
              I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Sin[θ]) + 
       E^(I ϕ - I Conjugate[ϕ]) Conjugate[
         Sin[θ]] Sin[θ] ((0. + 
            0. I) - (0.0752515 + 0.0998109 I) E^(I ϕ) Conjugate[
            Cos[θ]] Sin[θ] + 
          0.125 E^(I ϕ - I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Sin[θ]))}};

test2 = {{1/(0.5 + 
        0.602012 Cos[θ] Cos[ϕ] Sin[θ]) (Conjugate[
         Cos[θ]] Cos[θ] ((0. + 0. I) + 
          0.375 Conjugate[
            Cos[θ]] Cos[θ] + (0.225754 - 
             0.0998109 I) E^(I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Cos[θ]) - 
       E^(-I ϕ) Conjugate[
         Cos[θ]] ((0. + 
            0. I) - (0.225754 + 0.0998109 I) Conjugate[
            Cos[θ]] Cos[θ] - 
          0.375 E^(I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Cos[θ]) Sin[θ] - 
       E^(I Conjugate[ϕ]) Conjugate[
         Sin[θ]] Cos[θ] ((0. + 0. I) - 
          0.375 E^(-I ϕ) Conjugate[
            Cos[θ]] Sin[θ] - (0.225754 - 
             0.0998109 I) E^(-I ϕ + 
              I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Sin[θ]) + 
       E^(-I ϕ + I Conjugate[ϕ]) Conjugate[
         Sin[θ]] Sin[θ] ((0. + 
            0. I) + (0.225754 + 0.0998109 I) E^(-I ϕ) Conjugate[
            Cos[θ]] Sin[θ] + 
          0.375 E^(-I ϕ + I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Sin[θ])), 
    1/(0.5 + 
        0.602012 Cos[θ] Cos[ϕ] Sin[θ]) (Conjugate[
         Cos[θ]] Cos[θ] ((0. + 0. I) + 
          0.125 Conjugate[
            Cos[θ]] Cos[θ] + (0.0752515 + 
             0.0998109 I) E^(I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Cos[θ]) - 
       E^(-I ϕ) Conjugate[
         Cos[θ]] ((0. + 
            0. I) - (0.0752515 - 0.0998109 I) Conjugate[
            Cos[θ]] Cos[θ] - 
          0.125 E^(I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Cos[θ]) Sin[θ] - 
       E^(I Conjugate[ϕ]) Conjugate[
         Sin[θ]] Cos[θ] ((0. + 0. I) - 
          0.125 E^(-I ϕ) Conjugate[
            Cos[θ]] Sin[θ] - (0.0752515 + 
             0.0998109 I) E^(-I ϕ + 
              I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Sin[θ]) + 
       E^(-I ϕ + I Conjugate[ϕ]) Conjugate[
         Sin[θ]] Sin[θ] ((0. + 
            0. I) + (0.0752515 - 0.0998109 I) E^(-I ϕ) Conjugate[
            Cos[θ]] Sin[θ] + 
          0.125 E^(-I ϕ + I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Sin[θ]))}, {1/(0.5 + 
        0.602012 Cos[θ] Cos[ϕ] Sin[θ]) (Conjugate[
         Cos[θ]] Cos[θ] ((0. + 0. I) + 
          0.125 Conjugate[
            Cos[θ]] Cos[θ] + (0.0752515 + 
             0.0998109 I) E^(I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Cos[θ]) - 
       E^(-I ϕ) Conjugate[
         Cos[θ]] ((0. + 
            0. I) - (0.0752515 - 0.0998109 I) Conjugate[
            Cos[θ]] Cos[θ] - 
          0.125 E^(I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Cos[θ]) Sin[θ] - 
       E^(I Conjugate[ϕ]) Conjugate[
         Sin[θ]] Cos[θ] ((0. + 0. I) - 
          0.125 E^(-I ϕ) Conjugate[
            Cos[θ]] Sin[θ] - (0.0752515 + 
             0.0998109 I) E^(-I ϕ + 
              I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Sin[θ]) + 
       E^(-I ϕ + I Conjugate[ϕ]) Conjugate[
         Sin[θ]] Sin[θ] ((0. + 
            0. I) + (0.0752515 - 0.0998109 I) E^(-I ϕ) Conjugate[
            Cos[θ]] Sin[θ] + 
          0.125 E^(-I ϕ + I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Sin[θ])), 
    1/(0.5 + 
        0.602012 Cos[θ] Cos[ϕ] Sin[θ]) (Conjugate[
         Cos[θ]] Cos[θ] ((0. + 0. I) + 
          0.125 Conjugate[
            Cos[θ]] Cos[θ] + (0.0752515 + 
             0.0998109 I) E^(I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Cos[θ]) - 
       E^(-I ϕ) Conjugate[
         Cos[θ]] ((0. + 
            0. I) - (0.0752515 - 0.0998109 I) Conjugate[
            Cos[θ]] Cos[θ] - 
          0.125 E^(I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Cos[θ]) Sin[θ] - 
       E^(I Conjugate[ϕ]) Conjugate[
         Sin[θ]] Cos[θ] ((0. + 0. I) - 
          0.125 E^(-I ϕ) Conjugate[
            Cos[θ]] Sin[θ] - (0.0752515 + 
             0.0998109 I) E^(-I ϕ + 
              I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Sin[θ]) + 
       E^(-I ϕ + I Conjugate[ϕ]) Conjugate[
         Sin[θ]] Sin[θ] ((0. + 
            0. I) + (0.0752515 - 0.0998109 I) E^(-I ϕ) Conjugate[
            Cos[θ]] Sin[θ] + 
          0.125 E^(-I ϕ + I Conjugate[ϕ]) Conjugate[
            Sin[θ]] Sin[θ]))}};

upF = {{0.375, -0.225754 + 0.0998109 I, 
    0.125, -0.0752515 - 0.0998109 I}, {-0.225754 - 0.0998109 I, 
    0.375, -0.0752515 + 0.0998109 I, 
    0.125}, {0.125, -0.0752515 - 0.0998109 I, 
    0.125, -0.0752515 - 0.0998109 I}, {-0.0752515 + 0.0998109 I, 
    0.125, -0.0752515 + 0.0998109 I, 0.125}};

p1 = KroneckerProduct[{Cos[θ], Exp[I ϕ]*Sin[θ]}, 
   Conjugate[{Cos[θ], Exp[I ϕ]*Sin[θ]}]];
p2 = KroneckerProduct[{Exp[-I ϕ]*Sin[θ], -Cos[θ]}, 
   Conjugate[{Exp[-I ϕ]*Sin[θ], -Cos[θ]}]];
(*dTraceSystem[upF,{2},2]*)
upF1 = dTraceSystem[
   Flatten[Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p1}}], 
     1] . upF . 
    Flatten[Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p1}}], 
     1], {2}, 2];
upF2 = dTraceSystem[
   Flatten[Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p2}}], 
     1] . upF . 
    Flatten[Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p2}}], 
     1], {2}, 2];
probM1 = 
  Tr[Flatten[Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p1}}], 
     1] . upF . 
    Flatten[Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p1}}], 
     1]];
probM2 = 
  Tr[Flatten[Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p2}}], 
     1] . upF . 
    Flatten[Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p2}}], 
     1]];
(*test1=upF1/probM1
test2=upF2/probM2*)
oop = Eigenvalues[test1 + 10^-15 IdentityMatrix[Length[test1]]];
entropyOop = -Sum[oop[[j]] Log[2, oop[[j]]], {j, 1, Length[oop]}];
(*ComplexExpand[entropyOop[[1]]]*)
oop1 = Eigenvalues[test2 + 10^-15 IdentityMatrix[Length[test2]]];
entropyOop1 = -Sum[oop1[[j]] Log[2, oop1[[j]]], {j, 1, Length[oop1]}];

Main change:

NMinimize[{probM1*entropyOop + probM2*entropyOop1 // 
   Re,  (* <-- NOTE WELL *)
  {θ ∈ Reals, θ <= 
    2*π, ϕ ∈ Reals, 
   0 <= ϕ <= 2*π}}, {θ, ϕ}]
(*  {0.27596, {θ -> -2.35619, ϕ -> 1.5708}}  *)

Essentially all I did was to apply Re to the objective function. Then no errors and a real result. I also got rid of the Simplify[ComplexExpand[...]] since that seems a waste of time in a numerical function, especially since the OP reports it wasn't working.

The problem is so complicated that I didn't want to invest a lot of time into investigating and understanding it. I poked at it this way: I conditionally applied Re to see what happens. Without the ?NumericQ protection in obj below I got an error as described in the OP. With ?NumericQ protection, I discovered something about NMinimize that I had not known: it tolerates some complex results in an objective function. It should not be surprising that the result is different. Different methods are used. Use Method -> "NelderMead" below to reproduce the result above.

obj[θ_?NumericQ, ϕ_?NumericQ] := Replace[
   probM1*entropyOop + probM2*entropyOop1,
   {z_Complex /; Im[z]/Abs[z] < 1.5*10^-15 :> Re@z,
    z_Complex :> (Print[{θ, ϕ} -> z]; z)}
   ];
NMinimize[
 {obj[θ, ϕ], {θ ∈ Reals, θ <= 2*π, ϕ ∈ Reals, 0 <= ϕ <= 2*π}},
 {θ, ϕ}]
(* results with the relatively largest imaginary part:
  {5.5481,  3.44761} -> 0.561011 + 8.65797*10^-16 I
  {5.5481,  3.44761} -> 0.561011 + 8.65797*10^-16 I
  {3.92965, 3.45605} -> 0.558183 + 8.52744*10^-16 I
  {3.92965, 3.45605} -> 0.558183 + 8.52744*10^-16 I

solution:
  {0.27596, {θ -> 5.49779, ϕ -> 4.71239}}
*)
$\endgroup$
3
  • $\begingroup$ I wonder why it can tolerate some complex results, but not others? $\endgroup$ Mar 9, 2022 at 16:10
  • $\begingroup$ @GaussStrife It probably rejects the complex numbers. Essentially treating the function as undefined there. When the function is complex valued everywhere then it complains. $\endgroup$
    – Michael E2
    Mar 9, 2022 at 16:39
  • $\begingroup$ I can see how that sort of behavior would be helpful if you needed to extremize a function of the form $\sqrt{f(x_i)}$ where $f(x_i)$ can become negative. $\endgroup$ Mar 9, 2022 at 17:47
1
$\begingroup$

First, I assume that your dTraceSystem is taken from "Partial Trace of a MultiquDit System".

If that's the case, then I have noticed the following potential issues with your code:

  • Replace by I ϕ in the definitions of p1 and p2
  • Turn probM1 and probM2 into evaluated expressions, not functions. Similarly for entropyOop and entropyOop1. (Also then use Simplify[...] instead of // Simplify)
  • Think about what are you trying to minimize? As far as I can see, the output of entropyOop1 may be complex.
  • Note that ComplexExpand assumes real variables (not real outputs!). For example, ComplexExpand[3^(I x)] results in Cos[x Log[3]] + I Sin[x Log[3]], which in general is complex even for real x. NMinimize needs real outputs.

Edit: here is the code I am essentially proposing:

upF = {{0.375, -0.225754 + 0.0998109 I, 
   0.125, -0.0752515 - 0.0998109 I}, {-0.225754 - 0.0998109 I, 
   0.375, -0.0752515 + 0.0998109 I, 
   0.125}, {0.125, -0.0752515 - 0.0998109 I, 
   0.125, -0.0752515 - 0.0998109 I}, {-0.0752515 + 0.0998109 I, 
   0.125, -0.0752515 + 0.0998109 I, 0.125}}

p1 = KroneckerProduct[{Cos[\[Theta]], Exp[I \[Phi]]*Sin[\[Theta]]}, 
   Conjugate[{Cos[\[Theta]], Exp[I \[Phi]]*Sin[\[Theta]]}]];
p2 = KroneckerProduct[{Exp[-I \[Phi]]*Sin[\[Theta]], -Cos[\[Theta]]}, 
   Conjugate[{Exp[-I \[Phi]]*Sin[\[Theta]], -Cos[\[Theta]]}]];

upF1 = dTraceSystem[
   Flatten[Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p1}}], 
     1] . upF . 
    Flatten[Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p1}}], 
     1], {2}, 2];
upF2 = dTraceSystem[
   Flatten[Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p2}}], 
     1] . upF . 
    Flatten[Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p2}}], 
     1], {2}, 2];

Clear[probM1]
probM1 = Simplify[
   ComplexExpand[
    Tr[Flatten[
       Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p1}}], 1] . 
      upF . Flatten[
       Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p1}}], 1]]]];
Clear[probM2]
probM2 = Simplify[
   ComplexExpand[
    Tr[Flatten[
       Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p2}}], 1] . 
      upF . Flatten[
       Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p2}}], 1]]]];
test1 = dTraceSystem[upF1, {2}, 2]/probM1;
test2 = dTraceSystem[upF2, {2}, 2]/probM2;
oop = Eigenvalues[test1 + 10^-15 IdentityMatrix[Length[test1]]];
Clear[entropyOop]
entropyOop = 
  Simplify[ComplexExpand[-Sum[
      oop[[j]] Log[2, oop[[j]]], {j, 1, Length[oop]}]]];
oop1 = Eigenvalues[test2 + 10^-15 IdentityMatrix[Length[test2]]];
Clear[entropyOop1]
entropyOop1 = 
  Simplify[ComplexExpand[-Sum[
      oop1[[j]] Log[2, oop1[[j]]], {j, 1, Length[oop1]}]]];
oop2 = Eigenvalues[
   dTraceSystem[upF, {2}, 2] + 
    10^-15 IdentityMatrix[Length[dTraceSystem[upF, {2}, 2]]]];
entropyOop2 = -Sum[oop2[[j]] Log[2, oop2[[j]]], {j, 1, Length[oop2]}]

NMinimize[
 probM1*entropyOop + 
  probM2*entropyOop1, {\[Theta], \[Phi]} \[Element] 
  Rectangle[{0, 0}, {2 \[Pi], 2 \[Pi]}]]

The issue is, for some values of theta and phi, this gives complex numbers. Specifically, if I use probM1*entropyOop + probM2*entropyOop1 /. \[Theta] -> 6.201806522051993 /. \[Phi] -> 5.22608549188592, I get 2.93718617402614*10^-17 + 2.29556419332293*10^-16 I. You should check each step of calculation and see if it's what you would expect. For example, oop, oop1, and oop2 look like eigenvalues of the reduced density matrix - however they are sometimes not real (maybe due to numerical errors), and seem to be close to 1 for both oop and oop1. Note that if you then apply -x*Log[x], you get something close to zero, but with a numerical error.

Edit: So after having updated the code to the above, and attempting both Re and Chop to handle the imaginary parts, I am still getting either not a number or not a scalar value

Edit 2 (by mszynisz):

I have rewritten the current code by OP, maybe this is something closer to what you want? Another issue is that the final answer seems to be always equal to 1 (see the table of values I printed at the end...). Please check each step with some analytical calculations for some example values of Theta and Phi.

upF = {{0.375, -0.225754 + 0.0998109 I, 
   0.125, -0.0752515 - 0.0998109 I}, {-0.225754 - 0.0998109 I, 
   0.375, -0.0752515 + 0.0998109 I, 
   0.125}, {0.125, -0.0752515 - 0.0998109 I, 
   0.125, -0.0752515 - 0.0998109 I}, {-0.0752515 + 0.0998109 I, 
   0.125, -0.0752515 + 0.0998109 I, 0.125}}

p1 = KroneckerProduct[{Cos[\[Theta]], Exp[I \[Phi]]*Sin[\[Theta]]}, 
   Conjugate[{Cos[\[Theta]], Exp[I \[Phi]]*Sin[\[Theta]]}]];
p2 = KroneckerProduct[{Exp[-I \[Phi]]*Sin[\[Theta]], -Cos[\[Theta]]}, 
   Conjugate[{Exp[-I \[Phi]]*Sin[\[Theta]], -Cos[\[Theta]]}]];
dTraceSystem[upF, {2}, 2]

upF1 = dTraceSystem[
   Flatten[Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p1}}], 
     1] . upF . 
    Flatten[Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p1}}], 
     1], {2}, 2];
upF2 = dTraceSystem[
   Flatten[Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p2}}], 
     1] . upF . 
    Flatten[Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p2}}], 
     1], {2}, 2];
probM1 = Simplify[
   ComplexExpand[
    Tr[Flatten[
       Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p1}}], 1] . 
      upF . Flatten[
       Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p1}}], 1]]]];
probM2 = Simplify[
   ComplexExpand[
    Tr[Flatten[
       Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p2}}], 1] . 
      upF . Flatten[
       Map[KroneckerProduct @@ # &, {{IdentityMatrix[2], p2}}], 1]]]];
test1 = upF1/probM1
test2 = upF2/probM2

test1 = Simplify[test1, 
  Assumptions -> {\[Theta], \[Phi]} \[Element] Reals]
test2 = Simplify[test2, 
  Assumptions -> {\[Theta], \[Phi]} \[Element] Reals]

oop = Eigenvalues[test1 + 10^-15 IdentityMatrix[Length[test1]]];
entropyOop = 
 Simplify[-Sum[oop[[j]] Log[2, oop[[j]]], {j, 1, Length[oop]}], 
  Assumptions -> {\[Theta], \[Phi]} \[Element] Reals]
oop1 = Eigenvalues[test2 + 10^-15 IdentityMatrix[Length[test2]]];
entropyOop1 = 
 Simplify[-Sum[oop1[[j]] Log[2, oop1[[j]]], {j, 1, Length[oop1]}], 
  Assumptions -> {\[Theta], \[Phi]} \[Element] Reals]

finexpr = 
 Simplify[probM1*entropyOop + probM2*entropyOop1, 
  Assumptions -> {\[Theta], \[Phi]} \[Element] Reals]

finexpr2 = 
 Simplify[Chop[ComplexExpand[finexpr]], 
  Assumptions -> {\[Theta], \[Phi]} \[Element] Reals]

NMinimize[{Re[
   finexpr2], {\[Theta] \[Element] Reals, \[Theta] <= 
    2*\[Pi], \[Phi] \[Element] Reals, 
   0 <= \[Phi] <= 2*\[Pi]}}, {\[Theta], \[Phi]}]

Table[Chop[finexpr2], {\[Theta], 0, 2 \[Pi], 0.1}, {\[Phi], 0, 
  2 \[Pi], 0.1}]
$\endgroup$
16
  • 1
    $\begingroup$ Presumably, OP wants to simplify their expression first and then run the numerics? (Why else would the simplification be in their code) $\endgroup$
    – mszynisz
    Feb 24, 2022 at 15:26
  • 1
    $\begingroup$ Surely simplifying before numerics would potentially make the numerics faster if you are dealing with a huge expression. Consider the example from Mathematica help, where 1/(3 (1 + x)) - (-1 + 2 x)/(6 (1 - x + x^2)) + 2/( 3 (1 + 1/3 (-1 + 2 x)^2)) simplifies to 1/(1 + x^3). If you plug the first expression into a numerical method, it will take more time than the second one. $\endgroup$
    – mszynisz
    Feb 24, 2022 at 15:47
  • 1
    $\begingroup$ @N.J.Evans don't know what to say. If I sub in values for theta and phi, I get an unnormalised density matrix, that normalizes just fine and obeys the properties it is meant to. I just don't know how to put it into a minimize function and get it to run. $\endgroup$ Feb 24, 2022 at 18:03
  • 1
    $\begingroup$ For example, how do I, using built in functions like Tr, minimize them with respect to certain variables present in the input of trace which is itself an expression? $\endgroup$ Feb 24, 2022 at 18:13
  • 1
    $\begingroup$ @GaussStrife I updated my answer above. In general, I believe you may be suffering from some numerical issues. Seems like the eigenvalues of your reduced density matrix are close to 1, plus some minuscule imaginary part, which then causes your entropy to be complex. Check each output and see if this is what you expect. If you need to remove a small complex part from an eigenvalue, either use Re[] or Chop[] (see their corresponding documentation). $\endgroup$
    – mszynisz
    Feb 25, 2022 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.