Skip to content

Commit

Permalink
Client + docs: rename Program (#1057)
Browse files Browse the repository at this point in the history
  • Loading branch information
IceKhan13 authored Oct 27, 2023
1 parent 4011d6a commit e60d0ee
Show file tree
Hide file tree
Showing 27 changed files with 406 additions and 349 deletions.
14 changes: 7 additions & 7 deletions client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Full docs can be found at https://qiskit-extensions.github.io/quantum-serverless

## Usage

### Step 1: write program
### Step 1: write pattern

```python
from quantum_serverless import distribute_task, get, get_arguments, save_result
Expand Down Expand Up @@ -67,22 +67,22 @@ Full docs can be found at https://qiskit-extensions.github.io/quantum-serverless
```


### Step 2: run program
### Step 2: run pattern

```python
from quantum_serverless import QuantumServerless, GatewayProvider
from quantum_serverless import ServerlessProvider, QiskitPattern
from qiskit.circuit.random import random_circuit

serverless = QuantumServerless(GatewayProvider(
serverless = ServerlessProvider(
username="<USERNAME>",
password="<PASSWORD>",
host="<GATEWAY_ADDRESS>",
))
)

# create program
program = Program(
program = QiskitPattern(
title="Quickstart",
entrypoint="program.py",
entrypoint="pattern.py",
working_dir="./src"
)

Expand Down
4 changes: 2 additions & 2 deletions client/quantum_serverless/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from .core import (
BaseProvider,
distribute_task,
distribute_program,
distribute_qiskit_pattern,
get,
put,
get_refs_by_status,
Expand All @@ -41,7 +41,7 @@
get_auto_discovered_provider,
QuantumServerlessException,
)
from .core.program import Program
from .core.pattern import QiskitPattern
from .serializers import get_arguments

try:
Expand Down
6 changes: 5 additions & 1 deletion client/quantum_serverless/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
BaseJobClient
RayJobClient
save_result
QiskitPattern
Program
ProgramStorage
ProgramRepository
Expand All @@ -46,6 +47,7 @@
fetch_execution_meta
distribute_task
distribute_program
distribute_qiskit_pattern
get
put
get_refs_by_status
Expand All @@ -61,7 +63,8 @@
RayProvider,
)
from .job import BaseJobClient, RayJobClient, GatewayJobClient, Job, save_result
from .program import (
from .pattern import (
QiskitPattern,
Program,
ProgramStorage,
ProgramRepository,
Expand All @@ -74,6 +77,7 @@
get_refs_by_status,
fetch_execution_meta,
distribute_task,
distribute_qiskit_pattern,
distribute_program,
Target,
CircuitMeta,
Expand Down
27 changes: 21 additions & 6 deletions client/quantum_serverless/core/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import inspect
import os
import shutil
import warnings
from dataclasses import dataclass
from typing import Optional, Dict, Any, Union, List, Callable, Sequence
from uuid import uuid4
Expand Down Expand Up @@ -332,15 +333,15 @@ def wrapper(*args, **kwargs):
"""


def distribute_program(
def distribute_qiskit_pattern(
provider: Optional[Any] = None,
dependencies: Optional[List[str]] = None,
working_dir: Optional[str] = None,
):
"""[Experimental] Program decorator to turn function into remotely executable program.
"""[Experimental] QiskitPattern decorator to turn function into remotely executable program.
Example:
>>> @distribute_program(provider=ServerlessProvider(...), dependencies=[...])
>>> @distribute_qiskit_pattern(provider=ServerlessProvider(...), dependencies=[...])
>>> def my_program():
>>> print("Hola!")
>>>
Expand All @@ -356,7 +357,7 @@ def distribute_program(
"""
# pylint: disable=import-outside-toplevel,cyclic-import
from quantum_serverless import QuantumServerlessException
from quantum_serverless.core.program import Program
from quantum_serverless.core.pattern import QiskitPattern
from quantum_serverless.core.provider import ServerlessProvider

# create provider
Expand Down Expand Up @@ -411,12 +412,12 @@ def wrapper(*args, **kwargs):
file.write(ENTRYPOINT_CONTENT.format(file_name=pickle_file_name))

# create program
wrapped_program = Program(
wrapped_program = QiskitPattern(
title=function.__name__,
entrypoint=entrypoint_file_name,
working_dir=working_directory,
dependencies=dependencies,
description="Program execution using @distribute_program decorator.",
description="QiskitPattern execution using @distribute_program decorator.",
)

# run program
Expand All @@ -434,3 +435,17 @@ def wrapper(*args, **kwargs):
return wrapper

return decorator


def distribute_program(
provider: Optional[Any] = None,
dependencies: Optional[List[str]] = None,
working_dir: Optional[str] = None,
):
"""Decorator for distributed program."""
warnings.warn(
"`distribute_program` has been deprecated "
"and will be removed in future releases. "
"Please, use `distribute_qiskit_pattern` instead."
)
return distribute_qiskit_pattern(provider, dependencies, working_dir)
30 changes: 18 additions & 12 deletions client/quantum_serverless/core/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
MAX_ARTIFACT_FILE_SIZE_MB,
ENV_JOB_ARGUMENTS,
)
from quantum_serverless.core.program import Program
from quantum_serverless.core.pattern import QiskitPattern
from quantum_serverless.exception import QuantumServerlessException
from quantum_serverless.serializers.program_serializers import (
QiskitObjectsEncoder,
Expand All @@ -68,17 +68,19 @@ class BaseJobClient:
"""Base class for Job clients."""

def run(
self, program: Program, arguments: Optional[Dict[str, Any]] = None
self, program: QiskitPattern, arguments: Optional[Dict[str, Any]] = None
) -> "Job":
"""Runs program."""
raise NotImplementedError

def upload(self, program: Program):
def upload(self, program: QiskitPattern):
"""Uploads program."""
raise NotImplementedError

def run_existing(
self, program: Union[str, Program], arguments: Optional[Dict[str, Any]] = None
self,
program: Union[str, QiskitPattern],
arguments: Optional[Dict[str, Any]] = None,
):
"""Executes existing program."""
raise NotImplementedError
Expand Down Expand Up @@ -144,7 +146,7 @@ def list(self, **kwargs) -> List["Job"]:
Job(job.job_id, job_client=self) for job in self._job_client.list_jobs()
]

def run(self, program: Program, arguments: Optional[Dict[str, Any]] = None):
def run(self, program: QiskitPattern, arguments: Optional[Dict[str, Any]] = None):
arguments = arguments or {}
entrypoint = f"python {program.entrypoint}"

Expand All @@ -166,11 +168,13 @@ def run(self, program: Program, arguments: Optional[Dict[str, Any]] = None):
)
return Job(job_id=job_id, job_client=self)

def upload(self, program: Program):
def upload(self, program: QiskitPattern):
raise NotImplementedError("Upload is not available for RayJobClient.")

def run_existing(
self, program: Union[str, Program], arguments: Optional[Dict[str, Any]] = None
self,
program: Union[str, QiskitPattern],
arguments: Optional[Dict[str, Any]] = None,
):
raise NotImplementedError("Run existing is not available for RayJobClient.")

Expand All @@ -191,7 +195,7 @@ def __init__(self, host: str, token: str, version: str):
self._token = token

def run( # pylint: disable=too-many-locals
self, program: Program, arguments: Optional[Dict[str, Any]] = None
self, program: QiskitPattern, arguments: Optional[Dict[str, Any]] = None
) -> "Job":
tracer = trace.get_tracer("client.tracer")
with tracer.start_as_current_span("job.run") as span:
Expand Down Expand Up @@ -249,7 +253,7 @@ def run( # pylint: disable=too-many-locals

return Job(job_id, job_client=self)

def upload(self, program: Program):
def upload(self, program: QiskitPattern):
tracer = trace.get_tracer("client.tracer")
with tracer.start_as_current_span("job.run") as span:
span.set_attribute("program", program.title)
Expand Down Expand Up @@ -304,9 +308,11 @@ def upload(self, program: Program):
return program_title

def run_existing(
self, program: Union[str, Program], arguments: Optional[Dict[str, Any]] = None
self,
program: Union[str, QiskitPattern],
arguments: Optional[Dict[str, Any]] = None,
):
if isinstance(program, Program):
if isinstance(program, QiskitPattern):
title = program.title
else:
title = str(program)
Expand Down Expand Up @@ -441,7 +447,7 @@ def get_programs(self, **kwargs):
)
)
return [
Program(program.get("title"), raw_data=program)
QiskitPattern(program.get("title"), raw_data=program)
for program in response_data.get("results", [])
]

Expand Down
Loading

0 comments on commit e60d0ee

Please sign in to comment.