Im trying to get started with QAOA. I tried to program a simple example for testing my understanding. The goal is to maximize $z^TV$ with $V \in \mathbb{R}^{nx1},z \in ${$-1,1$}$^n$. I tried formulating the problem Hamiltonian. With the canonical expansion $x_i = 1/2(1-z_i)$, the Cost function transforms from $C(z) = \sum_{i=1}^n v_iz_i$ into $C(x) = \sum_{i=1}^n v_i(-1)^{x_i}$.
Using the relation $Z\ket{x}=(-1)^x\ket{x}, x\in ${$0,1$}, the problem hamiltonian results in $C(x)\ket{x} = C \ket{x} = \sum_{i=1}^n v_iZ_i \ket{x_1...x_n}$. (I don't know why it can't display $\ket{}$)
However, If I implement this with Qiskit and QAOA, the results are not correct. As example, I consider $v=(1,1,0)$. The answer to the optimization problem should be obviously $max C(z)=2$. If I implement this, the histogram looks like this:
The $100$ equals after canonical inverse $-111$. This state is not the correct solution as it yields C(z=(-1 1 1)) = 0. I wonder where I made a mistake and Im not quite sure if my approach for this is correct. On the programming side, I implemented the cost function as the following
def vM_product(x,v):
"""
Given a 2n bit string x and an nxn Matrix A, the function returns the
required Vector-Matrix-Vector product
Args:
x:str
solution bitstring
v:Problem Vector
Returns:
obj: float
"""
n = v.size
z_T = np.fromstring(x,dtype='u1')-ord('0') #import string in correct datatype
z_T = -1*z_T #inverse canonical trafo
z_T[z_T==0]=1
z_T.reshape(1,n)
obj = -z_T.dot(v).astype(int) #include minus because optimizer we use minimizes obj
return obj
and a simple implementation of the problem hamiltonian with
# problem unitary
for i in range(n):
qc.rz(2*v[i].astype(int)* gamma[irep], i)
qc.barrier()
The rest of the code follows the sample of the Qiskit implementation in https://qiskit.org/textbook/ch-applications/qaoa.html.
Any help is appreciated. Thanks in advance!