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

Improve Docstrings in Task/braket_simulator #976

Closed
wants to merge 2 commits into from
Closed
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
84 changes: 74 additions & 10 deletions src/bloqade/task/braket_simulator.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
from bloqade.serialize import Serializer
from bloqade.builder.base import ParamType
from .base import LocalTask
from bloqade.submission.ir.task_results import QuEraTaskResults
"""
This module defines the BraketEmulatorTask class which integrates with the AWS Braket LocalSimulator
to run and manage quantum tasks, and serialize/deserialize task information.
"""

from dataclasses import dataclass
from braket.devices import LocalSimulator
from beartype.typing import Dict, Optional, Any
from bloqade.submission.ir.braket import (
from_braket_task_results,
BraketTaskSpecification,
)
from bloqade.task.base import Geometry
from braket.devices import LocalSimulator
from beartype.typing import Dict, Optional, Any
from dataclasses import dataclass
from bloqade.serialize import Serializer
from bloqade.builder.base import ParamType
from bloqade.submission.ir.task_results import QuEraTaskResults
from .base import LocalTask

## keep the old conversion for now,
## we will remove conversion btwn QuEraTask <-> BraketTask,
Expand All @@ -19,17 +24,48 @@
@dataclass
@Serializer.register
class BraketEmulatorTask(LocalTask):
"""
Represents a quantum task that runs on the AWS Braket LocalSimulator.

Attributes:
task_ir (BraketTaskSpecification): The task specification for the Braket simulator.
metadata (Dict[str, ParamType]): Metadata related to the task.
task_result_ir (Optional[QuEraTaskResults]): The results of the task, if available.
"""

task_ir: BraketTaskSpecification
metadata: Dict[str, ParamType]
task_result_ir: Optional[QuEraTaskResults] = None

def _geometry(self) -> Geometry:
"""
Constructs and returns the Geometry of the task based on the AHS register setup.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a private function, and I'm doing duplicated reviews on the same issue. Please work on one of your PRs first before requesting multiple reviews thank you.


Returns:
Geometry: The geometric setup of the task.
"""
return Geometry(
sites=self.task_ir.program.setup.ahs_register.sites,
filling=self.task_ir.program.setup.ahs_register.filling,
)

def run(self, **kwargs) -> "BraketEmulatorTask":
"""
Runs the task on the Braket LocalSimulator.

Args:
**kwargs: Additional arguments for the simulator run method.

Returns:
BraketEmulatorTask: The current instance with the task results updated.

Example:
>>> task_spec = BraketTaskSpecification(...) # Task specification setup
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as my previous comment, this needs to use a markdown code block

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also can you actually run these code and copy the important printings back, e.g the printing of task and result

Copy link
Member

@weinbe58 weinbe58 Jun 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shubhusion Just for clarity you all you need to do:

"""

Example:
    ```python
    >>> <example code>
    >>> ...
    ```

"""

>>> metadata = {"param1": value1, "param2": value2}
>>> task = BraketEmulatorTask(task_ir=task_spec, metadata=metadata)
>>> task.run(shots=1000)
>>> results = task.result()
"""
aws_task = LocalSimulator("braket_ahs").run(
self.task_ir.program,
shots=self.task_ir.nshots,
Expand All @@ -38,14 +74,42 @@ def run(self, **kwargs) -> "BraketEmulatorTask":
self.task_result_ir = from_braket_task_results(aws_task.result())
return self

def result(self):
def result(self) -> QuEraTaskResults:
"""
Retrieves the results of the task.

Returns:
QuEraTaskResults: The results of the task.

Raises:
ValueError: If the task has not been submitted yet.

Example:
>>> task_spec = BraketTaskSpecification(...) # Task specification setup
>>> metadata = {"param1": value1, "param2": value2}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to be more specific, e.g what does the specification take? and I don't think this is what user will be using. The result is called in the builder call chain.

>>> task = BraketEmulatorTask(task_ir=task_spec, metadata=metadata)
>>> task.run(shots=1000)
>>> results = task.result()
"""
if self.task_result_ir is None:
raise ValueError("Braket simulator job haven't submit yet.")
raise ValueError("Braket simulator job hasn't been submitted yet.")

return self.task_result_ir

@property
def nshots(self):
def nshots(self) -> int:
"""
Returns the number of shots for the task.

Returns:
int: The number of shots.

Example:
>>> task_spec = BraketTaskSpecification(...) # Task specification setup
>>> metadata = {"param1": value1, "param2": value2}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same, you can't ask a user to construct task specification directly

>>> task = BraketEmulatorTask(task_ir=task_spec, metadata=metadata)
>>> num_shots = task.nshots
"""
return self.task_ir.nshots


Expand Down
Loading