Codercise N.2.2a proposed solution bug? (SPOILER)

After getting my solution accepted, I looked at the suggested answer, and it looked dubious to me:

eigenvectors = np.linalg.eig(B)[1]
eigen_projectors = np.array([np.outer(vector,np.conj(vector)) for vector in eigenvectors])
return eigen_projectors

I think this doesn’t do what it’s actually supposed to do. For convenience, the task is to populate eigen_projectors with projectors to the respective eigenspaces determined by the observable B. However, I have noticed the following:

  • First, B should be assumed to only be Hermitian, and so np.linalg.eigh should be used.
  • The other issue is that for v in a iterates through the row vectors of a matrix, but the NumPy API docs note that the eigenvectors are the column vectors. Instead, the more cluttered ...np.conj(eigenvectors[:,i])) for i in range(eigenvectors.shape[1]) should be used; eigenvectors.shape[1] can be replaced with len(eigenvectors) at the cost of semantic transparency.
    • The row vectors will still form projectors under np.outer, and even an orthonormal basis, but they are not themselves related to the eigenspaces (they encode the linear combination of the column basis vectors that recovers the original matrix B).

Hi, @jhanschoo
Thanks for your question.

  1. Both np.linalg.eigh and np.linalg.eig can be used to calculate the eigenvectors for this hermitian matrix. np.linalg.eigh is optimized to perform with hermitian matrices, but they both give correct answers (I checked). Recall that the eigenvectors might look different due to global phases.
  2. I verified our code and it outputs the correct answer; we were lucky because of the way np.linalg.eig outputs the result, different from np.linalg.eigh. However, I see your point and I agree with you in that according to the documentation, the eigenvectors are the columns. This is a mistake in our code, nice catch! :partying_face:

I will correct it. Thank you!

1 Like