# What's a circuit to compute the expectation value of an observable?

Apologies if the question is naive. I want to compute the expectation value of some operator for a particular model. It seems I can do that with Hadamard test. I have the circuit for the ground state of the Hamiltonian. Is there any other methods to compute expectation values in a straightforward manner?

It all depends on the observable you care to measure. On the quantum computer, you only have access to single qubit Pauli-Z measurements. You may, for example be interested in computing the expectation value of a Pauli-X operator, how can we do this with only single qubit Pauli-Z measurements?

Since we don't get to change our measurements, we will have to change our state. To figure out what rotation we need to perform, we seek a unitary $$U$$ satisfying $$X = U Z U^\dagger$$ where $$X$$ and $$Z$$ are the Pauli-X and Pauli-Z operators respectively. Since these are $$2\times2$$ matrices is quite straight forward to show that in this case $$U=H$$, where $$H$$ is the Hadamard operator.

Ok, so now that we've found $$U$$ for transforming between $$Z$$ and $$X$$ measurements. Let's compute an expectation value! Let $$|\psi\rangle$$ be a single qubit state. We want $$\langle\psi|X|\psi\rangle$$ But we know that $$X = UZU^\dagger \equiv HZH$$. Therefore,

$$\langle\psi|X|\psi\rangle = \langle\psi|HZH|\psi\rangle$$

Therefore, the circuit you need to perform to perform an $$X$$ measurement is just the circuit you used to prepare $$|\psi\rangle$$ with an $$H$$ gate applied on it at the end, right before measurement! i.e. if $$U_\psi:|0\rangle \mapsto |\psi\rangle$$, then we just need to run the circuit $$HU_{\psi}$$ and perform the standard Z measurement multiple times and collect samples to compute the expectation value of $$\langle\psi|HZH|\psi\rangle = \langle\psi|X|\psi\rangle$$.

This procedure generalizes for more exotic operators. In general, you can find the measurement circuit you need to append by finding the unitary satisfying $$U^\dagger Z U = \mathcal{O}$$, for the operator $$\mathcal{O}$$ who's expected value you care about. It gets only slightly more complicated for simultaneous measurement of multiple (commuting) observables.

As a side note, computing expectation values of $$Z$$ operator is quite easy. Because it's eigenvalues are just $$\pm 1$$ and we always get an eigenstate of $$Z$$ (i.e. a bitstring) on measurement, so

$$\langle Z\rangle \approx \frac{N_0 - N_1}{N_{exp}} \equiv P(0)-P(1)$$

Where we've used $$N_i$$ to refer to the number of observations of the $$i^{th}$$ eigenstate.

You can see more general identities for measuring more complicated sets of Pauli Operators on Microsoft's website explaining Pauli measurements: https://docs.microsoft.com/en-us/azure/quantum/concepts-pauli-measurements.

I also really like a lot of the features for dealing with expectation values in Pennylane, they automatically compile circuits depending on the set of observables you are trying to compute the expectation of. You can find some examples of that functionality here: https://pennylane.readthedocs.io/en/stable/code/qml_grouping.html.

• BTW, thanks for the reference of Azure document on how to perform measurements for the multi-qubit observables. May 7, 2022 at 16:13

Just prepare the groud state and then do measurement in the basis of eigenvectors of the observable. For example, if you want to calculate $$\langle \sigma_x\rangle$$, then just measure in $$|+\rangle$$ and $$|-\rangle$$ basis, and assign $$+1$$ to measurement result $$|+\rangle$$, assign $$-1$$ to measurement result $$|-\rangle$$, then you can calculate $$\langle \sigma_x\rangle$$ as $$\sum_i{\frac{X_i}{N}}$$ where $$N$$ is repetition number of measurements and $$X_i=\pm 1$$ is the result for $$i$$-th measurement.

• I agree if we know the eigenvectors of the operators, we can compute the observables this way. However, I am a bit puzzled that why we should assume that we know the eigenvectors of the operators. In a general case, we won't know the eigenvectors beforehand. May 18, 2022 at 4:12
• @Anyon Quoted from Nielsen and Chuang's book, chap 2.2.5: Projective measurements: A projective measurement is described by an observable, $M$, a Hermitian operator on the state space of the system being observed. The observable has a spectral decomposition, $$M=\sum_{m} m P_{m}$$ where $P_{m}$ is the projector onto the eigenspace of $M$ with eigenvalue $m$. May 18, 2022 at 5:12
• From my perspective, an observable is an object to describe measurement, we know it so we can measure it. If we don't know it, there's no way to measure it. May 18, 2022 at 5:15