Skip to content

Add in-place variants for infer_pauli_measurements, to_bloch,...#568

Open
thierry-martinez wants to merge 3 commits into
TeamGraphix:masterfrom
thierry-martinez:in-place_methods
Open

Add in-place variants for infer_pauli_measurements, to_bloch,...#568
thierry-martinez wants to merge 3 commits into
TeamGraphix:masterfrom
thierry-martinez:in-place_methods

Conversation

@thierry-martinez

Copy link
Copy Markdown
Collaborator

Method Pattern.infer_pauli_measurements now operates in place by default, with an optional copy parameter. If copy=True, a modified copy is returned instead.

Added the following methods to Pattern. By default, they modify the pattern in place, with an optional copy parameter. If copy=True, a modified copy is returned instead.

  • apply, an in-place variant of map,
  • blochify, an in-place variant of to_bloch,
  • substitute, an in-place variant of subs,
  • replace_parameters, an in-place variant of xreplace.

This is a step toward the cleaner simulation API proposed in #493. It also makes the examples in the Graphix paper cleaner.

Method `Pattern.infer_pauli_measurements` now operates in place by
default, with an optional `copy` parameter.  If `copy=True`, a
modified copy is returned instead.

Added the following methods to `Pattern`. By default, they modify the
pattern in place, with an optional `copy` parameter.  If `copy=True`,
a modified copy is returned instead.
  - `apply`, an in-place variant of `map`,
  - `blochify`, an in-place variant of `to_bloch`,
  - `substitute`, an in-place variant of `subs`,
  - `replace_parameters`, an in-place variant of `xreplace`.

This is a step toward the cleaner simulation API proposed in TeamGraphix#493.
It also makes the examples in the Graphix paper cleaner.
@codecov

codecov Bot commented Jul 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.59%. Comparing base (2d437e0) to head (86f69ce).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #568      +/-   ##
==========================================
+ Coverage   90.58%   90.59%   +0.01%     
==========================================
  Files          49       49              
  Lines        7481     7489       +8     
==========================================
+ Hits         6777     6785       +8     
  Misses        704      704              

☔ View full report in Codecov by Harness.
📢 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.

@matulni matulni left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the change, new functions are useful!

I'm not convinced by the naming of the parametric angle functions. The following are not very intuitive to me:

  • subs and substitute have the same relationship as xreplace and replace_parameters.
  • substitute and replace_parameters act in-place.
  • subs replaces one parameter, and xreplace replaces multiple parameters in parallel. (I'm aware that this is a sympy convention and that it's independent of this PR, but I always found it difficult to remember. In addition, with the new methods we drift away from sympy's convention).

Here are two alternatives:

A)
substitute -> subs_in_place
replace_parameters -> xreplace_in_place

It solves 1) and 2), but it's more verbose and not true when calling subs_in_place(..., copy=True).

B)
subs -> substituted

substitute remains

xreplace -> substituted_multi

replace_parameters -> substitute_multi

I think this solves all three points but we don't follow sympy convention at all, and it requires a larger refactoring (we would need to update analogous methods in OpenGraph, etc.). If you think the -ed suffix may be easier to miss when reading the code, we could use with_substitution instead.

What do you think ?

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.

2 participants