# Measuring the Hamiltonian in the VQE

I am trying to implement VQE in pyQuil and am dumbfounded by how to measure the expectation value of a general Hamiltonian on $$\mathbb{C}^{2^n}$$ i.e. determine $$\langle\psi , H \psi\rangle$$ on a Quantum computer. As far as I understand on a real Quantum Computer (not any quantum virtual machine) I can only measure in the computational basis, which is the basis of the Hamiltonian $$H = X = \sum x \left|x\right>\left, but not for any Hamiltonian whose eigenvectors are not the computational basis. But how do I measure with any Hamiltonian that is not diagonal in the computational basis?

Sure I can measure e.g. some of the qubits in the $$X$$-basis instead of the $$Z$$-basis by applying a Hadamard gate to them, but this surely doesn't help me if I want to measure sth. non-local, i.e. if the ground-state of my hamiltonian is an entangled state.

On a maybe related note: Can I write any hamiltonian (hermitian matrix) as a Pauli decomposition? I know I can for a single qubit, but does this hold for multiqubits aswell?

Yes, you can decompose any Hamiltonian. For VQE purposes, any finite-dimensional Hamiltonian can be presented as a sum of terms which consist of tensor products of Pauli matrices (https://arxiv.org/abs/1304.3061):

$$H = \sum_{\alpha, i} h^{\alpha}_{i} \sigma^{\alpha}_{i} + \sum_{\alpha, \beta, i, j} h^{\alpha \beta}_{ij} \sigma^{\alpha}_{i} \sigma^{\beta}_{j} + \dots$$

As such, the expected value $$\langle H \rangle$$ can be estimated by measuring the expected values of such combinations of Pauli matrices. There may be trouble if the quantity of these terms grows exponentially in the size of the system, but many interesting Hamiltonians decompose into a polynomial number of operators.

The coefficients of the decomposition can be obtained by making a scalar product of the Hamiltonian with the basis term: $$(H, A) = \frac1d \mathrm{Tr}(HA)$$.

No. The computational basis is not necessarily the basis that diagonalizes the Hamiltonian. It also looks like you are confusing the X basis with the basis of the Hamiltonian.

Advice: You should write it as $$H = \sum \lambda_i | \lambda_i \rangle \langle \lambda_i |$$ not with $$x$$ so you don't confuse with the X operator. The right notation will help you avoid confusing yourself.

• Sure, that is precisely my problem. In fact the computational basis only diagonalizes the Hamiltonian $H=X$. So my question is what to do about all the other Hamiltonians out there? I edited my question to hopefully clarify what my actual question is Commented Nov 18, 2018 at 3:36