Skip to content

Commit

Permalink
Merge branch 'modulize' of github.com:QuantumBFS/Yao.jl into modulize
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger-luo committed Jun 4, 2018
2 parents 16fd841 + 2f43bb0 commit d9d41ea
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Blocks/ConstGateTools2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function define_callables(name)
# define shortcuts
(gate::$(gt_name))(itr) = RangedBlock(gate, itr)
# TODO: use Repeated instead
(gate::$(gt_name))(n::Int, itr) = KronBlock{n}(i=>$name for i in pos)
(gate::$(gt_name))(n::Int, itr) = KronBlock{n}(i=>$name for i in itr)
end
end
end
Expand Down
1 change: 1 addition & 0 deletions src/CacheServers/CacheServers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ get_server(::Type{K}, ::Type{ELT}, params...; kwargs...) where {K, ELT} =
get_server(DefaultServerType, K, ELT, params...; kwargs...)

function get_server(::Type{ST}, ::Type{K}, ::Type{ELT}, params...; kwargs...) where {ST, K, ELT}
global ServerPool
if ST{K, ELT} in keys(ServerPool)
ServerPool[ST{K, ELT}]
else
Expand Down
1 change: 1 addition & 0 deletions src/LuxurySparse/LuxurySparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module LuxurySparse
using Compat
using Compat.LinearAlgebra
using Compat.SparseArrays
using Compat.Random

import Compat: copyto!
import Compat.LinearAlgebra: ishermitian
Expand Down
57 changes: 47 additions & 10 deletions src/LuxurySparse/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ end

# NOTE: making them dry?
# to vector
function (*)(A::PermMatrix{Ta}, X::AbstractVector{Tx}) where {Ta, Tx}
function *(A::PermMatrix{Ta}, X::AbstractVector{Tx}) where {Ta, Tx}
nX = length(X)
nX == size(A, 2) || throw(DimensionMismatch())
v = similar(X, promote_type(Ta, Tx))
Expand All @@ -47,8 +47,7 @@ function (*)(A::PermMatrix{Ta}, X::AbstractVector{Tx}) where {Ta, Tx}
v
end

function (*)(X::RowVector{Tx}, A::PermMatrix{Ta}) where {Tx, Ta}
println("pass")
function *(X::RowVector{Tx}, A::PermMatrix{Ta}) where {Tx, Ta}
nX = length(X)
nX == size(A, 1) || throw(DimensionMismatch())
v = similar(X, promote_type(Tx, Ta))
Expand All @@ -59,36 +58,74 @@ function (*)(X::RowVector{Tx}, A::PermMatrix{Ta}) where {Tx, Ta}
end

# to diagonal
function (*)(D::Diagonal{Td}, A::PermMatrix{Ta}) where {Td, Ta}
function *(D::Diagonal{Td}, A::PermMatrix{Ta}) where {Td, Ta}
T = Base.promote_op(*, Td, Ta)
PermMatrix(A.perm, A.vals .* D.diag)
end

function (*)(A::PermMatrix{Ta}, D::Diagonal{Td}) where {Td, Ta}
function *(A::PermMatrix{Ta}, D::Diagonal{Td}) where {Td, Ta}
T = Base.promote_op(*, Td, Ta)
PermMatrix(A.perm, A.vals .* view(D.diag, A.perm))
end

# to self
function (*)(A::PermMatrix, B::PermMatrix)
function *(A::PermMatrix, B::PermMatrix)
size(A, 1) == size(B, 1) || throw(DimensionMismatch())
PermMatrix(B.perm[A.perm], A.vals.*view(B.vals, A.perm))
end

# to matrix
function (*)(A::PermMatrix, X::AbstractMatrix)
function *(A::PermMatrix, X::AbstractMatrix)
size(X, 1) == size(A, 2) || throw(DimensionMismatch())
return @views A.vals .* X[A.perm, :] # this may be inefficient for sparse CSC matrix.
end

function (*)(X::AbstractMatrix, A::PermMatrix)
function *(X::AbstractMatrix, A::PermMatrix)
mX, nX = size(X)
nX == size(A, 1) || throw(DimensionMismatch())
return @views (A.vals' .* X)[:, invperm(A.perm)]
end

@static if VERSION >= v"0.7-"
# NOTE: this is just a temperory fix for v0.7. We should overload mul! in
# the future (when we start to drop v0.6) to enable buildin lazy evaluation.

for MTYPE in [PermMatrix, IMatrix]

@eval begin

function *(lhs::Adjoint{T, <:AbstractArray{T}}, rhs::$MTYPE) where {T <: Real}
transpose(lhs.parent) * rhs
end

function *(lhs::Adjoint{T, <:AbstractArray{T}}, rhs::$MTYPE) where {T <: Complex}
conj(transpose(lhs.parent)) * rhs
end

function *(lhs::Adjoint{T, <:AbstractVector{T}}, rhs::$MTYPE) where {T <: Complex}
conj(transpose(lhs.parent)) * rhs
end

function *(lhs::Adjoint{T, <:AbstractVector{T}}, rhs::$MTYPE) where {T <: Real}
transpose(lhs.parent) * rhs
end

function *(lhs::Transpose{T, <:AbstractArray{T}}, rhs::$MTYPE) where T
copy(transpose(lhs.parent)) * rhs
end

function *(lhs::Transpose{T, <:AbstractVector{T}}, rhs::$MTYPE) where T
transpose(transpose(rhs) * lhs.parent)
end

end

end # ex

end

# to sparse
function (*)(A::PermMatrix, X::SparseMatrixCSC)
function *(A::PermMatrix, X::SparseMatrixCSC)
nA = size(A, 1)
mX, nX = size(X)
mX == nA || throw(DimensionMismatch())
Expand All @@ -105,7 +142,7 @@ function (*)(A::PermMatrix, X::SparseMatrixCSC)
SparseMatrixCSC(mX, nX, X.colptr, rowval, nzval)
end

function (*)(X::SparseMatrixCSC, A::PermMatrix)
function *(X::SparseMatrixCSC, A::PermMatrix)
nA = size(A, 1)
mX, nX = size(X)
nX == nA || throw(DimensionMismatch())
Expand Down

0 comments on commit d9d41ea

Please sign in to comment.