Skip to content

Commit 3141ded

Browse files
authored
Move methods from QuantumInterface to here which access a .data field (#199)
1 parent 25474bf commit 3141ded

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ FastGaussQuadrature = "0.5, 1"
2525
FillArrays = "0.13, 1"
2626
LRUCache = "1"
2727
LinearAlgebra = "1"
28-
QuantumInterface = "0.3.9"
28+
QuantumInterface = "0.4.0"
2929
Random = "1"
3030
RecursiveArrayTools = "3"
3131
SparseArrays = "1"

src/states.jl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ mutable struct Ket{B,T} <: AbstractKet{B,T}
3030
end
3131
end
3232

33-
Base.zero(x::Bra) = Bra(x.basis, zero(x.data))
34-
Base.zero(x::Ket) = Ket(x.basis, zero(x.data))
35-
eltype(::Type{K}) where {K <: Ket{B,V}} where {B,V} = eltype(V)
36-
eltype(::Type{K}) where {K <: Bra{B,V}} where {B,V} = eltype(V)
33+
Base.zero(x::T) where {T<:Union{Bra,Ket}} = T(x.basis, zero(x.data))
34+
Base.eltype(::Type{K}) where {K <: Ket{B,V}} where {B,V} = eltype(V)
35+
Base.eltype(::Type{K}) where {K <: Bra{B,V}} where {B,V} = eltype(V)
36+
Base.size(x::Union{Bra,Ket}) = size(x.data)
37+
@inline Base.axes(x::Union{Bra,Ket}) = axes(x.data)
3738

3839
Bra{B}(b::B, data::T) where {B,T} = Bra{B,T}(b, data)
3940
Ket{B}(b::B, data::T) where {B,T} = Ket{B,T}(b, data)
@@ -51,6 +52,8 @@ Ket{B}(b::B) where B = Ket{B}(ComplexF64, b)
5152
Bra(b::Basis) = Bra(ComplexF64, b)
5253
Ket(b::Basis) = Ket(ComplexF64, b)
5354

55+
Base.copy(a::T) where {T<:Union{Bra,Ket}} = T(a.basis, copy(a.data))
56+
5457
==(x::Ket{B}, y::Ket{B}) where {B} = (samebases(x, y) && x.data==y.data)
5558
==(x::Bra{B}, y::Bra{B}) where {B} = (samebases(x, y) && x.data==y.data)
5659
==(x::Ket, y::Ket) = false
@@ -80,6 +83,9 @@ Base.isapprox(x::Bra, y::Bra; kwargs...) = false
8083
/(a::Ket, b::Number) = Ket(a.basis, a.data ./ b)
8184
/(a::Bra, b::Number) = Bra(a.basis, a.data ./ b)
8285

86+
-(a::T) where {T<:Union{Ket,Bra}} = T(a.basis, -a.data)
87+
norm(x::Union{Ket,Bra}) = norm(x.data)
88+
normalize!(x::Union{Ket,Bra}) = (normalize!(x.data); x)
8389

8490
"""
8591
dagger(x)
@@ -278,4 +284,4 @@ RecursiveArrayTools.recursivecopy!(dest::Ket{B,A},src::Ket{B,A}) where {B,A} = c
278284
RecursiveArrayTools.recursivecopy!(dest::Bra{B,A},src::Bra{B,A}) where {B,A} = copyto!(dest, src)
279285
RecursiveArrayTools.recursivecopy(x::T) where {T<:Union{Ket, Bra}} = copy(x)
280286
RecursiveArrayTools.recursivecopy(x::AbstractArray{T}) where {T<:Union{Ket, Bra}} = copy(x)
281-
RecursiveArrayTools.recursivefill!(x::T, a) where {T<:Union{Ket, Bra}} = fill!(x, a)
287+
RecursiveArrayTools.recursivefill!(x::T, a) where {T<:Union{Ket, Bra}} = fill!(x, a)

0 commit comments

Comments
 (0)