Skip to content

Commit

Permalink
Add haar random states and unitaries (#143)
Browse files Browse the repository at this point in the history
* Add randstate_haar and randunitary_haar

* Silence Aqua ambiguities test for StatsBase pulled in by RandomMatrices
  • Loading branch information
akirakyle authored Sep 21, 2023
1 parent 4c6296c commit 3a63760
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ LRUCache = "8ac3fa9e-de4c-5943-b1dc-09c6b5f20637"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
QuantumInterface = "5717a53b-5d69-4fa3-b976-0bf2f97ca1e5"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
RandomMatrices = "2576dda1-a324-5b11-aa66-c48ed7e3c618"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Strided = "5e0ebb24-38b0-5f93-81fe-25c709ecae67"
UnsafeArrays = "c4a57d5a-5b31-53a6-b365-19f8c011fbd6"
Expand All @@ -24,6 +25,7 @@ FastGaussQuadrature = "0.5"
FillArrays = "0.13, 1"
LRUCache = "1"
QuantumInterface = "0.3.0"
RandomMatrices = "0.5"
Strided = "1, 2"
UnsafeArrays = "1"
julia = "1.6"
Expand All @@ -39,10 +41,11 @@ LRUCache = "8ac3fa9e-de4c-5943-b1dc-09c6b5f20637"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
Strided = "5e0ebb24-38b0-5f93-81fe-25c709ecae67"
StridedViews = "4db3bf67-4bd7-4b4e-b153-31dc3fb37143"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
UnsafeArrays = "c4a57d5a-5b31-53a6-b365-19f8c011fbd6"

[targets]
test = ["LinearAlgebra", "SparseArrays", "Random", "Test", "Aqua", "JET", "Adapt", "Dates", "FFTW", "LRUCache", "Strided", "StridedViews", "UnsafeArrays", "FillArrays"]
test = ["LinearAlgebra", "SparseArrays", "Random", "Test", "Aqua", "JET", "Adapt", "Dates", "FFTW", "LRUCache", "Strided", "StridedViews", "UnsafeArrays", "FillArrays", "StatsBase"]
1 change: 1 addition & 0 deletions src/QuantumOpticsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export Basis, GenericBasis, CompositeBasis, basis,
displace, displace_analytical, displace_analytical!,
squeeze,
randstate, randoperator, thermalstate, coherentthermalstate, phase_average, passive_state,
randstate_haar, randunitary_haar,
#spin
SpinBasis, sigmax, sigmay, sigmaz, sigmap, sigmam, spinup, spindown,
#subspace
Expand Down
15 changes: 15 additions & 0 deletions src/state_definitions.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using RandomMatrices
"""
randstate([T=ComplexF64,] basis)
Expand All @@ -10,6 +11,13 @@ function randstate(::Type{T}, b::Basis) where T
end
randstate(b) = randstate(ComplexF64, b)

"""
randstate_haar(b::Basis)
Returns a Haar random pure state of dimension `length(b)` by applying a Haar random unitary to a fixed pure state.
"""
randstate_haar(b::Basis) = Ket(b, rand(Haar(2), length(b), 2)[:,1])

"""
randoperator([T=ComplexF64,] b1[, b2])
Expand All @@ -20,6 +28,13 @@ randoperator(b1::Basis, b2::Basis) = randoperator(ComplexF64, b1, b2)
randoperator(::Type{T}, b::Basis) where T = randoperator(T, b, b)
randoperator(b) = randoperator(ComplexF64, b)

"""
randunitary_haar(b::Basis)
Returns a Haar random unitary matrix of dimension `length(b)`.
"""
randunitary_haar(b::Basis) = DenseOperator(b, b, rand(Haar(2), length(b), 2))

"""
thermalstate(H,T)
Expand Down
4 changes: 3 additions & 1 deletion test/test_aqua.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ using Test
using QuantumOpticsBase
using Aqua
using FillArrays
using StatsBase

@testset "aqua" begin
Aqua.test_all(QuantumOpticsBase;
ambiguities=(exclude=[FillArrays.reshape],), # Due to https://github.com/JuliaArrays/FillArrays.jl/issues/105#issuecomment-1518406018
ambiguities=(exclude=[FillArrays.reshape, # Due to https://github.com/JuliaArrays/FillArrays.jl/issues/105#issuecomment-1518406018
StatsBase.TestStat, StatsBase.:(==) , StatsBase.sort!],), # Due to https://github.com/JuliaStats/StatsBase.jl/issues/861
piracy=(broken=true,)
)
# manual piracy check to exclude identityoperator
Expand Down
10 changes: 10 additions & 0 deletions test/test_state_definitions.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Test
using QuantumOpticsBase
using Random

@testset "state_definitions" begin

Expand Down Expand Up @@ -37,4 +38,13 @@ end
@test isapprox(expect(destroy(b),rpas),0, atol=1e-14)
@test isapprox(entropy_vn(rpas),S, atol=1e-14)

Random.seed!(0)

for n in 1:5:50
b = FockBasis(n)
@test isapprox(norm(randstate_haar(b)), 1, atol=1e-8)
U = randunitary_haar(b)
@test isapprox(dagger(U)*U, identityoperator(b), atol=1e-8)
end

end # testset

0 comments on commit 3a63760

Please sign in to comment.