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

Type checking for channel_metrics #623

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft

Conversation

purva-thakre
Copy link
Collaborator

@purva-thakre purva-thakre commented Jun 6, 2024

Description

Related to #299

We use --explicit-package-bases here to disable mypy errors due to 2 files of the same name existing in different modules.

Changes

Right now ~/toqito$ mypy toqito/channel_metrics --explicit-package-bases is failing due to the following. These are mostly due to incompatibilities with type annotations in cvxpy compared to how we use them.

toqito/channel_metrics/diamond_norm.py:102: error: Argument 1 to "Parameter" has incompatible type "list[int]"; expected "int | tuple[int, ...]"  [arg-type]
toqito/channel_metrics/completely_bounded_trace_norm.py:67: error: Argument "dims" to "partial_trace" has incompatible type "tuple[int, int]"; expected "tuple[int]"  [arg-type]
toqito/channel_metrics/completely_bounded_spectral_norm.py:35: error: Module not callable  [operator]

Will need to refactor portions of:

  • diamon_norm.py:

j_var = cvxpy.Parameter([dim_squared, dim_squared], complex=True)

Here, [dim_squared, dim_Squared] is of type List[int] whereas cxpy.Parameter expects shape: int | tuple[int, ...]
https://www.cvxpy.org/api_reference/cvxpy.expressions.html#parameter

  • completely_bounded_trace_norm.py

Same as the previous item, we use an incompatible type compared to what's expected by cvpy type annotation. It expects tuple[int] but we use tuple[int. int].

cp.norm(cp.partial_trace(y0, dims=(dim, dim), axis=1)) + cp.norm(cp.partial_trace(y1, dims=(dim, dim), axis=1))

https://www.cvxpy.org/api_reference/cvxpy.atoms.affine.html#partial-trace

  • completely_bounded_spectral_norm.py

For some reason, completely_bounded_trace_norm or dual_channel shows up as a module not callable.

return completely_bounded_trace_norm(dual_channel(phi))

If I specify the full path for completely_bounded_trace_norm as completely_bounded_trace_norm.completely_bounded_trace_norm, mypy fails with the following error:

toqito/channel_metrics/completely_bounded_spectral_norm.py:35: error: Argument 1 to "completely_bounded_trace_norm" has incompatible type "ndarray[Any, Any] | list[list[ndarray[Any, Any]]]"; expected "ndarray[Any, Any]"  [arg-type]

Checklist

Before marking your PR ready for review, make sure you checked the following locally. If this is your first PR, you might be notified of some workflow failures after a maintainer has approved the workflow jobs to be run on your PR.

Additional information is available in the documentation.

  • Use ruff for errors related to code style and formatting.
  • Verify all previous and newly added unit tests pass in pytest.
  • Check the documentation build does not lead to any failures. Sphinx build can be checked locally for any failures related to your PR
  • Use linkcheck to check for broken links in the documentation
  • Use doctest to verify the examples in the function docstrings work as expected.

Copy link

codecov bot commented Jun 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.1%. Comparing base (1122a6e) to head (a7381f9).

Additional details and impacted files
@@          Coverage Diff           @@
##           master    #623   +/-   ##
======================================
  Coverage    98.1%   98.1%           
======================================
  Files         162     162           
  Lines        3112    3115    +3     
  Branches      759     759           
======================================
+ Hits         3054    3057    +3     
  Misses         37      37           
  Partials       21      21           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


[tool.mypy]
disable_error_code = ["import-untyped"]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added this because not all libraries use type annotations.

Skipping analyzing "picos": module is installed, but missing library stubs or py.typed marker

@@ -178,7 +179,7 @@ def fidelity_of_separability(
problem.add_constraint((picos.I(dim_r) @ sym_choi) * choi * (picos.I(dim_r) @ sym_choi) == choi)

# PPT condition on Choi state
sys = []
sys = [] # type: List[int]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is an inline type annotation as requested by mypy

Need type annotation for "sys" (hint: "sys: list[<type>] = ...") [var-annotated]

@purva-thakre
Copy link
Collaborator Author

I think the type annotation for dims argument in partial_trace is wrong.

It is dims: Tuple[int] when it should be dims: Tuple[int, ...]. We see the latter being used by cvxpy.partial_transpose.

https://www.cvxpy.org/api_reference/cvxpy.atoms.affine.html#partial-transpose

To Do: Understand if I need to raise this in the cvxpy repo.

Comment on lines 35 to 36
dual_as_array = np.asarray(dual_channel(phi))
return completely_bounded_trace_norm.completely_bounded_trace_norm(dual_as_array)
Copy link
Collaborator Author

@purva-thakre purva-thakre Jun 6, 2024

Choose a reason for hiding this comment

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

Don't know if this is a best practice hack.

Due to dual_channel, the return type of dual_channel(phi) is np.ndarray | List[List[np.ndarray]] when completely_bounded_trace_norm expects just a np.ndarray. So, added an additional step of converting dual_channel(phi).

If completely_bounded_trace_norm.completely_bounded_trace_norm was not specified, mypy failed with toqito/channel_metrics/completely_bounded_spectral_norm.py:35: error: Module not callable [operator] but python could not import this function to run unit tests.

@@ -64,7 +64,7 @@ def completely_bounded_trace_norm(phi: np.ndarray) -> float:
a_var = cp.bmat([[y0, -phi], [-phi.conj().T, y1]])
constraints += [a_var >> 0]
objective = cp.Minimize(
cp.norm(cp.partial_trace(y0, dims=(dim, dim), axis=1)) + cp.norm(cp.partial_trace(y1, dims=(dim, dim), axis=1))
cp.norm(cp.partial_trace(y0, dims=(dim, dim), axis=1)) + cp.norm(cp.partial_trace(y1, dims=(dim, dim), axis=1)) # type: ignore
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

temporarily ignoring this line due to #623 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant