Skip to content

Use dual to take the dual of the domain when building symmetric operators #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 10, 2025
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
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "QuantumOperatorDefinitions"
uuid = "826dd319-6fd5-459a-a990-3a4f214664bf"
authors = ["ITensor developers <[email protected]> and contributors"]
version = "0.1.6"
version = "0.1.7"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand All @@ -10,13 +10,13 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
[weakdeps]
BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
GradedUnitRanges = "e2de450a-8a67-46c7-b59c-01d5a3d041c5"
LabelledNumbers = "f856a3a6-4152-4ec4-b2a7-02c1a55d7993"
ITensorBase = "4795dd04-0d67-49bb-8f44-b89c448a1dc7"
LabelledNumbers = "f856a3a6-4152-4ec4-b2a7-02c1a55d7993"
NamedDimsArrays = "60cbd0c0-df58-4cb7-918c-6f5607b73fde"
SymmetrySectors = "f8a8ad64-adbc-4fce-92f7-ffe2bb36a86e"

[extensions]
QuantumOperatorDefinitionsITensorBaseExt = ["ITensorBase", "NamedDimsArrays"]
QuantumOperatorDefinitionsITensorBaseExt = ["ITensorBase", "GradedUnitRanges", "NamedDimsArrays"]
QuantumOperatorDefinitionsSymmetrySectorsExt = ["BlockArrays", "GradedUnitRanges", "LabelledNumbers", "SymmetrySectors"]

[compat]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module QuantumOperatorDefinitionsITensorBaseExt

using ITensorBase: ITensorBase, ITensor, Index, dag, gettag, prime, settag
using ITensorBase: ITensorBase, ITensor, Index, gettag, prime, settag
using GradedUnitRanges: dual
using NamedDimsArrays: dename
using QuantumOperatorDefinitions:
QuantumOperatorDefinitions,
Expand Down Expand Up @@ -38,7 +39,7 @@
end

function Base.axes(::OpName, domain::Tuple{Vararg{Index}})
return (prime.(domain)..., dag.(domain)...)
return (prime.(domain)..., dual.(domain)...)

Check warning on line 42 in ext/QuantumOperatorDefinitionsITensorBaseExt/QuantumOperatorDefinitionsITensorBaseExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/QuantumOperatorDefinitionsITensorBaseExt/QuantumOperatorDefinitionsITensorBaseExt.jl#L42

Added line #L42 was not covered by tests
end
## function Base.axes(::OpName"SWAP", domain::Tuple{Vararg{Index}})
## return (prime.(reverse(domain))..., dag.(domain)...)
Expand Down
1 change: 0 additions & 1 deletion src/sitetype.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ function Base.AbstractUnitRange(t::SiteType)
end
return Base.OneTo(length(t))
end
# kwargs are passed for fancier constructors, like `ITensors.Index`.
function (rangetype::Type{<:AbstractUnitRange})(t::SiteType)
return rangetype(AbstractUnitRange(t))
end
Expand Down
1 change: 1 addition & 0 deletions test/test_itensorbaseext.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using GradedUnitRanges: GradedUnitRanges
using ITensorBase: ITensor, Index, gettag, hastag, prime, settag
using NamedDimsArrays: dename
using QuantumOperatorDefinitions: OpName, SiteType, StateName, op, site, sites, state
Expand Down
32 changes: 10 additions & 22 deletions test/test_symmetrysectorsext.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using BlockArrays: AbstractBlockArray, blocklengths
using BlockSparseArrays: BlockSparseArray
using GradedUnitRanges: blocklabels
using GradedUnitRanges: blocklabels, dual, isdual
using ITensorBase: ITensor, Index, gettag, prime, settag
using QuantumOperatorDefinitions: OpName, SiteType, StateName, op, state
using SymmetrySectors: SectorProduct, U1, Z
using NamedDimsArrays: dename
using Test: @test, @test_broken, @testset
using Test: @test, @testset

@testset "SymmetrySectorsExt" begin
t = SiteType("S=1/2"; gradings=("Sz",))
Expand Down Expand Up @@ -54,36 +54,27 @@ using Test: @test, @test_broken, @testset
@test blocklabels(r2) == [SectorProduct((; Sz=U1(0))), SectorProduct((; Sz=U1(-1)))]
@test blocklengths(r2) == [1, 1]

# TODO: There is a bug slicing `BitVector` by `GradedOneTo` in Julia 1.11,
# investigate. See: https://github.com/ITensor/GradedUnitRanges.jl/issues/9
t = SiteType("S=1/2"; gradings=("Sz",))
@test state("0", t) == [1, 0] broken = VERSION ≥ v"1.11"
@test state("0", t) == [1, 0]

# Force conversion to `Vector{Float64}` before conversion,
# since there is a bug slicing `BitVector` by `GradedOneTo`.
t = SiteType("S=1/2"; gradings=("Sz",))
a = AbstractArray(2.0 * StateName("0"), t)
@test a == [2, 0]
@test a isa AbstractBlockArray
# TODO: Currently slicing a dense array by graded ranges outputs a `BlockedArray`
# rather than a `BlockSparseArray`.
# See: https://github.com/ITensor/GradedUnitRanges.jl/issues/9
@test_broken a isa BlockSparseArray
@test a isa BlockSparseArray
(r1,) = axes(a)
@test blocklabels(r1) == [SectorProduct((; Sz=U1(0))), SectorProduct((; Sz=U1(1)))]
@test blocklengths(r1) == [1, 1]

t = SiteType("S=1/2"; gradings=("Sz",))
a = op("σ⁺", t)
@test a == [0 2; 0 0]
@test a isa AbstractBlockArray
@test_broken a isa BlockSparseArray
@test a isa BlockSparseArray
(r1, r2) = axes(a)
@test blocklabels(r1) == [SectorProduct((; Sz=U1(0))), SectorProduct((; Sz=U1(1)))]
@test blocklengths(r1) == [1, 1]
# TODO: This is a bug in indexing with GradedUnitRangeDual, fix this.
@test_broken blocklabels(r2) ==
[SectorProduct((; Sz=U1(0))), SectorProduct((; Sz=U1(-1)))]
@test blocklabels(r2) == [SectorProduct((; Sz=U1(0))), SectorProduct((; Sz=U1(-1)))]
@test blocklengths(r2) == [1, 1]
end

Expand All @@ -97,17 +88,14 @@ end

i′ = prime(i)
a = op("σ⁺", i)
# TODO: The indices should be `(i′, dual(i))`.
@test a == ITensor([0 2; 0 0], (i′, i))
@test a == ITensor([0 2; 0 0], (i′, dual(i)))
@test all(isdual.(axes(a)) .== (false, true))
a′ = dename(a)
@test a′ isa AbstractBlockArray
@test_broken a′ isa BlockSparseArray
@test a′ isa BlockSparseArray
# TODO: Test these without denaming `a`.
(r1, r2) = axes(a′)
@test blocklabels(r1) == [SectorProduct((; Sz=U1(0))), SectorProduct((; Sz=U1(1)))]
@test blocklengths(r1) == [1, 1]
# TODO: This is a bug in indexing with GradedUnitRangeDual, fix this.
@test_broken blocklabels(r2) ==
[SectorProduct((; Sz=U1(0))), SectorProduct((; Sz=U1(-1)))]
@test blocklabels(r2) == [SectorProduct((; Sz=U1(0))), SectorProduct((; Sz=U1(-1)))]
@test blocklengths(r2) == [1, 1]
end
Loading