I've only recently started using density matrices in my work but I am confused with the following code that I have whether I am getting the right matrix:

def Hamiltonian(alpha,h):

    Sx = np.array([[0,1],[1,0]])
    Sy = np.array([[0,-1j],[1j,0]])
    Sz = np.array([[1,0],[0,-1]])
    I  = np.array([[1,0],[0,1]])

    H =  (alpha*np.kron(np.kron(Sx,Sx),I)) 
    H =+ (alpha*np.kron(np.kron(Sy,Sy),I)) 
    H =+ (alpha*np.kron(np.kron(I,Sx),Sx)) 
    H =+ (alpha*np.kron(np.kron(I,Sy),Sy)) 
    H =+ (h*np.kron(np.kron(I,Sz),I))

    return H

So the above gives me my Hamiltonian Function, where alpha is a real number and h is a magnetization parameter acting on one of my qubits.

I have tried the following:

H = Hamiltonian(1,0.5)

$$\begin{bmatrix} 0.5&0&0&0&0&0&0&0 \\ 0&0.5&0&0&0&0&0&0 \\ 0&0&-0.5&0&0&0&0&0 \\ 0&0&0&-0.5&0&0&0&0 \\ 0&0&0&0&0.5&0&0&0 \\ 0&0&0&0&0&0.5&0&0 \\ 0&0&0&0&0&0&-0.5&0 \\ 0&0&0&0&0&0&0&-0.5 \end{bmatrix}$$

Why is it diagonal?

  • $\begingroup$ No problem! If there's anything you don't like about them, feel free to rollback. It's also possible for me to format the tables so they display 'natively' (as opposed to being images) if you want? Also, welcome to Quantum Computing SE! $\endgroup$
    – Mithrandir24601
    Sep 28, 2018 at 16:22
  • $\begingroup$ Yeah I'd appreciate if you did that thank you I'm unfamiliar how. Thanks a mil $\endgroup$ Sep 28, 2018 at 16:23
  • $\begingroup$ It doesn't make sense to make a vector in the Hilbert space $(\mathbb{C}^2)^{\otimes 3}$ by $\mid \psi (\alpha,h) \rangle = \sum \lambda_i (\alpha,h) \mid i \rangle$ because you can always permute the eigenvalues to change the state. So you aren't getting something well defined. You have to say something else about how $\psi$ is constructed. $\mid i \rangle$ is in computational basis using binary expansion of $i \in [0,7]$ for which up/down. $\endgroup$
    – AHusain
    Sep 28, 2018 at 22:18
  • $\begingroup$ I dont understand what you mean $\endgroup$ Sep 29, 2018 at 12:00
  • $\begingroup$ You make the state $\rho$ with the eigenvalues the program spit out as $w$. But nothing stops the program from giving you $(-.5,-.5,-.5,-.5,.5,.5,.5,.5)$ instead and permuting the list of eigenvectors accordingly so they match. It isn't a vector in a Hilbert space. w is just an unordered multiset of eigenvalues. $\endgroup$
    – AHusain
    Sep 29, 2018 at 19:57

1 Answer 1


Your matrix is diagonal because the + signs are on the wrong sides of the equals signs. The code below will give you the correct matrix:


x=[0  1;  1 0 ];
y=[0 -1i; 1i 0];
z=[1  0;  0 -1];

H = alpha*kron(kron(x,x),I)+...

Here is the result:

enter image description here

The eigenvectors and eigenvlaues are much more complicated than what you have.

If you don't have Octave, these commands will install it, then open it:

sudo apt-get install octave
  • $\begingroup$ Why does that return a different result as opposed to my numpy version? Thank you for your answer I will try it and get back to you $\endgroup$ Sep 30, 2018 at 0:04
  • $\begingroup$ I fixed my numpy array to give this result so thanks for that. I can't upvote without 15 rep and unfortunately I didn't get a full answer yet so I can't accept it I appreciate the help $\endgroup$ Sep 30, 2018 at 10:53
  • $\begingroup$ I'm looking for an overall explanation of the physics as to what I need to get the density matrix. So far I've been using the outer product of the eigenvalues and I am not sure if this is right I need clarity $\endgroup$ Sep 30, 2018 at 11:04
  • $\begingroup$ That's fair enough man I'll do that now and you can answer it there thanks a mil $\endgroup$ Sep 30, 2018 at 11:09
  • $\begingroup$ I have created the new post $\endgroup$ Sep 30, 2018 at 11:26

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.