Skip to content

Commit f896512

Browse files
committed
Fix last failing qasm3 test for std gates without stdgates.inc
While the logic for the qasm3 exporter was fixed in commit a6e69ba to handle the edge case of a user specifying that the qasm exporter does not use the stdgates.inc include file in the output, but also has qiskit's standard gates in their circuit being exported. The one unit test to provide coverage for that scenario was not passing because when an id was used for the gate definitions in the qasm3 file it was being referenced against a temporary created by accessing a standard gate from the circuit and the ids weren't the same so the reference string didn't match what the exporter generated. This commit fixes this by changing the test to not do an exact string comparison, but instead a line by line comparison that either does exact equality check or a regex search for the expected line and the ids are checked as being any 15 character integer.
1 parent 0edcfb0 commit f896512

File tree

1 file changed

+52
-46
lines changed

1 file changed

+52
-46
lines changed

test/python/qasm3/test_export.py

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,52 +2130,58 @@ def test_no_include(self):
21302130
h_ = sx.definition.data[1].operation
21312131
u2_1 = h_.definition.data[0].operation
21322132
u3_3 = u2_1.definition.data[0].operation
2133-
expected_qasm = "\n".join(
2134-
[
2135-
"OPENQASM 3.0;",
2136-
f"gate u3_{id(u3_1)}(_gate_p_0, _gate_p_1, _gate_p_2) _gate_q_0 {{",
2137-
" U(0, 0, pi/2) _gate_q_0;",
2138-
"}",
2139-
f"gate u1_{id(u1_1)}(_gate_p_0) _gate_q_0 {{",
2140-
f" u3_{id(u3_1)}(0, 0, pi/2) _gate_q_0;",
2141-
"}",
2142-
f"gate rz_{id(rz)}(_gate_p_0) _gate_q_0 {{",
2143-
f" u1_{id(u1_1)}(pi/2) _gate_q_0;",
2144-
"}",
2145-
f"gate u3_{id(u3_2)}(_gate_p_0, _gate_p_1, _gate_p_2) _gate_q_0 {{",
2146-
" U(0, 0, -pi/2) _gate_q_0;",
2147-
"}",
2148-
f"gate u1_{id(u1_2)}(_gate_p_0) _gate_q_0 {{",
2149-
f" u3_{id(u3_2)}(0, 0, -pi/2) _gate_q_0;",
2150-
"}",
2151-
"gate sdg _gate_q_0 {",
2152-
f" u1_{id(u1_2)}(-pi/2) _gate_q_0;",
2153-
"}",
2154-
f"gate u3_{id(u3_3)}(_gate_p_0, _gate_p_1, _gate_p_2) _gate_q_0 {{",
2155-
" U(pi/2, 0, pi) _gate_q_0;",
2156-
"}",
2157-
f"gate u2_{id(u2_1)}(_gate_p_0, _gate_p_1) _gate_q_0 {{",
2158-
f" u3_{id(u3_3)}(pi/2, 0, pi) _gate_q_0;",
2159-
"}",
2160-
"gate h _gate_q_0 {",
2161-
f" u2_{id(u2_1)}(0, pi) _gate_q_0;",
2162-
"}",
2163-
"gate sx _gate_q_0 {",
2164-
" sdg _gate_q_0;",
2165-
" h _gate_q_0;",
2166-
" sdg _gate_q_0;",
2167-
"}",
2168-
"gate cx c, t {",
2169-
" ctrl @ U(pi, 0, pi) c, t;",
2170-
"}",
2171-
"qubit[2] q;",
2172-
f"rz_{id(rz)}(pi/2) q[0];",
2173-
"sx q[0];",
2174-
"cx q[0], q[1];",
2175-
"",
2176-
]
2177-
)
2178-
self.assertEqual(Exporter(includes=[]).dumps(circuit), expected_qasm)
2133+
id_len = len(str(id(u3_1)))
2134+
expected_qasm = [
2135+
"OPENQASM 3.0;",
2136+
re.compile(r"gate u3_\d{%s}\(_gate_p_0, _gate_p_1, _gate_p_2\) _gate_q_0 \{" % id_len),
2137+
" U(0, 0, pi/2) _gate_q_0;",
2138+
"}",
2139+
re.compile(r"gate u1_\d{%s}\(_gate_p_0\) _gate_q_0 \{" % id_len),
2140+
re.compile(r" u3_\d{%s}\(0, 0, pi/2\) _gate_q_0;" % id_len),
2141+
"}",
2142+
re.compile(r"gate rz_\d{%s}\(_gate_p_0\) _gate_q_0 \{" % id_len),
2143+
re.compile(r" u1_\d{%s}\(pi/2\) _gate_q_0;" % id_len),
2144+
"}",
2145+
re.compile(r"gate u3_\d{%s}\(_gate_p_0, _gate_p_1, _gate_p_2\) _gate_q_0 \{" % id_len),
2146+
" U(0, 0, -pi/2) _gate_q_0;",
2147+
"}",
2148+
re.compile(r"gate u1_\d{%s}\(_gate_p_0\) _gate_q_0 \{" % id_len),
2149+
re.compile(r" u3_\d{%s}\(0, 0, -pi/2\) _gate_q_0;" % id_len),
2150+
"}",
2151+
"gate sdg _gate_q_0 {",
2152+
re.compile(r" u1_\d{%s}\(-pi/2\) _gate_q_0;" % id_len),
2153+
"}",
2154+
re.compile(r"gate u3_\d{%s}\(_gate_p_0, _gate_p_1, _gate_p_2\) _gate_q_0 \{" % id_len),
2155+
" U(pi/2, 0, pi) _gate_q_0;",
2156+
"}",
2157+
re.compile(r"gate u2_\d{%s}\(_gate_p_0, _gate_p_1\) _gate_q_0 \{" % id_len),
2158+
re.compile(r" u3_\d{%s}\(pi/2, 0, pi\) _gate_q_0;" % id_len),
2159+
"}",
2160+
"gate h _gate_q_0 {",
2161+
re.compile(r" u2_\d{%s}\(0, pi\) _gate_q_0;" % id_len),
2162+
"}",
2163+
"gate sx _gate_q_0 {",
2164+
" sdg _gate_q_0;",
2165+
" h _gate_q_0;",
2166+
" sdg _gate_q_0;",
2167+
"}",
2168+
"gate cx c, t {",
2169+
" ctrl @ U(pi, 0, pi) c, t;",
2170+
"}",
2171+
"qubit[2] q;",
2172+
re.compile(r"rz_\d{%s}\(pi/2\) q\[0\];" % id_len),
2173+
"sx q[0];",
2174+
"cx q[0], q[1];",
2175+
"",
2176+
]
2177+
res = Exporter(includes=[]).dumps(circuit).splitlines()
2178+
for result, expected in zip(res, expected_qasm):
2179+
if isinstance(expected, str):
2180+
self.assertEqual(result, expected)
2181+
else:
2182+
self.assertTrue(
2183+
expected.search(result), f"Line {result} doesn't match regex: {expected}"
2184+
)
21792185

21802186
def test_unusual_conditions(self):
21812187
"""Test that special QASM constructs such as ``measure`` are correctly handled when the

0 commit comments

Comments
 (0)