# Variational Quantum Eigen Solver

using Yao, Yao.AD, Yao.EasyBuild

number of qubits

n = 4
4

depth

d = 5
circuit = dispatch!(variational_circuit(n, d),:random)

gatecount(circuit)

nparameters(circuit)

h = heisenberg(n)
nqubits: 4
+
├─ +
│  ├─ repeat on (1, 2)
│  │  └─ X
│  ├─ repeat on (1, 2)
│  │  └─ Y
│  └─ repeat on (1, 2)
│     └─ Z
├─ +
│  ├─ repeat on (2, 3)
│  │  └─ X
│  ├─ repeat on (2, 3)
│  │  └─ Y
│  └─ repeat on (2, 3)
│     └─ Z
├─ +
│  ├─ repeat on (3, 4)
│  │  └─ X
│  ├─ repeat on (3, 4)
│  │  └─ Y
│  └─ repeat on (3, 4)
│     └─ Z
└─ +
├─ repeat on (1, 4)
│  └─ X
├─ repeat on (1, 4)
│  └─ Y
└─ repeat on (1, 4)
└─ Z


pick the one you like either reverse-mode or forward mode grad = faithfulgrad(h, zerostate(n) => circuit; nshots=100)

for i in 1:1000
_, grad = expect'(h, zero_state(n) => circuit)
println("Step $i, energy =$(real.(expect(h, zero_state(n)=>circuit)))")
end

using LinearAlgebra
w, _ = eigen(Matrix(mat(h)))
LinearAlgebra.Eigen{ComplexF64, Float64, Matrix{ComplexF64}, Vector{Float64}}
values:
16-element Vector{Float64}:
-7.9999999999999964
-4.0
-3.9999999999999956
-3.999999999999994
-7.437659514860159e-32
-4.426399962566284e-32
0.0
2.7369110631344083e-48
2.003298162163793e-32
3.552713678800501e-15
4.440892098500626e-15
3.9999999999999987
3.9999999999999996
4.0
4.0
4.0
vectors:
16×16 Matrix{ComplexF64}:
0.0+0.0im          0.0+0.0im           0.0+0.0im   0.0+0.0im           0.0+0.0im          0.0+0.0im        0.0+0.0im           0.0+0.0im           0.0+0.0im           0.0+0.0im           0.0+0.0im           0.0+0.0im        0.0+0.0im  1.0+0.0im   0.0+0.0im  0.0+0.0im
0.0+0.0im          0.0+0.0im          -0.5+0.0im   0.0+0.0im      0.559869+0.0im      0.43191+0.0im        0.0+0.0im           0.0+0.0im           0.0+0.0im           0.0+0.0im           0.0+0.0im          -0.5+0.0im        0.0+0.0im  0.0+0.0im   0.0+0.0im  0.0+0.0im
0.0+0.0im          0.0+0.0im           0.5+0.0im   0.0+0.0im   2.22045e-16+0.0im          0.0+0.0im        0.0+0.0im           0.0+0.0im      0.707107+0.0im           0.0+0.0im           0.0+0.0im          -0.5+0.0im        0.0+0.0im  0.0+0.0im   0.0+0.0im  0.0+0.0im
0.288675+0.0im  9.56933e-17+0.0im   6.41024e-18+0.0im   0.0+0.0im      0.481733+0.0im    -0.624453+0.0im        0.0+0.0im     -0.211325+0.0im   1.34897e-17+0.0im           0.0+0.0im     -0.288675+0.0im  -6.41024e-18+0.0im  -0.408248+0.0im  0.0+0.0im   0.0+0.0im  0.0+0.0im
0.0+0.0im          0.0+0.0im          -0.5+0.0im   0.0+0.0im     -0.559869+0.0im     -0.43191+0.0im        0.0+0.0im           0.0+0.0im   2.22045e-16+0.0im           0.0+0.0im           0.0+0.0im          -0.5+0.0im        0.0+0.0im  0.0+0.0im   0.0+0.0im  0.0+0.0im
-0.57735+0.0im     0.707107+0.0im  -1.30125e-33+0.0im   0.0+0.0im  -1.30736e-32+0.0im          0.0+0.0im        0.0+0.0im  -8.71576e-33+0.0im   3.06708e-33+0.0im           0.0+0.0im  -1.97795e-16+0.0im   1.30125e-33+0.0im  -0.408248+0.0im  0.0+0.0im   0.0+0.0im  0.0+0.0im
0.288675+0.0im  9.56933e-17+0.0im  -2.39233e-17+0.0im   0.0+0.0im      -0.12908+0.0im     0.167322+0.0im        0.0+0.0im      0.788675+0.0im   2.77891e-17+0.0im           0.0+0.0im     -0.288675+0.0im   2.39233e-17+0.0im  -0.408248+0.0im  0.0+0.0im   0.0+0.0im  0.0+0.0im
0.0+0.0im          0.0+0.0im           0.0+0.0im  -0.5+0.0im           0.0+0.0im          0.0+0.0im   0.707107+0.0im           0.0+0.0im           0.0+0.0im  -4.71028e-16+0.0im           0.0+0.0im           0.0+0.0im        0.0+0.0im  0.0+0.0im  -0.5+0.0im  0.0+0.0im
0.0+0.0im          0.0+0.0im           0.5+0.0im   0.0+0.0im  -1.29477e-16+0.0im  6.29876e-17+0.0im        0.0+0.0im   6.06671e-17+0.0im     -0.707107+0.0im           0.0+0.0im           0.0+0.0im          -0.5+0.0im        0.0+0.0im  0.0+0.0im   0.0+0.0im  0.0+0.0im
0.288675+0.0im  9.56933e-17+0.0im   1.75131e-17+0.0im   0.0+0.0im     -0.352653+0.0im     0.457131+0.0im        0.0+0.0im      -0.57735+0.0im  -4.12788e-17+0.0im           0.0+0.0im     -0.288675+0.0im  -1.75131e-17+0.0im  -0.408248+0.0im  0.0+0.0im   0.0+0.0im  0.0+0.0im
-0.57735+0.0im    -0.707107+0.0im   1.30125e-33+0.0im   0.0+0.0im   1.30736e-32+0.0im          0.0+0.0im        0.0+0.0im   8.71576e-33+0.0im  -3.06708e-33+0.0im           0.0+0.0im  -3.14995e-16+0.0im  -1.30125e-33+0.0im  -0.408248+0.0im  0.0+0.0im   0.0+0.0im  0.0+0.0im
0.0+0.0im          0.0+0.0im           0.0+0.0im   0.5+0.0im           0.0+0.0im          0.0+0.0im        0.0+0.0im           0.0+0.0im           0.0+0.0im     -0.707107+0.0im           0.0+0.0im           0.0+0.0im        0.0+0.0im  0.0+0.0im  -0.5+0.0im  0.0+0.0im
0.288675+0.0im          0.0+0.0im           0.0+0.0im   0.0+0.0im           0.0+0.0im          0.0+0.0im        0.0+0.0im           0.0+0.0im           0.0+0.0im           0.0+0.0im      0.866025+0.0im           0.0+0.0im  -0.408248+0.0im  0.0+0.0im   0.0+0.0im  0.0+0.0im
0.0+0.0im          0.0+0.0im           0.0+0.0im  -0.5+0.0im           0.0+0.0im          0.0+0.0im  -0.707107+0.0im           0.0+0.0im           0.0+0.0im  -4.71028e-16+0.0im           0.0+0.0im           0.0+0.0im        0.0+0.0im  0.0+0.0im  -0.5+0.0im  0.0+0.0im
0.0+0.0im          0.0+0.0im           0.0+0.0im   0.5+0.0im           0.0+0.0im          0.0+0.0im        0.0+0.0im           0.0+0.0im           0.0+0.0im      0.707107+0.0im           0.0+0.0im           0.0+0.0im        0.0+0.0im  0.0+0.0im  -0.5-0.0im  0.0+0.0im
0.0+0.0im          0.0+0.0im           0.0+0.0im   0.0+0.0im           0.0+0.0im          0.0+0.0im        0.0+0.0im           0.0+0.0im           0.0+0.0im           0.0+0.0im           0.0+0.0im           0.0+0.0im        0.0+0.0im  0.0+0.0im   0.0+0.0im  1.0+0.0im