Install and use the Python SDK for optimization

This guide provides a basic overview of how to install and use the Python SDK for optimization.


Python SDK for optimization installation

The Python SDK is distributed as the azure-quantum PyPI package. To install the package you will need to follow the steps below:

  1. Install Python 3.6 or later.

  2. Install PIP, the Python Package Installer, and ensure you have version 19.2 or higher

  3. Install the azure-quantum Python package:

    pip install azure-quantum

Jupyter Notebooks installation

You can also choose to interact with Azure Quantum optimization using Jupyter Notebooks. In order to do this, you will need to:

  1. Install the Python SDK for optimization (as described in the previous section)

  2. Install Jupyter Notebooks

  3. In your terminal of choice, use the following command to launch a new Jupyter Notebook:

    jupyter notebook

    This will launch a new browser window (or a new tab) showing the Notebook Dashboard, a sort of control panel that allows you (among other things) to select which notebook to open.

  4. In the browser view, select the dropdown button on the right hand top corner and select Python 3 from the list. This should create a new notebook.

Usage example

Whether you choose to solve optimization problems using Jupyter Notebooks or a Python script, once you have installed the prerequisites from the previous sections you can follow the instructions below to run a test problem.

Connecting to an Azure Quantum workspace

A Workspace represents the Azure Quantum workspace you previously created and is the main interface for interacting with the service.

from typing import List
from azure.quantum.optimization import Term
from azure.quantum import Workspace

workspace = Workspace (
    subscription_id = "",  # Add your subscription_id
    resource_group = "",   # Add your resource_group
    name = "",             # Add your workspace name
    location = ""          # Add your workspace location (for example, "westus")


When you run the command workspace.login() for the first time, you will see the following displayed in your console:

To sign in, use a web browser to open the page and enter the code [RANDOM-CODE] to authenticate.

Once you sign into your Azure account, your credentials will be cached so that you do not have to repeat this process for future runs.

Expressing and solving a simple problem

To express a simple problem to be solved, create an instance of a Problem and set the problem_type to either ProblemType.ising or ProblemType.pubo. For more information, see ProblemType.

from azure.quantum.optimization import Problem, ProblemType, Term, ParallelTempering

problem = Problem(name="My First Problem", problem_type=ProblemType.ising)

Next, create an array of Term objects and add them to the Problem:

terms = [
    Term(c=-9, indices=[0]),
    Term(c=-3, indices=[1,0]),
    Term(c=5, indices=[2,0]),
    Term(c=9, indices=[2,1]),
    Term(c=2, indices=[3,0]),
    Term(c=-4, indices=[3,1]),
    Term(c=4, indices=[3,2])



There are multiple ways to supply terms to the problem, and not all terms must be added at once.

Next, we're ready to apply a solver. In this example we'll use a parameter-free version of parallel tempering. You can find documentation on this solver and the other available solvers in the Microsoft QIO provider reference.

solver = ParallelTempering(workspace, timeout=100)

result = solver.optimize(problem)

This method will submit the problem to Azure Quantum for optimization and synchronously wait for it to be solved. You'll see output like the following in your terminal window or Jupyter Notebook:

{'configuration': {'0': 1, '1': 1, '2': -1, '3': 1}, 'cost': -32.0}

Next steps


Samples and end-to-end learning