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.

Support all operations with `param_shift_hessian()`

See original GitHub issue

Feature details

It should be possible to support operations with arbitrary grad_recipes in param_shift_hessian() by computing the applicable grad recipe for each entry in the hessian from the grad_recipess of the operations instead of using fixed diag_recipe and off_diag_recipes.

A first and easier improvement would be to at least replace the hard coded list of “supported operations” by code that inspects the grad_recipes of the occuring operations so that newly added operations that are also differentiable according to the standard rule are supported automatically.

Implementation

No response

How important would you say this feature is?

2: Somewhat important. Needed this quarter.

Additional information

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:30 (22 by maintainers)

github_iconTop GitHub Comments

2reactions
soranjhcommented, May 17, 2022

For the first bug, the problem seems to stem from the behaviour of tape.trainable_params together with the Hamiltonian:

dev = qml.device('default.qubit', wires=2)
qubit_operator = of.QubitOperator('X0 Y1', 1.0)
hamiltonian = qml.import_operator(qubit_operator)
hamiltonian.coeffs.requires_grad = False

@qml.qnode(dev, diff_method='parameter-shift')
def circuit(param):
    qml.RY(param, wires=[0])
    return qml.expval(hamiltonian)
>>> param = np.array(0.2, requires_grad=True)
>>> circuit.construct((params,), {})
>>> print(circuit.qtape.trainable_params)
[0, 1]

As we can see, the Hamiltonian coefficient is set to be trainable.

Setting requires_grad = False for the Hamiltonian coefficients seems to solve the issue:

dev = qml.device('default.qubit', wires=2)
qubit_operator = of.QubitOperator('X0 Y1', 1.0)
hamiltonian = qml.import_operator(qubit_operator)

c = np.array(hamiltonian.coeffs, requires_grad = False)
o = hamiltonian.ops
hamiltonian = qml.Hamiltonian(c, o)

@qml.qnode(dev, diff_method='parameter-shift')
def circuit(param):
    qml.RY(param, wires=[0])
    return qml.expval(hamiltonian)

params = np.array(0.2, requires_grad=True)
circuit.construct((params,), {})
print(circuit.qtape.trainable_params) # output: [0]

Not sure why hamiltonian.coeffs.requires_grad = True does not do the same thing.

2reactions
cvjjmcommented, Feb 11, 2022

I don’t know yet what will be useful, so I would say it generalizes 😃

Maybe the user can pass in a “mask” of the same shape as the hessian with boolean entries and then get a hessian with only those entries back?

Read more comments on GitHub >

github_iconTop Results From Across the Web

pennylane [python]: Datasheet - Package Galaxy
Need information about pennylane? Check download stats, version history, popularity, recent code changes and more.
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