ProjectQ-Framework / ProjectQ

ProjectQ: An open source software framework for quantum computing
https://projectq.ch
Apache License 2.0
874 stars 271 forks source link

Update to add commutation rules to the LocalOptimizer engine #386

Closed daisyrainsmith closed 2 years ago

daisyrainsmith commented 3 years ago

Update to add commutation rules to the optimizer engine.

  1. Update _optimizer.py and _optimizer_test.py
    • apply_commutation Boolean – tells LocalOptimizer whether to consider commutation rules
    • Rearranged existing code to streamline and improve readability.
    • Restructuring of optimizer to check if the next command is commutable with the current command, or if it may be part of a commutable circuit.
  2. Update _basics.py and _basics_test.py
    • Added attributes to do with commutation rules.
  3. Update _command.py and _command_test.py
    • Added commutation attributes and overlap function to streamline optimizer code.
  4. Update _gates.py and _gates_test.py
    • Added attributes to do with commutation rules.
    • Updated Rxx, Ryy, Rzz to have interchangeable qubit indices.
  5. Update _metagates.py
    • Added commutable rule.
  6. Addition of _relative_command.py and _relative_command_test.py
    • Dummy command which can be used without an engine, used to define commutation rules.
  7. Update restricted_gate_set.py
    • By default set apply_commutation = True
Takishima commented 3 years ago

Thank you for your contribution, I will review it next week. In the meantime, you could make sure that the docstring of all functions follow the current style used in ProjectQ (essentially the Google docstring style)

daisyrainsmith commented 3 years ago

Hi Takishima, I was just wondering if you've had a chance to look at the latest commits? I hope you haven't been waiting on me for something because I was unaware. Thanks for all your help so far! Daisy.

Takishima commented 3 years ago

Sorry, I was a bit busy with the other PRs and some other projects at work. I will look at your latest changes in the coming days.

Be aware, I will not merge any PR until #395 gets merged since that one introduces a few important changes in the way the project is configured.

daisyrainsmith commented 3 years ago

No worries, just wanted to check we weren't unwittingly both waiting on each other. Thanks, Daisy.

Takishima commented 3 years ago

Dear @daisyrainsmith,

I have taken a deeper look at your contribution today and things look good for the most part. There are still a few places where I would like to see some cleanup like in some of your test functions that are a bit overcrowded for a lack of a better word.

But more importantly, the current test suite does not seem to cover all the code you have improved upon. I do not have time right now to look in more details to see if we should increase coverage in those locations, but here is the result of me running pytest-cov on the latest commit to date. Note in particular the cases for projectq/ops/_relative_command.py and projectq/cengines/_optimize.py where I think most of your modifications lie.

---------- coverage: platform darwin, python 3.9.5-final-0 -----------
Name                                                          Stmts   Miss  Cover   Missing
-------------------------------------------------------------------------------------------
projectq/__init__.py                                              3      0   100%
projectq/_version.py                                              2      0   100%
projectq/backends/__init__.py                                     8      0   100%
projectq/backends/_aqt/__init__.py                                1      0   100%
projectq/backends/_aqt/_aqt.py                                  157      0   100%
projectq/backends/_aqt/_aqt_http_client.py                      109      0   100%
projectq/backends/_awsbraket/__init__.py                          2      0   100%
projectq/backends/_awsbraket/_awsbraket.py                      158      0   100%
projectq/backends/_awsbraket/_awsbraket_boto3_client.py         165      0   100%
projectq/backends/_circuits/__init__.py                           4      0   100%
projectq/backends/_circuits/_drawer.py                           89      0   100%
projectq/backends/_circuits/_drawer_matplotlib.py                82      0   100%
projectq/backends/_circuits/_plot.py                            162      0   100%
projectq/backends/_circuits/_to_latex.py                        364      5    99%   60-61, 79-81
projectq/backends/_ibm/__init__.py                                1      0   100%
projectq/backends/_ibm/_ibm.py                                  169      0   100%
projectq/backends/_ibm/_ibm_http_client.py                      171      0   100%
projectq/backends/_printer.py                                    47      1    98%   106
projectq/backends/_resource.py                                   88      0   100%
projectq/backends/_sim/__init__.py                                2      0   100%
projectq/backends/_sim/_classical_simulator.py                  105      0   100%
projectq/backends/_sim/_pysim.py                                243      1    99%   27
projectq/backends/_sim/_simulator.py                            135      3    98%   39-41
projectq/cengines/__init__.py                                    13      0   100%
projectq/cengines/_basicmapper.py                                38      0   100%
projectq/cengines/_basics.py                                     61      0   100%
projectq/cengines/_cmdmodifier.py                                 9      0   100%
projectq/cengines/_ibm5qubitmapper.py                            77      0   100%
projectq/cengines/_linearmapper.py                              318      0   100%
projectq/cengines/_main.py                                       96      2    98%   196-197
projectq/cengines/_manualmapper.py                               16      0   100%
projectq/cengines/_optimize.py                                  266     18    93%   75-76, 135-137, 166-168, 209, 274-275, 375-377, 380-382, 386, 494, 549
projectq/cengines/_replacer/__init__.py                           3      0   100%
projectq/cengines/_replacer/_decomposition_rule.py               12      0   100%
projectq/cengines/_replacer/_decomposition_rule_set.py           31      1    97%   70
projectq/cengines/_replacer/_replacer.py                         68      0   100%
projectq/cengines/_swapandcnotflipper.py                         59      0   100%
projectq/cengines/_tagremover.py                                 13      0   100%
projectq/cengines/_testengine.py                                 64      0   100%
projectq/cengines/_twodmapper.py                                335      0   100%
projectq/libs/__init__.py                                         0      0   100%
projectq/libs/hist/__init__.py                                    2      0   100%
projectq/libs/hist/_histogram.py                                 32      0   100%
projectq/libs/math/__init__.py                                    2      0   100%
projectq/libs/math/_constantmath.py                              59      0   100%
projectq/libs/math/_default_rules.py                             90      0   100%
projectq/libs/math/_gates.py                                    174      0   100%
projectq/libs/math/_quantummath.py                              188      0   100%
projectq/libs/revkit/__init__.py                                  3      0   100%
projectq/libs/revkit/_control_function.py                        28     22    21%   55-69, 81-112, 120-121
projectq/libs/revkit/_permutation.py                             20     14    30%   48-51, 61-84, 92-94
projectq/libs/revkit/_phase.py                                   28     22    21%   59-73, 83-114, 122-123
projectq/libs/revkit/_utils.py                                    3      2    33%   25-26
projectq/meta/__init__.py                                         8      0   100%
projectq/meta/_compute.py                                       156      0   100%
projectq/meta/_control.py                                        38      0   100%
projectq/meta/_dagger.py                                         37      0   100%
projectq/meta/_dirtyqubit.py                                      6      0   100%
projectq/meta/_logicalqubit.py                                    8      0   100%
projectq/meta/_loop.py                                           89      0   100%
projectq/meta/_util.py                                           10      0   100%
projectq/ops/__init__.py                                         12      0   100%
projectq/ops/_basics.py                                         172      1    99%   146
projectq/ops/_command.py                                        114      0   100%
projectq/ops/_gates.py                                          212      1    99%   386
projectq/ops/_metagates.py                                       87      0   100%
projectq/ops/_qaagate.py                                          8      0   100%
projectq/ops/_qftgate.py                                          5      0   100%
projectq/ops/_qpegate.py                                          7      0   100%
projectq/ops/_qubit_operator.py                                 235      0   100%
projectq/ops/_relative_command.py                                31     15    52%   77, 86-100
projectq/ops/_shortcuts.py                                        8      0   100%
projectq/ops/_state_prep.py                                      15      0   100%
projectq/ops/_time_evolution.py                                  68      0   100%
projectq/ops/_uniformly_controlled_rotation.py                   56      0   100%
projectq/setups/__init__.py                                       0      0   100%
projectq/setups/aqt.py                                           27      0   100%
projectq/setups/awsbraket.py                                     22      0   100%
projectq/setups/decompositions/__init__.py                        2      0   100%
projectq/setups/decompositions/amplitudeamplification.py         25      0   100%
projectq/setups/decompositions/arb1qubit2rzandry.py              86      0   100%
projectq/setups/decompositions/barrier.py                         8      0   100%
projectq/setups/decompositions/carb1qubit2cnotrzandry.py        106      0   100%
projectq/setups/decompositions/cnot2cz.py                        14      0   100%
projectq/setups/decompositions/cnot2rxx.py                       24      0   100%
projectq/setups/decompositions/cnu2toffoliandcu.py               31      0   100%
projectq/setups/decompositions/crz2cxandrz.py                    16      0   100%
projectq/setups/decompositions/entangle.py                       12      0   100%
projectq/setups/decompositions/globalphase.py                     9      0   100%
projectq/setups/decompositions/h2rx.py                           18      0   100%
projectq/setups/decompositions/ph2r.py                           13      0   100%
projectq/setups/decompositions/phaseestimation.py                23      0   100%
projectq/setups/decompositions/qft2crandhadamard.py              15      0   100%
projectq/setups/decompositions/qubitop2onequbit.py               24      0   100%
projectq/setups/decompositions/r2rzandph.py                      12      0   100%
projectq/setups/decompositions/rx2rz.py                          16      0   100%
projectq/setups/decompositions/ry2rz.py                          17      0   100%
projectq/setups/decompositions/rz2rx.py                          26      0   100%
projectq/setups/decompositions/sqrtswap2cnot.py                  17      0   100%
projectq/setups/decompositions/stateprep2cnot.py                 51      0   100%
projectq/setups/decompositions/swap2cnot.py                      13      0   100%
projectq/setups/decompositions/time_evolution.py                 63      0   100%
projectq/setups/decompositions/toffoli2cnotandtgate.py           28      0   100%
projectq/setups/decompositions/uniformlycontrolledr2cnot.py      65      0   100%
projectq/setups/default.py                                        7      0   100%
projectq/setups/grid.py                                          59      0   100%
projectq/setups/ibm.py                                           42      0   100%
projectq/setups/linear.py                                        59      0   100%
projectq/setups/restrictedgateset.py                             95      0   100%
projectq/setups/trapped_ion_decomposer.py                        45      0   100%
projectq/tests/__init__.py                                        0      0   100%
projectq/types/__init__.py                                        1      0   100%
projectq/types/_qubit.py                                         72      0   100%
-------------------------------------------------------------------------------------------
TOTAL                                                          6920    108    98%
daisyrainsmith commented 2 years ago

Hi Takishima,

Sorry I've been away from this project for a while, I've been busy with other things. Could you let me know what needs doing before my commit can be merged into develop. I don't know if anything has changed while I've been away.

Thanks, Daisy.

Takishima commented 2 years ago

Hi @daisyrainsmith ,

Right now, you probably need to rebase this branch onto the current develop in order to get all the latest changes.

Also, my last comment here about test coverage still stands.

daisyrainsmith commented 2 years ago

Hi Takishima,

I have submitted a new Pull Request because it was too difficult to rebase to I have submitted a simple merge between my commutation update and your latest develop branch.

If that's alright, please close this pull request and we'll move to the other one.

Thanks,

Daisy.

Takishima commented 2 years ago

Ok, I'll close this one then.