diff --git a/src/pauli.jl b/src/pauli.jl index cbeb21c1..253cd3b3 100644 --- a/src/pauli.jl +++ b/src/pauli.jl @@ -4,6 +4,7 @@ export PauliBasis, PauliTransferMatrix, DensePauliTransferMatrix, ChiMatrix, DenseChiMatrix import Base: == +import Base: isapprox import Base: * using ..bases, ..spin, ..superoperators @@ -300,10 +301,12 @@ function ChiMatrix(ptm::DensePauliTransferMatrix{B, B, Matrix{Float64}}) where B 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)) +==(sop1::T, sop2::T) where T<:Union{DensePauliTransferMatrix, DenseSuperOperator, DenseChiMatrix} = sop1.data == sop2.data +==(sop1::Union{DensePauliTransferMatrix, DenseSuperOperator, DenseChiMatrix}, sop2::Union{DensePauliTransferMatrix, DenseSuperOperator, DenseChiMatrix}) = false + +"""Approximate equality for all varieties of superoperators.""" +function isapprox(sop1::T, sop2::T; kwargs...) where T<:Union{DensePauliTransferMatrix, DenseSuperOperator, DenseChiMatrix} + return isapprox(sop1.data, sop2.data; kwargs...) +end end # end module diff --git a/test/test_pauli.jl b/test/test_pauli.jl index 8d1ad05f..66c51944 100644 --- a/test/test_pauli.jl +++ b/test/test_pauli.jl @@ -54,6 +54,22 @@ CNOT_ptm = PauliTransferMatrix(CNOT) @test DenseChiMatrix((q2, q2), (q2, q2), CNOT_chi.data) == CNOT_chi # Test equality and conversion among all three bases. +ident = Complex{Float64}[1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1] + +IDENT = DenseOperator(q2, ident) + +IDENT_sop = SuperOperator(IDENT) +IDENT_chi = ChiMatrix(IDENT) +IDENT_ptm = PauliTransferMatrix(IDENT) + +@test ChiMatrix(IDENT_sop) == IDENT_chi +@test ChiMatrix(IDENT_ptm) == IDENT_chi +@test SuperOperator(IDENT_chi) == IDENT_sop +@test SuperOperator(IDENT_ptm) == IDENT_sop +@test PauliTransferMatrix(IDENT_sop) == IDENT_ptm +@test PauliTransferMatrix(IDENT_chi) == IDENT_ptm + +# Test approximate equality and 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)] CPHASE = DenseOperator(q2, cphase) @@ -62,15 +78,15 @@ 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 +@test isapprox(ChiMatrix(CPHASE_sop), CPHASE_chi) +@test isapprox(ChiMatrix(CPHASE_ptm), CPHASE_chi) +@test isapprox(SuperOperator(CPHASE_chi), CPHASE_sop) +@test isapprox(SuperOperator(CPHASE_ptm), CPHASE_sop) +@test isapprox(PauliTransferMatrix(CPHASE_sop), CPHASE_ptm) +@test isapprox(PauliTransferMatrix(CPHASE_chi), CPHASE_ptm) # Test composition. -@test ChiMatrix(CPHASE) * ChiMatrix(CNOT) == ChiMatrix(CPHASE * CNOT) -@test PauliTransferMatrix(CPHASE) * PauliTransferMatrix(CNOT) == PauliTransferMatrix(CPHASE * CNOT) +@test isapprox(ChiMatrix(CPHASE) * ChiMatrix(CNOT), ChiMatrix(CPHASE * CNOT)) +@test isapprox(PauliTransferMatrix(CPHASE) * PauliTransferMatrix(CNOT), PauliTransferMatrix(CPHASE * CNOT)) end # testset