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.

Gradients of non-linear post-processing functions

See original GitHub issue

Context We have been stumbling over this problem during the classical shadows PRs https://github.com/PennyLaneAI/pennylane/pull/2871 as well as the counts https://github.com/PennyLaneAI/pennylane/issues/2932 . I believe it is also related to this older issue https://github.com/PennyLaneAI/pennylane/issues/1722.

Here is an abstract example where a qnode is executed and returns a value $g(x)$, which is then post-processed in $f(g)$.

@qml.qnode(dev)
def qnode(x):
    ..
    qml.RX(x, 0)
    ..
    return g

def f(g):
    return non-linear(g)

When derivative of $f$ is given by $\partial_x f(g(x)) = f’(g(x)) \partial_x g(x) = f’(g(x)) (g(x+\pi/2) - g(x-\pi/2))/2$.

This is also what PennyLane is doing. However, there are situations where I want to be able to do something different. For example, in classical shadows, $g$ are some abstract bits that inform the classical representation of the snapshot states. From this I can post-process the expectation value, $f$, which is a non-linear function of $g$, the bits and recipes (measurement choices).

When I call qml.grad(f)(x), PennyLane does the above logic (I am actually not sure what it computes for the gradients of the bits and recipes). But what I would want instead is

$\partial_x f = (f(g(x+\pi/2)) - f(g(x-\pi/2)))/2$

Solutions One solution is to perform the post-processing inside the qnode, i.e. registering a new measurement. As far as I understand, by default, calling grad on this measurement outcome does exactly what I want.

Instead of registering new measurements, it would be great to have the possibility to register custom functions as outputs of qnodes. That custom function would internally call a valid measurement, and post-process it to the desired output, which is then returned by the qnode.

A different solution would be to register custom vjps, but as far as I understand, this is not something very desirable.

Disclaimer While writing this I realized a lot of false conceptions on my end, so I am not 100% sure I am making sense. Please understand this issue more as a discussion forum rather than a contained and explicit feature request.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:3
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
josh146commented, Aug 12, 2022

Instead of registering new measurements, it would be great to have the possibility to register custom functions as outputs of qnodes. That custom function would internally call a valid measurement, and post-process it to the desired output, which is then returned by the qnode.

I think a solution where we make it really easy for users and developers to define their own measurement process, which can be automatically used by devices without needing to modify devices, is the key thing we are missing

1reaction
josh146commented, Aug 31, 2022

The idea of registering callables instead of MeasurementProcesses in QNodes is pretty cool, but I don’t think it would be very maintainable…

@dwierichs I’m curious why you think this? To me, this feels like the most natural solution, especially if we build it into the device API.

For a long time, we have actually wanted to:

  • Move the default implementations of expval, var, etc. out of QubitDevice and into the corresponding MeasurementProcess

  • Allow devices to choose whether they use their own expval, var, etc. logic, or utilize the logic provided by a MeasurementProcess.

The advantage here is that any developers/user can define their own measurement process and have it be used by any device, without needing to modify the device. They also get gradient support via calling some_autodiff_framework.grad(...) for free, even if they use NumPy* 🙂 Currently, any time we want to add new measurement support to PL, we have to manually add it to every device we want to support as a new device method, which is a big overhead.

* Assuming their output of the measurement process supports the parameter-shift rule.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Post-Processing of non gradient-based Topology Optimization ...
In this paper, a post-processing algorithm is presented to improve the results from a non-gradient topology optimization simulated annealing based process.
Read more >
Optimized Nonlinear Gradients for Reversed-Phase ... - NCBI
We developed an algorithm to generate optimized gradient functions for shotgun proteomics experiments and evaluated it for two data sets consisting each of ......
Read more >
Optimized Nonlinear Gradients for ... - ACS Publications
We developed an algorithm to generate optimized gradient functions for shotgun proteomics experiments and evaluated it for two data sets ...
Read more >
Postprocessing technique to correct for ... - Wiley Online Library
In this case, the time-domain signal is weighted by a sinc function characterized by the amplitude Gb of the background gradient, ...
Read more >
Postprocessing techniques for gradient percolation ...
probability gradients (insert), the behavior is nonlinear and even nonmonotonic. ... function of the minimum occupation probability p(y0),.
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