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

[Return-types Fix #4] Update Hamiltonian expand transform #3232

Merged
merged 8 commits into from
Oct 31, 2022

Conversation

eddddddy
Copy link
Contributor

Context:
Part of the ongoing return types project

Description of the Change:
Update the hamiltonian_expand transform to work with the new return types.

@github-actions
Copy link
Contributor

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

@codecov
Copy link

codecov bot commented Oct 27, 2022

Codecov Report

Merging #3232 (761ae5a) into master (5f332df) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #3232   +/-   ##
=======================================
  Coverage   99.70%   99.70%           
=======================================
  Files         277      277           
  Lines       24542    24544    +2     
=======================================
+ Hits        24469    24471    +2     
  Misses         73       73           
Impacted Files Coverage Δ
pennylane/transforms/hamiltonian_expand.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Contributor

@rmoyard rmoyard left a comment

Choose a reason for hiding this comment

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

Thanks for the fix @eddddddy, it is looking good! 💯 Do you know if we should worry about multiple measurement and expectations values of different hamiltonian or different measurements? (see the example below). I think it should be separated in two different cases but I am not totally sure.

@qnode(dev, diff_method=diff_method, mode=mode, max_diff=max_diff)
def circuit(data, weights, coeffs):
   weights = weights.reshape(1, -1)
   qml.templates.AngleEmbedding(data, wires=[0, 1])
   qml.templates.BasicEntanglerLayers(weights, wires=[0, 1])
   H = qml.Hamiltonian(coeffs, obs)
   H.compute_grouping()
   H_2= qml.Hamiltonian(coeffs_2, obs_2)
   H_2.compute_grouping()
   return qml.expval(H), qml.expval(H2)

@eddddddy
Copy link
Contributor Author

eddddddy commented Oct 28, 2022

Hi @rmoyard, it seems that we don't have to worry about multiple Hamiltonians being measured:

try:
circuits, hamiltonian_fn = qml.transforms.hamiltonian_expand(circuit, group=False)
except ValueError as e:
raise ValueError(
"Can only return the expectation of a single Hamiltonian observable"
) from e

Running your code, we get the following Exception:

Traceback (most recent call last):
  File "C:\Users\Edward\Documents\pennylane\pennylane\_device.py", line 738, in batch_transform
    circuits, hamiltonian_fn = qml.transforms.hamiltonian_expand(circuit, group=False)
  File "C:\Users\Edward\Documents\pennylane\pennylane\transforms\hamiltonian_expand.py", line 128, in hamiltonian_expand
    raise ValueError(
ValueError: Passed tape must end in `qml.expval(H)`, where H is of type `qml.Hamiltonian`

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "../scripts/return.py", line 159, in <module>
    main()
  File "../scripts/return.py", line 154, in main
    debug2()
  File "../scripts/return.py", line 146, in debug2
    circuit()
  File "C:\Users\Edward\Documents\pennylane\pennylane\qnode.py", line 622, in __call__
    res = qml.execute(
  File "C:\Users\Edward\Documents\pennylane\pennylane\interfaces\execution.py", line 575, in execute
    return _execute_new(
  File "C:\Users\Edward\Documents\pennylane\pennylane\interfaces\execution.py", line 347, in _execute_new
    tapes, batch_fn = qml.transforms.map_batch_transform(device.batch_transform, tapes)
  File "C:\Users\Edward\Documents\pennylane\pennylane\transforms\batch_transform.py", line 471, in map_batch_transform
    new_tapes, fn = transform(t)
  File "C:\Users\Edward\Documents\pennylane\pennylane\_device.py", line 741, in batch_transform
    raise ValueError(
ValueError: Can only return the expectation of a single Hamiltonian observable

@rmoyard rmoyard self-requested a review October 28, 2022 15:09
Copy link
Contributor

@rmoyard rmoyard left a comment

Choose a reason for hiding this comment

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

Thanks @eddddddy 💯

Copy link
Contributor

@antalszava antalszava left a comment

Choose a reason for hiding this comment

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

@eddddddy a nice fix! 👏 Just 2 questions.

pennylane/transforms/hamiltonian_expand.py Show resolved Hide resolved
tests/returntypes/test_autograd_qnode_new.py Show resolved Hide resolved
@eddddddy eddddddy requested a review from antalszava October 31, 2022 16:57
Copy link
Contributor

@antalszava antalszava left a comment

Choose a reason for hiding this comment

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

Looks good! 👍 Thanks for the answers.

@eddddddy eddddddy merged commit 993a163 into master Oct 31, 2022
@eddddddy eddddddy deleted the ham_expand_return branch October 31, 2022 20:41
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.

3 participants