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
Merged
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of a Qiskit project.
#
# (C) Copyright IBM 2022, 2024.
# (C) Copyright IBM 2022, 2025.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand Down Expand Up @@ -193,7 +193,7 @@ def fit(
# training loop
for step in range(1, self._num_steps + 1):
# for every step, a random index (determining a random datum) is fixed
i = algorithm_globals.random.integers(0, len(y))
i = int(algorithm_globals.random.integers(0, len(y)))

value = self._compute_weighted_kernel_sum(i, X, training=True)

Expand Down
10 changes: 7 additions & 3 deletions qiskit_machine_learning/connectors/torch_connector.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of a Qiskit project.
#
# (C) Copyright IBM 2021, 2024.
# (C) Copyright IBM 2021, 2025.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand Down Expand Up @@ -237,7 +237,9 @@ def backward(ctx: Any, grad_output: Tensor) -> tuple: # type: ignore[override]
from sparse import COO

grad_output = grad_output.detach().cpu()
grad_coo = COO(grad_output.indices(), grad_output.values())
grad_coo = COO(
grad_output.indices(), grad_output.values(), shape=grad_output.shape
)

# Takes gradients from previous layer in backward pass (i.e. later layer in
# forward pass) j for each observation i in the batch. Multiplies this with
Expand Down Expand Up @@ -280,7 +282,9 @@ def backward(ctx: Any, grad_output: Tensor) -> tuple: # type: ignore[override]
from sparse import COO

grad_output = grad_output.detach().cpu()
grad_coo = COO(grad_output.indices(), grad_output.values())
grad_coo = COO(
grad_output.indices(), grad_output.values(), shape=grad_output.shape
)

# Takes gradients from previous layer in backward pass (i.e. later layer in
# forward pass) j for each observation i in the batch. Multiplies this with
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of a Qiskit project.
#
# (C) Copyright IBM 2022, 2024.
# (C) Copyright IBM 2022, 2025.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand All @@ -13,7 +13,7 @@

import logging
import time
from typing import Union, List, Tuple
from typing import Any, Union, List, Tuple

import numpy as np
from scipy.special import logsumexp
Expand Down Expand Up @@ -136,14 +136,14 @@ def run_monte_carlo(self) -> Tuple[np.ndarray, np.ndarray]:
outputs: QNN output vector, result of forward passes, of shape
``(num_input_samples * num_weight_samples, output_size)``.
"""
grads = np.zeros(
grads: Any = np.zeros(
(
self._num_input_samples * self._num_weight_samples,
self._model.output_shape[0],
self._model.num_weights,
)
)
outputs = np.zeros(
outputs: Any = np.zeros(
(self._num_input_samples * self._num_weight_samples, self._model.output_shape[0])
)

Expand Down
8 changes: 4 additions & 4 deletions qiskit_machine_learning/optimizers/adam_amsgrad.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of a Qiskit project.
#
# (C) Copyright IBM 2019, 2024.
# (C) Copyright IBM 2019, 2025.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand Down Expand Up @@ -106,10 +106,10 @@ def __init__(

# runtime variables
self._t = 0 # time steps
self._m = np.zeros(1)
self._v = np.zeros(1)
self._m: Any = np.zeros(1)
self._v: Any = np.zeros(1)
if self._amsgrad:
self._v_eff = np.zeros(1)
self._v_eff: Any = np.zeros(1)

if self._snapshot_dir is not None:
file_path = os.path.join(self._snapshot_dir, "adam_params.csv")
Expand Down
4 changes: 2 additions & 2 deletions qiskit_machine_learning/optimizers/aqgd.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of a Qiskit project.
#
# (C) Copyright IBM 2019, 2024.
# (C) Copyright IBM 2019, 2025.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand Down Expand Up @@ -161,7 +161,7 @@ def _compute_objective_fn_and_gradient(
# Evaluate,
# reshaping to flatten, as expected by objective function
if self._max_evals_grouped > 1:
batches = [
batches: Any = [
param_sets_to_eval[i : i + self._max_evals_grouped]
for i in range(0, len(param_sets_to_eval), self._max_evals_grouped)
]
Expand Down