AWS Quantum Technologies Blog

Using D-Wave Leap from the AWS Marketplace with Amazon Braket Notebooks and Braket SDK

Starting today, access to D-Wave products and services has fully transitioned to the AWS Marketplace, and customers can no longer access the D-Wave 2000Q and Advantage systems via Amazon Braket, the cloud computing service of AWS. In this blog, we will show how you can continue to use the Amazon Braket SDK to describe quantum annealing problems, and how you can use Braket managed Jupyter Notebooks to submit these problems to D-Wave quantum processors.

D-Wave’s Leap quantum cloud service offering provides on-demand access to the D-Wave 2000Q, the latest Advantage systems, and the hybrid solver service. Follow this link to read more details about the Leap quantum cloud service and to purchase access to the service for your needs. Since you will be accessing D-Wave devices via the Leap service, there will be no additional Braket-related charges besides your managed Jupyter notebook instance.

Figure 1: D-Wave’s Leap software-as-a-service offering on the AWS Marketplace.

Setup

If you want to use the Leap quantum cloud service from a Braket Jupyter notebook, you will need to use an existing notebook or create a new one by following the step-by-step instructions in the developer guide here.

With the Braket notebook instance running, first install or update the latest version of D-Wave’s software libraries. The D-Wave Ocean SDK is no longer included in the latest Braket notebooks. These libraries will make it easier to interact with Leap. In a notebook, run a cell with the following command to install the software.

!pip install -U dwave-ocean-sdk

Now, configure your environment to use the credentials you obtained from D-Wave to authenticate with their service. The service will look for the credentials in the default config directory of the current user, so you can copy the details to a file, and then authenticate to D-Wave Leap directly from an Amazon Braket notebook. To experiment with other available options, you can open a terminal in your notebook instance and run the dwave config command, but for now, you only need the authentication token. To create this file in your notebook, modify the following block with your token. You only need to complete these steps once.

import os
os.makedirs('/home/ec2-user/.config/dwave/')

with open('/home/ec2-user/.config/dwave/dwave.conf', "w") as f:
    print("[defaults]",
        "token = ABC-123456789123456789123456789", # <- Edit 
with your authentication token from D-Wave
        sep='\n', file=f)</p>

At this point, you have installed D-Wave’s software for interacting with their service, and configured your environment to use the credentials. For every new project, you can simply create a new notebook in this instance and continue to use the environment. Let’s start a new notebook now to show how to use the Leap offering to solve an annealing problem specified using the Amazon Braket Python SDK.

First, import the Python packages you are going to use for solving your annealing problem.

import dwave.cloud
from braket.annealing import Problem, ProblemType
from braket.tasks import AnnealingQuantumTaskResult
import numpy

Next, define an annealing problem formulated as a quadratic unconstrained binary optimization (QUBO) using the Braket SDK, as shown with an example input here.

p = Problem(ProblemType('QUBO'), linear={0: 1, 4: 1}, quadratic={(0, 4): -2})

This is a simple two variable problem. This problem can be thought of as the minimization of x+y−2xy where x and y must be either 0 or 1. By inspecting the problem, it is clear that the value of this function is minimized when x=y=0 or when x=y=1. In either case, the cost function has value 0.

Submit your workload to the D-Wave Leap service

Next, select a D-Wave annealing system to use for this example. You can constrain your choice of solvers to use devices with minimum qubit counts, or with certain connectivity, or to use D-Wave’s hybrid systems. The following example uses the default system. To connect to this system and prepare to submit tasks, run the following in your notebook.

client = dwave.cloud.Client.from_config()
solver = client.get_solver()

The D-Wave Ocean client handles QUBO problems differently from the Braket SDK, so before you submit the problem, you must reformulate the problem by packing all of the linear and quadratic terms into one statement. Use the following wrapper function to translate problems to a form that the Ocean client can interpret:

def sample_problem(solver: dwave.cloud.Solver, p: Problem, 
**solver_params) -> dwave.cloud.Future:
    if p.problem_type == 'QUBO':
        return solver.sample_qubo({**p.quadratic, **{(u, u): 
x for u,x in p.linear.items()}}, **solver_params)
    elif p.problem_type == 'ISING':
        return solver.sample_ising(p.linear, p.quadratic, 
**solver_params)
    else:
        raise ValueError(f"Unknown problem type 
'{p.problem_type}'")

You are now ready to run your annealing problem on a quantum annealer. Run the following to submit the task.

computation = sample_problem(solver, p)

Because the quantum devices are shared, you may not obtain your results immediately. Instead, D-Wave provides a Future object that describes how to fetch the results at a later time. Let’s define another function, which uses this feature to obtain results for you as soon as they are ready.

def braket_result(computation: dwave.cloud.Future) -> 
AnnealingQuantumTaskResult:
    solution = computation.samples
    value = computation.energies
    solution_count = computation.num_occurrences

    num_solutions, variable_count = solutions.shape
    datatypes = [
        ("solution", solutions.dtype, (variable_count,)),
        ("value", values.dtype),
        ("solution_count", solution_counts.dtype),
    ]

    record = numpy.rec.array(numpy.zeros(num_solutions, dtype=datatypes))
    record["solution"] = solutions
    record["value"] = values
    record["solution_count"] = solution_counts

    return AnnealingQuantumTaskResult(record, variable_count, computation.problem_type, None, None)

Putting these together, you can receive results for your annealing problem by running

result = braket_result(sample_problem(solver, p))

The result object returned here is an AnnealingQuantumTaskResult from the Braket SDK, so any workflow expecting these results as input will work with Leap from AWS Marketplace by making these minor modifications. To manually inspect the results obtained from running this problem, print the record of measurements:

print(result.record_array)

You should see a list of measurements, where for each measurement, the first entry is the state of the qubits, the second entry is the energy level, and the last entry is the number of times that state was observed. For example, for this problem you may see output like,

[([0, 0], 0., 488) ([1, 1], 0., 512)]

This represents 488 and 512 measurements of the two qubits in the two separate ground states of the system. The exact numbers will vary due to the probabilistic nature of the computational process, but as we expect, the quantum annealer finds the ground state of this simple system every time with ease.

Conclusion

We have shown how to use the AWS Marketplace to access D-Wave quantum annealing computers and use the Braket SDK to solve a basic annealing problem using the Jupyter notebook development environment available in Braket. To learn more about quantum computing with AWS and the quantum processors available directly on Amazon Braket, refer to our Getting Started Page. To learn more about running hybrid quantum-classical workloads on gate-based or analog quantum processors, refer to Amazon Braket Hybrid Jobs. For more information on the Ocean SDK, refer to the Ocean documentation from D-Wave.