I am a new user of Qiskit and I believe there is a simple answer to my question but I have had a very hard time finding a straightforward answer. I am trying to transform a given $3 \times 3$ (Hermitian) Hamiltonian matrix into a qubit operator and then use the built-in VQE solver for evaluating the minimum eigenvalue for said matrix.
I have been successful in doing something similar using the qiskit.chemistry.FermionicOperator
built-in class (see example below) but for this example the Hamiltonian is mapped to a Fermionic one-body qubit Hamiltonian which has $2^n$ eigenvalues instead of $n$ eigenvalues (where $n=3$ for this case). I am hoping there is a simple way to directly map a matrix to a qubit operator. My vague understanding is I need to write the matrix as a sum of weighted Pauli operators but I would expect there would exist some predefined function for doing this? (I have looked at the source code for the FermionicOperator
class but it did not help me much).
import numpy as np
import scipy
import h5py
from qiskit.aqua.algorithms import VQE, NumPyEigensolver
from qiskit.chemistry import FermionicOperator
from qiskit import Aer
backend = Aer.get_backend("qasm_simulator")
c1=1
c2=2
c3=3
ff=np.zeros((3,3))
ff[1,1]=2
ff[2,2]=1
n=3
Hamil=np.zeros((n,n))
Hamil[1,1]=c2
Hamil[2,2]=-c3/2+c2
Hamil[0,2]=np.sqrt(2)*c1
Hamil[1,2]=np.sqrt(2)*c1
Hamil[2,0]=np.sqrt(2)*c1
Hamil[2,1]=np.sqrt(2)*c1
vals,vecs=np.linalg.eig(Hamil)
print("Standard Eigenvalues: ")
print(vals)
Hamil_op = FermionicOperator(h1=Hamil)
Hamil_ops = Hamil_op.mapping(map_type='parity', threshold=1e-12)
result = NumPyEigensolver(Hamil_ops,k=int(2**n)).run()
print("Quibit Eigenvalues=")
print(result['eigenvalues'])
vqe = VQE(operator=Hamil_ops)
vqe_result = np.real(vqe.run(backend)['eigenvalue'])
print("VQE Eigenvalue")
print(vqe_result)
With result:
Standard Eigenvalues:
[-1.45426242 0.90078898 3.05347344]
Quibit Eigenvalues=
[-1.45426242+0.j -0.55347344+0.j 0. +0.j 0.90078898+0.j
1.59921102+0.j 2.5 +0.j 3.05347344+0.j 3.95426242+0.j]
VQE Eigenvalue
0.18608074335373637
NumPyEigensolver
class (at least not in a straighforward way that I can figure out). $\endgroup$