# Quantum Computing With Amazon Braket

Amazon Braket [1] is a fully managed service for quantum computing in the cloud. You can either run your algorithms within a simulator or on real quantum computing hardware called Quantum Processing Unit (QPU). Programming will be done using the Python-based Amazon Braket SDK [3].

**Disclaimer**: I'm not a physicist, so this article is not meant as a scientific paper about quantum computing. This article will give you (hopefully) a good overview of Amazon Braket and what is needed to run your first quantum program on such a system.

# A bit of theory first

**Bra-ket notation established by Paul Dirac**Amazon Braket is named after the bra-ket notation [4], which is a notation used to denote states of quantum mechanical systems. The English theoretical physicist, Paul Dirac [5], introduced this notation to describe the state of quantum systems. The Bra-ket notation is also known as the Dirac notation.

**Qubits and their superposition**In classical computers, information gets represented as binary bits, which can be either 0 or 1. If you have two bits, they can be in four possible states (00,01,10,11) but only one at a time.

In a quantum computer, a quantum bit, a so-called qubit, can be 0 or 1 but also 0 and 1 at the same time, which is called the *superposition*. If you have two qubits, they can also be in the states (00,01,10,11), but due to the superposition, the qubits can represent all four states simultaneously. A qubit has two basic states, which are called* ket 0* and *ket 1* — written as |0> and |1>

The above diagram shows the Bloch sphere [8], which is a geometrical representation of the states of a quantum mechanical system. The north and the south poles are defined as the pure states of qubits, and all other points on this sphere are the superpositions with a given probability. The probability on the equator is 50:50 between |0> and |1>.

**The building blocks of the classical and quantum computer world**

Like traditional logic gates, quantum gates are the fundamental building blocks of a quantum computer. In a classical computer, gates are implementing binary operations on binary inputs — i.e., the classical AND-Gate generates an output-bit 1 if both input bits are also 1. Classic logic gates are AND, OR, NOT, NOR, NAND, XOR, XNOR, BUFFER. For our first little quantum program, we will only need the Hadamard- and the controlled-Not Gate. If you want to understand the quantum gates in detail, please have a look at this Wikipedia Page.

**Quantum logic gates**

- Hadamard-Gate (H)
- Identity-Gate (IGate)
- controlled-Not Gate (cNot)
- Unitary-Gate
- Toffoli-Gate
- Pauli-Gates (X, Y, Z)
- S- and T-Gate
- Rotation-Gate
- Swap-Gate

**Quantum Simulators vs. **Quantum Processing Units (QPU)

To start experimenting with quantum computing, Amazon Braket supports you either with a simulated quantum environment or with real quantum computers called Quantum processing units (QPU). To get your hands on and also to reduce your costs check out the simulators first.

**Using Quantum Simulators first**

To prototype, validate, and debug your quantum algorithm Amazon Braket provides a suite of quantum simulators. You can choose four different simulators depending on your use cases and the number of simulated qubits you need. To learn more about quantum simulators, look at the blog Simulating quantum circuits with Amazon Braket [9].

**Local simulator (state vector)**The Amazon Braket SDK comes with a small simulator that runs locally in your environment. This simulator can be used only for small-sized simulations up to 25 qubits. The simulator supports all quantum circuit gates.

**Local simulator (density matrix)**Like the state vector simulator, the density matrix simulator also runs locally within your environment and can be used on small circuits up to 12 qubits. You can choose particular qubits and gates and apply noise operations. The simulated system resembles a real quantum processing unit where noise is one of the most dominant factors.

**Managed cloud simulators**Beyond the local simulator, Amazon offers three fully managed simulators. These simulators are running in the AWS Cloud environment and differ by the demanded use case and the number of needed qubits.

**SV1-**a general-purpose State Vector Simulator**DM1-**Density Matrix Simulator for simulating quantum circuits under the influence of noise**TN1-**a high-performance Tensor Network Simulator

## The real quantum hardware — AWS Quantum Processing Units

For running your algorithms on a real QPU, Amazon Braket currently provides five devices from the following providers:

- D-Wave
- IonQ
- Rigetti

D-Wave is only supporting quantum annealer devices where IonQ and Rigetti devices are gate-based. The IonQ QPU is based on ion trap technology, where Rigetti devices are based on superconducting qubits. More details about the provided Quantum computer hardware can be found here.

**Getting started — build your first quantum circuit gate**

Nearly every programming tutorial covers a Hello-World example [11]. A Hello-World in the quantum world is probably slightly different, but the so-called *Bell state** *[10]* *is perhaps a good candidate for your first quantum program. Follow the next steps to write your first quantum circuit code.

- Sign in to the AWS Management Console.
- Currently, Amazon Braket is only available within the U.S. Regions US East — N. Virginia, US -West — N. California, and US-West — Oregon. Choose your preferred one.
- Select the Amazon Braket Service.
- Specify the data storage settings where Braket will be writing your algorithm results to. You can either use an existing S3 Bucket or create a new one. Further, you have to accept the Terms & conditions that the quantum computing hardware can be operated by third-party providers, and your data can be handled outside AWS facilities!

If Amazon Braket is enabled successfully, you will see the status of the available Quantum processing units (QPU) and the available AWS-managed simulators.

- To continue with our first implementation of a quantum circuit, we chose the option to use a managed notebook and a managed simulator to run it on.
- Amazon Braket notebooks are fully managed Jupyter notebooks based on Amazon Sagemaker [7]. You’re paying the chosen SageMaker instance by usage. To keep costs low, remember to turn them off as soon as you do not need them anymore.

It can take some minutes to create a notebook instance. If the instance is up and running, the status will change from *Pending* to *InService*. Select your running Notebook and choose the option *Open in Jupyter.*

Continue by going to the *JupyterLab, *and you are all set to type in the code of your first quantum circuit, a so-called Bell State [10]*.*

**Build your first quantum circuit — the Bell state —the Hello-World program of the quantum universe**

The above circuit works with two qubits. Each of them is initialized at state |0>.** **On the first qubit, we perform a Hadamard-Gate (H). After the Hadamard gate, we do a cNOT-gate on the first qubit as the control bit and the second qubit as the target bit. These two gates built the so-called Bell-State. The Hadamard gate has just one input qubit, and it brings this qubit into superposition, which means that it has the values ∣0⟩ and ∣1⟩ both at the same time. The cNOT gate has two input qubits. One acts as a control bit and the second as a target bit. The cNOT flips the state of the target bit only if it is itself in state 1.

**cNote-Gate:**∣00⟩ → ∣00⟩

∣01⟩ → ∣01⟩

∣10⟩ → ∣11⟩ # control bit is in state 1, so target bit state gets flipped from 0 to 1

∣11⟩ → ∣10⟩ # control bit is in state 1, so target bit state gets flipped from 1 to 0

The result of our quantum circuit is a so-called entangled qubit |00> and |11> with a probability of 50:50 to either measure the one or the other state.

**Run the Bell-State circuit on a local simulator**

Using the Amazon Braket Python SDK, the above quantum circuit can be expressed by the following lines of code which we will run through a local simulator that is part of the SDK.

If we look into the result which the simulator prints out

`Counter({'11': 503, '00': 497})`

we can see that from our 1000 simulation runs (shots), we have 503 measurements of the state |11> and 497 of the state |00>, which is nearly the expected 50:50 ratio.

Instead of running the above quantum circuit in a local simulator, a managed simulator can be used. For this purpose, we have to adapt the code from above and use an *AwsDevice* instead of a *LocalSimulator *device. Furthermore, you have to specify the S3 Bucket where the result of your print-outs should be copied.

# Import the AwsDevice module instead of using the LocalSimulator

from braket.aws import AwsDevice# Create a Simulator Device for the managed Simulator SV1

device = AwsDevice("arn:aws:braket:::device/quantum-simulator/amazon/sv1")# This S3 bucket gets used by the Simulator to store the result

s3_output_folder = ("amazon-braket-Your-Bucket-Name", "folder-name")# run Bell state circuit 1000 times and request the resulttask = device.run(bell, shots=1000)# print the resultprint(task.result().measurement_counts)

Depending on your use case you can use the following simulator ARNs:

- arn:aws:braket:::device/quantum-simulator/amazon/sv1
- arn:aws:braket:::device/quantum-simulator/amazon/tn
- arn:aws:braket:::device/quantum-simulator/amazon/dm1

To run your quantum circuit on a real device, you would replace the simulator-*ARN *with the device *ARN *of a Quantum processing unit.

If you are interested in more complicated quantum algorithms like Grover, Quantum Fourier Transformation (QFT), Quantum Phase Estimation (QPE), or Simon’s problem, the Github project *aws/amazon-braket-examples* is an excellent point to start exploring.

**Pricing**

So what does all this rocket science cost if you want to experiment with Amazon Braket? Like all the other AWS services, you pay by usage, and it differs if you're going to run your algorithms on a simulator or a real quantum computer.

**Simulators: **The cost is based on the duration of each simulation task. Billing gets done at a per-minute rate with a minimum of 3 seconds per simulation.

Running algorithms via a simulation (SV1, DM1, TN1) is free tier eligible, and you get one free hour of quantum circuit simulation time per month.

Using the local simulator is for free.

**Managed Jupyter-Notebooks:**

Using Jupyter notebooks gets billed via the* AWS SageMaker* service [6], depending on the chosen instance type and the amount of time (hours) you’re using it.

**Quantum Processing Unit (QPU): **Using dedicated hardware running your algorithm also gets paid by usage. There are two pricing components:

Per-Shot fee: A shot is a single execution of your algorithm on a QPU.

Per-Task fee: A task is a sequence of repeated shots.

Example: To run our little quantum circuit example as one single task with 1000 shots on an IonQ device costs 10.30 USD.

To get more information on Amazon Braket costs, look at the Amazon Braket Pricing [6]. The above price calculation is based on price information from November 2021 and is given with no warranty.

**Conclusion**

Quantum computing, and particularly putting quantum computing in the cloud as a service for everybody, has a high potential for changing and potentially disrupting certain areas. Probably not tomorrow or the day after, but maybe the day after that. Quantum computers will not replace their classical counterparts but complement each another — like a GPU complements a CPU. Having the ability to easily access a QPU not only for scientists and start programming these machines (even if it is an entirely new world also for experienced (classical) software engineers) will have a profound impact. The more people start programming and using these machines, the more ideas and innovations will be influencing this area.

A lot of research has to be done up until quantum computing reaches the maturity to be applied to tasks that go beyond the solution of highly specialized and rather theoretical problems, and thus faces the real world. The fact that all major cloud providers are putting significant investments into this field and are opening quantum computers up for a broad audience of developers is another piece of evidence that this new area can have a massive impact on several industries.

If you like this article please follow me to get informed on any new publications. Also, a clap is highly appreciated.

**References**

**[1] Amazon Braket**https://aws.amazon.com/braket

**[2] Amazon Braket Tutorials GitHub**https://github.com/aws/amazon-braket-examples

**[3]** **Amazon Braket SDK**https://github.com/aws/amazon-braket-sdk-python

**[4]** **Bra-ket notation**https://en.wikipedia.org/wiki/Bra%E2%80%93ket_notation

**[5] Paul Dirac**https://en.wikipedia.org/wiki/Paul_Dirac

**[6]** **Amazon Braket Pricing**https://aws.amazon.com/braket/pricing

**[7] AWS SageMaker**https://aws.amazon.com/sagemaker

**[8] Bloch sphere**https://en.wikipedia.org/wiki/Bloch_sphere

**[9] Simulating quantum circuits with Amazon Braket**

https://aws.amazon.com/blogs/quantum-computing/simulating-quantum-circuits-with-amazon-braket

**[10] Bell state**

https://en.wikipedia.org/wiki/Bell_state

**[11] The Hello World Collection**

http://helloworldcollection.de

**[12] Quantum entanglement**

https://en.wikipedia.org/wiki/Quantum_entanglement