Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/QuantumBFS/tutorials
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger-luo committed Dec 23, 2019
2 parents c8cb5da + 8df3746 commit 4451252
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 23 deletions.
1 change: 0 additions & 1 deletion developer-guide/2.cuda-acceleration/main.jl
Expand Up @@ -34,4 +34,3 @@
# Only if two qubits at `locs` are $0$ and $1$ respectively, they are exchanged, otherwise do nothing.
# Although $3/4$ of threads are idle and plenty room for optimization, from this example, we see how easy CUDA programming is with
# [CUDAnative](https://github.com/JuliaGPU/CUDAnative.jl).
# In appendix \App{app-reg}, we show another example of creating a register that echos received instructions.
2 changes: 1 addition & 1 deletion quick-start/5.shor-9-code/main.jl
Expand Up @@ -21,7 +21,7 @@ shor(E) = chain(9,
)

# Now we can check whether it can correct a given error by doing symbolic computation
# on a 1-qubit quantum state ``α|0⟩ + β|1⟩`` and an arbitrary weight-9 error.
# on an arbitrary 1-qubit pure quantum state ``α|0⟩ + β|1⟩`` and a specific weight-9 error.

@vars α β
s = α * ket"0" + β * ket"1" |> addbits!(8)
Expand Down
32 changes: 11 additions & 21 deletions quick-start/7.variation-quantum-eigen-solver/main.jl
Expand Up @@ -2,36 +2,26 @@

using Yao, Yao.AD, YaoExtensions


using YaoExtensions: get_diffblocks, _perturb
using StatsBase

function _expect(op::AbstractBlock, reg::ArrayReg; nshots=nothing)
if nshots === nothing
expect(op, reg)
else
mean(measure(op, copy(reg); nshots=nshots))
end
end

@inline function my_faithful_grad(op::AbstractBlock, pair::Pair{<:ArrayReg, <:AbstractBlock}; nshots=nothing)
map(get_diffblocks(pair.second)) do diffblock
r1, r2 = _perturb(()->_expect(op, copy(pair.first) |> pair.second; nshots=nshots) |> real, diffblock, π/2)
(r2 - r1)/2
end
end

# number of qubits
n = 4

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

gatecount(circuit)

nparameters(circuit)

h = heisenberg(n)

for i in 1:1000
grad = my_faithful_grad(h, zero_state(n) => circuit; nshots=100)
# pick the one you like
# either reverse-mode
# or forward mode
# grad = faithful_grad(h, zero_state(n) => circuit; nshots=100)

for i in 1:1000
_, grad = expect'(h, zero_state(n) => circuit)
dispatch!(-, circuit, 1e-2 * grad)
println("Step $i, energy = $(real.(expect(h, zero_state(n)=>circuit)))")
end
Expand Down

0 comments on commit 4451252

Please sign in to comment.