11# This code is part of a Qiskit project.
22#
3- # (C) Copyright IBM 2021, 2023 .
3+ # (C) Copyright IBM 2021, 2025 .
44#
55# This code is licensed under the Apache License, Version 2.0. You may
66# obtain a copy of this license in the LICENSE.txt file in the root directory
1515
1616import dill
1717
18+ from ..utils .deprecation import issue_deprecation_msg
19+
1820
1921class SerializableModelMixin :
2022 """
21- Provides convenient methods for saving and loading models.
23+ Provides convenient methods for saving and loading models via dill serialization.
24+
25+ .. warning::
26+ The legacy :meth:`save` and :meth:`load` methods are deprecated in v0.9.0
27+ and will be removed in a future release. Please use :meth:`to_dill`
28+ and :meth:`from_dill` respectively.
29+
2230 """
2331
24- def save (self , file_name : str ) -> None :
32+ def to_dill (self , file_name : str ) -> None :
2533 """
2634 Saves this model to the specified file. Internally, the model is serialized via ``dill``.
2735 All parameters are saved, including a primitive instance that is referenced by internal
2836 objects. That means if a model is loaded from a file and is used, for instance, for
2937 inference, the same primitive will be used even if a cloud primitive was used.
3038
39+ .. warning::
40+ Replaces the deprecated :meth:`save` method.
41+
3142 Args:
32- file_name: a file name or path where to save the model.
43+ file_name: Path where the serialized model will be written.
44+
45+ Example:
46+ .. code-block::
47+
48+ model.to_dill('model_state.dill')
3349 """
3450 with open (file_name , "wb" ) as handler :
3551 dill .dump (self , handler )
3652
53+ def save (self , * args ) -> None :
54+ """Backwards compatibility with :meth:`to_dill`, deprecated in v0.9.0."""
55+ issue_deprecation_msg (
56+ msg = "SerializableModelMixin.save() is deprecated." ,
57+ version = "0.9.0" ,
58+ remedy = "Use the to_dill() method instead." ,
59+ period = "4 months" ,
60+ )
61+ self .to_dill (* args )
62+
3763 @classmethod
38- def load (cls , file_name : str ) -> Any :
64+ def from_dill (cls , file_name : str ) -> Any :
3965 """
40- Loads a model from the file. If the loaded model is not an instance of the class whose
66+ Loads a model from a file. If the loaded model is not an instance of the class whose
4167 method was called, then a warning is raised. Nevertheless, the loaded model may be a valid
4268 model.
4369
70+ Replaces the deprecated :meth:`load` method.
71+
4472 Args:
45- file_name: a file name or path to load a model from .
73+ file_name: Path to the dill file containing the serialized model.
4674
4775 Returns:
48- A loaded model.
76+ An instance of the model loaded from disk.
77+
78+ Example:
79+ .. code-block::
80+
81+ loaded = MyModel.from_dill('model_state.dill')
4982
5083 Raises:
5184 TypeError: if a loaded model is not an instance of the expected class.
@@ -55,3 +88,14 @@ def load(cls, file_name: str) -> Any:
5588 if not isinstance (model , cls ):
5689 raise TypeError (f"Loaded model is of class { type (model )} . Expected class: { cls } ." )
5790 return model
91+
92+ @classmethod
93+ def load (cls , * args ) -> Any :
94+ """Backwards compatibility with :meth:`from_dill`, deprecated in v0.9.0."""
95+ issue_deprecation_msg (
96+ msg = "SerializableModelMixin.load() is deprecated." ,
97+ version = "0.9.0" ,
98+ remedy = "Use the from_dill() classmethod instead." ,
99+ period = "4 months" ,
100+ )
101+ return cls .from_dill (* args )
0 commit comments