Skip to content
Open
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
18 changes: 18 additions & 0 deletions docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading