Use hyperparameters - Amazon Braket

Learn the foundations of quantum computing with AWS! Enroll in the Amazon Braket Digital Learning Plan and earn your own Digital badge after completing a series of learning courses and a digital assessment.

Use hyperparameters

You can define hyperparameters needed by your algorithm, such as the learning rate or step size, when you create a hybrid job. Hyperparameter values are typically used to control various aspects of the algorithm, and can often be tuned to optimize the algorithm’s performance. To use hyperparameters in a Braket hybrid job, you need to specify their names and values explicitly as a dictionary. Note that the values must be of the string datatype. You specify the hyperparameter values that you want to test when searching for the optimal set of values. The first step to using hyperparameters is to set up and define the hyperparameters as a dictionary, which can be seen in the following code:

#defining the number of qubits used n_qubits = 8 #defining the number of layers used n_layers = 10 #defining the number of iterations used for your optimization algorithm n_iterations = 10 hyperparams = { "n_qubits": n_qubits, "n_layers": n_layers, "n_iterations": n_iterations }

You would then pass the hyperparameters defined in the code snippet given above to be used in the algorithm of your choice with something that looks like the following:

import time from braket.aws import AwsQuantumJob #Name your job so that it can be later identified job_name = f"qcbm-gaussian-training-{n_qubits}-{n_layers}-" + str(int(time.time())) job = AwsQuantumJob.create( #Run this hybrid job on the SV1 simulator device="arn:aws:braket:::device/quantum-simulator/amazon/sv1", #The directory or single file containing the code to run. source_module="qcbm", #The main script or function the job will run. entry_point="qcbm.qcbm_job:main", #Set the job_name job_name=job_name, #Set the hyperparameters hyperparameters=hyperparams, #Define the file that contains the input data input_data="data.npy", # or input_data=s3_path # wait_until_complete=False, )
Note

In order to learn more about input data see the Inputs section.

The hyperparameters would then be loaded into the hybrid job script using the following code:

import json import os #Load the Hybrid Job hyperparameters hp_file = os.environ["AMZN_BRAKET_HP_FILE"] with open(hp_file, "r") as f: hyperparams = json.load(f)
Note

For more information about how to pass information like the input data and the device arn to the hybrid job script, see this github page.

A couple guides that are very useful for learning about how to use hyperparameters are given by the QAOA with Amazon Braket Hybrid Jobs and PennyLane and Quantum machine learning in Amazon Braket Hybrid Jobs tutorials.