Comments (19)
I made no changes to QNode_vjp yet. I think I found a fix, I'm testing it now.
from pennylane.
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.
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.
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.
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.
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.
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.
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.
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.
Any ideas?
from pennylane.
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.
I tried this already and replacing the qfunc with
def circuit(weights):
return weights[0, 1]
does run.
from pennylane.
It feels like it is something about how weights are turned into Variables in the background...
from pennylane.
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.
It wasn't just that, it has to do with QNode_vjp. I'll try to fix it tonight.
from pennylane.
Thanks so much @smite !
from pennylane.
@smite @mariaschuld What is the status on this issue? Was the observed bug fixed? Can we close?
from pennylane.
@mariaschuld, can you test your example and see if it runs properly now?
from pennylane.
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)
- [BUG] `qml.simplify` removes information regarding `num_steps` from `Exp`
- [BUG] QAOA layers incorrectly forbid `Sum` observables
- Add function to convert PennyLane objects to OpenFermion objects HOT 2
- [BUG] Adjoint method does not respect global wires return types
- [BUG] TorchLayer does not work correctly with broadcasting and `tuple` returns HOT 1
- Propagate the gradient to part of an array only HOT 2
- Prevent writing to a specific file when creating the Hamiltonian HOT 12
- [BUG] Some templates not properly differentiated with parameter shift on the legacy device HOT 1
- Update documentation to remove pennylane.optimize HOT 3
- [BUG] `qml.eigvals` sometimes fails when using `LinearCombination` HOT 1
- [BUG] `qml.Adjoint` and `qml.Pow` do not commute when exponent is fractional HOT 1
- [BUG] JIT + Global measurements without device wires does not work
- [BUG] `StatePrep` and its decomposition differ in derivative
- Support broadcasting/batching of `GlobalPhase`
- [BUG] `qml.pauli_decompose` fails when being jit-ted HOT 1
- [BUG] Allow natural extension of `QubitUnitary` to 1x1 matrices
- [BUG] Documentation inconsistently states that GlobalPhase acts on all wires
- Align definition of GlobalPhase convention HOT 4
- [BUG] Tensor product alters operands
- [BUG] Second-oder derivatives fail with shots enabled for product of input variables HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pennylane.