I am attempting to understand the post-aqua way that external qiskit libraries are supposed to be used for solving QUBOs. To do so I am trying to work with the toy problem of minimizing $f(x_1, x_2, x_3) = 2x_1x_2 + 3x_2x_3 - 4x_1x_3$, where $x_1,x_2,x_3 ∈\{0,1\}$. Looking at the migration guide I found code very much like the following:

from qiskit.algorithms import QAOA
from qiskit.algorithms.optimizers import COBYLA
from qiskit.opflow import Z, I
from qiskit import Aer, transpile
from qiskit.utils import QuantumInstance

# Define the problem Hamiltonian
observable = Z ^ I  # Example Hamiltonian (modify as per your problem)

# Define the QAOA instance with an optimizer and quantum instance
optimizer = COBYLA()  # You can choose a different optimizer
quantum_instance = QuantumInstance(Aer.get_backend('statevector_simulator'))

qaoa = QAOA(optimizer=optimizer, quantum_instance=quantum_instance)

# Compute the minimum eigenvalue using QAOA
result = qaoa.compute_minimum_eigenvalue(observable)

I imagine that, to an expert's eyes, it is trivial to replace the line observable = Z ^ I with code that corresponds to $2x_1x_2 + 3x_2x_3 - 4x_1x_3$.

The Question

I get a wide range of errors as I try different ways of doing this and a lot of tutorials online use deprecated code. A template solution would be greatly appreciated. Can someone show me how to solve this toy problem?


1 Answer 1


First of all, you will need to convert your problem into a QuadraticProgram

from qiskit_optimization import QuadraticProgram

problem = QuadraticProgram()

# 2x_1x_2 + 3x_2x_3 − 4x_1x_3
problem.minimize(quadratic={("x1", "x2"): 2, ("x2", "x3"): 3, ("x1", "x3"): -4})


Then you can use QAOA or any other SamplingMinimumEigensolver to solve it

from qiskit_algorithms import QAOA
from qiskit_algorithms.optimizers import COBYLA
from qiskit_optimization.algorithms import MinimumEigenOptimizer
from qiskit.primitives import Sampler

qaoa = QAOA(sampler=Sampler(), optimizer=COBYLA())
min_eigen_optimizer = MinimumEigenOptimizer(qaoa)

result = min_eigen_optimizer.solve(problem)

Note that, Qiskit optimization provides automatic conversion from a QuadraticProgram to an Ising Hamiltonian. So, you don't need to do this conversion by yourself. If, however, you want to get the corresponding Hamiltonian for a quadratic program, you can use QuadraticProgram.to_ising() method:

hamiltonian, offset = problem.to_ising()

For an up-to-date tutorial see here


This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .