[PYTHON] I tried to solve a combination optimization problem with Qiskit

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.

Problem to solve

The problem to solve is the same as before. Find the combination of $ q (0) $ and $ q (1) $ that minimizes the following Hamiltonian $ H . $ H = 1 - q(0) - q(1) $$ Note that $ q (0) $ and $ q (1) $ are either 0 or 1. In this problem setting, Hamiltonian $ H = -1 $ is the minimum value when $ q (0) = q (1) = 1 $.

Supplementary information

By the way, Hamiltonian has the following values for all combinations of $ q (0) and q (1) $.

About the execution environment

Run with Google Colabratory. The version of Qiskit is "0.14.1".

Source code

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!

Impressions of implementing

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.

Finally

Please let me know if there is a better implementation in Qiskit. I personally think it's not very smart.

Recommended Posts

I tried to solve a combination optimization problem with Qiskit
I tried to solve the problem with Python Vol.1
I tried to solve TSP with QAOA
I tried to solve the virtual machine placement optimization problem (simple version) with blueqat
I wanted to solve the ABC164 A ~ D problem with Python
I tried to create a table only with Django
I tried to draw a route map with Python
I tried to solve the soma cube with python
I tried to automatically generate a password with Python3
I tried to step through Bayesian optimization. (With examples)
I tried to solve AOJ's number theory with Python
[Keras] I tried to solve a donut-type region classification problem by machine learning [Study]
I tried to automatically create a report with Markov chain
I tried to get started with Hy ・ Define a class
I tried to sort a random FizzBuzz column with bubble sort.
I tried to divide with a deep learning language model
The 15th offline real-time I tried to solve the problem of how to write with python
[5th] I tried to make a certain authenticator-like tool with python
I tried to solve the ant book beginner's edition with python
[2nd] I tried to make a certain authenticator-like tool with python
[3rd] I tried to make a certain authenticator-like tool with python
[Python] A memo that I tried to get started with asyncio
I tried running platypus which can solve a little optimization problem-Part 2
I tried to create a list of prime numbers with python
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
I tried to create Bulls and Cows with a shell program
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
I tried to solve the shift scheduling problem by various methods
I tried to make a strange quote for Jojo with LSTM
I tried to make a mechanism of exclusive control with Go
I tried to create a linebot (implementation)
I tried to create a linebot (preparation)
I tried to visualize AutoEncoder with TensorFlow
I tried to get started with Hy
I wanted to solve ABC160 with Python
I tried to let optuna solve Sudoku
I tried a functional language with Python
[AtCoder] Solve ABC1 ~ 100 A problem with Python
I tried to implement CVAE with PyTorch
I tried to make a Web API
AtCoder Green tried to solve with Go
I implemented NSGA-II, a multi-objective optimization problem.
I wanted to solve ABC172 with Python
I tried "Implementing a genetic algorithm (GA) in python to solve the traveling salesman problem (TSP)"
I tried to introduce a serverless chatbot linked with Rakuten API to Teams
I tried to communicate with a remote server by Socket communication with Python.
I tried to implement a blockchain that actually works with about 170 lines
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
I tried to express sadness and joy with the stable marriage problem.
How to write offline real time I tried to solve E11 with python
I tried to create a plug-in with HULFT IoT Edge Streaming [Development] (2/3)
I tried to make a calculator with Tkinter so I will write it
Try to solve the traveling salesman problem with a genetic algorithm (Theory)
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
I tried to draw a system configuration diagram with Diagrams on Docker
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I tried to create a plug-in with HULFT IoT Edge Streaming [Execution] (3/3)
I tried to discriminate a 6-digit number with a number discrimination application made with python