from dell_runtime import DellRuntimeProvider
import os

provider = DellRuntimeProvider()

provider.remote(os.getenv("SERVER_URL"))

provider.runtime.programs()

provider.runtime.pprint_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
    result = backend.run(circuits, **kwargs).result()

    user_messenger.publish({'results': 'finally'})
    user_messenger.publish(result.to_dict(), 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)

proglist = provider.runtime.programs()
proglist

program_id

provider.runtime.pprint_programs(refresh=True)

from qiskit import QuantumCircuit, execute, transpile
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)

result = job.result(timeout=120)

job.status()

