Gradient Not Working Because of Keyword Arguments

def gen_ansatz(theta_g, x=None):
    #Reshape theta so params are easier to access
    #theta_g = theta_g.reshape(NUM_QUBITS, NUM_LAYERS, PARAMS_PER_LAYER)

    for i in range(NUM_LAYERS):
        # Hadamard
        for q in range(NUM_QUBITS):

        # RX RZ
        for q in range(NUM_QUBITS):
            qml.RX(x[q // 2] * theta_g[q, i, 0], wires=q)
            qml.RZ(x[q // 2] * theta_g[q, i, 1], wires=q)

        # Entanglement
        for q in range(NUM_QUBITS-1):
            qml.CNOT(wires=[q, q + 1])

In my above quantum circuit, I am getting the following error during gradient calculation:

unsupported operand type for /: ‘Variable’ and ‘int’

I assume this comes from my multiplication of x and theta_g within the RX and RZ. As I understand it, since x is a keyword argument, it should not be differentiated. Can anyone point out my issue?


Hi SreeramV181,

In order to better understand the issue, could you also tell us what call you made to this circuit? e.g., gen_ansatz(0.5, x=2) etc.

def real_disc_circuit(disc_weights, data=None):
    print("Running discriminator circuit")
    Feeds discriminator with true examples

    print("Discriminator ansatz complete")
    return [qml.expval.Hadamard(i) for i in range(NUM_FEATURES + 1)]


I pasted the code above. Please let me know if you need anything else.

I tried modifying the Pennylane code on my computer directly to match the changes in the post from below:

It still didn’t work. Let me know what I can do.


Yes, since the issue seems to be with the keyword argument x, which is passed the value data, could you also include the code where you assign something to the variable data? Mostly likely this is in a call to real_disc_circuit.


Hi @SreeramV181, as mentioned by @nathan, we’ll need to see how you are calling the QNode to provide more help.

In the meantime, here is how you can install the PR you are referencing:

git clone
cd pennylane
git checkout variables_during_construction
pip install -e .

After installing this branch, you will need to run the function qml.eager_mode() after importing PennyLane:

import pennylane as qml