Skip to content

Conversation

@thierry-martinez
Copy link
Collaborator

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-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.

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
Copy link

codecov bot commented Dec 24, 2025

Codecov Report

❌ Patch coverage is 65.74074% with 37 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.57%. Comparing base (14a05b1) to head (b8f4416).

Files with missing lines Patch % Lines
graphix/optimization.py 3.57% 27 Missing ⚠️
graphix/qasm3_exporter.py 89.33% 8 Missing ⚠️
graphix/pattern.py 66.66% 1 Missing ⚠️
graphix/sim/statevec.py 50.00% 1 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

yield 'include "stdgates.inc";\n'
yield "\n"
for node in pattern.input_nodes:
yield f"// prepare input qubit {node} in |+⟩\n"
Copy link
Contributor

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"

Copy link
Collaborator Author

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"
Copy link
Contributor

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?

Copy link
Collaborator Author

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'{{...}}')
{...}

if cmd.plane == Plane.XY:
yield f"h q{cmd.node};\n"
if cmd.angle != 0:
rad_angle = angle_to_rad(cmd.angle)
Copy link
Contributor

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).

Copy link
Collaborator Author

@thierry-martinez thierry-martinez Jan 9, 2026

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:
Copy link
Contributor

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 ?)

Copy link
Collaborator Author

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.

See :func:`pattern_to_qasm3`.
"""
yield "// generated by graphix\n"
Copy link
Contributor

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants