Skip to content
Merged
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
28 changes: 28 additions & 0 deletions docs/design.md

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please wrap to 80 columns, per .editorconfig and .markdownlintrc settings.

Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,34 @@ 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. The recreated model must
be built (its variables must be created) before calling `load_weights()`:

```python
model.save_weights("my_model.weights.h5")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

build_model() is never defined in the example. Readers have to guess what it should contain. At minimum, add a sentence saying the recreated model must be built (its variables created) before calling load_weights(). With the HDF5 format in tf-keras, loading weights into an unbuilt subclassed model raises a ValueError.


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