-
Notifications
You must be signed in to change notification settings - Fork 55
/
loaders.py
233 lines (193 loc) · 7.98 KB
/
loaders.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
""" Functions for loading GST objects from text files."""
from __future__ import division, print_function, absolute_import, unicode_literals
#*****************************************************************
# pyGSTi 0.9: Copyright 2015 Sandia Corporation
# This Software is released under the GPL license detailed
# in the file "license.txt" in the top-level pyGSTi directory
#*****************************************************************
import os as _os
from . import stdinput as _stdinput
from .. import objects as _objs
def load_dataset(filename, cache=False, collisionAction="aggregate",
verbosity=1):
"""
Load a DataSet from a file. First tries to load file as a
saved DataSet object, then as a standard text-formatted DataSet.
Parameters
----------
filename : string
The name of the file
cache : bool, optional
When set to True, a pickle file with the name filename + ".cache"
is searched for and loaded instead of filename if it exists
and is newer than filename. If no cache file exists or one
exists but it is older than filename, a cache file will be
written after loading from filename.
collisionAction : {"aggregate", "keepseparate"}
Specifies how duplicate gate sequences should be handled. "aggregate"
adds duplicate-sequence counts, whereas "keepseparate" tags duplicate-
sequence data with by appending a final "#<number>" gate label to the
duplicated gate sequence.
verbosity : int, optional
If zero, no output is shown. If greater than zero,
loading progress is shown.
Returns
-------
DataSet
"""
printer = _objs.VerbosityPrinter.build_printer(verbosity)
try:
# a saved Dataset object is ok
ds = _objs.DataSet(fileToLoadFrom=filename)
except:
#Parser functions don't take a VerbosityPrinter yet, and so
# always output to stdout (TODO)
bToStdout = (printer.verbosity > 0 and printer.filename is None)
if cache:
#bReadCache = False
cache_filename = filename + ".cache"
if _os.path.exists( cache_filename ) and \
_os.path.getmtime(filename) < _os.path.getmtime(cache_filename):
try:
printer.log("Loading from cache file: %s" % cache_filename)
ds = _objs.DataSet(fileToLoadFrom=cache_filename)
return ds
except: print("WARNING: Failed to load from cache file") # pragma: no cover
else:
printer.log("Cache file not found or is tool old -- one will"
+ "be created after loading is completed")
# otherwise must use standard dataset file format
parser = _stdinput.StdInputParser()
ds = parser.parse_datafile(filename, bToStdout,
collisionAction=collisionAction)
printer.log("Writing cache file (to speed future loads): %s"
% cache_filename)
ds.save(cache_filename)
else:
# otherwise must use standard dataset file format
parser = _stdinput.StdInputParser()
ds = parser.parse_datafile(filename, bToStdout,
collisionAction=collisionAction)
return ds
def load_multidataset(filename, cache=False, collisionAction="aggregate",
verbosity=1):
"""
Load a MultiDataSet from a file. First tries to load file as a
saved MultiDataSet object, then as a standard text-formatted MultiDataSet.
Parameters
----------
filename : string
The name of the file
cache : bool, optional
When set to True, a pickle file with the name filename + ".cache"
is searched for and loaded instead of filename if it exists
and is newer than filename. If no cache file exists or one
exists but it is older than filename, a cache file will be
written after loading from filename.
collisionAction : {"aggregate", "keepseparate"}
Specifies how duplicate gate sequences should be handled. "aggregate"
adds duplicate-sequence counts, whereas "keepseparate" tags duplicate-
sequence data with by appending a final "#<number>" gate label to the
duplicated gate sequence.
verbosity : int, optional
If zero, no output is shown. If greater than zero,
loading progress is shown.
Returns
-------
MultiDataSet
"""
printer = _objs.VerbosityPrinter.build_printer(verbosity)
try:
# a saved MultiDataset object is ok
mds = _objs.MultiDataSet(fileToLoadFrom=filename)
except:
#Parser functions don't take a VerbosityPrinter yet, and so
# always output to stdout (TODO)
bToStdout = (printer.verbosity > 0 and printer.filename is None)
if cache:
# bReadCache = False
cache_filename = filename + ".cache"
if _os.path.exists( cache_filename ) and \
_os.path.getmtime(filename) < _os.path.getmtime(cache_filename):
try:
printer.log("Loading from cache file: %s" % cache_filename)
mds = _objs.MultiDataSet(fileToLoadFrom=cache_filename)
return mds
except: print("WARNING: Failed to load from cache file") # pragma: no cover
else:
printer.log("Cache file not found or is too old -- one will be"
+ "created after loading is completed")
# otherwise must use standard dataset file format
parser = _stdinput.StdInputParser()
mds = parser.parse_multidatafile(filename, bToStdout,
collisionAction=collisionAction)
printer.log("Writing cache file (to speed future loads): %s"
% cache_filename)
mds.save(cache_filename)
else:
# otherwise must use standard dataset file format
parser = _stdinput.StdInputParser()
mds = parser.parse_multidatafile(filename, bToStdout,
collisionAction=collisionAction)
return mds
def load_tddataset(filename, cache=False):
"""
Load a TDDataSet (time-dependent data set) from a file.
"""
parser = _stdinput.StdInputParser()
tdds = parser.parse_tddatafile(filename)
return tdds
def load_gateset(filename):
"""
Load a GateSet from a file, formatted using the
standard text-format for gate sets.
Parameters
----------
filename : string
The name of the file
Returns
-------
GateSet
"""
return _stdinput.read_gateset(filename)
def load_gatestring_dict(filename):
"""
Load a gate string dictionary from a file, formatted
using the standard text-format.
Parameters
----------
filename : string
The name of the file.
Returns
-------
Dictionary with keys = gate string labels and
values = GateString objects.
"""
std = _stdinput.StdInputParser()
return std.parse_dictfile(filename)
def load_gatestring_list(filename, readRawStrings=False):
"""
Load a gate string list from a file, formatted
using the standard text-format.
Parameters
----------
filename : string
The name of the file
readRawStrings : boolean
If True, gate strings are not converted
to tuples of gate labels.
Returns
-------
list of GateString objects
"""
if readRawStrings:
rawList = []
with open(filename, 'r') as gatestringlist:
for line in gatestringlist:
if len(line.strip()) == 0: continue
if len(line) == 0 or line[0] == '#': continue
rawList.append( line.strip() )
return rawList
else:
std = _stdinput.StdInputParser()
return std.parse_stringfile(filename)