Advanced logging - Amazon Braket

Advanced logging

You can record the whole task-processing process using a logger. These advanced logging techniques allow you to see the background polling and create a record for later debugging.

To use the logger, we recommend changing the poll_timeout_seconds and poll_interval_seconds parameters, so that a quantum task can be long-running and the quantum task status is logged continuously, with results saved to a file. You can transfer this code to a Python script instead of a Jupyter notebook, so that the script can run as a process in the background.

Configure the logger

First, configure the logger so that all logs are written into a text file automatically, as shown in the following example lines.

# import the module import logging from datetime import datetime # set filename for logs log_file = 'device_logs-'+datetime.strftime(datetime.now(), '%Y%m%d%H%M%S')+'.txt' print('Task info will be logged in:', log_file) # create new logger object logger = logging.getLogger("newLogger") # configure to log to file device_logs.txt in the appending mode logger.addHandler(logging.FileHandler(filename=log_file, mode='a')) # add to file all log messages with level DEBUG or above logger.setLevel(logging.DEBUG)
Task info will be logged in: device_logs-20200803203309.txt

Create and run the circuit

Now you can create a circuit, submit it to a device to run, and see what happens as shown in this example.

# define circuit circ_log = Circuit().rx(0, 0.15).ry(1, 0.2).rz(2, 0.25).h(3).cnot(control=0, target=2).zz(1, 3, 0.15).x(4) print(circ_log) # define backend device = AwsDevice("arn:aws:braket:::device/quantum-simulator/amazon/sv1") # define what info to log logger.info( device.run(circ_log, s3_location, poll_timeout_seconds=1200, poll_interval_seconds=0.25, logger=logger, shots=1000) .result().measurement_counts )

Check the log file

You can check what is written into the file by entering the following command.

# print logs ! cat {log_file}
Task arn:aws:braket:us-west-2:123412341234:quantum-task/5088ec6c-89cf-4338-9750-9f5bb12a0dc4: start polling for completion Task arn:aws:braket:us-west-2:123412341234:quantum-task/5088ec6c-89cf-4338-9750-9f5bb12a0dc4: task status CREATED Task arn:aws:braket:us-west-2:123412341234:quantum-task/5088ec6c-89cf-4338-9750-9f5bb12a0dc4: task status CREATED Task arn:aws:braket:us-west-2:123412341234:quantum-task/5088ec6c-89cf-4338-9750-9f5bb12a0dc4: task status QUEUED Task arn:aws:braket:us-west-2:123412341234:quantum-task/5088ec6c-89cf-4338-9750-9f5bb12a0dc4: task status RUNNING Task arn:aws:braket:us-west-2:123412341234:quantum-task/5088ec6c-89cf-4338-9750-9f5bb12a0dc4: task status RUNNING Task arn:aws:braket:us-west-2:123412341234:quantum-task/5088ec6c-89cf-4338-9750-9f5bb12a0dc4: task status COMPLETED Counter({'00001': 493, '00011': 493, '01001': 5, '10111': 4, '01011': 3, '10101': 2})

Get the ARN from the log file

From the log file output that’s returned, as shown in the previous example, you can obtain the ARN information. With the ARN ID, you can retrieve the result of the completed quantum task.

# parse log file for arn with open(log_file) as openfile: for line in openfile: for part in line.split(): if "arn:" in part: arn = part break # remove final semicolon in logs arn = arn[:-1] # with this arn you can restore again task from unique arn task_load = AwsQuantumTask(arn=arn, aws_session=AwsSession()) # get results of task result = task_load.result()