4
$\begingroup$

The projection operator $P$ is defined in quantum mechanics as $$ P=\sum_i^n \vert\psi_i\rangle \langle \psi_i\vert, $$

where $\vert\psi_i\rangle$ is a column vector and $(\langle \psi_i\vert=(\vert\psi_i\rangle)^*)^T$ is a row vector. The $\vert\psi_i\rangle$ that I am using are eigenvectors of a Hermitian matrix.

So here is what I am using:

{energy, states} = Eigensystem[H];

Reenergy = N[Re[energy]];

NormalizedOrderedStates = states[[Ordering[Reenergy]]];

psiket = Map[ConjugateTranspose[{#}] &,NormalizedOrderedStates[[1 ;;n]]]; (*This step turns the states into nx1 matrices*)

psibra = Map[ {#} &, NormalizedOrderedStates[[1 ;;n]]]; (*This step turns the states into 1xn matrices*)

Proj = Sum[psiket[[i]].psibra[[i]], {i, 1, n}];

The problem that I am having is that $H$ can be somewhere from 5000 by 5000 to 50000 by 50000 matrix. The above code is too slow and uses too much memory and in general doesn't feel right. Is there a better way to achieve this?

$\endgroup$
5
  • 2
    $\begingroup$ Look for KroneckerProductor TensorProduct $\endgroup$ Commented Jul 21, 2023 at 14:39
  • $\begingroup$ Try to Import and ImageAdjust a typical 4K 50 MP photo with Mathematica to get a feeling what is possible and impossible with an interpreting language with PC-range computers. $\endgroup$
    – Roland F
    Commented Jul 21, 2023 at 14:56
  • $\begingroup$ Do you really need the projector itself, or can you simplify the formula where the projector is being used? The latter is often much more practical, especially with large-dimensional system as you're having. $\endgroup$
    – Roman
    Commented Jul 22, 2023 at 9:26
  • $\begingroup$ @RolandF I am fine with the code running for a few days but not a few weeks on my PC. That is the kind of possibility I am aiming for. $\endgroup$
    – wooohooo
    Commented Jul 22, 2023 at 13:34
  • $\begingroup$ @Roman Unfortunately I do need the full projection operator $\endgroup$
    – wooohooo
    Commented Jul 22, 2023 at 13:35

1 Answer 1

3
$\begingroup$

If I understand it right:

size = 500; H = RandomComplex[1 + I, {size, size}];
n = size; (* set n = size as an example. *)

{energysorted, statessorted} = Eigensystem[H]\[Transpose] // Sort // Transpose;

(bra = statessorted[[;; n]];
 ket = Conjugate@bra;
 myProj = ket\[Transpose] . bra;) // AbsoluteTiming
(* {0.0158231, Null} *)

Key point to understand the code:

  1. Check carefully about how Sort works by reading the Details section of its document.

  2. Forget about column and row when using Mathematica to deal with matrix.

For comparison:

{energy, states} = Eigensystem[H];

(Reenergy = N[Re[energy]];
  
  NormalizedOrderedStates = states[[Ordering[Reenergy]]];
  
  psiket = Map[ConjugateTranspose[{#}] &, NormalizedOrderedStates[[1 ;; n]]]; 
  
  psibra = Map[{#} &, NormalizedOrderedStates[[1 ;; n]]]; 
  
  Proj = Sum[psiket[[i]] . psibra[[i]], {i, 1, n}];) // AbsoluteTiming
(* {3.67195, Null} *)

As we can see, it's a 200x speed-up.

Because of numeric error, the matrix won't be exactly the same, but:

myProj - Proj // Chop // Flatten // Union
(* {0} *)

BTW, being poor in math, I'm not sure about the reason, but the sorting step seems to be redundant for n = size:

{energy, states} = Eigensystem[H];

(bra = states[[;; n]];
  ket = Conjugate@bra;
  myProj2 = ket\[Transpose] . bra;) // AbsoluteTiming

myProj - myProj2 // Chop // Flatten // Union
(* {0} *)
$\endgroup$
6
  • $\begingroup$ I am not sure I understand {energysorted, statessorted} = Eigensystem[H] // Sort;. It appears that the energysorted is not sorted by the value but is still ordered by their absolute value. I also did not find the detail part of the document (reference.wolfram.com/language/ref/Sort.html) mentioning sorting this kind of list. Could you be more specific? $\endgroup$
    – wooohooo
    Commented Jul 22, 2023 at 13:29
  • $\begingroup$ @wooohooo "Sort orders complex numbers by their real parts, and in the event of a tie, by the absolute values of their imaginary parts. If a tie persists, they are ordered by their imaginary parts.", and "Sort usually orders expressions by putting shorter ones first, and then comparing parts in a depth-first manner. "<- this means that when dealing with regular lists, Sort sorts with the first element as the first step, try e.g. Sort[{{5., a, b}, {2., c, d}}]. $\endgroup$
    – xzczd
    Commented Jul 22, 2023 at 13:32
  • $\begingroup$ I am sorting the states according to their energy. For example Sort[{{1, -1, -3, 3, 4}, {{a, a, a, a}, {b, b, b, b}, {c, c, c, c}, {d, d, d, d}, {e, e, e, e}}}] should give me the states {c,b,a,d,e} but on my PC it simply returns the input. $\endgroup$
    – wooohooo
    Commented Jul 22, 2023 at 13:45
  • $\begingroup$ @wooohooo Oh, indeed I didn't sort it right… It's interesting that the sorting step doesn't seem to influence the result when n = size . I've corrected the code, see my edit. $\endgroup$
    – xzczd
    Commented Jul 22, 2023 at 13:49
  • 1
    $\begingroup$ As for the redundant sorting step that is true when the sum goes over ALL the eigenstates. In the problem that I am working with, I need half of the eigenstates corresponding to the lower energy section. In that case n=1/2 size. $\endgroup$
    – wooohooo
    Commented Jul 22, 2023 at 14:10

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.