Skip to content

Conversation

@thierry-martinez
Copy link
Collaborator

This commit adds the method Pattern.check_runnability that ensures a pattern is runnable. This covers #193, given that determinism is already checkable with Pauli flow finding, and pattern concatenation is already implemented by Pattern.compose.

This commit adds the method `Pattern.check_runnability` that ensures a
pattern is runnable. This covers TeamGraphix#193, given that determinism is
already checkable with Pauli flow finding, and pattern concatenation
is already implemented by `Pattern.compose`.
@codecov
Copy link

codecov bot commented Nov 13, 2025

Codecov Report

❌ Patch coverage is 90.32258% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.25%. Comparing base (0c9d827) to head (7962710).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
graphix/pattern.py 86.90% 11 Missing ⚠️
graphix/optimization.py 96.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #364      +/-   ##
==========================================
- Coverage   86.27%   86.25%   -0.02%     
==========================================
  Files          44       44              
  Lines        6106     6163      +57     
==========================================
+ Hits         5268     5316      +48     
- Misses        838      847       +9     

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

Copy link
Contributor

@matulni matulni left a comment

Choose a reason for hiding this comment

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

Looks good, thanks, just a couple of minor comments.

Comment on lines 116 to 135
def add(self, cmd: Command) -> None:
"""Add command to the end of the pattern.
An MBQC command is an instance of :class:`graphix.command.Command`.
Parameters
----------
cmd : :class:`graphix.command.Command`
MBQC command.
"""
if cmd.kind == CommandKind.N:
if cmd.node in self.__output_nodes:
raise NodeAlreadyPreparedError(cmd.node)
raise RunnabilityError(cmd, cmd.node, RunnabilityErrorReason.AlreadyActive)
self.__n_node += 1
self.__output_nodes.append(cmd.node)
elif cmd.kind == CommandKind.M:
if cmd.node not in self.__output_nodes:
raise RunnabilityError(cmd, cmd.node, RunnabilityErrorReason.AlreadyMeasured)
self.__output_nodes.remove(cmd.node)
self.__seq.append(cmd)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would add a comment emphasising that this method dos not ensure a secure pattern generation (which has to be verified with :func:Pattern.check_runnability.

Some minor comment: if we add a measurement command on a node i which is not in the pattern, we get an error message M(i): node i is already measured. which is a bit misleading. One way to fix it could be to store the existing nodes in the pattern on the fly (which would also allow to ensure that we can only add E and C commands on existing nodes).

Copy link

Choose a reason for hiding this comment

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

Checking N and M here means that we don't run into problems with extract_graph, space_list and similar methods, but I do think that there may be some niche use for allowing unrunnable patterns (not sure what though).
If we leave this as is, I would suggest adding check_runnability to methods associated with using the pattern for computation (or at the very least a flag _is_runnable that is checked when using these methods that may display a warning). Specifically simulate_pattern and perform_pauli_measurements but may be worthwhile (I'm not confident there won't be errors, so I think it's worth checking) in minimize_space, parallelize_pattern and shift_signals.

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 49616d8, I removed all the partial runnability checks from Pattern.add: I think we would have the choice either to perform a full runnability check in add to guarantee that Pattern objects are always runnable or, as it is done for now, to make no such guarantee and to provide a runnability checker instead, but having only a partial test was awkward. Moreover, having add implementing a part of the runnability check makes it difficult to write tests for check_runnability that were already handled by add, so it is more convenient to be able to let non-runnable patterns being built.

Since standardization and signal shifting could have turned non-runnable patterns into runnable ones (and hence could hide some code-logic errors), I added a call to check_runnability at the beginning of standardization and signal shifting, so in particular all methods that require the pattern to be standardized first check for runnability (minimize_space, parallelize_pattern, perform_pauli_measurements), and I added a check in simulate_pattern too, so that error messages are clearer and raised before starting the simulation.

Copy link

@emlynsg emlynsg left a comment

Choose a reason for hiding this comment

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

Looks great Thierry! No approval-blocking issues.

@thierry-martinez
Copy link
Collaborator Author

Checking runnability before standardizing enables me to catch a bug in move_pauli_measurements_to_the_front that could break runnability; this is fixed in d9c81f3.

Copy link
Contributor

@matulni matulni left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

Copy link

@emlynsg emlynsg left a comment

Choose a reason for hiding this comment

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

Looks good to me too.

@thierry-martinez thierry-martinez merged commit 55abf0a into TeamGraphix:master Nov 25, 2025
19 checks passed
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