{ "cells": [ { "cell_type": "markdown", "id": "a4d9978c-e611-429e-991c-8a550d9b123d", "metadata": {}, "source": [ "# Saving and loading operators\n", "\n", "Often times it is unnecessary to repeatedly build the quantum operator associated with the Hamiltonian of interest. Here we show how to load and save operators. In this example we will use a `FermionicOperator`, but the usage is identical for the `QubitOperator`." ] }, { "cell_type": "code", "execution_count": null, "id": "0228dcfd-f005-4065-a39e-fd52712c6f67", "metadata": {}, "outputs": [], "source": [ "import fulqrum as fq" ] }, { "cell_type": "markdown", "id": "e8b020a4-dc6a-417b-a352-9ffed161c212", "metadata": {}, "source": [ "## Loading an operator\n", "\n", "Fulqrum stores its operators as JSON files. In addition it supports the LZMA compression of the JSON data. That is to say that the valid file extensions are `*.json` and `*.xz`. \n", "\n", "For example, to load a `FermionicOperator` from an uncompressed JSON file one can do:" ] }, { "cell_type": "code", "execution_count": 5, "id": "ac656bed-12f6-445a-b336-51676aa23ead", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fop = fq.FermionicOperator.from_json(\"./data/lih.json\")\n", "fop" ] }, { "cell_type": "markdown", "id": "a480be00-1f09-4da6-9790-b6a207f865bf", "metadata": {}, "source": [ "## Saving an operator\n", "\n", "Saving an operator is very easy, and also supports `*.json` and `*.xz` extensions. Compression saves 10x+ in the final file size and is highly recommended. Moreover, it is suggested to save the files with the file name specified as `*.json.xz`. In this way, uncompressing the file outside of Fulqrum will recognize the output as being of JSON type.\n", "\n", "Here we save the operator loaded above into a compressed file. In addition, we highlight the `overwrite` flag that must be set if the file already exists at the given location:" ] }, { "cell_type": "code", "execution_count": 10, "id": "19ea6b8d-5dbe-48b1-a2cf-d5f3be136d55", "metadata": {}, "outputs": [], "source": [ "fop.to_json(\"./data/lih.json.xz\", overwrite=True)" ] }, { "cell_type": "markdown", "id": "32537d7a-2667-4f6e-8bee-b358da490eb1", "metadata": {}, "source": [ "To show that this equals the original operator we load the compressed version as well:" ] }, { "cell_type": "code", "execution_count": 11, "id": "fc6c6b47-e09f-4772-b9af-fa62812167da", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fop2 = fq.FermionicOperator.from_json(\"./data/lih.json.xz\")\n", "fop2" ] }, { "cell_type": "code", "execution_count": null, "id": "c2f5e982-22f3-49df-855a-913033ba30a1", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.12" } }, "nbformat": 4, "nbformat_minor": 5 }