Skip to content

Commit ef1b0f1

Browse files
committed
new gatelearning example
1 parent c8805e0 commit ef1b0f1

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using YaoExtensions, Yao
2+
using Test, Random
3+
using Optim: LBFGS, optimize
4+
using Optim
5+
6+
"""
7+
learn_u4(u::AbstractMatrix; niter=100)
8+
9+
Learn a general U4 gate. The optimizer is LBFGS.
10+
"""
11+
function learn_u4(u::AbstractBlock; niter=100)
12+
ansatz = general_U4()
13+
params = parameters(ansatz)
14+
println("initial loss = $(operator_fidelity(u,ansatz))")
15+
optimize(x->-operator_fidelity(u, dispatch!(ansatz, x)),
16+
(G, x) -> (G .= -operator_fidelity'(u, dispatch!(ansatz, x))[2]),
17+
parameters(ansatz),
18+
LBFGS(),
19+
Optim.Options(iterations=niter))
20+
println("final fidelity = $(operator_fidelity(u,ansatz))")
21+
return ansatz
22+
end
23+
24+
using Random
25+
Random.seed!(2)
26+
u = matblock(rand_unitary(4))
27+
c = learn_u4(u; niter=150)

src/hamiltonian_solvers.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ variational quantum eigensolver, faithful simulation with optimizer Adam(lr=0.01
3939
"""
4040
function vqe_solve!(circuit::AbstractBlock{N}, hamiltonian::AbstractBlock; niter::Int=100) where N
4141
optimizer = Adam(lr=0.01)
42-
dbs = collect_blocks(Diff, circuit)
4342
params = parameters(circuit)
4443
for i = 1:niter
45-
grad = opdiff.(()->zero_state(N) |> circuit, dbs, Ref(hamiltonian))
44+
grad = expect'(hamiltonian, zero_state(N) => circuit).second
4645
dispatch!(circuit, update!(params, grad, optimizer))
4746
println("Step $i, Energy = $(expect(hamiltonian, zero_state(N) |> circuit))")
4847
end

test/HadamardTest.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ single_swap_test(reg::AbstractRegister, ϕ::Real) = hadamard_test(SWAP, reg, ϕ)
2020
@test desired res
2121
desired = tr(Matrix(rho1)*Matrix(rho2)*Matrix(rho3)) |> real
2222
c = swap_test_circuit(2, 3, 0)
23-
res = expect(put(7, 1=>Z), reduce(, [reg3, reg2, reg1, zero_state(1)]) |> c) |> tr |> real
23+
res = expect(put(7, 1=>Z), reduce(join, [reg3, reg2, reg1, zero_state(1)]) |> c) |> tr |> real
2424
@test desired res
2525
desired = tr(Matrix(rho1)*Matrix(rho2)*Matrix(rho3)) |> imag
2626
c = swap_test_circuit(2, 3, -π/2)
27-
res = expect(put(7, 1=>Z), reduce(, [reg3, reg2, reg1, zero_state(1)]) |> c) |> tr |> real
27+
res = expect(put(7, 1=>Z), reduce(join, [reg3, reg2, reg1, zero_state(1)]) |> c) |> tr |> real
2828
@test desired res
2929
end
3030

@@ -36,7 +36,7 @@ end
3636
@test hadamard_test(U, reg, 0.0) real(expect(U, reg))
3737
@test hadamard_test(U, reg, -π/2) imag(expect(U, reg))
3838

39-
reg = zero_state(2) |> singlet_block()
39+
reg = zero_state(2) |> YaoExtensions.singlet_block()
4040
@test single_swap_test(reg, 0) -1
4141

4242
reg = zero_state(2)

test/hamiltonian_solvers.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ using YaoBlocks: ConstGate
2424
N = 4
2525
h = heisenberg(N)
2626
E = eigen(h |> mat |> Matrix).values[1]
27-
c = YaoExtensions.variational_circuit(N, 5, [i=>mod(i,N)+1 for i=1:N], mode=:Merged) |> autodiff(:QC)
27+
c = YaoExtensions.variational_circuit(N, 5)
2828
dispatch!(c, :random)
2929
vqe_solve!(c, h)
3030
E2 = expect(h, zero_state(N) |> c)
31-
@test isapprox(E, E2, atol=1e-1)
31+
@test isapprox(E, E2, rtol=1e-1)
3232
end

test/runtests.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,3 @@ end
1414
@testset "hadamard test" begin
1515
include("HadamardTest.jl")
1616
end
17-
18-
@testset "QSVD" begin
19-
include("QSVD.jl")
20-
end

0 commit comments

Comments
 (0)