• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

qiskit-community / qiskit-machine-learning / 15541719624

09 Jun 2025 06:27PM UTC coverage: 90.8% (-0.07%) from 90.865%
15541719624

Pull #951

github

web-flow
Merge cfcc7edca into 198a8ca46
Pull Request #951: Update serialising methods and instructions

6 of 10 new or added lines in 1 file covered. (60.0%)

4530 of 4989 relevant lines covered (90.8%)

0.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.95
/qiskit_machine_learning/algorithms/serializable_model.py
1
# This code is part of a Qiskit project.
2
#
3
# (C) Copyright IBM 2021, 2025.
4
#
5
# This code is licensed under the Apache License, Version 2.0. You may
6
# obtain a copy of this license in the LICENSE.txt file in the root directory
7
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
8
#
9
# Any modifications or derivative works of this code must retain this
10
# copyright notice, and modified files need to carry a notice indicating
11
# that they have been altered from the originals.
12
"""A mixin class for saving and loading models."""
13

14
from typing import Any
1✔
15

16
import dill
1✔
17

18
from ..utils.deprecation import issue_deprecation_msg
1✔
19

20

21
class SerializableModelMixin:
1✔
22
    """
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

30
    """
31

32
    def to_dill(self, file_name: str) -> None:
1✔
33
        """
34
        Saves this model to the specified file. Internally, the model is serialized via ``dill``.
35
        All parameters are saved, including a primitive instance that is referenced by internal
36
        objects. That means if a model is loaded from a file and is used, for instance, for
37
        inference, the same primitive will be used even if a cloud primitive was used.
38

39
        .. warning::
40
            Replaces the deprecated :meth:`save` method.
41

42
        Args:
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')
49
        """
50
        with open(file_name, "wb") as handler:
1✔
51
            dill.dump(self, handler)
1✔
52

53
    def save(self, *args) -> None:
1✔
54
        """Backwards compatibility with :meth:`to_dill`, deprecated in v0.9.0."""
NEW
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
        )
NEW
61
        self.to_dill(*args)
×
62

63
    @classmethod
1✔
64
    def from_dill(cls, file_name: str) -> Any:
1✔
65
        """
66
        Loads a model from a file. If the loaded model is not an instance of the class whose
67
        method was called, then a warning is raised. Nevertheless, the loaded model may be a valid
68
        model.
69

70
        Replaces the deprecated :meth:`load` method.
71

72
        Args:
73
            file_name: Path to the dill file containing the serialized model.
74

75
        Returns:
76
            An instance of the model loaded from disk.
77

78
        Example:
79
            .. code-block::
80

81
                loaded = MyModel.from_dill('model_state.dill')
82

83
        Raises:
84
            TypeError: if a loaded model is not an instance of the expected class.
85
        """
86
        with open(file_name, "rb") as handler:
1✔
87
            model = dill.load(handler)
1✔
88
        if not isinstance(model, cls):
1✔
89
            raise TypeError(f"Loaded model is of class {type(model)}. Expected class: {cls}.")
1✔
90
        return model
1✔
91

92
    @classmethod
1✔
93
    def load(cls, *args) -> Any:
1✔
94
        """Backwards compatibility with :meth:`from_dill`, deprecated in v0.9.0."""
NEW
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
        )
NEW
101
        return cls.from_dill(*args)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc