#!/usr/bin/env python # coding: utf-8 # # Text File Input and Output # # This brief tutorial focuses on the different pyGSTi objects that can be converted to & from text files. Currently, `GateSet`, `DataSet`, `MultiDataSet`, and `TDDataSet` objects, as well as lists and dictionaries of `GateString` objects, can be saved to and loaded from text files. All text-based input and output is done via the `pygsti.io` sub-package. When objects have `save` and `load` methods (as the data set types do), these save and load *binary* formats which are different from the text formats used by the `pygsti.io` routines. Objects may also be pickled using Python's `pickle` package, which is yet another way of saving and loading pyGSTi objects. # # Below we give examples of saving and loading each type of objects to/from text format. Many of these examples appear in other tutorials, but we thought it might be useful to collect them here. # In[1]: import pygsti #GateSets ------------------------------------------------------------ gateset_txt = \ """ # Example text file describing a gateset # State prepared, specified as a state in the Pauli basis (I,X,Y,Z) PREP: rho0 LiouvilleVec 1/sqrt(2) 0 0 1/sqrt(2) POVM: Mdefault # State measured as yes (zero) outcome, also specified as a state in the Pauli basis EFFECT: 0 LiouvilleVec 1/sqrt(2) 0 0 1/sqrt(2) EFFECT: 1 LiouvilleVec 1/sqrt(2) 0 0 -1/sqrt(2) END POVM GATE: Gi LiouvilleMx 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 GATE: Gx LiouvilleMx 1 0 0 0 0 1 0 0 0 0 0 -1 0 0 1 0 GATE: Gy LiouvilleMx 1 0 0 0 0 0 0 1 0 0 1 0 0 -1 0 0 BASIS: pp 2 """ with open("tutorial_files/TestGateSet.txt","w") as f: f.write(gateset_txt) gs_target = pygsti.io.load_gateset("tutorial_files/TestGateSet.txt") pygsti.io.write_gateset(gs_target, "tutorial_files/TestGateSet.txt") #DataSets ------------------------------------------------------------ dataset_txt = \ """## Columns = 0 count, count total {} 0 100 Gx 10 90 GxGy 40 60 Gx^4 20 90 """ with open("tutorial_files/TestDataSet.txt","w") as f: f.write(dataset_txt) ds = pygsti.io.load_dataset("tutorial_files/TestDataSet.txt") pygsti.io.write_dataset("tutorial_files/TestDataSet.txt", ds) #MultiDataSets ------------------------------------------------------------ multidataset_txt = \ """## Columns = DS0 0 count, DS0 1 count, DS1 0 frequency, DS1 count total {} 0 100 0 100 Gx 10 90 0.1 100 GxGy 40 60 0.4 100 Gx^4 20 80 0.2 100 """ with open("tutorial_files/TestMultiDataSet.txt","w") as f: f.write(multidataset_txt) multiDS = pygsti.io.load_multidataset("tutorial_files/TestMultiDataSet.txt", cache=True) pygsti.io.write_multidataset("tutorial_files/TestDataSet.txt", multiDS) #TDDataSets ------------------------------------------------------------ # Note: left of equals sign is letter, right is spam label tddataset_txt = \ """## 0 = 0 ## 1 = 1 {} 011001 Gx 111000111 Gy 11001100 """ with open("tutorial_files/TestTDDataset.txt","w") as f: f.write(tddataset_txt) tdds_fromfile = pygsti.io.load_tddataset("tutorial_files/TestTDDataset.txt") #NOTE: currently there's no way to *write* a TDDataSet. #GateStrings ------------------------------------------------------------ from pygsti.construction import std1Q_XY gsList = pygsti.construction.make_lsgst_experiment_list( ['Gx','Gy'], std1Q_XY.prepStrs, std1Q_XY.effectStrs, std1Q_XY.germs, [1,2,4,8]) pygsti.io.write_gatestring_list("tutorial_files/TestGatestringList.txt",gsList,"#Test GateString List") pygsti.io.write_empty_dataset("tutorial_files/TestEmptyDataset.txt",gsList) #additionally creates columns of zeros where data should go... gsList2 = pygsti.io.load_gatestring_list("tutorial_files/TestGatestringList.txt") # In[ ]: