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
added feature to decompose hamiltonians to Pauli matrices #535
added feature to decompose hamiltonians to Pauli matrices #535
Conversation
Thanks for the pull request @rahul1704! |
Hey @co9olguy! I saw it failed a few tests, so I was unsure if I should keep it open and fix it or close it, for now, fix the issues and then create a new one. Let me know which is preferable |
No problem to keep open. Work in progress PRs often fail tests early on :) |
Codecov Report
@@ Coverage Diff @@
## decompose-hamiltonian #535 +/- ##
========================================================
Coverage ? 98.93%
========================================================
Files ? 83
Lines ? 5158
Branches ? 0
========================================================
Hits ? 5103
Misses ? 55
Partials ? 0 Continue to review full report at Codecov.
|
Hi @rahul1704, it appears that some of the checks are still failing:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this @rahul1704! I've left some comments and suggestions. Don't forget to also update the .github/CHANGELOG.md
file with the change, a link back to this PR, and to add your name to the contributors list!
if len(H) - 2 ** N != 0: | ||
raise ValueError("Hamiltonian should be in the form (n^2 x n^2), for any n>=1") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! Very useful to have this dimension validation.
) | ||
for term in zip(coeff, matrices): | ||
result = result + term[0] * term[1] | ||
assert (H == result).all |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good first attempt for a functional test, however, it isn't sufficient --- additional unit and functional tests should be added. These include:
-
Working out, by hand, the decompositions of several different Hamiltonians. Then, run the test (using
@pytest.mark.parametrize()
) on this test data, verifying that the expected result is returned. -
Checking that
coeffs
is a list of floats, -
Checking that
len(coeffs) == len(obs_list)
, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you give me an example of a Hamiltonian that must be decomposed into its component parts? Or I can actually find that myself. But would any Hermitian matrix suffice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @yishairasowsky, yes, any Hermitian matrix will suffice!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i got an error when i tried to push...
remote: Permission to XanaduAI/pennylane.git denied to yishairasowsky.
fatal: unable to access 'https://github.com/XanaduAI/pennylane.git/': The requested URL returned error: 403
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @yishairasowsky, that's no problem. For external contributors, it's not possible to directly push to the repo. You'll can push to a fork of PennyLane which is on your own GitHub account, then make a pull request from there to github.com/XanaduAI's decompose-hamiltonian
branch
|
||
@pytest.mark.parametrize("H", test_Hamiltonians) | ||
def test_decomp(self, H): | ||
"""Tests that decompose_hamiltonian successfully decomposes Hamiltonians into Pauli matrices""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This docstring can be made more specific --- what are we explicitly comparing? How do we determine the decomposition was successful?
@@ -63,6 +65,33 @@ | |||
U_toffoli[6:8, 6:8] = np.array([[0, 1], [1, 0]]) | |||
|
|||
|
|||
test_Hamiltonians = [np.array([[2.5, -0.5], [-0.5, 2.5]]), np.array(np.diag([0, 0, 0, 1]))] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to also consider multi-qubit cases, as well as Hermitian matrices with complex off-diagonal entries.
result = np.zeros((2 ** N, 2 ** N)) | ||
matrices = [] | ||
for term in combs: | ||
base = [qml.Identity] * N | ||
if term.num_wires == N: | ||
matrices.append(term.matrix) | ||
else: | ||
base[term.wires[0]] = term.__class__ | ||
matrices.append( | ||
functools.reduce(operator.matmul, [t(i) for i, t in enumerate(base)]).matrix | ||
) | ||
for term in zip(coeff, matrices): | ||
result = result + term[0] * term[1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is quite neat; essentially you are decomposing a Hamiltonian using the function, and then evaluating the linear combination by making use of the observable.matrix
property. Nice! However, it could be done in a slightly cleaner manner, without re-using the logic from the function to be tested, like so:
mats = []
for c, o in zip(coeff, obs_list):
base = [np.identity(2)] * N
if isinstance(o, qml.operation.Tensor):
for obs in o.obs:
base[obs.wires[0]] = obs.matrix
else:
base[o.wires[0]] = o.matrix
mats.append(c*functools.reduce(np.kron, base))
result = np.linalg.multi_dot(mats)
Note: I have note checked the above for correctness, it could need adjusting
@rahul1704, how is everything going on this PR? Is it something you would like to continue working on? |
@yishairasowsky This PR got part way, but appears to have stalled out. If you're interested (and depending on your level of comfort), it might make a good candidate to help carry across the finish line |
i will look into it. let me check each reviewer comment, and if it is not
addressed already, i will try to do so.
…On Mon, Apr 27, 2020 at 10:18 PM Nathan Killoran ***@***.***> wrote:
@yishairasowsky <https://github.com/yishairasowsky> This PR got part way,
but appears to have stalled out. If you're interested (and depending on
your level of comfort), it might make a good candidate to help carry across
the finish line
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#535 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJN7CN23MPBNHA76BG2A2NTROXK7XANCNFSM4LDMIJ6A>
.
--
Yishai Rasowsky
054.848.2245
Visit my Shiurim
<https://torahdownloads.com/s-437-rabbi-yishai-rasowsky.html> | Thesis
<https://www.amherst.edu/media/view/58703/original/jesse_thesis.pdf> |
Workplace <https://www.smrtflow.com/> | Github
<https://github.com/yishairasowsky/info_about_your_location> | Linked-In
<https://www.linkedin.com/in/yishai-rasowsky-a28189164/>
|
i am trying to open the version that @rahul1704 made with the incoming changes for hamiltionian. but i can't find that branch. when i type https://github.com/yishairasowsky/pennylane/tree/hamiltonianDecomp into my address bar, i et a 404 not found. |
@yishairasowsky Amazing, thanks for helping out! 😃 |
Hi @yishairasowsky, thanks for helping out on this! Does the following work for you? git clone https://github.com/rahul1704/pennylane.git
cd pennylane
git checkout hamiltonianDecomp |
ok thanks for this tip!
i typed it in, but then i received an error:
*fatal: destination path 'pennylane' already exists and is not an empty
directory.*
i guess because i already forked the repo.
so maybe i should delete my own copy from my computer, and then retype the
commands you gave me.
yishai
…On Fri, May 1, 2020 at 6:00 AM Josh Izaac ***@***.***> wrote:
Hi @yishairasowsky <https://github.com/yishairasowsky>, thanks for
helping out on this!
Does the following work for you?
git clone https://github.com/rahul1704/pennylane.gitcd pennylanegit checkout hamiltonianDecomp
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#535 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJN7CN7YT36BERT255GVSALRPI3M7ANCNFSM4LDMIJ6A>
.
--
Yishai Rasowsky
054.848.2245
Visit my Shiurim
<https://torahdownloads.com/s-437-rabbi-yishai-rasowsky.html> | Thesis
<https://www.amherst.edu/media/view/58703/original/jesse_thesis.pdf> |
Workplace <https://www.smrtflow.com/> | Github
<https://github.com/yishairasowsky/info_about_your_location> | Linked-In
<https://www.linkedin.com/in/yishai-rasowsky-a28189164/>
|
@josh146 thanks for your help. |
Hi @yishairasowsky, I've merged this WIP PR into the branch
|
Hi @josh146! Thanks a lot. Just checking: To make sure I am up to date, I just made a pull. That is sufficient, right? |
Yes, if you are successfully able to pull from the example (assuming you are on a local clone of pennylane):
|
excellent, thank you Nathan!
…On Wed, May 6, 2020 at 8:13 PM Nathan Killoran ***@***.***> wrote:
Yes, if you are successfully able to pull from the decompose-hamiltonian
branch Josh just created, you should be good to go!
example (assuming you are on a local fork of pennylane):
git fetch
git checkout decompose-hamiltonian
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#535 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJN7CN4GQLWNTUNB47SPSUDRQGLBZANCNFSM4LDMIJ6A>
.
--
Yishai Rasowsky
054.848.2245
Visit my Shiurim
<https://torahdownloads.com/s-437-rabbi-yishai-rasowsky.html> | Thesis
<https://www.amherst.edu/media/view/58703/original/jesse_thesis.pdf> |
Workplace <https://www.smrtflow.com/> | Github
<https://github.com/yishairasowsky/info_about_your_location> | Linked-In
<https://www.linkedin.com/in/yishai-rasowsky-a28189164/>
|
sry! when i clone my fork, then try git checkout decompose-hamiltonian, i get an error: error: pathspec 'decompose-hamiltonian' did not match any file(s) known to git |
OK, i see now it is OK. |
You got it :) |
i wrote git remote add pennylane https://github.com/yishairasowsky/pennylane.git in order that my pushes should go to my fork. but still when i type git push, i get the error |
Looks like |
thanks! hmm, i get |
another thing i tried and failed |
The tricky thing about If you do |
Just for clarity, your above attempts have likely created the aliases Check out https://stackoverflow.com/a/18801178/4680447 for some details, but the |
OK, thanks! hmm, i am getting
did i goof something? |
i made and pushed a couple changes, but then i see that there are no
changes apparent for a new PR...
decompose-hamiltonian...yishairasowsky:decompose-hamiltonian
…On Fri, May 8, 2020 at 12:18 AM Nathan Killoran ***@***.***> wrote:
Just for clarity, your above attempts have likely created the aliases
origin and pennylane-yishai for the remote server
https://github.com/yishairasowsky/pennylane. But your default target for
push still seems to be the official pennylane
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#535 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJN7CN7FMU6MMTZYFD3JG5TRQMQSHANCNFSM4LDMIJ6A>
.
--
Yishai Rasowsky
054.848.2245
Visit my Shiurim
<https://torahdownloads.com/s-437-rabbi-yishai-rasowsky.html> | Thesis
<https://www.amherst.edu/media/view/58703/original/jesse_thesis.pdf> |
Workplace <https://www.smrtflow.com/> | Github
<https://github.com/yishairasowsky/info_about_your_location> | Linked-In
<https://www.linkedin.com/in/yishai-rasowsky-a28189164/>
|
* added feature to decompose hamiltonians to Pauli matrices (#535) * addded hamiltonians decomp feature * reformatted utils.py and tests/test_utils.py * Apply suggestions from code review * Apply suggestions from code review * Update utils.py * Update qml_utils.rst * Apply suggestions from code review Co-authored-by: Rahul Shekhawat <rahul@Rahuls-MacBook-Pro.local> Co-authored-by: Rahul Shekhawat <rahul@Rahuls-MBP.mshome.net> Co-authored-by: Nathan Killoran <co9olguy@users.noreply.github.com> Co-authored-by: Josh Izaac <josh146@gmail.com> * Updated code for decomposition; added tests * Fixed formatting * Update pennylane/utils.py Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com> * Update pennylane/utils.py Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com> * Update pennylane/utils.py Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com> * Update tests/test_utils.py Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com> * Updates following PR * Moved hide_identity usage * Updated docstring * Update pennylane/utils.py Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com> * Added 2 tests * Update pennylane/utils.py Co-authored-by: Josh Izaac <josh146@gmail.com> * Updated test * Updated and added tests * Update pennylane/utils.py Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com> * Update pennylane/utils.py Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com> * Update pennylane/utils.py Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com> * Update tests/test_utils.py Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com> * Updated test string * Updated exception message * Refactored shape check * Updated tests descriptions * Reformatted * Updated CHANGELOG * Update tests/test_utils.py Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com> * Update tests/test_utils.py Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com> * Update tests/test_utils.py Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com> Co-authored-by: Rahul Shekhawat <rahul170492@gmail.com> Co-authored-by: Rahul Shekhawat <rahul@Rahuls-MacBook-Pro.local> Co-authored-by: Rahul Shekhawat <rahul@Rahuls-MBP.mshome.net> Co-authored-by: Nathan Killoran <co9olguy@users.noreply.github.com> Co-authored-by: Josh Izaac <josh146@gmail.com> Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com> Co-authored-by: trbromley <brotho02@gmail.com>
hi @co9olguy |
Hi @yishairasowsky, if you're interested in employment opportunities at Xanadu, you should check out the opportunities on the careers page. GitHub is better used for discussing contributions to the open-source library. Good luck with your job search :) |
you are right, of course. thank you!
…On Wed, Nov 11, 2020 at 8:01 PM Nathan Killoran ***@***.***> wrote:
Hi @yishairasowsky <https://github.com/yishairasowsky>, if you're
interested in employment opportunities at Xanadu, you should check out the
opportunities on the careers page <https://secure.collage.co/jobs/xanadu>.
GitHub is better used for discussing contributions to the open-source
library
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#535 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJN7CNZ55DO77RVQGZI3ZTLSPLGQ7ANCNFSM4LDMIJ6A>
.
--
Yishai Rasowsky
054.848.2245
Audio classes here <https://anchor.fm/yishai-rasowsky>.
|
Context: Currently, if a user has a numerical matrix, they must either use
qml.Hermitian
in a single QNode (causing additional classical processing/requiring full subsystem rotations), or decompose it manually into Pauli operators to be used withqml.map()
.Description of the Change: Adds a function that allows users to easily decompose their Hamiltonian into Pauli operators.
Benefits: Allows numeric Hamiltonians to be easily used with
qml.map()
.Possible Drawbacks: n/a
Related GitHub Issues:
Closes #527
Provides another solution to #553