In OpenFermion you can create a Hamiltonian in terms of creation and annihilation pretty easily: ham = ((FermionOperator('1^ 1', .5)) + (FermionOperator('2^ 2', .25)))

And getting the eigenvalues of the Hamiltonian is pretty straightforward as well: vals = eigenspectrum(ham)

But I don't understand how to get the eigenvectors of the Hamiltonian. FermionOperator doesn't return a numpy object so I can't use usual linear algebra libraries. What am I missing?


1 Answer 1


The following works:

sparse_mat = openfermion.get_sparse_operator(ham, n_qubits=n) # type: scipy.sparse.csr_matrix
mat = sparse_mat.toarray() # type: np.ndarray
w, v = numpy.linalg.eigh(mat)

Then w will contain the eigenvalues and the columns of v will contain the eigenvectors. Setting the n_qubits=n parameter will result in a matrix that gets padded up to $2^n \times 2^n$ which is important if you're going to do further matrix manipulations or algebra involving the matrix representing your Hamiltonian.

And of course, for efficiency reasons you may choose not to cast the sparse operator to a numpy array but then you will end up using sparse matrix functions from scipy, for example scipy.sparse.linalg.eigsh to find some subset of eigenvalues/eigenvectors of a sparse matrix.


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.