Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ITensors"
uuid = "9136182c-28ba-11e9-034c-db9fb085ebd5"
authors = ["Matthew Fishman <[email protected]>", "Miles Stoudenmire <[email protected]>"]
version = "0.9.6"
version = "0.9.7"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
45 changes: 25 additions & 20 deletions src/lib/QuantumNumbers/src/qn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,29 +176,34 @@ function Base.:(-)(qn::QN)
end

function Base.:(+)(a::QN, b::QN)
!isactive(b[1]) && return a
ma = MQNStorage(data(a))
@inbounds for nb in 1:maxQNs
!isactive(b[nb]) && break
bname = name(b[nb])
for na in 1:maxQNs
aname = name(a[na])
if !isactive(ma[na])
ma[na] = b[nb]
break
elseif name(ma[na]) == bname
ma[na] = ma[na] + b[nb]
break
elseif (bname < aname) && (na == 1 || bname > name(ma[na - 1]))
for j in maxQNs:-1:(na + 1)
ma[j] = ma[j - 1]
end
ma[na] = b[nb]
break
na = nactive(a)
iszero(na) && return b
nb = nactive(b)
iszero(nb) && return a
sectors_a = data(a)
sectors_b = data(b)
msectors_c = MQNStorage(sectors_a)
nc = na
@inbounds for ib in 1:nb
sector_b = sectors_b[ib]
found = false
for ia in 1:na
sector_a = sectors_a[ia]
if name(sector_b) == name(sector_a)
msectors_c[ia] = sector_a + sector_b
found = true
continue
end
end
if !found
if nc >= length(msectors_c)
error("Cannot add QN, maximum number of QNVals reached")
end
msectors_c[nc += 1] = sector_b
end
end
return QN(QNStorage(ma))
sort!(view(msectors_c, 1:nc); by=name)
return QN(msectors_c)
end

Base.:(-)(a::QN, b::QN) = (a + (-b))
Expand Down
4 changes: 4 additions & 0 deletions test/base/test_qn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ import ITensors: nactive
@test QN("Sz", 2) - QN() == QN("Sz", 2)
@test QN() - QN(("Sz", 2), ("N", 1)) == QN(("Sz", -2), ("N", -1))
@test QN("N", 1) - QN("Sz", 2) == QN(("N", 1), ("Sz", -2))

# Regression test for https://github.com/ITensor/ITensors.jl/issues/1658
@test QN("S3", 3) + QN(("S1", 1), ("S2", 2)) == QN(("S1", 1), ("S2", 2), ("S3", 3))
@test QN(("S1", 1), ("S2", 2)) + QN("S3", 3) == QN(("S1", 1), ("S2", 2), ("S3", 3))
end

@testset "Ordering" begin
Expand Down
Loading