Skip to content

Commit 41e5a28

Browse files
authored
Overload sparse for StateVectors (#36)
1 parent 56d87ea commit 41e5a28

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/states.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ function sparsebasisstate(::Type{T}, b::Basis, index::Integer) where T
195195
end
196196
sparsebasisstate(b::Basis, indices) = sparsebasisstate(ComplexF64, b, indices)
197197

198+
SparseArrays.sparse(x::Ket) = Ket(x.basis,sparse(x.data))
199+
SparseArrays.sparse(x::Bra) = Bra(x.basis,sparse(x.data))
200+
198201
# Helper functions to check validity of arguments
199202
function check_multiplicable(a::Bra, b::Ket)
200203
if a.basis != b.basis

test/test_states.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Test
22
using QuantumOpticsBase
33
using LinearAlgebra, Random
4+
using SparseArrays
45

56
@testset "states" begin
67

@@ -119,6 +120,10 @@ x2 = basisstate(b2, 1)
119120
@test x1.data[2] == 1
120121
@test basisstate(b, [2, 1]) == x1 x2
121122

123+
# Conversion to sparse
124+
@test length(sparse(x1).data.nzval)==1
125+
@test sparse(x1').data isa SparseVector
126+
122127
# Test permutating systems
123128
b1 = GenericBasis(2)
124129
b2 = GenericBasis(5)

0 commit comments

Comments
 (0)