diff --git a/docs/design.md b/docs/design.md index a82bc9716..e8e6e0091 100644 --- a/docs/design.md +++ b/docs/design.md @@ -87,6 +87,24 @@ operations. Additionally, you can create a layer with a high degree of complexity supporting batch circuit, batch control parameter value, and perform batch readout operations. See `tfq.layers.Sample` for an example. +#### Saving and loading models + +Saving a complete Keras model with `model.save()` is not supported for models containing TFQ layers, including `tfq.layers.PQC` and `tfq.layers.ControlledPQC`. These layers accept quantum objects, such as `cirq.Circuit`, `cirq.PauliSum`, and `sympy.Symbol`, that are not +JSON-serializable, and the layers do not implement `get_config()` to serialize their configurations. + +Depending on the serialization path, calling `model.save()` may raise a `NotImplementedError` indicating that the layer must override +`get_config()`, a `TypeError` for non-JSON-serializable `call()` +arguments such as `sympy.Symbol` or `cirq.PauliSum`, or a `NotEncodableError` when tracing layer `call` defaults for `SavedModel` export. This limitation is not specific to a particular TFQ, TensorFlow, or Keras version. + +As an alternative, `model.save_weights()` saves the model's weights without saving the complete model configuration. Recreate the same model architecture before loading the saved weights: + +```python +model.save_weights("my_model.weights.h5") + +new_model = build_model() # Recreate the same model architecture. +new_model.load_weights("my_model.weights.h5") +``` + ### Differentiators Unlike many TensorFlow operations, observables in quantum circuits do not have