The world’s leading publication for data science, AI, and ML professionals.

Simulated Quantum Computation of Molecular Energies using VQE

Calculating the Ground State Energy of Hamiltonian using Variational Quantum EigenSolver

Figure 1: Periodic table (Image by Elchinator from Pixabay, Source https://pixabay.com/illustrations/periodic-system-chemistry-science-3962844/)
Figure 1: Periodic table (Image by Elchinator from Pixabay, Source https://pixabay.com/illustrations/periodic-system-chemistry-science-3962844/)

Nowadays, numerous physical problems can be solved using Quantum Computing. One such application lies in Quantum Chemistry, which is simulating the molecule for finding Ground State energy. The Ground state of an atom refers to an unexcited state where the electrons are in their lowest energy levels. Calculating the Ground state energy is extremely important for many-body physics, molecular dynamics, condensed matter physics and various other disciplines. For illustration, it is interesting to know the Ground state of a system in condensed matter, as it informs us about the system’s behaviour at low temperatures, where the quantum effects are usually the strongest. For example, when metal is considered at room temperature, the relevant temperature scale is the Fermi temperature, which may be hundreds of Kelvins. So a room temperature metal could be considered to be in its Ground state with some excitations on top. Thus calculating Ground state energies is an important task.

Simulation of larger molecules presents a barrier for Classical Computing due to its limited computing power. This is where Quantum Computers can provide an efficient alternative. As one of the greatest minds of all times, Richard Feynman says,

Nature isn’t classical, dammit. And if you want to make a simulation of nature, you’d better make it quantum mechanical.

Thus, compared to Classical Computers, Quantum Computers are a perfect tool for simulating Hamiltonian to find Ground state energies. This is performed using the algorithm called Variational Quantum Eigensolvers (VQE). Therefore, the study of Variational Quantum Eigensolvers is of key importance in Quantum Computing. In this article, I will try to explicate this approach with minimal Mathematics. Let’s get to the VQE concept and explore its working.

Variational Quantum Eigensolver

VQE is a hybrid Quantum-Classical algorithm used for computing the Ground state of Quantum systems. The VQE uses the variational method to reach the lowest energy state (the Ground-state) of a system under consideration. Briefly speaking, in this algorithm, a Quantum Computer is used for creating a wave function Ansatz of the molecule and calculate the expectation value using the Hamiltonian. A classical optimizer is used for optimizing the variational parameters in the Quantum circuit. The Hamiltonian ‘H’, used for the purpose, is always a Hermitian matrix. That means the conjugate transpose of H is always equal to itself. The spectral theorem states that the eigenvalues of a Hermitian matrix must be real. As any measurable quantity must be real, Hermitian matrices are suitable for describing the Hamiltonians of quantum systems. To understand the operation of this algorithm, we need to comprehend some basic concepts in mathematics.

An eigenvector of matrix A is invariant of the transformation A applied to the vector space. That means, after the application of matrix operation on vector space, the direction of the particular eigenvector remains unchanged, but the magnitude of the vector may vary. This concept can be understood more lucidly from the below diagram,

Figure 2: Visualization of EigenVectors (Source https://gfycat.com/fluffyminiaturebackswimmer)
Figure 2: Visualization of EigenVectors (Source https://gfycat.com/fluffyminiaturebackswimmer)

As seen from the above image, even after applying the transformation A on the vector space, the direction of the two vectors represented in yellow is unaltered. These are called the eigenvectors for transformation A, as the only effect of the transformation is to stretch or squish the magnitude. In contrast to this, the pink vector gets knocked off from its initial direction. That is the span of that vector changes. Hence, it cannot be considered as an eigenvector for this transformation A.

Every 2D transformation doesn’t have to have an eigenvalue. Some transformations may not have any eigenvectors. For example, the transformation of rotating the 2D vector space by 90⁰ in anticlockwise direction may not have any eigenvectors as it changes the span of all the possible vectors. Thus, the eigenvalues for such a transformation are imaginary, denoting that no eigenvectors are possible for that transformation in the 2D plane.

Using the same logic, the equation for Matrix-vector multiplication and scalar-vector multiplication is given as,

H|ψᵢ> = λᵢ |ψᵢ>

where |ψᵢ> is the eigenvector and λ is the Eigen energies or eigenvalues for corresponding Eigen vector . We are interested particularly in the least eigen energy level thus,

H|ψ𝔤> = E𝔤 |ψ𝔤>

where, E𝔤 = λ𝔤 = min(λ)

Thus we are interested in E𝔤, which is the smallest eigenvalue associated with the Hamiltonian. The VQE parameterizes the space of possible quantum states and optimizes the parameters to find the set of parameters with the least energy. Thus to optimize the parameters, iterative varying of the Ansatz is necessary. This is achieved by a linear transformation of Universal gates in the Variational Ansatz. Iterative optimization of parameters over |ψ> aims to yield the expectation value,

<ψ𝔤|H|ψ𝔤> ∼ λ𝔤, which is our expected value for the ground state

The algorithm used for finding Ground state using VQE

For calculating the Ground state energy, the most important thing is the Hamiltonian of the system. Pennylane provides various inbuilt libraries for processing and calculating the Hamiltonian of the molecule. For calculating the Hamiltonian in Pauli X, Y and Z gates, complex computations are required. But these computations can be cut off using the pennylane molecular_hamiltonian() function. This function takes input parameters as follows,

  1. Symbols and Coordinates: Geometry of the interested molecule can be downloaded from various databases like NIST Chemistry WebBook, ChemSpider, and SMART-SNS. From this geometry using the function qchem.read_structure(GEOMETRY_FILE), we can calculate the Symbols and Coordinates of the structure.
  2. Charge: Not always the molecule of which the Ground state energy is to be found out, has a Neutral charge. Depending on the number of electrons added or removed, the charge on the atom changes. This can be taken care of by using the Charge parameter.
  3. Active electrons and orbitals: The number of active electrons and orbitals can be changed by tweaking this specific parameter. If not specified explicitly, all electrons and orbitals are considered active.
  4. Mapping: To solve the problem on the Quantum Devie, it is necessary to convert fermionic Hamiltonian to the qubit Hamiltonian(Pauli gates). This transformation method is selected using Mapping.

These parameters can be feed to the inbuilt function qchem.molecular_hamiltonian(), which provides us with the required Hamiltonian in Pauli X, Y and Z bases.

For this article, I have considered it a simple Hamiltonian, requiring only two wires(qubits). The Hamiltonian for which the Ground state energy is calculated is given below,

(32.7658547225) [I0]
+ (-2.1433035249352805) [X1]
+ (-2.1433035249352805) [Z0 X1]
+ (17.015854722500002) [Z1]
+ (3.913118960624632) [X0 Z1]
+ (-23.359145277499998) [Z0 Z1]
+ (-3.913118960624632) [X0]
+ (-26.859145277499998) [Z0]

where X and Z are Pauli gates applied on the respective qubits.

After finding the Hamiltonian, we want to design an Ansatz U(α), which is a critical task. The Ansatz should be fairly complex so that the computations are done perfectly and not too complex that the optimization of parameters takes long enough time. For this specific problem, the Ansatz is designed using the below user-defined function.

def variational_ansatz(params, wires):
    n_qubits = len(wires)
    n_rotations = len(params)
    #print(wires," ",n_qubits," ",n_rotations,"n")

    if n_rotations > 1:
        n_layers = n_rotations // n_qubits
        n_extra_rots = n_rotations - n_layers * n_qubits
        # Alternating layers of unitary rotations on every qubit                   .       # followed by a ring cascade of CNOTs.
        for layer_idx in range(n_layers):
            layer_params = params[layer_idx * n_qubits : layer_idx *   .           n_qubits + n_qubits, :]
            qml.broadcast(qml.Rot, wires, pattern="single",               .           parameters=layer_params)
            qml.broadcast(qml.CNOT, wires, pattern="ring")
        extra_params = params[-n_extra_rots:, :]
        extra_wires = wires[: n_qubits - 1 - n_extra_rots : -1]
        qml.broadcast(qml.Rot, extra_wires, pattern="single",               .       parameters=extra_params)
    else:
        # For 1-qubit case, just a single rotation to the qubit
        qml.Rot(*params[0], wires=wires[0])

This algorithm uses an Adam optimizer for parameter optimization with a learning rate of 0.1. The number of iterations is selected as 250 iterations. These hyperparameters can be tweaked depending on the application and the convergence of the Ground-state energy. Finally, after the 250 iterations, the optimized parameter set is used in the Ansatz for visualization purposes. The Ansatz with optimized parameters is shown below,

Figure 3: Ansatz structure with optimied paramters for the given Hamiltonian
Figure 3: Ansatz structure with optimied paramters for the given Hamiltonian

To summarise the algorithm,

Figure 4: Flowchart for VQE
Figure 4: Flowchart for VQE

Conclusion

  1. The algorithm took around 15 sec to execute. The output is as follows:
Figure 5: Output of the algorithm
Figure 5: Output of the algorithm

The visualization of the decreasing energy of the state is illustrated below,

Figure 6: Visualization of the reducing energy of Hamiltonian using VQE
Figure 6: Visualization of the reducing energy of Hamiltonian using VQE

This graph clearly shows that the energy of the state starts from -0.6258 Ha and reduces and reaches its Ground state, which is around -2.0457 Ha.

As for this problem, the Ansatz is well-chosen and the Hamiltonian is less complex, the optimizer did not stuck in the local minimum. But in some cases, the optimizer may get stuck in a local minimum for complex Hamiltonians and Ansatz function. For avoiding this situation, hyperparameter tuning plays an important role.

Complete code can be found on Github.


References

  1. https://pennylane.ai/qml/demos/tutorial_vqe.html
  2. https://grove-docs.readthedocs.io/en/latest/vqe.html
  3. https://qiskit.org/textbook/ch-applications/vqe-molecules.html
  4. https://grove-docs.readthedocs.io/en/latest/vqe.html
  5. https://youtu.be/PFDu9oVAE-g

For more such stories related to Quantum Computing and Machine Learning, follow me on Medium. Also, check out my Github and Linkedin.


Related Articles