Saving your job results - Amazon Braket

Saving your job results

You can save the results generated by the algorithm script so that they are available from the hybrid job object in the hybrid job script as well as from the output folder in Amazon S3 (in a tar-zipped file named model.tar.gz).

The output must be saved in a file using a JavaScript Object Notation (JSON) format. If the data can not be readily serialized to text, as in the case of a numpy array, you could pass in an option to serialize using a pickled data format. See the braket.jobs.data_persistence module  for more details.

To save the results of the hybrid jobs, you add the following lines commented with #ADD to the algorithm script.

from braket.aws import AwsDevice from braket.circuits import Circuit from braket.jobs import save_job_result #ADD def start_here(): print("Test job started!!!!!") device = AwsDevice(os.environ['AMZN_BRAKET_DEVICE_ARN']) results = [] #ADD bell = Circuit().h(0).cnot(0, 1) for count in range(5): task = device.run(bell, shots=100) print(task.result().measurement_counts) results.append(task.result().measurement_counts) #ADD save_job_result({ "measurement_counts": results }) #ADD print("Test job completed!!!!!")

You can then display the results of the job from your job script by appending the line print(job.result()) commented with #ADD.

import time from braket.aws import AwsQuantumJob job = AwsQuantumJob.create( source_module="algorithm_script.py", entry_point="algorithm_script:start_here", device_arn="arn:aws:braket:::device/quantum-simulator/amazon/sv1", ) print(job.arn) while job.state() not in AwsQuantumJob.TERMINAL_STATES: print(job.state()) time.sleep(10) print(job.state()) print(job.result()) #ADD

In this example, we have removed wait_until_complete=True to suppress verbose output. You can add it back in for debugging. When you run this hybrid job, it outputs the identifier and the job-arn, followed by the state of the hybrid job every 10 seconds until the hybrid job is COMPLETED, after which it shows you the results of the bell circuit. See the following example.

arn:aws:braket:us-west-2:111122223333:job/braket-job-default-1234567890123 INITIALIZED RUNNING RUNNING RUNNING RUNNING RUNNING RUNNING RUNNING RUNNING RUNNING RUNNING ... RUNNING RUNNING COMPLETED {'measurement_counts': [{'11': 53, '00': 47},..., {'00': 51, '11': 49}]}