Saving and loading operators

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.

[1]:
import fulqrum as fq

Loading an operator

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.

For example, to load a FermionicOperator from an uncompressed JSON file one can do:

[2]:
fop = fq.FermionicOperator.from_json("./data/lih.json")
fop
[2]:
<FermionicOperator[('', (1.5875316327600002+0j)), ('+:0 -:0', (-4.967732288374651+0j)), ('+:0 -:1', (0.14689500990413493+0j)), ('+:0 -:2', (0.17429174218333252+0j)), ('+:0 -:5', (0.06238463680356952+0j)), ('+:6 -:6', (-4.967732288374651+0j)), ('+:6 -:7', (0.14689500990413493+0j)), ('+:6 -:8', (0.17429174218333252+0j)), ('+:6 -:11', (0.06238463680356952+0j)), ('-:0 +:1', (-0.1468950099041349-0j)), ('+:1 -:1', (-1.7548076405756867+0j)), ('+:1 -:2', (0.04174210386247541+0j)), ('+:1 -:5', (-0.3241690966367784+0j)), ('-:6 +:7', (-0.1468950099041349-0j)), ('+:7 -:7', (-1.7548076405756867+0j)), ('+:7 -:8', (0.04174210386247541+0j)), ('+:7 -:11', (-0.3241690966367784+0j)), ('-:0 +:2', (-0.1742917421833327-0j)), ('-:1 +:2', (-0.04174210386247552-0j)), ('+:2 -:2', (-1.1812532412470829+0j)), ('+:2 -:5', (0.03685429859814189+0j)), ('-:6 +:8', (-0.1742917421833327-0j)), ('-:7 +:8', (-0.04174210386247552-0j)), ('+:8 -:8', (-1.1812532412470829+0j)), ('+:8 -:11', (0.03685429859814189+0j)), ('+:3 -:3', (-1.2007233089159732+0j)), ('+:9 -:9', (-1.2007233089159732+0j)), ('+:4 -:4', (-1.2007233089159732+0j)), ('+:10 -:10', (-1.2007233089159732+0j)), ('-:0 +:5', (-0.0623846368035691-0j)), ('-:1 +:5', (0.3241690966367782+0j)), ('-:2 +:5', (-0.03685429859814207-0j)), ('+:5 -:5', (-0.9457979824776613+0j)), ('-:6 +:11', (-0.0623846368035691-0j)), ('-:7 +:11', (0.3241690966367782+0j)), ('-:8 +:11', (-0.03685429859814207-0j)), ('+:11 -:11', (-0.9457979824776613+0j)), ('+:0 +:0 -:0 -:0', (0.8265878101733488+0j)), ('+:0 +:0 -:0 -:1', (-0.08074920665817954+0j)), ('+:0 +:0 -:0 -:2', (-0.06512387394877255+0j)), ('+:0 +:0 -:0 -:5', (-0.02991600261035553+0j)), ('+:0 +:0 -:0 -:1', (0.08074920665817967+0j)), ('+:0 +:0 -:1 -:1', (0.01563021691406638+0j)), ('+:0 +:0 -:1 -:2', (0.007241974243568983+0j)), ('+:0 +:0 -:1 -:5', (0.0044910895777624665+0j)), ('+:0 +:0 -:0 -:2', (0.06512387394877257+0j)), ('+:0 +:0 -:1 -:2', (-0.007241974243568983-0j)), ('+:0 +:0 -:2 -:2', (0.010402033799454063+0j)), ('+:0 +:0 -:2 -:5', (0.0042687967535342914+0j)), ('+:0 +:0 -:3 -:3', (0.005061102947179691+0j)), ('+:0 +:0 -:4 -:4', (0.005061102947179691+0j)), ('+:0 +:0 -:0 -:5', (0.029916002610355697+0j)), ('+:0 +:0 -:1 -:5', (-0.004491089577762447-0j)), ('+:0 +:0 -:2 -:5', (-0.00426879675353429-0j)), ('+:0 +:0 -:5 -:5', (0.00289544975130684+0j)), ('+:0 -:0 +:6 -:6', (0.8265878101733488+0j)), ('+:0 -:1 +:6 -:6', (-0.08074920665817954+0j)), ('+:0 -:2 +:6 -:6', (-0.06512387394877255+0j)), ('+:0 -:5 +:6 -:6', (-0.02991600261035553+0j)), ('+:0 -:0 +:6 -:7', (-0.08074920665817967+0j)), ('+:0 -:1 +:6 -:7', (0.01563021691406638+0j)), ('+:0 -:2 +:6 -:7', (0.007241974243568983+0j)), ('+:0 -:5 +:6 -:7', (0.0044910895777624665+0j)), ('+:0 -:0 +:6 -:8', (-0.06512387394877257+0j)), ('+:0 -:1 +:6 -:8', (0.007241974243568983+0j)), ('+:0 -:2 +:6 -:8', (0.010402033799454063+0j)), ('+:0 -:5 +:6 -:8', (0.0042687967535342914+0j)), ('+:0 -:3 +:6 -:9', (0.005061102947179691+0j)), ('+:0 -:4 +:6 -:10', (0.005061102947179691+0j)), ('+:0 -:0 +:6 -:11', (-0.029916002610355697+0j)), ('+:0 -:1 +:6 -:11', (0.004491089577762447+0j)), ('+:0 -:2 +:6 -:11', (0.00426879675353429+0j)), ('+:0 -:5 +:6 -:11', (0.00289544975130684+0j)), ('+:0 -:0 -:0 +:1', (-0.08074920665817967+0j)), ('+:0 -:0 +:1 -:1', (-0.01563021691406638-0j)), ('+:0 -:0 +:1 -:2', (-0.007241974243568983-0j)), ('+:0 -:0 +:1 -:5', (-0.0044910895777624665-0j)), ('+:0 -:0 +:1 -:1', (0.2350161806915096+0j)), ('+:0 +:1 -:1 -:1', (0.007301700469854646+0j)), ('+:0 +:1 -:1 -:2', (-0.012631474947092715+0j)), ('+:0 +:1 -:1 -:5', (0.002523429884054909+0j)), ('+:0 -:0 +:1 -:2', (0.0025301362519371696+0j)), ('+:0 +:1 -:1 -:2', (0.0032409538879733624+0j)), ('+:0 +:1 -:2 -:2', (4.3755849108912444e-06+0j)), ('+:0 +:1 -:2 -:5', (-0.0018940407883860407+0j)), ('+:0 +:1 -:3 -:3', (0.004096236189882244+0j)), ('+:0 +:1 -:4 -:4', (0.004096236189882244+0j)), ('+:0 -:0 +:1 -:5', (0.04273887999635893+0j)), ('+:0 +:1 -:1 -:5', (-0.0063231751825688414-0j)), ('+:0 +:1 -:2 -:5', (0.006161322397037098+0j)), ('+:0 +:1 -:5 -:5', (0.004045883790841293+0j)), ('+:0 -:0 -:6 +:7', (0.08074920665817967+0j)), ('+:0 -:1 -:6 +:7', (-0.01563021691406638-0j)), ('+:0 -:2 -:6 +:7', (-0.007241974243568983-0j)), ('+:0 -:5 -:6 +:7', (-0.0044910895777624665-0j)), ('+:0 -:0 +:7 -:7', (0.2350161806915096+0j)), ('+:0 -:1 +:7 -:7', (0.007301700469854646+0j)), ('+:0 -:2 +:7 -:7', (-0.012631474947092715+0j)), ('+:0 -:5 +:7 -:7', (0.002523429884054909+0j)), ('+:0 -:0 +:7 -:8', (0.0025301362519371696+0j)) + 1761 more terms], width=12>

Saving an operator

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.

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:

[3]:
fop.to_json("./data/lih.json.xz", overwrite=True)

To show that this equals the original operator we load the compressed version as well:

[4]:
fop2 = fq.FermionicOperator.from_json("./data/lih.json.xz")
fop2
[4]:
<FermionicOperator[('', (1.5875316327600002+0j)), ('+:0 -:0', (-4.967732288374651+0j)), ('+:0 -:1', (0.14689500990413493+0j)), ('+:0 -:2', (0.17429174218333252+0j)), ('+:0 -:5', (0.06238463680356952+0j)), ('+:6 -:6', (-4.967732288374651+0j)), ('+:6 -:7', (0.14689500990413493+0j)), ('+:6 -:8', (0.17429174218333252+0j)), ('+:6 -:11', (0.06238463680356952+0j)), ('-:0 +:1', (-0.1468950099041349-0j)), ('+:1 -:1', (-1.7548076405756867+0j)), ('+:1 -:2', (0.04174210386247541+0j)), ('+:1 -:5', (-0.3241690966367784+0j)), ('-:6 +:7', (-0.1468950099041349-0j)), ('+:7 -:7', (-1.7548076405756867+0j)), ('+:7 -:8', (0.04174210386247541+0j)), ('+:7 -:11', (-0.3241690966367784+0j)), ('-:0 +:2', (-0.1742917421833327-0j)), ('-:1 +:2', (-0.04174210386247552-0j)), ('+:2 -:2', (-1.1812532412470829+0j)), ('+:2 -:5', (0.03685429859814189+0j)), ('-:6 +:8', (-0.1742917421833327-0j)), ('-:7 +:8', (-0.04174210386247552-0j)), ('+:8 -:8', (-1.1812532412470829+0j)), ('+:8 -:11', (0.03685429859814189+0j)), ('+:3 -:3', (-1.2007233089159732+0j)), ('+:9 -:9', (-1.2007233089159732+0j)), ('+:4 -:4', (-1.2007233089159732+0j)), ('+:10 -:10', (-1.2007233089159732+0j)), ('-:0 +:5', (-0.0623846368035691-0j)), ('-:1 +:5', (0.3241690966367782+0j)), ('-:2 +:5', (-0.03685429859814207-0j)), ('+:5 -:5', (-0.9457979824776613+0j)), ('-:6 +:11', (-0.0623846368035691-0j)), ('-:7 +:11', (0.3241690966367782+0j)), ('-:8 +:11', (-0.03685429859814207-0j)), ('+:11 -:11', (-0.9457979824776613+0j)), ('+:0 +:0 -:0 -:0', (0.8265878101733488+0j)), ('+:0 +:0 -:0 -:1', (-0.08074920665817954+0j)), ('+:0 +:0 -:0 -:2', (-0.06512387394877255+0j)), ('+:0 +:0 -:0 -:5', (-0.02991600261035553+0j)), ('+:0 +:0 -:0 -:1', (0.08074920665817967+0j)), ('+:0 +:0 -:1 -:1', (0.01563021691406638+0j)), ('+:0 +:0 -:1 -:2', (0.007241974243568983+0j)), ('+:0 +:0 -:1 -:5', (0.0044910895777624665+0j)), ('+:0 +:0 -:0 -:2', (0.06512387394877257+0j)), ('+:0 +:0 -:1 -:2', (-0.007241974243568983-0j)), ('+:0 +:0 -:2 -:2', (0.010402033799454063+0j)), ('+:0 +:0 -:2 -:5', (0.0042687967535342914+0j)), ('+:0 +:0 -:3 -:3', (0.005061102947179691+0j)), ('+:0 +:0 -:4 -:4', (0.005061102947179691+0j)), ('+:0 +:0 -:0 -:5', (0.029916002610355697+0j)), ('+:0 +:0 -:1 -:5', (-0.004491089577762447-0j)), ('+:0 +:0 -:2 -:5', (-0.00426879675353429-0j)), ('+:0 +:0 -:5 -:5', (0.00289544975130684+0j)), ('+:0 -:0 +:6 -:6', (0.8265878101733488+0j)), ('+:0 -:1 +:6 -:6', (-0.08074920665817954+0j)), ('+:0 -:2 +:6 -:6', (-0.06512387394877255+0j)), ('+:0 -:5 +:6 -:6', (-0.02991600261035553+0j)), ('+:0 -:0 +:6 -:7', (-0.08074920665817967+0j)), ('+:0 -:1 +:6 -:7', (0.01563021691406638+0j)), ('+:0 -:2 +:6 -:7', (0.007241974243568983+0j)), ('+:0 -:5 +:6 -:7', (0.0044910895777624665+0j)), ('+:0 -:0 +:6 -:8', (-0.06512387394877257+0j)), ('+:0 -:1 +:6 -:8', (0.007241974243568983+0j)), ('+:0 -:2 +:6 -:8', (0.010402033799454063+0j)), ('+:0 -:5 +:6 -:8', (0.0042687967535342914+0j)), ('+:0 -:3 +:6 -:9', (0.005061102947179691+0j)), ('+:0 -:4 +:6 -:10', (0.005061102947179691+0j)), ('+:0 -:0 +:6 -:11', (-0.029916002610355697+0j)), ('+:0 -:1 +:6 -:11', (0.004491089577762447+0j)), ('+:0 -:2 +:6 -:11', (0.00426879675353429+0j)), ('+:0 -:5 +:6 -:11', (0.00289544975130684+0j)), ('+:0 -:0 -:0 +:1', (-0.08074920665817967+0j)), ('+:0 -:0 +:1 -:1', (-0.01563021691406638-0j)), ('+:0 -:0 +:1 -:2', (-0.007241974243568983-0j)), ('+:0 -:0 +:1 -:5', (-0.0044910895777624665-0j)), ('+:0 -:0 +:1 -:1', (0.2350161806915096+0j)), ('+:0 +:1 -:1 -:1', (0.007301700469854646+0j)), ('+:0 +:1 -:1 -:2', (-0.012631474947092715+0j)), ('+:0 +:1 -:1 -:5', (0.002523429884054909+0j)), ('+:0 -:0 +:1 -:2', (0.0025301362519371696+0j)), ('+:0 +:1 -:1 -:2', (0.0032409538879733624+0j)), ('+:0 +:1 -:2 -:2', (4.3755849108912444e-06+0j)), ('+:0 +:1 -:2 -:5', (-0.0018940407883860407+0j)), ('+:0 +:1 -:3 -:3', (0.004096236189882244+0j)), ('+:0 +:1 -:4 -:4', (0.004096236189882244+0j)), ('+:0 -:0 +:1 -:5', (0.04273887999635893+0j)), ('+:0 +:1 -:1 -:5', (-0.0063231751825688414-0j)), ('+:0 +:1 -:2 -:5', (0.006161322397037098+0j)), ('+:0 +:1 -:5 -:5', (0.004045883790841293+0j)), ('+:0 -:0 -:6 +:7', (0.08074920665817967+0j)), ('+:0 -:1 -:6 +:7', (-0.01563021691406638-0j)), ('+:0 -:2 -:6 +:7', (-0.007241974243568983-0j)), ('+:0 -:5 -:6 +:7', (-0.0044910895777624665-0j)), ('+:0 -:0 +:7 -:7', (0.2350161806915096+0j)), ('+:0 -:1 +:7 -:7', (0.007301700469854646+0j)), ('+:0 -:2 +:7 -:7', (-0.012631474947092715+0j)), ('+:0 -:5 +:7 -:7', (0.002523429884054909+0j)), ('+:0 -:0 +:7 -:8', (0.0025301362519371696+0j)) + 1761 more terms], width=12>
[ ]: