How to use a tensor measurements with variable number of qubits?

Hey @ankit27kh! Yes, there are two ways of doing this :slightly_smiling_face:

Method 1:

from pennylane.operation import Tensor 

@qml.qnode(dev)
    ...
    return qml.expval(Tensor(*[qml.PauliZ(i) for i in range(n_qubits)]))

Method 2:

from functools import reduce
import operator

@qml.qnode(dev)
    ...
    obs = reduce(operator.matmul, [qml.PauliZ(i) for i in range(n_qubits)])
    return qml.expval(obs)
1 Like