|
| 1 | +{ |
| 2 | + "metadata": { |
| 3 | + "language_info": { |
| 4 | + "codemirror_mode": { |
| 5 | + "name": "ipython", |
| 6 | + "version": 3 |
| 7 | + }, |
| 8 | + "file_extension": ".py", |
| 9 | + "mimetype": "text/x-python", |
| 10 | + "name": "python", |
| 11 | + "nbconvert_exporter": "python", |
| 12 | + "pygments_lexer": "ipython3", |
| 13 | + "version": "3.7.10-final" |
| 14 | + }, |
| 15 | + "orig_nbformat": 2, |
| 16 | + "kernelspec": { |
| 17 | + "name": "python3", |
| 18 | + "display_name": "Python 3.7.10 64-bit", |
| 19 | + "metadata": { |
| 20 | + "interpreter": { |
| 21 | + "hash": "fd69f43f58546b570e94fd7eba7b65e6bcc7a5bbc4eab0408017d18902915d69" |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + }, |
| 26 | + "nbformat": 4, |
| 27 | + "nbformat_minor": 2, |
| 28 | + "cells": [ |
| 29 | + { |
| 30 | + "source": [ |
| 31 | + "# Running ProjectQ code on AWS Braket service provided devices\n", |
| 32 | + "## Compiling code for AWS Braket Service\n", |
| 33 | + "\n", |
| 34 | + "In this tutorial we will see how to run code on some of the devices provided by the Amazon AWS Braket service. The AWS Braket devices supported are: the State Vector Simulator 'SV1', the Rigetti device 'Aspen-8' and the IonQ device 'IonQ'\n", |
| 35 | + "\n", |
| 36 | + "You need to have a valid AWS account, created a pair of access key/secret key, and have activated the braket service. As part of the activation of the service, a specific S3 bucket and folder associated to the service should be configured.\n", |
| 37 | + "\n", |
| 38 | + "First we need to do the required imports. That includes the mail compiler engine (MainEngine), the backend (AWSBraketBackend in this case) and the operations to be used in the cicuit" |
| 39 | + ], |
| 40 | + "cell_type": "markdown", |
| 41 | + "metadata": {} |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "code", |
| 45 | + "execution_count": null, |
| 46 | + "metadata": {}, |
| 47 | + "outputs": [], |
| 48 | + "source": [ |
| 49 | + "from projectq import MainEngine\n", |
| 50 | + "from projectq.backends import AWSBraketBackend\n", |
| 51 | + "from projectq.ops import Measure, H, C, X, All\n" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "source": [ |
| 56 | + "Prior to the instantiation of the backend we need to configure the credentials, the S3 storage folder and the device to be used (in the example the State Vector Simulator SV1)" |
| 57 | + ], |
| 58 | + "cell_type": "markdown", |
| 59 | + "metadata": {} |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "code", |
| 63 | + "execution_count": null, |
| 64 | + "metadata": {}, |
| 65 | + "outputs": [], |
| 66 | + "source": [ |
| 67 | + "creds = {\n", |
| 68 | + " 'AWS_ACCESS_KEY_ID': 'aws_access_key_id',\n", |
| 69 | + " 'AWS_SECRET_KEY': 'aws_secret_key',\n", |
| 70 | + " } # replace with your Access key and Secret key\n", |
| 71 | + "\n", |
| 72 | + "s3_folder = ['S3Bucket', 'S3Directory'] # replace with your S3 bucket and directory\n", |
| 73 | + "\n", |
| 74 | + "device = 'SV1' # replace by the device you want to use" |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "source": [ |
| 79 | + "Next we instantiate the engine with the AWSBraketBackend including the credentials and S3 configuration. By setting the 'use_hardware' parameter to False we indicate the use of the Simulator. In addition we set the number of times we want to run the circuit and the interval in secons to ask for the results. For a complete list of parameters and descriptions, please check the documentation." |
| 80 | + ], |
| 81 | + "cell_type": "markdown", |
| 82 | + "metadata": {} |
| 83 | + }, |
| 84 | + { |
| 85 | + "cell_type": "code", |
| 86 | + "execution_count": null, |
| 87 | + "metadata": {}, |
| 88 | + "outputs": [], |
| 89 | + "source": [ |
| 90 | + "eng = MainEngine(AWSBraketBackend(use_hardware=False,\n", |
| 91 | + " credentials=creds,\n", |
| 92 | + " s3_folder=s3_folder,\n", |
| 93 | + " num_runs=10,\n", |
| 94 | + " interval=10))" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "source": [ |
| 99 | + "We can now allocate the required qubits and create the circuit to be run. With the last instruction we ask the backend to run the circuit." |
| 100 | + ], |
| 101 | + "cell_type": "markdown", |
| 102 | + "metadata": {} |
| 103 | + }, |
| 104 | + { |
| 105 | + "cell_type": "code", |
| 106 | + "execution_count": null, |
| 107 | + "metadata": {}, |
| 108 | + "outputs": [], |
| 109 | + "source": [ |
| 110 | + "# Allocate the required qubits\n", |
| 111 | + "qureg = eng.allocate_qureg(3)\n", |
| 112 | + "\n", |
| 113 | + "# Create the circuit. In this example a quantum teleportation algorithms that teleports the first qubit to the third one.\n", |
| 114 | + "H | qureg[0]\n", |
| 115 | + "H | qureg[1]\n", |
| 116 | + "C(X) | (qureg[1], qureg[2])\n", |
| 117 | + "C(X) | (qureg[0], qureg[1])\n", |
| 118 | + "H | qureg[0]\n", |
| 119 | + "C(X) | (qureg[1], qureg[2])\n", |
| 120 | + "\n", |
| 121 | + "# At the end we measure the qubits to get the results; should be all-0 or all-1\n", |
| 122 | + "All(Measure) | qureg\n", |
| 123 | + "\n", |
| 124 | + "# And run the circuit\n", |
| 125 | + "eng.flush()\n" |
| 126 | + ] |
| 127 | + }, |
| 128 | + { |
| 129 | + "source": [ |
| 130 | + "The backend will automatically create the task and generate a unique identifier (the task Arn) that can be used to recover the status of the task and results later on.\n", |
| 131 | + "\n", |
| 132 | + "Once the circuit is executed the indicated number of times, the results are stored in the S3 folder configured previously and can be recovered to obtain the probabilities of each of the states." |
| 133 | + ], |
| 134 | + "cell_type": "markdown", |
| 135 | + "metadata": {} |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "code", |
| 139 | + "execution_count": null, |
| 140 | + "metadata": {}, |
| 141 | + "outputs": [], |
| 142 | + "source": [ |
| 143 | + "# Obtain and print the probabilies of the states\n", |
| 144 | + "prob_dict = eng.backend.get_probabilities(qureg)\n", |
| 145 | + "print(\"Probabilites for each of the results: \", prob_dict)" |
| 146 | + ] |
| 147 | + }, |
| 148 | + { |
| 149 | + "source": [ |
| 150 | + "## Retrieve results form a previous execution\n", |
| 151 | + "\n", |
| 152 | + "We can retrieve the result later on (of this job or a previously executed one) using the task Arn provided when it was run. In addition, you have to remember the amount of qubits involved in the job and the order you used. The latter is required since we need to set up a mapping for the qubits when retrieving results of a previously executed job.\n", |
| 153 | + "\n", |
| 154 | + "To retrieve the results we need to configure the backend including the parameter 'retrieve_execution' set to the Task Arn of the job. To be able to get the probabilities of each state we need to configure the qubits and ask the backend to get the results." |
| 155 | + ], |
| 156 | + "cell_type": "markdown", |
| 157 | + "metadata": {} |
| 158 | + }, |
| 159 | + { |
| 160 | + "cell_type": "code", |
| 161 | + "execution_count": null, |
| 162 | + "metadata": {}, |
| 163 | + "outputs": [], |
| 164 | + "source": [ |
| 165 | + "# Set the Task Arn of the job to be retrieved and instantiate the engine with the AWSBraketBackend\n", |
| 166 | + "task_arn = 'your_task_arn' # replace with the actual TaskArn you want to use\n", |
| 167 | + "\n", |
| 168 | + "eng1 = MainEngine(AWSBraketBackend(retrieve_execution=task_arn, credentials=creds, num_retries=2, verbose=True))\n", |
| 169 | + "\n", |
| 170 | + "# Configure the qubits to get the states probabilies\n", |
| 171 | + "qureg1 = eng1.allocate_qureg(3)\n", |
| 172 | + "\n", |
| 173 | + "# Ask the backend to retrieve the results\n", |
| 174 | + "eng1.flush()\n", |
| 175 | + "\n", |
| 176 | + "# Obtain and print the probabilities of the states\n", |
| 177 | + "prob_dict1 = eng1.backend.get_probabilities(qureg1)\n", |
| 178 | + "print(\"Probabilities \", prob_dict1)\n" |
| 179 | + ] |
| 180 | + }, |
| 181 | + { |
| 182 | + "source": [ |
| 183 | + "We can plot an histogram with the probabilities as well." |
| 184 | + ], |
| 185 | + "cell_type": "markdown", |
| 186 | + "metadata": {} |
| 187 | + }, |
| 188 | + { |
| 189 | + "cell_type": "code", |
| 190 | + "execution_count": null, |
| 191 | + "metadata": {}, |
| 192 | + "outputs": [], |
| 193 | + "source": [ |
| 194 | + "import matplotlib.pyplot as plt\n", |
| 195 | + "%matplotlib inline\n", |
| 196 | + "from projectq.libs.hist import histogram\n", |
| 197 | + "\n", |
| 198 | + "histogram(eng1.backend, qureg1)\n", |
| 199 | + "plt.show()" |
| 200 | + ] |
| 201 | + }, |
| 202 | + { |
| 203 | + "cell_type": "code", |
| 204 | + "execution_count": null, |
| 205 | + "metadata": {}, |
| 206 | + "outputs": [], |
| 207 | + "source": [] |
| 208 | + } |
| 209 | + ] |
| 210 | +} |
0 commit comments