In the following example we will illustrate how to apply the quantum natural gradient descent optimization for parametrized quantum circuits by calculating the Fubiny–Study metric tensor through different approaches.

Paclet Installation

Wolfram quantum framework is a paclet. So first, let’s install it:
PacletInstall["https://wolfr.am/DevWQCF",ForceVersionInstall->True]
<<Wolfram`QuantumFramework`

Theory

An optimization step in a gradient descent is given by:
θ
t+1
=
θ
t
-η∇ℒ(θ)
where ℒ(θ) is the cost function with parameters θ, and η is the step rate. Each step in this approach assumes a flat Euclidean space, which would lead us to not–unique parametrizations that can disrupt our optimization, specially in singular points. In order to avoid this problem we must use non-Euclidean parameter spaces, performing what is called a natural gradient descent.
The quantum state space features an invariant metric tensor referred to as the Fubini–Study metric tensor
g
ij
, which can be used to develop a quantum version of a natural gradient descent. Then each optimization step is given by:
θ
t+1
=
θ
t
-η
+
g
(θ)∇ℒ(θ)
where
+
g
the Fubini-Study metric tensor pseudo-inverse.
In the following example we will demonstrate two methods to calculate the Fubini–Study metric tensor:
◼
  • Using the cost function indistinguishability in a variational quantum eigensolver setting.
  • ◼
  • Using a block–diagonal approximation.
  • In both cases we will perform a natural gradient optimization and compare it with classical methods.

    Variational quantum eigensolver (VQE)

    The fundamental process of the Variational Quantum Eigensolver (VQE) involves using a brief quantum circuit U(θ), characterized by parameters θ =
    {
    θ
    1
    , . . . ,
    θ
    m
    }
    , to iteratively adjust θ to minimize the average energy or cost function:
    f(θ)=〈ϕ(θ)|H|ϕ(θ)〉
    for the ansatz
    |ϕ(θ)〉=U(θ)|0〉
    .
    In the VQE setting the Fubiny-Study metric follows:
    g
    ij
    =Re
    ∂
    i
    ϕ
    
    ∂
    j
    ϕ
    -
    ∂
    i
    ϕ
    
    ϕ
    
    ϕ
    
    ∂
    j
    ϕ
    ​​
    where
    |
    ∂
    i
    ϕ(θ)〉=
    ∂
    i
    |ϕ(θ)〉/∂
    θ
    i
    .

    Block-diagonal metric tensor

    We can calculate
    g
    using an approximation of the Fubini-Study metric tensor for a variational quantum circuit, named the block-diagonal approach:
    (l)
    g
    ij
    =〈
    ψ
    l-1
    |
    K
    i
    K
    j
    |
    ψ
    l-1
    〉-〈
    ψ
    l-1
    |
    K
    i
    |
    ψ
    l-1
    〉〈
    ψ
    l-1
    |
    K
    j
    |
    ψ
    l-1
    〉​​
    The circuit is given by:
    U(θ)|
    ψ
    0
    〉=
    V
    L
    (
    θ
    L
    )
    W
    L
    ...
    V
    l
    (
    θ
    l
    )
    W
    l
    ...
    V
    0
    (
    θ
    0
    )
    W
    0
    |
    ψ
    0
    〉
    where W are layers of non-parametrized gates and
    (l)
    K
    i
    are the generators of the parametrized gates V of the form
    (l)
    θ
    i
    (l)
    K
    i
    
    .
    The quantum state before the application of layer l is:
    |
    ψ
    l-1
    〉=
    V
    l-1
    (
    θ
    l-1
    )
    W
    l-1
    ...
    V
    0
    (
    θ
    0
    )
    W
    0
    |
    ψ
    0
    〉

    Defining Helper Functions
    

    Single qubit example

    We can solve a single qubit example using VQE algorithm. We need to drive the following ansatz state to the ground state of the Hamiltonian
    H=
    σ
    x
    In[]:=
    singleState=QuantumState[{Cos[θ1],Exp[2*I*θ2]*Sin[θ1]},"Parameters"->{θ1,θ2}];​​singleState["Formula"]
    Out[]=
    Cos[θ1]|0〉+
    2θ2
    
    Sin[θ1]|1〉

    Fubini-Study metric tensor calculation

    Following the theory, we can implement a function to calculate the Fubini-Study metric tensor from a
    |ϕ(θ)〉
    state:
    We will need the correspondent ϕ ket and bra. Since it is a simple exam\bple, we will use fancy notation for our variables:
    In[]:=
    |ϕ〉=singleState["StateVector"];
    In[]:=
    〈ϕ|=singleState["Dagger"]["StateVector"];
    Follow the equation
    Re
    ∂
    i
    ϕ
    
    ∂
    j
    ϕ
    -
    ∂
    i
    ϕ
    
    ϕ
    
    ϕ
    
    ∂
    j
    ϕ
    
    for
    θ
    i
    ={
    θ
    1
    ,
    θ
    2
    }
    In[]:=
    singleStateFubini[{θ1_,θ2_}]=Table[​​Re[D[〈ϕ|,θi].D[|ϕ〉,θj]]-D[〈ϕ|,θi].|ϕ〉〈ϕ|.D[|ϕ〉,θj],​​{θi,{θ1,θ2}},{θj,{θ1,θ2}}​​]//Normal//ComplexExpand//FullSimplify;​​singleStateFubini[{θ1,θ2}]//MatrixForm
    Out[]//MatrixForm=
    1
    0
    0
    2
    Sin[2θ1]

    Cost function

    Let’s calculate the energy (cost) function:
    In[]:=
    singleStateCost[θ1_,θ2_]=〈ϕ|.PauliMatrix[1].|ϕ〉;​​singleStateCost[θ1,θ2]//ComplexExpand//FullSimplify
    Out[]=
    Cos[2θ2]Sin[2θ1]
    Proceed to calculate the correspondent gradient for each
    θ
    i
    :
    In[]:=
    singleStateCostGrad[θ1_,θ2_]=Grad[singleStateCost[θ1,θ2],{θ1,θ2}]//ComplexExpand//Simplify
    Out[]=
    {2Cos[2θ1]Cos[2θ2],-8Cos[θ1]Cos[θ2]Sin[θ1]Sin[θ2]}

    Test using
    θ
    1
    =π/12and
    θ
    2
    =π/12

    For our gradient descent and quantum gradient descent we will be using GradientDescent and QuantumNaturalGradient functions, with
    η=0.05
    for 50 steps:
    singleStateQuantumParameters=QuantumNaturalGradientDescent[{π/12,π/12},0.05,singleStateFubini,singleStateCostGrad,50];
    In[]:=
    singleStateVainillaParameters=GradientDescent[{π/12,π/12},0.05,singleStateCostGrad,50];
    Let’s plot our variables and their correspondant cost function to compare the optimization steps:
    Out[]=
    Quantum Natural Gradient Descent
    Gradient Descent
    Analyzing the trajectory we discover that the Natural Gradient Descent gets as optimum point closest to
    P
    0
    the point
    P
    *
    ={-π/4,0}
    . The main issue is that the line
    θ
    1
    =0
    is a singular point and depending on the metric, it may cause the distance to change. In the other hand, the Quantum Natural Gradient Descent gets as optimum point closest to
    P
    0
    the point
    
    P
    *
    ={π/4,π/2}
    which does not cross the singular point.
    In[]:=
    ListLinePlot[{singleStateCost@@@singleStateQuantumParameters,singleStateCost@@@singleStateVainillaParameters},PlotRange->All,​​PlotLegends->{"Quantum Natural Gradient Descent","Gradient Descent"},FrameLabel->{"Optimization steps","Cost Function Value"},Frame->True,GridLines->Automatic]
    Out[]=
    Quantum Natural Gradient Descent
    Gradient Descent
    The ground state is reached in less steps by the quantum gradient descent.

    Test using
    θ
    1
    =5π/12and
    θ
    2
    =π/12

    In[]:=
    singleStateQuantumParameters2=QuantumNaturalGradientDescent[{5π/12,π/12},0.05,singleStateFubini,singleStateCostGrad,50];
    In[]:=
    singleStateVainillaParameters2=GradientDescent[{5π/12,π/12},0.05,singleStateCostGrad,50];
    Out[]=
    Quantum Natural Gradient Descent
    Gradient Descent
    As the previous example, now the singularity is found at
    θ
    1
    =π/2
    . The quantum descent result is the same as the last example, the regular algorithm fails to notice the singularity.
    In[]:=
    ListLinePlot[{singleStateCost@@@singleStateQuantumParameters2,singleStateCost@@@singleStateVainillaParameters2},PlotRange->All,​​PlotLegends->{"Quantum Natural Gradient Descent","Gradient Descent"},FrameLabel->{"Optimization steps","Cost Function Value"},Frame->True,GridLines->Automatic]
    Again, the ground state is reached in less steps by the quantum gradient descent.

    Variational Quantum Circuit example

    Circuits & State

    Let’s build a “more complicated” circuit for the following example:

    Block-diagonal implementation

    Consider the following variational quantum circuit

    Step-by-step calculation

    In this case we got 2 parametrized layers with two parameters each, then following the block-diagonal approach:
    Calculate the first layer using the following formula:
    The second layer:
    Then the block diagonal matrix is the following:

    Acknowledgment

    I would like to express my sincere gratitude to Cesar Guerra and the Wolfram Quantum Computation Framework Team for their help through this notebook implementation.

    References

    Yamamoto, N. (2019). On the natural gradient for variational quantum eigensolver (Version 1). arXiv. https://doi.org/10.48550/ARXIV.1909.05074
    Izaac, J. (2019). Quantum natural gradient. PennyLane Demos. https://pennylane.ai/qml/demos/tutorial_quantum_natural_gradient/

    CITE THIS NOTEBOOK

    Natural gradient descent optimization for quantum circuits with Fubiny–Study metric tensor​
    by Sebastian Rodriguez​
    Wolfram Community, STAFF PICKS, February 28, 2024
    ​https://community.wolfram.com/groups/-/m/t/3131997