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

Rename run to job and runtime to backend #102

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions examples/hqca_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from quantuminspire.sdk.models.circuit import Circuit
from quantuminspire.sdk.models.hybrid_algorithm import HybridAlgorithm
from quantuminspire.util.api.local_runtime import LocalRuntime
from quantuminspire.util.api.local_backend import LocalBackend
from quantuminspire.util.api.quantum_interface import QuantumInterface


Expand Down Expand Up @@ -64,8 +64,8 @@ def finalize(list_of_measurements: Dict[int, List[Any]]) -> Dict[str, Any]:
algorithm = HybridAlgorithm("test", "test")
algorithm.read_file(Path(__file__))

local_runtime = LocalRuntime()
run_id = local_runtime.run(algorithm, 0)
results = local_runtime.get_results(run_id)
local_backend = LocalBackend()
job_id = local_backend.run(algorithm, 0)
results = local_backend.get_results(job_id)

print("=== Execute ===\n", results)
2 changes: 1 addition & 1 deletion examples/qaoa.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def f(theta):
gamma = theta[P:]
circuit = qaoa_circuit(graph, beta, gamma)
# This slightly awkward. f needs to by a normal (non async) function since scipy can only minimize those.
# However, in the classical runtime there is already an asycio event loop so we can't just call
# However, in the classical backend there is already an asycio event loop so we can't just call
#
# result = asyncio.run(qi.execute_circuit(circuit, SHOTS))
#
Expand Down
6 changes: 3 additions & 3 deletions examples/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import time

from quantuminspire.sdk.models.circuit import Circuit
from quantuminspire.util.api.remote_runtime import RemoteRuntime
from quantuminspire.util.api.remote_backend import RemoteBackend

with Circuit(platform_name="spin-2", program_name="prgm1") as c:
k = c.init_kernel("new_kernel", 2)
Expand All @@ -14,9 +14,9 @@

print(c.content)

runtime = RemoteRuntime()
backend = RemoteBackend()

startTime = time.time()
runtime.run(c, runtime_type_id=3)
backend.run(c, backend_type_id=3)
executionTime = time.time() - startTime
print("Execution time in seconds: " + str(executionTime))
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ python = "^3.8"
typer = {extras = ["all"], version = "^0.9.0"}
qutechopenql = "^0.11.1"
pydantic = "^1.10.9"
qi-compute-api-client = {git = "https://github.com/QuTech-Delft/compute-api-client.git", branch = "0.2.0"}
qi-compute-api-client = {git = "https://github.com/QuTech-Delft/compute-api-client.git", branch="0.9.0"}
qxelarator = {version = "^0.6.2", optional = true}

[tool.poetry.extras]
Expand Down
26 changes: 13 additions & 13 deletions quantuminspire/cli/command_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from typer import Typer

from quantuminspire.sdk.models.hybrid_algorithm import HybridAlgorithm
from quantuminspire.util.api.local_runtime import LocalRuntime
from quantuminspire.util.api.remote_runtime import RemoteRuntime
from quantuminspire.util.api.local_backend import LocalBackend
from quantuminspire.util.api.remote_backend import RemoteBackend

app = Typer(add_completion=False, no_args_is_help=True)
algorithms_app = Typer(no_args_is_help=True)
Expand Down Expand Up @@ -233,21 +233,21 @@ def sync_projects(
@files_app.command("upload")
def upload_files(
name: str = typer.Argument(..., help="The name of the file to upload"),
runtime_type_id: int = typer.Argument(
..., help="The id of the runtime type on which this algorithm should be executed"
backend_type_id: int = typer.Argument(
..., help="The id of the backend type on which this algorithm should be executed"
),
) -> None:
"""Upload a file to the QI API.
Upload a Hybrid Quantum/Classical Algorithm to the Quantum Inspire API. This file is marked as a hybrid algorithm
when sent to the API.
"""
runtime = RemoteRuntime()
backend = RemoteBackend()
program = HybridAlgorithm(platform_name="spin-2", program_name=name)
program.read_file(Path(name))
run_id = runtime.run(program, runtime_type_id=runtime_type_id)
job_id = backend.run(program, backend_type_id=backend_type_id)
typer.echo(f"Upload file with name: {name}")
typer.echo(f"run_id {run_id}")
typer.echo(f"job_id {job_id}")


@files_app.command("run")
Expand All @@ -261,20 +261,20 @@ def run_file(
algorithm = HybridAlgorithm("test", "test")
algorithm.read_file(Path(name))

local_runtime = LocalRuntime()
run_id = local_runtime.run(algorithm, 0)
results = local_runtime.get_results(run_id)
local_backend = LocalBackend()
job_id = local_backend.run(algorithm, 0)
results = local_backend.get_results(job_id)
typer.echo(f"{results}")


@results_app.command("get")
def get_results(run_id: int = typer.Argument(..., help="The id of the run")) -> None:
def get_results(job_id: int = typer.Argument(..., help="The id of the run")) -> None:
"""Retrieve the results for a run.
Takes the id as returned by upload_files and retrieves the results for that run, if it's finished.
"""
runtime = RemoteRuntime()
results = runtime.get_results(run_id)
backend = RemoteBackend()
results = backend.get_results(job_id)

if results is None:
typer.echo("No results.")
Expand Down
8 changes: 4 additions & 4 deletions quantuminspire/sdk/quantum_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from quantuminspire.util.api.quantum_interface import QuantumInterface as QuantumInterfaceProtocol

if TYPE_CHECKING: # pragma: no cover
from quantuminspire.util.api.local_runtime import LocalRuntime
from quantuminspire.util.api.local_backend import LocalBackend


class QuantumInterface(QuantumInterfaceProtocol):
Expand All @@ -27,12 +27,12 @@ class QuantumInterface(QuantumInterfaceProtocol):
# pylint: disable = R0903
# R0903: Too few public methods (1/2) (too-few-public-methods)

def __init__(self, runtime: "LocalRuntime"):
self.runtime = runtime
def __init__(self, backend: "LocalBackend"):
self.backend = backend
self.results: List[Any] = []

def execute_circuit(self, circuit: str, number_of_shots: int) -> ExecuteCircuitResult:
"""Execute a quantum circuit."""
results = self.runtime.run_quantum(circuit, number_of_shots)
results = self.backend.run_quantum(circuit, number_of_shots)
self.results.append(results.results)
return results
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
from quantuminspire.sdk.models.base_algorithm import BaseAlgorithm


class BaseRuntime(ABC):
"""Base interface for runtimes.
class BaseBackend(ABC):
"""Base interface for backends.

A runtime will execute the hybrid algorithm, or quantum circuit provided.
A backend will execute the hybrid algorithm, or quantum circuit provided.
"""

@abstractmethod
def run(self, program: BaseAlgorithm, runtime_type_id: int) -> int:
def run(self, program: BaseAlgorithm, backend_type_id: int) -> int:
"""Execute provided algorithm/circuit."""
raise NotImplementedError

@abstractmethod
def get_results(self, run_id: int) -> Any:
def get_results(self, job_id: int) -> Any:
"""Get results for algorithm/circuit."""
raise NotImplementedError
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from quantuminspire.sdk.models.circuit import Circuit
from quantuminspire.sdk.models.hybrid_algorithm import HybridAlgorithm
from quantuminspire.sdk.quantum_interface import QuantumInterface
from quantuminspire.util.api.base_runtime import BaseRuntime
from quantuminspire.util.api.base_backend import BaseBackend
from quantuminspire.util.api.quantum_interface import ExecuteCircuitResult
from quantuminspire.util.api.quantum_interface import QuantumInterface as QuantumInterfaceProtocol

Expand All @@ -39,15 +39,15 @@ def import_qxelarator() -> types.ModuleType: # pragma: no cover
raise SystemExit from exc


class LocalRuntime(BaseRuntime):
"""Connection to remote runtime.
class LocalBackend(BaseBackend):
"""Connection to remote backend.
Create a connection with the remote runtime for Quantum Inspire. This connection creates the appropriate projects,
Create a connection with the remote backend for Quantum Inspire. This connection creates the appropriate projects,
algorithms, files etc. The algorithm/circuit will also be run.
"""

class RunFakeID(IntEnum):
"""Helper to conform to BaseRuntime interface of returning job IDs."""
class JobFakeID(IntEnum):
"""Helper to conform to BaseBackend interface of returning job IDs."""

QUANTUM = -1
HYBRID = -2
Expand All @@ -58,15 +58,15 @@ def __init__(self, qxelarator: Union[types.ModuleType, None] = None) -> None:
self._hybrid_results: Union[Any, None] = None
self._qxelarator = qxelarator if qxelarator else import_qxelarator()

def run(self, program: BaseAlgorithm, runtime_type_id: int) -> int:
def run(self, program: BaseAlgorithm, backend_type_id: int) -> int:
"""Execute provided algorithm/circuit."""
if isinstance(program, HybridAlgorithm):
quantum_interface = QuantumInterface(self)
self._hybrid_results = self.run_hybrid(program, quantum_interface)
return self.RunFakeID.HYBRID
return self.JobFakeID.HYBRID
if isinstance(program, Circuit):
self._quantum_results = self.run_quantum(program.content)
return self.RunFakeID.QUANTUM
return self.JobFakeID.QUANTUM
raise AssertionError("Unknown algorithm type")

def run_hybrid(self, algorithm: HybridAlgorithm, quantum_interface: QuantumInterfaceProtocol) -> Any:
Expand Down Expand Up @@ -100,11 +100,11 @@ def run_quantum(self, circuit: str, number_of_shots: int = 1) -> ExecuteCircuitR
shots_requested=result.shots_requested,
)

def get_results(self, run_id: int) -> Any:
def get_results(self, job_id: int) -> Any:
"""Get results for algorithm/circuit."""
if run_id == self.RunFakeID.QUANTUM:
if job_id == self.JobFakeID.QUANTUM:
return self._quantum_results
if run_id == self.RunFakeID.HYBRID:
if job_id == self.JobFakeID.HYBRID:
return self._hybrid_results

raise AssertionError("Unknown run id")
raise AssertionError("Unknown job id")
Loading