Hi everyone,
I would like to ask for help with calculating fidelity in PennyLane when using a device with finite shots, for example:
dev = qml.device("lightning.qubit", wires=n_qubits, shots=10000)
My goal is to prepare the ground state of the Ising model using Quantum Natural Gradient. How can fidelity be calculated effectively in this scenario?
I am very grateful for your help and insights!
Hi @QuantumH , welcome to the Forum!
You could try getting the expectation value of a Hermitian operator rho to obtain the fidelity between rho and the state of your circuit.
Here’s a code example.
import numpy as np
import pennylane as qml
dev = qml.device("lightning.qubit", wires=2, shots=10)
y=np.array([[1,0],[0,-1]])
@qml.qnode(dev)
def circuit(param):
qml.RX(param, wires=0)
qml.CNOT(wires=[0, 1])
return qml.expval(qml.Hermitian(y, wires=[1]))
print("Result: ", circuit(0.123))
You can see another example that does the same in our data-reuploading classifier demo.
If you just want to compute the fidelity for two states you can use qml.math.fidelity without needing to create a circuit.
Let me know if this is what you were looking for!
Thank you @CatalinaAlbornoz for your help! This is very useful.
1 Like