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
27 changes: 27 additions & 0 deletions docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,33 @@ 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 Keras models

Saving a complete model with `tf.keras.Model.save()` can fail when a layer
was created with arguments that Keras cannot serialize and the layer does not
override `get_config()`. For example, a model containing
`tfq.layers.ControlledPQC` fails to save in TFQ 0.7.2. Depending on the
format, the error is a `NotEncodableError` or a `NotImplementedError` saying
the layer must override `get_config()`. The message does not always make
clear that the layer is the cause.

As an alternative, `tf.keras.Model.save_weights()` saves only the model
weights, without the complete model configuration. Recreate the 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")
```

For details about the project's Keras setup, see the
[installation guide](install.md). TensorFlow Quantum's installation
instructions use the legacy `tf-keras` package via
`TF_USE_LEGACY_KERAS=1`. Saving and loading behavior may depend on the
TensorFlow and Keras versions in use.

### Differentiators

Unlike many TensorFlow operations, observables in quantum circuits do not have
Expand Down
Loading