# Tutorial: Solve an optimization problem using an Azure Quantum notebook

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 procedure, 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 (
)


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.ising)


This code creates an instance of a Problem and sets the problem_type to ProblemType.ising. 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=),
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])
]



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 Microsoft QIO provider, you'll use the parameter-free version of parallel tempering. For more information about the parallel tempering solver and other Microsoft QIO solvers, see the Microsoft QIO provider reference.

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

from azure.quantum.optimization import ParallelTempering

solver = ParallelTempering(workspace, timeout=100)

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


This method submits the problem to Azure Quantum for optimization and synchronously wait for it to be solved.

Click Run all and you'll see output like the following in your Notebook:

......{'version': '1.0', 'configuration': {'0': 1, '1': 1, '2': -1, '3': 1}, 'cost': -32.0, 'parameters': {'all_betas': [0.05263157894736842, 0.08483446830185856, 0.13674085322912188, 0.22040641399786567, 0.05263157894736842, 0.06507156662728714, 0.0804518668832599, 0.09946745130748995, 0.12297755481503365, 0.15204450088433905, 0.18798170352254984, 0.2324130149640922, 0.2873461007774075, 0.05263157894736842, 0.06032292374035997, 0.0691382474431204, 0.0792418033330163, 0.09082184793061673, 0.1040941487268661, 0.11930600451390022, 0.13674085322912186, 0.1567235531691094, 0.1796264359766303, 0.20587624418424752, 0.23596208257968374, 0.27044453154834486, 0.3099660921991755, 0.05263157894736842, 0.05819611133264724, 0.06434896011059557, 0.07115232568798963, 0.07867498467898539, 0.08699298518197843, 0.09619041556538521, 0.10636025453415848, 0.11760531107053311, 0.13003926375105576, 0.14378780994656645, 0.1589899365226302, 0.17579932488618855, 0.19438590458233618, 0.2149375711468486, 0.23766208558058496, 0.2627891746479403, 0.29057285323169424, 0.32129399222141625, 0.05263157894736842, 0.05699027110239624, 0.061709929006167785, 0.06682044609165143, 0.0723541914209791, 0.07834621470504914, 0.08483446830185858, 0.09186004759966065, 0.09946745130749, 0.10770486330168681, 0.1166244578135835, 0.1262827298913559, 0.1367408532291219, 0.14806506762971203, 0.16032709855522653, 0.17360461142273625, 0.18798170352255, 0.20354943667476758, 0.22040641399786576, 0.23865940444246173, 0.25842401904593415, 0.27982544319117336, 0.3029992295074498, 0.3280921564354881], 'replicas': 70, 'sweeps': 600}, 'solutions': [{'configuration': {'0': 1, '1': 1, '2': -1, '3': 1}, 'cost': -32.0}]}


## Prerequisites

To complete this procedure, 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 (
)


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.ising)


This code creates an instance of a Problem and sets the problem_type to ProblemType.ising. 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=),
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])
]



Note

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

## Apply an optimization solver

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



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


This method submits the problem to Azure Quantum for optimization and synchronously wait for it to be solved.

Click Run all and you'll see output like the following in your Notebook:

......{'version': '1.0', 'configuration': {'0': 1, '1': 1, '2': -1, '3': 1}, 'cost': -32.0, 'parameters': {'all_betas': [0.05263157894736842, 0.08483446830185856, 0.13674085322912188, 0.22040641399786567, 0.05263157894736842, 0.06507156662728714, 0.0804518668832599, 0.09946745130748995, 0.12297755481503365, 0.15204450088433905, 0.18798170352254984, 0.2324130149640922, 0.2873461007774075, 0.05263157894736842, 0.06032292374035997, 0.0691382474431204, 0.0792418033330163, 0.09082184793061673, 0.1040941487268661, 0.11930600451390022, 0.13674085322912186, 0.1567235531691094, 0.1796264359766303, 0.20587624418424752, 0.23596208257968374, 0.27044453154834486, 0.3099660921991755, 0.05263157894736842, 0.05819611133264724, 0.06434896011059557, 0.07115232568798963, 0.07867498467898539, 0.08699298518197843, 0.09619041556538521, 0.10636025453415848, 0.11760531107053311, 0.13003926375105576, 0.14378780994656645, 0.1589899365226302, 0.17579932488618855, 0.19438590458233618, 0.2149375711468486, 0.23766208558058496, 0.2627891746479403, 0.29057285323169424, 0.32129399222141625, 0.05263157894736842, 0.05699027110239624, 0.061709929006167785, 0.06682044609165143, 0.0723541914209791, 0.07834621470504914, 0.08483446830185858, 0.09186004759966065, 0.09946745130749, 0.10770486330168681, 0.1166244578135835, 0.1262827298913559, 0.1367408532291219, 0.14806506762971203, 0.16032709855522653, 0.17360461142273625, 0.18798170352255, 0.20354943667476758, 0.22040641399786576, 0.23865940444246173, 0.25842401904593415, 0.27982544319117336, 0.3029992295074498, 0.3280921564354881], 'replicas': 70, 'sweeps': 600}, 'solutions': [{'configuration': {'0': 1, '1': 1, '2': -1, '3': 1}, 'cost': -32.0}]}


## 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 (
)


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



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}...}


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

### Samples and end-to-end learning

• QIO samples repo
• Getting started