Skip to content

Commit

Permalink
changed how equality is handled and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander papageorge committed Jun 6, 2019
1 parent e5b0947 commit ce25ba7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 22 deletions.
24 changes: 18 additions & 6 deletions src/pauli.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ mutable struct DensePauliTransferMatrix{B1<:Tuple{PauliBasis, PauliBasis},
end
end

PauliTransferMatrix(ptm::DensePauliTransferMatrix{B, B, Array{Float64, 2}}) where B <: Tuple{PauliBasis, PauliBasis} = ptm

"""
Base class for χ (process) matrix classes.
"""
Expand Down Expand Up @@ -82,6 +84,8 @@ mutable struct DenseChiMatrix{B1<:Tuple{PauliBasis, PauliBasis},
end
end

DenseChiMatrix(chi_matrix::DenseChiMatrix{B, B, Array{Complex{Float64}, 2}}) where B <: Tuple{PauliBasis, PauliBasis} = chi_matrix

"""
pauli_operators(num_qubits::Int64)
Expand Down Expand Up @@ -120,13 +124,12 @@ function PauliTransferMatrix(sop::DenseSuperOperator{B, B, Array{Complex{Float64
pbv = pauli_basis_vectors(num_qubits)
sop_dim = 2 ^ (2 * num_qubits)
data = Array{Float64}(undef, (sop_dim, sop_dim))
data .= pbv' * sop.data * pbv / sop_dim
data .= real.(pbv' * sop.data * pbv / sop_dim)
return DensePauliTransferMatrix(sop.basis_l, sop.basis_r, data)
end

function SuperOperator(unitary::DenseOperator{B, B, Array{Complex{Float64},2}}) where B <: PauliBasis
return spre(unitary) * spost(unitary')
end
SuperOperator(unitary::DenseOperator{B, B, Array{Complex{Float64},2}}) where B <: PauliBasis = spre(unitary) * spost(unitary')
SuperOperator(sop::DenseSuperOperator{B, B, Array{Complex{Float64}, 2}}) where B <: Tuple{PauliBasis, PauliBasis} = sop

"""
SuperOperator(ptm::DensePauliTransferMatrix)
Expand Down Expand Up @@ -173,7 +176,7 @@ function ChiMatrix(sop::DenseSuperOperator{B, B, Array{Complex{Float64}, 2}}) wh
po = pauli_operators(num_qubits)
data = Array{Complex{Float64}, 2}(undef, (sop_dim, sop_dim))
for (idx, jdx) in Iterators.product(1:sop_dim, 1:sop_dim)
data[idx, jdx] = tr((spre(po[idx]) * spost(po[jdx])).data' .* sop.data) / sop_dim
data[idx, jdx] = tr((spre(po[idx]) * spost(po[jdx])).data' * sop.data) / sop_dim
end
return DenseChiMatrix(sop.basis_l, sop.basis_r, data)
end
Expand All @@ -191,7 +194,7 @@ function PauliTransferMatrix(chi_matrix::DenseChiMatrix{B, B, Array{Complex{Floa
for (idx, jdx) in Iterators.product(1:sop_dim, 1:sop_dim)
data[idx, jdx] = tr(mapreduce(x -> po[idx] * po[x[1]] * po[jdx] * po[x[2]] * chi_matrix.data[x[1], x[2]],
+,
Iterators.product(1:16, 1:16)).data) / sop_dim
Iterators.product(1:16, 1:16)).data) / sop_dim |> real
end
return DensePauliTransferMatrix(chi_matrix.basis_l, chi_matrix.basis_r, data)
end
Expand All @@ -214,4 +217,13 @@ function ChiMatrix(ptm::DensePauliTransferMatrix{B, B, Array{Float64, 2}}) where
return ChiMatrix(SuperOperator(ptm))
end

"""
Equality for all varieties of superoperators.
"""
==(sop1::Union{DensePauliTransferMatrix, DenseSuperOperator, DenseChiMatrix},
sop2::Union{DensePauliTransferMatrix, DenseSuperOperator, DenseChiMatrix}) = ((typeof(sop1) == typeof(sop2)) &
(sop1.basis_l == sop2.basis_l) &
(sop1.basis_r == sop2.basis_r) &
isapprox(sop1.data, sop2.data))

end # end module
1 change: 1 addition & 0 deletions src/superoperators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import SparseArrays: sparse
using ..bases, ..operators, ..operators_dense, ..operators_sparse
using SparseArrays


"""
Base class for all super operator classes.
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ names = [
"test_stochastic_schroedinger.jl",
"test_stochastic_master.jl",
"test_stochastic_semiclassical.jl",
"test_pauli.jl",

"test_printing.jl"
]
Expand Down
41 changes: 31 additions & 10 deletions test/test_pauli.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,38 @@ using QuantumOptics
# Test conversion of unitary matrices to superoperators.
q2 = PauliBasis(2)
CZ = DenseOperator(q2, q2, diagm(0 => [1,1,1,-1]))
sopCZ = SuperOperator(CZ)
CZ_sop = SuperOperator(CZ)

# Test conversion of unitary matrices to superoperators.
@test diag(CZ_sop.data) == ComplexF64[1,1,1,-1,1,1,1,-1,1,1,1,-1,-1,-1,-1,1]
@test CZ_sop.basis_l == CZ_sop.basis_r == (q2, q2)

# Test conversion of superoperator to Pauli transfer matrix.
ptmCZ = PauliTransferMatrix(sopCZ)
cz = zeros(Float64, (16, 16))
for idx in [1,30,47,52,72,91,117,140,166,185,205,210,227,256]
cz[idx] = 1
end
for idx in [106,151]
cz[idx] = -1
end
@test ptmCZ.data == cz
CZ_ptm = PauliTransferMatrix(CZ_sop)

CZ_ptm_test = zeros(Float64, (16, 16))
CZ_ptm_test[[1,30,47,52,72,91,117,140,166,185,205,210,227,256]] = ones(14)
CZ_ptm_test[[106,151]] = -1 * ones(2)

@test CZ_ptm.data == CZ_ptm_test
@test CZ_ptm == PauliTransferMatrix(ChiMatrix(CZ))

# Test conversion among all three bases.
cphase = Complex{Float64}[1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 exp(1im*.6)]

q2 = PauliBasis(2)

CPHASE = DenseOperator(q2, cphase)

CPHASE_sop = SuperOperator(CPHASE)
CPHASE_chi = ChiMatrix(CPHASE)
CPHASE_ptm = PauliTransferMatrix(CPHASE)

@test ChiMatrix(CPHASE_sop) == CPHASE_chi
@test ChiMatrix(CPHASE_ptm) == CPHASE_chi
@test SuperOperator(CPHASE_chi) == CPHASE_sop
@test SuperOperator(CPHASE_ptm) == CPHASE_sop
@test PauliTransferMatrix(CPHASE_sop) == CPHASE_ptm
@test PauliTransferMatrix(CPHASE_chi) == CPHASE_ptm

end # testset
6 changes: 0 additions & 6 deletions test/test_superoperators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,6 @@ x = -s1
@test x.basis_l == (b1, b2)
@test x.basis_r == (b3, b4)

# Test conversion of unitary matrices to superoperators.
b5 = GenericBasis(4)
CZ = DenseOperator(b5, b5, diagm(0 => [1,1,1,-1]))
sopCZ = SuperOperator(CZ)
@test diag(sopCZ.data) == ComplexF64[1,1,1,-1,1,1,1,-1,1,1,1,-1,-1,-1,-1,1]
@test sopCZ.basis_l == sopCZ.basis_r == (b5, b5)

# TODO: Clean-up this part
ωc = 1.2
Expand Down

0 comments on commit ce25ba7

Please sign in to comment.