Hi @NikSchet,
Thank you for your question!
The following example would simulate noise models by:
- Using the
default.mixed
device that supports noise models
- Applying
qml.DepolarizingChannel
noise channels within the quantum function
import pennylane as qml
from pennylane import numpy as np
n_qubits = 5
dev = qml.device('default.mixed', wires=n_qubits)
inputs = np.ones(5, requires_grad=False)
weights = np.ones((3,3,5,3), requires_grad=True)
blocks = 3
p = 0.01
@qml.qnode(dev, interface="tf", diff_method="best")
def qnode(inputs, weights):
for i in range(blocks):
qml.templates.AngleEmbedding(inputs, wires=range(n_qubits))
qml.templates.StronglyEntanglingLayers(weights[i], wires=range(n_qubits))
qml.DepolarizingChannel(p, wires=0)
qml.DepolarizingChannel(p, wires=1)
return [qml.expval(qml.PauliZ(i)) for i in range(n_qubits)]
weight_shapes = {"weights": (3,3,5,3)}
qlayer = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=2)
qlayer(inputs)
<tf.Tensor: shape=(5,), dtype=float32, numpy=
array([ 0.02523608, 0.0064983 , 0.248738 , -0.07350533, -0.12045245],
dtype=float32)>
Would this be something close to what you’d like to have?
Note: we’re setting diff_method="best"
in the example which will correspond to the parameter-shift rule. The reason for this is that the default.mixed
device doesn’t support backpropagation. Furthermore, in the original linked example passing grad_method="backprop"
to the QNode will not take any effect, as the keyword is diff_method
rather than grad_method
.
Let us know if we could further help.