/
test_circuitparser.py
65 lines (56 loc) · 2.44 KB
/
test_circuitparser.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
import unittest
from ..util import BaseCase
from pygsti.circuits.circuitparser import slowcircuitparser
try:
from pygsti.circuits.circuitparser import fastcircuitparser
_FASTCIRCUITPARSER_LOADED = True
except ImportError:
_FASTCIRCUITPARSER_LOADED = False
class CircuitParserBase():
@classmethod
def setUpClass(cls):
cls.test_cases = [
("{}", ()),
("{}^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')),
("G_my_xG_my_y", ('G_my_x', 'G_my_y')),
("G_my_x*G_my_y", ('G_my_x', 'G_my_y')),
("GsG___", ('Gs', 'G___'))
]
cls.fail_cases = [
"FooBar",
"G1G2^2^2",
"(G1"
]
def setUp(self):
self.parser = None
def test_parse_circuit(self):
for string, expected in self.test_cases:
results, line_labels, occurrence, compilable_indices = self.parser.parse_circuit(string, create_subcircuits=True, integerize_sslbls=True)
assert line_labels is None
assert occurrence is None
assert compilable_indices is None
flat_results = [lbl for item in results for lbl in item.expand_subcircuits()]
for result_label, expected_label in zip(flat_results, expected):
assert result_label == expected_label
def test_parse_circuit_raises_on_syntax_error(self):
for string in self.fail_cases:
with self.assertRaises(ValueError):
self.parser.parse_circuit(string, create_subcircuits=True, integerize_sslbls=True)
class SlowParser(CircuitParserBase, BaseCase):
def setUp(self):
self.parser = slowcircuitparser
@unittest.skipUnless(_FASTCIRCUITPARSER_LOADED, "`pygsti.io.fastcircuitparser` not built")
class FastParser(CircuitParserBase, BaseCase):
def setUp(self):
self.parser = fastcircuitparser