from qiskit.optimization import QuadraticProgram
from qiskit.optimization.algorithms import MinimumEigenOptimizer
from dwave.plugins.qiskit import DWaveMinimumEigensolver
import numpy as np

A = np.array([[0,1,2,3,4,1],
              [2,1,4,1,0,2],
              [1,1,1,1,0,3],
              [1,5,2,3,4,1]])
m, n = A.shape
c = np.array([1,2,1,3,1,4])
b = np.array([1, 3, 2, 6])

model = QuadraticProgram("Binary Test")

# add variables
x = [model.binary_var('x'+str(j)) for j in range(n)]

# add linear constraints
for i in range(m):
    model.linear_constraint(linear=A[i], sense='==', rhs=b[i], name='A'+str(i)+'eq'+'b'+str(i))
    
# add linear objective function

model.minimize(linear = c)

print(model.export_as_lp_string())

# Use D-Wave QPU as a minimum eigen solver
# See https://github.com/dwavesystems/dwave-qiskit-plugin

dwave_solver = DWaveMinimumEigensolver()
optimizer = MinimumEigenOptimizer(dwave_solver)
result = optimizer.solve(model) 

print(result)

result.min_eigen_solver_result.sampleset.to_pandas_dataframe()

