Add .ptn text format for pattern serialization#161
Conversation
Implement a human-readable text format (.ptn) for exporting and importing MBQC patterns. The format separates quantum instructions from classical feedforward processing, with explicit timeslice markers for parallel execution. Format structure: - Header section: version, input/output nodes, coordinates - Quantum section: N/E/M/X/Z commands grouped by timeslice [0], [1], etc. - Classical section: xflow/zflow definitions, detector groups Key features: - Assembly-style syntax (one command per line) - Human-readable angle formatting (pi/2, pi/4, etc.) - Support for 2D/3D node coordinates - Inline comment support Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Change measurement output format for Pauli basis measurements: - X measurement: "M <node> X +" or "M <node> X -" - Y measurement: "M <node> Y +" or "M <node> Y -" - Z measurement: "M <node> Z +" or "M <node> Z -" Non-Pauli measurements continue to use plane+angle format: - "M <node> XY pi/4" etc. This makes the format more readable for common Clifford patterns while maintaining full expressiveness for arbitrary measurements. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #161 +/- ##
==========================================
+ Coverage 83.57% 83.73% +0.16%
==========================================
Files 21 22 +1
Lines 2313 2551 +238
Branches 416 466 +50
==========================================
+ Hits 1933 2136 +203
- Misses 288 311 +23
- Partials 92 104 +12 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3c4e6cad58
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if commands or slice_num == 0: | ||
| out.write(f"[{slice_num}]\n") |
There was a problem hiding this comment.
Preserve empty timeslices when writing quantum section
The write_slice guard drops every empty slice except slice 0, so patterns that contain intentional idle slices (e.g., manual schedules with gaps) or end with a trailing TICK get serialized without those boundaries. When reloaded, loads() reconstructs TICKs only from emitted [n] headers, so the pattern depth/timing changes and schedule-sensitive metrics or execution order can be corrupted.
Useful? React with 👍 / 👎.
graphqomb/ptn_format.py
Outdated
| else: | ||
| # X or Z measurement: + at 0, - at pi | ||
| sign = "+" if is_close_angle(angle, 0.0) else "-" | ||
| out.write(f"M {cmd.node} {pauli_axis.name} {sign}\n") |
There was a problem hiding this comment.
Preserve measurement plane for Pauli commands
Serializing all Pauli measurements as X/Y/Z +/- removes the original plane (XY, XZ, or YZ), so dumps()/loads() is lossy for Pauli cases like XZ pi/2 or YZ 0. On load, these become AxisMeasBasis with canonical planes, which can change downstream behavior because correction logic branches on meas_basis.plane (for example in PatternSimulator.apply_cmd and feedforward.propagate_flow).
Useful? React with 👍 / 👎.
graphqomb/ptn_format.py
Outdated
| # Check if this is a Pauli measurement (X/Y/Z with +/-) | ||
| if basis_spec in {"X", "Y", "Z"}: | ||
| sign_str = parts[3] | ||
| sign = Sign.PLUS if sign_str == "+" else Sign.MINUS |
There was a problem hiding this comment.
Validate Pauli sign tokens instead of defaulting to minus
The parser currently treats any sign token other than + as -, so malformed input like M 3 X plus is silently accepted as a minus-basis measurement. This can quietly corrupt loaded experiments instead of surfacing a format error, so invalid sign tokens should raise ValueError.
Useful? React with 👍 / 👎.
- Replace multiple if-statements with lookup tables for angle formatting/parsing - Split monolithic loads() function into _Parser class with small focused methods - Remove Raises section from loads() docstring (exception is raised by _Parser.parse) - Remove per-file-ignores for ptn_format.py from pyproject.toml Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add return type annotations to all test functions - Move Pattern import to TYPE_CHECKING block - Add Returns section to create_simple_pattern docstring - Use raw strings for regex patterns in pytest.raises match - Fix import order Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Summary
.ptn) for MBQC pattern serializationX +,Y -) for readabilityFormat Example
API
Test plan
🤖 Generated with Claude Code