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

Add support for arbitrary linear combination gradient recipes #909

Merged
merged 48 commits into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
6246431
Have positive and negative multiplier and shift values
antalszava Nov 18, 2020
8f4f5d3
No print
antalszava Nov 18, 2020
19d5ad9
Formatting
antalszava Nov 18, 2020
a77d73f
Merge branch 'master' into multiple_shifts
antalszava Nov 18, 2020
7eeb013
3 element terms for grad_recipes; qubit okay; CV draft
antalszava Nov 18, 2020
50db4f9
CV for tape mode
antalszava Nov 18, 2020
e166e5c
Merge branch 'master' into multiple_shifts
antalszava Nov 18, 2020
e34c034
Comments
antalszava Nov 18, 2020
b966cd9
Merge branch 'multiple_shifts' of https://github.com/XanaduAI/pennyla…
antalszava Nov 18, 2020
ef78263
Remove unused
antalszava Nov 18, 2020
0a0dbdf
Formatting
antalszava Nov 18, 2020
95d73f7
Solve casting by specifying dtype at creation
antalszava Nov 19, 2020
538bec5
No casting needed for shifted
antalszava Nov 19, 2020
b1b4334
Update module docstring and Operation.grad_recipe docstring
antalszava Nov 19, 2020
8b30ce5
Development guide update
antalszava Nov 19, 2020
8e201b1
Wording
antalszava Nov 19, 2020
6f08762
Adding tests; adding error raised for unsupported logic for tape seco…
antalszava Nov 19, 2020
9741a6b
No f strings
antalszava Nov 19, 2020
573795a
Merge branch 'master' into multiple_shifts
antalszava Nov 20, 2020
9489dcd
Update pennylane/qnodes/cv.py
antalszava Nov 20, 2020
a92decf
Update pennylane/tape/tapes/cv_param_shift.py
antalszava Nov 20, 2020
9d7096a
Simplify using np.dot in CV param shift tape
antalszava Nov 20, 2020
34debc9
Merge branch 'master' into multiple_shifts
antalszava Nov 20, 2020
7680d25
Merge branch 'master' into multiple_shifts
josh146 Nov 21, 2020
1dc4234
Update tests/qnodes/test_qnode_cv.py
antalszava Nov 23, 2020
db02026
get_parameter_shift in tape mode as per Josh's suggestion; use that
antalszava Nov 23, 2020
32de6d2
Merge branch 'multiple_shifts' of https://github.com/XanaduAI/pennyla…
antalszava Nov 23, 2020
aa7d6ea
Changelog
antalszava Nov 23, 2020
b0916a0
Update tests/tape/tapes/test_cv_param_shift.py
antalszava Nov 23, 2020
5970497
Update .github/CHANGELOG.md
antalszava Nov 23, 2020
19783eb
Merge branch 'master' into multiple_shifts
antalszava Nov 23, 2020
3d155c5
merge in changes from 915
josh146 Nov 24, 2020
9c425b4
Merge branch 'master' into multiple_shifts
josh146 Nov 24, 2020
debf057
Update pennylane/operation.py
antalszava Nov 24, 2020
1bac591
Update grad recipe formulae as per Tom's suggestions
antalszava Nov 24, 2020
ba443c0
Merge branch 'multiple_shifts' of https://github.com/XanaduAI/pennyla…
antalszava Nov 24, 2020
456b563
Update other formula in comment
antalszava Nov 24, 2020
4c00dc1
Merge branch 'master' into multiple_shifts
josh146 Nov 24, 2020
c5ca866
CHANGELOG
antalszava Nov 24, 2020
59454bf
Add rendering img url approach
antalszava Nov 24, 2020
30b3314
Plus
antalszava Nov 24, 2020
e8c849c
Update pennylane/operation.py
antalszava Nov 24, 2020
a3a821d
Applying review suggestions
antalszava Nov 24, 2020
85881e3
Merge branch 'multiple_shifts' of https://github.com/XanaduAI/pennyla…
antalszava Nov 24, 2020
4a03b07
Update doc/development/plugins.rst
antalszava Nov 24, 2020
834801e
Update pennylane/operation.py
antalszava Nov 24, 2020
b0989c2
Merge branch 'master' into multiple_shifts
antalszava Nov 24, 2020
a9a8122
equation formatting fixes
josh146 Nov 25, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,22 @@

<h3>Breaking changes</h3>

* Updated how gradient recipes are stored for operations, allowing for
gradient recipe definitions involving custom multipliers and with more than
two terms.
* Updated how parameter-shift gradient recipes are defined for operations, allowing for
gradient recipes that are specified as an arbitrary number of terms.
[(#909)](https://github.com/PennyLaneAI/pennylane/pull/909)

- The ``VQECost`` class has been renamed to ``ExpvalCost`` to reflect its general applicability
beyond VQE. Use of ``VQECost`` is still possible but will result in a deprecation warning.
Previously, `Operation.grad_recipe` was restricted to two-term parameter-shift formulas.
With this change, the gradient recipe now contains elements of the form
<img src="https://render.githubusercontent.com/render/math?math=[c_i, a_i, s_i]">, resulting in a gradient recipe of
<img src="https://render.githubusercontent.com/render/math?math=\frac{\partial}{\partial\phi_k}f(\phi_k) = \sum_{i} c_i * f(a_i * \phi_k+s_i)">.
antalszava marked this conversation as resolved.
Show resolved Hide resolved

As this is a breaking change, all custom operations with defined gradient recipes must be
updated to continue working with PennyLane 0.13. Note though that if `grad_recipe = None`, the
default gradient recipe remains unchanged, and corresponds to the two terms <img src="https://render.githubusercontent.com/render/math?math=[c_0, a_0, s_0]=[1/2, 1, \pi/2]">
and <img src="https://render.githubusercontent.com/render/math?math=[c_1, a_1, s_1]=[-1/2, 1, -\pi/2]"> for every parameter.

- The `VQECost` class has been renamed to `ExpvalCost` to reflect its general applicability
beyond VQE. Use of `VQECost` is still possible but will result in a deprecation warning.
[(#913)](https://github.com/PennyLaneAI/pennylane/pull/913)

<h3>Documentation</h3>
Expand Down
13 changes: 4 additions & 9 deletions doc/development/plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -469,17 +469,12 @@ where
* :attr:`~.Operation.grad_recipe`: The gradient recipe for the analytic ``'A'``
method. This is a tuple with one nested list per operation parameter. For
trbromley marked this conversation as resolved.
Show resolved Hide resolved
parameter :math:`k`, the nested list contains elements of the form
antalszava marked this conversation as resolved.
Show resolved Hide resolved
:math:`[c_i, a_i, s_i]` where :math:`i \in I_{k}` is the index of the term,
resulting in a gradient recipe of
:math:`[c_i, a_i, s_i]`, resulting in a gradient recipe of

.. math:: \frac{\partial}{\partial\phi_k}O = \sum_{i \in I_{k}} c_i * O(a_i * \phi_k+s_i).
.. math:: \frac{\partial}{\partial\phi_k}f(\phi_k) = \sum_{i} c_i * f(a_i * \phi_k+s_i),

where :math:`f` is an expectation value that depends on :math:`O(\phi_k)`, an example being

.. math:: f(O(\phi_k)) = \braket{0 | O^{\dagger}(\phi_k) \hat{B} O(\phi_k) | 0}

which is the simple expectation value of the operator :math:`\hat{B}` evolved via the gate
:math:`O(\phi_k)`.
where :math:`f` is the expectation value of an observable on a circuit that has been evolved by
the operation being considered with parameter :math:`\phi_k`.

Note that if ``grad_recipe = None``, the default gradient recipe containing
the two terms :math:`[c_0, a_0, s_0]=[1/2, 1, \pi/2]` and :math:`[c_1, a_1,
Expand Down
19 changes: 12 additions & 7 deletions pennylane/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,22 @@
transformation on the quadrature operators.

For gates that *are* supported via the analytic method, the gradient recipe
(with multipliers :math:`c_i`, scaling factors :math:`a_i` and parameter shifts
:math:`s_i` for parameter :math:`\phi_k` where :math:`i \in I_{k}`) works as
follows:
works as follows:

.. math:: \frac{\partial}{\partial\phi_k}O = \sum_{i \in I_{k}} c_i * O(a_i * \phi_k+s_i).
.. math:: \frac{\partial}{\partial\phi_k}f = \sum_{i \in I_{k}} c_i * f(a_i * \phi_k+s_i).
antalszava marked this conversation as resolved.
Show resolved Hide resolved

where :math:`f` is the expectation value of an observable on a circuit that has
been evolved by the operation being considered with parameter :math:`\phi_k`,
there are multiple terms indexed with :math:`i` for each parameter :math:`\phi`
and the :math:`[c_i, a_i, s_i]` are coefficients specific to the gate.

The following specific case holds for example for qubit operations that are
generated by one of the Pauli matrices and results in an overall positive and
negative shift:

.. math:: \frac{\partial}{\partial\phi_k}O = c_k\left[O(\phi_k+s_k)-O(\phi_k-s_k)\right].
.. math:: \frac{\partial}{\partial\phi_k}f = \frac{1}{2}\left[f(\phi_k+\frac{\pi}{2})-f(\phi_k-\frac{\pi}{2})\right],
antalszava marked this conversation as resolved.
Show resolved Hide resolved

i.e., so that :math:`[c_0, a_0, s_0]=[1/2, 1, \pi/2]` and :math:`[c_1, a_1, s_1]=[-1/2, 1, -\pi/2]`.

CV Operation base classes
~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -652,7 +657,7 @@ def get_parameter_shift(self, idx, shift=np.pi / 2):
a = 1

# We set the default recipe following:
# ∂f(x) = c1*f(a1*x+s1) + c2*f(a2*x+s2)
# ∂f(x) = c*f(x+s) - c*f(x-s)
# where we express a positive and a negative shift by default
default_param_shift = [[multiplier, a, shift], [-multiplier, a, -shift]]
param_shift = default_param_shift if recipe is None else recipe
Expand Down Expand Up @@ -1621,7 +1626,7 @@ def heisenberg_pd(self, idx):
shift = np.pi / 2

# We set the default recipe to as follows:
# ∂f(x) = c1*f(a1*x+s1) + c2*f(a2*x+s2)
# ∂f(x) = c*f(x+s) - c*f(x-s)
default_param_shift = [[multiplier, a, shift], [-multiplier, a, -shift]]
param_shift = default_param_shift if recipe is None else recipe

Expand Down