question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Derivatives of `qml.CommutingEvolution`

See original GitHub issue

In cleaning warnings from the test suite, I found this warning:

templates/test_subroutines/test_commuting_evolution.py::TestGradients::test_differentiable_hamiltonian
  /Users/christina/Prog/pl/lib/python3.8/site-packages/autograd/tracer.py:14: UserWarning: Output seems independent of input.
    warnings.warn("Output seems independent of input.")

The effect of CommutingEvolution on the state did not change with the time.
This problems seems to stem from commuting_evolution.py Line 160:

        return qml.templates.ApproxTimeEvolution(hamiltonian, time_and_coeffs[1], 1).decomposition()

This line incorrectly indexes into time_and_coeffs to try and extract the time. It should instead use time_and_coeffs[0].

Upon fixing that bug, we now find that parameter shift no longer works for CommutingEvolution.

These two tests start to fail:

==================================================================================== short test summary info ====================================================================================
FAILED templates/test_subroutines/test_commuting_evolution.py::TestGradients::test_two_term_case - assert False
FAILED templates/test_subroutines/test_commuting_evolution.py::TestGradients::test_four_term_case - assert False

The tests that fail manually specify the frequencies. If the frequencies are specified, the operation sets its own grad_recipe. This grad_recipe gives incorrect results. I have yet to track down the problem with the grad_recipe property.

In summary, fixing the bug in the creation of the ApproxTimeEvolution operation allows finite difference derivatives and derivatives for operations without specified frequencies to work correctly. Since the finite-difference derivatives are now correct, we can see that the parameter-shift gradients never worked correctly.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
licedriccommented, May 2, 2022

In fact the evaluation of qml.CommutingEvolution is now incorrect. Running the example code in the comment at https://github.com/PennyLaneAI/pennylane/blob/master/pennylane/templates/subroutines/commuting_evolution.py#L103

import pennylane as qml
n_wires = 2
dev = qml.device('default.qubit', wires=n_wires)
coeffs = [1, -1]
obs = [qml.PauliX(0) @ qml.PauliY(1), qml.PauliY(0) @ qml.PauliX(1)]
hamiltonian = qml.Hamiltonian(coeffs, obs)
frequencies = (2, 4)
@qml.qnode(dev)
def circuit(time):
    qml.PauliX(0)
    qml.CommutingEvolution(hamiltonian, time, frequencies)
    return qml.expval(qml.PauliZ(0))
>>> circuit(1)
0.6536436208636115

Gives 0.14550003 instead.

1reaction
josh146commented, Apr 28, 2022

Oof, really nice find @albi3ro. Since qml.CommutingEvolution is really integral to QAOA workflows, I would say that this is a very high priority bug.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Release notes — PennyLane 0.28.0 documentation
Fixed a bug with the decomposition of qml.CommutingEvolution . (#2542). Fixed a bug enabling PennyLane to work with the latest version of Autoray....
Read more >
pennylane Changelog - pyup.io
Fixed a bug where `qml.math.fidelity(non_trainable_state, trainable_state)` failed ... If set to `True`, broadcasting is used to compute the derivative:
Read more >
PennyLaneAI/pennylane v0.20.0 on GitHub - NewReleases.io
The QNode has been re-written to support batch execution across the board, custom gradients, better decomposition strategies, and higher-order derivatives.
Read more >
Pennylane Versions - Open Source Agenda
Fixed a bug where qml.math.fidelity(non_trainable_state, trainable_state) failed ... If set to True , broadcasting is used to compute the derivative:
Read more >
Error in panel: this.$refs.options.toggle is not a function - Getkirby/Kirby
How to get snapshot diffs in Mochawesome Reporter, 3, 2019-09-10, 2022-09-27. [BUG] Derivatives of `qml.CommutingEvolution`, 10, 2022-04-28, 2022-08-27.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found