QuantumBFS / Yao.jl

Extensible, Efficient Quantum Algorithm Design for Humans.

Home Page:https://yaoquantum.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

evaluate expectation value of general hermitian operator in Yao

hongyehu opened this issue · comments

When using Yao for variational quantum algorithm, is there a way to set the loss as the expectation value of some general hermitian operator? For example, in variational state preparation, we have the target state |psi>, and the loss would be the expectation value on hermitian operator -|psi><psi|.
Thanks!

Please try the following code

julia> using Yao

julia> reg = rand_state(6)
ArrayReg{2, ComplexF64, Array...}
    active qubits: 6/6
    nlevel: 2

julia> op = matblock(YaoBlocks.AD.OuterProduct(conj(reg.state), reg.state))
matblock(...)

julia> expect'(-op, zero_state(6) => EasyBuild.variational_circuit(6))
ArrayReg{2, ComplexF64, Array...}
    active qubits: 6/6
    nlevel: 2 => [-0.020295020766777812, -0.0, 0.023824991309762213, -0.0, 0.042856417765074165, -0.0, 0.027912583015592053, -0.0, 0.02097702478725904, -0.0    -0.0, -0.039694288468499636, -0.0, 0.025153466384883374, -0.0, -0.005618395607029307, -0.0, 0.0018088008801726279, -0.0, 0.014730977164301431]