I’m trying to implement a non-unitary operation, which is made deterministic by post-selection on a mid-circuit measurement. This works as expected using the following code:
import pennylane as qml
from pennylane import numpy as np
from pennylane.operation import Operation, AnyWires
from numpy import tan, tanh, arctan, arctanh, pi, cosh, sinh
dev = qml.device("default.qubit", wires=2)
@qml.qnode(dev)
def single_layer_circuit(tau):
t = 2*arctan(tanh(tau))
qml.Hadamard(wires=0)
qml.Hadamard(wires=1)
qml.MultiRZ(t, wires=[0,1])
qml.Hadamard(wires=0)
qml.RX(-pi/2, wires=0)
qml.measure(0, postselect=0)
return qml.density_matrix(1)
Now I wish to repeat the application of this gate in a compact manner. I’m trying to do this by using qml.layer, as follows:
import pennylane as qml
from pennylane import numpy as np
from pennylane.operation import Operation, AnyWires
from numpy import tan, tanh, arctan, arctanh, pi, cosh, sinh
def single_layer(tau):
t = 2*arctan(tanh(tau))
qml.Hadamard(wires=0)
qml.Hadamard(wires=1)
qml.MultiRZ(t, wires=[0,1])
qml.Hadamard(wires=0)
qml.RX(-pi/2, wires=0)
qml.measure(0, postselect=0)
@qml.qnode(dev)
def circuit(params):
qml.layer(single_layer, 2, params)
return qml.density_matrix(1)
params = np.array([[0.5], [0.4]])
This works if I remove the mid-circuit measurement, meaning that the most straightforward way to repeat this gate (with possibly different parameters) requires some additional loop, making the code much more cumbersome.
Is there anyway to make this gate (with measurements) iterative via qml.layer? Or in some other way which makes it easy to manage the different parameters in the layers (e.g. by defining some Operation class)? Thank you.