Giter Site home page Giter Site logo

Comments (19)

smite avatar smite commented on August 20, 2024 1

I made no changes to QNode_vjp yet. I think I found a fix, I'm testing it now.

from pennylane.

mariaschuld avatar mariaschuld commented on August 20, 2024

The alternative would be to make the user reshape and flatten the weights when creating them and feeding them into a step of the optimizer. It's rather ugly but conceptually cleaner.

from pennylane.

mariaschuld avatar mariaschuld commented on August 20, 2024

Current hotfix in examples_maria branch _optimize.py seems to work well:

def step(self, objective_fn, x, grad_fn=None):
    """Update x with one step of the optimizer."""

    x_shape = x.shape

    g = self.compute_grad(objective_fn, x, grad_fn=grad_fn)

    if len(x_shape) > 1:  # reshape gradient after grad() flattened it
        g = g.reshape(x_shape)

    x_out = self.apply_grad(g, x)

    return x_out

from pennylane.

co9olguy avatar co9olguy commented on August 20, 2024

I believe there are some helper functions already available in autograd. Perhaps we can leverage those?

e.g., in https://github.com/HIPS/autograd/blob/master/autograd/misc/optimizers.py, the decorator unflatten_optimizer:

"'Takes an optimizer that operates on flat 1D numpy arrays and returns a
wrapped version that handles trees of nested containers (lists/tuples/dicts)
with arrays/scalars at the leaves."

from pennylane.

co9olguy avatar co9olguy commented on August 20, 2024

Sorry, I thought by the title that the discussion was about actual nested structures (trees, etc). It seems you meant just multidimensional arrays?

from pennylane.

mariaschuld avatar mariaschuld commented on August 20, 2024

Oh, yes, that's true. The code runs fine, but I was just not sure if grad() has side effects when using multidim arrays.

from pennylane.

co9olguy avatar co9olguy commented on August 20, 2024

From what I remember when digging into autograd, I believe it should be fine, but I'd like to double check and get back to you

from pennylane.

co9olguy avatar co9olguy commented on August 20, 2024

I checked, and autograd has no issues with either multidimensional arrays or nested structures. For example, if you look at the autograd neural network example (https://github.com/HIPS/autograd/blob/master/examples/neural_net.py), the params fed to the optimizer look like this:
init_params = ((W_1,b_1),(W_2,b_2))
where W_i are weight matrices (2-dimensional) and b_i are bias vectors (1-dimensional).

The question of whether openqml safely passes on nested/multidimensional arguments to autograd, I don't know the answer (@josh146 and @smite would know better)

from pennylane.

mariaschuld avatar mariaschuld commented on August 20, 2024

I just cannot trace this bug, which seems to be related to multidim weights since it disappears when I use 1-d weights:

import numpy as np
import autograd

dev = qm.device('strawberryfields.fock', wires=1, cutoff_dim=10)

@qm.qfunc(dev)
def circuit(weights):
    return qm.expectation.X(0)

def cost(weights):
    a = circuit(weights)
    b = weights[0,0]
    return a + b

w0 = np.array([[2., 3.], [7., 4.]])
autograd.grad(lambda w: cost(w))(w0)```

Error:
...
  File "/home/maria/.local/lib/python3.6/site-packages/autograd/core.py", line 205, in _mut_add
    def _mut_add(self, x, y):    x += y; return x
ValueError: operands could not be broadcast together with shapes (2,2) (4,) (2,2) 

from pennylane.

mariaschuld avatar mariaschuld commented on August 20, 2024

Any ideas?

from pennylane.

co9olguy avatar co9olguy commented on August 20, 2024

Can you work up a version which doesn't have the qnode? Just make circuit a purely classical function that has the same signature and returns an output of the same shape as you want. That way we should be able to determine if it is an autograd bug or an openqml bug (more likely).

from pennylane.

mariaschuld avatar mariaschuld commented on August 20, 2024

I tried this already and replacing the qfunc with

def circuit(weights):
    return weights[0, 1] 

does run.

from pennylane.

mariaschuld avatar mariaschuld commented on August 20, 2024

It feels like it is something about how weights are turned into Variables in the background...

from pennylane.

smite avatar smite commented on August 20, 2024

I found one bug already, if the circuit output does not depend on any of its parameters, QNode methods behave a bit erratically. I'll fix it and see if it's enough.

from pennylane.

smite avatar smite commented on August 20, 2024

It wasn't just that, it has to do with QNode_vjp. I'll try to fix it tonight.

from pennylane.

mariaschuld avatar mariaschuld commented on August 20, 2024

Thanks so much @smite !

from pennylane.

co9olguy avatar co9olguy commented on August 20, 2024

@smite @mariaschuld What is the status on this issue? Was the observed bug fixed? Can we close?

from pennylane.

smite avatar smite commented on August 20, 2024

@mariaschuld, can you test your example and see if it runs properly now?

from pennylane.

mariaschuld avatar mariaschuld commented on August 20, 2024

It seems all MWEs and examples that were complaining before are working. qnode.jacobian(weights) returns another shape than autograd.grad(qfunc)(weights), but I assume this is intended.

What was strange, my tests for multidim weights did not pick up the issue in the first place, so I have to think about better tests.

Thanks @ville, closing this.

from pennylane.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.