Skip to content

docs: explain save vs save_weights for TFQ layers - #1120

Open
SripriyaDevarapalli wants to merge 2 commits into
tensorflow:masterfrom
SripriyaDevarapalli:docs-save-vs-save-weights
Open

SripriyaDevarapalli wants to merge 2 commits into
tensorflow:masterfrom
SripriyaDevarapalli:docs-save-vs-save-weights

Conversation

@SripriyaDevarapalli

Copy link
Copy Markdown

Description

This pull request addresses the documentation issue in #899 regarding the behavior of tf.keras.Model.save() versus save_weights() for TensorFlow Quantum (TFQ) layers.

Summary of changes

  • Documents the reported serialization issue when saving complete Keras models containing TFQ layers that may not support configuration serialization.
  • Explains save_weights() as an alternative when complete model serialization fails.
  • Provides a Python example showing how to save weights and load them into a recreated model architecture.
  • Links to the TFQ installation guide and notes that saving and loading behavior may depend on the TensorFlow and Keras versions in use.

Related to #899 and #56.

How Has This Been Tested?

  • Ran git diff --cached --check successfully.
  • Reviewed the documentation changes.
    • A TFQ runtime save/load test could not be completed because the local checkout lacks the compiled native operation library required to import TFQ.
  • This is a documentation-only change; no source code or tests were modified.

Test Configuration:

  • Python version: 3.11
  • Operating system: Windows
  • TensorFlow version: 2.19.1
  • tf-keras version: 2.19.0

AI use

  • No AI tool was used.
  • AI-assisted: I did the research and wrote the change, and used an AI tool for parts of it (for example completion, editing or review).
  • AI-generated: an AI tool did most of the research or wrote most of the change. I have read all of it and can explain it.
  • Translated into English with an AI tool.

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new errors or warnings
  • My code and tests pass locally and have at least 80% line coverage
  • Any dependent changes have been merged and published in downstream modules

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request updates the design documentation in docs/design.md to include a new section on saving Keras models. It explains why saving a complete model with tf.keras.Model.save() can fail for certain layers and provides a code example demonstrating how to use save_weights() and load_weights() as a workaround. There are no review comments, so there is no feedback to provide.

@SripriyaDevarapalli

SripriyaDevarapalli commented Oct 13, 2026 •

Copy link
Copy Markdown
Author

Hey @mhucka I've created a PR #1120 addressing the documentation issue regarding the behavior of save() versus save_weights() for TensorFlow Quantum layers.

The changes explain the reported serialization issue, document save_weights() as an alternative, and include a usage example, and if you have any suggestions for improvement please do let me know .

Thank you!

@SripriyaDevarapalli

Copy link
Copy Markdown
Author

I also noticed that the CI run failed in the Tutorial tests job.

The logs show an ImportError indicating that pydot and/or Graphviz are missing when tf.keras.utils.plot_model() is called. The file format and lint checks, wheel test, and library tests all passed.

This appears to be a missing dependency in the tutorial test environment rather than an issue caused by my documentation changes. I wanted to bring this to your attention for investigation.

Thank you!

@mhucka mhucka left a comment

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.

Thanks for working on this. A few adjustments are needed to make the section accurate and fit the structure of docs/design.md:

  1. In docs/design.md, the ### headings under ## TensorFlow Quantum primitives (Fundamental ops, Layers, Differentiators, Datasets) correspond to the four main component types in TFQ. Since model saving applies specifically to tfq.layers, please make this a #### Saving and loading models subsection under ### Layers rather than a sibling ### section.

  2. This limitation is not specific to TFQ 0.7.2, and it does not depend on the TensorFlow/Keras version. None of the layers in tfq.layers implement get_config(), because they accept non-JSON-serializable quantum objects (such as cirq.Circuit, cirq.PauliSum, and sympy.Symbol). Please state directly that saving a complete model with model.save() is not supported for models containing TFQ layers, and mention tfq.layers.PQC as well as tfq.layers.ControlledPQC.

  3. Calling model.save() raises a NotImplementedError (stating 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).

  4. The tf-keras environment variable is unrelated to why model.save() fails on TFQ layers, so linking to install.md here can confuse readers. Remove the final paragraph about install.md and TF_USE_LEGACY_KERAS=1.

@SripriyaDevarapalli

Copy link
Copy Markdown
Author

@mhucka Thank you for the feedback! I've updated docs/design.md to address all the requested changes.
I've committed and pushed the updates to the existing PR. All six checks are passing. Could you please take a look and review it ?
Thanks again for your guidance!

@SripriyaDevarapalli

SripriyaDevarapalli commented Oct 18, 2026 via email

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants