QHack 2023: 4 week prep challenge, day 7, VQE

I am continuing my preparations, following the QHack 2022 challenges. I’ll tackle the QML track, again starting from the most basic example, I am trying to solve them correctly, and learn as much as I can along the way.

Knowing a field, always requires the knowledge of it’s language. I recall the days in my physics studies, where different branches used different words of the same entities – it gave me a really hard time. Interesting are also the cases where you are using a “default” option, out of many possible options, and are surprised when someone diligently states the choice explicitly: Most calculations in quantum computing are performed in the computational basis, i.e. the eigenbasis of the Pauli-Z operator

\[ \sigma_z = \left(\begin{array}{rrr} 1 & 0 \\ 0 & -1 \end{array}\right) \]

A notable alternative is the Fourier basis. Goal of this exercise, will be to approximate the Fourier transform of a given state in the computational basis, using a VQE routine.

Approximate the QFT of a state

To solve this exercise, it is sufficient to implement the pre-sketched ansatz, and a cost function.

@qml.qnode(dev)
def circuit(angles):
    """This is the quantum circuit that we will use."""
     # Add the template of the statement with the angles passed as an argument.
    for i, angle in enumerate(angles):
        qml.Hadamard(wires=i)
        qml.RZ(angle, wires=i)

    # We apply QFT^-1 to return to the computational basis.
    # This will help us to see how well we have done.
    qml.adjoint(qml.QFT)(wires=range(n_qubits))

    # We return the probabilities of seeing each basis state.
    return qml.probs(wires=range(n_qubits))

def error(angles):
    """This function will determine, given a set of angles, how well it approximates
    the desired state. Here it will be necessary to call the circuit to work with these results.
    """

    probs = circuit(angles)
        
    # The return error should be smaller when the state m is more likely to be obtained.
    return 1-probs[m]

The tests check out, a very simple exercise.

This task is mostly academic, there is a general algorithm to perform the QFT and the solution puts much computational effort in finding the FT of a single state. However, I used my daily time budget to get a quick refresher on the QFT and it’s usages, as well as to do some exploratory reading. So it was again a productive day.