From 8c48db240c693e84d3fae375fd2bc49b8d62e1d2 Mon Sep 17 00:00:00 2001 From: Iskandar Sitdikov Date: Fri, 14 Apr 2023 09:30:40 -0400 Subject: [PATCH 1/2] Issue 411 | Client: rename run_qiskit_remote to distribute_task --- client/quantum_serverless/__init__.py | 1 + client/quantum_serverless/core/__init__.py | 10 +++++- client/quantum_serverless/core/decorators.py | 28 +++++++++++++++-- .../quantum_serverless/library/transpiler.py | 6 ++-- client/tests/core/test_decorator.py | 31 ++++++++++++++++++- client/tests/resources/test-compose.yml | 2 +- 6 files changed, 70 insertions(+), 8 deletions(-) diff --git a/client/quantum_serverless/__init__.py b/client/quantum_serverless/__init__.py index 4ef673e8f..925443bf0 100644 --- a/client/quantum_serverless/__init__.py +++ b/client/quantum_serverless/__init__.py @@ -17,6 +17,7 @@ from .core import ( Provider, run_qiskit_remote, + distribute_task, get, put, get_refs_by_status, diff --git a/client/quantum_serverless/core/__init__.py b/client/quantum_serverless/core/__init__.py index 102b110af..84bb59800 100644 --- a/client/quantum_serverless/core/__init__.py +++ b/client/quantum_serverless/core/__init__.py @@ -29,6 +29,7 @@ Provider ComputeResource + distribute_task run_qiskit_remote get put @@ -56,7 +57,14 @@ """ from .provider import Provider, ComputeResource, KuberayProvider, GatewayProvider -from .decorators import remote, get, put, run_qiskit_remote, get_refs_by_status +from .decorators import ( + remote, + get, + put, + run_qiskit_remote, + get_refs_by_status, + distribute_task, +) from .events import RedisEventHandler, EventHandler, ExecutionMessage from .state import RedisStateHandler, StateHandler from .job import save_result diff --git a/client/quantum_serverless/core/decorators.py b/client/quantum_serverless/core/decorators.py index 8ced942b6..053ffcc64 100644 --- a/client/quantum_serverless/core/decorators.py +++ b/client/quantum_serverless/core/decorators.py @@ -27,9 +27,11 @@ put run_qiskit_remote get_refs_by_status + distribute_task """ import functools import os +import warnings from dataclasses import dataclass from typing import Optional, Dict, Any, Union, List, Callable, Sequence @@ -207,6 +209,29 @@ def wraps(*args, **kwargs): def run_qiskit_remote( target: Optional[Union[Dict[str, Any], Target]] = None, state: Optional[StateHandler] = None, +): + """(Deprecated) Wraps local function as remote executable function. + New function will return reference object when called. + + Args: + target: target object or dictionary for requirements for node resources + state: state handler + + Returns: + object reference + """ + warnings.warn( + "Decorator `run_qiskit_remote` is deprecated. " + "Please, consider using `distribute_task` instead.", + DeprecationWarning, + stacklevel=2, + ) + return distribute_task(target, state) + + +def distribute_task( + target: Optional[Union[Dict[str, Any], Target]] = None, + state: Optional[StateHandler] = None, ): """Wraps local function as remote executable function. New function will return reference object when called. @@ -214,7 +239,7 @@ def run_qiskit_remote( Example: >>> import quantum_serverless as qs >>> - >>> @run_qiskit_remote() + >>> @distribute_task() >>> def awesome_function(seed: int): >>> return 42 >>> @@ -224,7 +249,6 @@ def run_qiskit_remote( Args: target: target object or dictionary for requirements for node resources state: state handler - events: events handler Returns: object reference diff --git a/client/quantum_serverless/library/transpiler.py b/client/quantum_serverless/library/transpiler.py index 224d76c31..96655d52f 100644 --- a/client/quantum_serverless/library/transpiler.py +++ b/client/quantum_serverless/library/transpiler.py @@ -31,12 +31,12 @@ from qiskit.providers import Backend from quantum_serverless.exception import QuantumServerlessException -from quantum_serverless import run_qiskit_remote, get, put +from quantum_serverless import distribute_task, get, put -transpile_ray = run_qiskit_remote()(transpile) +transpile_ray = distribute_task()(transpile) -@run_qiskit_remote() +@distribute_task() def remote_transpile( circuits: List[Union[QuantumCircuit, List[QuantumCircuit]]], backends: List[Backend] ) -> List[List[QuantumCircuit]]: diff --git a/client/tests/core/test_decorator.py b/client/tests/core/test_decorator.py index 063b2c249..9e5389ef2 100644 --- a/client/tests/core/test_decorator.py +++ b/client/tests/core/test_decorator.py @@ -20,6 +20,7 @@ from quantum_serverless import QuantumServerless, get from quantum_serverless.core.decorators import ( run_qiskit_remote, + distribute_task, Target, fetch_execution_meta, ) @@ -65,6 +66,34 @@ def ultimate_function(ultimate_argument: int): ) return mid_result + with self.assertWarns(Warning), serverless.context(): + reference = ultimate_function(1) + result = get(reference) + self.assertEqual(result, 4) + + def test_distribute_task(self): + """Test for run_qiskit_remote.""" + + serverless = QuantumServerless() + + @distribute_task() + def another_function( + circuit: List[QuantumCircuit], other_circuit: QuantumCircuit + ): + """Another test function.""" + return circuit[0].compose(other_circuit, range(5)).depth() + + @distribute_task(target={"cpu": 1}) + def ultimate_function(ultimate_argument: int): + """Test function.""" + print("Printing function argument:", ultimate_argument) + mid_result = get( + another_function( + [random_circuit(5, 2)], other_circuit=random_circuit(5, 2) + ) + ) + return mid_result + with serverless.context(): reference = ultimate_function(1) result = get(reference) @@ -81,7 +110,7 @@ def test_remote_with_state_injection(self): serverless = QuantumServerless() state_handler = TestHandler() - @run_qiskit_remote(target={"cpu": 1}, state=state_handler) + @distribute_task(target={"cpu": 1}, state=state_handler) def ultimate_function_with_state(state: StateHandler, ultimate_argument: int): """Test function.""" state.set("some_key", {"result": ultimate_argument}) diff --git a/client/tests/resources/test-compose.yml b/client/tests/resources/test-compose.yml index ce9c42f81..51c900c92 100644 --- a/client/tests/resources/test-compose.yml +++ b/client/tests/resources/test-compose.yml @@ -1,7 +1,7 @@ services: testrayhead: container_name: testrayhead - image: qiskit/quantum-serverless-ray-node:latest + image: qiskit/quantum-serverless-ray-node:nightly entrypoint: [ "ray", "start", "--head", "--port=6379", "--dashboard-host=0.0.0.0", "--block" From d132068712b981754ebf13e69abae8275349d374 Mon Sep 17 00:00:00 2001 From: Iskandar Sitdikov Date: Fri, 14 Apr 2023 10:57:24 -0400 Subject: [PATCH 2/2] Issue 411 | Docs: rename run_qiskit_remote to distribute_task --- docs/getting_started/01_intro_level_1.ipynb | 34 ++++++------- docs/getting_started/02_intro_level_2.ipynb | 15 +++--- docs/getting_started/03_intro_level_3.ipynb | 50 ++++++------------- .../source_files/gs_level_3.py | 6 +-- ..._run_anything_in_distributed_fashion.ipynb | 4 +- .../02_specific_resource_allocation.ipynb | 4 +- ...e_primitives_with_quantum_serverless.ipynb | 4 +- .../03_specific_package_installation.ipynb | 4 +- docs/guides/04_run_programs.ipynb | 4 +- docs/guides/05_get_finished_tasks.ipynb | 4 +- docs/guides/06_shared_state.ipynb | 8 +-- docs/guides/07_working_with_datasets.ipynb | 4 +- docs/tutorials/01_quantum_serverless.ipynb | 21 ++------ ...pproaches_to_serverless_code_writing.ipynb | 18 ++----- ...ting_serverless_pacakge_from_scratch.ipynb | 6 +-- ...nverting_existing_code_to_serverless.ipynb | 6 +-- .../06_electronic_structure_problem.ipynb | 4 +- docs/tutorials/07_benchmark_program.ipynb | 48 ++++++------------ docs/tutorials/source_files/benchmark.py | 14 +++--- .../electronic_structure_problem.py | 4 +- 20 files changed, 101 insertions(+), 161 deletions(-) diff --git a/docs/getting_started/01_intro_level_1.ipynb b/docs/getting_started/01_intro_level_1.ipynb index 159d1b9b8..7b4fef471 100644 --- a/docs/getting_started/01_intro_level_1.ipynb +++ b/docs/getting_started/01_intro_level_1.ipynb @@ -39,7 +39,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 15, "id": "81dd7807-7180-4b87-bbf9-832b7cf29d69", "metadata": {}, "outputs": [], @@ -49,7 +49,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 16, "id": "acdec789-4967-48ee-8f6c-8d2b0ff57e91", "metadata": {}, "outputs": [ @@ -59,7 +59,7 @@ "" ] }, - "execution_count": 2, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } @@ -76,7 +76,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "4dd85621-9ab0-4f34-9ab4-07ad773c5e00", "metadata": {}, @@ -91,17 +90,17 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 17, "id": "d51df836-3f22-467c-b637-5803145d5d8a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 3, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -120,7 +119,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "39ee31d2-3553-4e19-bcb9-4cccd0df0e4c", "metadata": {}, @@ -130,7 +128,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 19, "id": "cc7ccea6-bbae-4184-ba7f-67b6c20a0b0b", "metadata": {}, "outputs": [ @@ -140,7 +138,7 @@ "'SUCCEEDED'" ] }, - "execution_count": 4, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -151,7 +149,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 14, "id": "ca76abfa-2ff5-425b-a225-058d91348e8b", "metadata": {}, "outputs": [ @@ -161,7 +159,7 @@ "'Quasi distribution: {0: 0.4999999999999999, 3: 0.4999999999999999}\\n'" ] }, - "execution_count": 5, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -181,17 +179,17 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 20, "id": "f942b76d-596c-4384-8f36-e5f73e72cefd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'e4a801d2-f9eb-4392-b584-cbdd600755c8'" + "'aae3533f-ed0d-4e93-9247-4705d476ee97'" ] }, - "execution_count": 6, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } @@ -210,17 +208,17 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 21, "id": "45e2927f-655b-47a4-8003-f16e5ba0a1cd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 7, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } diff --git a/docs/getting_started/02_intro_level_2.ipynb b/docs/getting_started/02_intro_level_2.ipynb index 052d37a21..255ebb4db 100644 --- a/docs/getting_started/02_intro_level_2.ipynb +++ b/docs/getting_started/02_intro_level_2.ipynb @@ -100,7 +100,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "id": "b6ec8969-8c3d-4b7f-8c4c-adc6dbb9c59f", "metadata": {}, "outputs": [ @@ -110,7 +110,7 @@ "" ] }, - "execution_count": 2, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -127,7 +127,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "544f7c64-ae1e-4480-b5d0-93f0c335eccd", "metadata": {}, @@ -139,17 +138,17 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "id": "3ee09b31-4c7f-4ff3-af8f-294e4256793e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 3, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -173,7 +172,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 9, "id": "420f2711-b8c6-4bf9-8651-c9d098348467", "metadata": {}, "outputs": [ @@ -183,7 +182,7 @@ "'SUCCEEDED'" ] }, - "execution_count": 6, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } diff --git a/docs/getting_started/03_intro_level_3.ipynb b/docs/getting_started/03_intro_level_3.ipynb index a1478edb8..d5b97c196 100644 --- a/docs/getting_started/03_intro_level_3.ipynb +++ b/docs/getting_started/03_intro_level_3.ipynb @@ -20,12 +20,12 @@ "from qiskit.quantum_info import SparsePauliOp\n", "from qiskit.primitives import Estimator\n", "\n", - "from quantum_serverless import QuantumServerless, run_qiskit_remote, get, put, save_result\n", + "from quantum_serverless import QuantumServerless, ditribute_task, get, put, save_result\n", "\n", "# 1. let's annotate out function to convert it\n", "# to function that can be executed remotely\n", - "# using `run_qiskit_remote` decorator\n", - "@run_qiskit_remote()\n", + "# using `ditribute_task` decorator\n", + "@ditribute_task()\n", "def my_function(circuit: QuantumCircuit, obs: SparsePauliOp):\n", " \"\"\"Compute expectation value of an obs given a circuit\"\"\"\n", " return Estimator().run([circuit], [obs]).result().values\n", @@ -70,7 +70,7 @@ "\n", "As you can see we move to advanced section of using serverless. \n", "\n", - "Here we are using `run_qiskit_remote` decorator to convert our function to asynchronous distributed one. \n", + "Here we are using `ditribute_task` decorator to convert our function to asynchronous distributed one. \n", "With that `my_function` is converted into asynchronous distributed function (as a result you will be getting function pointer), which means that the function no longer executes as part of your local python process, but executed on configured compute resources.\n", "\n", "Moreover, we are using `save_result` function in order to save results into database storage, so we can retrieve it later after program execution.\n", @@ -80,7 +80,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 16, "id": "9130c64a-1e7f-4d08-afff-b2905b2d95ad", "metadata": {}, "outputs": [], @@ -90,7 +90,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 17, "id": "0f22daae-9f0e-4f7a-8a1f-5ade989d8be9", "metadata": {}, "outputs": [ @@ -100,7 +100,7 @@ "" ] }, - "execution_count": 4, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -126,17 +126,17 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 18, "id": "f556dd85-35da-48d1-9ae1-f04a386544d9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 6, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -156,7 +156,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 20, "id": "2de3fd64-9010-48d9-ac7c-f46a7b36ba81", "metadata": {}, "outputs": [ @@ -166,7 +166,7 @@ "'SUCCEEDED'" ] }, - "execution_count": 7, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } @@ -175,26 +175,6 @@ "job.status()" ] }, - { - "cell_type": "code", - "execution_count": 8, - "id": "d6586e7a-388b-42cc-a860-abd4f6d514b9", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Single execution: [1.]\n", - "N parallel executions: [array([1.]), array([0.97400357]), array([1.])]\n", - "\n" - ] - } - ], - "source": [ - "print(job.logs())" - ] - }, { "cell_type": "markdown", "id": "29336f0b-ffcf-4cdb-931c-11faf09f15ff", @@ -205,17 +185,17 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 21, "id": "1fb8931f-c8e2-49dd-923f-16fa3a7a5feb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'{\"status\": \"ok\", \"single\": [1.0], \"parallel_result\": [[1.0], [0.9740035726118753], [1.0]]}'" + "'{\"status\": \"ok\", \"single\": [1.0], \"parallel_result\": [[1.0], [1.0], [1.0]]}'" ] }, - "execution_count": 9, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } diff --git a/docs/getting_started/source_files/gs_level_3.py b/docs/getting_started/source_files/gs_level_3.py index 21a03edcd..6314aba46 100644 --- a/docs/getting_started/source_files/gs_level_3.py +++ b/docs/getting_started/source_files/gs_level_3.py @@ -3,12 +3,12 @@ from qiskit.quantum_info import SparsePauliOp from qiskit.primitives import Estimator -from quantum_serverless import QuantumServerless, run_qiskit_remote, get, put, save_result +from quantum_serverless import QuantumServerless, distribute_task, get, put, save_result # 1. let's annotate out function to convert it # to function that can be executed remotely -# using `run_qiskit_remote` decorator -@run_qiskit_remote() +# using `distribute_task` decorator +@distribute_task() def my_function(circuit: QuantumCircuit, obs: SparsePauliOp): """Compute expectation value of an obs given a circuit""" return Estimator().run([circuit], [obs]).result().values diff --git a/docs/guides/01_run_anything_in_distributed_fashion.ipynb b/docs/guides/01_run_anything_in_distributed_fashion.ipynb index 571ff4726..8d109d0e5 100644 --- a/docs/guides/01_run_anything_in_distributed_fashion.ipynb +++ b/docs/guides/01_run_anything_in_distributed_fashion.ipynb @@ -25,7 +25,7 @@ "from qiskit import QuantumCircuit, transpile\n", "from qiskit.circuit.random import random_circuit\n", "\n", - "from quantum_serverless import QuantumServerless, run_qiskit_remote, get, put" + "from quantum_serverless import QuantumServerless, distribute_task, get, put" ] }, { @@ -187,7 +187,7 @@ "\n", "# in order to make any function a remote/parallel function annotate it with `remote` decorator\n", "\n", - "@run_qiskit_remote()\n", + "@distribute_task()\n", "def remote_transpile(circuits: List[QuantumCircuit]):\n", " return transpile(circuits)\n", "\n", diff --git a/docs/guides/02_specific_resource_allocation.ipynb b/docs/guides/02_specific_resource_allocation.ipynb index 1545c9169..995346d8d 100644 --- a/docs/guides/02_specific_resource_allocation.ipynb +++ b/docs/guides/02_specific_resource_allocation.ipynb @@ -20,7 +20,7 @@ "from qiskit import QuantumCircuit, transpile\n", "from qiskit.circuit.random import random_circuit\n", "\n", - "from quantum_serverless import QuantumServerless, run_qiskit_remote, get" + "from quantum_serverless import QuantumServerless, distribute_task, get" ] }, { @@ -63,7 +63,7 @@ "metadata": {}, "outputs": [], "source": [ - "@run_qiskit_remote(target={\"cpu\": 2})\n", + "@distribute_task(target={\"cpu\": 2})\n", "def remote_transpile(circuits: List[QuantumCircuit]):\n", " return transpile(circuits)" ] diff --git a/docs/guides/03_runtime_primitives_with_quantum_serverless.ipynb b/docs/guides/03_runtime_primitives_with_quantum_serverless.ipynb index b24b33af8..adf807701 100644 --- a/docs/guides/03_runtime_primitives_with_quantum_serverless.ipynb +++ b/docs/guides/03_runtime_primitives_with_quantum_serverless.ipynb @@ -32,12 +32,12 @@ "from qiskit.quantum_info import SparsePauliOp\n", "from qiskit_ibm_runtime import Estimator\n", "\n", - "from quantum_serverless import QuantumServerless, run_qiskit_remote, get, put\n", + "from quantum_serverless import QuantumServerless, distribute_task, get, put\n", "\n", "# 1. let's annotate out function to convert it\n", "# to function that can be executed remotely\n", "# using `run_qiskit_remote` decorator\n", - "@run_qiskit_remote()\n", + "@distribute_task()\n", "def my_function(circuit: QuantumCircuit, obs: SparsePauliOp):\n", " return Estimator().run([circuit], [obs]).result().values\n", "\n", diff --git a/docs/guides/03_specific_package_installation.ipynb b/docs/guides/03_specific_package_installation.ipynb index b4b6beeba..93d2b98c0 100644 --- a/docs/guides/03_specific_package_installation.ipynb +++ b/docs/guides/03_specific_package_installation.ipynb @@ -24,7 +24,7 @@ "outputs": [], "source": [ "import requests\n", - "from quantum_serverless import QuantumServerless, run_qiskit_remote, get" + "from quantum_serverless import QuantumServerless, distribute_task, get" ] }, { @@ -56,7 +56,7 @@ "metadata": {}, "outputs": [], "source": [ - "@run_qiskit_remote(target={\"pip\": [\"requests\"]})\n", + "@distribute_task(target={\"pip\": [\"requests\"]})\n", "def call_ibm():\n", " return requests.get(\"https://ibm.com\").status_code" ] diff --git a/docs/guides/04_run_programs.ipynb b/docs/guides/04_run_programs.ipynb index ba9fed89d..64cee8ed8 100644 --- a/docs/guides/04_run_programs.ipynb +++ b/docs/guides/04_run_programs.ipynb @@ -1,7 +1,6 @@ { "cells": [ { - "attachments": {}, "cell_type": "markdown", "id": "83581f24-f41b-4d0a-81c5-70402812ce75", "metadata": {}, @@ -53,7 +52,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "abe78b1e-ec3b-430a-b57f-b3262ff4ee93", "metadata": {}, @@ -73,7 +71,7 @@ "from quantum_serverless import QuantumServerless, run_qiskit_remote, get\n", "\n", "\n", - "@run_qiskit_remote()\n", + "@distribute_task()\n", "def ultimate():\n", " return 42\n", "\n", diff --git a/docs/guides/05_get_finished_tasks.ipynb b/docs/guides/05_get_finished_tasks.ipynb index de21b41d3..28dd3c430 100644 --- a/docs/guides/05_get_finished_tasks.ipynb +++ b/docs/guides/05_get_finished_tasks.ipynb @@ -26,7 +26,7 @@ "from quantum_serverless import (\n", " QuantumServerless, \n", " get, \n", - " run_qiskit_remote,\n", + " distribute_task,\n", " get_finished,\n", " get_refs_by_status\n", ")" @@ -50,7 +50,7 @@ } ], "source": [ - "@run_qiskit_remote()\n", + "@distribute_task()\n", "def sleepy_function(sleep_time: int):\n", " time.sleep(sleep_time)\n", " return sleep_time\n", diff --git a/docs/guides/06_shared_state.ipynb b/docs/guides/06_shared_state.ipynb index 83d234afc..9a2d18176 100644 --- a/docs/guides/06_shared_state.ipynb +++ b/docs/guides/06_shared_state.ipynb @@ -66,7 +66,7 @@ ], "source": [ "gateway_provider = GatewayProvider(\n", - " username=\"john\",\n", + " username=\"user\",\n", " password=\"password123\",\n", " host=\"http://localhost:8000\",\n", ")\n", @@ -106,7 +106,7 @@ "\n", "```python\n", "# job_with_state.py\n", - "from quantum_serverless import QuantumServerless, run_qiskit_remote, get\n", + "from quantum_serverless import QuantumServerless, distribute_task, get\n", "\n", "from quantum_serverless.core.state import RedisStateHandler\n", "\n", @@ -114,13 +114,13 @@ "serverless = QuantumServerless()\n", "\n", "\n", - "@run_qiskit_remote(state=state_handler)\n", + "@distribute_task(state=state_handler)\n", "def func_with_state(state: RedisStateHandler, seed: int):\n", " state.set(\"in_job\", {\"k\": seed})\n", " return seed\n", "\n", "\n", - "@run_qiskit_remote(state=state_handler)\n", + "@distribute_task(state=state_handler)\n", "def other_func_with_state(state: RedisStateHandler, seed: int):\n", " state.set(\"in_other_job\", {\"other_k\": seed})\n", " return get(func_with_state(seed))\n", diff --git a/docs/guides/07_working_with_datasets.ipynb b/docs/guides/07_working_with_datasets.ipynb index 6dc02fd3e..b48d0f082 100644 --- a/docs/guides/07_working_with_datasets.ipynb +++ b/docs/guides/07_working_with_datasets.ipynb @@ -24,7 +24,7 @@ "from qiskit import QuantumCircuit\n", "from qiskit.circuit.random import random_circuit\n", "from qiskit.primitives import Sampler\n", - "from quantum_serverless import QuantumServerless, run_qiskit_remote, get\n", + "from quantum_serverless import QuantumServerless, distribute_task, get\n", "\n", "from ray import data # let's import data from ray" ] @@ -211,7 +211,7 @@ "metadata": {}, "outputs": [], "source": [ - "@run_qiskit_remote()\n", + "@distribute_task()\n", "def sample(data):\n", " sampler = Sampler()\n", " circuits = [r[\"circuit\"] for r in data.iter_rows()]\n", diff --git a/docs/tutorials/01_quantum_serverless.ipynb b/docs/tutorials/01_quantum_serverless.ipynb index 680389146..de4ed7678 100644 --- a/docs/tutorials/01_quantum_serverless.ipynb +++ b/docs/tutorials/01_quantum_serverless.ipynb @@ -1,7 +1,6 @@ { "cells": [ { - "attachments": {}, "cell_type": "markdown", "id": "978d83ac-86ca-4fb3-8b43-81c02c7be120", "metadata": {}, @@ -101,7 +100,7 @@ "Example\n", "\n", "```python\n", - "@quantum_serverless.run_qiskit_remote()\n", + "@quantum_serverless.distribute_task()\n", "def exp_val_remote(cirucit, obs):\n", " estimator = Estimator(...)\n", " return estimator.run(circuit, obs)\n", @@ -166,7 +165,7 @@ "Example\n", "\n", "```python\n", - "@quantum_serverless.run_qiskit_remote(target={\"cpu\": 2, \"mem\": 8})\n", + "@quantum_serverless.distribute_task(target={\"cpu\": 2, \"mem\": 8})\n", "def exp_val_remote(cirucit, obs):\n", " estimator = Estimator(...)\n", " exp_val_result = estimator.run(circuit, obs)\n", @@ -207,7 +206,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "3a8c5fc8-5e85-4d47-aa9f-1e3b849783a4", "metadata": {}, @@ -221,7 +219,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "6277cbd3-a1ac-4a8f-8a29-c9c92732e780", "metadata": {}, "outputs": [], @@ -229,11 +227,11 @@ "from qiskit.circuit.random import random_circuit\n", "from qiskit.primitives import Estimator\n", "from qiskit.quantum_info import SparsePauliOp\n", - "from quantum_serverless import QuantumServerless, run_qiskit_remote, put, get\n", + "from quantum_serverless import QuantumServerless, distribute_task, put, get\n", "\n", "serverless = QuantumServerless()\n", "\n", - "@run_qiskit_remote()\n", + "@distribute_task()\n", "def exp_val_remote(circuit, obs):\n", " estimator = Estimator()\n", " return estimator.run(circuit, obs).result()\n", @@ -253,7 +251,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "a962b872-427d-47c6-9eea-56a53bdfcdc6", "metadata": {}, @@ -385,14 +382,6 @@ "source": [ "print(job.logs())" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "b65df46e-303c-48f5-9638-8c7d11e7c828", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/docs/tutorials/02_tutorial_-_approaches_to_serverless_code_writing.ipynb b/docs/tutorials/02_tutorial_-_approaches_to_serverless_code_writing.ipynb index c949ea962..2f04c9845 100644 --- a/docs/tutorials/02_tutorial_-_approaches_to_serverless_code_writing.ipynb +++ b/docs/tutorials/02_tutorial_-_approaches_to_serverless_code_writing.ipynb @@ -36,7 +36,7 @@ "from qiskit.providers.aer import AerSimulator\n", "from qiskit.providers.fake_provider import FakeVigo, FakeAlmaden, FakeBrooklyn, FakeCasablanca\n", "\n", - "from quantum_serverless import QuantumServerless, run_qiskit_remote, get, put" + "from quantum_serverless import QuantumServerless, distribute_task, get, put" ] }, { @@ -93,7 +93,7 @@ "\n", "# your_module.transpiler\n", "\n", - "@run_qiskit_remote()\n", + "@distribute_task()\n", "def remote_transpile(circuits: List[QuantumCircuit], backend: Backend):\n", " return transpile(circuits=circuits, backend=backend) " ] @@ -108,18 +108,10 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "10a3945b-05ca-4a9e-b7e2-0f872c5385be", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Results: [[, , , , , , , , , ], [, , , , , , , , , ], [, , , , , , , , , ], [, , , , , , , , , ]]\n" - ] - } - ], + "outputs": [], "source": [ "# ================\n", "# User perspective\n", @@ -180,7 +172,7 @@ "\n", "# your_module.transpiler\n", "\n", - "@run_qiskit_remote()\n", + "@distribute_task()\n", "def remote_transpile(circuits: List[QuantumCircuit], backend: Backend):\n", " return transpile(circuits=circuits, backend=backend) \n", "\n", diff --git a/docs/tutorials/03_tutorial_writing_serverless_pacakge_from_scratch.ipynb b/docs/tutorials/03_tutorial_writing_serverless_pacakge_from_scratch.ipynb index f0edca45b..9d77800d3 100644 --- a/docs/tutorials/03_tutorial_writing_serverless_pacakge_from_scratch.ipynb +++ b/docs/tutorials/03_tutorial_writing_serverless_pacakge_from_scratch.ipynb @@ -52,7 +52,7 @@ "from qiskit.circuit.random import random_circuit\n", "from qiskit.opflow import X, I\n", "\n", - "from quantum_serverless import QuantumServerless, put, get, run_qiskit_remote" + "from quantum_serverless import QuantumServerless, put, get, distribute_task" ] }, { @@ -71,13 +71,13 @@ ], "source": [ "# classical funciton that will be executed as remote task\n", - "@run_qiskit_remote()\n", + "@distribute_task()\n", "def add_prefix(circuit: QuantumCircuit, prefix_depth) -> QuantumCircuit:\n", " return random_circuit(circuit.num_qubits, prefix_depth).compose(circuit)\n", "\n", "# quantum related compute that should be executed as closer to quantum as possible\n", "# using resource QPU\n", - "@run_qiskit_remote()\n", + "@distribute_task()\n", "def measure_exp_val(circuits: List[QuantumCircuit], observable) -> EstimatorResult:\n", " with Estimator(circuits, observable) as estimator:\n", " return estimator(range(len(circuits)), [0] * len(circuits))\n", diff --git a/docs/tutorials/04_tutorial_-_converting_existing_code_to_serverless.ipynb b/docs/tutorials/04_tutorial_-_converting_existing_code_to_serverless.ipynb index a021d6b39..06b30e9df 100644 --- a/docs/tutorials/04_tutorial_-_converting_existing_code_to_serverless.ipynb +++ b/docs/tutorials/04_tutorial_-_converting_existing_code_to_serverless.ipynb @@ -20,7 +20,7 @@ "\n", "warnings.filterwarnings('ignore')\n", "\n", - "from quantum_serverless import QuantumServerless, run_qiskit_remote, get, put" + "from quantum_serverless import QuantumServerless, distribute_task, get, put" ] }, { @@ -172,7 +172,7 @@ "metadata": {}, "outputs": [], "source": [ - "remote_transpile = run_qiskit_remote()(transpile)" + "remote_transpile = distribute_task()(transpile)" ] }, { @@ -386,7 +386,7 @@ "metadata": {}, "outputs": [], "source": [ - "@run_qiskit_remote()\n", + "@distribute_task()\n", "def sum_remote(a: int, b: int):\n", " return a + b\n", "\n", diff --git a/docs/tutorials/06_electronic_structure_problem.ipynb b/docs/tutorials/06_electronic_structure_problem.ipynb index ed881be97..8430e8033 100644 --- a/docs/tutorials/06_electronic_structure_problem.ipynb +++ b/docs/tutorials/06_electronic_structure_problem.ipynb @@ -31,10 +31,10 @@ "For full implementation [see here](./source_files/electronic_structure_problem.py).\n", "\n", "```python\n", - "from quantum_serverless import run_qiskit_remote, get\n", + "from quantum_serverless import distribute_task, get\n", "\n", "\n", - "@run_qiskit_remote()\n", + "@distribute_task()\n", "def ground_state_solve(\n", " molecule: Molecule,\n", " initial_point: Union[List[float], np.ndarray],\n", diff --git a/docs/tutorials/07_benchmark_program.ipynb b/docs/tutorials/07_benchmark_program.ipynb index 9f4ea8391..9ed16b541 100644 --- a/docs/tutorials/07_benchmark_program.ipynb +++ b/docs/tutorials/07_benchmark_program.ipynb @@ -1,7 +1,6 @@ { "cells": [ { - "attachments": {}, "cell_type": "markdown", "id": "604850c4-053a-4b78-bff2-f5f0c9250ed8", "metadata": {}, @@ -32,7 +31,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "id": "b031e9c4-8503-4535-8517-22f445593eb5", "metadata": {}, "outputs": [], @@ -59,7 +58,7 @@ ], "source": [ "gateway_provider = GatewayProvider(\n", - " username=\"john\",\n", + " username=\"user\",\n", " password=\"password123\",\n", " host=\"http://localhost:8000\",\n", ")\n", @@ -69,31 +68,24 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "id": "030ccde4-d18b-43fc-979e-68df797f5977", "metadata": {}, "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{\"n_qubits\": 2, \"n_entries\": 2, \"depth_of_recursion\": 4, \"n_backends\": 3, \"n_graphs\": 1} []\n" - ] - }, { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 4, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "program = Program(\n", - " name=\"benchmark_program\",\n", + " title=\"benchmark_program\",\n", " entrypoint=\"benchmark.py\",\n", " arguments={\n", " \"n_qubits\": 2,\n", @@ -112,31 +104,23 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 13, "id": "316d3bdd-c9ed-404e-bf76-afca850920f5", "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Execution time: 9.523066759109497\n", - "Results: [[EstimatorResult(values=array([ 3.30466138e-01+0.97886459j, 6.99025164e-17+1.j ,\n", - " 3.59667555e-01+0.07793376j, 9.59608666e-01-0.95960867j,\n", - " -9.20956756e-01-0.99668204j, 0.00000000e+00-1.j ,\n", - " 0.00000000e+00+0.138121j , 2.55847956e-01+0.j ]), metadata=[{}, {}, {}, {}, {}, {}, {}, {}]), EstimatorResult(values=array([ 3.30466138e-01+0.97886459j, 6.99025164e-17+1.j ,\n", - " 3.59667555e-01+0.07793376j, 9.59608666e-01-0.95960867j,\n", - " -9.20956756e-01-0.99668204j, 0.00000000e+00-1.j ,\n", - " 0.00000000e+00+0.138121j , 2.55847956e-01+0.j ]), metadata=[{}, {}, {}, {}, {}, {}, {}, {}]), EstimatorResult(values=array([ 3.30466138e-01+0.97886459j, 6.99025164e-17+1.j ,\n", - " 3.59667555e-01+0.07793376j, 9.59608666e-01-0.95960867j,\n", - " -9.20956756e-01-0.99668204j, 0.00000000e+00-1.j ,\n", - " 0.00000000e+00+0.138121j , 2.55847956e-01+0.j ]), metadata=[{}, {}, {}, {}, {}, {}, {}, {}])]]\n", - "\n" - ] + "data": { + "text/plain": [ + "'SUCCEEDED'" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print(job.logs())" + "job.status()" ] } ], diff --git a/docs/tutorials/source_files/benchmark.py b/docs/tutorials/source_files/benchmark.py index 3284ffa40..3c5d7b9fb 100644 --- a/docs/tutorials/source_files/benchmark.py +++ b/docs/tutorials/source_files/benchmark.py @@ -22,10 +22,10 @@ from qiskit.providers import Backend from qiskit.providers.fake_provider import ConfigurableFakeBackend from qiskit.quantum_info.random import random_pauli_list -from quantum_serverless import QuantumServerless, get, run_qiskit_remote, put +from quantum_serverless import QuantumServerless, get, distribute_task, put -@run_qiskit_remote() +@distribute_task() def generate_circuits( depth_of_recursion: int, n_qubits: int, depth_of_circuit: int, n_circuits: int ): @@ -41,7 +41,7 @@ def generate_circuits( ) -@run_qiskit_remote() +@distribute_task() def generate_observables( depth_of_recursion: int, n_qubits: int, size: int, n_observables: int ): @@ -55,7 +55,7 @@ def generate_observables( ) -@run_qiskit_remote() +@distribute_task() def generate_data( depth_of_recursion: int, n_qubits: int, @@ -87,7 +87,7 @@ def get_backends(n_backends: int, n_qubits: int): return [backend for _ in range(n_backends)] -@run_qiskit_remote() +@distribute_task() def transpile_remote( circuits: List[QuantumCircuit], backend: Backend ) -> List[QuantumCircuit]: @@ -95,13 +95,13 @@ def transpile_remote( return transpile(circuits, backend) -@run_qiskit_remote() +@distribute_task() def estimate(circuits: list, observables: list): """Estimates expectation values of given circuit.""" return Estimator().run(circuits, observables).result() -@run_qiskit_remote() +@distribute_task() def run_graph( depth_of_recursion: int, n_qubits: int, diff --git a/docs/tutorials/source_files/electronic_structure_problem.py b/docs/tutorials/source_files/electronic_structure_problem.py index d096e61a8..90471c8e0 100644 --- a/docs/tutorials/source_files/electronic_structure_problem.py +++ b/docs/tutorials/source_files/electronic_structure_problem.py @@ -23,11 +23,11 @@ from qiskit_nature.drivers import Molecule from qiskit.algorithms.optimizers import SPSA -from quantum_serverless import run_qiskit_remote, get, QuantumServerless +from quantum_serverless import distribute_task, get, QuantumServerless from quantum_serverless.core import RedisStateHandler -@run_qiskit_remote(target={"cpu": 2}) +@distribute_task(target={"cpu": 2}) def ground_state_solve( molecule: Molecule, initial_point: Union[List[float], np.ndarray],