2323from abc import abstractmethod
2424from collections .abc import Iterable
2525import pickle
26- from typing import Any , TYPE_CHECKING
26+ from typing import Any
2727
2828import numpy as np
2929
3232from lambeq .backend .tensor import Diagram
3333from lambeq .training .checkpoint import Checkpoint
3434from lambeq .training .model import Model
35-
36-
37- if TYPE_CHECKING :
38- from jax import numpy as jnp
35+ from lambeq .typing import AnyTensor
3936
4037
4138class QuantumModel (Model ):
@@ -46,29 +43,31 @@ class QuantumModel(Model):
4643 symbols : list of symbols
4744 A sorted list of all :py:class:`Symbols <.Symbol>` occurring in
4845 the data.
49- weights : array
46+ weights : AnyTensor
5047 A data structure containing the numeric values of the model
51- parameters
48+ parameters. This could be a `torch.Tensor`, `np.ndarray`, or
49+ one from a different backend.
5250
5351 """
54- weights : np .ndarray
52+
53+ weights : AnyTensor
5554
5655 def __init__ (self ) -> None :
5756 """Initialise a :py:class:`QuantumModel`."""
5857 super ().__init__ ()
5958
6059 self ._training = False
61- self ._train_predictions : list [Any ] = []
60+ self ._train_predictions : list [AnyTensor ] = []
6261
63- def _log_prediction (self , y : Any ) -> None :
62+ def _log_prediction (self , y : AnyTensor ) -> None :
6463 """Log a prediction of the model."""
6564 self ._train_predictions .append (y )
6665
6766 def _clear_predictions (self ) -> None :
6867 """Clear the logged predictions of the model."""
6968 self ._train_predictions = []
7069
71- def _normalise_vector (self , predictions : np . ndarray ) -> np . ndarray :
70+ def _normalise_vector (self , predictions : AnyTensor ) -> AnyTensor :
7271 """Normalise the vector input.
7372
7473 Special cases:
@@ -77,11 +76,11 @@ def _normalise_vector(self, predictions: np.ndarray) -> np.ndarray:
7776 """
7877
7978 backend = numerical_backend .get_backend ()
80- ret : np . ndarray = backend .abs (predictions )
79+ ret : AnyTensor = backend .abs (predictions )
8180
8281 if predictions .shape :
8382 # Prevent division by 0
84- l1_norm = backend .maximum (1e-9 , ret .sum ())
83+ l1_norm = backend .maximum (backend . array ( 1e-9 ) , ret .sum ())
8584 ret = ret / l1_norm
8685
8786 return ret
@@ -158,7 +157,7 @@ def _fast_subs(self,
158157 def get_diagram_output (
159158 self ,
160159 diagrams : list [Diagram ]
161- ) -> jnp . ndarray | np . ndarray :
160+ ) -> AnyTensor :
162161 """Return the diagram prediction.
163162
164163 Parameters
@@ -169,14 +168,14 @@ def get_diagram_output(
169168
170169 """
171170
172- def __call__ (self , * args : Any , ** kwargs : Any ) -> Any :
171+ def __call__ (self , * args : Any , ** kwargs : Any ) -> AnyTensor :
173172 out = self .forward (* args , ** kwargs )
174173 if self ._training :
175174 self ._log_prediction (out )
176175 return out
177176
178177 @abstractmethod
179- def forward (self , x : list [Diagram ]) -> Any :
178+ def forward (self , x : list [Diagram ]) -> AnyTensor :
180179 """Compute the forward pass of the model using
181180 `get_model_output`
182181
0 commit comments