-
Notifications
You must be signed in to change notification settings - Fork 41
Fix #177: Correct pattern export to OpenQASM 3 #404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Fix #177: Correct pattern export to OpenQASM 3 #404
Conversation
This commit introduces `pattern_to_qasm3`, improving the export of patterns to OpenQASM 3: - Planes YZ and XZ are now handled correctly (TeamGraphix#177). - For plane XY, the angle is no longer ignored when the s-domain is empty. - Support for |0⟩ and |+⟩ initial states. - Arithmetic expressions unsupported by `qiskit-qasm3-import` are replaced with a simpler encoding, and the `single_qubit_domains` pass ensures compatibility with Qiskit. - `test_qasm3_exporter_to_qiskit.py` verifies that the Graphix pattern simulator and Qiskit AER simulator produce equivalent statevectors for exported circuits. Additionally, this commit fixes a regression from TeamGraphix#312: statevectors are now properly normalized.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #404 +/- ##
==========================================
+ Coverage 85.03% 85.57% +0.54%
==========================================
Files 46 46
Lines 6654 6691 +37
==========================================
+ Hits 5658 5726 +68
+ Misses 996 965 -31 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
graphix/qasm3_exporter.py
Outdated
| yield 'include "stdgates.inc";\n' | ||
| yield "\n" | ||
| for node in pattern.input_nodes: | ||
| yield f"// prepare input qubit {node} in |+⟩\n" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this misleading, since input qubits may be prepared in |+> or |0> ?
I think it should be moved to the branch elif state == BasicStates.PLUS: in :func: state_to_qasm3_lines, or alternatively
f"// prepare input qubit {node} in |+⟩ or |0⟩\n"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch! This is now fixed.
| condition = " ^ ".join(f"c{node}" for node in domain) | ||
| if not condition: | ||
| return | ||
| yield f"if ({condition}) {{\n" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible that there's one { too many?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In f-strings, { should be doubled to appear in the final string. https://docs.python.org/3/reference/lexical_analysis.html#f-strings:
Any doubled curly braces ({{ or }}) outside replacement fields are replaced with the corresponding single curly brace:
print(f'{{...}}') {...}
graphix/qasm3_exporter.py
Outdated
| if cmd.plane == Plane.XY: | ||
| yield f"h q{cmd.node};\n" | ||
| if cmd.angle != 0: | ||
| rad_angle = angle_to_rad(cmd.angle) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we use angle_to_qasm3 ? (This would prevent errors if the cmd.angle is parametric).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you are right, angle_to_qasm3 is better here. This is fixed.
| yield f"rx({-rad_angle}) q{cmd.node};\n" | ||
| elif cmd.plane == Plane.XZ: | ||
| yield f"ry({-rad_angle}) q{cmd.node};\n" | ||
| else: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To improve readability, I would suggest putting elif cmd.plane == Plane.YZ followed by typing_extensions.assert_never (or match. Actually I'm not sure if there is an advantage on using match over chained if-else, or if it's just a matter of taste. Would we need typing_extensions.assert_never if we were using match ?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. There is no default exhaustiveness check with match in Python, any more than with chained if/elif: we need to add a default clause, case e: assert_never(e), to ensure exhaustiveness.
graphix/qasm3_exporter.py
Outdated
| See :func:`pattern_to_qasm3`. | ||
| """ | ||
| yield "// generated by graphix\n" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could it be useful to add graphix version here ? Not sure if we expose it at the moment, if not, I wouldn't bother adding it to this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. Fixed.
This commit introduces
pattern_to_qasm3, improving the export of patterns to OpenQASM 3:Planes YZ and XZ are now handled correctly (QASM3 export only supports XY plane measurements #177).
For plane XY, the angle is no longer ignored when the s-domain is empty.
Support for |0⟩ and |+⟩ initial states.
Arithmetic expressions unsupported by
qiskit-qasm3-importare replaced with a simpler encoding, and thesingle_qubit_domainspass ensures compatibility with Qiskit.test_qasm3_exporter_to_qiskit.pyverifies that the Graphix pattern simulator and Qiskit AER simulator produce equivalent statevectors for exported circuits.