{ "cells": [ { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "# Text File Input and Output\n", "\n", "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.\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loading tutorial_files/TestTDDataset.txt: 100%\n" ] } ], "source": [ "import pygsti\n", "\n", "\n", "#GateSets ------------------------------------------------------------\n", "gateset_txt = \\\n", "\"\"\"\n", "# Example text file describing a gateset\n", "\n", "# State prepared, specified as a state in the Pauli basis (I,X,Y,Z)\n", "PREP: rho0\n", "LiouvilleVec\n", "1/sqrt(2) 0 0 1/sqrt(2)\n", "\n", "POVM: Mdefault\n", "\n", "# State measured as yes (zero) outcome, also specified as a state in the Pauli basis\n", "EFFECT: 0\n", "LiouvilleVec\n", "1/sqrt(2) 0 0 1/sqrt(2)\n", "\n", "EFFECT: 1\n", "LiouvilleVec\n", "1/sqrt(2) 0 0 -1/sqrt(2)\n", "\n", "END POVM\n", "\n", "GATE: Gi\n", "LiouvilleMx\n", "1 0 0 0\n", "0 1 0 0\n", "0 0 1 0\n", "0 0 0 1\n", "\n", "GATE: Gx\n", "LiouvilleMx\n", "1 0 0 0\n", "0 1 0 0\n", "0 0 0 -1\n", "0 0 1 0\n", "\n", "GATE: Gy\n", "LiouvilleMx\n", "1 0 0 0\n", "0 0 0 1\n", "0 0 1 0\n", "0 -1 0 0\n", "\n", "BASIS: pp 2\n", "\"\"\"\n", "with open(\"tutorial_files/TestGateSet.txt\",\"w\") as f:\n", " f.write(gateset_txt)\n", " \n", "gs_target = pygsti.io.load_gateset(\"tutorial_files/TestGateSet.txt\")\n", "pygsti.io.write_gateset(gs_target, \"tutorial_files/TestGateSet.txt\")\n", "\n", "\n", "#DataSets ------------------------------------------------------------\n", "dataset_txt = \\\n", "\"\"\"## Columns = 0 count, count total\n", "{} 0 100\n", "Gx 10 90\n", "GxGy 40 60\n", "Gx^4 20 90\n", "\"\"\"\n", "with open(\"tutorial_files/TestDataSet.txt\",\"w\") as f:\n", " f.write(dataset_txt)\n", "\n", "ds = pygsti.io.load_dataset(\"tutorial_files/TestDataSet.txt\")\n", "pygsti.io.write_dataset(\"tutorial_files/TestDataSet.txt\", ds)\n", "\n", "\n", "#MultiDataSets ------------------------------------------------------------\n", "multidataset_txt = \\\n", "\"\"\"## Columns = DS0 0 count, DS0 1 count, DS1 0 frequency, DS1 count total \n", "{} 0 100 0 100 \n", "Gx 10 90 0.1 100 \n", "GxGy 40 60 0.4 100 \n", "Gx^4 20 80 0.2 100 \n", "\"\"\"\n", "\n", "with open(\"tutorial_files/TestMultiDataSet.txt\",\"w\") as f:\n", " f.write(multidataset_txt)\n", " \n", "multiDS = pygsti.io.load_multidataset(\"tutorial_files/TestMultiDataSet.txt\", cache=True)\n", "pygsti.io.write_multidataset(\"tutorial_files/TestDataSet.txt\", multiDS)\n", "\n", "\n", "#TDDataSets ------------------------------------------------------------\n", "# Note: left of equals sign is letter, right is spam label\n", "tddataset_txt = \\\n", "\"\"\"## 0 = 0\n", "## 1 = 1\n", "{} 011001\n", "Gx 111000111\n", "Gy 11001100\n", "\"\"\"\n", "with open(\"tutorial_files/TestTDDataset.txt\",\"w\") as f:\n", " f.write(tddataset_txt)\n", " \n", "tdds_fromfile = pygsti.io.load_tddataset(\"tutorial_files/TestTDDataset.txt\")\n", "#NOTE: currently there's no way to *write* a TDDataSet.\n", "\n", "\n", "#GateStrings ------------------------------------------------------------\n", "from pygsti.construction import std1Q_XY \n", "gsList = pygsti.construction.make_lsgst_experiment_list(\n", " ['Gx','Gy'], std1Q_XY.prepStrs, std1Q_XY.effectStrs,\n", " std1Q_XY.germs, [1,2,4,8]) \n", "pygsti.io.write_gatestring_list(\"tutorial_files/TestGatestringList.txt\",gsList,\"#Test GateString List\")\n", "pygsti.io.write_empty_dataset(\"tutorial_files/TestEmptyDataset.txt\",gsList) \n", " #additionally creates columns of zeros where data should go...\n", "gsList2 = pygsti.io.load_gatestring_list(\"tutorial_files/TestGatestringList.txt\")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.5.2" } }, "nbformat": 4, "nbformat_minor": 2 }