/
stdinput.py
1280 lines (1091 loc) · 59.3 KB
/
stdinput.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Text-parsing classes and functions to read input files.
"""
#***************************************************************************************************
# Copyright 2015, 2019 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
# Under the terms of Contract DE-NA0003525 with NTESS, the U.S. Government retains certain rights
# in this software.
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0 or in the LICENSE file in the root pyGSTi directory.
#***************************************************************************************************
import ast as _ast
import os as _os
import re as _re
import sys as _sys
import time as _time
import warnings as _warnings
from collections import OrderedDict as _OrderedDict
import numpy as _np
from scipy.linalg import expm as _expm
from pygsti import baseobjs as _baseobjs
from pygsti import tools as _tools
from pygsti.modelmembers import instruments as _instrument
from pygsti.modelmembers import operations as _op
from pygsti.modelmembers import povms as _povm
from pygsti.modelmembers import states as _state
from pygsti.baseobjs import statespace as _statespace
from pygsti.models import gaugegroup as _gaugegroup
from pygsti.circuits.circuit import Circuit as _Circuit
from pygsti.circuits.circuitparser import CircuitParser as _CircuitParser
from pygsti.data import DataSet as _DataSet, MultiDataSet as _MultiDataSet
# A dictionary mapping qubit string representations into created
# :class:`Circuit` objects, which can improve performance by reducing
# or eliminating the need to parse circuit strings we've already parsed.
_global_parse_cache = {False: {}, True: {}} # key == create_subcircuits
def _create_display_progress_fn(show_progress):
"""
Create and return a progress-displaying function.
Only return a function that does somethign if `show_progress == True`
and the current environment is interactive. Otherwise, return a
do-nothing function.
Parameters
----------
show_progress : bool
Whether or not to even try to get a real progress-displaying function.
Returns
-------
function
"""
def _is_interactive():
import __main__ as main
return not hasattr(main, '__file__')
if _is_interactive() and show_progress:
try:
from IPython.display import clear_output
def _display_progress(i, n, filename):
_time.sleep(0.001); clear_output()
print("Reading %s: %.0f%%" % (filename, 100.0 * float(i) / float(n)))
_sys.stdout.flush()
except:
def _display_progress(i, n, f): pass
else:
def _display_progress(i, n, f): pass
return _display_progress
class StdInputParser(object):
"""
Encapsulates a text parser for reading GST input files.
"""
# Using a single parser. This speeds up parsing, however, it means the parser is NOT reentrant
_circuit_parser = _CircuitParser()
use_global_parse_cache = True
def __init__(self):
""" Create a new standard-input parser object """
pass
def parse_circuit(self, s, lookup={}, create_subcircuits=True):
"""
Parse a circuit from a string.
Parameters
----------
s : string
The string to parse.
lookup : dict, optional
A dictionary with keys == reflbls and values == tuples of operation labels
which can be used for substitutions using the S<reflbl> syntax.
create_subcircuits : bool, optional
Whether to create sub-circuit-labels when parsing
string representations or to just expand these into non-subcircuit
labels.
Returns
-------
Circuit
"""
circuit = None
if self.use_global_parse_cache:
circuit = _global_parse_cache[create_subcircuits].get(s, None)
if circuit is None: # wasn't in cache
layer_tuple, line_lbls, occurrence_id, compilable_indices = \
self.parse_circuit_raw(s, lookup, create_subcircuits)
if line_lbls is None: # if there are no line labels then we need to use "auto" and do a full init
circuit = _Circuit(layer_tuple, stringrep=s, line_labels="auto",
expand_subcircuits=False, check=False, occurrence=occurrence_id,
compilable_layer_indices=compilable_indices)
#Note: never expand subcircuits since parse_circuit_raw already does this w/create_subcircuits arg
else:
circuit = _Circuit._fastinit(layer_tuple, line_lbls, editable=False,
name='', stringrep=s, occurrence=occurrence_id,
compilable_layer_indices=compilable_indices)
if self.use_global_parse_cache:
_global_parse_cache[create_subcircuits][s] = circuit
return circuit
def parse_circuit_raw(self, s, lookup={}, create_subcircuits=True):
"""
Parse a circuit's constituent pieces from a string.
This doesn't actually create a circuit object, which may be desirable
in some scenarios.
Parameters
----------
s : string
The string to parse.
lookup : dict, optional
A dictionary with keys == reflbls and values == tuples of operation labels
which can be used for substitutions using the S<reflbl> syntax.
create_subcircuits : bool, optional
Whether to create sub-circuit-labels when parsing
string representations or to just expand these into non-subcircuit
labels.
Returns
-------
label_tuple: tuple
Tuple of operation labels representing the circuit's layers.
line_labels: tuple or None
A tuple or `None` giving the parsed line labels (follwing the '@' symbol) of the circuit.
occurrence_id: int or None
The "occurence id" - an integer following a second '@' symbol that identifies a particular
copy of this circuit.
compilable_indices : tuple or None
A tuple of layer indices (into `label_tuple`) marking the layers that can be "compiled",
and are *not* followed by a barrier so they can be compiled with following layers. This
is non-`None` only when there are explicit markers within the circuit string indicating
the presence or absence of barriers.
"""
self._circuit_parser.lookup = lookup
circuit_tuple, circuit_labels, occurrence_id, compilable_indices = \
self._circuit_parser.parse(s, create_subcircuits)
# print "DB: result = ",result
# print "DB: stack = ",self.exprStack
return circuit_tuple, circuit_labels, occurrence_id, compilable_indices
def parse_dataline(self, s, lookup={}, expected_counts=-1, create_subcircuits=True,
line_labels=None):
"""
Parse a data line (dataline in grammar)
Parameters
----------
s : string
The string to parse.
lookup : dict, optional
A dictionary with keys == reflbls and values == tuples of operation labels
which can be used for substitutions using the S<reflbl> syntax.
expected_counts : int, optional
The expected number of counts to accompany the circuit on this
data line. If < 0, no check is performed; otherwise raises ValueError
if the number of counts does not equal expected_counts.
create_subcircuits : bool, optional
Whether to create sub-circuit-labels when parsing string representations
or to just expand these into non-subcircuit labels.
Returns
-------
circuit : Circuit
The circuit.
counts : list
List of counts following the circuit.
"""
# get counts from end of s
parts = s.split()
circuitStr = parts[0]
counts = []
if expected_counts == -1: # then we expect to be given <outcomeLabel>:<count> items
if len(parts) == 1: # only a circuit, no counts on line
pass # just leave counts empty
elif parts[1] == "BAD":
counts.append("BAD")
else:
for p in parts[1:]:
t = p.split(':')
counts.append((tuple(t[0:-1]), float(t[-1])))
else: # data is in columns as given by header
for p in parts[1:]:
if p in ('--', 'BAD'):
counts.append(p)
else:
counts.append(float(p))
if len(counts) > expected_counts >= 0:
counts = counts[0:expected_counts]
nCounts = len(counts)
if nCounts != expected_counts:
raise ValueError("Found %d count columns when %d were expected" % (nCounts, expected_counts))
if nCounts == len(parts):
raise ValueError("No circuit column found -- all columns look like data")
circuit = self.parse_circuit(circuitStr, lookup, create_subcircuits)
return circuit, counts
def parse_dictline(self, s):
"""
Parse a circuit dictionary line (dictline in grammar)
Parameters
----------
s : string
The string to parse.
Returns
-------
circuitLabel : string
The user-defined label to represent this circuit.
circuitTuple : tuple
The circuit as a tuple of operation labels.
circuitStr : string
The circuit as represented as a string in the dictline.
circuitLineLabels : tuple
The line labels of the cirucit.
occurrence : object
Circuit's occurrence id, or `None` if there is none.
compilable_indices : tuple or None
A tuple of layer indices (into `label_tuple`) marking the layers that can be "compiled",
and are *not* followed by a barrier so they can be compiled with following layers. This
is non-`None` only when there are explicit markers within the circuit string indicating
the presence or absence of barriers.
"""
label = r'\s*([a-zA-Z0-9_]+)\s+'
match = _re.match(label, s)
if not match:
raise ValueError("'{}' is not a valid dictline".format(s))
circuitLabel = match.group(1)
circuitStr = s[match.end():]
circuitTuple, circuitLineLabels, occurrence_id, compilable_indices = self._circuit_parser.parse(circuitStr)
return circuitLabel, circuitTuple, circuitStr, circuitLineLabels, occurrence_id, compilable_indices
def parse_stringfile(self, filename, line_labels="auto", num_lines=None, create_subcircuits=True):
"""
Parse a circuit list file.
Parameters
----------
filename : string
The file to parse.
line_labels : iterable, optional
The (string valued) line labels used to initialize :class:`Circuit`
objects when line label information is absent from the one-line text
representation contained in `filename`. If `'auto'`, then line labels
are taken to be the list of all state-space labels present in the
circuit's layers. If there are no such labels then the special value
`'*'` is used as a single line label.
num_lines : int, optional
Specify this instead of `line_labels` to set the latter to the
integers between 0 and `num_lines-1`.
create_subcircuits : bool, optional
Whether to create sub-circuit-labels when parsing
string representations or to just expand these into non-subcircuit
labels.
Returns
-------
list of Circuits
The circuits read from the file.
"""
circuit_list = []
with open(filename, 'r') as stringfile:
for line in stringfile:
line = line.strip()
if len(line) == 0 or line[0] == '#': continue
if line_labels == "auto":
# can be cached, and cache assumes "auto" behavior
circuit = self.parse_circuit(line, {}, create_subcircuits)
else:
layer_lbls, parsed_line_lbls, occurrence_id, compilable_indices = \
self.parse_circuit_raw(line, {}, create_subcircuits)
if parsed_line_lbls is None:
parsed_line_lbls = line_labels # default to the passed-in argument
#nlines = num_lines
#else: nlines = None # b/c we've got a valid line_lbls
circuit = _Circuit._fastinit(layer_lbls, parsed_line_lbls, editable=False,
name='', stringrep=line.strip(), occurrence=occurrence_id,
compilable_layer_indices=compilable_indices)
#circuit = _Circuit(layer_lbls, stringrep=line.strip(),
# line_labels=parsed_line_lbls, num_lines=nlines,
# expand_subcircuits=False, check=False, occurrence=occurrence_id)
##Note: never expand subcircuits since parse_circuit_raw already does this w/create_subcircuits arg
circuit_list.append(circuit)
return circuit_list
def parse_dictfile(self, filename):
"""
Parse a circuit dictionary file.
Parameters
----------
filename : string
The file to parse.
Returns
-------
dict
Dictionary with keys == circuit labels and values == Circuits.
"""
lookupDict = {}
with open(filename, 'r') as dictfile:
for line in dictfile:
line = line.strip()
if len(line) == 0 or line[0] == '#': continue
label, tup, s, lineLbls, occurrence_id, compilable_indices = self.parse_dictline(line)
if lineLbls is None: lineLbls = "auto"
lookupDict[label] = _Circuit(tup, stringrep=s, line_labels=lineLbls,
check=False, occurrence=occurrence_id,
compilable_layer_indices=compilable_indices)
return lookupDict
def parse_datafile(self, filename, show_progress=True,
collision_action="aggregate", record_zero_counts=True,
ignore_zero_count_lines=True, with_times="auto"):
"""
Parse a data set file into a DataSet object.
Parameters
----------
filename : string
The file to parse.
show_progress : bool, optional
Whether or not progress should be displayed
collision_action : {"aggregate", "keepseparate"}
Specifies how duplicate circuits should be handled. "aggregate"
adds duplicate-circuit counts, whereas "keepseparate" tags duplicate
circuits by setting their `.occurrence` IDs to sequential positive integers.
record_zero_counts : bool, optional
Whether zero-counts are actually recorded (stored) in the returned
DataSet. If False, then zero counts are ignored, except for potentially
registering new outcome labels.
ignore_zero_count_lines : bool, optional
Whether circuits for which there are no counts should be ignored
(i.e. omitted from the DataSet) or not.
with_times : bool or "auto", optional
Whether to the time-stamped data format should be read in. If
"auto", then this format is allowed but not required. Typically
you only need to set this to False when reading in a template file.
Returns
-------
DataSet
A static DataSet object.
"""
#Parse preamble -- lines beginning with # or ## until first non-# line
preamble_directives = {}
preamble_comments = []
with open(filename, 'r') as datafile:
for line in datafile:
line = line.strip()
if len(line) == 0 or line[0] != '#': break
if line.startswith("## "):
parts = line[len("## "):].split("=")
if len(parts) == 2: # key = value
preamble_directives[parts[0].strip()] = parts[1].strip()
elif line.startswith("#"):
preamble_comments.append(line[1:].strip())
def str_to_outcome(x): # always return a tuple as the "outcome label" (even if length 1)
return tuple(x.strip().split(":"))
#Process premble
orig_cwd = _os.getcwd()
outcomeLabels = None
outcome_labels_specified_in_preamble = False
if len(_os.path.dirname(filename)) > 0: _os.chdir(
_os.path.dirname(filename)) # allow paths relative to datafile path
try:
if 'Lookup' in preamble_directives:
lookupDict = self.parse_dictfile(preamble_directives['Lookup'])
else: lookupDict = {}
if 'Columns' in preamble_directives:
colLabels = [l.strip() for l in preamble_directives['Columns'].split(",")]
#OLD: outcomeLabels, fillInfo = self._extract_labels_from_col_labels(colLabels)
fixed_column_outcome_labels = []
for i, colLabel in enumerate(colLabels):
assert(colLabel.endswith(' count')), \
"Invalid count column name `%s`! (Only *count* columns are supported now)" % colLabel
outcomeLabel = str_to_outcome(colLabel[:-len(' count')])
if outcomeLabel not in fixed_column_outcome_labels:
fixed_column_outcome_labels.append(outcomeLabel)
nDataCols = len(colLabels)
else:
fixed_column_outcome_labels = None
nDataCols = -1 # no column count check
if 'Outcomes' in preamble_directives:
outcomeLabels = [l.strip().split(':') for l in preamble_directives['Outcomes'].split(",")]
outcome_labels_specified_in_preamble = True
if 'StdOutcomeQubits' in preamble_directives:
outcomeLabels = int(preamble_directives['Outcomes'])
outcome_labels_specified_in_preamble = True
finally:
_os.chdir(orig_cwd)
#Read data lines of data file
dataset = _DataSet(outcome_labels=outcomeLabels, collision_action=collision_action,
comment="\n".join(preamble_comments))
if outcome_labels_specified_in_preamble and (fixed_column_outcome_labels is not None):
fixed_column_outcome_indices = [dataset.olIndex[ol] for ol in fixed_column_outcome_labels]
else:
fixed_column_outcome_indices = None
nLines = 0
with open(filename, 'r') as datafile:
nLines = sum(1 for line in datafile)
nSkip = int(nLines / 100.0)
if nSkip == 0: nSkip = 1
display_progress = _create_display_progress_fn(show_progress)
warnings = [] # to display *after* display progress
looking_for = "circuit_line"; current_item = {}
def parse_comment(comment, filename, i_line):
commentDict = {}
comment = comment.strip()
if len(comment) == 0: return {}
try:
if comment.startswith("{") and comment.endswith("}"):
commentDict = _ast.literal_eval(comment)
else: # put brackets around it
commentDict = _ast.literal_eval("{ " + comment + " }")
#commentDict = _json.loads("{ " + comment + " }")
#Alt: safer(?) & faster, but need quotes around all keys & vals
except:
commentDict = {}
warnings.append("%s Line %d: Could not parse comment '%s'"
% (filename, i_line, comment))
return commentDict
last_circuit = last_commentDict = None
#REMOVE DEBUG
#from mpi4py import MPI
#comm = MPI.COMM_WORLD
#debug_circuit_elements = 0; debug_test_simple_dict = {}; circuit_bytes = 0; sizeof_bytes = 0
with open(filename, 'r') as inputfile:
for (iLine, line) in enumerate(inputfile):
if iLine % nSkip == 0 or iLine + 1 == nLines: display_progress(iLine + 1, nLines, filename)
line = line.strip()
if '#' in line:
i = line.index('#')
dataline, comment = line[:i], line[i + 1:]
else:
dataline, comment = line, ""
if looking_for == "circuit_data_or_line":
# Special confusing case: lines that just have a circuit could be either the beginning of a
# long-format (with times, reps, etc, lines) block OR could just be a circuit that doesn't have
# any count data. This case figures out which one based on the line that follows.
if len(dataline) == 0 or dataline.split()[0] in ('times:', 'outcomes:', 'repetitions:', 'aux:'):
looking_for = "circuit_data" # blank lines shoudl process acumulated data
else:
# previous blank line was just a circuit without any data (*not* the beginning of a timestamped
# section), so add it with zero counts (if we don't ignore it), and look for next circuit.
looking_for = "circuit_line"
if ignore_zero_count_lines is False and last_circuit is not None:
dataset.add_count_list(last_circuit, [], [], aux=last_commentDict,
record_zero_counts=record_zero_counts, update_ol=False, unsafe=True)
if looking_for == "circuit_line":
if len(dataline) == 0: continue
try:
circuit, valueList = \
self.parse_dataline(dataline, lookupDict, nDataCols,
create_subcircuits=not _Circuit.default_expand_subcircuits)
commentDict = parse_comment(comment, filename, iLine)
except ValueError as e:
raise ValueError("%s Line %d: %s" % (filename, iLine, str(e)))
if with_times is True and len(valueList) > 0:
raise ValueError(("%s Line %d: Circuit line cannot contain count information when "
"'with_times=True'") % (filename, iLine))
if with_times is False or len(valueList) > 0:
if 'BAD' in valueList: # entire line is known to be BAD => no data for this circuit
oliArray = _np.zeros(0, dataset.oliType)
countArray = _np.zeros(0, dataset.repType)
count_values = []
else:
if fixed_column_outcome_labels is not None:
if outcome_labels_specified_in_preamble:
outcome_indices, count_values = \
zip(*[(oli, v) for (oli, v) in zip(fixed_column_outcome_indices, valueList)
if v != '--']) # drop "empty" sentinels
else:
outcome_labels, count_values = \
zip(*[(nm, v) for (nm, v) in zip(fixed_column_outcome_labels, valueList)
if v != '--']) # drop "empty" sentinels
dataset.add_outcome_labels(outcome_labels, update_ol=False)
outcome_indices = [dataset.olIndex[ol] for ol in outcome_labels]
else: # assume valueList is a list of (outcomeLabel, count) tuples -- see parse_dataline
outcome_labels, count_values = zip(*valueList) if len(valueList) else ([], [])
if not outcome_labels_specified_in_preamble:
dataset.add_outcome_labels(outcome_labels, update_ol=False)
outcome_indices = [dataset.olIndex[ol] for ol in outcome_labels]
oliArray = _np.array(outcome_indices, dataset.oliType)
countArray = _np.array(count_values, dataset.repType)
if all([(abs(v) < 1e-9) for v in count_values]):
if ignore_zero_count_lines is True:
if not ('BAD' in valueList): # supress "no data" warning for known-bad circuits
s = circuit.str if len(circuit.str) < 40 else circuit.str[0:37] + "..."
warnings.append("Dataline for circuit '%s' has zero counts and will be ignored" % s)
continue # skip lines in dataset file with zero counts (no experiments done)
else:
#if not bBad:
# s = circuitStr if len(circuitStr) < 40 else circuitStr[0:37] + "..."
# warnings.append("Dataline for circuit '%s' has zero counts." % s)
# don't make a fuss if we don't ignore the lines (needed for
# fill_in_empty_dataset_with_fake_data).
pass
#Call this low-level function for performance, so need to construct outcome *index* arrays above
dataset.add_count_arrays(circuit, oliArray, countArray,
record_zero_counts=record_zero_counts, aux=commentDict)
else:
current_item.clear()
current_item['circuit'] = circuit
current_item['aux'] = commentDict
last_circuit, last_commentDict = circuit, commentDict # for circuit_data_or_line processing
looking_for = "circuit_data" if (with_times is True) else "circuit_data_or_line"
elif looking_for == "circuit_data":
if len(line) == 0:
#add current item & look for next one
# Note: if last line was just a circuit (without any following data lines)
# then current_item will only have 'circuit' & 'aux' keys, so we need to use .get(...) below
dataset.add_raw_series_data(current_item['circuit'], current_item.get('outcomes', []),
current_item.get('times', []),
current_item.get('repetitions', None),
record_zero_counts=record_zero_counts,
aux=current_item.get('aux', None),
update_ol=False) # for performance - to this once at the end.
current_item.clear()
looking_for = "circuit_line"
else:
parts = dataline.split()
if parts[0] == 'times:':
current_item['times'] = [float(x) for x in parts[1:]]
elif parts[0] == 'outcomes:':
current_item['outcomes'] = parts[1:] # no conversion needed
elif parts[0] == 'repetitions:':
try:
current_item['repetitions'] = [int(x) for x in parts[1:]]
except ValueError: # raised if int(x) fails b/c reps are floats
current_item['repetitions'] = [float(x) for x in parts[1:]]
elif parts[0] == 'aux:':
current_item['aux'] = parse_comment(" ".join(parts[1:]), filename, iLine)
else:
raise ValueError("Invalid circuit data-line prefix: '%s'" % parts[0])
#REMOVE print("Rank %d DONE load loop. circuit bytes = %g" % (comm.rank, circuit_bytes))
if looking_for in ("circuit_data", "circuit_data_or_line") and current_item:
#add final circuit info (no blank line at end of file)
dataset.add_raw_series_data(current_item['circuit'], current_item.get('outcomes', []),
current_item.get('times', []), current_item.get('repetitions', None),
record_zero_counts=record_zero_counts, aux=current_item.get('aux', None),
update_ol=False) # for performance - to this once at the end.
dataset.update_ol() # because we set update_ol=False above, we need to do this
if warnings:
_warnings.warn('\n'.join(warnings)) # to be displayed at end, after potential progress updates
dataset.done_adding_data()
return dataset
def parse_multidatafile(self, filename, show_progress=True,
collision_action="aggregate", record_zero_counts=True, ignore_zero_count_lines=True):
"""
Parse a multiple data set file into a MultiDataSet object.
Parameters
----------
filename : string
The file to parse.
show_progress : bool, optional
Whether or not progress should be displayed
collision_action : {"aggregate", "keepseparate"}
Specifies how duplicate circuits should be handled. "aggregate"
adds duplicate-circuit counts, whereas "keepseparate" tags duplicate
circuits by setting their `.occurrence` IDs to sequential positive integers.
record_zero_counts : bool, optional
Whether zero-counts are actually recorded (stored) in the returned
MultiDataSet. If False, then zero counts are ignored, except for
potentially registering new outcome labels.
ignore_zero_count_lines : bool, optional
Whether circuits for which there are no counts should be ignored
(i.e. omitted from the MultiDataSet) or not.
Returns
-------
MultiDataSet
A MultiDataSet object.
"""
#Parse preamble -- lines beginning with # or ## until first non-# line
preamble_directives = {}
preamble_comments = []
with open(filename, 'r') as multidatafile:
for line in multidatafile:
line = line.strip()
if len(line) == 0 or line[0] != '#': break
if line.startswith("## "):
parts = line[len("## "):].split("=")
if len(parts) == 2: # key = value
preamble_directives[parts[0].strip()] = parts[1].strip()
elif line.startswith("#"):
preamble_comments.append(line[1:].strip())
#Process premble
orig_cwd = _os.getcwd()
if len(_os.path.dirname(filename)) > 0:
_os.chdir(_os.path.dirname(filename)) # allow paths relative to datafile path
try:
if 'Lookup' in preamble_directives:
lookupDict = self.parse_dictfile(preamble_directives['Lookup'])
else: lookupDict = {}
if 'Columns' in preamble_directives:
colLabels = [l.strip() for l in preamble_directives['Columns'].split(",")]
else: colLabels = ['dataset1 1 count', 'dataset1 count total']
dsOutcomeLabels, fillInfo = self._extract_labels_from_multi_data_col_labels(colLabels)
nDataCols = len(colLabels)
finally:
_os.chdir(orig_cwd)
#Read data lines of data file
datasets = _OrderedDict()
for dsLabel, outcomeLabels in dsOutcomeLabels.items():
datasets[dsLabel] = _DataSet(outcome_labels=outcomeLabels,
collision_action=collision_action)
dsCountDicts = _OrderedDict()
for dsLabel in dsOutcomeLabels: dsCountDicts[dsLabel] = {}
nLines = 0
with open(filename, 'r') as datafile:
nLines = sum(1 for line in datafile)
nSkip = max(int(nLines / 100.0), 1)
display_progress = _create_display_progress_fn(show_progress)
warnings = [] # to display *after* display progress
mds = _MultiDataSet(comment="\n".join(preamble_comments))
with open(filename, 'r') as inputfile:
for (iLine, line) in enumerate(inputfile):
if iLine % nSkip == 0 or iLine + 1 == nLines: display_progress(iLine + 1, nLines, filename)
line = line.strip()
if '#' in line:
i = line.index('#')
dataline, comment = line[:i], line[i + 1:]
else:
dataline, comment = line, ""
if len(dataline) == 0: continue
try:
circuit, valueList = \
self.parse_dataline(dataline, lookupDict, nDataCols,
create_subcircuits=not _Circuit.default_expand_subcircuits)
commentDict = {}
comment = comment.strip()
if len(comment) > 0:
try:
if comment.startswith("{") and comment.endswith("}"):
commentDict = _ast.literal_eval(comment)
else: # put brackets around it
commentDict = _ast.literal_eval("{ " + comment + " }")
except:
warnings.append("%s Line %d: Could not parse comment '%s'"
% (filename, iLine, comment))
except ValueError as e:
raise ValueError("%s Line %d: %s" % (filename, iLine, str(e)))
bBad = ('BAD' in valueList) # supresses warnings
for count_dict in dsCountDicts.values(): count_dict.clear() # reset before filling
self._fill_multi_data_count_dicts(dsCountDicts, fillInfo, valueList)
bSkip = False
if all([(abs(v) < 1e-9) for cDict in dsCountDicts.values() for v in cDict.values()]):
if ignore_zero_count_lines:
if not bBad:
s = circuit.str if len(circuit.str) < 40 else circuit.str[0:37] + "..."
warnings.append("Dataline for circuit '%s' has zero counts and will be ignored" % s)
bSkip = True # skip lines in dataset file with zero counts (no experiments done)
else:
if not bBad:
s = circuit.str if len(circuit.str) < 40 else circuit.str[0:37] + "..."
warnings.append("Dataline for circuit '%s' has zero counts." % s)
if not bSkip:
for dsLabel, countDict in dsCountDicts.items():
datasets[dsLabel].add_count_dict(
circuit, countDict, record_zero_counts=record_zero_counts, update_ol=False)
mds.add_auxiliary_info(circuit, commentDict)
for dsLabel, ds in datasets.items():
ds.update_ol() # because we set update_ol=False above, we need to do this
ds.done_adding_data()
# auxinfo already added, and ds shouldn't have any anyway
mds.add_dataset(dsLabel, ds, update_auxinfo=False)
return mds
#Note: outcome labels must not contain spaces since we use spaces to separate
# the outcome label from the dataset label
def _extract_labels_from_multi_data_col_labels(self, col_labels):
def str_to_outcome(x): # always return a tuple as the "outcome label" (even if length 1)
return tuple(x.strip().split(":"))
dsOutcomeLabels = _OrderedDict()
countCols = []; freqCols = []; impliedCounts1Q = []
for i, colLabel in enumerate(col_labels):
wordsInColLabel = colLabel.split() # split on whitespace into words
if len(wordsInColLabel) < 3: continue # allow other columns we don't recognize
if wordsInColLabel[-1] == 'count':
if len(wordsInColLabel) > 3:
_warnings.warn("Column label '%s' has more words than were expected (3)" % colLabel)
outcomeLabel = str_to_outcome(wordsInColLabel[-2])
dsLabel = wordsInColLabel[-3]
if dsLabel not in dsOutcomeLabels:
dsOutcomeLabels[dsLabel] = [outcomeLabel]
else: dsOutcomeLabels[dsLabel].append(outcomeLabel)
countCols.append((dsLabel, outcomeLabel, i))
elif wordsInColLabel[-1] == 'frequency':
if len(wordsInColLabel) > 3:
_warnings.warn("Column label '%s' has more words than were expected (3)" % colLabel)
outcomeLabel = str_to_outcome(wordsInColLabel[-2])
dsLabel = wordsInColLabel[-3]
if '%s count total' % dsLabel not in col_labels:
raise ValueError("Frequency columns specified without"
"count total for dataset '%s'" % dsLabel)
else: iTotal = col_labels.index('%s count total' % dsLabel)
if dsLabel not in dsOutcomeLabels:
dsOutcomeLabels[dsLabel] = [outcomeLabel]
else: dsOutcomeLabels[dsLabel].append(outcomeLabel)
freqCols.append((dsLabel, outcomeLabel, i, iTotal))
for dsLabel, outcomeLabels in dsOutcomeLabels.items():
if '%s count total' % dsLabel in col_labels:
if ('1',) in outcomeLabels and ('0',) not in outcomeLabels:
dsOutcomeLabels[dsLabel].append(('0',))
iTotal = col_labels.index('%s count total' % dsLabel)
impliedCounts1Q.append((dsLabel, ('0',), iTotal))
if ('0',) in outcomeLabels and ('1',) not in outcomeLabels:
dsOutcomeLabels[dsLabel].append(('1',))
iTotal = col_labels.index('%s count total' % dsLabel)
impliedCounts1Q.append((dsLabel, ('1',), iTotal))
#TODO - add standard count completion for 2Qubit case?
fillInfo = (countCols, freqCols, impliedCounts1Q)
return dsOutcomeLabels, fillInfo
def _fill_multi_data_count_dicts(self, count_dicts, fill_info, col_values):
countCols, freqCols, impliedCounts1Q = fill_info
for dsLabel, outcomeLabel, iCol in countCols:
if col_values[iCol] == '--':
continue
if col_values[iCol] > 0 and col_values[iCol] < 1:
raise ValueError("Count column (%d) contains value(s) between 0 and 1 - "
"could this be a frequency?" % iCol)
count_dicts[dsLabel][outcomeLabel] = col_values[iCol]
for dsLabel, outcomeLabel, iCol, iTotCol in freqCols:
if col_values[iCol] == '--':
continue
if col_values[iCol] < 0 or col_values[iCol] > 1.0:
raise ValueError("Frequency column (%d) contains value(s) outside of [0,1.0] interval - "
"could this be a count?" % iCol)
count_dicts[dsLabel][outcomeLabel] = col_values[iCol] * col_values[iTotCol]
for dsLabel, outcomeLabel, iTotCol in impliedCounts1Q:
if col_values[iTotCol] == '--': raise ValueError("Mising total (== '--')!")
if outcomeLabel == '0':
count_dicts[dsLabel]['0'] = col_values[iTotCol] - count_dicts[dsLabel]['1']
elif outcomeLabel == '1':
count_dicts[dsLabel]['1'] = col_values[iTotCol] - count_dicts[dsLabel]['0']
#TODO - add standard count completion for 2Qubit case?
return count_dicts
def parse_tddatafile(self, filename, show_progress=True, record_zero_counts=True,
create_subcircuits=True):
"""
Parse a timstamped data set file into a DataSet object.
Parameters
----------
filename : string
The file to parse.
show_progress : bool, optional
Whether or not progress should be displayed
record_zero_counts : bool, optional
Whether zero-counts are actually recorded (stored) in the returned
DataSet. If False, then zero counts are ignored, except for
potentially registering new outcome labels.
create_subcircuits : bool, optional
Whether to create sub-circuit-labels when parsing
string representations or to just expand these into non-subcircuit
labels.
Returns
-------
DataSet
A static DataSet object.
"""
#Parse preamble -- lines beginning with # or ## until first non-# line
preamble_directives = _OrderedDict()
with open(filename, 'r') as f:
for line in f:
line = line.strip()
if len(line) == 0 or line[0] != '#': break
if line.startswith("## "):
parts = line[len("## "):].split("=")
if len(parts) == 2: # key = value
preamble_directives[parts[0].strip()] = parts[1].strip()
#Process premble
orig_cwd = _os.getcwd()
if len(_os.path.dirname(filename)) > 0: _os.chdir(
_os.path.dirname(filename)) # allow paths relative to datafile path
try:
if 'Lookup' in preamble_directives:
lookupDict = self.parse_dictfile(preamble_directives['Lookup'])
else: lookupDict = {}
finally:
_os.chdir(orig_cwd)
outcomeLabelAbbrevs = _OrderedDict()
for key, val in preamble_directives.items():
if key == "Lookup": continue
outcomeLabelAbbrevs[key] = val
outcomeLabels = outcomeLabelAbbrevs.values()
#Read data lines of data file
dataset = _DataSet(outcome_labels=outcomeLabels)
with open(filename, 'r') as f:
nLines = sum(1 for line in f)
nSkip = int(nLines / 100.0)
if nSkip == 0: nSkip = 1
display_progress = _create_display_progress_fn(show_progress)
with open(filename, 'r') as f:
for (iLine, line) in enumerate(f):
if iLine % nSkip == 0 or iLine + 1 == nLines: display_progress(iLine + 1, nLines, filename)
line = line.strip()
if len(line) == 0 or line[0] == '#': continue
try:
parts = line.split()
lastpart = parts[-1]
circuitStr = line[:-len(lastpart)].strip()
circuit = self.parse_circuit(circuitStr, lookupDict, create_subcircuits)
timeSeriesStr = lastpart.strip()
except ValueError as e:
raise ValueError("%s Line %d: %s" % (filename, iLine, str(e)))
seriesList = [outcomeLabelAbbrevs[abbrev] for abbrev in timeSeriesStr] # iter over characters in str
timesList = list(range(len(seriesList))) # FUTURE: specify an offset and step??
dataset.add_raw_series_data(circuit, seriesList, timesList,
record_zero_counts=record_zero_counts)
dataset.done_adding_data()
return dataset
def _eval_element(el, b_complex):
myLocal = {'pi': _np.pi, 'sqrt': _np.sqrt}
exec("element = %s" % el, {"__builtins__": None}, myLocal)
return complex(myLocal['element']) if b_complex else float(myLocal['element'])
def _eval_row_list(rows, b_complex):
return _np.array([[_eval_element(x, b_complex) for x in r] for r in rows],
'complex' if b_complex else 'd')
def parse_model(filename):
"""
Parse a model file into a Model object.
Parameters
----------
filename : string
The file to parse.
Returns
-------
Model
"""
from ..models import ExplicitOpModel as _ExplicitOpModel
basis = 'pp' # default basis to load as
basis_abbrev = "pp" # default assumed basis
basis_dim = None
gaugegroup_name = None
state_space = None
#First try to find basis:
with open(filename) as inputfile:
for line in inputfile:
line = line.strip()
if line.startswith("BASIS:"):
parts = line[len("BASIS:"):].split()
basis_abbrev = parts[0]
if len(parts) > 1:
basis_dims = list(map(int, "".join(parts[1:]).split(",")))
assert(len(basis_dims) == 1), "Multiple basis dims is no longer supported!"
basis_dim = basis_dims[0]
else:
basis_dim = None
elif line.startswith("GAUGEGROUP:"):
gaugegroup_name = line[len("GAUGEGROUP:"):].strip()
if gaugegroup_name not in ("Full", "TP", "Unitary"):
_warnings.warn(("Unknown GAUGEGROUP name %s. Default gauge"
"group will be set to None") % gaugegroup_name)
elif line.startswith("STATESPACE:"):
tpbs_lbls = []; tpbs_udims = []
tensor_prod_blk_strs = line[len("STATESPACE:"):].split("+")
for tpb_str in tensor_prod_blk_strs:
tpb_lbls = []; tpb_udims = []
for lbl_and_dim in tpb_str.split("*"):