# How does the codebook find analytical solutions?

I have a general question about the entire codebook which I suspect is due to some backend processes. Due to the randomness of measurements, you always get a different value of expected value/probabilities and this variance is reduced as the number of shots approaches infinity. However, the examples in the codebook seem to return an analytic deterministic output.

For example, the following code snippet from I.10 will always return the same values, but if you add shots=1000 into the dev object, which I believe is the default, then it introduces the randomness that you would expect.

dev = qml.device('default.qubit', wires=1)

@qml.qnode(dev)
def circuit():
qml.RX(np.pi/4, 0)
qml.PauliZ(0)

return qml.expval(qml.PauliY(0))

print(circuit())


Update: I have found an explanation for my question in another thread:

Since you are working with a simulator, you can calculate the exact value of the wave function without having to run different shots, just multiplying matrices. Not indicating the number of shots means that you want the analytical solution (this will not be possible with a real quantum computer)

Also note that the function qml.device() used in the codebooks is different to the class qml.Device() which has the default setting as shots=1000. I got mixed up with the documentation of these two.

Though I am still curious on cases when qml.device() cannot find analytical solutions. And why do real quantum computers not compute analytical solutions? Is it because the cases in which analytical solutions are computable are trivial, and can just be left to simulations?

Hey @AvanishM, great questions!

Indeed, because we are simulating quantum computers, we can analytically calculate / predict what the results are because the simulator is essentially doing matrix-vector multiplication under the hood — much different than how a real quantum computer would operate! For example, the following expectation value can be obtained analytically via matrix-vector multiplication:

\langle + \vert Y \vert + \rangle = \frac{1}{\sqrt{2}}\begin{pmatrix} 1 & 1 \end{pmatrix} \begin{pmatrix}0 & -i \\ i & 0 \end{pmatrix} \frac{1}{\sqrt{2}} \begin{pmatrix} 1 \\ 1 \end{pmatrix} = \frac{1}{2} \begin{pmatrix} 1 & 1 \end{pmatrix} \begin{pmatrix} -i \\ i \end{pmatrix} = \frac{1}{2}(-i + i) = 0.

A quantum computer can’t do this because there isn’t a little matrix-vector-product demon running around inside the computer . We can measure an outcome once, the outcome being one of the eigenvalues of the observable we’re measuring. The probability of measuring a given eigenvalue relates back to the state we’re measuring with respect to.

When we measure many times, we can get a statistical average of what the true / analytic answer is. The number of times we measure is called the number of shots. When you specify a finite value for shots when you create a device in pennylane, you’re asking PennyLane to simulate what actually happens in nature .

Let me know if this helps!

1 Like