I implemented the combination optimization problem in blueqat before (see [here] for the article on blueqat [https://qiita.com/Tusnori/items/71ea8d01fb4892e0eae9)), but this time I would like to implement it in Qiskit. Since this article is intended to confirm the implementation method, I would like to deal with the simple combination optimization problem itself.
The problem to solve is the same as before. Find the combination of $ q (0) $ and $ q (1) $ that minimizes the following Hamiltonian $ H
By the way, Hamiltonian has the following values for all combinations of $ q (0) and q (1) $.
Run with Google Colabratory. The version of Qiskit is "0.14.1".
Install Qiskit
pip install qiskit
Import required packages for Qiskit
from qiskit import BasicAer
from qiskit.aqua.algorithms import QAOA
from qiskit.optimization.algorithms import MinimumEigenOptimizer
from qiskit.optimization import QuadraticProgram
Definition of variable, Hamiltonian
qubo = QuadraticProgram()
#Binary variable definition
qubo.binary_var('q0')
qubo.binary_var('q1')
#Hamiltonian definition
qubo.minimize(linear=[-1,-1],constant=1.0)
print(qubo.export_as_lp_string())
The definition result of the variable Hamiltonian is as follows.
\ This file has been generated by DOcplex
\ ENCODING=ISO-8859-1
\Problem name: CPLEX
Minimize
obj: - q0 - q1 + 1
Subject To
Bounds
0 <= q0 <= 1
0 <= q1 <= 1
Binaries
q0 q1
End
Solve the Hamiltonian defined in QAOA.
#Initialization of MinimumEigensolver
qaoa_mes = QAOA(quantum_instance=BasicAer.get_backend('statevector_simulator'))
#Creating an Optimizer
qaoa = MinimumEigenOptimizer(qaoa_mes)
#QAOA calculation/Result output
qaoa_result = qaoa.solve(qubo)
print("QAOA result:")
print(qaoa_result)
The execution result is as follows.
QAOA result:
x=[1.0,1.0], fval=-1.0
I was able to find a solution successfully!
The impression I implemented is "difficult to write ...". What is particularly strange is the Hamiltonian definition. I thought I could handle the binary variables q0 and q1 like Sympy's Symbol, but that doesn't seem to be the case.
Please let me know if there is a better implementation in Qiskit. I personally think it's not very smart.
Recommended Posts