Skip to content

Commit 6ff72ef

Browse files
author
Rogerluo
authored
Merge pull request #3 from dgan181/HHL_linear_diff
WIP : Forward Euler implementation using HHL
2 parents 071b02a + d2d2cc5 commit 6ff72ef

File tree

5 files changed

+131
-28
lines changed

5 files changed

+131
-28
lines changed

.travis.yml

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,18 @@
1-
## Documentation: http://docs.travis-ci.com/user/languages/julia/
1+
# Documentation: http://docs.travis-ci.com/user/languages/julia/
22
language: julia
33
os:
44
- linux
55
- osx
66
julia:
7-
- 0.7
87
- 1.0
8+
- 1.1
99
- nightly
10+
matrix:
11+
allow_failures:
12+
- julia: nightly
13+
fast_finish: true
1014
notifications:
1115
email: false
12-
git:
13-
depth: 99999999
14-
15-
## uncomment the following lines to allow failures on nightly julia
16-
## (tests will run but not make your overall status red)
17-
#matrix:
18-
# allow_failures:
19-
# - julia: nightly
20-
21-
## uncomment and modify the following lines to manually install system packages
22-
#addons:
23-
# apt: # apt-get for linux
24-
# packages:
25-
# - gfortran
26-
#before_script: # homebrew for mac
27-
# - if [ $TRAVIS_OS_NAME = osx ]; then brew install gcc; fi
28-
29-
## uncomment the following lines to override the default test script
30-
#script:
31-
# - julia -e 'Pkg.clone(pwd()); Pkg.build("QuAlgorithmZoo"); Pkg.test("QuAlgorithmZoo"; coverage=true)'
3216
after_success:
33-
# push coverage results to Coveralls
34-
- julia -e 'using Pkg; cd(Pkg.dir("QuAlgorithmZoo")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
35-
# push coverage results to Codecov
36-
- julia -e 'using Pkg; cd(Pkg.dir("QuAlgorithmZoo")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'
37-
# add Documenter
38-
- julia -e 'using Pkg; cd(Pkg.dir("QuAlgorithmZoo")); include(joinpath("docs", "make.jl"))'
17+
- julia -e 'using Pkg; Pkg.add("Coverage"); using Coverage; Codecov.submit(process_folder())'
18+
- julia -e 'using Pkg; Pkg.add("Coverage"); using Coverage; Coveralls.submit(process_folder())'

Project.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ Yao = "5872b779-8223-5990-8dd0-5abbb0748c8c"
1313

1414
[extras]
1515
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
16+
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
17+
18+
[targets]
19+
test = ["Test", "OrdinaryDiffEq"]

src/QuAlgorithmZoo.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ include("PhaseEstimation.jl")
2929
include("HHL.jl")
3030
include("hamiltonian_solvers.jl")
3131
include("HadamardTest.jl")
32+
include("lin_diffEq_HHL.jl")
3233

3334

3435
end # module

src/lin_diffEq_HHL.jl

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
export Array_QuEuler, prepare_init_state, solve_QuEuler
2+
3+
"""
4+
Based on : arxiv.org/abs/1010.2745v2
5+
6+
QuArray_Euler(N_t,N,h,A)
7+
prepare_init_state(b,x,h,N_t)
8+
9+
x' = Ax + b
10+
11+
* A - input matrix.
12+
* b - input vector.
13+
* x - inital vector
14+
* N - dimension of b (as a power of 2).
15+
* h - step size.
16+
* tspan - time span.
17+
"""
18+
function prepare_init_state(tspan::Tuple,x::Vector,h::Float64,g::Function)
19+
init_state = x;
20+
N_t = round(2*(tspan[2] - tspan[1])/h + 3)
21+
b = similar(g(1))
22+
for i = 1:N_t
23+
if i < (N_t+1)/2
24+
b = g(h*i + tspan[1])
25+
init_state = [init_state;h*b]
26+
else
27+
init_state = [init_state;zero(b)]
28+
end
29+
30+
end
31+
init_state = [init_state;zero(init_state)]
32+
init_state
33+
end
34+
35+
function Array_QuEuler(tspan::Tuple,h::Float64,g::Function)
36+
N_t = round(2*(tspan[2] - tspan[1])/h + 3)
37+
I_mat = Matrix{Float64}(I, size(g(1)));
38+
A_ = I_mat;
39+
zero_mat = zero(I_mat);
40+
tmp_A = Array{Float64,2}(undef,size(g(1)))
41+
42+
for j = 2: N_t +1
43+
A_ = [A_ zero_mat]
44+
end
45+
46+
for i = 2 : N_t+1
47+
tmp_A = g(i*h + tspan[1])
48+
tmp_A = -1*(I_mat + h*tmp_A)
49+
tA_ = copy(zero_mat)
50+
if i<3
51+
tA_ = [tmp_A I_mat]
52+
else
53+
for j = 1:i-3
54+
tA_ = [tA_ zero_mat]
55+
end
56+
if i < (N_t + 1)/2 + 1
57+
tA_ = [tA_ tmp_A I_mat]
58+
else
59+
tA_ = [tA_ -1*I_mat I_mat]
60+
end
61+
end
62+
if i< N_t+1
63+
for j = i+1: N_t+1
64+
tA_ = [tA_ zero_mat]
65+
end
66+
end
67+
A_ = [A_;tA_]
68+
69+
end
70+
A_ = [zero(A_) A_;A_' zero(A_)]
71+
A_
72+
end
73+
74+
function solve_QuEuler(A::Function, b::Function, x::Vector,tspan::Tuple, h::Float64)
75+
76+
mat = Array_QuEuler(tspan,h,A)
77+
state = prepare_init_state(tspan,x,h,b)
78+
λ = maximum(eigvals(mat))
79+
C_value = minimum(eigvals(mat) .|> abs)*0.1;
80+
n_reg = 12;
81+
mat = 1/*2)*mat
82+
state = state*1/(2*λ) |> normalize!
83+
res = hhlsolve(mat,state, n_reg, C_value)
84+
res = res/λ
85+
res
86+
end

test/lin_diffEq_test.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Yao
2+
using Yao.Intrinsics
3+
using QuAlgorithmZoo
4+
using Test, LinearAlgebra
5+
using OrdinaryDiffEq
6+
7+
function diffEq_problem(nbit::Int)
8+
siz = 1<<nbit
9+
A = (rand(ComplexF64, siz,siz))
10+
A = (A+A')/2
11+
b = normalize!(rand(ComplexF64, siz))
12+
x = normalize!(rand(ComplexF64, siz))
13+
A, b, x
14+
end
15+
16+
@testset "Linear_differential_equation_Euler_HHL" begin
17+
N = 1
18+
h = 0.02
19+
tspan = (0.0,0.6)
20+
M, v, x = diffEq_problem(N)
21+
A(t) = M
22+
b(t) = v
23+
res = solve_QuEuler(A, b, x, tspan, h)
24+
25+
f(u,p,t) = M*u + v;
26+
prob = ODEProblem(f,x,tspan)
27+
sol = solve(prob,Euler(),dt = 0.02,adaptive = :false)
28+
s = vcat(sol.u...)
29+
N_t = round(2*(tspan[2] - tspan[1])/h + 3);
30+
r = res[Int((N_t+1)*2+2^N+1): Int((N_t+1)*2+2^N + N_t - 1)] # range of relevant values in the obtained state.
31+
@test isapprox.(s,r,atol = 0.05) |> all
32+
end

0 commit comments

Comments
 (0)