-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 1036 | Docs: tutorial notebook
- Loading branch information
Showing
4 changed files
with
240 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from qiskit import QuantumCircuit | ||
from qiskit.primitives import Sampler | ||
|
||
from quantum_serverless import save_result | ||
|
||
# all print statement will be available in job logs | ||
print("Running program...") | ||
|
||
# creating circuit | ||
circuit = QuantumCircuit(2) | ||
circuit.h(0) | ||
circuit.cx(0, 1) | ||
circuit.measure_all() | ||
|
||
# running Sampler primitive | ||
sampler = Sampler() | ||
quasi_dists = sampler.run(circuit).result().quasi_dists | ||
|
||
# saves results of program execution, | ||
# which will be accessible by calling `.result()` | ||
save_result(quasi_dists) | ||
print("Completed running program.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "22e5c830-772d-41cc-94fa-bd7a8d2d6ffd", | ||
"metadata": {}, | ||
"source": [ | ||
"# Upload program and run by name (Experimental)\n", | ||
"\n", | ||
"This tutorial explains how to upload and reuse uploaded programs.\n", | ||
"\n", | ||
"First let's import necessary libraries, create provider and create our program." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "94fd2c47-fc43-4733-b762-a108bb74b719", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from quantum_serverless import QuantumServerless, ServerlessProvider, Program\n", | ||
"import os" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "35a44703-2ffe-4b99-afdc-340985d4c478", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"<QuantumServerless | providers [gateway-provider]>" | ||
] | ||
}, | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"provider = ServerlessProvider(\n", | ||
" username=os.environ.get(\"GATEWAY_USER\", \"user\"),\n", | ||
" password=os.environ.get(\"GATEWAY_PASSWORD\", \"password123\"),\n", | ||
" # token=os.environ.get(\"GATEWAY_TOKEN\", \"<TOKEN>\"), # token can be used instead of user/password combination\n", | ||
" host=os.environ.get(\"GATEWAY_HOST\", \"http://localhost:8000\"),\n", | ||
")\n", | ||
"\n", | ||
"serverless = QuantumServerless(provider)\n", | ||
"serverless" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"id": "770b6947-fa92-466b-b714-aa0422ab53c6", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"Program(hello_qiskit)" | ||
] | ||
}, | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"program = Program(\n", | ||
" title=\"hello_qiskit\", entrypoint=\"program.py\", working_dir=\"./source_files/\"\n", | ||
")\n", | ||
"program" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "4f1e4b08-1052-4d21-a196-60d490176e8a", | ||
"metadata": {}, | ||
"source": [ | ||
"Now we have our program, let's upload it to serverless using `upload` method of `QuantumServerless` object." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 11, | ||
"id": "c7d534bb-a827-4b13-b45b-64bf1575bd5a", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"'hello_qiskit'" | ||
] | ||
}, | ||
"execution_count": 11, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"serverless.upload(program)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "da58408f-1939-46db-b197-cd195c73853d", | ||
"metadata": {}, | ||
"source": [ | ||
"We can see list of uploaded/available programs by calling `get_programs` method of `QuantumServerless` object." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 13, | ||
"id": "fd695745-3e25-402c-8218-00015764486c", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"[Program(hello_qiskit),\n", | ||
" Program(Hello, Qiskit!),\n", | ||
" Program(Program with parallel workflow),\n", | ||
" Program(Program with dependencies),\n", | ||
" Program(Program with arguments),\n", | ||
" Program(First program)]" | ||
] | ||
}, | ||
"execution_count": 13, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"serverless.get_programs()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "d396e1bd-eca7-4d19-a617-40cd548b7fdb", | ||
"metadata": {}, | ||
"source": [ | ||
"We can see our program in a list of available program. Now we can call `run` method and provide only name of our program to run it." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 14, | ||
"id": "2b3f8f88-7168-4dc8-849d-e9b8b92dae68", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"<Job | 14383080-bc37-47d3-b40d-89cacef6ffba>" | ||
] | ||
}, | ||
"execution_count": 14, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"job = serverless.run(\"hello_qiskit\")\n", | ||
"job" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 15, | ||
"id": "86996633-e59b-4312-b7c3-05c4d1608bee", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"[{'0': 0.4999999999999999, '3': 0.4999999999999999}]" | ||
] | ||
}, | ||
"execution_count": 15, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"job.result()" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"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.9.16" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |