Skip to content

Adding Generic submission option. #961

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

Merged
merged 6 commits into from
May 29, 2024
Merged
Changes from 3 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
102 changes: 100 additions & 2 deletions src/bloqade/ir/routine/quera.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from collections import OrderedDict
from collections import OrderedDict, namedtuple
from pydantic.v1.dataclasses import dataclass
import json

Expand All @@ -10,8 +10,9 @@
from bloqade.task.batch import RemoteBatch
from bloqade.task.quera import QuEraTask

from beartype.typing import Tuple, Union, Optional
from beartype.typing import Tuple, Union, Optional, NamedTuple, List, Dict, Any
from beartype import beartype
from requests import Response, request


@dataclass(frozen=True, config=__pydantic_dataclass_config__)
Expand Down Expand Up @@ -41,6 +42,103 @@
backend = MockBackend(state_file=state_file, submission_error=submission_error)
return QuEraHardwareRoutine(self.source, self.circuit, self.params, backend)

@property
def custom(self) -> "CustomSubmissionRoutine":
return CustomSubmissionRoutine(self.source, self.circuit, self.params)

Check warning on line 47 in src/bloqade/ir/routine/quera.py

View check run for this annotation

Codecov / codecov/patch

src/bloqade/ir/routine/quera.py#L47

Added line #L47 was not covered by tests


@dataclass(frozen=True, config=__pydantic_dataclass_config__)
class CustomSubmissionRoutine(RoutineBase):
def _compile(
self,
shots: int,
args: Tuple[LiteralType, ...] = (),
):
from bloqade.compiler.passes.hardware import (

Check warning on line 57 in src/bloqade/ir/routine/quera.py

View check run for this annotation

Codecov / codecov/patch

src/bloqade/ir/routine/quera.py#L57

Added line #L57 was not covered by tests
analyze_channels,
canonicalize_circuit,
assign_circuit,
validate_waveforms,
generate_ahs_code,
generate_quera_ir,
)
from bloqade.factory import get_capabilities

Check warning on line 65 in src/bloqade/ir/routine/quera.py

View check run for this annotation

Codecov / codecov/patch

src/bloqade/ir/routine/quera.py#L65

Added line #L65 was not covered by tests

circuit, params = self.circuit, self.params
capabilities = get_capabilities()

Check warning on line 68 in src/bloqade/ir/routine/quera.py

View check run for this annotation

Codecov / codecov/patch

src/bloqade/ir/routine/quera.py#L67-L68

Added lines #L67 - L68 were not covered by tests

for batch_params in params.batch_assignments(*args):
assignments = {**batch_params, **params.static_params}
final_circuit, metadata = assign_circuit(circuit, assignments)

Check warning on line 72 in src/bloqade/ir/routine/quera.py

View check run for this annotation

Codecov / codecov/patch

src/bloqade/ir/routine/quera.py#L70-L72

Added lines #L70 - L72 were not covered by tests

level_couplings = analyze_channels(final_circuit)
final_circuit = canonicalize_circuit(final_circuit, level_couplings)

Check warning on line 75 in src/bloqade/ir/routine/quera.py

View check run for this annotation

Codecov / codecov/patch

src/bloqade/ir/routine/quera.py#L74-L75

Added lines #L74 - L75 were not covered by tests

validate_waveforms(level_couplings, final_circuit)
ahs_components = generate_ahs_code(

Check warning on line 78 in src/bloqade/ir/routine/quera.py

View check run for this annotation

Codecov / codecov/patch

src/bloqade/ir/routine/quera.py#L77-L78

Added lines #L77 - L78 were not covered by tests
capabilities, level_couplings, final_circuit
)

task_ir = generate_quera_ir(ahs_components, shots).discretize(capabilities)

Check warning on line 82 in src/bloqade/ir/routine/quera.py

View check run for this annotation

Codecov / codecov/patch

src/bloqade/ir/routine/quera.py#L82

Added line #L82 was not covered by tests

MetaData = namedtuple("MetaData", metadata.keys())

Check warning on line 84 in src/bloqade/ir/routine/quera.py

View check run for this annotation

Codecov / codecov/patch

src/bloqade/ir/routine/quera.py#L84

Added line #L84 was not covered by tests

yield MetaData(**metadata), task_ir

Check warning on line 86 in src/bloqade/ir/routine/quera.py

View check run for this annotation

Codecov / codecov/patch

src/bloqade/ir/routine/quera.py#L86

Added line #L86 was not covered by tests

def submit(
self,
shots: int,
url: str,
json_body_template: str,
method: str = "POST",
args: Tuple[LiteralType] = (),
request_options: Dict[str, Any] = {},
) -> List[Tuple[NamedTuple, Response]]:
"""Compile to QuEraTaskSpecification and submit to a custom service.

Args:
shots (int): number of shots
url (str): url of the custom service
json_body_template (str): json body template, must contain '{task_ir}'
to be replaced by QuEraTaskSpecification
method (str): http method to be used. Defaults to "POST".
args (Tuple[LiteralType]): additional arguments to be passed into the
compiler coming from `args` option of the build. Defaults to ().
**request_options: additional options to be passed into the request method,
Note the `json` option will be overwritten by the `json_body_template`.

Returns:
List[Tuple[NamedTuple, Response]]: List of parameters for each batch in
the task and the response from the post request.

Examples:
Here is a simple example of how to use this method.

```python
>>> body_template = "{"token": "my_token", "task": {task_ir}}"
>>> responses = (
program.quera.custom.submit(
100,
"http://my_custom_service.com",
body_template
)
)
```
"""

if r"{task_ir}" not in json_body_template:
raise ValueError(r"body_template must contain '{task_ir}'")

Check warning on line 130 in src/bloqade/ir/routine/quera.py

View check run for this annotation

Codecov / codecov/patch

src/bloqade/ir/routine/quera.py#L129-L130

Added lines #L129 - L130 were not covered by tests

out = []
for metadata, task_ir in self._compile(shots, args):
request_options.update(

Check warning on line 134 in src/bloqade/ir/routine/quera.py

View check run for this annotation

Codecov / codecov/patch

src/bloqade/ir/routine/quera.py#L132-L134

Added lines #L132 - L134 were not covered by tests
json=json_body_template.format(task_ir=task_ir.json())
)
response = request(method, url, **request_options)
out.append((metadata, response))

Check warning on line 138 in src/bloqade/ir/routine/quera.py

View check run for this annotation

Codecov / codecov/patch

src/bloqade/ir/routine/quera.py#L137-L138

Added lines #L137 - L138 were not covered by tests

return out

Check warning on line 140 in src/bloqade/ir/routine/quera.py

View check run for this annotation

Codecov / codecov/patch

src/bloqade/ir/routine/quera.py#L140

Added line #L140 was not covered by tests


@dataclass(frozen=True, config=__pydantic_dataclass_config__)
class QuEraHardwareRoutine(RoutineBase):
Expand Down