I am trying to train a parametrized quantum circuit with gradients calculated using parameter shift. I am trying to use the following class in my run_circuit() function (which has the qnode decorator) but it doesn’t work.
class Loader(Operation):
num_params = 1
num_wires = 2
par_domain = "R"
grad_method = "A" # Analytic differentiation with Parameter shift
grad_recipe = None
@staticmethod
def compute_decomposition(theta: float, wires: int | list):
qml.RY(theta, wires=0)
qml.RY(theta, wires=1)
qml.IsingXX(theta, wires=[0, 1])
qml.RY(theta, wires=0)
qml.RY(theta, wires=1)
Moreover, here is the run circuit function:
@qml.qnode(dev, diff_method = "parameter-shift")
def run_circuit(phi, weight0, weight1, weight2, weight3, weight4, weight5):
"""
Input a numpy array feature (which encodes a single normalized angle encoded spot price)
"""
loader.Loader(phi, wires=[0,1])
qml.StronglyEntanglingLayers(np.tensor([[[weight0, weight1, weight2] , [weight3, weight4, weight5]]]), wires=[0,1])
return qml.expval(qml.PauliZ(0))
It works as expected when I modify as such:
@qml.qnode(dev, diff_method = "parameter-shift")
def run_circuit(phi, weight0, weight1, weight2, weight3, weight4, weight5):
"""
Input a numpy array feature (which encodes a single normalized angle encoded spot price)
"""
qml.RY(theta, wires=0)
qml.RY(theta, wires=1)
qml.IsingXX(theta, wires=[0, 1])
qml.RY(theta, wires=0)
qml.RY(theta, wires=1)
qml.StronglyEntanglingLayers(np.tensor([[[weight0, weight1, weight2] , [weight3, weight4, weight5]]]), wires=[0,1])
return qml.expval(qml.PauliZ(0))