-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwisdom.jl
48 lines (25 loc) · 806 Bytes
/
wisdom.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
push!(LOAD_PATH, "src")
using Zygote
using Zygote: @adjoint
# import VQC: collect_variables_impl!, set_parameters_impl!
using Util: check_gradient
using LinearAlgebra: norm, I
isunitary(x::AbstractMatrix) = isapprox(x*x', I)
random_hermitian(L::Int) = begin
m = randn(Complex{Float64}, L, L)
return m + m'
end
random_unitary(L::Int) = exp(im * random_hermitian(L))
λ = 0.01
L = 4
W = random_unitary(L)
m1 = randn(Complex{Float64}, L, L)
m2 = randn(Complex{Float64}, L, L)
loss(x::AbstractMatrix) = norm((x + m1) * m2)
println("gradient is corrent? $(check_gradient(loss, W, verbose=1)).")
println("W is unitary? $(isunitary(W)).")
grad = gradient(loss, W)[1]
println(isunitary(grad))
A = W * grad' - W' * grad
W1 = (I + (λ/2) * A) \ (I - (λ/2) * A) * W
println(W1' * W1)