Intro to Quantum Computing with AWS Braket

Intro to Quantum Computing with AWS Braket

Quantum Computing. The phrase invokes images from popular science fiction. From the innumerable Qubit machine of Devs on FX to the foundational component of a whole new virtual world in Neal Stephenson’s “Fall”. The phrase Quantum Computing invokes visions of mythological computational feats. Discussions around Quantum Computing veer into many branches of computational theory, artificial intelligence, cryptography, and compression.

What is Quantum Computing? How do we start taking advantage of Quantum Computing right now? Is it worth it, or are we just forcing a solution onto problems created for the solution because the tech is “neat”? The aim of ths article is to answer these questions, show how to integrate with Quantum Computing hosted in the cloud with AWS Braket, and discuss the value of researching this technology within your business.

To scope this a bit tighter, this is specifically a post about Quantum Computing within AWS Braket, not a detailed exploration into Quantum Mechanics or Quantum Theory. By keeping our focus specifically on Quantum Computing we can more readily learn how to program Quantum Circuits using nothing more complex than Linear Combinations. This is the equivalent of how Electrical Engineers first learned to program using logic gates on classical hardware.

To begin it is assumed you have pre-requisite knowledge in Computer Science and Linear Algebra. If you need a brush up on Linear Algebra, follow the links listed below.

Quantum Computing, Then, Now, and Forward

Before moving forward, one should go back and understand where Quantum Computing originated and what is seen as its future. Quantum computing is young (in human terms) and has seen more growth in the past ten years than the last couple of decades. The indicator of this growth is the adoption and enablement of using quantum computing in the cloud.

The first quantum computer hardware was produced in the mid-1990s, 1998 to be exact, by Isaac Chuang of the Los Alamos National Laboratory, Neil Gershenfeld of the Massachusetts Institute of Technology (MIT), and Mark Kubinec of the University of California at Berkeley. That was a 2-Qubit system. That research opened to door to the reality of using Quantum Computers.

Fast forward to the now and we find ourselves at the stage of having 5K+ quantum systems, but we still do not have formal definitions for common scalar types such as integer and floating point values. This means we still do quantum operations by composing circuit configurations for the target hardware. This can be like the early days of classical computing using tubes and vacuums.

Quantum Computing is also not seen as the primary or general means of computation. Like the GPUs we use for video games and heterogenous computing, a Quantum Processing Unit (QPU) is most likely to be used as a co-processor for specific types of tasks. But the benefit of being a young technology is that barriers and original assumptions may be broken, and perhaps a truly general-purpose quantum computer will arise.

As it stands, quantum computing is young, the programmability of it exists, but is not as easy as picking up a language like Javascript. So – where does that put us? It puts us in an age of discovery and experimentation. It allows people to answer the call to action to push quantum computing further. There are some limitations out there that need to be explored or solved, such as scaling, discovering algorithms, developing algorithms, reducing the cost of hardware, and quantum error correction. What is amazing about this, is that this is not like the early years of classical computers.

Why? We have computers that can synthesize circuits and simulate the quantum computers. The old classical computing model is ready and able to obsolete itself with the work of discovering more in the field of quantum computing. What is more, is that through Cloud services such as AWS Braket we are all enabled to explore innovation within Quantum Computing. Furthermore, we are at a stage where the benefits are already enjoyed by the early adopters in the fields of product development, Chemistry, Finance, Cryptography, Machine Learning, Statistics, and Energy.

To see benefits so early in an unknown technology is exciting. It leads to innovation now in preparation for the future state of computing. To prepare for this future state, there are two main hurdles to cross when one wishes to learn Quantum Computing. The barriers being an understanding of the symbols physicists, engineers, and mathematicians use to represent various states and operations; and an understanding of what a Qubit is.

The Qubit

The first step is understanding a Qubit and the computational basis. A Qubit It is something that represents the quantum state of particles such as Ions, Photons, and Electrons. Other types of quantum like materials may be used as well so this is not a fully inclusive list. A Qubit maintains either a |0> state, or a |1> state, or a state in between the infinite ranges of |0> and |1>.

Mathematically speaking, a Qubit is a unit vector [a,b] where |a|^2 + |b|^2 = 1.

Now that we have our understanding of state, we must do something with that state. To do so we operate on Qubits using logic gates.

The Bra and the Ket

The two symbols that are most important, are the bra (a 2-dimensional row vector) and the Ket (a 2-dimensional column vector). From experience, the Ket is more prevalent in documentation and scientific papers; therefore, the remainder of discussion will be terms of Kets. This means our algorithms for our Quantum Circuits will be applied to column vectors.

Kets are shorthand for a column vector. A single Ket of a computational basis state is shown below, a 0 Ket |0> is equal to the [1, 0] vector and a 1 Ket |1> is equal to the [0, 1] vector. We can also combine Kets, like how we build strings of bits to represent specific binary data. Here we can see that doing this is nothing more than representing the tensor product of all the vectors involved.

Quantum Logic Gates

Beyond a representation of our state, we must also figure out how to apply logic to our state. This is done via Quantum Logic Gates. The biggest qualifier for these, mathematically, is that they are unitary matrices that preserve the length of our inputs. From the Matrix form of each of these gates you can begin to work out how that is accomplished.

Also note, as in the case of two Qubit and three Qubit gates such as the Controlled Not, Controlled Z, SWAP, and Toffoli, the gates become larger matrices in relation to the number of Qubits. This is where we begin to realize why it begins a complex and inefficient method of computation on a classical computer. Therefore, most simulators only simulate tens of Qubits at a time. Keep in mind these are not all the gates, but just like with some CPUS, only certain gates are supported, and these will be the most common for the time being.

Quantum Logic Gates are operations that manipulate the Qubit or system of Qubits in a manner that mathematically preserves length. Therefore, all the gates can be represented as unitary matrices, their elements contain 1, 0, or imaginary values. The preservation of length is an important characteristic that explains why we use a 2-element vector for Qubit state, as opposed to a quaternion, and a 2x2 matrix for our logic gates. If you are interested in the proof behind this, I would recommend reading “Quantum Computing since Democritus” by Scott Aaronson. The four most basic gates for any given quantum computer are the logic Not gate, the Hadamard gate, the Controlled Not gate, and the Toffoli gate. From these gates you can create any other logical gate and quantum system.

Single Qubit Gates

The beginning implementation of any Quantum Computer begins with single Qubit gate operations. These are operations that operate only upon a single Qubit at a time. The most basic of these gates are known as the I Gate, Pauli X, Pauli Y, Pauli Z, and the Hadamard Gate. This is not a fully inclusive list but one containing the most common gates.

The I Gate is the Identity Gate. Given a State Vector, once the Identity Gate is applied, a State Vector equal to the previous State Vector will be produced. This is done by applying the 2x2 identity matrix to the 2-Dimensional State Vector. If you were to imagine the state vector as a sphere, the Paul X Gate simply spins that sphere around by pi. The Pauli X gate is the quantum version of a Binary Not gate. Sometimes the term Pauli X is used, others Not is used, both refer to the same type of gate.

The Pauli Y and Pauli Z gates act similar in nature as the Pauli X gate if the Qubit’s state vector is imagined as a 3-D sphere. Each of those gates spin the sphere along the relevant Y and Z axis.

Example of a Bloc Sphere. source: wikipedia.org

Using the beforementioned Pauli X, Y, and Z gates we get the same functionality on a Quantum Computer as we would find in a classical computer. To take advantage of Quantum Computing we must first put our Qubit into Superposition. This is where the real “magic” of quantum computing occurs. We will not digress into a detailed explanation of Superposition; we will just discuss how to use it within the context of Quantum Circuits.

The Hadamard Gate is the gate that enables us to achieve superposition for a Qubit. Again, visualizing the Qubit as a sphere, it will rotate the sphere about the X and Z axis. When a Qubit passes through a Hadamard Gate, it will re-enter itself into a quantum state. The classic example is that it will be like a spinning penny, the odds of falling heads or tails equal. Essentially, a Hadamard gate initializes a Qubit.

There are many more single-Qubit quantum gates available. However, they are mostly shortcut gates to reduce the depth of a quantum program. Just as a classical computer can be built with NOT, OR, and AND binary gates, a fully functional quantum computer can be built with only a handful of Quantum Circuits similar in behavior.

N-Qubit Gates

Moving away from single Qubit logic gates, we enter multiple Qubit logic gates. These gates enable us to build something like the computers we know and consume today. The first gate, and the one we will be looking at today, is the Controlled Not Gate. This gate takes a control Qubit and a target Qubit. Much like classical computing, the target Qubit state will change in relation to the state of the control Qubit. Unlike classical computing, there is a potential for the target Qubit state to impact the control Qubit state.

The second important multi Qubit gate is the Toffoli gate, also known as the Controlled Controlled Not gate. This is a three-Qubit gate that uses two control Qubits to manipulate a single target Qubit. This is like an AND gate in classical computing. The third Qubit is also known as an ancillary Qubit. The reason this third Qubit exists is because we cannot copy the state of a Qubit, therefore we must apply state to a Qubit due to the state of two other Qubits. Another note is that if we do not have a Toffoli gate available, we can compose a quantum circuit from Unitary matrices and CNOT gates to replicate a Toffoli gate. Other gates of note are the Controllzed Z gate, T Gate, and Swap Gate.

Closing the Gate

To summarize, here is a convenient image showing the name of the gate, its symbolic representation on a quantum wiring diagram, and its matrix form. Credit to Wikipedia for this image.

Common Single and N-Qubit Gates, source: wikipedia.org

If you are interested in learning more specifics and the mathematics behind quantum computing, I would recommend the following resources:

Measuring

Now that we understand what is known as a Ket and the computational basis state, the state we are most familiar with seeing, we must now understand how quantum measurements apply here. For quantum computing to work we must apply the horizontal and vertical measurements of our quantum state to our Kets. This is typically denoted as alpha and beta. This is how we begin to realize the probabilities of our algorithms and the measurements of quantum state, in particular positions.

Typically, a Qubit is measured once we want to begin the process of converting the quantum information into binary information for analysis or additional processing.

Quantum Computing with AWS Braket

There are several ways to run the following example. Locally, by installing Python and the AWS Braket SDK for Python, or in the cloud by activating AWS Braket in your account.

Right, after that cursory and crash course in Quantum Computing and setup, let us write some code and fire off some quantum circuits!

There are several quantum providers for AWS Braket. AWS offers simulators on classical hardware, but also enables access to D-Wave, IonQ, Rigetti.

Within each of those there are many different implementations of the layout of the Qubits, number of Qubits, and Qubit volume. What we care most about is going to be the implementation based on our problem. For instance, if we know we are going to be solving for NP-Hard problems - problems at least as hard as non-deterministic polynomial problems - to determine decisions with a focus on combinatorial complexity, quantum annealers will be our friend. On the contrast, Universal Gate-Model QPUs (Quantum Processing Units) are as they sound, built to provide circuits for universal problem solving.

AWS Braket is generally available for anybody who activates it within their AWS Account. They offer real hardware, provisioned based on a queue, and simulators for immediate testing. Simulators run on optimized instances. You can do a lot of development on AWS by provisioning a Jupyter notebook – something data scientists and machine learning professionals are familiar with. AWS Braket is also a Python SDK that enables the compositions of circuits and executing that on locally simulated Qubits. Let us look at an example.

from Braket.aws import AwsDevice
from Braket.devices import LocalSimulator
from Braket.circuits import Circuit

def bell_state_example():
  # Our Quantum Circuit
  # (h) Apply the Hadamard gate to the first Qubit, this puts it into superposition
  # (cnot) Apply a Controlled Not gate to the first Qubit (control) and a second Qubit (target)
  # This entagles the two Qubits. This is known as the Bell State.
  
  bell_state = Circuit().h(0).cnot(0, 1)
  
  # AWS Braket can print a textual representation of your circuit
  print(bell_state)
  
  # This will run a quantum simulation on your local machine, try to keep below 20-ish Qubits
  simulator = LocalSimulator()
  
  # AWS Braket simulations are ran async, but here we can block on the result for a local simulation.
  # Shots is unique to quantum computing, you must run the same algorithm multiple times to get ample
  # enough results for your probability distribution of results. This is where you decide the most
  # probable result of your algorithm, which may be the "right" result
  simulation = simulator.run(bell_state, shots=1000)
  result = simulation.result()
  
  # Measure our results
  counts = result.measurement_counts
  
  # What this displays is how often your entagled Qubits ended up in the states of '11' or '00'.
  # This sums to the number of shots
  print(counts)
  
  return
  
if __name__ == "__main__":
  bell_state_example()

This code, and other examples, can be found on GitHub.

This is a simple sample of a bell state in quantum computing. This is analogous to the programmer’s “Hello World” application in classical computing.

For the most part it is straight forward in implementation, the SDK functions representing quantum computation circuit gates, seen at the top, build QASM (Quantum Assembly Language, an Assembly and C-Like language using the OpenQASM 2.0 specification) that will be sent as instructions, over the internet, to an awaiting quantum computer. However, in this sample we see we can also simulate quantum operations within our local environment up to a few tens of Qubits.

To create a bell state, which creates an entangled state between two Qubits, we apply a Hadamard gate on the control Qubit, and a controlled not gate onto a target Qubit. From here we can run a local simulation and launch an asynchronous request to execute the quantum circuit.

Integrating Applications with AWS Braket

In the real world, this application would run on a classical computer, and when the run method is called, we are making a request to AWS Braket to execute a given quantum circuit on the target hardware. We can then operate as normal and handle a response when it is complete.

There are several methods of integrating results from AWS Braket into existing infrastructure. These methods are through the AWS Braket SDK, handling events from AWS Event Bridge, or enabling S3 Bucket Notifications for when results are added as an S3 Object to an S3 Bucket.

One of the simplest methods for a developer to leverage AWS Braket is to use the AWS Braket SDK to issue requests for Quantum Computing. An existing Python Application can await asynchronously for request to code to complete and use the results to continue processing. The caveat to this is that AWS Braket Quantum Computations are executed via a queue of requests shared across all AWS Accounts; therefore, you may have stalling code in your application as it awaits a response.

For this reason, there are two decoupled and cloud friendly methods for integrating with AWS Braket results. The advantage is you are no longer limited to Python and just need to handle data when it comes it using an eventing model in your infrastructure.

Those familiar with Serverless application infrastructure may be delighted to discover AWS Braket publishes execution results to AWS Event Bridge. An AWS Lambda Function, or other serverless application, or a long running server, can handle AWS Braket results in an eventually consistent manner by leveraging AWS Event Bridge. The event contains, among other state data, the result state of the execution and the location of the data in S3.

Another approach to handling the results of AWS Braket quantum computations is to integrate directly with the S3 Bucket that stores the results of computations.Developers can deploy an AWS Lambda function to handle an S3:ObjectCreated:* S3 Bucket Notification. This event will contain the data necessary to get the results stored in S3 by AWS Braket.

Going Further

Quantum Computing is still in a young state, so there is much to discover. What we do know is that we can start creating algorithms now in preparation for the future and apply the smaller ones on real hardware available to us in the cloud. There are many algorithms available to explore, each with different use cases and advantages. A comprehensive list can be found here on Quantum Algorithm Zoo.

The ideal application for Quantum Computing for businesses is to use it as a hybrid computational model. Developers can use classical computing and languages to build applications integrating Quantum Computing. When a difficult combinatorial problem presents itself, a cloud native or integrated hybrid infrastructure can call out to AWS Braket to perform a Quantum Circuit.

There are many examples as to the use cases of Quantum Computing. Such use cases of Quantum Hybrid applications is material design, optimizing battery design through simulation, or chemical engineering. These all are real world examples within business, and there are many more other industries that can be explored with Quantum Computing. The real question is, when the Quantum Revolution comes, will your company be ready?

Additional Quantum Computing Resources

Here are a couple of other resources to help you get started in Quantum Computing with AWS Braket.

Here are highly recommended readings for anybody looking to get serious with Quantum Computing.

  • Quantum Computing since Democritus by Scott Aaronson, ISBN: 9780511979309
  • Quantum Computation and Quantum Information by Isaac Chung and Michael Nielsen, ISBN: 9780511976667

Get your project started today

Get in Touch