GTorlai / PastaQ.jl

Package for Simulation, Tomography and Analysis of Quantum Computers
Apache License 2.0
142 stars 23 forks source link

VQE with noise #252

Closed StefanoBarison closed 2 years ago

StefanoBarison commented 2 years ago

Good morning,

I would like to use PastaQ to measure expectation values of observables on (noisy) quantum circuit. I copy-pasted the VQE example that can be found in the README.md but when I try to run it I get the error

ERROR: LoadError: MethodError: no method matching inner(::MPO, ::Vector{ITensor}, ::MPS; cutoff=1.0e-8)
Closest candidates are:
  inner(::MPS, ::Vector{ITensor}, ::MPS; kwargs...) at ~/.julia/packages/PastaQ/mzm3A/src/autodiff.jl:20
  inner(::MPS, ::Vector{<:Tuple}, ::MPS; kwargs...) at ~/.julia/packages/PastaQ/mzm3A/src/autodiff.jl:25
  inner(::MPS, ::MPO, ::MPS; kwargs...) at ~/.julia/packages/ITensors/cu9Bo/src/mps/mpo.jl:258
  ...

referred to

function loss(θ⃗)
  circuit = variationalcircuit(N, depth, θ⃗)
  U = buildcircuit(ψ, circuit)
  return inner(H, U, ψ; cutoff = 1e-8)
end

Which is the right way to measure expectation values on PastaQ?

mtfishman commented 2 years ago

Hi @StefanoBarison, thanks for you interest in PastaQ!

The line:

  return inner(H, U, ψ; cutoff = 1e-8)

should be:

  return rayleigh_quotient(H, U, ψ; cutoff = 1e-8)

It looks like there were a few other issues, like not loading Zygote. I've pushed a fix to the README.

That functionality is a bit in flux, it was only recently added so the interface might be changing a bit.

StefanoBarison commented 2 years ago

Thank you @mtfishman for the answer and the fix, now it works.

What if now I would like to measure the same expectation value <ψ| H |ψ> but with a noisemodel?

Thanks again

mtfishman commented 2 years ago

In principle all you should need to do is add a noise term to the buildcircuit call within the loss function, however I'm not sure if that functionality was tested with automatic differentiation (AD). If that functionality wasn't made to work with AD yet, computing the gradient of the loss function will likely throw long error from Zygote, the automatic differentiation library we currently use. Please try that and let us know if it works, if not we can raise that as a separate issue to address.

@GTorlai, did you test the VQE functionality with noisy circuits?

StefanoBarison commented 2 years ago

I get stuck before

ERROR: LoadError: MethodError: no method matching inner(::MPO, ::MPO, ::MPO)
Closest candidates are:
  inner(::MPO, ::MPS, ::MPO, ::MPS) at ~/.julia/packages/ITensors/cu9Bo/src/mps/mpo.jl:315
  inner(::MPST, ::MPST; kwargs...) where MPST<:ITensors.AbstractMPS at ~/.julia/packages/ITensors/cu9Bo/src/mps/abstractmps.jl:1054
  inner(::MPS, ::MPO, ::MPS; kwargs...) at ~/.julia/packages/ITensors/cu9Bo/src/mps/mpo.jl:258
  ...

So I guess simply adding the noise term is not enough in this case. In general given a noisy circuit I would try to obtain the gradient using the parameter-shift rule as on real hardware, but backpropagation will surely be faster for simulations.

mtfishman commented 2 years ago

Thanks for checking, it looks like the cost function will need to be changed for that case as well.

We would definitely want this functionality to work. If you have time and want to look into it, feel free and we can try to help. I'm not sure how much time I would have in the next few weeks to look into it.

mtfishman commented 2 years ago

Related to #240.

GTorlai commented 2 years ago

Yes, a new Zygote rule need to be added, since the previous one is specific to a pure state evolution. I'm working on #240 and though it is not exactly the same thing, I should in principle be able to add this feature as well.

GTorlai commented 2 years ago

Hi @StefanoBarison, a small clarification: do you simply need to compute expectation values of observables on the output of a noisy circuit, or do you also need to optimize the circuit according to those averages?

StefanoBarison commented 2 years ago

@GTorlai the aim is to make a VQE with noisy circuit. If I am able to measure expectation values I will use the parameter-shift rule to compute gradients and then optimise, so the backpropagation is not necessary, but is certainly useful to save simulation time.

Thanks for the help.

GTorlai commented 2 years ago

@StefanoBarison we have found a strategy to implement this efficiently (with AD), and moved it up in the priority list. Should be implemented shortly.

StefanoBarison commented 2 years ago

@GTorlai this is great news! I was trying myself to implement a noisy version of VQE by combining PastaQ with Yao but I can't say it's efficient. Moreover, the gradient is obtained by parameter-shift rule and not with AD.

Thanks again