Skip to content

Commit

Permalink
defined some apply! methods (#103)
Browse files Browse the repository at this point in the history
* moved apply! implementations

* added tests

* Update apply.jl

* bump version number and quiet jet tests

---------

Co-authored-by: Stefan Krastanov <[email protected]>
  • Loading branch information
ba2tro and Krastanov authored Jun 14, 2023
1 parent d9b5ae1 commit f98ec89
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "QuantumOpticsBase"
uuid = "4f57444f-1401-5e15-980d-4471b28d5678"
version = "0.4.2"
version = "0.4.3"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand All @@ -19,7 +19,7 @@ Adapt = "1, 2, 3.3"
FFTW = "1.2"
FillArrays = "0.13, 1"
LRUCache = "1"
QuantumInterface = "0.2.0"
QuantumInterface = "0.2.1"
Strided = "1, 2"
UnsafeArrays = "1"
julia = "1.3"
Expand Down
5 changes: 4 additions & 1 deletion src/QuantumOpticsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using SparseArrays, LinearAlgebra, LRUCache, Strided, UnsafeArrays, FillArrays
import LinearAlgebra: mul!, rmul!

import QuantumInterface: dagger, directsum, , dm, embed, expect, identityoperator,
permutesystems, projector, ptrace, reduced, tensor, , variance
permutesystems, projector, ptrace, reduced, tensor, , variance, apply!, basis, AbstractSuperOperator

# index helpers
import QuantumInterface: complement, remove, shiftremove, reducedindices!, check_indices, check_sortedindices, check_embed_indices
Expand Down Expand Up @@ -57,6 +57,8 @@ export Basis, GenericBasis, CompositeBasis, basis,
ChiMatrix, DenseChiMatrix, avg_gate_fidelity,
SumBasis, directsum, , LazyDirectSum, getblock, setblock!,
qfunc, wigner, coherentspinstate, qfuncsu2, wignersu2
#apply
apply!

include("polynomials.jl")
include("bases.jl")
Expand All @@ -82,5 +84,6 @@ include("metrics.jl")
include("spinors.jl")
include("phasespace.jl")
include("printing.jl")
include("apply.jl")

end # module
21 changes: 21 additions & 0 deletions src/apply.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function apply!(state::Ket, indices, operation::Operator)
op = basis(state)==basis(operation) ? operation : embed(basis(state), indices, operation)
state.data = (op*state).data
state
end

function apply!(state::Operator, indices, operation::Operator)
op = basis(state)==basis(operation) ? operation : embed(basis(state), indices, operation)
state.data = (op*state*op').data
state
end

function apply!(state::Ket, indices, operation::T) where {T<:AbstractSuperOperator}
apply!(dm(state), indices, operation)
end

function apply!(state::Operator, indices, operation::T) where {T<:AbstractSuperOperator}
op = basis(state)==basis(operation) ? operation : embed(basis(state), indices, operation)
state.data = (op*state).data
state
end
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ names = [
"test_printing.jl",

"test_aqua.jl",
"test_jet.jl"
"test_jet.jl",
"test_apply.jl"
]

detected_tests = filter(
Expand Down
54 changes: 54 additions & 0 deletions test/test_apply.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using QuantumOpticsBase
using QuantumInterface
using Test

@testset "apply" begin

_b2 = SpinBasis(1//2)
_l0 = spinup(_b2)
_l1 = spindown(_b2)
_x = sigmax(_b2)
_y = sigmay(_b2)


@test QuantumOpticsBase.apply!(_l0_l1, 1, _x) == _l1_l1
@test QuantumOpticsBase.apply!(_x, 1, _y) == _x

# Test Operator with IncompatibleBases
_l01 = _l0_l1
op = projector(_l0, _l01')

try
QuantumOpticsBase.apply!(_l0, 1, op)
catch e
@test typeof(e) <: QuantumInterface.IncompatibleBases
end

try
QuantumOpticsBase.apply!(_x, 1, op)
catch e
@test typeof(e) <: QuantumInterface.IncompatibleBases
end

# Test SuperOperator
sOp = spre(create(FockBasis(1)))
st = coherentstate(FockBasis(1), 1.4)

@test QuantumOpticsBase.apply!(st, 1, sOp).data == (sOp*projector(st)).data

# Test SuperOperator with IncompatibleBases
b1 = FockBasis(1)
b2 = FockBasis(2)

k1 = coherentstate(b1, 0.39)
k2 = coherentstate(b2, 1.4)
op = projector(k1, k2')
sOp2 = spre(op)

try
QuantumOpticsBase.apply!(st, 1, sOp2)
catch e
@test typeof(e) <: QuantumInterface.IncompatibleBases
end

end #testset
3 changes: 2 additions & 1 deletion test/test_jet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ using LinearAlgebra, LRUCache, Strided, Dates, SparseArrays
AnyFrameModule(SparseArrays))
)
@show rep
@test length(JET.get_reports(rep)) == 0
@test length(JET.get_reports(rep)) <= 1
@test_broken length(JET.get_reports(rep)) == 0
end
end # testset

2 comments on commit f98ec89

@Krastanov
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/85624

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.3 -m "<description of version>" f98ec890036ca8de661d877a05471b6e2cfb2912
git push origin v0.4.3

Please sign in to comment.