In this tutorial, we will learn how to access and use the quantum devices and simulators provided by AWS Braket using the Command Line Interface (CLI).
You can use the Amazon Braket Python SDK to run your quantum circuits either on your local computer or on AWS Braket’s simulators, and then execute them on the available Quantum Processing Units (QPUs).
Before the installation, following this linkto create and get the AWS Access Key ID and AWS Secret Access Key.
Install AWS CLI
First, you need to install the AWS Command Line Interface (CLI) on your system. Follow the instructions provided in the tutorial to download and install the AWS CLI.
After installing the AWS CLI, you need to configure it with your AWS Access Key ID, AWS Secret Access Key, and the desired AWS Region. This information is required for authenticating your requests to AWS services.
$ aws configure
AWS Access Key ID [None]: ********************
AWS Secret Access Key [None]: ********************
Default region name [None]: us-west-1
Default output format [None]: json
Note
Access keys consist of an access key ID and secret access key, which are used to sign programmatic requests that you make to AWS.
The Default region name identifies the AWS Region whose servers you want to send your requests to by default. This is typically the Region closest to you, but it is recommended that you pick one of the 3 regions in which Amazon Braket is available: us-east-1, us-west-1, us-west-2.
The Default output format specifies how the results are formatted. The value can be any of the values in the following list. If you don’t specify an output format, json is used as the default.
Install Anaconda or Miniconda (Optional)
Next, you need to install Anaconda or Miniconda, which are open-source distribution platforms for Python and other programming languages. This step ensures that you have a consistent Python environment for running your quantum computing experiments.
frombraket.awsimportAwsDevicefrombraket.circuitsimportCircuitdevice=AwsDevice("arn:aws:braket:::device/quantum-simulator/amazon/sv1")# Choose S3 bucket to store resultsbucket=# <<Update with your actual bucket name>> #eg: "amazon-braket-unique-aabbcdd"prefix="results"s3_folder=(bucket,prefix)bell=Circuit().h(0).cnot(0,1)print(bell)task=device.run(bell,s3_folder,shots=100)print("Measurement Results")print(task.result().measurement_counts)
frombraket.awsimportAwsDevicefrombraket.circuitsimportCircuitdevice=AwsDevice("arn:aws:braket:us-east-1::device/qpu/ionq/Aria-1")# Choose S3 bucket to store resultsbucket=<<Updatewithyouactualbucketname>>#eg: "amazon-braket-unique-aabbcdd"prefix="results"s3_folder=(bucket,prefix)bell=Circuit().h(0).cnot(0,1)print(bell)task=device.run(bell,s3_folder,shots=100)print("Task submitted. Task ARN is: ",task.id)
frombraket.awsimportAwsQuantumTask# replace with your actual task arnionq_task_id="arn:aws:braket:us-west-1:83xxxxxxx862:quantum-task/321c7904-a07b-4aa9-945e-daec329ed1cd"# recover tasktask_load=AwsQuantumTask(arn=ionq_task_id)# print statusstatus=task_load.state()print('Status of (reconstructed) task:',status)print('\n')ifstatus=='COMPLETED':# get resultsionq_results=task_load.result()# get all metadata of submitted taskmetadata=task_load.metadata()# example for metadatashots=metadata['shots']machine=metadata['deviceArn']# print example metadataprint("{} shots taken on machine {}.\n".format(shots,machine))# get measurement countsionq_counts=ionq_results.measurement_countsprint('Measurement counts:',rigetti_counts)elifstatusin['FAILED','CANCELLED']:# print terminal messageprint('Your task is in terminal status, but has not completed.')else:# print current statusprint('Sorry, your task is still being processed and has not been finalized yet.')
So far, the steps walks you through the steps to create Python scripts, submit tasks to the QPUs, and retrieve the results.
By following this AWS Braket note, you can now start to set up the Command Line Interface (CLI) for AWS, install the required dependencies, and run quantum circuits on various platforms, including local simulators, AWS Braket simulators, and real Quantum Processing Units (QPUs).