from qiskit import QuantumCircuit, execute, transpile
from dell_runtime import DellRuntimeProvider

provider = DellRuntimeProvider()

provider.remote('http://qdr-dev.oro-sandbox-small1.k8s.cec.lab.emc.com')

provider.runtime.programs()

RUNTIME_PROGRAM = """
from qiskit.compiler import transpile, schedule


def main(
    backend,
    user_messenger,
    circuits,
    **kwargs,
):

    user_messenger.publish({'results': 'intermittently'})

    circuits = transpile(
        circuits,
    )

    if not isinstance(circuits, list):
        circuits = [circuits]

    # Compute raw results
    for i in range(0, 10):
        result = backend.run(circuits, **kwargs).result()
        user_messenger.publish({f'result_{i}': result})
    
    user_messenger.publish('completed', final=True)
    print("job complete successfully")
"""

RUNTIME_PROGRAM_METADATA = {
    "max_execution_time": 600,
    "description": "Qiskit test program"
}

program_id = provider.runtime.upload_program(RUNTIME_PROGRAM, metadata=RUNTIME_PROGRAM_METADATA)

program_id

qc = QuantumCircuit(2, 2)
qc.h(0)
qc.cx(0, 1)
qc.measure([0, 1], [0, 1])

program_inputs = {
    'circuits': qc,
}

job = provider.runtime.run(program_id, options=None, inputs=program_inputs, callback=print)

job.get_unread_messages()

job.result(timeout=20)

job.status()

