Skip to content

Commit

Permalink
fixed subtract bug
Browse files Browse the repository at this point in the history
  • Loading branch information
nmayhall-vt committed Oct 25, 2023
1 parent 68d59b8 commit 211b79d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/type_PauliSum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ end
Subtract two `PauliSum`s.
"""
function Base.:-(ps1::PauliSum{N}, ps2::PauliSum{N}) where {N}
return PauliSum{N}(mergewith(-, ps1.ops, ps2.ops))
# out = PauliSum{N}()
# merge!(out, ps2)
out = deepcopy(ps2)
map!(x->-x, values(out.ops))
mergewith!(+, out.ops, ps1.ops)
return out
end

Base.length(ps::PauliSum) = length(ps.ops)
Expand Down
15 changes: 15 additions & 0 deletions test/tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ using Random
for i in 1:100
sum!(H, random_ScaledPauli(N))
end

test = true
for (op,coeff) in diag(H).ops
test = test && is_diagonal(op)
Expand Down Expand Up @@ -313,4 +314,18 @@ using Random
# @show norm(Matrix(o)*Vector(v) - Vector(o*v))
@test norm(Matrix(o)*Vector(v) - Vector(o*v)) < 1e-13
end


## sum/Subtract
N = 8
H1 = PauliSum(N)
H2 = PauliSum(N)
for i in 1:100
sum!(H1, random_ScaledPauli(N))
sum!(H2, random_ScaledPauli(N))
end
@test norm(Matrix(H1) + Matrix(H2) - Matrix(H1 + H2)) < 1e-8
@test norm(Matrix(H1) - Matrix(H2) - Matrix(H1 - H2)) < 1e-8
end


0 comments on commit 211b79d

Please sign in to comment.