# Computing variance under the action of a unitary operator

I wish to calculate the expectation and variance for an observable on a particular qubit of a multi qubit quantum state. I'm using a quantum computing simulation library which allows me to apply operators to a state vector and perform calculations such as computing inner products between state vectors.

To compute the expectation value:

I can use the definition of expectation value $$\langle\psi|O|\psi\rangle$$ to achieve this computation as follows:

1. Create a copy of the given state vector $$\psi$$, apply the observable to the required qubit to obtain $$O|\psi\rangle$$.

2. Calculate the inner product of the state calculated in 1 with the conjugate transpose of $$|\psi\rangle$$ that is $$\langle\psi|O|\psi\rangle$$.

Is the method for computing the expectation values correct ?

For computing the variance:

I'm thinking of using the following definition: $$\text{Variance}(O) = \text{Expectation}(O^2) - \text{Expectation}(O)^2.$$

If I understand correctly, I need to compute the following two quantities:

a. $$\text{Expectation}(O)^2$$: This is simply the square of the expectation value that I can calculate using the method stated earlier.
b. $$\text{Expectation}(O^2)$$: For calculating this, I think I need to apply the observable to the target qubit in the state vector twice (Is that right?). If so, in case my observable is unitary, I can avoid applying the unitary altogether. So this value should simply be $$\langle\psi|\psi\rangle$$ (which should be 1 since my state is normalized).

1. Is this correct?
2. Is there a better (more efficient) way to accomplish this?
• Hi and welcome to QCSE. The problem with $O | \psi \rangle$ is that $O$ is not necessarily a unitary operator, which means we can't apply (not unitary) $O$ in the circuit, but of course mathematically (with a classical computer) one can do it. – Davit Khachatryan Jun 13 at 8:57
• In this Qiskit textbook's VQE tutorial the expectation value is calculated with matrix multiplications (without "QC mode") qiskit.org/textbook/ch-applications/vqe-molecules.html – Davit Khachatryan Jun 13 at 9:08

Calculating the expectation value for a specific Hermitian operator

This approach can be implemented with real Quantum Hardware and with a simulator. Every Hermitian operator can be decomposed in the sum of Pauli tensor product terms (Pauli terms) with real coefficients (see this thread [1])

$$H = a \cdot \sigma_z \otimes I + b \cdot\sigma_y \otimes \sigma_y + c \cdot\sigma_x \otimes I,$$

where $$a,b,c$$ are real numbers. The expectation value of $$H$$:

$$\langle \psi | H | \psi \rangle = \langle \psi | \sigma_z \otimes I | \psi \rangle + \langle \psi | \sigma_y \otimes \sigma_y | \psi \rangle + \langle \psi | \sigma_x \otimes I | \psi \rangle$$

So, finding the expectation value of $$H$$ operator can be calculated from summing separately calculated expectation values of the Pauli terms with their real coefficients. Now we should run separate experiments for calculating the expectation value of each Pauli term. Note that in some cases when the Pauli terms commute we can combine the expectation value estimation procedure for them like was shown for the two-qubit state in this question [2] (or here [3]). It is not our case, because our Pauli terms do not commute with each other.

Note that any one-qubit state can be expressed with different eigenbasis (like every vector in a 2D real space can be expressed with a combination of any set of 2 orthonormal 2D vectors):

$$|\psi \rangle = c_0 |0 \rangle + c_1| 1 \rangle = c_+ | + \rangle + c_-| - \rangle$$

where $$c$$s are complex numbers, $$|0 \rangle$$ and $$| 1 \rangle$$ are eigenbasis for $$\sigma_z$$, $$|+ \rangle = \frac{1}{\sqrt{2}}(|0 \rangle + | 1 \rangle)$$ and $$| - \rangle = \frac{1}{\sqrt{2}}(|0 \rangle - | 1 \rangle)$$ are eigenbasis for $$\sigma_x$$. For the two-qubit case, we can use Bell basis that is also an eigenbasis for $$\sigma_y \otimes \sigma_y$$ [2], [4]

$$|\psi \rangle = c_{\Phi^+}|\Phi^+\rangle + c_{\Phi^-}|\Phi^-\rangle + c_{\Psi^+}|\Psi^+\rangle + c_{\Psi^-}|\Psi^-\rangle$$

where $$|\Phi^+\rangle$$, $$\Phi^-\rangle$$, $$|\Psi^+\rangle$$ and $$|\Psi^-\rangle$$ are Bell states. By taking this into account we can obtain:

$$\langle \psi| \sigma_z | \psi \rangle = |c_0|^2 - |c_1|^2 \\ \langle \psi| \sigma_x | \psi \rangle = |c_+|^2 - |c_-|^2 \\ \langle \psi| \sigma_y \otimes \sigma_y | \psi \rangle = -|c_{\Phi^+}|^2 + |c_{\Phi^-}|^2 + |c_{\Psi^+}|^2 - |c_{\Psi^-}|^2$$

"+" and "-" sign comes from $$P |\lambda \rangle = \pm |\lambda \rangle$$ for a Pauli term $$P$$ with correspoding eigenvector $$|\lambda \rangle$$. Here $$|c_0|^2 = \frac{N_0}{N}$$, $$|c_1|^2 = \frac{N_1}{N}$$ for big enough $$N$$, $$N$$ is the number of the measurements, $$N_0$$ is the number of $$|0\rangle$$ measurements, $$N_1$$ is the number of $$|1\rangle$$ measurements. The same for $$\sigma_x$$: $$|c_+|^2 = \frac{N_+}{N}$$, $$|c_-|^2 = \frac{N_-}{N}$$ for big enough $$N$$, $$N_+$$ is the number of $$|0\rangle$$ measurements, $$N_-$$ is the number of $$|1\rangle$$ measurements. Similarly for the Bell states. So by just measuring in the appropriate basis we will be able to calculate the expectation value of the Pauli terms. Here we can have a problem, if our quantum computer don't have a possibility to measure in arbitrary eigenbasis. For a given $$P$$ Pauli term this problem can be overcomed by applying such $$U$$ unitary operator before the $$\sigma_z$$ (default) measurements (like was presented here [5]), that:

$$\langle \psi |P| \psi \rangle = \langle \psi | U^{\dagger} \sigma_z \otimes \sigma_z \otimes ...\otimes \sigma_z U | \psi \rangle$$

Note that in the product $$\sigma_z \otimes \sigma_z \otimes ...\otimes \sigma_z$$ in some places we can have $$I$$ instead of $$\sigma_z$$: it just means that we will not do any measurement for the corresponding qubit. For $$P = \sigma_x$$ this can be done with $$U = H$$ gate. For $$\sigma_y \otimes \sigma_y$$ we can take $$U = (H\otimes I) CNOT$$ [2], [4].

The same can be done for $$H^2$$ because it is also a Hermitian operator and hence with this method, we will be able also to calculate the variance of $$H$$.

I'll comment on how you would obtain these quantities in a (generic, idealised) experimental scenario.

To compute an expectation value $$\langle \psi|O|\psi\rangle$$ you need to be able to measure in the eigenbasis of $$O$$. Being observables, by definitions, Hermitian operators, you can always write $$O$$ as $$O = \sum_k \lambda_k |u_k\rangle\!\langle u_k|$$, where $$O|u_k\rangle=\lambda_k|u_k\rangle$$ and $$|u_k\rangle$$ are an orthonormal basis.

In terms of these quantities, the expectation value reads $$\langle\psi|O|\psi\rangle = \sum_k \lambda_k |\langle u_k|\psi\rangle|^2.$$ What you need to do is therefore measure $$|\psi\rangle$$ in the basis $$\{|u_k\rangle\}_k$$. Each such measurement will give you as output one of the possible output state (e.g. you might find $$|u_1\rangle$$ at the first run, $$|u_3\rangle$$ at the second run, etc.). Running the experiment multiple times you can estimate the probabilities of each outcome. When a sufficient number of measurements are performed, these will approach the true probabilities $$p_k\equiv |\langle u_k|\psi\rangle|^2$$.

Once you estimated these probabilities, the expectation value is obtained by simply attaching (multiplying) the number $$\lambda_k$$ to each probability $$p_k$$ and summing the resulting values.

Measuring $$\langle\psi|O^2|\psi\rangle$$ is fully analogous, and doesn't require additional measurements. The only difference is that each $$p_k$$ is multiplied by $$\lambda_k^2$$ rather than by $$\lambda_k$$.

With regards to the calculation involving applying $$O$$ to $$|\psi\rangle$$, it is important to note that this is useful as a mathematical trick to compute this quantity, but does not represent a physical operation. Observables are not physical operations that transform states into other states.