Skip to main content
added 377 characters in body
Source Link
Egretta.Thula
  • 11.8k
  • 1
  • 12
  • 34

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

from qiskit_optimization import QuadraticProgram

problem = QuadraticProgram()
problem.binary_var(name="x1")
problem.binary_var(name="x2")
problem.binary_var(name="x3")

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

print(problem.prettyprint())

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)
print(result)

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()
print(hamiltonian)

For an up-to-date tutorial see here

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

from qiskit_optimization import QuadraticProgram

problem = QuadraticProgram()
problem.binary_var(name="x1")
problem.binary_var(name="x2")
problem.binary_var(name="x3")

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

print(problem.prettyprint())

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)
print(result)

For an up-to-date tutorial see here

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

from qiskit_optimization import QuadraticProgram

problem = QuadraticProgram()
problem.binary_var(name="x1")
problem.binary_var(name="x2")
problem.binary_var(name="x3")

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

print(problem.prettyprint())

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)
print(result)

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()
print(hamiltonian)

For an up-to-date tutorial see here

Source Link
Egretta.Thula
  • 11.8k
  • 1
  • 12
  • 34

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

from qiskit_optimization import QuadraticProgram

problem = QuadraticProgram()
problem.binary_var(name="x1")
problem.binary_var(name="x2")
problem.binary_var(name="x3")

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

print(problem.prettyprint())

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)
print(result)

For an up-to-date tutorial see here