from bqcloud import load_api
from bqcloud import Device
from blueqat import Circuit
import math
#import api key
api = load_api()
Copy
#let's make a simple entanglement using rxx gate for trial
qc = Circuit().rxx(math.pi/2)[0,1]
Copy
#simulator
qc.m[:].run(shots=100)
Counter({'00': 46, '11': 54})
Copy
#post task to IonQ on the cloud
qc = Circuit().rxx(math.pi/2)[0,1]
task = api.execute(qc, Device.IonQDevice, 10)
Copy
#task queued
print(task.status())
Status.QUEUED
Copy
#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,
Hc=Z0Z1+Z1Z2+Z2Z0
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.
Hinit=X0+X1+X2
Another thing that needs to be prepared in the above formulation is the superposition state, which is the eigenstate of Hinit.
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
∣ψt+1⟩=U∣ψt⟩
The Hamiltonian to be applied is the process of swapping the above two Hamiltonians.
Ht=s∗Hinit+(1−s)∗Hc
We use time evolution operator e−iHt
Finally the quantum circuit to solve the qaoa in 1step p=1 is
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.
Copy
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))
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.