Retrieving the actual parameter values of a trained Qnode

Hello,
During the training process of a QNN, I would like to print circuit via the textual version qml.plot and the actual values of the trained parameters.

@qml.qnode(dev)
def circuit(theta):
    qml.RY(theta[0], wires=0)
    qml.RY(theta[1], wires=1)
    qml.CNOT(wires=[0, 1])
    return qml.expval(qml.PauliZ(1))

theta = np.array([0.1, 0.3], requires_grad = True)

So referring to the above code snippet, Instead of theta I would like to provide the actual trained parameters for RX and RY.

Thanks,

Hey @Solomon! Welcome to the forum :rocket:

I think you mean qml.draw? I can print the circuit with the values I give it like so:

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

@qml.qnode(dev)
def circuit(x):
    qml.RX(x[0], 0)
    qml.RX(x[1], 1)
    return [qml.expval(qml.PauliZ(i)) for i in range(2)]

print(qml.draw(circuit)([0.1, 0.2]))

'''
0: ──RX(0.10)─┤  <Z>
1: ──RX(0.20)─┤  <Z>
'''

Let me know if that helps!

Hello Issac and thanks for the reply,
I am already using draw … imagine training a QNN and optimising the PQC parameters, now say one epoch before the final epoch I just want to pick at the values of all the trained parameters. How do I do that?

If I understand correctly, you want something more meaningful than just printing the parameters. Maybe this?

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

@qml.qnode(dev)
def circuit(x):
    qml.RX(x[0], 0)
    qml.RX(x[1], 1)
    return [qml.expval(qml.PauliZ(i)) for i in range(2)]

circuit([0.1, 0.2])

tape = circuit.tape
operations = tape.operations

for op in operations:
    print(op.name, op.data)

'''
RX [0.1]
RX [0.2]
'''

That way you see the operation and the parameter belonging to it. Let me know if that works!