Quickstart: Solve an optimization problem in Azure Quantum

Learn how to use optimization solvers in Azure Quantum to solve a simple binary optimization problem.

Prerequisites

To complete this tutorial, you need

Jupyter Notebooks installation

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

  1. Install the azure-quantum Python package (as described in Prerequisites)

  2. Install Jupyter Notebooks

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

    jupyter notebook
    

    This launches a new browser window (or a new tab) showing the Notebook Dashboard, a control panel that allows you to create and manage your notebooks.

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

Create and connect to an Azure Quantum workspace

A Workspace represents a Azure Quantum workspace 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")
    )

The first time you run a method which interacts with the Azure service, a window might prompt in your default browser asking for your credentials. You can optionally pass a credential to be used in the authentication in the construction of the Workspace object or via its credentials property. See more at Azure.Quantum.Workspace

Note

The workspace.login() method has been deprecated and is no longer necessary. The first time there is a call to the service, an authentication will be attempted using the credentials passed in the Workspace constructor or its credentials property. If no credentials were passed, several authentication methods will be attempted by the DefaultAzureCredential.

Express 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

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])
]

problem.add_terms(terms=terms)

Note

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

Apply an optimization solver

For the Microsoft QIO provider, you'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.

from azure.quantum.optimization import ParallelTempering

solver = ParallelTempering(workspace, timeout=100)

result = solver.optimize(problem)
print(result)

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:

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

Prerequisites

To complete this tutorial, you need

Jupyter Notebooks installation

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

  1. Install the azure-quantum Python package (as described in Prerequisites)

  2. Install Jupyter Notebooks

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

    jupyter notebook
    

    This launches a new browser window (or a new tab) showing the Notebook Dashboard, a control panel that allows you to create and manage your notebooks.

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

Create and connect to an Azure Quantum workspace

A Workspace represents a Azure Quantum workspace 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")
    )

The first time you run a method which interacts with the Azure service, a window might prompt in your default browser asking for your credentials. You can optionally pass a credential to be used in the authentication in the construction of the Workspace object or via its credentials property. See more at Azure.Quantum.Workspace

Note

The workspace.login() method has been deprecated and is no longer necessary. The first time there is a call to the service, an authentication will be attempted using the credentials passed in the Workspace constructor or its credentials property. If no credentials were passed, several authentication methods will be attempted by the DefaultAzureCredential.

Express 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

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])
]

problem.add_terms(terms=terms)

Note

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

Apply an optimization solver

For the 1QBit provider, we'll use the path-relinking solver. You can find documentation on this solver and the other available solvers in the 1QBit provider reference.


from azure.quantum.optimization.oneqbit import PathRelinkingSolver

solver = PathRelinkingSolver(workspace)

result = solver.optimize(problem)
print(result)

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:

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

Prerequisites

To complete this tutorial, you need

Jupyter Notebooks installation

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

  1. Install the azure-quantum Python package (as described in Prerequisites)

  2. Install Jupyter Notebooks

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

    jupyter notebook
    

    This launches a new browser window (or a new tab) showing the Notebook Dashboard, a control panel that allows you to create and manage your notebooks.

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

Create and connect to an Azure Quantum workspace

A Workspace represents a Azure Quantum workspace 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")
    )

The first time you run a method which interacts with the Azure service, a window might prompt in your default browser asking for your credentials. You can optionally pass a credential to be used in the authentication in the construction of the Workspace object or via its credentials property. See more at Azure.Quantum.Workspace

Note

The workspace.login() method has been deprecated and is no longer necessary. The first time there is a call to the service, an authentication will be attempted using the credentials passed in the Workspace constructor or its credentials property. If no credentials were passed, several authentication methods will be attempted by the DefaultAzureCredential.

Express 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

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

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])
]

problem.add_terms(terms=terms)

Note

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

Apply an optimization solver

For the Toshiba SQBM+ provider, we'll use the Ising solver, namely SimulatedBifurcationMachine. You can find documentation on this solver in the Toshiba SQBM+ provider reference.

from azure.quantum.optimization.toshiba import SimulatedBifurcationMachine

solver = SimulatedBifurcationMachine(workspace)

result = solver.optimize(problem)
print(result)

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:

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

Note

If you run into an error while working with Azure Quantum, you can check our list of common issues. Also if your are using an optimization solver and you get an error in the form <AZQxxx>, you can check our list of common user errors in optimization solvers.

Next steps

Documentation

Samples and end-to-end learning