import time
import numpy as np 

from qiskit import BasicAer
from qiskit.aqua import QuantumInstance, aqua_globals
from qiskit.aqua.algorithms import QAOA
from qiskit.aqua.components.optimizers import COBYLA
from qiskit.optimization.algorithms import MinimumEigenOptimizer, CplexOptimizer
from qiskit.optimization.algorithms.optimization_algorithm import OptimizationResultStatus

from random_lp.lp_random_gen import create_models

DIR = 'TEST_DATA' + "/" + time.strftime("%d_%m_%Y") + "/DENSE/" # 23.04.2021
Q_SEED = 10598 # as used in most issues
aqua_globals.random_seed = Q_SEED
shots = 4096

# select linear program to solve
qps = create_models(DIR)
qp = qps['test_dense_3']

# init classical Optimizers
optimizer = COBYLA() # SLSQP is default in Class VQE
cplex = CplexOptimizer()

# solve qps with Minimum Eigen Optimizer QAOA
backend = BasicAer.get_backend('qasm_simulator')
quantum_instance = QuantumInstance(backend,
                                   seed_simulator=Q_SEED,
                                   seed_transpiler=Q_SEED,
                                   shots=shots)
qaoa_mes = QAOA(quantum_instance=quantum_instance, optimizer=optimizer)


qaoa = MinimumEigenOptimizer(qaoa_mes)

cplex.solve(qp)

print("number of qubits: ",qp.get_num_vars())

res = qaoa.solve(qp)
res

