Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hello-world and QAOA samples from Pasqal #607

Merged
merged 21 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
10c719b
Add QAOA sample from Pasqal
ArnaudPel May 7, 2024
141f6b0
Add hello-world sample from Pasqal
ArnaudPel May 7, 2024
7380d94
Apply suggestions from code review
ArnaudPel May 7, 2024
55d8c90
Add hint about where to find resource_id and location
ArnaudPel May 7, 2024
233f177
Apply suggestions from a-corni
ArnaudPel May 14, 2024
bc65571
Add requirements-pulser.txt and show cost estimation of real QPU
ArnaudPel May 22, 2024
6a3d902
Remove redundant wait and fix link
ArnaudPel May 22, 2024
996c7eb
Split one markdown string
ArnaudPel May 22, 2024
35b2fd2
Align targets listing with other HW notebooks
ArnaudPel May 23, 2024
9f7cd63
Remove "price estimation" step as it is not implemented by Pasqal
ArnaudPel May 23, 2024
fbf03cf
Fix error 422 + remove unnecessary import
ArnaudPel May 27, 2024
ceb2a21
Merge branch 'main' into arnaudpel/pasqal-samples
kikomiss May 29, 2024
05451e1
Merge branch 'main' into arnaudpel/pasqal-samples
kikomiss Jun 22, 2024
6831447
Merge branch 'main' into arnaudpel/pasqal-samples
kikomiss Jun 24, 2024
152238a
Inline one dict + fix one typo in number of shots
ArnaudPel Jun 24, 2024
18b1c34
Depend on full pulser + Remove some redundant imports
ArnaudPel Jun 24, 2024
9fff8e3
Move introduction up
ArnaudPel Jun 24, 2024
8355bbc
Put shots param on a single line + increase it to 100
ArnaudPel Jun 24, 2024
6737922
Remove assignment to target.latest_session
ArnaudPel Jun 24, 2024
332e85d
Use context manager to close session
ArnaudPel Jun 24, 2024
3d481f5
Really inline number of shots
ArnaudPel Jun 24, 2024
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
357 changes: 357 additions & 0 deletions samples/hello-world/HW-pasqal-pulser.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,357 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 👋🌍 Hello, world: Submit a Pulser job to Pasqal\n",
"\n",
"In this notebook, we'll review the basics of Azure Quantum by submitting a simple *job*, or quantum program, to [Pasqal](https://pasqal.com/). We will use [Pulser](https://pulser.readthedocs.io/) to express the quantum job in an analog way."
ArnaudPel marked this conversation as resolved.
Show resolved Hide resolved
],
"id": "8493267a95ed8cbd"
},
{
"cell_type": "markdown",
"metadata": {
"nteract": {
"transient": {
"deleting": false
}
}
},
"source": [
"## Submit a simple job to Pasqal using Azure Quantum\n",
"Azure Quantum provides several ways to express quantum programs. In this example we are using Pulser, the analog quantum computing library of Pasqal. All code in this example will be written in Python.\n",
ArnaudPel marked this conversation as resolved.
Show resolved Hide resolved
"\n",
"Let's begin. 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."
],
"id": "808523d9ed21b01b"
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"try:\n",
" import pulser\n",
"except ImportError:\n",
" !pip -q install pulser-core\n",
" print(\"Please restart kernel to complete the installation of pulser.\")"
],
"id": "261dab3c8880aeee"
},
kikomiss marked this conversation as resolved.
Show resolved Hide resolved
{
"cell_type": "markdown",
"metadata": {
"nteract": {
"transient": {
"deleting": false
}
}
},
"source": "### 1. Connect to the Azure Quantum workspace\n",
"id": "658fc276abc991eb"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from azure.quantum import Workspace\n",
"\n",
"workspace = Workspace(\n",
" resource_id = \"\",\n",
ArnaudPel marked this conversation as resolved.
Show resolved Hide resolved
" location = \"\",\n",
")"
],
"id": "4f3047eec84a00af"
},
{
"cell_type": "markdown",
"metadata": {
"nteract": {
"transient": {
"deleting": false
}
}
},
"source": "Let's see whether the Pasqal provider is enabled in this workspace with the following command:\n",
"id": "d3ad6d0abf4de566"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"outputs_hidden": false,
"source_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
}
},
"outputs": [],
"source": [
"target = workspace.get_targets(name=\"pasqal.sim.emu-tn\")\n",
ArnaudPel marked this conversation as resolved.
Show resolved Hide resolved
"print(target)"
],
"id": "95dbec7c0e4f59df"
},
{
"cell_type": "markdown",
"metadata": {
"nteract": {
"transient": {
"deleting": false
}
}
},
"source": [
"### ❕ Do you see `Target name=\"pasqal.sim.emu-tn\"` ? If so, you're ready to keep going.\n",
"\n",
"Don't see it? You may need to add Pasqal to your workspace to run this sample. Navigate to the **Providers** page in the portal and click **+Add** to add the Pasqal provider. Don't worry, there's a free credits plan available."
],
"id": "64dc34808344e811"
},
{
"cell_type": "markdown",
"metadata": {
"nteract": {
"transient": {
"deleting": false
}
}
},
"source": [
"## Pasqal: The quantum provider\n",
"Azure Quantum partners with third-party companies to deliver solutions to quantum jobs. These company offerings are called *providers*. Each provider can offer multiple *targets* with different capabilities. See the table below for Pasqal's targets.\n",
"\n",
"| Target name | Target ID | Number of qubits | Description |\n",
"|-------------|----------------------|-------------------------------|--------------------------------------------------------------------------------------------------------------------------------------|\n",
"| Emu-TN | `pasqal.sim.emu-tn` | 100 qubits 1D and 2D networks | Simulates the time-evolution of a quantum state using the Schrödinger equation corresponding to the actions that the lasers perform. |\n",
"| Fresnel1 | `pasqal.qpu.fresnel` | 100 qubits | PASQAL's neutral atoms quantum computer. |\n",
"\n",
"For this example, we will use `pasqal.sim.emu-tn`. To learn more about Pasqal's targets, check out our [documentation](https://learn.microsoft.com/azure/quantum/provider-pasqal)."
],
"id": "32fb070cacaba76d"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. Build the quantum program\n",
"\n",
"Let's create a simple Pulser sequence to run on an array (register) of neutral atoms."
ArnaudPel marked this conversation as resolved.
Show resolved Hide resolved
],
"id": "a58addf868b244dd"
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"import math\n",
"import pulser\n",
"\n",
"reg = pulser.Register.rectangle(1, 2, spacing=8, prefix=\"atom\")\n",
"reg.draw()\n",
"\n",
"duration = 1000 # Typical: ~1 µsec\n",
"pi_pulse = pulser.Pulse.ConstantDetuning(\n",
" pulser.BlackmanWaveform(duration, math.pi), 0.0, 0.0\n",
")\n",
"\n",
"seq = pulser.Sequence(reg, pulser.DigitalAnalogDevice)\n",
"\n",
"seq.declare_channel(\"ryd\", \"rydberg_local\", \"atom0\")\n",
"\n",
"seq.add(pi_pulse, \"ryd\")\n",
"seq.target(\"atom1\", \"ryd\")\n",
"seq.add(pi_pulse, \"ryd\")\n",
"seq.measure()\n",
"\n",
"seq.draw()"
],
"id": "9681bbcd96eee008"
},
{
"cell_type": "markdown",
"metadata": {
"nteract": {
"transient": {
"deleting": false
}
}
},
"source": [
"Each pulse acts on a set of atoms at a certain moment of time. The entire `Sequence` is stored by Pulser and can then be either simulated or sent to a real device.\n",
"\n",
"We've just built a sequence sending the same π-pulse to two atoms, sequentially, using the same channel. With Pasqal's idealized simulator, we will be able to simulate the quantum evolution of both atoms over time."
],
"id": "c3d23fc53155e243"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "### 3. Submit the quantum program to Pasqal",
"id": "95f8578dfb2660f4"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Using the Pasqal simulator target, call \"run\" to submit the job. We'll\n",
"# use 10 repetitions (simulated runs).\n",
"input_args = dict(\n",
kikomiss marked this conversation as resolved.
Show resolved Hide resolved
" input_data=dict(sequence_builder=seq.to_abstract_repr()),\n",
" input_params={\"count\": 100}, # Number of shots\n",
ArnaudPel marked this conversation as resolved.
Show resolved Hide resolved
")\n",
"\n",
"job = target.submit(\n",
" **input_args, \n",
" input_data_format=\"pasqal.pulser.v1\", \n",
" output_data_format=\"pasqal.pulser-results.v1\",\n",
" name=\"Pasqal sequence\",\n",
")\n",
"\n",
"# Print the job ID.\n",
"print(\"Job id:\", job.id)"
],
"id": "ed2e79aac388baa9"
},
{
"cell_type": "markdown",
"metadata": {
"nteract": {
"transient": {
"deleting": false
}
}
},
"source": [
"The job ID can be used to retrieve the results later using the [get_job method](https://learn.microsoft.com/python/azure-quantum/azure.quantum.workspace?#azure-quantum-workspace-get-job) or by viewing it under the **Job management** section of the portal."
],
"id": "bb4c8c91778444da"
},
{
"cell_type": "markdown",
"metadata": {
"nteract": {
"transient": {
"deleting": false
}
}
},
"source": [
"### 4. Obtain the job results\n",
"Let's await the job execution by calling `job.get_results()`. This may take a minute or so ⏳. Your job is being packaged and sent to Pasqal, where it will wait its turn to be run."
],
"id": "f2111a90ca2d482a"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Await job results.\n",
"print(\"Awaiting job results...\")\n",
"job.wait_until_completed()\n",
ArnaudPel marked this conversation as resolved.
Show resolved Hide resolved
"result = job.get_results()\n",
"\n",
"# To view each measured qubit state, you can print the result.\n",
"print(\"Job finished. Measure count for each state:\")\n",
"print(result)"
],
"id": "4bf1813d0e966f42"
},
{
"cell_type": "markdown",
"metadata": {
"nteract": {
"transient": {
"deleting": false
}
}
},
"source": "**See the result above? Congratulations, you've submitted a Pasqal job with Azure Quantum! 👏**",
"id": "a13268e76598db7b"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5. Estimate costs\n",
"\n",
"To estimate the costs of running this program on a simulator or hardware, you can use the `target.estimate_cost` method.\n",
"\n",
"Note that cost emulation for Pasqal emulators has not been implemented yet."
kikomiss marked this conversation as resolved.
Show resolved Hide resolved
],
"id": "c873638c19d5265c"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"estimate = target.estimate_cost(**input_args)\n",
"print(estimate)"
],
"id": "e6c76075e7c69fb9"
},
{
"cell_type": "markdown",
"metadata": {
"nteract": {
"transient": {
"deleting": false
}
}
},
"source": [
"### 6. Next steps\n",
"Next, you can try running a program on Pasqal's hardware target. Just replace `pasqal.sim.emu-tn` with `pasqal.qpu.fresnel`. Don't worry - your work here is automatically saved.\n",
"\n",
"To learn more about submitting Pulser sequences to Azure Quantum, review [this documentation](https://learn.microsoft.com/en-us/azure/quantum/quickstart-microsoft-provider-format#platform-pasqal).\n",
ArnaudPel marked this conversation as resolved.
Show resolved Hide resolved
"\n",
"To learn more about job pricing, review the [Azure Quantum documentation on job costs](https://learn.microsoft.com/en-us/azure/quantum/pricing#pasqal)."
],
"id": "2e02e1b62a75939e"
}
],
"metadata": {
"kernel_info": {
"name": "python3"
},
"kernelspec": {
"display_name": "Python 3",
"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.3"
},
"nteract": {
"version": "nteract-front-end@1.0.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading
Loading