/
writers.py
351 lines (288 loc) · 15 KB
/
writers.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
""" Functions for writing GST objects to 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 warnings as _warnings
# from . import stdinput as _stdinput
from .. import tools as _tools
from .. import objects as _objs
def write_empty_dataset(filename, gatestring_list,
headerString='## Columns = 1 frequency, count total', numZeroCols=None,
appendWeightsColumn=False):
"""
Write an empty dataset file to be used as a template.
Parameters
----------
filename : string
The filename to write.
gatestring_list : list of GateStrings
List of gate strings to write, each to be followed by numZeroCols zeros.
headerString : string, optional
Header string for the file; should start with a pound (#) or double-pound (##)
so it is treated as a commend or directive, respectively.
numZeroCols : int, optional
The number of zero columns to place after each gate string. If None,
then headerString must begin with "## Columns = " and number of zero
columns will be inferred.
appendWeightsColumn : bool, optional
Add an additional 'weights' column.
"""
if len(gatestring_list) > 0 and not isinstance(gatestring_list[0], _objs.GateString):
raise ValueError("Argument gatestring_list must be a list of GateString objects!")
if numZeroCols is None: #TODO: cleaner way to extract number of columns from headerString?
if headerString.startswith('## Columns = '):
numZeroCols = len(headerString.split(','))
else:
raise ValueError("Must specify numZeroCols since I can't figure it out from the header string")
with open(filename, 'w') as output:
zeroCols = " ".join( ['0']*numZeroCols )
output.write(headerString + '\n')
for gateString in gatestring_list: #gateString should be a GateString object here
output.write(gateString.str + " " + zeroCols + ((" %f" % gateString.weight) if appendWeightsColumn else "") + '\n')
def _outcome_to_str(x):
if _tools.isstr(x): return x
else: return ":".join([str(i) for i in x])
def write_dataset(filename, dataset, gatestring_list=None,
outcomeLabelOrder=None, fixedColumnMode=True):
"""
Write a text-formatted dataset file.
Parameters
----------
filename : string
The filename to write.
dataset : DataSet
The data set from which counts are obtained.
gatestring_list : list of GateStrings, optional
The list of gate strings to include in the written dataset.
If None, all gate strings are output.
outcomeLabelOrder : list, optional
A list of the outcome labels in dataset which specifies
the column order in the output file.
fixedColumnMode : bool, optional
When `True`, a file is written with column headers indicating which
outcome each column of counts corresponds to. If a row doesn't have
any counts for an outcome, `'--'` is used in its place. When `False`,
each row's counts are written in an expanded form that includes the
outcome labels (each "count" has the format <outcomeLabel>:<count>).
"""
if gatestring_list is not None:
if len(gatestring_list) > 0 and not isinstance(gatestring_list[0], _objs.GateString):
raise ValueError("Argument gatestring_list must be a list of GateString objects!")
else:
gatestring_list = list(dataset.keys())
if outcomeLabelOrder is not None: #convert to tuples if needed
outcomeLabelOrder = [ (ol,) if _tools.isstr(ol) else ol
for ol in outcomeLabelOrder ]
outcomeLabels = dataset.get_outcome_labels()
if outcomeLabelOrder is not None:
assert(len(outcomeLabelOrder) == len(outcomeLabels))
assert(all( [ol in outcomeLabels for ol in outcomeLabelOrder] ))
assert(all( [ol in outcomeLabelOrder for ol in outcomeLabels] ))
outcomeLabels = outcomeLabelOrder
headerString = ""
if hasattr(dataset,'comment') and dataset.comment is not None:
for commentLine in dataset.comment.split('\n'):
if commentLine.startswith('#'):
headerString += commentLine + '\n'
else:
headerString += "# " + commentLine + '\n'
if fixedColumnMode:
headerString += '## Columns = ' + ", ".join( [ "%s count" % _outcome_to_str(ol)
for ol in outcomeLabels ]) + '\n'
with open(filename, 'w') as output:
output.write(headerString)
for gateString in gatestring_list: #gateString should be a GateString object here
dataRow = dataset[gateString.tup]
counts = dataRow.counts
if fixedColumnMode:
#output '--' for outcome labels that aren't present in this row
output.write(gateString.str + " " +
" ".join( [(("%g" % counts[ol]) if (ol in counts) else '--')
for ol in outcomeLabels] ) + '\n')
else: # use expanded label:count format
output.write(
gateString.str + " " +
" ".join( [("%s:%g" % (_outcome_to_str(ol),counts[ol]))
for ol in outcomeLabels if ol in counts] )+'\n')
def write_multidataset(filename, multidataset, gatestring_list=None, outcomeLabelOrder=None):
"""
Write a text-formatted multi-dataset file.
Parameters
----------
filename : string
The filename to write.
multidataset : MultiDataSet
The multi data set from which counts are obtained.
gatestring_list : list of GateStrings
The list of gate strings to include in the written dataset.
If None, all gate strings are output.
outcomeLabelOrder : list, optional
A list of the SPAM labels in multidataset which specifies
the column order in the output file.
"""
if gatestring_list is not None:
if len(gatestring_list) > 0 and not isinstance(gatestring_list[0], _objs.GateString):
raise ValueError("Argument gatestring_list must be a list of GateString objects!")
else:
gatestring_list = list(multidataset.gsIndex.keys()) #TODO: make access function for gatestrings?
if outcomeLabelOrder is not None: #convert to tuples if needed
outcomeLabelOrder = [ (ol,) if _tools.isstr(ol) else ol
for ol in outcomeLabelOrder ]
outcomeLabels = multidataset.get_outcome_labels()
if outcomeLabelOrder is not None:
assert(len(outcomeLabelOrder) == len(outcomeLabels))
assert(all( [ol in outcomeLabels for ol in outcomeLabelOrder] ))
assert(all( [ol in outcomeLabelOrder for ol in outcomeLabels] ))
outcomeLabels = outcomeLabelOrder
dsLabels = list(multidataset.keys())
headerString = ""
if hasattr(multidataset,'comment') and multidataset.comment is not None:
for commentLine in multidataset.comment.split('\n'):
if commentLine.startswith('#'):
headerString += commentLine + '\n'
else:
headerString += "# " + commentLine + '\n'
headerString += '## Columns = ' + ", ".join( [ "%s %s count" % (dsl,_outcome_to_str(ol))
for dsl in dsLabels
for ol in outcomeLabels ])
# parser = _stdinput.StdInputParser()
with open(filename, 'w') as output:
output.write(headerString + '\n')
for gateString in gatestring_list: #gateString should be a GateString object here
gs = gateString.tup #gatestring tuple
output.write(gateString.str + " " + " ".join( [("%g" % multidataset[dsl][gs].counts.get(ol,'--'))
for dsl in dsLabels for ol in outcomeLabels] ) + '\n')
def write_gatestring_list(filename, gatestring_list, header=None):
"""
Write a text-formatted gate string list file.
Parameters
----------
filename : string
The filename to write.
gatestring_list : list of GateStrings
The list of gate strings to include in the written dataset.
header : string, optional
Header line (first line of file). Prepended with a pound sign (#), so no
need to include one.
"""
if len(gatestring_list) > 0 and not isinstance(gatestring_list[0], _objs.GateString):
raise ValueError("Argument gatestring_list must be a list of GateString objects!")
with open(filename, 'w') as output:
if header is not None:
output.write("# %s" % header + '\n')
for gateString in gatestring_list:
output.write(gateString.str + '\n')
def write_gateset(gs,filename,title=None):
"""
Write a text-formatted gate set file.
Parameters
----------
gs : GateSet
The gate set to write to file.
filename : string
The filename to write.
title : string, optional
Header line (first line of file). Prepended with a pound sign (#), so no
need to include one.
"""
with open(filename, 'w') as output:
if title is not None:
output.write("# %s" % title + '\n')
output.write('\n')
for prepLabel,rhoVec in gs.preps.items():
if isinstance(rhoVec, _objs.FullyParameterizedSPAMVec): typ = "PREP"
elif isinstance(rhoVec, _objs.TPParameterizedSPAMVec): typ = "TP-PREP"
elif isinstance(rhoVec, _objs.StaticSPAMVec): typ = "STATIC-PREP"
else:
_warnings.warn(
("Non-standard prep of type {typ} cannot be described by"
"text format gate set files. It will be read in as a"
"fully parameterized spam vector").format(typ=str(type(rhoVec))))
typ = "PREP"
output.write("%s: %s\n" % (typ,prepLabel))
output.write("LiouvilleVec\n")
output.write(" ".join( "%.8g" % el for el in rhoVec ) + '\n')
output.write("\n")
for povmLabel,povm in gs.povms.items():
if isinstance(povm, _objs.UnconstrainedPOVM): povmType = "POVM"
elif isinstance(povm, _objs.TPPOVM): povmType = "TP-POVM"
else:
_warnings.warn(
("Non-standard POVM of type {typ} cannot be described by"
"text format gate set files. It will be read in as a"
"standard POVM").format(typ=str(type(povm))))
typ = "POVM"
output.write("%s: %s\n\n" % (povmType,povmLabel))
for ELabel,EVec in povm.items():
if isinstance(EVec, _objs.FullyParameterizedSPAMVec): typ = "EFFECT"
elif isinstance(EVec, _objs.ComplementSPAMVec): typ = "EFFECT" # ok
elif isinstance(EVec, _objs.TPParameterizedSPAMVec): typ = "TP-EFFECT"
elif isinstance(EVec, _objs.StaticSPAMVec): typ = "STATIC-EFFECT"
else:
_warnings.warn(
("Non-standard effect of type {typ} cannot be described by"
"text format gate set files. It will be read in as a"
"fully parameterized spam vector").format(typ=str(type(EVec))))
typ = "EFFECT"
output.write("%s: %s\n" % (typ,ELabel))
output.write("LiouvilleVec\n")
output.write(" ".join( "%.8g" % el for el in EVec ) + '\n')
output.write("\n")
output.write("END POVM\n\n")
for label,gate in gs.gates.items():
if isinstance(gate, _objs.FullyParameterizedGate): typ = "GATE"
elif isinstance(gate, _objs.TPParameterizedGate): typ = "TP-GATE"
elif isinstance(gate, _objs.LindbladParameterizedGate): typ = "CPTP-GATE"
elif isinstance(gate, _objs.StaticGate): typ = "STATIC-GATE"
else:
_warnings.warn(
("Non-standard gate of type {typ} cannot be described by"
"text format gate set files. It will be read in as a"
"fully parameterized gate").format(typ=str(type(gate))))
typ = "GATE"
output.write(typ + ": " + label + '\n')
output.write("LiouvilleMx\n")
output.write(_tools.mx_to_string(gate, width=16, prec=8) + '\n')
output.write("\n")
for instLabel,inst in gs.instruments.items():
if isinstance(inst, _objs.Instrument): typ = "Instrument"
elif isinstance(inst, _objs.TPInstrument): typ = "TP-Instrument"
else:
_warnings.warn(
("Non-standard Instrument of type {typ} cannot be described by"
"text format gate set files. It will be read in as a"
"standard Instrument").format(typ=str(type(inst))))
typ = "Instrument"
output.write(typ + ": " + instLabel + '\n\n')
for label,gate in inst.items():
if isinstance(gate, _objs.FullyParameterizedGate): typ = "IGATE"
elif isinstance(gate, _objs.TPInstrumentGate): typ = "IGATE" # ok b/c instrument itself is marked as TP
elif isinstance(gate, _objs.StaticGate): typ = "STATIC-IGATE"
else:
_warnings.warn(
("Non-standard gate of type {typ} cannot be described by"
"text format gate set files. It will be read in as a"
"fully parameterized gate").format(typ=str(type(gate))))
typ = "IGATE"
output.write(typ + ": " + label + '\n')
output.write("LiouvilleMx\n")
output.write(_tools.mx_to_string(gate, width=16, prec=8) + '\n')
output.write("\n")
output.write("END Instrument\n\n")
dims = gs.basis.dim.blockDims
if dims is None:
output.write("BASIS: %s\n" % gs.basis.name)
else:
if type(dims) != int:
dimStr = ",".join(map(str,dims))
else: dimStr = str(dims)
output.write("BASIS: %s %s\n" % (gs.basis.name, dimStr))
if isinstance(gs.default_gauge_group, _objs.FullGaugeGroup):
output.write("GAUGEGROUP: Full\n")
elif isinstance(gs.default_gauge_group, _objs.TPGaugeGroup):
output.write("GAUGEGROUP: TP\n")
elif isinstance(gs.default_gauge_group, _objs.UnitaryGaugeGroup):
output.write("GAUGEGROUP: Unitary\n")