Skip to content

add JSON serialization of ffsim objects - #754

Open
Kevin J. Sung (kevinsung) wants to merge 1 commit into
Qiskit:mainfrom
kevinsung:serialization
Open

Kevin J. Sung (kevinsung) wants to merge 1 commit into
Qiskit:mainfrom
kevinsung:serialization

Conversation

@kevinsung

@kevinsung Kevin J. Sung (kevinsung) commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Add ffsim.to_json and ffsim.from_json, along with the dictionary-level ffsim.to_json_dict and ffsim.from_json_dict, for saving and loading ffsim objects.

  • Supported types: every public ffsim dataclass (Hamiltonians, ansatz operators, StateVector, ReducedDensityMatrix, and MolecularData) and FermionOperator. A test checks that new public dataclasses get registered. Registration is internal, so users can't register their own classes.
  • Format: JSON tagged with a stable type name, such as "ffsim.MolecularHamiltonian", that is independent of the module path. Arrays are stored as base64 bytes with their dtype and shape, so they round-trip exactly. Values that can't be represented exactly raise an error instead of being silently converted.
  • Loading: only ffsim's own classes are constructed, so unlike pickle, loading a file can't run arbitrary code. Optional validation is skipped because the validation tolerances aren't stored, which lets objects created with validate=False load.
  • Compression: gzip, bz2, or lzma. By default it is inferred from the file extension (.gz, .bz2, .xz, .lzma) when writing and detected from the file contents when reading.

MolecularData.to_json and MolecularData.from_json are deprecated in favor of the new functions, and they keep their previous defaults. ffsim.from_json still reads MolecularData files written by ffsim 0.0.86 and earlier, but those versions can't read the new format. This removes the orjson dependency.

@kevinsung
Kevin J. Sung (kevinsung) force-pushed the serialization branch 7 times, most recently from 0070013 to 0f7cc6b Compare November 18, 2026 14:50
Add ffsim.to_json and ffsim.from_json, along with the dictionary-level
to_json_dict and from_json_dict, for saving and loading every public
ffsim dataclass and FermionOperator. Objects are stored as JSON tagged
with a stable type name, and arrays are stored as base64 bytes with
their dtype and shape, so they round-trip exactly. Loading only
constructs ffsim's own classes, and it skips optional validation
because validation tolerances are not stored.

Compression (gzip, bz2, or lzma) is inferred from the file extension
when writing and detected from the file contents when reading.

Deprecate MolecularData.to_json and MolecularData.from_json in favor of
the new functions. ffsim.from_json still reads MolecularData files
written by ffsim 0.0.86 and earlier, but those versions cannot read the
new format. This removes the orjson dependency.

Add a how-to guide on saving and loading objects to the docs.

This branch has not been deployed

No deployments
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.

1 participant