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

feat: add gate class docs to the subroutines in circuits #800

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 12 additions & 4 deletions src/braket/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,15 @@ class Circuit:
_ALL_QUBITS = "ALL" # Flag to indicate all qubits in _qubit_observable_mapping

@classmethod
def register_subroutine(cls, func: SubroutineCallable) -> None:
def register_subroutine(cls, func: SubroutineCallable, cls_docstring: str) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps this should be str | None and default to None? For backward compatibility reasons it seems like it needs a default value.

"""
Register the subroutine `func` as an attribute of the `Circuit` class. The attribute name
is the name of `func`.

Args:
func (SubroutineCallable): The function of the subroutine to add to the class.
cls_docstring (str): The docstring to the class. This will be appended to the
subroutine docstring.

Examples:
>>> def h_on_all(target):
Expand All @@ -115,7 +117,11 @@ def method_from_subroutine(self, *args, **kwargs) -> SubroutineReturn:
setattr(cls, function_name, method_from_subroutine)

function_attr = getattr(cls, function_name)
setattr(function_attr, "__doc__", func.__doc__)
# docstrings don't like a single newline for some reason.
function_docstring = (
"\n\n".join([cls_docstring, func.__doc__]) if len(cls_docstring) > 0 else func.__doc__
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"\n\n".join([cls_docstring, func.__doc__]) if len(cls_docstring) > 0 else func.__doc__
"\n\n".join([cls_docstring, func.__doc__]) if cls_docstring else func.__doc__

)
setattr(function_attr, "__doc__", function_docstring)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you whether setting __doc__ in this way allows the docstring to appear in IDEs such as VS Code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't have VSCode setup so I am unsure at the moment.


def __init__(self, addable: AddableTypes | None = None, *args, **kwargs):
"""
Expand Down Expand Up @@ -1522,13 +1528,15 @@ def __call__(self, arg: Any | None = None, **kwargs) -> Circuit:
return self.make_bound_circuit(param_values)


def subroutine(register: bool = False) -> Callable:
def subroutine(register: bool = False, cls_docstring: str = "") -> Callable:
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be str | None and default to None? Just to avoid a potential usage mistake, seems like None and "" should behave the same way here.

"""
Subroutine is a function that returns instructions, result types, or circuits.

Args:
register (bool): If `True`, adds this subroutine into the `Circuit` class.
Default = `False`.
cls_docstring (str): The docstring to the class. This will be appended to the
subroutine docstring.

Returns:
Callable: The subroutine function.
Expand All @@ -1548,7 +1556,7 @@ def subroutine(register: bool = False) -> Callable:

def _subroutine_function_wrapper(func: Callable[..., SubroutineReturn]) -> SubroutineReturn:
if register:
Circuit.register_subroutine(func)
Circuit.register_subroutine(func, cls_docstring)
return func

return _subroutine_function_wrapper
2 changes: 1 addition & 1 deletion src/braket/circuits/gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def fixed_qubit_count() -> int:
return 1

@staticmethod
@circuit.subroutine(register=True)
@circuit.subroutine(register=True, cls_docstring=__doc__)
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you mean to only add this to the h gate, or do you also want to add this to the rest of the gates?

def h(
target: QubitSetInput,
*,
Expand Down