Skip to content

Commit

Permalink
Continue working towards qojulia#40
Browse files Browse the repository at this point in the history
  • Loading branch information
akirakyle committed Dec 8, 2024
1 parent e0261e0 commit b59db60
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 22 deletions.
6 changes: 4 additions & 2 deletions src/abstract_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
Abstract type for all specialized bases of a Hilbert space.
The `Basis` type specifies an orthonormal basis for the Hilbert
space of the studied system. All subtypes must implement `Base.:(==)` along with
`Base.length`, which should return the total dimension of the Hilbert space.
space of the studied system. All subtypes must implement `Base.:(==)`,
and `Base.size`. `size` should return a tuple representing the total dimension
of the Hilbert space with any tensor product structure the basis has such that
`length(b::Basis) = prod(size(b))` gives the total Hilbert dimension
Composite systems can be defined with help of [`CompositeBasis`](@ref).
Expand Down
20 changes: 20 additions & 0 deletions src/bases.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Total dimension of the Hilbert space.
"""
Base.length(b::Basis) = prod(b.shape)
#Base.length(b::Basis) = prod(size(b))

"""
GenericBasis(N)
Expand Down Expand Up @@ -43,6 +44,7 @@ CompositeBasis(bases) = CompositeBasis([length(b) for b ∈ bases], bases)
CompositeBasis(bases::Basis...) = CompositeBasis((bases...,))
CompositeBasis(bases::Vector) = CompositeBasis((bases...,))
bases(b::CompositeBasis) = b.bases
Base.size(b::CompositeBasis) = length.(b.bases)

Base.:(==)(b1::T, b2::T) where T<:CompositeBasis = equal_shape(b1.shape, b2.shape)

Expand Down Expand Up @@ -172,8 +174,26 @@ struct KetBraBasis <: Basis
left::Basis
right::Basis
end
KetBraBasis(b::Basis) = KetBraBasis(b,b)
basis_l(b::KetBraBasis) = b.left
basis_r(b::KetBraBasis) = b.right
Base.:(==)(b1::KetBraBasis, b2::KetBraBasis) = (b1.left == b2.left && b1.right == b2.right)
Base.length(b::KetBraBasis) = length(b.left)*length(b.right)
Base.size(b::KetBraBasis) = (length(b.left), length(b.right))

struct ChoiRefSysBasis <: Basis
basis::Basis
end
Base.:(==)(b1::ChoiRefSysBasis, b2::ChoiRefSysBasis) = (b1.basis == b2.basis)
Base.length(b::ChoiRefSysBasis) = length(b.basis)
Base.size(b::ChoiRefSysBasis) = (length(b.basis),)

struct ChoiOutSysBasis <: Basis
basis::Basis
end
Base.:(==)(b1::ChoiOutSysBasis, b2::ChoiOutSysBasis) = (b1.basis == b2.basis)
Base.length(b::ChoiOutSysBasis) = length(b.basis)
Base.size(b::ChoiOutSysBasis) = (length(b.basis),)

"""
PauliBasis()
Expand Down
36 changes: 22 additions & 14 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,46 @@ function equal_bases(a, b)
return true
end

Base.@deprecate PauliBasis(num_qubits) NQubitBasis(num_qubits) false

function samebases(b1::Basis, b2::Basis)
Base.depwarn("`==` should be preferred over `samebases(b1::Basis, b2::Basis)`!", :samebases)
#Base.depwarn("`==` should be preferred over `samebases(b1::Basis, b2::Basis)`!", :samebases)
b1==b2
end

function samebases(b1::Tuple{Basis, Basis}, b2::Tuple{Basis, Basis})
Base.depwarn("`==` should be preferred over `samebases(b1::Tuple{Basis, Basis}, b2::Tuple{Basis, Basis})`!", :samebases)
#Base.depwarn("`==` should be preferred over `samebases(b1::Tuple{Basis, Basis}, b2::Tuple{Basis, Basis})`!", :samebases)
b1==b2 # for checking superoperators
end

function samebases(a::AbstractOperator)
Base.depwarn("`issquare` should be preferred over `samebases(a::AbstractOperator)`!", :check_samebases)
#Base.depwarn("`issquare` should be preferred over `samebases(a::AbstractOperator)`!", :check_samebases)
samebases(a.basis_l, a.basis_r)::Bool # FIXME issue #12
end

function samebases(a::AbstractOperator, b::AbstractOperator)
Base.depwarn("`addible` should be preferred over `samebases(a::AbstractOperator, b::AbstractOperator)`!", :check_samebases)
#Base.depwarn("`addible` should be preferred over `samebases(a::AbstractOperator, b::AbstractOperator)`!", :check_samebases)
samebases(a.basis_l, b.basis_l)::Bool && samebases(a.basis_r, b.basis_r)::Bool # FIXME issue #12
end

function check_samebases(b1, b2)
Base.depwarn("Depending on context, `check__multiplicable`, `check_addible`, or `check_issquare` should be preferred over `check_samebases`!", :check_samebases)
#Base.depwarn("Depending on context, `check__multiplicable`, `check_addible`, or `check_issquare` should be preferred over `check_samebases`!", :check_samebases)
if BASES_CHECK[] && !samebases(b1, b2)
throw(IncompatibleBases())
end
end

function check_samebases(a::Union{AbstractOperator, AbstractSuperOperator})
Base.depwarn("`check_issquare` should be preferred over `check_samebases(a::Union{AbstractOperator, AbstractSuperOperator})`!", :check_samebases)
check_samebases(a.basis_l, a.basis_r) # FIXME issue #12
end
check_samebases(a::AbstractOperator) = check_samebases(a.basis_l, a.basis_r) # FIXME issue #12

"""
check_samebases(a::Union{AbstractOperator, AbstractSuperOperator}) = check_samebases(a.basis_l, a.basis_r) # FIXME issue #12
"""

function multiplicable(b1::Basis, b2::Basis)
Base.depwarn("`==` should be preferred over `multiplicable(b1::Basis, b2::Basis)`!", :multiplicable)
#Base.depwarn("`==` should be preferred over `multiplicable(b1::Basis, b2::Basis)`!", :multiplicable)
b1==b2
end

function multiplicable(b1::CompositeBasis, b2::CompositeBasis)
Base.depwarn("`==` should be preferred over `multiplicable(b1::CompositeBasis, b2::CompositeBasis)`!", :multiplicable)
#Base.depwarn("`==` should be preferred over `multiplicable(b1::CompositeBasis, b2::CompositeBasis)`!", :multiplicable)
if !equal_shape(b1.shape,b2.shape)
return false
end
Expand All @@ -63,4 +62,13 @@ function multiplicable(b1::CompositeBasis, b2::CompositeBasis)
return true
end

Base.@deprecate @samebases(ex) @compatiblebases(ex) false
macro samebases(ex)
return quote
BASES_CHECK.x = false
local val = $(esc(ex))
BASES_CHECK.x = true
val
end
end

#Base.@deprecate @samebases(ex) @compatiblebases(ex) false
6 changes: 4 additions & 2 deletions src/expect_variance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ If an `index` is given, it assumes that `op` is defined in the subsystem specifi
variance(indices, op::AbstractOperator, state::AbstractOperator) =
variance(op, ptrace(state, complement(nsubsystems(state), indices)))

variance(index::Integer, op::AbstractOperator{B,B}, state::AbstractOperator{BC,BC}) where {B,BC<:CompositeBasis} = variance([index], op, state)
variance(index::Integer, op::AbstractOperator, state::AbstractOperator) = variance([index], op, state)

variance(op::AbstractOperator, states::Vector) = [variance(op, state) for state=states]

variance(indices, op::AbstractOperator, states::Vector) = [variance(indices, op, state) for state=states]

function variance(op::AbstractOperator, state::AbstractOperator)
check_multiplicable(op); check_multiplicable(state); check_multiplicable(op,state)
check_multiplicable(op,op)
check_multiplicable(state,state)
check_multiplicable(op,state)
@compatiblebases expect(op*op, state) - expect(op, state)^2
end
6 changes: 3 additions & 3 deletions src/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ multiplicable(a::AbstractOperator, b::AbstractKet) = (basis_r(a) == basis(b))
multiplicable(a::AbstractBra, b::AbstractOperator) = (basis(a) == basis_l(b))
multiplicable(a::AbstractOperator, b::AbstractOperator) = (basis_r(a) == basis_l(b))

basis(a::StateVector) = throw(ArgumentError("basis() is not defined for this type of state vector: $(typeof(op))."))
basis_l(a::AbstractOperator) = throw(ArgumentError("basis_l() is not defined for this type of operator: $(typeof(op))."))
basis_r(a::AbstractOperator) = throw(ArgumentError("basis_r() is not defined for this type of operator: $(typeof(op))."))
basis(a::StateVector) = throw(ArgumentError("basis() is not defined for this type of state vector: $(typeof(a))."))
basis_l(a::AbstractOperator) = throw(ArgumentError("basis_l() is not defined for this type of operator: $(typeof(a))."))
basis_r(a::AbstractOperator) = throw(ArgumentError("basis_r() is not defined for this type of operator: $(typeof(a))."))
basis(a::AbstractOperator) = (basis_l(a) == basis_r(a); basis_l(a))

##
Expand Down
2 changes: 1 addition & 1 deletion test/test_bases.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ comp1 = tensor(b1, b2, b3)
comp2 = tensor(b2, b1, b3)
@test permutesystems(comp1, [2,1,3]) == comp2

@test [b1, b2] !== [b1, b3]
@test [b1, b2] != [b1, b3]
@test !multiplicable(comp1, b1 b2 NLevelBasis(prod(b3.shape)))

end # testset

0 comments on commit b59db60

Please sign in to comment.