from qiskit.circuit.library import RealAmplitudes, EfficientSU2


efficientSU2 = EfficientSU2(1, reps=1, entanglement='linear', insert_barriers=True)
realAmplitudes = RealAmplitudes(1,reps=1)

efficientSU2.draw(output='text')

realAmplitudes.draw(output='text')

import numpy as np

rdist = np.random.rand(1, 1)
rdistt = np.transpose(rdist)
z1 = np.zeros((1, 1))
z2 = np.zeros((1, 1))
matrix = np.block([[z1, rdist], [rdistt, z2]])

from qiskit import IBMQ

from qiskit.providers.ibmq import least_busy
from qiskit.providers import JobStatus


IBMQ.load_account()
provider = IBMQ.get_provider('ibm-q-research-2','vienna-uni-tech-1','main')

backend = least_busy(
    provider.backends(
        filters=lambda x: x.configuration().n_qubits >= 3
        and not x.configuration().simulator
        and x.status().operational
        and x.configuration().dynamic_reprate_enabled
    )
)
print(backend.name())

from qiskit.algorithms.optimizers import COBYLA
from qiskit.opflow.primitive_ops import MatrixOp
import time

matrix_op = MatrixOp(matrix)

vqe_inputs = {
        'ansatz': realAmplitudes,
        'operator': matrix_op,
        'optimizer': COBYLA(max_iter=100, tol=0.00001),
        'initial_point': np.random.random(realAmplitudes.num_parameters),
        'measurement_error_mitigation': True,
        'shots': 1024
    }

options = {
        'backend_name': backend.name(),
    }

job = provider.runtime.run(program_id='vqe', inputs=vqe_inputs, options=options)
while job.status() != JobStatus.RUNNING:
    pass
start = time.time()
res = job.result()
end = time.time()
print("Runtime: "+str(end-start))

from qiskit.algorithms.optimizers import COBYLA
from qiskit.opflow.primitive_ops import MatrixOp
import time

matrix_op = MatrixOp(matrix)

vqe_inputs = {
        'ansatz': efficientSU2,
        'operator': matrix_op,
        'optimizer': COBYLA(max_iter=100, tol=0.00001),
        'initial_point': np.random.random(efficientSU2.num_parameters),
        'measurement_error_mitigation': True,
        'shots': 1024
    }

options = {
        'backend_name': backend.name(),
    }

job = provider.runtime.run(program_id='vqe', inputs=vqe_inputs, options=options)
while job.status() != JobStatus.RUNNING:
    pass
start = time.time()
res = job.result()
end = time.time()
print("Runtime: "+str(end-start))

