Skip to content

Fix CI for mypy and add explicit shape for COO #919

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

Merged
merged 2 commits into from
May 14, 2025

Conversation

woodsp-ibm
Copy link
Member

Summary

To fix CI. I tested the changes on a couple of machines 1 with 3.9 the other 3.12 and it seems to pass checks locally.

Fixes #917

Details and comments

  1. There was a unit test failure around shape. See Nightly tests failing around sparse COO #917 so I added shape parameter explicitly to COO. This had been noted in a deprecation warning that shape would become required and did so in the lastest sparse which is where things broke. It still works with the earlier sparse versions where its optional.

  2. There are several mypy failures as below. These do not show up in 3.9 and my first attempt was to add a type: ignore comment for the incompatible type ones which passed under 3.12 but under 3.9 gave error: Unused "type: ignore" comment [unused-ignore] as it does not fail under the older mypy which is still in use by 3.9. Given the code works, though I guess the newer mypy notes the type differences from what it sees to start with being assigned to what is done to the same variable later I simply added an Any type to the variable which passes mypy now under 3.9 and 3.12 since it allows all (any) types.

For the pegagos_svc failure I cast the return of the algorithm_globals.random.integers call to an int type so as to avoid the later use of it in the get being unable to match appropriately.

qiskit_machine_learning/optimizers/aqgd.py:170: error: Incompatible types in assignment (expression has type "ndarray[tuple[int, ...], dtype[Any]] | ndarray[tuple[int, ...], dtype[float64]]", variable has type "list[ndarray[tuple[int, ...], dtype[Any]] | ndarray[tuple[int, ...], dtype[float64]]]")  [assignment]
qiskit_machine_learning/optimizers/adam_amsgrad.py:187: error: Incompatible types in assignment (expression has type "ndarray[tuple[int, ...], dtype[Any]]", variable has type "ndarray[tuple[int], dtype[float64]]")  [assignment]
qiskit_machine_learning/optimizers/adam_amsgrad.py:189: error: Incompatible types in assignment (expression has type "ndarray[tuple[int, ...], dtype[Any]]", variable has type "ndarray[tuple[int], dtype[float64]]")  [assignment]
qiskit_machine_learning/optimizers/adam_amsgrad.py:190: error: Incompatible types in assignment (expression has type "ndarray[tuple[int, ...], dtype[Any]]", variable has type "ndarray[tuple[int], dtype[float64]]")  [assignment]
qiskit_machine_learning/optimizers/adam_amsgrad.py:216: error: Incompatible types in assignment (expression has type "ndarray[tuple[int, ...], dtype[float64]]", variable has type "ndarray[tuple[int], dtype[float64]]")  [assignment]
qiskit_machine_learning/optimizers/adam_amsgrad.py:217: error: Incompatible types in assignment (expression has type "ndarray[tuple[int, ...], dtype[float64]]", variable has type "ndarray[tuple[int], dtype[float64]]")  [assignment]
qiskit_machine_learning/optimizers/adam_amsgrad.py:219: error: Incompatible types in assignment (expression has type "ndarray[tuple[int, ...], dtype[float64]]", variable has type "ndarray[tuple[int], dtype[float64]]")  [assignment]
qiskit_machine_learning/optimizers/adam_amsgrad.py:234: error: Incompatible types in assignment (expression has type "ndarray[tuple[int, ...], dtype[Any]]", variable has type "ndarray[tuple[int], dtype[float64]]")  [assignment]
qiskit_machine_learning/neural_networks/effective_dimension.py:177: error: Incompatible types in assignment (expression has type "ndarray[tuple[int, ...], dtype[float64]]", variable has type "ndarray[tuple[int, int, int], dtype[float64]]")  [assignment]
qiskit_machine_learning/neural_networks/effective_dimension.py:178: error: Incompatible types in assignment (expression has type "ndarray[tuple[int, ...], dtype[float64]]", variable has type "ndarray[tuple[int, int], dtype[float64]]")  [assignment]
qiskit_machine_learning/algorithms/classifiers/pegasos_qsvc.py:198: error: Argument 1 to "_compute_weighted_kernel_sum" of "PegasosQSVC" has incompatible type "signedinteger[_64Bit]"; expected "int"  [arg-type]
qiskit_machine_learning/algorithms/classifiers/pegasos_qsvc.py:202: error: Invalid index type "signedinteger[_64Bit]" for "dict[int, int]"; expected type "int"  [index]
qiskit_machine_learning/algorithms/classifiers/pegasos_qsvc.py:202: error: No overload variant of "get" of "dict" matches argument types "signedinteger[_64Bit]", "int"  [call-overload]
qiskit_machine_learning/algorithms/classifiers/pegasos_qsvc.py:202: note: Possible overload variants:
qiskit_machine_learning/algorithms/classifiers/pegasos_qsvc.py:202: note:     def get(self, int, /) -> int
qiskit_machine_learning/algorithms/classifiers/pegasos_qsvc.py:202: note:     def get(self, int, int, /) -> int
qiskit_machine_learning/algorithms/classifiers/pegasos_qsvc.py:202: note:     def [_T] get(self, int, _T, /) -> int | _T

@woodsp-ibm
Copy link
Member Author

woodsp-ibm commented May 13, 2025

Seems this just failed per #903 (ie randomly). I'll note it here but its outside of any changes done by this PR. I'll re-run once all tests are done and it should (hopefully) pass ok then.

Update: I re-ran that failed check and the test now passed as expected.

test.gradients.test_estimator_gradient.TestEstimatorGradientV2.test_gradient_parameter_coefficient_2_LinCombEstimatorGradient
-----------------------------------------------------------------------------------------------------------------------------

Captured traceback:
~~~~~~~~~~~~~~~~~~~
    Traceback (most recent call last):

      File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/ddt.py", line 221, in wrapper
    return func(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^

      File "/Users/runner/work/qiskit-machine-learning/qiskit-machine-learning/test/gradients/test_estimator_gradient.py", line 614, in test_gradient_parameter_coefficient
    np.testing.assert_allclose(gradients, correct_results[i], atol=1e-1, rtol=1e-1)

      File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/numpy/testing/_private/utils.py", line 1715, in assert_allclose
    assert_array_compare(compare, actual, desired, err_msg=str(err_msg),

      File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/numpy/testing/_private/utils.py", line 921, in assert_array_compare
    raise AssertionError(msg)

    AssertionError: 
Not equal to tolerance rtol=0.1, atol=0.1

Mismatched elements: 1 / 4 (25%)
Max absolute difference among violations: 0.18941639
Max relative difference among violations: 0.26066525
 ACTUAL: array([-0.537249, -0.424078, -0.003906, -0.919434])
 DESIRED: array([-0.726665, -0.490513, -0.006861, -0.922888])

@coveralls
Copy link

Pull Request Test Coverage Report for Build 15005482575

Details

  • 10 of 10 (100.0%) changed or added relevant lines in 5 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 90.857%

Totals Coverage Status
Change from base Build 13973547049: 0.0%
Covered Lines: 4482
Relevant Lines: 4933

💛 - Coveralls

@edoaltamura edoaltamura merged commit 4b918b0 into qiskit-community:main May 14, 2025
26 of 27 checks passed
@woodsp-ibm woodsp-ibm deleted the fix_ci branch May 15, 2025 20:16
@edoaltamura edoaltamura added the stable backport potential The bug might be minimal and/or import enough to be port to stable label May 22, 2025
mergify bot pushed a commit that referenced this pull request May 22, 2025
* Fix CI for mypy and add explicit shape for COO

* Fix style

(cherry picked from commit 4b918b0)
edoaltamura pushed a commit that referenced this pull request May 22, 2025
* Fix CI for mypy and add explicit shape for COO

* Fix style

(cherry picked from commit 4b918b0)

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Nightly tests failing around sparse COO
3 participants