from qiskit.optimization import QuadraticProgram
from docplex.mp.advmodel import AdvModel
from docplex.mp.model_reader import ModelReader

from qiskit.optimization.algorithms import MinimumEigenOptimizer

from dwave.plugins.qiskit import DWaveMinimumEigensolver
from dwave.system import DWaveCliqueSampler, LeapHybridSampler

import numpy as np

model = ModelReader.read(filename='BURKARDT_DATA/BINARY/reid.lp',model_name = "Test_Bin_reid", model_class=AdvModel)

qp = QuadraticProgram()
qp.from_docplex(model)

dwave_solver_hybrid = DWaveMinimumEigensolver(sampler =  LeapHybridSampler())
optimizer = MinimumEigenOptimizer(dwave_solver_hybrid)

result = optimizer.solve(qp) # Problem ID : 8f677f6a-7c23-481c-ad1e-e2887cc8758f

result

print(qp.export_as_lp_string())

# Use D-Wave QPU as a minimum eigen solver
# See https://github.com/dwavesystems/dwave-qiskit-plugin
num_reads = 1000
cliqueSampler = DWaveCliqueSampler()
dwave_solver_clique = DWaveMinimumEigensolver(sampler = cliqueSampler, num_reads = num_reads)
optimizer = MinimumEigenOptimizer(dwave_solver_clique)

result = optimizer.solve(qp) # Problem ID : 74108957-e013-40d5-a7ff-e88c9c552ab2
result

result.min_eigen_solver_result.sampleset.to_pandas_dataframe()

model = ModelReader.read(filename='BURKARDT_DATA/BINARY/two_by_four.lp',model_name = "Test_Bin_16", model_class=AdvModel)
qp = QuadraticProgram()
qp.from_docplex(model)
optimizer_hybrid = MinimumEigenOptimizer(dwave_solver_hybrid)

result = optimizer_hybrid.solve(qp) # Problem ID : 1270f318-2655-4417-a2de-5daf6c4e017a

result

result = optimizer.solve(qp) # Problem ID : 93df81b0-9ca4-450e-a921-edf22bd6b5ee
result

result.min_eigen_solver_result.sampleset.to_pandas_dataframe()

model.solve()

