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

Commit

Permalink
Merge pull request #8 from QuantumBFS/compatibility
Browse files Browse the repository at this point in the history
fix several bugs
  • Loading branch information
Roger-luo authored Apr 21, 2019
2 parents 64cb6d0 + 1fad981 commit 7f8a919
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,37 @@ end
# basic arithmatics

# neg
Base.:-(reg::ArrayRegOrAdjointArrayReg) = ArrayReg(-state(reg))
Base.:-(reg::ArrayReg) = ArrayReg(-state(reg))

# +, -
for op in [:+, :-]
@eval function Base.$op(lhs::ArrayRegOrAdjointArrayReg{B}, rhs::ArrayRegOrAdjointArrayReg{B}) where B
@eval function Base.$op(lhs::ArrayReg{B}, rhs::ArrayReg{B}) where B
return ArrayReg(($op)(state(lhs), state(rhs)))
end
end

# *, /
for op in [:*, :/]
@eval function Base.$op(lhs::RT, rhs::Number) where {B, RT <: ArrayRegOrAdjointArrayReg{B}}
@eval function Base.$op(lhs::RT, rhs::Number) where {B, RT <: ArrayReg{B}}
ArrayReg{B}($op(state(lhs), rhs))
end

if op == :*
@eval function Base.$op(lhs::Number, rhs::RT) where {B, RT <: ArrayRegOrAdjointArrayReg{B}}
@eval function Base.$op(lhs::Number, rhs::RT) where {B, RT <: ArrayReg{B}}
ArrayReg{B}(($op)(lhs, state(rhs)))
end
end
end

for op in [:(==), :]
@eval function Base.$op(lhs::ArrayRegOrAdjointArrayReg{B}, rhs::ArrayRegOrAdjointArrayReg{B}) where B
($op)(state(lhs), state(rhs))
for AT in [:ArrayReg, :AdjointArrayReg]
@eval function Base.$op(lhs::$AT, rhs::$AT)
($op)(state(lhs), state(rhs))
end
end
end

Base.:*(op::AbstractMatrix, r::ArrayRegOrAdjointArrayReg) = op * state(r)
Base.:*(bra::AdjointArrayReg{1}, ket::ArrayReg{1}) = dot(state(bra), state(ket))
Base.:*(bra::AdjointArrayReg{1}, ket::ArrayReg{1}) = dot(parent(bra).state, state(ket))
Base.:*(bra::AdjointArrayReg{B}, ket::ArrayReg{B}) where B = bra .* ket

# broadcast
Expand Down
3 changes: 3 additions & 0 deletions test/operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ end
reg = rand_state(4)
@test all(state(reg + (-reg)).==0)
@test all(state(reg*2 - reg/0.5) .== 0)

reg = rand_state(3)
@test reg'*reg 1
end

0 comments on commit 7f8a919

Please sign in to comment.