Skip to content

Commit

Permalink
Fix last failing qasm3 test for std gates without stdgates.inc
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mtreinish committed May 25, 2024
1 parent 0edcfb0 commit f896512
Showing 1 changed file with 52 additions and 46 deletions.
98 changes: 52 additions & 46 deletions test/python/qasm3/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -2130,52 +2130,58 @@ def test_no_include(self):
h_ = sx.definition.data[1].operation
u2_1 = h_.definition.data[0].operation
u3_3 = u2_1.definition.data[0].operation
expected_qasm = "\n".join(
[
"OPENQASM 3.0;",
f"gate u3_{id(u3_1)}(_gate_p_0, _gate_p_1, _gate_p_2) _gate_q_0 {{",
" U(0, 0, pi/2) _gate_q_0;",
"}",
f"gate u1_{id(u1_1)}(_gate_p_0) _gate_q_0 {{",
f" u3_{id(u3_1)}(0, 0, pi/2) _gate_q_0;",
"}",
f"gate rz_{id(rz)}(_gate_p_0) _gate_q_0 {{",
f" u1_{id(u1_1)}(pi/2) _gate_q_0;",
"}",
f"gate u3_{id(u3_2)}(_gate_p_0, _gate_p_1, _gate_p_2) _gate_q_0 {{",
" U(0, 0, -pi/2) _gate_q_0;",
"}",
f"gate u1_{id(u1_2)}(_gate_p_0) _gate_q_0 {{",
f" u3_{id(u3_2)}(0, 0, -pi/2) _gate_q_0;",
"}",
"gate sdg _gate_q_0 {",
f" u1_{id(u1_2)}(-pi/2) _gate_q_0;",
"}",
f"gate u3_{id(u3_3)}(_gate_p_0, _gate_p_1, _gate_p_2) _gate_q_0 {{",
" U(pi/2, 0, pi) _gate_q_0;",
"}",
f"gate u2_{id(u2_1)}(_gate_p_0, _gate_p_1) _gate_q_0 {{",
f" u3_{id(u3_3)}(pi/2, 0, pi) _gate_q_0;",
"}",
"gate h _gate_q_0 {",
f" u2_{id(u2_1)}(0, pi) _gate_q_0;",
"}",
"gate sx _gate_q_0 {",
" sdg _gate_q_0;",
" h _gate_q_0;",
" sdg _gate_q_0;",
"}",
"gate cx c, t {",
" ctrl @ U(pi, 0, pi) c, t;",
"}",
"qubit[2] q;",
f"rz_{id(rz)}(pi/2) q[0];",
"sx q[0];",
"cx q[0], q[1];",
"",
]
)
self.assertEqual(Exporter(includes=[]).dumps(circuit), expected_qasm)
id_len = len(str(id(u3_1)))
expected_qasm = [
"OPENQASM 3.0;",
re.compile(r"gate u3_\d{%s}\(_gate_p_0, _gate_p_1, _gate_p_2\) _gate_q_0 \{" % id_len),
" U(0, 0, pi/2) _gate_q_0;",
"}",
re.compile(r"gate u1_\d{%s}\(_gate_p_0\) _gate_q_0 \{" % id_len),
re.compile(r" u3_\d{%s}\(0, 0, pi/2\) _gate_q_0;" % id_len),
"}",
re.compile(r"gate rz_\d{%s}\(_gate_p_0\) _gate_q_0 \{" % id_len),
re.compile(r" u1_\d{%s}\(pi/2\) _gate_q_0;" % id_len),
"}",
re.compile(r"gate u3_\d{%s}\(_gate_p_0, _gate_p_1, _gate_p_2\) _gate_q_0 \{" % id_len),
" U(0, 0, -pi/2) _gate_q_0;",
"}",
re.compile(r"gate u1_\d{%s}\(_gate_p_0\) _gate_q_0 \{" % id_len),
re.compile(r" u3_\d{%s}\(0, 0, -pi/2\) _gate_q_0;" % id_len),
"}",
"gate sdg _gate_q_0 {",
re.compile(r" u1_\d{%s}\(-pi/2\) _gate_q_0;" % id_len),
"}",
re.compile(r"gate u3_\d{%s}\(_gate_p_0, _gate_p_1, _gate_p_2\) _gate_q_0 \{" % id_len),
" U(pi/2, 0, pi) _gate_q_0;",
"}",
re.compile(r"gate u2_\d{%s}\(_gate_p_0, _gate_p_1\) _gate_q_0 \{" % id_len),
re.compile(r" u3_\d{%s}\(pi/2, 0, pi\) _gate_q_0;" % id_len),
"}",
"gate h _gate_q_0 {",
re.compile(r" u2_\d{%s}\(0, pi\) _gate_q_0;" % id_len),
"}",
"gate sx _gate_q_0 {",
" sdg _gate_q_0;",
" h _gate_q_0;",
" sdg _gate_q_0;",
"}",
"gate cx c, t {",
" ctrl @ U(pi, 0, pi) c, t;",
"}",
"qubit[2] q;",
re.compile(r"rz_\d{%s}\(pi/2\) q\[0\];" % id_len),
"sx q[0];",
"cx q[0], q[1];",
"",
]
res = Exporter(includes=[]).dumps(circuit).splitlines()
for result, expected in zip(res, expected_qasm):
if isinstance(expected, str):
self.assertEqual(result, expected)
else:
self.assertTrue(
expected.search(result), f"Line {result} doesn't match regex: {expected}"
)

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

0 comments on commit f896512

Please sign in to comment.