-
Notifications
You must be signed in to change notification settings - Fork 118
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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: | ||||||
""" | ||||||
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): | ||||||
|
@@ -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__ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
) | ||||||
setattr(function_attr, "__doc__", function_docstring) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you whether setting There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||||||
""" | ||||||
|
@@ -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: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be |
||||||
""" | ||||||
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. | ||||||
|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,7 +90,7 @@ def fixed_qubit_count() -> int: | |
return 1 | ||
|
||
@staticmethod | ||
@circuit.subroutine(register=True) | ||
@circuit.subroutine(register=True, cls_docstring=__doc__) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you mean to only add this to the |
||
def h( | ||
target: QubitSetInput, | ||
*, | ||
|
There was a problem hiding this comment.
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 toNone
? For backward compatibility reasons it seems like it needs a default value.