In this article, we will review QAOA. We will solve a combinatorial optimization problem with quantum gates. First, let's check the calculation with blueqat cloud.
from bqcloud import load_api
from bqcloud import Device
from blueqat import Circuit
import math
#import api key
api = load_api()
#let's make a simple entanglement using rxx gate for trial
qc = Circuit().rxx(math.pi/2)[0,1]
#simulator
qc.m[:].run(shots=100)
Counter({'00': 46, '11': 54})
#post task to IonQ on the cloud
qc = Circuit().rxx(math.pi/2)[0,1]
task = api.execute(qc, Device.IonQDevice, 10)
#task queued
print(task.status())
Status.QUEUED
#get the result
result = task.wait(timeout=3600)
if result:
print(result.shots())
else:
print("timeout")
Counter({'00': 5, '11': 5})
The calculation results came out successfully. Now, let's try QAOA on IonQ.
Let's start with a simple equation and consider the three-vertex maxcut problem.
Let's consider three vertices and set the edge weights all to 1.
For the formulation, we will consider the Ising model. We will create an equation called the Hamiltonian. Assign qubits to the vertices, each of which takes the value 1 or -1.
The formulation is,
The coefficients in front of Z correspond to the weights of the edges, respectively.
Next, we need to prepare one more formulation. That is a mixer to search for a solution. We usually use X as the most simple formulation.
Another thing that needs to be prepared in the above formulation is the superposition state, which is the eigenstate of
The quantum state is then time-evolved. The equation for time evolution is based on the operation of gradually replacing the Hamiltonian with respect to time t. The time evolution is
The Hamiltonian to be applied is the process of swapping the above two Hamiltonians.
We use time evolution operator
Finally the quantum circuit to solve the qaoa in 1step p=1 is
|0> --H--RZZ(beta)-------------RZZ(beta)--RX(gamma)--
| |
|0> --H--RZZ(beta)--RZZ(beta)--|----------RX(gamma)--
| |
|0> --H-------------RZZ(beta)--RZZ(beta)--RX(gamma)--
To optimize beta and gamma, we can get solution for qaoa.
blueqat sdk has a function to create the qaoa circuit.
We call this circuit Ansatz.
from blueqat.pauli import Z
from blueqat import vqe
hamiltonian = Z[0]*Z[1] + Z[1]*Z[2] + Z[2]*Z[0]
result = vqe.Vqe(vqe.QaoaAnsatz(hamiltonian, 1)).run()
print(result.most_common(12))
(((1, 0, 0), 0.162033015432909), ((1, 1, 0), 0.162033015432909), ((0, 0, 1), 0.162033015432909), ((0, 1, 1), 0.162033015432909), ((0, 1, 0), 0.16203301543290893), ((1, 0, 1), 0.16203301543290893), ((0, 0, 0), 0.013900953701272978), ((1, 1, 1), 0.013900953701272978))
It has a function to check the quantum circuit
result.circuit
Circuit(3).h[:].cx[0, 1].rz(-25.930489083659825)[1].cx[0, 1].cx[0, 2].rz(-25.930489083659825)[2].cx[0, 2].cx[1, 2].rz(-25.930489083659825)[2].cx[1, 2].rx(0.4578213663591716)[:]
rzz is usually rewrited as combination of cx,rz,cx.
beta = -25.930489083659825
gamma = 0.4578213663591716
Circuit(3).h[:].cx[0, 1].rz(beta)[1].cx[0, 1].cx[0, 2].rz(beta)[2].cx[0, 2].cx[1, 2].rz(beta)[2].cx[1, 2].rx(gamma)[:].run()
array([-0.02214869+0.11580323j, 0.27513225-0.29382863j,
0.27513225-0.29382863j, 0.27513225-0.29382863j,
0.27513225-0.29382863j, 0.27513225-0.29382863j,
0.27513225-0.29382863j, -0.02214869+0.11580323j])
If we check the rzz form, we have the same state vector.
Circuit(3).h[:].rzz(beta)[0,1].rzz(beta)[0,2].rzz(beta)[1,2].rx(gamma)[:].run()
array([-0.02214869+0.11580323j, 0.27513225-0.29382863j,
0.27513225-0.29382863j, 0.27513225-0.29382863j,
0.27513225-0.29382863j, 0.27513225-0.29382863j,
0.27513225-0.29382863j, -0.02214869+0.11580323j])
Basically IonQ has rzz/rxx basic gate set for trapped ion. For qaoa rzz is much more suitable and we can write the quantum cirucit shorter with these gate set. Let's apply for IonQ device.
task = api.execute(Circuit(3).h[:].rzz(beta)[0,1].rzz(beta)[0,2].rzz(beta)[1,2].rx(gamma)[:], Device.IonQDevice, 100)
#get the result
result = task.wait(timeout=3600)
if result:
print(result.shots())
else:
print("timeout")
Counter({'110': 19, '101': 19, '100': 18, '001': 16, '011': 13, '010': 12, '111': 2, '000': 1})
Now we have results from IonQ device precisely. It is quite precise than expected.