Saving and Reloading ResultHandle to Monitor Job Status

This document shows two workflows to use reference data (pytket.backends.resulthandle.ResultHandle) for H-Series jobs across different sessions.

A H-Series job submission with pytket involves:

  1. circuit synthesis.

  2. compilation to the H-Series gate-set.

  3. submission of the circuit to H-Series (the job).

  4. retrieval of job results.

Completion of step 3. produces a pytket.backends.resulthandle.ResultHandle(link). This ResultHandle is required to complete step 4. Sometimes, a job may take hours or days to run, depending on demand for the device, operational hours (visible in the user portal), the fair queue and the customer’s group- and user-priorities.

In this scenario, it is recommended to save the ResultHandle data to disk after step 3. is completed and reload from disk once step 4. is possible.

Content

Save ResultHandle to Disk

The ResultHandle is created after a circuit is submitted with QuantinuumBackend to H-Series.

from pytket.extensions.quantinuum import QuantinuumBackend

backend = QuantinuumBackend(device_name="H1-1SC")
backend.login()
from pytket.circuit import Circuit
from numpy.random import random_sample

circ = Circuit(10)
for qubit in circ.qubits:
    angles = random_sample(2) * 0.1
    circ.PhasedX(*angles.tolist(), qubit)

measurement_circuit = circ.copy().measure_all()
result_handle = backend.process_circuit(
    measurement_circuit, n_shots=10, options={"tket-opt-level": 2}
)
jobid = backend.get_jobid(result_handle)
print(jobid)
file_io = open("result_handles.txt", "w")
file_io.write(str(result_handle))
file_io.close()

Reload ResultHandle from Disk

The ResultHandle data is read from disk and reloaded into the ResultHandle object using the from_str method.

text_stream = open("result_handles.txt", "r")
result_handle_text = text_stream.read()
text_stream.close()
from pytket.backends.resulthandle import ResultHandle

reloaded_result_handle = ResultHandle.from_str(result_handle_text)
print(reloaded_result_handle)
('e1d4d707e3d4442bbb5fe6e7f529d90a', 'null', 10, '[["c", 0], ["c", 1], ["c", 2], ["c", 3], ["c", 4], ["c", 5], ["c", 6], ["c", 7], ["c", 8], ["c", 9]]')

This can now be used to retrieve job results from H-Series.

result = backend.get_result(reloaded_result_handle)
print(result.get_distribution())
{(0, 0, 0, 0, 0, 0, 0, 0, 0, 0): 1.0}

Reload ResultHandle using Job ID

It is also possible to retrieve H-Series data using the Job ID alone. However, it is recommended to always save ResultHandle information to disk using the workflow above.

All Job IDs are recorded in the user portal. A single Job ID is the first argument of ResultHandle. The second and fourth argument are the string “null” and the third argument is an integer, which is set to the integer 0 in the code-cell below.

handle_from_id = ResultHandle(jobid, "null", 0, "null")
result_from_id = backend.get_result(handle_from_id)
print(result_from_id.get_distribution())
{(): 1.0}

Summary

This document shows two workflows to use reference data (pytket.backends.resulthandle.ResultHandle) for H-Series jobs across different sessions.

  1. Save and reload from disk.

  2. Reinstantiate using Job ID.

This workflow is very useful when jobs are submitted and results are recovered on different days. It is recommended to use method 1 over method 2.