Building Variational Quantum Eigensolver from scratch

Let us assume that we are given a 2N×2N dimension matrix (like the one given below).

(1)[1000001001000001].

Today, we will be finding eigenvalues of such matrices using VQE-like circuits for such matrices. While doing so, we will cover the following topics:

  1. Hamiltonian in Pauli Basis
  2. Calculations of Expectation Value
  3. Designing Ansatze
  4. Running Simulations
  5. Running Noisy Simulations
  6. Extension to Excited Energy States
  7. Interpreting the Results

 

Necessary Imports

 

Hamiltonian in Pauli Basis

(2)σ0=(1001)σx=(0110)σy=(0ii0)σz=(1001)

The normalized Pauli matrices {σ0,σx,σy,σz}/2 form an orthogonal basis of M2, this vector space can be endowed with a scalar product called the Hilbert-Schmidt inner product: A,B=Tr(AB). Since the Pauli matrices anticommute, their product is traceless, and since they are Hermitian this implies that they are orthogonal with respect to that scalar product. Hence this property can be used for decomposing a Hamiltonian as:

H=i1,,in={0,x,y,z}hi1,,in12nσi1σin

hi1,,in=12nTr((σi1σin)H)=12nTr((σi1σin)H)

 

Decomposing Hamiltonian into Pauli Terms

 

Composing Hamiltonian into Pauli Terms

 

Test Hamiltonian

(3)H0=[0.705600.0.0.1.12460.1820.0.0.1820.43180.0.00.0.888]

 

 

 

H0=0.2252×I0I1+0.3435×Z00.4347×Z1+0.91×X0X1+0.91×Y0Y1+0.5716×Z0Z1

 

 

Given Hamiltonian

(4)H1=[1.00.0.0.010.0.100.0.00.1.]

 

 

$ H_{1} = 0.5\times (I_{0}I_{1} - X_{0}X_{1} - Y_{0}Y_{1} + Z_{0}Z_{1})$

 

Calculating the Expectation values

For an n-qubit quantum state, measuring one qubit corresponds to projecting the quantum state onto one of two half-spaces determined by the unique eigenvalues of our measurement operator.

By convention, performing a computational basis measurement is equivalent to measuring in measuring Pauli Z, which gives us two eigenvectors |0 and |1, with corresponding eigenvalues ±1. Therefore, doing a computation measurement of a qubit and obtaining 0 means the state of our qubit is in the +1 eigenstate of the Z operator. In general, we can then use the definition of the tensor product to perform multi-qubit measurements as will be explained below.

For some qubit |ψ we have |ψ=α|0+β|1, we can calculate expectation value of a single-qubit operator M=mm|mm|, as M=ψ|mm|mm|ψ=mmψ|mm|ψ=mm p(m)=(+1)p(0)+(1)p(1)=p(0)p(1). Similarly, for a two-qubit operator M we will get M=p(00)+p(11)p(01)p(10).

 

Change of Basis

Therefore, to measure a qubit, we can use any 2×2 operator that is a unitary transformation of Z. That is, we could also use a measurement operator M=UZU, where U is an unitary operator and M gives two unique outcomes of a measurement in its ±1 eigenvectors. Thereofre, to measure in a basis other than the computational (or Z), we need to find the corresponding unitary equivalence. Similar to the one-qubit case, all two-qubit Pauli-measurements can be written as (U1U2)[ZZ](U1U2) for 2×2 unitary matrices U1 and U2.

For X, and Y basis measurements, these unitary equivalence are given below:

X=(H)ZH Y=(HS)Z(HS)

This feature can be exploited to create circuits for both X basis and Y basis measurements from the standard Z basis. It is done by applying the corresponding unitary operation to the prepared quantum state and then performing a measurement in Z basis.

For example: In order to do a Y basis measurement, first apply HS to the quantum state and then do a normal Z basis measurement.

Similar to the one-qubit case, all multi-qubit Pauli-measurements can be written as a generalization of above.

For example: For two-qubit X basis measurement, XX=(HH)[ZZ](HH).

 

Constructing Measurement Circuits

 

Examples

Measurements in XX, YY, ZZ basis

     ┌───┐┌─┐   
q_0: ┤ H ├┤M├───
     ├───┤└╥┘┌─┐
q_1: ┤ H ├─╫─┤M├
     └───┘ ║ └╥┘
c: 2/══════╩══╩═
           0  1 

 

 

     ┌─────┐┌───┐┌─┐   
q_0: ┤ SDG ├┤ H ├┤M├───
     ├─────┤├───┤└╥┘┌─┐
q_1: ┤ SDG ├┤ H ├─╫─┤M├
     └─────┘└───┘ ║ └╥┘
c: 2/═════════════╩══╩═
                  0  1 

 

 

     ┌─┐   
q_0: ┤M├───
     └╥┘┌─┐
q_1: ─╫─┤M├
      ║ └╥┘
c: 2/═╩══╩═
      0  1 

 

Measurements in XY, YZ, ZX basis (To show flexibility of the function)

      ┌───┐      ┌─┐   
q_0: ─┤ H ├──────┤M├───
     ┌┴───┴┐┌───┐└╥┘┌─┐
q_1: ┤ SDG ├┤ H ├─╫─┤M├
     └─────┘└───┘ ║ └╥┘
c: 2/═════════════╩══╩═
                  0  1 

 

 

     ┌─────┐┌───┐┌─┐
q_0: ┤ SDG ├┤ H ├┤M├
     └─┬─┬─┘└───┘└╥┘
q_1: ──┤M├────────╫─
       └╥┘        ║ 
c: 2/═══╩═════════╩═
        1         0 

 

 

          ┌─┐   
q_0: ─────┤M├───
     ┌───┐└╥┘┌─┐
q_1: ┤ H ├─╫─┤M├
     └───┘ ║ └╥┘
c: 2/══════╩══╩═
           0  1 

 

Determining the Ansatz

 

Designing Ansatze

Ansatze are simply a parameterized quantum circuits (PQC), which play an essential role in the performance of many variational hybrid quantum-classical (HQC) algorithms. Major challenge while designing an asatz is to choose an effective template circuit that well represents the solution space while maintaining a low circuit depth and number of parameters. Here, we make a choice of two ansatze, one randomly and another inspired from the given hint.

 

Ansatz 1 (Random Choice)

     ┌─────────────┐     ┌──────────────┐
q_0: ┤ RX(0.74906) ├──■──┤ RZ(-0.10279) ├
     ├─────────────┤┌─┴─┐├─────────────┬┘
q_1: ┤ RX(0.40493) ├┤ X ├┤ RZ(-2.3803) ├─
     └─────────────┘└───┘└─────────────┘ 
c: 2/════════════════════════════════════
                                         

 

Ansatz 2 (From Hint)

     ┌───┐     ┌────────┐
q_0: ┤ H ├──■──┤ RX(pi) ├
     └───┘┌─┴─┐└────────┘
q_1: ─────┤ X ├──────────
          └───┘          
c: 2/════════════════════
                         

 

Running Simulations

Variational Quantum Eigensolver (VQE) is based on Rayleigh-Ritz variational principle. To perform VQE, the first step is to enocde the problem into a Hermitian matrix M, whose expectation value w.r.t a trial wave function |ψ(θ) which is yielded by an ansatz U(θ), i.e., nothing but a unitary paramterized by {θ}.

Here, we optimize these parameters {θ} using scipy.optimize library based on a cost function C(θ), which is nothing but the calculated expectation value of Hamiltonian, H1 for |ψ(θ): C(θ)=ψ(θ)|H1|ψ(θ)

 

Ansatz 1 (Random Choice)

 

 

Visualizing the Result

After looking at multiple results' res.x, we realize that first and second parameters always have the values ±nπ/2 and ±π respectively. So, while keeping them fixed, we do a parameters search-scan in the range θ3,4[0,2π] and visualize how they affect our expectation value.

output1

Ansatz 2 (From Hint)

 

Visualizing the Result

We scan over the paramter θ[0,2π], to visualize the change in expectation value for the ansatz2, which is inspired from the hint given.

 

Number of Measurements (or Shots)

In order to obtain results from a quantum computer, one needs to perform sampling. This allows us to obtain the probability distribution, and hence know the most probable results from the measurement. Here, we see the change of expectation value of H1 with the number of shots, i.e., number of measurements for ansatz 2.

 

Performance of Classical Optimizers

Successful application of hybrid-quantum classical algorithms, with the classical step involving an optimizer, on current hardware, requires the classical optimizer to be noise-aware. In order to study this, we study the change of expectation value of H1 with different classical optimizers for ansatz 2, first in absence of noise, then later in the presence of noise.

 

 

 

Noisy Simulation

Computational power of NISQ processors suffers a lot from the limited capabilities of their physical qubits. This is essentially due to present of decoherence, limited connectivity, and absence of error-correction. Hence, it is essential to see how our ansatz would perform on a real device. Here, we replicate the noise model from a real device, and then test the perfrormance of our VQE module.

Qubit coupling map of this real device is the following:

 

png

 

 

 

Ansatz 1 (Random Choice)

 

 

 

Ansatz 2 (From Hint)

 

 

 

Visualizing the Result (Noisy v/s Mitigated v/s Ideal)

 

 

Number of Measurements (or Shots)

Let's see the change of expectation value of H1 with the number of shots for ansatz 2.

 

Peformance of Classical Optimizers

Let's see the change of expectation value of H1 with different classical optimizers for ansatz 2.

 

 

 

 

Comparing the performance of VQE in presence of errors due to Readout, Thermal Relaxation, Amplitude-Phase damping and Depolarization

Readout

Describes classical readout errors

 

Thermal Relaxation

Single qubit thermal relaxation error is characterized by relaxation time constants T1, T2, and the gate time t.

 

Amplitude-Phase Damping

Single-qubit generalized combined phase and amplitude damping error is given by an amplitude damping parameter λ, and a phase damping parameter γ.

 

Depolarization

N-qubit depolarizing error is given by a depolarization probability p.

 

We see from the above plot that the presence of noise due to depolarization has affected our VQE results the most. This can be explained via its definiton that it is the uniform contraction of the Bloch sphere, parameterized by p. So, we now do a quick comparision between the effects of depolarization on single qubit gates v/s two-qubit gates v/s measure gates.

 

 

Excited States of the Hamiltonian

 

One of the way to find the kth exicted states of a Hamiltonian would be to update the Hamiltonain H to Hk as follows: Hk=H+i=0k1βi|ii| For example: To find the first excited state, we find H1=H+β0|gg|, where g is the ground state of the Hamiltonian. This works because of spectral decomposition.

This can be used to update our cost function, Ck, as follows: C(θk)=ψ(θk)|H|ψ(θk)+i=0k1βi|ψ(θk)|ψ(θi)|2

We'd already calculated the first term in VQE, but to calculate the next summation term, we refer to [2]. It suggests us to rewrite the overlap term |ψ(θk)|ψ(θi)|2 as |00|U(θk)U(θi)|00|2. Therefore, we can prepare the state U(θk)U(θi)|00 using the trial state preparation circuit for current state and ith previously-computed state.

This technique is known as Variational Quantum Deflation and requires the same number of qubits as Variational Quantum Eigensolver and around twice the circuit depth.

 

Ansatz 1

 

Ansatz 2

 

Interpreting the Results

  1. Both of our ansatz produced accurate results despite of ansatz 2 being more expressible than ansatz 1. One reason for this can be that the the ansatz 2 expressibility was covered the interested solution subspace. Another reason could be due to its more entangling capability.
  2. Convergence of VQE depends upon both the type of optimizer (gradient-based or point-search) and the choice of ansatz. In general more the number of parameters, and the depth of ansatz, more difficult will be the convergence.
  3. Number of shots did matter for non-optimal values of theta in ansatz 2. However, at optimal values of theta, less shots also gave good results. For noisy simulations, higher number of shots were required to get agreeable results.
  4. COBYLA and Powell performed far better than others in both noisy and ideal conditions. Next one in line was Nelder-Mead. Gradient-based optimizers gave much poor results. In future, it would nice to see results from specialized optimizers such as rotosolve and rotoselect, and also optimizers that calculate gradients via parameter-shift rule.
  5. Ansatz 2 performed better than ansatz 1 under Noisy simulation. However, mitigation improved the result for both of them.
  6. Out of all the all the noise models tested, we saw VQE still gave the (deviated) minima at the optimal value of theta, and performed worst for errors due to damping and depolarization.
  7. Even with depolarization, presence of errors in measurement gates gave much poor result than two-qubit gates which in turn gave worse results than single-qubit gates.
  8. VQD was able to perform better as we adjusted the values of βi based on the difference between the eigen-energies. In general, values of these βi should be larger than the largest difference between the consecutive eigen-energies.