Skip to content
This repository has been archived by the owner on Dec 18, 2021. It is now read-only.

Commit

Permalink
run formatter (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger-luo authored and GiggleLiu committed Nov 14, 2019
1 parent 280dcc8 commit eeda6eb
Show file tree
Hide file tree
Showing 17 changed files with 904 additions and 530 deletions.
262 changes: 208 additions & 54 deletions benchmark/benchmarks.jl

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions benchmark/runbench.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using PkgBenchmark

current = BenchmarkConfig(id="transpose_storage", juliacmd=`julia -O3`)
baseline = BenchmarkConfig(id="master", juliacmd=`julia -O3`)
current = BenchmarkConfig(id = "transpose_storage", juliacmd = `julia -O3`)
baseline = BenchmarkConfig(id = "master", juliacmd = `julia -O3`)
results = judge("YaoArrayRegister", current, baseline)
export_markdown("report.md", results)
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ makedocs(
doctest = true,
clean = false,
sitename = "YaoArrayRegister.jl",
pages = ["index.md"]
pages = ["index.md"],
)
34 changes: 17 additions & 17 deletions src/density_matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Density Matrix.
- `B`: batch size
- `T`: element type
"""
struct DensityMatrix{B, T, MT<:AbstractArray{T, 3}} <: AbstractRegister{B}
struct DensityMatrix{B,T,MT<:AbstractArray{T,3}} <: AbstractRegister{B}
state::MT
end

Expand All @@ -19,7 +19,7 @@ end
Create a `DensityMatrix` with a state represented by array.
"""
DensityMatrix(state::MT) where {T, MT<:AbstractArray{T, 3}} = DensityMatrix{size(state, 3), T, MT}(state)
DensityMatrix(state::MT) where {T,MT<:AbstractArray{T,3}} = DensityMatrix{size(state, 3),T,MT}(state)
DensityMatrix(state::AbstractMatrix) = DensityMatrix(reshape(state, size(state)..., 1))

"""
Expand All @@ -31,52 +31,52 @@ state(ρ::DensityMatrix) = ρ.state

YaoBase.nqubits::DensityMatrix) = log2dim1(state(ρ))
YaoBase.nactive::DensityMatrix) = nqubits(ρ)
YaoBase.nbatch(dm::DensityMatrix{B}) where B = B
YaoBase.nbatch(dm::DensityMatrix{B}) where {B} = B
YaoBase.density_matrix(reg::ArrayReg{1}) = DensityMatrix(reg.state * reg.state')
function YaoBase.density_matrix(reg::ArrayReg{B}) where B
function YaoBase.density_matrix(reg::ArrayReg{B}) where {B}
M = size(reg.state, 1)
s = reshape(reg |> state, M, :, B)
out = similar(s, M, M, B)
for b in 1:B
@inbounds @views out[:,:,b] = s[:,:,b]*s[:,:,b]'
@inbounds @views out[:, :, b] = s[:, :, b] * s[:, :, b]'
end
return DensityMatrix(out)
end

YaoBase.tracedist(dm1::DensityMatrix{B}, dm2::DensityMatrix{B}) where B =
map(b->trnorm(dm1.state[:,:,b] - dm2.state[:,:,b]), 1:B)
YaoBase.tracedist(dm1::DensityMatrix{B}, dm2::DensityMatrix{B}) where {B} =
map(b -> trnorm(dm1.state[:, :, b] - dm2.state[:, :, b]), 1:B)

# TODO: use batch_broadcast in the future
"""
probs(ρ)
Returns the probability distribution from a density matrix `ρ`.
"""
function YaoBase.probs(m::DensityMatrix{B, T}) where {B, T}
function YaoBase.probs(m::DensityMatrix{B,T}) where {B,T}
res = zeros(T, size(m.state, 1), B)
for i in 1:B
@inbounds res[:,B] = diag(view(m.state, :,:,i))
@inbounds res[:, B] = diag(view(m.state, :, :, i))
end
return res
end

YaoBase.probs(m::DensityMatrix{1})= diag(view(m.state, :,:,1))
YaoBase.probs(m::DensityMatrix{1}) = diag(view(m.state, :, :, 1))

"""
purify(r::DensityMatrix{B}; nbit_env::Int=nactive(r)) -> ArrayReg
Get a purification of target density matrix.
"""
function purify(r::DensityMatrix{B}; nbit_env::Int=nactive(r)) where B
Ne = 1<<nbit_env
Ns = size(r.state,1)
state = similar(r.state,Ns,Ne,B)
function purify(r::DensityMatrix{B}; nbit_env::Int = nactive(r)) where {B}
Ne = 1 << nbit_env
Ns = size(r.state, 1)
state = similar(r.state, Ns, Ne, B)
for ib in 1:B
R, U = eigen!(r.state[:,:,ib])
state[:,:,ib] .= view(U,:,Ns-Ne+1:Ns) .* sqrt.(abs.(view(R,Ns-Ne+1:Ns)'))
R, U = eigen!(r.state[:, :, ib])
state[:, :, ib] .= view(U, :, Ns-Ne+1:Ns) .* sqrt.(abs.(view(R, Ns-Ne+1:Ns)'))
end
return ArrayReg(state)
end

# obtaining matrix from Yao.DensityMatrix{1}, `1` is the batch size.
LinearAlgebra.Matrix(d::DensityMatrix{1}) = dropdims(d.state, dims=3)
LinearAlgebra.Matrix(d::DensityMatrix{1}) = dropdims(d.state, dims = 3)
21 changes: 9 additions & 12 deletions src/focus.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using YaoBase, TupleTools

export focus!,
relax!,
partial_tr,
exchange_sysenv
export focus!, relax!, partial_tr, exchange_sysenv


"""
Expand Down Expand Up @@ -80,7 +77,7 @@ function group_permutedims(A::AbstractArray, orders)
end

# forward directly if the length is the same with ndims
function group_permutedims(A::AbstractArray{T, N}, orders::NTuple{N, Int}) where {T, N}
function group_permutedims(A::AbstractArray{T,N}, orders::NTuple{N,Int}) where {T,N}
return unsafe_group_permutedims(A, orders)
end

Expand All @@ -99,7 +96,7 @@ is_order_same(locs) = all(a == b for (a, b) in zip(locs, 1:length(locs)))
# NOTE: locations is not the same with orders
# locations: some location of the wire
# orders: includes all the location of the wire in some order
function YaoBase.focus!(r::ArrayReg{B}, locs) where B
function YaoBase.focus!(r::ArrayReg{B}, locs) where {B}
if is_order_same(locs)
arr = r.state
else
Expand All @@ -110,19 +107,19 @@ function YaoBase.focus!(r::ArrayReg{B}, locs) where B
return r
end

function YaoBase.relax!(r::ArrayReg{B}, locs; to_nactive::Int=nqubits(r)) where B
function YaoBase.relax!(r::ArrayReg{B}, locs; to_nactive::Int = nqubits(r)) where {B}
r.state = reshape(state(r), 1 << to_nactive, :)
if !is_order_same(locs)
new_orders = TupleTools.invperm(move_ahead(to_nactive+1, locs))
new_orders = TupleTools.invperm(move_ahead(to_nactive + 1, locs))
r.state = reshape(group_permutedims(hypercubic(r), new_orders), 1 << to_nactive, :)
end
return r
end

function YaoBase.partial_tr(r::ArrayReg{B}, locs) where B
function YaoBase.partial_tr(r::ArrayReg{B}, locs) where {B}
orders = setdiff(1:nqubits(r), locs)
focus!(r, orders)
state = sum(rank3(r); dims=2)
state = sum(rank3(r); dims = 2)
relax!(r, orders)
return normalize!(ArrayReg(state))
end
Expand All @@ -132,6 +129,6 @@ end
Exchange system (focused qubits) and environment (remaining qubits).
"""
function exchange_sysenv(reg::ArrayReg{B}) where B
ArrayReg{B}(reshape(permutedims(rank3(reg), (2,1,3)), :,size(reg.state, 1)*B))
function exchange_sysenv(reg::ArrayReg{B}) where {B}
ArrayReg{B}(reshape(permutedims(rank3(reg), (2, 1, 3)), :, size(reg.state, 1) * B))
end
Loading

0 comments on commit eeda6eb

Please sign in to comment.