Skip to content

Commit

Permalink
add variational controled rotation gates
Browse files Browse the repository at this point in the history
  • Loading branch information
guochu committed Mar 31, 2020
1 parent 9cfa7c4 commit 75fb2f5
Show file tree
Hide file tree
Showing 12 changed files with 743 additions and 40 deletions.
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

50 changes: 30 additions & 20 deletions src/diff/differentiation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,36 @@ set_parameters_impl!(s::RzGate{<:Variable}, coeff::AbstractVector{<:Number}, sta
collect_variables_impl!(a::Vector, b::RzGate{<:Variable}) = push!(a, value(b.parameter))


# differentiate(s::CRxGate{<:Variable}) = begin
# m = permute(reshape(kron(Rx(value(s.parameter+0.5*pi)), DOWN), 2,2,2,2), s.perm)
# return TwoBodyGate(key(s), m)
# end
# reset_parameter(s::CRxGate, v::Number) = CRxGate(key(s), Variable(v))
# nparameters(s::CRxGate{<:Variable}) = 1

# differentiate(s::CRyGate{<:Variable}) = begin
# m = permute(reshape(kron(Ry(value(s.parameter+0.5*pi)), DOWN), 2,2,2,2), s.perm)
# return TwoBodyGate(key(s), m)
# end
# reset_parameter(s::CRyGate, v::Number) = CRyGate(key(s), Variable(v))
# nparameters(s::CRyGate{<:Variable}) = 1

# differentiate(s::CRzGate{<:Variable}) = begin
# m = permute(reshape(kron(Rz(value(s.parameter+0.5*pi)), DOWN), 2,2,2,2), s.perm)
# return TwoBodyGate(key(s), m)
# end
# reset_parameter(s::CRzGate, v::Number) = CRzGate(key(s), Variable(v))
# nparameters(s::CRzGate{<:Variable}) = 1
# controled rotation gates
differentiate(s::CRxGate{<:Variable}) = begin
m = permute(reshape(_row_kron(DOWN, Rx(value(s.parameter+0.5*pi))), 2,2,2,2), s.perm)
return TwoBodyGate(key(s), m)
end
set_parameters_impl!(s::CRxGate{<:Variable}, coeff::AbstractVector{<:Number}, start_pos::Int=1) = set_parameters_impl!(
s.parameter, coeff, start_pos)
collect_variables_impl!(a::Vector, b::CRxGate{<:Variable}) = push!(a, value(b.parameter))
nparameters(s::CRxGate{<:Variable}) = 1

differentiate(s::CRyGate{<:Variable}) = begin
m = permute(reshape(_row_kron(DOWN, Ry(value(s.parameter+0.5*pi))), 2,2,2,2), s.perm)
return TwoBodyGate(key(s), m)
end
set_parameters_impl!(s::CRyGate{<:Variable}, coeff::AbstractVector{<:Number}, start_pos::Int=1) = set_parameters_impl!(
s.parameter, coeff, start_pos)
collect_variables_impl!(a::Vector, b::CRyGate{<:Variable}) = push!(a, value(b.parameter))
nparameters(s::CRyGate{<:Variable}) = 1

differentiate(s::CRzGate{<:Variable}) = begin
m = permute(reshape(_row_kron(DOWN, Rz(value(s.parameter+0.5*pi))), 2,2,2,2), s.perm)
return TwoBodyGate(key(s), m)
end
set_parameters_impl!(s::CRzGate{<:Variable}, coeff::AbstractVector{<:Number}, start_pos::Int=1) = set_parameters_impl!(
s.parameter, coeff, start_pos)
collect_variables_impl!(a::Vector, b::CRzGate{<:Variable}) = push!(a, value(b.parameter))
nparameters(s::CRzGate{<:Variable}) = 1




nparameters(s::AbstractCircuit) = isempty(s) ? 0 : sum([nparameters(gate) for gate in s])

Expand Down
3 changes: 0 additions & 3 deletions test/algs.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
push!(LOAD_PATH, "../src")


using VQC: qstate, QCircuit, measure!, measure
using VQC: add!, H, CONTROL, extend!, QFT, apply!, qvalues

Expand Down
3 changes: 0 additions & 3 deletions test/chaingrad.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
push!(LOAD_PATH, "../src")


using VQC: ctrlham, qrandn, qstate, check_gradient, RxGate, expham, QCircuit, Variable, Chain

using LinearAlgebra: dot
Expand Down
3 changes: 0 additions & 3 deletions test/check_measure.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
push!(LOAD_PATH, "../src")


using VQC: qrandn, qstate, measure, measure!, post_select, post_select!, renormalize!, ZERO, ONE
using VQC: distance, check_gradient

Expand Down
3 changes: 0 additions & 3 deletions test/circuit2dgrad.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
push!(LOAD_PATH, "../src")


using VQC: qstate, qrandn, simple_gradient, distance, check_gradient
using VQC: variational_circuit_2d
using LinearAlgebra: dot
Expand Down
2 changes: 0 additions & 2 deletions test/circuitgrad.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
push!(LOAD_PATH, "../src")

using VQC: qstate, qrandn, simple_gradient, distance, check_gradient
using VQC: variational_circuit_1d
using LinearAlgebra: dot
Expand Down
37 changes: 37 additions & 0 deletions test/crxgategrad.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using VQC: CRxGate, CNOTGate, Variable, nparameters


function crx_circuit(L::Int, depth::Int)
n = 0
circuit = QCircuit()
for d in 1:depth
for i in 1:L-1
push!(circuit, CNOTGate((i, i+1)))
push!(circuit, CRxGate((i, i+1), Variable(randn())))
push!(circuit, CNOTGate((i, i+1)))
n += 1
end
end
return circuit, n
end

"""
circuit gradient with dot loss function
"""
function crx_circuit_grad_dot_real(L::Int, depth::Int)
target_state = qrandn(Complex{Float64}, L)
initial_state = qstate(Complex{Float64}, L)
circuit, n = crx_circuit(L, depth)

loss(x) = real(dot(target_state, x * initial_state))

return nparameters(circuit)==n && check_gradient(loss, circuit)
end

@testset "gradient of variable quantum circuit with parameterized controled rotational gate" begin
for L in 2:5
for depth in 1:5
@test crx_circuit_grad_dot_real(L, depth)
end
end
end
2 changes: 0 additions & 2 deletions test/ctrlhamgrad.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
push!(LOAD_PATH, "../src")

using VQC: QCircuit, ctrlham, qrandn, qstate, check_gradient, RxGate, expham, get_times, Variable

using LinearAlgebra: dot
Expand Down
2 changes: 0 additions & 2 deletions test/parameter.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
push!(LOAD_PATH, "../src")

using VQC: nparameters, parameters, set_parameters!, QCircuit, Variable
using VQC: RxGate, HGate, CNOTGate, add!

Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
push!(LOAD_PATH, "../src")
using Test


Expand All @@ -15,6 +16,7 @@ end
@testset "test quantum circuit gradient" begin
include("circuitgrad.jl")
include("circuit2dgrad.jl")
include("crxgategrad.jl")
end

@testset "test quantum state gradient (may not variable via a quantum computer)" begin
Expand Down
2 changes: 0 additions & 2 deletions test/stategrad.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
push!(LOAD_PATH, "../src")

using VQC: qstate, qrandn, simple_gradient, distance, check_gradient, probabilities
using VQC: variational_circuit_1d
using LinearAlgebra: dot
Expand Down

0 comments on commit 75fb2f5

Please sign in to comment.