Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pr swap based transpiler #2118

Merged
merged 21 commits into from
Mar 24, 2022

Conversation

mauriceweber
Copy link
Contributor

@mauriceweber mauriceweber commented Jan 21, 2022

Before submitting

Please complete the following checklist when submitting a PR:

  • All new features must include a unit test.
    If you've fixed a bug or added code that should be tested, add a test to the
    test directory!

  • All new functions and code must be clearly commented and documented.
    If you do make documentation changes, make sure that the docs build and
    render correctly by running make docs.

  • Ensure that the test suite passes, by running make test.

  • Add a new entry to the doc/releases/changelog-dev.md file, summarizing the
    change, and including a link back to the PR.

  • The PennyLane source code conforms to
    PEP8 standards.
    We check all of our code against Pylint.
    To lint modified files, simply pip install pylint, and then
    run pylint pennylane/path/to/file.py.

When all the above are checked, delete everything above the dashed
line and fill in the pull request template.


Context:
Add a swap-based router transform

Description of the Change:
new module transpile.py in pennylane.transforms; the transpile function takes as input a tape and hardware connectivity graph, and compiles the tape to ensure that it can be executed on the corresponding hardware.

Related GitHub Issues:
#2115

@josh146 josh146 self-requested a review January 21, 2022 14:59
@josh146
Copy link
Member

josh146 commented Jan 21, 2022

Thanks @mauriceweber --- I skimmed through quickly, very thorough documentation! I'll leave a review shortly :)

@josh146 josh146 linked an issue Jan 21, 2022 that may be closed by this pull request
@codecov
Copy link

codecov bot commented Jan 21, 2022

Codecov Report

Merging #2118 (208d45e) into master (0814da9) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #2118   +/-   ##
=======================================
  Coverage   99.42%   99.42%           
=======================================
  Files         243      244    +1     
  Lines       18338    18399   +61     
=======================================
+ Hits        18232    18293   +61     
  Misses        106      106           
Impacted Files Coverage Δ
pennylane/transforms/__init__.py 100.00% <100.00%> (ø)
pennylane/transforms/transpile.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0814da9...208d45e. Read the comment docs.

Copy link
Member

@josh146 josh146 left a comment

Choose a reason for hiding this comment

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

@mauriceweber thank you, this is a great implementation! I went through and left some comments, mostly relating to documentation and testing :)

For the testing, Codecov raised some places where parts of the logic is not being tested, so it might be good to add some additional unit tests, checking that this works for various circuit edge cases (e.g., for circuits that return qml.probs()).

A final test that could be very nice to have: a test that ensures that after a circuit is transpiled, it remains differentiable. This is a good 'sanity check' test, since it can be very easy to accidentally break the differentiability when manipulating operations!

pennylane/transforms/transpile.py Outdated Show resolved Hide resolved
pennylane/transforms/transpile.py Outdated Show resolved Hide resolved
pennylane/transforms/__init__.py Show resolved Hide resolved
pennylane/transforms/transpile.py Outdated Show resolved Hide resolved
pennylane/transforms/transpile.py Outdated Show resolved Hide resolved
tests/transforms/test_transpile.py Outdated Show resolved Hide resolved
tests/transforms/test_transpile.py Show resolved Hide resolved
pennylane/transforms/transpile.py Outdated Show resolved Hide resolved
pennylane/transforms/transpile.py Outdated Show resolved Hide resolved
pennylane/transforms/transpile.py Outdated Show resolved Hide resolved
@josh146
Copy link
Member

josh146 commented Mar 14, 2022

Hey @mauriceweber, I just wanted to check in here and see on the status of this PR :) Was this something you might have time to complete?

@mauriceweber
Copy link
Contributor Author

Hi @josh146, yep I am planning to finish this still! The main thing left to do I think is to improve the functionality of the _adjust_mmt_indices function -- it currently doesn't support expectation values of tensor products and Hamiltonians. I will look into how we can achieve this:)

@josh146
Copy link
Member

josh146 commented Mar 15, 2022

Great to hear @mauriceweber!

The main thing left to do I think is to improve the functionality of the _adjust_mmt_indices function -- it currently doesn't support expectation values of tensor products and Hamiltonians. I will look into how we can achieve this:)

This is completely up to you, I might suggest not including this in the first version of this functionality, as the Tensor class and the Hamiltonian class are likely going to be 'merged' into the main Operator class going forward!

If it is a quick and easy change, though, that would be fine. Otherwise, the following is also okay:

  • We can open an issue detailing the need to be able to easily map the wires of tensor product/Hamiltonian objects

  • In this PR, we add a .. warning:: box near the top of the docstring, informing readers that this function does not yet work for tensors/Hamiltonians.

  • Within the code, we raise an error (for now) in those two cases.

@mauriceweber
Copy link
Contributor Author

I couldn't find an easy fix for that, so let us not include it in the first version.

In this PR, we add a .. warning:: box near the top of the docstring, informing readers that this function does not yet work for tensors/Hamiltonians.

This is already included:

.. warning::
This transform does not yet support measurements of Hamiltonians or tensor products of observables. If a circuit
is passed which contains these types of measurements, a NotImplementedError will be raised.

Within the code, we raise an error (for now) in those two cases.

This is also implemented already (both in the _adjust_mmt_indices function and in the transpile function itself):

if isinstance(_m.obs, (Hamiltonian, Tensor)):
# tensor products of observables
raise NotImplementedError(
"Measuring expectation values of tensor products or Hamiltonians is not yet supported"
)

if any(isinstance(m.obs, (Hamiltonian, Tensor)) for m in tape.measurements):
raise NotImplementedError(
"Measuring expectation values of tensor products or Hamiltonians is not yet supported"
)

@josh146
Copy link
Member

josh146 commented Mar 16, 2022

My mistake @mauriceweber, apologies!

I'll give it a final review, I just noticed that the tests aren't currently running due to the following issue:

 tests/transforms/test_transpile.py:6: in <module>
    import regex as re
E   ModuleNotFoundError: No module named 'regex'

I think you can remove this import, and simply write the test as follows:

err_msg = "Measuring expectation values of tensor products or Hamiltonians is not yet supported"
with pytest.raises(NotImplementedError, match=err_msg):
    transpiled_qnode()

@mauriceweber
Copy link
Contributor Author

mauriceweber commented Mar 16, 2022

cool, thanks @josh146 !:)

I think you can remove this import, and simply write the test as follows:

Ah I see, I removed all the regex dependencies now, so that the tests should go through (had to remove some special characters from one error message)

@josh146 josh146 marked this pull request as ready for review March 16, 2022 17:12
@mauriceweber
Copy link
Contributor Author

Hi @josh146 , I can see that some changes were requested but can't find what these are. Can you point me to the place where I can see the requested changes?

And also, it seems that the tests stalled. Do we need to retrigger them?

@josh146 josh146 self-requested a review March 17, 2022 10:10
@josh146
Copy link
Member

josh146 commented Mar 17, 2022

I can see that some changes were requested but can't find what these are. Can you point me to the place where I can see the requested changes?

Hey @mauriceweber, I think this is from my old review - let me clear that for you :) I will find some time in the next two days or so to go through and give it a fresh review!

And also, it seems that the tests stalled. Do we need to retrigger them?

That is very odd 🤔 I'm not sure why that has happened, I will need to investigate that.

Copy link
Member

@josh146 josh146 left a comment

Choose a reason for hiding this comment

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

@mauriceweber fantastic work! This looks almost ready for merging now 🙂

Once the __main__ block is removed, and the other suggestions addressed, let me know and I can merge this in 💯

doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
pennylane/transforms/transpile.py Outdated Show resolved Hide resolved
pennylane/transforms/transpile.py Outdated Show resolved Hide resolved
Comment on lines 173 to 177
if isinstance(_m.obs, (Hamiltonian, Tensor)):
# tensor products of observables
raise NotImplementedError(
"Measuring expectation values of tensor products or Hamiltonians is not yet supported"
)
Copy link
Member

Choose a reason for hiding this comment

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

@mauriceweber I don't think this error can ever be raised, since such cases should be caught by the exception on line 94? In which case, this can be removed :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh yep, that won't be raised -- I removed it:)

Comment on lines 187 to 188
if __name__ == "__main__":
import pennylane as qml
Copy link
Member

Choose a reason for hiding this comment

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

While convenient for testing and prototyping, this section should be removed prior to merging!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh totally, I forgot to remove it after testing!

@josh146 josh146 merged commit c121d9c into PennyLaneAI:master Mar 24, 2022
@josh146
Copy link
Member

josh146 commented Mar 24, 2022

Thanks for this awesome feature @mauriceweber!

@mauriceweber
Copy link
Contributor Author

thanks for the support and the code reviews @josh146 !:)

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.

Add a swap-based router transform
3 participants