Skip to content
This repository has been archived by the owner on Jan 30, 2024. It is now read-only.

Add hello world samples #32

Open
wants to merge 1 commit into
base: feature/samples-gallery
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
260 changes: 260 additions & 0 deletions samples/hello-world/HW-1qbit.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 👋🌍 Hello, world: Submit an optimization job to 1QBit\n",
"\n",
"This notebook will walk through how to use Azure Quantum to submit optimization problems to [1QBit](https://1qbit.com/).\n",
"\n",
"## Submit a simple job to 1QBit using Azure Quantum\n",
"\n",
"Let's begin. All code in this example will be written in Python. When you see a code block, hover over it and click the triangle play-button to execute it. To avoid any compilation issues, this should be done in order from top to bottom."
]
},
{
"cell_type": "markdown",
"metadata": {
"nteract": {
"transient": {
"deleting": false
}
}
},
"source": [
"### 1. Connect to the Azure Quantum workspace\n",
"The first step is connecting to your Azure Quantum workspace. To do so, simply create a `Workspace` object as seen in the following code cell. The parameters corresponding to your workspace should have been automatically entered for you."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"outputs_hidden": false,
"source_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
}
},
"outputs": [],
"source": [
"from azure.quantum import Workspace\n",
"\n",
"workspace = Workspace (\n",
" subscription_id = \"\",\n",
" resource_group = \"\",\n",
" name = \"\",\n",
" location = \"\"\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"nteract": {
"transient": {
"deleting": false
}
}
},
"source": [
"Let's use the `Workspace` object to view the available _targets_."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"targets = workspace.get_targets()\n",
"print(\"This workspace's targets:\")\n",
"for target in targets:\n",
" print(\"-\", target.name)\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"nteract": {
"transient": {
"deleting": false
}
}
},
"source": [
"The resulting list should contain a list of target names, along with some details for each one. A _target_ is a remote solver that can perform optimization or quantum computation. In this example, we will focus on optimization."
]
},
{
"cell_type": "markdown",
"metadata": {
"nteract": {
"transient": {
"deleting": false
}
}
},
"source": [
"### ❕ Do you see `1qbit.tabu` in your list of targets? If so, you're ready to keep going.\n",
"\n",
"Don't see it? You may need to add 1QBit to your workspace to run this sample. Navigate to the **Providers** page in the portal and click **+Add** to add the 1QBit provider."
]
},
{
"cell_type": "markdown",
"metadata": {
"nteract": {
"transient": {
"deleting": false
}
}
},
"source": [
"### 1QBit: The optimization provider\n",
"Azure Quantum connects with first- and third-party optimizers to deliver optimization solutions. These offerings are called *providers*. Each provider can offer multiple *targets* with different capabilities. See the table below for 1QBit's targets.\n",
"\n",
"Optimization Algorithm | Target ID | Python Class\n",
"| --- | ---| ---| --- | -- |\n",
"[Tabu search](https://docs.microsoft.com/azure/quantum/provider-1qbit#tabu-search-solver) | `1qbit.tabu` | `azure.quantum.optimization.oneqbit.TabuSearch` |\n",
"[Path relinking](https://docs.microsoft.com/azure/quantum/optimization-parallel-tempering) | `1qbit.pticm` | `azure.quantum.optimization.oneqbit.PathRelinkingSolver`\n",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Path relinking URL points to our solver - should be: "https://docs.microsoft.com/en-us/azure/quantum/provider-1qbit#path-relinking-solver" ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Table cell for path relinking says pticm but should be "1qbit.pathrelinking"?

"[Parallel tempering with isoenergetic cluster moves](https://docs.microsoft.com/azure/quantum/provider-1qbit#pticm-solver) | `1qbit.pathrelinking` | `azure.quantum.optimization.oneqbit.PticmSolver`\n",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Table cell for parallel tempering says pathrelinking but should be "1qbit.pticm"?

"\n",
"For this example, we will use `1qbit.tabu`. To learn more about 1QBit's targets, check out our [documentation](https://docs.microsoft.com/en-us/azure/quantum/provider-1qbit#pticm-solver)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. Define an optimization job\n",
"We will demonstrate how to use Azure Quantum for optimization by submitting a very simple cost function. Let's define one that contains just two terms."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from typing import List\n",
"from azure.quantum.optimization import Problem, ProblemType, Term\n",
"\n",
"terms = [Term(c=1, indices=[1, 0]), Term(c=2, indices=[0, 1])]\n",
"problem = Problem(name=\"Optimization problem\", problem_type=ProblemType.ising, terms=terms)\n",
"\n",
"print(\"Name:\", problem.name)\n",
"print(\"Number of terms:\", len(problem.terms))\n",
"print(\"Problem type:\", problem.problem_type)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nteract": {
"transient": {
"deleting": false
}
}
},
"source": [
"You should see the problem details in the output. Note that this problem uses `ProblemType.ising`, which means it uses an [Ising model](https://docs.microsoft.com/azure/quantum/optimization-concepts-ising-model-for-optimization). [Binary optimization](https://docs.microsoft.com/azure/quantum/optimization-binary-optimization) formulations (such as PUBO) are also supported. Read more about problem types [here](https://docs.microsoft.com/azure/quantum/optimization-problem-type). "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. Submit the job\n",
"Now that we've defined a problem, we can submit it to Azure Quantum. Note that this will consume a few seconds of computation time, and depending on the offer you chose when adding the 1QBit provider, charges may apply."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from azure.quantum.optimization.oneqbit import TabuSearch\n",
"import time\n",
"\n",
"# Instantiate a solver to solve the problem. \n",
"solver = TabuSearch(workspace, timeout=100)\n",
"\n",
"# Optimize the problem.\n",
"print('Submitting problem...')\n",
"start = time.time()\n",
"result = solver.optimize(problem)\n",
"time_elapsed = time.time() - start\n",
"solutions = result['solutions']\n",
"\n",
"# Print the solution(s).\n",
"print(f'\\nObtained result in {time_elapsed} seconds.')\n",
"print(f'Computed {len(solutions)} solution(s).')\n",
"for i in range(len(solutions)):\n",
" print(f'Solution {i}:')\n",
" print('\\tCost:', solutions[i]['cost'])\n",
" print('\\tConfiguration:', solutions[i]['configuration'])"
]
},
{
"cell_type": "markdown",
"metadata": {
"nteract": {
"transient": {
"deleting": false
}
}
},
"source": [
"**See a solutiuon listed above? Congratulations, you've submitted an optimization job with Azure Quantum! 👏**"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: "solution"

]
},
{
"cell_type": "markdown",
"metadata": {
"nteract": {
"transient": {
"deleting": false
}
}
},
"source": [
"### 4. Next steps\n",
"Next, you can try submitting an optimization job to other solvers that 1QBit offers. Or try submitting to another provider entirely by navigating back to the sample gallery. The same \"hello world\" sample can be run with different quantum providers by choosing another option in the gallery card drop-down menu. Don't worry - your work here is automatically saved."
]
}
],
"metadata": {
"kernel_info": {
"name": "python3"
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.12"
},
"nteract": {
"version": "[email protected]"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading