/
test_stdinput.py
658 lines (544 loc) · 18.5 KB
/
test_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
import functools
import numpy as np
from pathlib import Path
from . import IOBase, with_temp_path, with_temp_file
from pygsti.modelpacks.legacy import std1Q_XYI as std
from pygsti.objects import Circuit, CircuitLabel
import pygsti.construction as pc
from pygsti import io
import pygsti.io.stdinput as stdin
class StdInputBase:
def setUp(self):
self.std = stdin.StdInputParser()
class ParserTester(StdInputBase, IOBase):
def test_parse_circuit(self):
lkup = {'1': ('G1',),
'2': ('G1', 'G2'),
'3': ('G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7', 'G8', 'G9', 'G10'),
'G12': ('G1', 'G2'),
'S23': ('G2', 'G3')}
string_tests = [("{}", ()),
("{}^127", ()),
("{}^0002", ()),
("G1", ('G1',)),
("G1G2G3", ('G1', 'G2', 'G3')),
("G1(G2)G3", ('G1', 'G2', 'G3')),
("G1(G2)^3G3", ('G1', 'G2', 'G2', 'G2', 'G3')),
("G1(G2G3)^2", ('G1', 'G2', 'G3', 'G2', 'G3')),
("G1*G2*G3", ('G1', 'G2', 'G3')),
("G1^02", ('G1', 'G1')),
("G1*((G2G3)^2G4G5)^2G7", ('G1', 'G2', 'G3', 'G2', 'G3',
'G4', 'G5', 'G2', 'G3', 'G2', 'G3', 'G4', 'G5', 'G7')),
("G1(G2^2(G3G4)^2)^2", ('G1', 'G2', 'G2', 'G3', 'G4', 'G3', 'G4', 'G2', 'G2', 'G3', 'G4', 'G3', 'G4')),
("G1*G2", ('G1', 'G2')),
#("S<1>", ('G1',)),
#("S<2>", ('G1', 'G2')),
#("G1S<2>^2G3", ('G1', 'G1', 'G2', 'G1', 'G2', 'G3')),
#("G1S<1>G3", ('G1', 'G1', 'G3')),
#("S<3>[0:4]", ('G1', 'G2', 'G3', 'G4')),
("G_my_xG_my_y", ('G_my_x', 'G_my_y')),
("G_my_x*G_my_y", ('G_my_x', 'G_my_y')),
("G_my_x*G_my_y", ('G_my_x', 'G_my_y')),
("GsG___", ('Gs', 'G___')),
#("S < 2 >G3", ('G1', 'G2', 'G3')),
#("S<G12>", ('G1', 'G2')),
#("S<S23>", ('G2', 'G3')),
("G1G2", ('G1', 'G2')),
("rho0*Gx", ('rho0', 'Gx')),
("rho0*Gx*Mdefault", ('rho0', 'Gx', 'Mdefault'))]
for s, expected in string_tests:
result, line_labels = self.std.parse_circuit(s, lookup=lkup)
self.assertEqual(line_labels, None)
circuit_result = Circuit(result, line_labels="auto", expand_subcircuits=True)
#use "auto" line labels since none are parsed.
self.assertEqual(circuit_result.tup, expected)
def test_parse_circuit_raises_on_syntax_error(self):
with self.assertRaises(ValueError):
self.std.parse_circuit("FooBar")
with self.assertRaises(ValueError):
self.std.parse_circuit("G1G2^2^2")
with self.assertRaises(ValueError):
self.std.parse_circuit("(G1")
with self.assertRaises(ValueError):
self.std.parse_circuit("G1*S[test]")
with self.assertRaises(ValueError):
self.std.parse_circuit("G1*SS")
def test_parse_dataline(self):
dataline_tests = ["G1G2G3 0.1 100",
"G1*G2*G3 0.798 100",
"G1*(G2*G3)^2*G4 1.0 100"]
self.assertEqual(
self.std.parse_dataline(dataline_tests[0], expected_counts=2),
(['G1', 'G2', 'G3'], 'G1G2G3', None, [0.1, 100.0])
)
self.assertEqual(
self.std.parse_dataline(dataline_tests[1], expected_counts=2),
(['G1', 'G2', 'G3'], 'G1*G2*G3', None, [0.798, 100.0])
)
self.assertEqual(
self.std.parse_dataline(dataline_tests[2], expected_counts=2),
(['G1', CircuitLabel('', ('G2', 'G3'), None, 2), 'G4'], 'G1*(G2*G3)^2*G4', None, [1.0, 100.0])
)
self.assertEqual(
self.std.parse_dataline("G1G2G3 0.1 100 2.0", expected_counts=2),
(['G1', 'G2', 'G3'], 'G1G2G3', None, [0.1, 100.0])
) # extra col ignored
def test_parse_dataline_raises_on_syntax_error(self):
with self.assertRaises(ValueError):
self.std.parse_dataline("G1G2G3 1.0", expected_counts=2) # too few cols == error
with self.assertRaises(ValueError):
self.std.parse_dataline("1.0 2.0") # just data cols (no circuit col!)
def test_parse_dictline(self):
dictline_tests = ["1 G1G2G3",
"MyFav (G1G2)^3"]
self.assertEqual(
self.std.parse_dictline(dictline_tests[0]),
('1', ['G1', 'G2', 'G3'], 'G1G2G3', None)
)
self.assertEqual(
self.std.parse_dictline(dictline_tests[1]),
('MyFav', [CircuitLabel('', ('G1', 'G2'), None, 3)], '(G1G2)^3', None)
)
class FileInputTester(StdInputBase, IOBase):
def _write_dictfile(self, file_path):
"""Helper used for dict tests"""
contents = """#My Dictionary file
# You can't use lookups within this file.
1 G1
2 G1G2
3 G1G2G3G4G5G6
MyFav1 G1G1G1
MyFav2 G2^3
this1 G3*G3*G3
thatOne G1G2*G3
"""
with open(file_path, 'w') as f:
f.write(contents)
@with_temp_path
def test_parse_dictfile(self, tmp_path):
self._write_dictfile(tmp_path)
lkupDict = self.std.parse_dictfile(tmp_path)
# TODO assert correctness
@with_temp_path
def test_parse_datafile(self, tmp_path):
# write lookup dict
dict_path = str(Path(tmp_path).parent / "sip_test.dict")
self._write_dictfile(dict_path)
contents = """#My Data file
#Get string lookup data from the file test.dict
## Lookup = {dict_path}
## Columns = 0 frequency, count total
# OLD Columns = 0 count, 1 count
#empty string
{{}} 1.0 100
#simple sequences
G1G2 0.098 100
G2G3 0.2 100
(G1)^4 0.1 1000
#using lookups
#G1 S<1> 0.9999 100
#S<MyFav1>G2 0.23 100
#G1S<2>^2 0.5 20
#S<3>[0:4] 0.2 5
#G1G2G3G4 0.2 5
#different ways to concatenate gates
G_my_xG_my_y 0.5 24.0
G_my_x*G_my_y 0.5 24.0
G_my_xG_my_y 0.5 24.0
""".format(dict_path=dict_path)
with open(tmp_path, 'w') as f:
f.write(contents)
ds = self.std.parse_datafile(tmp_path)
# TODO assert correctness
@with_temp_path
def test_parse_multidatafile(self, tmp_path):
# write lookup dict
dict_path = str(Path(tmp_path).parent / "sip_test.dict")
self._write_dictfile(dict_path)
contents = """#Multi Data File
## Lookup = {dict_path}
## Columns = ds1 0 count, ds1 count total, ds2 0 count, ds2 count total
{{}} 30 100 20 200
G1 10 100 10 200
G2 20 100 5 200
G3 10 100 80 200
""".format(dict_path=dict_path)
with open(tmp_path, 'w') as f:
f.write(contents)
mds = self.std.parse_multidatafile(tmp_path)
# TODO assert correctness
@with_temp_file("""#My string file
G1
G1G2
G1(G2G3)^2
""")
def test_parse_stringfile(self, tmp_path):
strlist = self.std.parse_stringfile(tmp_path)
# TODO assert correctness
@with_temp_file("""#Data File without Header
{} 1.0 100
""")
def test_parse_datafile_no_header(self, tmp_path):
ds = self.std.parse_datafile(tmp_path)
# TODO assert correctness
@with_temp_file("""#Data File with bad syntax
## Columns = 0 frequency, count total
{} 1.0 100
G1 0.0 100
FooBar 0.4 100
G3 0.2 100
""")
def test_parse_datafile_raises_on_bad_data(self, tmp_path):
with self.assertRaises(ValueError):
self.std.parse_datafile(tmp_path)
@with_temp_file("""#Data File with bad syntax
## Columns = 0 count, count total
{xx} 10 100
""")
def test_parse_datafile_raises_on_syntax_error(self, tmp_path):
with self.assertRaises(ValueError):
self.std.parse_datafile(tmp_path)
@with_temp_file("""#Data File with zero counts
## Columns = 0 frequency, count total
{} 1.0 100
G1 0.0 100
G2 0 0
G3 0.2 100
""")
def test_parse_datafile_warns_on_missing_counts(self, tmp_path):
self.assertWarns(self.std.parse_datafile, tmp_path)
# TODO assert correctness
@with_temp_file("""#Data File with bad columns
## Columns = 0 frequency, 1 frequency
{} 1.0 0.0
G1 0.0 1.0
G2 0 1.0
G3 0.2 0.8
""")
def test_parse_datafile_raises_on_bad_columns(self, tmp_path):
with self.assertRaises(ValueError):
self.std.parse_datafile(tmp_path)
@with_temp_file("""#Data File with bad frequency
## Columns = 1 frequency, count total
{} 1.0 100
G1 0.0 100
G2 3.4 100
G3 0.2 100
""")
def test_parse_datafile_warns_on_frequency_out_of_range(self, tmp_path):
self.assertWarns(self.std.parse_datafile, tmp_path)
# TODO assert correctness
@with_temp_file("""#Data File with bad counts
## Columns = 0 count, count total
{} 30 100
G1 10 100
G2 0.2 100
G3 0.1 100
""")
def test_parse_datafile_warns_on_counts_out_of_range(self, tmp_path):
self.assertWarns(self.std.parse_datafile, tmp_path)
# TODO assert correctness
@with_temp_file("""#Multi Data File with default cols
{} 30 100
G1 10 100
G2 20 100
G3 10 100
""")
def test_parse_multidatafile_no_header(self, tmp_path):
mds = self.std.parse_multidatafile(tmp_path)
# TODO assert correctness
@with_temp_file("""#Multi Data File syntax error
{} 30 100
FooBar 10 100
G2 20 100
""")
def test_parse_multidatafile_raises_on_bad_data(self, tmp_path):
with self.assertRaises(ValueError):
self.std.parse_multidatafile(tmp_path)
@with_temp_file("""#Multi Data File bad columns
## Columns = ds1 0 frequency, ds1 1 frequency, ds2 1 count, ds2 count total
{} 0.3 0.4 20 200
G1 0.1 0.5 10 200
G2 0.2 0.3 5 200
""")
def test_parse_multidatafile_raises_on_bad_columns(self, tmp_path):
with self.assertRaises(ValueError):
self.std.parse_multidatafile(tmp_path)
@with_temp_file("""#Multi Data File frequency out of range and count before frequency
## Columns = ds1 count total, ds1 0 frequency, ds2 0 count, ds2 count total
{} 100 0.3 20 200
G1 100 10 10 200
G2 100 0.2 5 200
""")
def test_parse_multidatafile_raises_on_frequency_out_of_range(self, tmp_path):
with self.assertRaises(ValueError):
self.std.parse_multidatafile(tmp_path)
@with_temp_file("""#Multi Data File count out of range
## Columns = ds1 0 count, ds1 count total, ds2 0 count, ds2 count total
{} 0.3 100 20 200
G1 0.1 100 10 200
G2 20 100 5 200
""")
def test_parse_multidatafile_raises_on_counts_out_of_range(self, tmp_path):
with self.assertRaises(ValueError):
self.std.parse_multidatafile(tmp_path)
@with_temp_file("""#Multi Data File with bad syntax
## Columns = ds1 0 count, ds1 count total, ds2 0 count, ds2 count total
{xxx} 0.3 100 20 200
""")
def test_parse_multidatafile_raises_on_syntax_error(self, tmp_path):
with self.assertRaises(ValueError):
self.std.parse_multidatafile(tmp_path)
@with_temp_file("""#My Model file
PREP: rho
LiouvilleVec
1.0/sqrt(2) 0 0 1.0/sqrt(2)
POVM: Mdefault
EFFECT: 0
LiouvilleVec
1.0/sqrt(2) 0 0 -1.0/sqrt(2)
END POVM
GATE: G1
LiouvilleMx
1 0 0 0
0 1 0 0
0 0 0 -1
0 0 1 0
GATE: G2
LiouvilleMx
1 0 0 0
0 0 0 1
0 0 1 0
0 -1 0 0
BASIS: pp 4
""")
def test_read_model(self, tmp_path):
gs1 = stdin.read_model(tmp_path)
rotXPiOv2 = pc.build_operation([(4,)], [('Q0',)], "X(pi/2,Q0)")
rotYPiOv2 = pc.build_operation([(4,)], [('Q0',)], "Y(pi/2,Q0)")
self.assertArraysAlmostEqual(gs1.operations['G1'], rotXPiOv2)
self.assertArraysAlmostEqual(gs1.operations['G2'], rotYPiOv2)
self.assertArraysAlmostEqual(gs1.preps['rho'], 1 / np.sqrt(2) * np.array([1, 0, 0, 1]).reshape(-1, 1))
self.assertArraysAlmostEqual(gs1.povms['Mdefault']['0'], 1 / np.sqrt(2)
* np.array([1, 0, 0, -1]).reshape(-1, 1))
@with_temp_file("""#My Model file specified using non-Liouville format
PREP: rho_up
StateVec
1 0
PREP: rho_dn
DensityMx
0 0
0 1
POVM: Mdefault
EFFECT: 0
StateVec
1 0
END POVM
#G1 = X(pi/2)
GATE: G1
UnitaryMx
1/sqrt(2) -1j/sqrt(2)
-1j/sqrt(2) 1/sqrt(2)
#G2 = Y(pi/2)
GATE: G2
UnitaryMxExp
0 -1j*pi/4.0
1j*pi/4.0 0
#G3 = X(pi)
GATE: G3
UnitaryMxExp
0 pi/2
pi/2 0
BASIS: pp 4
GAUGEGROUP: Full
""")
def test_read_model_non_liouville(self, tmp_path):
gs2 = stdin.read_model(tmp_path)
rotXPi = pc.build_operation([(4,)], [('Q0',)], "X(pi,Q0)")
rotXPiOv2 = pc.build_operation([(4,)], [('Q0',)], "X(pi/2,Q0)")
rotYPiOv2 = pc.build_operation([(4,)], [('Q0',)], "Y(pi/2,Q0)")
self.assertArraysAlmostEqual(gs2.operations['G1'], rotXPiOv2)
self.assertArraysAlmostEqual(gs2.operations['G2'], rotYPiOv2)
self.assertArraysAlmostEqual(gs2.operations['G3'], rotXPi)
self.assertArraysAlmostEqual(gs2.preps['rho_up'], 1 / np.sqrt(2) * np.array([1, 0, 0, 1]).reshape(-1, 1))
self.assertArraysAlmostEqual(gs2.povms['Mdefault']['0'], 1 / np.sqrt(2) * np.array([1, 0, 0, 1]).reshape(-1, 1))
@with_temp_file("""#My Model file specifying 2-Qubit gates using non-Lioville format
PREP: rho_up
DensityMx
1 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
POVM: Mdefault
EFFECT: 00
DensityMx
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 1
EFFECT: 11
DensityMx
1 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
END POVM
GATE: G1
UnitaryMx
1/sqrt(2) -1j/sqrt(2) 0 0
-1j/sqrt(2) 1/sqrt(2) 0 0
0 0 1 0
0 0 0 1
GATE: G2
UnitaryMxExp
0 -1j*pi/4.0 0 0
1j*pi/4.0 0 0 0
0 0 1 0
0 0 0 1
BASIS: pp 16
GAUGEGROUP: Full
""")
def test_read_model_2q(self, tmp_path):
gs8 = stdin.read_model(tmp_path)
# TODO assert correctness
@with_temp_file("""#My Model file with instrument and POVM at end
PREP: rho
LiouvilleVec
1.0/sqrt(2) 0 0 1.0/sqrt(2)
GATE: G1
LiouvilleMx
1 0 0 0
0 1 0 0
0 0 0 -1
0 0 1 0
GATE: G2
LiouvilleMx
1 0 0 0
0 0 0 1
0 0 1 0
0 -1 0 0
Instrument: Iz
IGATE: minus
LiouvilleMx
0.50000000 0 0 -0.50000000
0 0 0 0
0 0 0 0
-0.50000000 0 0 0.50000000
IGATE: plus
LiouvilleMx
0.50000000 0 0 0.50000000
0 0 0 0
0 0 0 0
0.50000000 0 0 0.50000000
END Instrument
BASIS: pp 4
GAUGEGROUP: full
POVM: Mdefault
EFFECT: 0
LiouvilleVec
1.0/sqrt(2) 0 0 -1.0/sqrt(2)
END POVM
""")
def test_read_model_with_instrument_and_povm(self, tmp_path):
gs10 = stdin.read_model(tmp_path)
# TODO assert correctness
@with_temp_file("""#My Model file with bad StateVec size
PREP: rho_up
StateVec
1 0 0
""")
def test_read_model_raises_on_bad_statevec(self, tmp_path):
with self.assertRaises(ValueError):
stdin.read_model(tmp_path)
@with_temp_file("""#My Model file with bad DensityMx size
PREP: rho_dn
DensityMx
0 0 0
0 1 0
0 0 1
BASIS: pp 4
""")
def test_read_model_raises_on_bad_densitymx(self, tmp_path):
with self.assertRaises(ValueError):
stdin.read_model(tmp_path)
@with_temp_file("""#My Model file with bad UnitaryMx size
#G1 = X(pi/2)
GATE: G1
UnitaryMx
1/sqrt(2) -1j/sqrt(2)
BASIS: pp 4
""")
def test_read_model_raises_on_bad_unitarymx(self, tmp_path):
with self.assertRaises(AssertionError):
stdin.read_model(tmp_path)
@with_temp_file("""#My Model file with bad UnitaryMxExp size
#G2 = Y(pi/2)
GATE: G2
UnitaryMxExp
0 -1j*pi/4.0 0.0
1j*pi/4.0 0 0.0
BASIS: pp 4
""")
def test_read_model_raises_on_bad_unitarymxexp(self, tmp_path):
with self.assertRaises(ValueError):
stdin.read_model(tmp_path)
@with_temp_file("""#My Model file with bad format spec
GATE: G2
FooBar
0 1
1 0
BASIS: pp 4
""")
def test_read_model_raises_on_bad_format_spec(self, tmp_path):
with self.assertRaises(ValueError):
stdin.read_model(tmp_path)
@with_temp_file("""# Invalid gauge group
GATE: G1
UnitaryMx
1 0
0 1
BASIS: pp 4
GAUGEGROUP: Foobar
""")
def test_read_model_warns_on_invalid_gauge_group(self, tmp_path):
self.assertWarns(stdin.read_model, tmp_path)
@with_temp_file("""# Invalid item type
FOOBARGATE: G1
UnitaryMx
1 0
0 1
BASIS: pp 4
GAUGEGROUP: full
""")
def test_read_model_raises_on_bad_item_type(self, tmp_path):
with self.assertRaises(ValueError):
stdin.read_model(tmp_path)
@with_temp_file("""# No basis dimension
BASIS: pp
""")
def test_read_model_raises_on_no_basis_dimension(self, tmp_path):
with self.assertRaises(ValueError):
stdin.read_model(tmp_path)
@with_temp_path
def _test_gateset_writeload(self, tmp_path, param):
mdl = std.target_model()
mdl.set_all_parameterizations(param)
io.write_model(mdl, tmp_path)
gs2 = stdin.read_model(tmp_path)
self.assertAlmostEqual(mdl.frobeniusdist(gs2), 0.0)
for lbl in mdl.operations:
self.assertEqual(type(mdl.operations[lbl]), type(gs2.operations[lbl]))
for lbl in mdl.preps:
self.assertEqual(type(mdl.preps[lbl]), type(gs2.preps[lbl]))
for lbl in mdl.povms:
self.assertEqual(type(mdl.povms[lbl]), type(gs2.povms[lbl]))
for lbl in mdl.instruments:
self.assertEqual(type(mdl.instruments[lbl]), type(gs2.instruments[lbl]))
def test_read_model_full_param(self):
self._test_gateset_writeload('full')
def test_read_model_TP_param(self):
self._test_gateset_writeload('TP')
def test_read_model_CPTP_param(self):
self._test_gateset_writeload('CPTP')
def test_read_model_static_param(self):
self._test_gateset_writeload('static')