Learning behaviour depends on packaging of gate

I have a the following sequence of gates that form a generalized U4 gate as part of my circuit:

qml.U3(params[0], params[1], params[2], wires=wires[i])
qml.U3(params[3], params[4], params[5], wires=wires[i+1])
qml.CNOT(wires=[wires[i], wires[i+1]])
qml.RY(params[6], wires=wires[i])
qml.RZ(params[7], wires=wires[i+1])
qml.CNOT(wires=[wires[i+1], wires[i]])
qml.RY(params[8], wires=wires[i])
qml.CNOT(wires=[wires[i], wires[i+1]])
qml.U3(params[9], params[10], params[11], wires=wires[i])
qml.U3(params[12], params[13], params[14], wires=wires[i+1])

But in a bizarre turn of things, when I package these into a function and call it while building the circuit, I get a much different(worse) accuracy than if I directly put the gates in the circuit. I’m using jax to optimize my code if that makes a difference. Is there something going on behind the scenes that’s different in the two methods?

Hi @somearthling, thank you for posting this question.

Could you please post a minimum working example of the code you’re using in both cases? Also, do you see the same behaviour without using jax?

What versions of Python, Jax and PennyLane are you using?

This is a very strange behaviour so we need to understand the full context.