Sample-based differentiable probabilities

Hi,

My question is pretty simple: when using qml.probs() (for computational basis states), the probabilities are exact and the circuit is differentiable. However, I want to obtain these same probabilities by sampling instead and also want it to be differentiable.

Am I correct that there is no argument in qml.probs() that achieves this and we have to implement it ourselves? E.x. sample every qubit in Z basis, then convert computational basis state binary representation into floats and then calculate probabilities. Would that even be differentiable? Seems like too much work for something that’s definitely not a rare requirement.

Hi @karolishp!

qml.probs is not always exact; it is only exact if shots=0. However, if shots are finite, then the probabilities will be estimated from the samples:

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

@qml.qnode(dev)
def circuit(x):
    qml.RX(x, wires=0)
    return qml.probs(wires=0)
>>> circuit(0.7)
tensor([0.92, 0.08], requires_grad=True)
>>> circuit(0.7)
tensor([0.87, 0.13], requires_grad=True)
>>> circuit(0.7)
tensor([0.95, 0.05], requires_grad=True)

It also remains differentiable:

>>> x = np.array(0.7, requires_grad=True)
>>> qml.jacobian(circuit)(x)
array([-0.28,  0.28])
>>> qml.jacobian(circuit)(x)
array([-0.34,  0.34])

Thank you, I did not notice this apparently :slight_smile: Apologies for taking up your time!

No need to apologize @karolishp! We’re happy to help.