Printing out diff_method

Hello!

In the interface documentation it states:

Note
If not specified, the default differentiation method is diff_method="best". PennyLane will attempt to determine the best differentiation method given the device and interface. Typically, PennyLane will prioritize device-provided gradients, backpropagation, parameter-shift rule, and finally finite differences, in that order.

When using diff_method="best" is there a way to print out/inform the user which diff_method was ultimately selected and used in the computation? For some reason I can’t find how to do this in the documentation.

Also given that PennyLane 0.30 is a fairly recent release (and some of us are still using 0.29), would such method to print out the diff_method be different between v0.30 and v0.29?

Thanks!

Hey @QML! Great question. To see what diff_method was assigned, you can use the diff_method attribute for QNodes:

dev = qml.device("default.qubit", wires=1)

@qml.qnode(dev)
def circuit(x):
    qml.RX(x, 0)
    return qml.expval(qml.PauliZ(0))

print(circuit.diff_method) 

In this case, circuit.diff_method is "best" :sweat_smile:. The hierarchy of differentiation methods is listed here in the docs:

  • device
  • backprop
  • parameter-shift
  • finite-diff

Hope this helps!

Hey @isaacdevlugt! Thanks for the quick response. I ran your code and I can confirm that circuit.diff_method is "best" ( :stuck_out_tongue_winking_eye:) but unfortunately for PennyLane to return "best" doesn’t really help - what I’m looking for is to have print(circuit.diff_method) return either

  • backprop
  • parameter-shift
  • finite-diff
  • some other type of diff_method (as "best" or "device" aren’t mathematical differentiation methods)

Is there a way to do this?

Thanks!

Sure! You can do this :slight_smile: print(circuit.best_method_str(dev, circuit.interface))

1 Like

That’s exactly it! Thank you!

1 Like

Awesome! Glad I could help.