DEV Community

derwind
derwind

Posted on

Easy VQE

One of the leading algorithms in the NISQ quantum computer is called VQE (Variational Quantum Eigensolver). The name sounds hard, but the explanations are often overwhelmingly difficult1. I would like to approach this with a short approach.

Conclusion

VQE is, in essence, the following:

qc = QuantumCircuit(1)
qc.ry(Parameter("θ"), 0)
print(VQE(Estimator(), qc, SPSA()).compute_minimum_eigenvalue(Pauli("Z")).optimal_parameters)
Enter fullscreen mode Exit fullscreen mode

{Parameter(θ): 3.141592653589793}

The detail

The VQE story is generally accompanied by the following difficult explanations.

  • Quantum chemical calculations
    • Full CI
    • Quantum phase estimation
  • Schrödinger equation and Hamiltonian
  • Second quantization
  • Eigenvalues of the ground state of the Hamiltonian

However, these are mainly related to how to formulate the problem and how to make mathematical models. Therefore, they can be separated from the main routine of VQE.

The main routine of VQE is essentially the same as the optimization routine of deep learning, which is a numerical calculation to find the minimum value of a given cost function.

The code I wrote at the beginning of this post is too rough, but if we write it more carefully, it would be as follows:

from qiskit import QuantumCircuit
from qiskit.quantum_info import Pauli
from qiskit.circuit import Parameter
from qiskit.primitives import Estimator
from qiskit_algorithms.minimum_eigensolvers import VQE
from qiskit_algorithms.optimizers import SPSA


qc = QuantumCircuit(1)
qc.ry(Parameter("θ"), 0)

estimator = Estimator()  # a too to define the cost function
operator = Pauli("Z")  # the objective to analyze
optimizer = SPSA()  # an optimizer

vqe = VQE(estimator, qc, optimizer)  # a trainer which trains a model inside a loop
result = vqe.compute_minimum_eigenvalue(operator)

print(result.optimal_parameters)
Enter fullscreen mode Exit fullscreen mode

Mathematically, we take a function

f(θ)=(10) ⁣(cos(θ2) ⁣ ⁣ ⁣ ⁣sin(θ2)sin(θ2) ⁣ ⁣ ⁣ ⁣cos(θ2)) ⁣ ⁣(1 ⁣ ⁣ ⁣ ⁣00 ⁣ ⁣ ⁣ ⁣1) ⁣ ⁣(cos(θ2) ⁣ ⁣ ⁣ ⁣sin(θ2)sin(θ2) ⁣ ⁣ ⁣ ⁣cos(θ2)) ⁣(10)=(10) ⁣(cos(θ2)sin(θ2)sin(θ2)cos(θ2)) ⁣ ⁣(cos(θ2)sin(θ2))=cosθ \begin{align*} f(\theta) &= (1 \quad 0) \! \begin{pmatrix} \cos(\frac{\theta}{2}) \!\!&\!\! \sin(\frac{\theta}{2}) \\ \sin(\frac{\theta}{2}) \!\!&\!\! \cos(\frac{\theta}{2}) \end{pmatrix} \!\! \begin{pmatrix} 1 \!\!&\!\! 0 \\ 0 \!\!&\!\! -1 \end{pmatrix} \!\! \begin{pmatrix} \cos(\frac{\theta}{2}) \!\!&\!\! -\sin(\frac{\theta}{2}) \\ \sin(\frac{\theta}{2}) \!\!&\!\! \cos(\frac{\theta}{2}) \end{pmatrix} \! \begin{pmatrix} 1 \\ 0 \end{pmatrix} \\ &= (1 \quad 0) \! \begin{pmatrix} \cos(\frac{\theta}{2}) & \sin(\frac{\theta}{2}) \\ \sin(\frac{\theta}{2}) & \cos(\frac{\theta}{2}) \end{pmatrix} \!\! \begin{pmatrix} \cos(\frac{\theta}{2}) \\ -\sin(\frac{\theta}{2}) \end{pmatrix} \\ &= \cos \theta \end{align*}

as the cost function, and θ\theta is to be optimized so that f(θ)f(\theta) is minimized. It is obvious that θ=±π\theta = \pm \pi are optima and take the minimum value 1-1 . Thus, going back to the beginning, we have

{Parameter(θ): 3.141592653589793}

It is quick to start the story where the cost function f(θ)=cosθf(\theta) = \cos \theta is given.

  • Determine randomly the initial value of θ\theta
  • Update θ\theta iteratively with an appropriate optimizer
  • If the loss function value doesn't seem to change, optimization is complete

These procedures are like what we have seen often somewhere... Yes! Deep Learning.

SPSA (Simultaneous Perturbation Stochastic Approximation) is used as an optimizer that fits the situation of NISQ quantum computer, but if we assume an ideal situation where a reliable gradient values can be calculated, there is no problem to optimize with Adam. It is completely an optimization method for deep learning.

Wrap up

I have tried to reduce difficult portions as much as possible to concentrate on the main routine of the VQE, although it becomes a little more complicated than I expected.

There are various types of quantum machine learning, but the above is in essence optimizing a cost function created from a quantum mechanical point of view with an optimization method similar to that often used in deep learning.

While the case of quantum chemical computation is a direct quantum mechanical problem, problems such as the traveling salesman problem and other route optimization problems are not quantum mechanical in nature. However, even for such problems, in some cases, the (mathematical) formulation of the problem can be done within the framework of quantum computation. If you find merit in quantum computation, you can use the formulation in the style of quantum computation and obtain a numerical solution by optimization similar to that used in deep learning.


  1. For example, https://learn.qiskit.org/course/ch-applications/simulating-molecules-using-vqe 

Top comments (0)