Tutorial: Solve an optimization problem using an Azure Quantum notebook

Note

Please note that Toshiba SQBM+ optimization solver will be deprecated and no longer available in the Azure Quantum service soon.

Learn how to use Azure Quantum optimization solvers to solve a simple binary optimization problem. This tutorial uses free hosted notebooks in the Azure Quantum portal.

Prerequisites

To complete this tutorial, you need

Create a new Notebook in your workspace

  1. Log in to the Azure portal and select the workspace you created in the previous step.
  2. In the left blade, select Notebooks.
  3. Click My Notebooks and click Add New.
  4. In Kernel Type, select IPython.
  5. Type a name for the file, for example SimpleOptimization.ipynb, and click Create file.

When your new Notebook opens, it automatically creates the code for the first cell, based on your subscription and workspace information.

from azure.quantum import Workspace
workspace = Workspace (
    subscription_id = <your subscription ID>, 
    resource_group = <your resource group>,   
    name = <your workspace name>,          
    location = <your location>        
    )

You'll need to import two additional modules. Click + Code to add a new cell and add the following lines:

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

Express a simple problem

Click + Code to add another new cell and add the following lines:

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

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

This code creates an instance of a Problem and sets the problem_type to ProblemType.pubo. For more information, see ProblemType.

Next, add another cell to 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 a 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.

Add another cell with the following code that opens the solver, submits the problem, and displays the result:

from azure.quantum.target.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 Notebook:

{... 'solutions': [{'cost': -14.0, 'configuration': {'0': 1, '1': 1, '2': 0, '3': 1}...}

For more information about solver results, see Interpreting solver results.

Note

If you run into an error while working with Azure Quantum, you can check our list of common issues.

Next steps