TeamGraphix / graphix

measurement-based quantum computing (MBQC) compiler and simulator
https://graphix.readthedocs.io
Apache License 2.0
55 stars 20 forks source link

Fix #45: Parameterized measurement patterns #158

Open thierry-martinez opened 1 month ago

thierry-martinez commented 1 month ago

This commit is yet another tentative to implement parameterized measurement patterns, to fulfill issue #45 (previous tentative: #68).

This commit adds two methods to the class Pattern:

codecov[bot] commented 1 month ago

Codecov Report

Attention: Patch coverage is 81.87050% with 126 lines in your changes missing coverage. Please review.

Project coverage is 72.95%. Comparing base (ec4c582) to head (5feac05). Report is 5 commits behind head on master.

Files Patch % Lines
graphix/parameter.py 80.54% 122 Missing :warning:
graphix/transpiler.py 80.95% 4 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #158 +/- ## ========================================== + Coverage 71.82% 72.95% +1.13% ========================================== Files 30 31 +1 Lines 5359 6038 +679 ========================================== + Hits 3849 4405 +556 - Misses 1510 1633 +123 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

shinich1 commented 1 month ago

@thierry-martinez great, is the intention to merge @pafloxy's #68-equivalent code first (when it's ready), before merging this?

thierry-martinez commented 1 month ago

@thierry-martinez great, is the intention to merge @pafloxy's #68-equivalent code first (when it's ready), before merging this?

I adapted #68 code for Parameter class in the commit 81bbeb7 I just pushed. #68 was incorrect because arithmetic operators changed parameters in place (computing alpha + 1 did change the value of alpha to alpha + 1!). In this PR, we distinguish between Parameter (alpha) and ParameterExpression (alpha + 1), and a Parameter is a particular case of ParameterExpression.

The test test_parameter_simulation is illustrative: we can either substitute a parameter by a value in a pattern then simulate, or simulate symbolically (given that the pattern is deterministic!) then substitute in the (symbolic) state vector or the density matrix, and we get the same result.

shinich1 commented 1 month ago

@thierry-martinez @pafloxy great! give us a few days to look through. A few quick comments:

  1. is TN backend working with parameters, or should we throw error in TN backend if pattern is parametrised? 2. could you add sympy to requirements.txt?
  2. will circuit.simulate_statevector, pattern.perform_pauli_measurements, as well as visualization tool, work with parameters?
EarlMilktea commented 1 month ago

Let me review it later.

Anyway, is it absolutely necessary to use sympy? I'm concerned with...

Additionally, let me point out that @masa10-f is planning to completely eliminate sympy from the source.

EarlMilktea commented 1 month ago

If sympy is absolutely necessary, it's OK, but we may need to verify that we're using it wisely.

pafloxy commented 4 weeks ago

@thierry-martinez @pafloxy great! give us a few days to look through. A few quick comments:

  1. is TN backend working with parameters, or should we throw error in TN backend if pattern is parametrised? ~2. could you add sympy to requirements.txt?~
  2. will circuit.simulate_statevector, pattern.perform_pauli_measurements, as well as visualization tool, work with parameters?

@shinich1 @thierry-martinez Maybe you guys figured it already but I think we need to make some small modification to the visualization.py module as well. There are some checks to determine if a measurement angle is Pauli in some of the class methods for GraphVisualizer of the form

elif (
                show_pauli_measurement
                and self.meas_angles is not None
                and (
                    2 * self.meas_angles[node] == int(2 * self.meas_angles[node])
                )  # measurement angle is integer or half-integer
            ):

where we might need to add another line or so to exclude the case where the measurement angles is an instance of Parameter/ParameterExpression.

shinich1 commented 3 weeks ago

Hi @pafloxy @thierry-martinez

I think the best way is to move parameter implementation with sympy out of main graphix repository, for example a separate repo (wrapper/module) dedicated to parameterized patterns and their executions (it seems possible, given the relatively small changes required to implement?). I am happy to initiate a creation of such repository in teamgraphix organization, if that makes sense to you? for example, that can be part of extra pip installation.

The reason mostly follows @EarlMilktea 's. Sustaining maintainability is going to help a lot in the long run for everyone contributing to this repo and will help a lot those maintaining.

  1. sympy has quite a performance issue. Looking at their github repo, it seems like a recurring issue in qiskit (which has sympy incorporated) and quite a bit of effort can be saved by deciding to move it out at this point (nb as @EarlMilktea mentioned sympy will be removed from requirements.txt when gflow is refactored for performance soon.)
  2. typing, linter compatibility, as in @EarlMilktea's comment
thierry-martinez commented 3 weeks ago

I propose in my last commits a version which does not require mypy.

shinich1 commented 2 weeks ago

I propose in my last commits a version which does not require mypy.

  • The module parameter.py implements symbolic expressions so as to support parameters, but without all the symbolic. machinery of mypy (computations with known values are done numerically, not symbolically).
  • The rest of the code does not depend on parameter.py: in particular, the code for simulators is generic. If we still need the sympy version of parameters, we can implement it separately, and simulators should work without any specific modification in the code of graphix.
  • Tests for parameters are done in a specific test_parameter.py module, and the tests include circuit simulation.

Thank you! I believe you mean sympy instead of mypy above? Let me take a detailed look at the code soon.

shinich1 commented 1 week ago

@thierry-martinez the implementation looks good to me! we need to:

EarlMilktea commented 1 week ago

I'm a little bit concerned about...

shinich1 commented 2 days ago

We suggest the following: