The variational eigensolver (VQE) looks for the lowest energy \langle H\rangle by tuning the dials of a small circuit, the ansatz. Here the circuit has one dial: the one-qubit state
H is a list of pairs (coefficient, letter) with letters "I", "X", "Y", "Z", and \langle H\rangle = \sum_k c_k \langle\psi|P_k|\psi\rangle, where I is the identity, X=\begin{pmatrix}0&1\\1&0\end{pmatrix}, Y=\begin{pmatrix}0&-i\\i&0\end{pmatrix}, Z=\begin{pmatrix}1&0\\0&-1\end{pmatrix} and \langle\psi|P|\psi\rangle = \sum_i \overline{\psi_i}(P\psi)_i. A letter may appear more than once.
Write best_angle(terms) that returns the pair (theta, value): the angle \theta in [0, 2\pi) at which \langle H\rangle is smallest, and that smallest value.
- If \langle H\rangle does not depend on \theta at all, return \theta = 0. Otherwise it really does depend on it: the X and Z coefficients together have size \sqrt{c_X^2 + c_Z^2} \ge 0.05, so a flat curve is exactly flat.
- Otherwise exactly one angle in [0, 2\pi) is best. Return
0.0if your angle comes out within1e-9of 2\pi; no test has a best angle in (2\pi - 10^{-6}, 2\pi).
Press Run with ts = [k * 2 * math.pi / 100 for k in range(101)] and a plot of the energy at each angle to see the curve. Then compare value with the smallest eigenvalue of the 2 \times 2 matrix \sum_k c_k P_k, np.linalg.eigvalsh(M)[0]: when are they different?
Examples
Input: terms = [(1, "Z")]
Output: (3.141592653589793, -1.0)
Explanation: theta = pi gives the state |1>, and <Z> = -1.
Input: terms = [(1, "Z"), (1, "X")]
Output: (3.9269908169872414, -1.4142135623730951)
Input: terms = [(0.5, "I"), (2, "Y")]
Output: (0, 0.5)
Explanation: the amplitudes of R_y(theta)|0> are real, so <Y> = 0 for every theta: the energy is always 0.5.
Both numbers are compared with a tolerance of 1e-6.
Goals
- Write the energy of the one-parameter state $R_y(\theta)|0\rangle$ as a function of $\theta$
- Find the angle that minimises it, with a stated rule for ties and the angle range
- See that a small ansatz can miss the true smallest eigenvalue