-
Notifications
You must be signed in to change notification settings - Fork 343
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
Type annotations for gates, quilatom, and quilbase modules #999
Conversation
3684e8b
to
25d86be
Compare
@@ -475,7 +484,7 @@ def RESET(qubit_index=None): | |||
""" | |||
|
|||
|
|||
def MEASURE(qubit, classical_reg): | |||
def MEASURE(qubit: QubitDesignator, classical_reg: Optional[MemoryReferenceDesignator]) -> Measurement: |
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.
The classical_reg
arg here can also be an int
, but that is deprecated. The current type hint causes mypy to warn:
pyquil/tests/test_qvm.py:16: error: Argument 2 to "MEASURE" has incompatible type "int"; expected "Union[MemoryReference, Tuple[str, int], List[Any], str, None]"
This warning only pops up in the three places in test_qvm.py
. We could update the type hint here to include the deprecated int
type and get rid of the warnings, but it seems saner to leave it as advertising only non-deprecated types.
This is ready for review. There is still a remaining TODO to update the docstrings, but I'd rather go through a round of review first to make sure the types look sane, then update the docstrings once at the end. |
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.
Small nits for your picking.
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.
Can't be the most exciting thing you've worked on! :D
It's a dirty job, but somebody gotta do it. |
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.
@appleby is there some way we can get around the init -> None thing? seems like Guido himself is not a fan python/mypy#604 (comment)
I agree with Guido here, but sadly mypy seems to require them. I think we can drop the On the other hand, I think mypy only sees this as an error when run in |
I have not read this PR line-by-line, but I approve of its spirit. |
Here is a list of the new type aliases added in this PR: pyquil/api/_quantum_computer.py
ExecutableDesignator = Union[BinaryExecutableResponse, PyQuilExecutableResponse]
pyquil/quilatom.py
QubitDesignator = Union[Qubit, QubitPlaceholder, int]
MemoryReferenceDesignator = Union['MemoryReference', Tuple[str, int], List[Any], str]
ParameterDesignator = Union['Expression', 'MemoryReference', np.int_, int, float, complex]
ExpressionValueDesignator = Union[int, float, complex]
ExpressionDesignator = Union['Expression', ExpressionValueDesignator]
ParamSubstitutionsMapDesignator = Mapping['Parameter', ExpressionValueDesignator] A brief explanation for some of the less obvious types:
edit: updated with new type aliases from latest commit |
4081bfd
to
1bf76e3
Compare
Last TODO of updating dosctrings is now done. Marking this ready for review. I thought I was going to have to add explicit types to the docstring of every function that I added type hints for, hence why I was procrastinating on that thankless task. Finally started doing it, got annoyed and thought: "surely there is a sphinx plugin to figure this out from the type hints in the source", googled it and realized we are already using sphinx-autodoc-typehints (yay). So I ended up removing a handful of explicit references to argument and return types in the docstrings in order to allow sphinx-autodoc-typehints to take care of it and prevent the docstrings from going stale. |
- Replace GateParameter with ParameterDesignator everywhere - Allow immediate floats as third arg in prepare_ternary_operands - Allow immediate values as source arg for STORE and ClassicalStore Fixes #815
Import quilatom.Expression in gates.py. Even though it isn't referenced directly, it is indirectly used by quilatom.ParameterDesignator, which causes sphinx to choke when generating the docs.
Remove any explicit types from :param: and :rtype: sphinx docstrings, and let sphinx-autodoc-typehints take care of it.
Sometimes code wants to iterate over instr.qubits, which has type List[Union[Qubit, QubitPlaceholder]]. Ensure that Qubit and QubitPlaceholder have matching interfaces to prevent mypy warnings. Previously, code like [q.index for q in instr.qubits] would cause mypy to complain because q might be a QubitPlaceholder, which did not have an index attribute.
Also, restrict types of classical instr constructors in quilbase to not allow MemoryReferenceDesignator. Desginators are only accepted in the high-level interfaces found in the gates module.
These asserts are required to help mypy deduce that the types are sufficiently constrained.
And use it as the return type for QuantumComputer.compile.
52f3b3e
to
4072509
Compare
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.
looks great!
Description
This PR adds type annotations for the
pyquil.gates
,pyquil.quilatom
, andpyquil.quilbase
modules.This is still a WIP, but is ready for review. Since I'm new-ish to pyQuil, I probably got the types wrong in several places. See theTODO
s in the diff for places I'm particularly unsure about. Any hints from reviewers on these are greatly appreciated.So far, these pass muster when running
mypy
like so:Resolves #988
TODO:
TODO:(appleby)
s in the source--strict
flag and fix any errors reportedChecklist
auto-close keywords.
including author and PR number (@username, gh-xxx).