Skip to content

Commit

Permalink
Issue 411 | Client: rename run_qiskit_remote to distribute_task (#…
Browse files Browse the repository at this point in the history
…442)

* Issue 411 | Client: rename run_qiskit_remote to distribute_task

* Issue 411 | Docs: rename run_qiskit_remote to distribute_task
  • Loading branch information
IceKhan13 authored Apr 19, 2023
1 parent 0987a44 commit 670ee36
Show file tree
Hide file tree
Showing 26 changed files with 171 additions and 169 deletions.
1 change: 1 addition & 0 deletions client/quantum_serverless/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .core import (
Provider,
run_qiskit_remote,
distribute_task,
get,
put,
get_refs_by_status,
Expand Down
10 changes: 9 additions & 1 deletion client/quantum_serverless/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
Provider
ComputeResource
distribute_task
run_qiskit_remote
get
put
Expand Down Expand Up @@ -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
28 changes: 26 additions & 2 deletions client/quantum_serverless/core/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -207,14 +209,37 @@ 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.
Example:
>>> import quantum_serverless as qs
>>>
>>> @run_qiskit_remote()
>>> @distribute_task()
>>> def awesome_function(seed: int):
>>> return 42
>>>
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions client/quantum_serverless/library/transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]:
Expand Down
31 changes: 30 additions & 1 deletion client/tests/core/test_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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)
Expand All @@ -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})
Expand Down
2 changes: 1 addition & 1 deletion client/tests/resources/test-compose.yml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
34 changes: 16 additions & 18 deletions docs/getting_started/01_intro_level_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 15,
"id": "81dd7807-7180-4b87-bbf9-832b7cf29d69",
"metadata": {},
"outputs": [],
Expand All @@ -49,7 +49,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 16,
"id": "acdec789-4967-48ee-8f6c-8d2b0ff57e91",
"metadata": {},
"outputs": [
Expand All @@ -59,7 +59,7 @@
"<QuantumServerless | providers [gateway-provider]>"
]
},
"execution_count": 2,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -76,7 +76,6 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "4dd85621-9ab0-4f34-9ab4-07ad773c5e00",
"metadata": {},
Expand All @@ -91,17 +90,17 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 17,
"id": "d51df836-3f22-467c-b637-5803145d5d8a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Job | e4a801d2-f9eb-4392-b584-cbdd600755c8>"
"<Job | aae3533f-ed0d-4e93-9247-4705d476ee97>"
]
},
"execution_count": 3,
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -120,7 +119,6 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "39ee31d2-3553-4e19-bcb9-4cccd0df0e4c",
"metadata": {},
Expand All @@ -130,7 +128,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 19,
"id": "cc7ccea6-bbae-4184-ba7f-67b6c20a0b0b",
"metadata": {},
"outputs": [
Expand All @@ -140,7 +138,7 @@
"'SUCCEEDED'"
]
},
"execution_count": 4,
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -151,7 +149,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 14,
"id": "ca76abfa-2ff5-425b-a225-058d91348e8b",
"metadata": {},
"outputs": [
Expand All @@ -161,7 +159,7 @@
"'Quasi distribution: {0: 0.4999999999999999, 3: 0.4999999999999999}\\n'"
]
},
"execution_count": 5,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -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"
}
Expand All @@ -210,17 +208,17 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 21,
"id": "45e2927f-655b-47a4-8003-f16e5ba0a1cd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Job | e4a801d2-f9eb-4392-b584-cbdd600755c8>"
"<Job | aae3533f-ed0d-4e93-9247-4705d476ee97>"
]
},
"execution_count": 7,
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
Expand Down
15 changes: 7 additions & 8 deletions docs/getting_started/02_intro_level_2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"id": "b6ec8969-8c3d-4b7f-8c4c-adc6dbb9c59f",
"metadata": {},
"outputs": [
Expand All @@ -110,7 +110,7 @@
"<QuantumServerless | providers [gateway-provider]>"
]
},
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -127,7 +127,6 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "544f7c64-ae1e-4480-b5d0-93f0c335eccd",
"metadata": {},
Expand All @@ -139,17 +138,17 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"id": "3ee09b31-4c7f-4ff3-af8f-294e4256793e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Job | 3b7512fa-3d68-4572-86b1-3f36eb6517d4>"
"<Job | 6c40a1eb-a9b6-4d05-8175-29e9b3014bb6>"
]
},
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -173,7 +172,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 9,
"id": "420f2711-b8c6-4bf9-8651-c9d098348467",
"metadata": {},
"outputs": [
Expand All @@ -183,7 +182,7 @@
"'SUCCEEDED'"
]
},
"execution_count": 6,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
Expand Down
Loading

0 comments on commit 670ee36

Please sign in to comment.