I want to calculate gradient descent with the following setup of quantum circuit.
import pennylane as qml from pennylane import numpy as np dev = qml.device('default.qubit', wires = ['wire1', 'wire2'], shots = 1000) @qml.qnode(dev, interface='autograd') def circuit(params): qml.RX(params, wires = 'wire1') # Rx is applied with the 1st parameter, on the first qubit qml.RY(params, wires = 'wire1') # Ry is applied with the 2nd parameter, on the first qubit qml.CNOT(wires = ['wire1', 'wire2']) # CNOT is applied on the first and second qubit #my_circuit(wires = (1,0)) return qml.sample(qml.PauliZ('wire1')), qml.sample(qml.PauliZ('wire2'))
I then implement the gradient descent function,
dcircuit = qml.grad(circuit, argnum=0) print(dcircuit([0, 0]))
which return the following error
Grad only applies to real scalar-output functions. Try jacobian, elementwise_grad or holomorphic_grad.
After a few trials, I realise that I can run gradient descent only the result is measured in expval(), but not sample()
I don’t know why, I hope someone can explain it to me.
What if I want to use to check the number of shots from sample()
result = circuit(params) result.shape
how can I correct the code?