In this computational essay, we will explore how to apply Variational Quantum Linear Solver algorithm, proposed by Bravo-Prieto et al, in order to solve linear systems. We will compare results vs classical linear solver in Mathematica, and also symbolic and numeric quantum cases.

Paclet Installation

Wolfram quantum framework is a paclet. So first, let’s install it:
In[]:=
PacletInstall["https://wolfr.am/DevWQCF",ForceVersionInstall->True]
Out[]=
PacletObject
Name: Wolfram/QuantumFramework
Version: 1.4.0

In[]:=
Get["Wolfram`QuantumFramework`"]
Load additional functions one needs for quantum variational linear solvers:
In[]:=
Needs["Wolfram`QuantumFramework`ExampleRepository`"]

Theory

The Variational Quantum Linear Solver (VQLS) is a a hybrid quantum-classical technique designed to address Quantum Linear Systems Problem (QLSP). It aims to find linear system solutions using quantum computing techniques. The corresponding quantum algorithm can be summarized as follows:
Given:
◼
  • A quantum state
    |b〉
    such that
    |b〉=U|0〉
  • ◼
  • n
    2
    x
    n
    2
    dimensional matrix
    A
    such that
    A=
    L-1
    ∑
    0
    c
    l
    A
    l
    where
    c
    l
    ϵ
    and
    A
    l
    are unitary gates
  • One wants to prepare:
    ◼
  • A quantum state
    |x〉
    such that
    |ψ〉=A|x〉
    and
    |ψ〉
    
    ψ
    
    ψ
    
    ->|b〉
  • VQLS Approach
    

    QLSP introductory example

    In this implementation we will consider the first qubit as our auxiliary qubit, since in the Wolfram QuantumFramework negative qubits and qubit 0 are used only for classical bits.
    For this example we will consider a 2 qubits system plus an auxiliary qubit.
    A=
    X
    2
    H
    3
    |b〉=U|0〉=
    H
    2
    H
    3
    |0〉
    where the operator’s index indicate the qubit it is applied on.
    Given the ansatz:
    |x〉=V(ω)|0〉=[
    R
    y
    (
    ω
    1
    )
    R
    y
    (
    ω
    2
    )]
    H
    2
    H
    3
    |0〉
    We will proceed to build the circuit to minimize
    C
    L
    .

    Circuit Implementation

    First we define the gates associated with the example:
    The unitary matrix such that
    |b〉=
    U
    b
    |0〉
    :
    In[]:=
    Ub=QuantumCircuitOperator[{"H"->{2},"H"->{3}},"Label"->"
    U
    b
    "];
    Because our goal is to conduct a Hadamard test, it’s necessary for the unitary operation
    A
    to be controlled by the state of an auxiliary qubit:
    In[]:=
    ControlledAGate=QuantumCircuitOperator[{"CNOT"->{1,2},"CH"->{1,3}},"Label"->"
    A
    2
    "];
    Next, we implement the variational quantum circuit that generates a guess for
    |x〉
    :
    |x〉=V(ω)|0〉=[
    R
    x
    (
    ω
    1
    )
    R
    x
    (
    ω
    2
    )]
    H
    2
    H
    3
    |0〉
    The first layer prepare an equal superposition of states and the second layer is the ansatz proposed before:
    In[]:=
    VariationalBlock=QuantumCircuitOperator[​​{"H"->{1,2},​​"RY"[ω1]->{1},"RY"[ω2]{2}},​​"Label"->"V(ω)","Parameters"->{ω1,ω2}];
    In order to obtain
    μ
    l,l',j
    we will implement a circuit that will deploy a Hadamard test:
    1. We first apply a Hadamard in the auxiliar qubit to prepare a state in superposition.​2. We apply a Phase Shift to estimate the imaginary or real part of the μ: Re[μ] when θ  0 and Im[μ] when θ 
    -
    π
    2
    .​3. Apply the Variational Circuit to generate a guess on
    |x〉
    .​4. Apply the Controlled Gate corresponding to the
    A
    l
    component.​5. Apply the adjoint
    U
    b
    , but in this case is the same as
    U
    b
    .​6. Implement a controlled Z operator from the auxiliar qubit to the system qubit j. For the normalization calculation, we will use j = -1 to apply the identity.​7. From this step, we will “undo” our operations, first by applying
    U
    b
    .​8. Apply the
    A
    l
    Controlled gate‘s adjoint, which is the same as
    A
    l
    .​9. Last Hadamard gate for the Hadamard test​At the end we apply a QuantumMeasureOperator to measure the expectation values.
    In[]:=
    Clear[LocalHadamardCircuit];​​LocalHadamardCircuit[j_]:=QuantumCircuitOperator[{​​"Hadamard"->{1},​​"P"[θ],​​VariationalBlock->{2,3},​​ControlledAGate,​​Ub,​​If[!MatchQ[j,-1],"CZ"->{1,j},Nothing],​​Ub,​​ControlledAGate,​​"Hadamard"->{1},​​QuantumMeasurementOperator["Z"->{-1,1},{1}]​​},​​"Parameters"->{ω1,ω2,θ}​​]
    You can test the multiple variations of the circuit for each controlled gate:
    In[]:=
    LocalHadamardCircuit[2]["Diagram",ImageSize->Large]
    Out[]=

    Optimized Circuit Implementation

    As you might notice, the past implementation might be slow if we want to obtain the expected value for each combination. Moreover, the local cost function that we will implement later to optimize will need to calculate the mean value multiple times.
    Instead we could try to use the useful KroneckerProduct to build the same circuit with all the variations.
    We can re-implement
    CZ
    gate, since it depends in which qubit it will be applied on:
    In[]:=
    ControlledGateZ=QuantumOperator​​QuantumOperator["CZ"->{1,2}]*KroneckerDelta[j,2]+QuantumOperator["CZ"->{1,3}]*KroneckerDelta[j,3]+QuantumOperator["I"]*KroneckerDelta[j,-1],​​"Label"->"Controlled
    Z
    j
    Gate"​​;
    As you might notice, we just apply a KroneckerDelta to get rid of the qubits we won’t be using for each case, this is possible since we already know how many qubits we are working on.
    As the previous subsection, we implement the Hadamard test:
    In[]:=
    ClearAll[LocalHadamardCircuit];​​LocalHadamardCircuit=QuantumCircuitOperator[{​​"Hadamard"->{1},​​"P"[θ],​​VariationalBlock->{2,3},​​ControlledAGate,​​Ub,​​ControlledGateZ,​​Ub,​​ControlledAGate,​​"Hadamard"->{1}​​},"Parameters"->{ω1,ω2,j,θ}];
    Check out the newly implemented circuit diagram:
    In[]:=
    LocalHadamardCircuit["Diagram",ImageSize->Large]
    Out[]=

    Local Cost Function

    We just obtained the Symbolic Mean value of our circuit, in order to replace the variables quickly we define the following function:
    Now we will implement all the functions needed for the cost function defined in the Theory section:
    Next, implement the normalization term:

    Symbolic Cost Function Optimization

    Let’s calculate the symbolic equation for the cost function. It is not that huge of an expression, but surely not visually pleasant to show here:
    Since we have the symbolic function, we can use NMinize:

    Numerical Cost Function Optimization

    We can use our previously defined LocalCost for numerical calculations using a numerical gradient descent:

    Classical Linear System Solving

    Solve the system by operating over the correspondent matrices:
    Finally, compute the probabilities:

    Symbolic & Numeric Quantum Circuit Result

    Test the Variational circuit with the optimized parameters results:
    The symbolic optimization resultant probabilities:
    The numerical optimization resultant probabilities:
    We can calculate how different one distribution is from another using a Kolmogorov Smirnov Test:
    In this case, the number is considered big enough to indicate that the results are from the reference distribution.
    Let’s plot the probabilities for a visual comparison:

    QLSP implemented in Rigetti Quantum Computer

    This QLSP was already tested by Bravo-Prieto using Rigetti’s quantum chip 16Q Aspen-4.
    For this example we will consider a 3 qubits system plus an auxiliar qubit.
    where the operator’s index indicate the qubit it is applied on.
    Given the ansatz:

    Circuit Implementation

    As the previous QLSP, we define the gates associated with the example:

    Local Cost Function

    Now let’s calculate the symbolic mean value:
    Finally we use the ket and bra obtained to calculate the expected value:
    Now we will implement all the functions needed for the cost function defined in the Theory section:
    Now we will implement the normalization term. In this case l and lp values correspond to our system qubits:

    Symbolic Cost Function Optimization

    Let’s calculate the symbolic equation for the cost function. It is not that huge of an expression, but surely not visually pleasant to show here:
    Since we have the symbolic function, we can use NMinimize:

    Numerical Cost Function Optimization

    We can use our previously defined LocalCost for numerical calculations using a numerical gradient descent:

    Classical Linear System Solving

    Solve the system by operating over the corresponding matrices:
    Finally, compute the probabilities:

    Symbolic & Numeric Quantum Circuit Result

    Test the Variational circuit with the optimized parameters results:
    The symbolic optimization resultant probabilities:
    The numerical optimization resultant probabilities:
    We can calculate how different one distribution is from another using the Kullback–Leibler divergence:
    Since the results are small, it is an indicator that the distributions are similar.
    We can also employ a Kolmogorov Smirnov Test:
    In this case, the number is considered big enough to indicate that the results are from the reference distribution.
    Let’s plot the probabilities for a visual comparison:

    QLSP randomly generated

    and we are given the state:
    Given the ansatz

    Circuit Implementation

    Following the same steps, we define the gates associated with the example:
    Ansatz variational circuit:
    As the previous example, we implement the Hadamard test:

    Local Cost Function

    Calculate the symbolic mean value of our function:
    Now we will implement the functions to calculate the local cost for this circuit:
    In this LocalHadamardMeanValues version, we call LocalHadamardStateVectors[{l, lp, j}] for the state vector already computed to save time per each execution of LocalCost:
    Implement the normalization term:

    Cost Function Optimization

    We will calculate the gradient descent for our cost function:

    Classical Linear System Solving

    Solve the system by operating over the correspondent matrices:
    Finally, compute the probabilities:

    Symbolic & Numeric Quantum Circuit Result

    We can build the Variational circuit from the example and test it with the optimized parameters result:
    Test the Variational circuit with the optimized parameters results:
    The optimization resultant probabilities:
    We can calculate how different one distribution is from another using the Kullback–Leibler divergence:
    Since the results are small, it is an indicator that the distributions are similar.
    We can also employ a Kolmogorov Smirnov Test:
    In this case, the number is considered big enough to indicate that the results are from the reference distribution.
    Let’s plot the probabilities for a visual comparison:

    References

    ◼
  • Bravo-Prieto, C., LaRose, R., Cerezo, M., Subasi, Y., Cincio, L., & Coles, P. J. (2019). Variational Quantum Linear Solver. arXiv. https://doi.org/10.48550/ARXIV.1909.05820
  • ◼
  • Mari, A. Variational Quantum Linear Solver. PennyLane Demos. https://pennylane.ai/qml/demos/tutorial_vqls/
  • CITE THIS NOTEBOOK

    Variational quantum linear solver​
    by Sebastian Rodriguez​
    Wolfram Community, STAFF PICKS, May 22, 2024
    ​https://community.wolfram.com/groups/-/m/t/3180154