From cc2c90a0449f487d63418c98746b700e3e069689 Mon Sep 17 00:00:00 2001 From: goropikari Date: Wed, 2 May 2018 00:43:37 +0900 Subject: [PATCH] support the case `standard_order` is false --- src/prettyprint.jl | 182 ++++++++++++++++++++++++++++++++------------- test/runtests.jl | 150 ++++++++++++++++++++++++++++++++++++- 2 files changed, 277 insertions(+), 55 deletions(-) diff --git a/src/prettyprint.jl b/src/prettyprint.jl index 0d6699b..052c402 100644 --- a/src/prettyprint.jl +++ b/src/prettyprint.jl @@ -1,14 +1,20 @@ using QuantumOptics -import QuantumOptics.printing: showoperatorheader, permuted_sparsedata, permuted_densedata +import QuantumOptics.printing: showoperatorheader, permuted_sparsedata, permuted_densedata, _std_order import Base.show export dirac +print_with_color(:cyan, + """ + DiracNotation.jl: By default, states are printed in standard order. + """) + """ set_properties(; statename = "State", round_digit = 3, number_term = 8, isallterms = false, - isdirac = true) + isdirac = true, + standard_order = true) Set default properties. @@ -18,37 +24,43 @@ Set default properties. - `number_term::Int`: The number of displayed terms. - `isallterms::Bool`: If this is `true`, show all terms. - `isdirac::Bool`: `true` -> Dirac notation. `false` -> matrix style +- `standard_order`: In detail, please read [QuantumOptics.jl Official documentation](https://qojulia.org/documentation/quantumobjects/operators.html#tensor_order-1). + If `isdirac` is `true` and `standard_order` is `false`, the most right hand side + is the first state, i.e., `spinup(SpisBasis(1//2)) ⊗ spindown(SpinBasis(1//2))` (``|01⟩`` in standard order) + is displayed as ``|10⟩``. """ function set_properties(; statename::String=_state_name, round_digit::Int=_digit, number_term::Int=_num_term, isallterms::Bool=_display_all_term, - isdirac::Bool=_diracstyle) + isdirac::Bool=_diracstyle, + standard_order::Bool=_std_order) global _state_name = statename global _digit = round_digit global _num_term = number_term global _display_all_term = isallterms global _diracstyle = isdirac + QuantumOptics.set_printing(standard_order=standard_order) nothing end if isdefined(Main, :IJulia) && Main.IJulia.inited - set_properties(statename="\\mathrm{State}", round_digit=3, number_term=8, isallterms=false, isdirac=true) + set_properties(statename="\\mathrm{State}", round_digit=3, number_term=8, isallterms=false, isdirac=true, standard_order=true) else - set_properties(statename="State", round_digit=3, number_term=8, isallterms=false, isdirac=true) + set_properties(statename="State", round_digit=3, number_term=8, isallterms=false, isdirac=true, standard_order=true) end """ - print_plain_in_md(io::IO, x::Union{Ket,Bra,Operator}) + print_plain_in_md(io::IO, x::AbstractArray) Replace `\n` and ` ` (empty space) with `
` and ` `, respectively. """ -function print_plain_in_md(io::IO, x::Union{Ket,Bra,Operator}) +function print_plain_in_md(io::IO, x::AbstractArray) if isdefined(Main, :IJulia) && Main.IJulia.inited - str = Main.IJulia.limitstringmime(MIME("text/plain"), x.data) + str = Main.IJulia.limitstringmime(MIME("text/plain"), x) n = search(str, '\n') str = str[n+1:end] else - str = sprint((u, v) -> Base.print_matrix(IOContext(u, :limit => true, :compact => true), v, " ", " ", "", " \u2026 ", "\u22ee", " \u22f1 "), x.data) + str = sprint((u, v) -> Base.print_matrix(IOContext(u, :limit => true, :compact => true), v, " ", " ", "", " \u2026 ", "\u22ee", " \u22f1 "), x) end str = replace(str, "\n", "
") @@ -58,7 +70,11 @@ function print_plain_in_md(io::IO, x::Union{Ket,Bra,Operator}) print(io, str) end +""" + showoperatorheader_md(io::IO, x::Union{DenseOperator,SparseOperator}) +Show `Operator` header by markdown. +""" function showoperatorheader_md(io::IO, x::Union{DenseOperator,SparseOperator}) print(io, "$(typeof(x).name.name)(dim=$(length(x.basis_l))x$(length(x.basis_r)))
") if bases.samebases(x) @@ -73,6 +89,16 @@ function showoperatorheader_md(io::IO, x::Union{DenseOperator,SparseOperator}) end end +""" + permuted_braketdata(x::Union{Ket,Bra}) + +Permute data in reverse order. +""" +function permuted_braketdata(x::Union{Ket,Bra}) + perm = collect(length(basis(x).shape):-1:1) + return permutesystems(x, perm).data +end + function show(io::IO, ::MIME"text/markdown", x::Ket) if _diracstyle print(io, "Ket(dim=$(length(x.basis)))
     basis: $(x.basis)
") @@ -80,7 +106,11 @@ function show(io::IO, ::MIME"text/markdown", x::Ket) print(io, "\$" * str * "\$") else print(io, "Ket(dim=$(length(x.basis)))
     basis: $(x.basis)
") - print_plain_in_md(io, x) + if _std_order + print_plain_in_md(io, permuted_braketdata(x) ) + else + print_plain_in_md(io, x.data) + end end end function show(io::IO, ::MIME"text/plain", x::Ket) @@ -89,7 +119,7 @@ function show(io::IO, ::MIME"text/plain", x::Ket) str = aa(x, _state_name) print(io, str) else - show(io, x) + show(io, x) # call the method in QuantumOptics.jl end end function show(io::IO, ::MIME"text/markdown", x::Bra) @@ -99,7 +129,11 @@ function show(io::IO, ::MIME"text/markdown", x::Bra) print(io, "\$" * str * "\$") else print(io, "Bra(dim=$(length(x.basis)))
     basis: $(x.basis)
") - print_plain_in_md(io, x) + if _std_order + print_plain_in_md(io, permuted_braketdata(x) ) + else + print_plain_in_md(io, x.data) + end end end function show(io::IO, ::MIME"text/plain", x::Bra) @@ -108,7 +142,7 @@ function show(io::IO, ::MIME"text/plain", x::Bra) str = aa(x, _state_name) print(io, str) else - show(io, x) + show(io, x) # call the method in QuantumOptics.jl end end function show(io::IO, ::MIME"text/markdown", x::DenseOperator) @@ -118,7 +152,11 @@ function show(io::IO, ::MIME"text/markdown", x::DenseOperator) print(io, "\$" * str * "\$") else showoperatorheader_md(io, x) - print_plain_in_md(io, x) + if _std_order + print_plain_in_md(io, permuted_densedata(x) ) + else + print_plain_in_md(io, x.data) + end end end function show(io::IO, ::MIME"text/markdown", x::SparseOperator) @@ -128,7 +166,11 @@ function show(io::IO, ::MIME"text/markdown", x::SparseOperator) print(io, "\$" * str * "\$") else showoperatorheader_md(io, x) - print_plain_in_md(io, x) + if _std_order + print_plain_in_md(io, permuted_sparsedata(x) ) + else + print_plain_in_md(io, x.data) + end end end function show(io::IO, ::MIME"text/plain", x::Union{DenseOperator,SparseOperator}) @@ -138,7 +180,7 @@ function show(io::IO, ::MIME"text/plain", x::Union{DenseOperator,SparseOperator} str = aa(x, _state_name) print(io, str) else - show(io, x) + show(io, x) # call the method in QuantumOptics.jl end end @@ -149,16 +191,21 @@ end Generate markdown for Dirac notation """ function md(x::Union{Ket,Bra}, statename::String) - shape = x.basis.shape - nq = length(shape) - isfirstterm = true - perm = collect(reverse(1:nq)) - if nq != 1 - x = permutesystems(x, perm) + nq = length(x.basis.shape) + + if _std_order + shape = x.basis.shape + perm = collect(reverse(1:nq)) + if nq != 1 + x = permutesystems(x, perm) + end + else + shape = reverse(x.basis.shape) end data = x.data - braket = ifelse(typeof(x) == Ket, ["|", "\\rangle"], ["\\langle", "|"]) + braket = ifelse(typeof(x) == Ket, ["|", "\\rangle"], ["\\langle", "|"]) + isfirstterm = true numnz = countnz(data) numprint = 0 @@ -199,18 +246,24 @@ function md(x::Union{Ket,Bra}, statename::String) return str end function md(x::Union{DenseOperator,SparseOperator}, statename::String) - rshape = x.basis_r.shape - lshape = x.basis_l.shape - ncol = prod(rshape) - nrow = prod(lshape) - - isfirstterm = true - if typeof(x) == DenseOperator - data = permuted_densedata(x) - elseif typeof(x) == SparseOperator - data = permuted_sparsedata(x) + ncol = prod(x.basis_r.shape) + nrow = prod(x.basis_l.shape) + + if _std_order + rshape = x.basis_r.shape + lshape = x.basis_l.shape + if typeof(x) == DenseOperator + data = permuted_densedata(x) + elseif typeof(x) == SparseOperator + data = permuted_sparsedata(x) + end + else + rshape = reverse(x.basis_r.shape) + lshape = reverse(x.basis_l.shape) + data = x.data end + isfirstterm = true numnz = countnz(data) numprint = 0 @@ -265,16 +318,20 @@ end Generate ASCIIart for Dirac notation. """ function aa(x::Union{Ket,Bra}, statename::String="") - shape = x.basis.shape - nq = length(shape) - isfirstterm = true - perm = collect(reverse(1:nq)) - if nq != 1 - x = permutesystems(x, perm) + nq = length(x.basis.shape) + if _std_order + shape = x.basis.shape + perm = collect(reverse(1:nq)) + if nq != 1 + x = permutesystems(x, perm) + end + else + shape = reverse(x.basis.shape) end data = x.data - braket = ifelse(typeof(x) == Ket, ["|", "⟩"], ["⟨", "|"]) + braket = ifelse(typeof(x) == Ket, ["|", "⟩"], ["⟨", "|"]) + isfirstterm = true numnz = countnz(data) numprint = 0 @@ -315,19 +372,24 @@ function aa(x::Union{Ket,Bra}, statename::String="") return str end function aa(x::Union{DenseOperator,SparseOperator}, statename::String="") - isfirstterm = true - rshape = x.basis_r.shape - lshape = x.basis_l.shape - ncol = prod(rshape) - nrow = prod(lshape) - - isfirstterm = true - if typeof(x) == DenseOperator - data = permuted_densedata(x) - elseif typeof(x) == SparseOperator - data = permuted_sparsedata(x) + ncol = prod(x.basis_r.shape) + nrow = prod(x.basis_l.shape) + + if _std_order + rshape = x.basis_r.shape + lshape = x.basis_l.shape + if typeof(x) == DenseOperator + data = permuted_densedata(x) + elseif typeof(x) == SparseOperator + data = permuted_sparsedata(x) + end + else + rshape = reverse(x.basis_r.shape) + lshape = reverse(x.basis_l.shape) + data = x.data end + isfirstterm = true numnz = countnz(data) numprint = 0 @@ -427,7 +489,20 @@ function ind2Nary(m::Int, dims::Vector{Int}) str *= string(m) return str end - +function ind2Nary_array(m::Int, dims::Vector{Int}) + m = m - 1 + nq = length(dims) + ar = zeros(Int, nq) + product = prod(dims[2:end]) + for ith in 1:nq-1 + d = div(m, product) + m = m - d * product + product = div(product, dims[ith+1]) + ar[ith] = d + end + ar[end] = m + return ar +end """ Nary2ind(x, dims) -> index @@ -447,6 +522,9 @@ function Nary2ind(x::Vector{Int}, dims::Vector{Int}) tmp += x[end] + 1 end +function mirror_world_index(i::Int, dims::Vector{Int}) + return Nary2ind( reverse(ind2Nary_array(i, dims)), reverse(dims) ) +end """ permuted_densedata2(x::DenseOperator) diff --git a/test/runtests.jl b/test/runtests.jl index df5aca3..b22fead 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,5 +1,5 @@ using DiracNotation, QuantumOptics -import DiracNotation: md +import DiracNotation: md, mirror_world_index @static if VERSION < v"0.7.0-DEV.2005" using Base.Test else @@ -8,6 +8,8 @@ end psi1 = basisstate(NLevelBasis(3), 2) psi2 = basisstate(NLevelBasis(5), 3) +us = spinup(SpinBasis(1//2)) +ds = spindown(SpinBasis(1//2)) spin1 = basisstate(SpinBasis(1//2), 1) spin2 = basisstate(SpinBasis(3//2), 4) @@ -25,6 +27,9 @@ function stdout2str(x) display(td, x); str = String(take!(buf)) end + +DiracNotation.set_properties(standard_order=true) +DiracNotation.set_properties(isdirac=true) @test stdout2str(psi1) == "Ket(dim=3)\n basis: NLevel(N=3)\n|State⟩ = |1⟩" @test stdout2str(dagger(psi1)) == "Bra(dim=3)\n basis: NLevel(N=3)\n⟨State| = ⟨1|" @test stdout2str(psi1 ⊗ psi1) == "Ket(dim=9)\n basis: [NLevel(N=3) ⊗ NLevel(N=3)]\n|State⟩ = |11⟩" @@ -44,9 +49,39 @@ end @test stdout2str( spin1 + sigmaz(SpinBasis(1//2)) * sigmax(SpinBasis(1//2)) * spin1 ) == "Ket(dim=2)\n basis: Spin(1/2)\n|State⟩ = |0⟩ -1.0|1⟩" @test stdout2str( coherentstate(b, alpha) ) == "Ket(dim=11)\n basis: Fock(cutoff=10)\n|State⟩ = 0.923|0⟩ + 0.369|1⟩ + 0.104|2⟩ + 0.024|3⟩ + ⋯ + 0.0|7⟩ + 0.0|8⟩ + 0.0|9⟩ + 0.0|10⟩" @test stdout2str( destroy(b) ) == "SparseOperator(dim=11x11)\n basis: Fock(cutoff=10)State = |0⟩⟨1| +1.414 |1⟩⟨2| +1.732 |2⟩⟨3| +2.0 |3⟩⟨4| + ⋯ +2.646 |6⟩⟨7| +2.828 |7⟩⟨8| +3.0 |8⟩⟨9| +3.162 |9⟩⟨10|" -# +@test sprint(dirac, psi1) == "Ket(dim=3)\n basis: NLevel(N=3)\n|ψ⟩ = |1⟩\n" +@test sprint(dirac, dagger(psi1)) == "Bra(dim=3)\n basis: NLevel(N=3)\n⟨ψ| = ⟨1|\n" +@test sprint(dirac, dm(psi1)) == "DenseOperator(dim=3x3)\n basis: NLevel(N=3)\nOperator = |1⟩⟨1|\n" +@test sprint(dirac, us ⊗ ds) == "Ket(dim=4)\n basis: [Spin(1/2) ⊗ Spin(1/2)]\n|ψ⟩ = |01⟩\n" +@test sprint(dirac, (psi1 ⊗ psi1)⊗ dagger(basisstate(NLevelBasis(10), 3)) ) == "DenseOperator(dim=9x10)\n basis left: [NLevel(N=3) ⊗ NLevel(N=3)]\n basis right: NLevel(N=10)\nOperator = |11⟩⟨2|\n" +@test sprint(dirac, sparse((psi1 ⊗ psi1)⊗ dagger(basisstate(NLevelBasis(10), 3))) ) == "SparseOperator(dim=9x10)\n basis left: [NLevel(N=3) ⊗ NLevel(N=3)]\n basis right: NLevel(N=10)\nOperator = |11⟩⟨2|\n" +@test sprint((io, x) -> dirac(io, x, ""), psi1) == "Ket(dim=3)\n basis: NLevel(N=3)\n|ψ⟩ = |1⟩\n" +@test sprint((io, x) -> dirac(io, x, ""), dm(psi1)) == "DenseOperator(dim=3x3)\n basis: NLevel(N=3)\nOperator = |1⟩⟨1|\n" + +@test md(psi1, "ψ") == "| ψ \\rangle = | 1 \\rangle" +@test md(dagger(psi1), "ψ") == "\\langle ψ | = \\langle 1 |" +@test md(dm(psi1), "ρ") == "ρ = | 1 \\rangle \\langle 1 |" +@test md(dm(psi1) ⊗ dm(psi2), "ρ") == "ρ = | 12 \\rangle \\langle 12 |" +@test md(coherentstate(b, alpha), "ψ") == "| ψ \\rangle = 0.923 | 0 \\rangle+0.369 | 1 \\rangle+0.104 | 2 \\rangle+0.024 | 3 \\rangle + \\cdots +0.0 | 7 \\rangle+0.0 | 8 \\rangle+0.0 | 9 \\rangle+0.0 | 10 \\rangle" +@test md(im * coherentstate(b, alpha), "ψ") == "| ψ \\rangle = 0.923i | 0 \\rangle+0.369i | 1 \\rangle+0.104i | 2 \\rangle+0.024i | 3 \\rangle + \\cdots +0.0i | 7 \\rangle+0.0i | 8 \\rangle+0.0i | 9 \\rangle+0.0i | 10 \\rangle" +@test md(spin1 ⊗ spin1 + spindown(SpinBasis(1//2)) ⊗ spindown(SpinBasis(1//2)), "\\psi") == "| \\psi \\rangle = | 00 \\rangle+1.0 | 11 \\rangle" +@test md(spin1 ⊗ spin1 - spindown(SpinBasis(1//2)) ⊗ spindown(SpinBasis(1//2)), "\\psi") == "| \\psi \\rangle = | 00 \\rangle-1.0 | 11 \\rangle" +@test md(spin1 + sigmaz(SpinBasis(1//2)) * sigmax(SpinBasis(1//2)) * spin1, "\\psi") == "| \\psi \\rangle = | 0 \\rangle-1.0 | 1 \\rangle" +@test md(sigmax(SpinBasis(1//2)), "\\rho") == "\\rho = | 0 \\rangle \\langle 1 |+ | 1 \\rangle \\langle 0 |" +@test md(sigmaz(SpinBasis(1//2)), "\\rho") == "\\rho = | 0 \\rangle \\langle 0 |-1.0 | 1 \\rangle \\langle 1 |" +@test md(sigmay(SpinBasis(1//2)), "\\rho") == "\\rho = -i | 0 \\rangle \\langle 1 |+i | 1 \\rangle \\langle 0 |" +@test md(sigmax(SpinBasis(1//2)) + sigmay(SpinBasis(1//2)), "\\rho") == "\\rho = (1.0-i) | 0 \\rangle \\langle 1 |+(1.0+i) | 1 \\rangle \\langle 0 |" +@test md(sigmay(SpinBasis(1//2)) + sigmaz(SpinBasis(1//2)), "\\rho") == "\\rho = | 0 \\rangle \\langle 0 |-i | 0 \\rangle \\langle 1 |+i | 1 \\rangle \\langle 0 |-1.0 | 1 \\rangle \\langle 1 |" + + +################################################################################ +DiracNotation.set_properties(standard_order=true) DiracNotation.set_properties(isdirac=false) +################################################################################ +@test sprint(show, us ⊗ ds) == "Ket(dim=4)\n basis: [Spin(1/2) ⊗ Spin(1/2)]\n 0.0+0.0im\n 1.0+0.0im\n 0.0+0.0im\n 0.0+0.0im" +@test sprint(show, dm(us ⊗ ds)) == "DenseOperator(dim=4x4)\n basis: [Spin(1/2) ⊗ Spin(1/2)]\n 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im\n 0.0+0.0im 1.0+0.0im 0.0+0.0im 0.0+0.0im\n 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im\n 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im" +@test sprint(show, sigmax(SpinBasis(1//2)) ⊗ sigmay(SpinBasis(1//2))) == "SparseOperator(dim=4x4)\n basis: [Spin(1/2) ⊗ Spin(1/2)]\n [4, 1] = 0.0+1.0im\n [3, 2] = 0.0-1.0im\n [2, 3] = 0.0+1.0im\n [1, 4] = 0.0-1.0im" @test stdout2str(psi1) == sprint(show, psi1) @test stdout2str(dagger(psi1)) == sprint(show, dagger(psi1)) @test stdout2str(psi1 ⊗ psi1) == sprint(show, psi1 ⊗ psi1) @@ -66,21 +101,123 @@ DiracNotation.set_properties(isdirac=false) @test stdout2str( spin1 + sigmaz(SpinBasis(1//2)) * sigmax(SpinBasis(1//2)) * spin1 ) == sprint(show, spin1 + sigmaz(SpinBasis(1//2)) * sigmax(SpinBasis(1//2)) * spin1) @test stdout2str( coherentstate(b, alpha) ) == sprint(show, coherentstate(b, alpha)) @test stdout2str( destroy(b) ) == sprint(show, destroy(b)) + +@test sprint(dirac, psi1) == "Ket(dim=3)\n basis: NLevel(N=3)\n|ψ⟩ = |1⟩\n" +@test sprint(dirac, dagger(psi1)) == "Bra(dim=3)\n basis: NLevel(N=3)\n⟨ψ| = ⟨1|\n" +@test sprint(dirac, dm(psi1)) == "DenseOperator(dim=3x3)\n basis: NLevel(N=3)\nOperator = |1⟩⟨1|\n" +@test sprint(dirac, us ⊗ ds) == "Ket(dim=4)\n basis: [Spin(1/2) ⊗ Spin(1/2)]\n|ψ⟩ = |01⟩\n" +@test sprint(dirac, (psi1 ⊗ psi1)⊗ dagger(basisstate(NLevelBasis(10), 3)) ) == "DenseOperator(dim=9x10)\n basis left: [NLevel(N=3) ⊗ NLevel(N=3)]\n basis right: NLevel(N=10)\nOperator = |11⟩⟨2|\n" +@test sprint(dirac, sparse((psi1 ⊗ psi1)⊗ dagger(basisstate(NLevelBasis(10), 3))) ) == "SparseOperator(dim=9x10)\n basis left: [NLevel(N=3) ⊗ NLevel(N=3)]\n basis right: NLevel(N=10)\nOperator = |11⟩⟨2|\n" +@test sprint((io, x) -> dirac(io, x, ""), psi1) == "Ket(dim=3)\n basis: NLevel(N=3)\n|ψ⟩ = |1⟩\n" +@test sprint((io, x) -> dirac(io, x, ""), dm(psi1)) == "DenseOperator(dim=3x3)\n basis: NLevel(N=3)\nOperator = |1⟩⟨1|\n" + +@test md(psi1, "ψ") == "| ψ \\rangle = | 1 \\rangle" +@test md(dagger(psi1), "ψ") == "\\langle ψ | = \\langle 1 |" +@test md(dm(psi1), "ρ") == "ρ = | 1 \\rangle \\langle 1 |" +@test md(dm(psi1) ⊗ dm(psi2), "ρ") == "ρ = | 12 \\rangle \\langle 12 |" +@test md(coherentstate(b, alpha), "ψ") == "| ψ \\rangle = 0.923 | 0 \\rangle+0.369 | 1 \\rangle+0.104 | 2 \\rangle+0.024 | 3 \\rangle + \\cdots +0.0 | 7 \\rangle+0.0 | 8 \\rangle+0.0 | 9 \\rangle+0.0 | 10 \\rangle" +@test md(im * coherentstate(b, alpha), "ψ") == "| ψ \\rangle = 0.923i | 0 \\rangle+0.369i | 1 \\rangle+0.104i | 2 \\rangle+0.024i | 3 \\rangle + \\cdots +0.0i | 7 \\rangle+0.0i | 8 \\rangle+0.0i | 9 \\rangle+0.0i | 10 \\rangle" +@test md(spin1 ⊗ spin1 + spindown(SpinBasis(1//2)) ⊗ spindown(SpinBasis(1//2)), "\\psi") == "| \\psi \\rangle = | 00 \\rangle+1.0 | 11 \\rangle" +@test md(spin1 ⊗ spin1 - spindown(SpinBasis(1//2)) ⊗ spindown(SpinBasis(1//2)), "\\psi") == "| \\psi \\rangle = | 00 \\rangle-1.0 | 11 \\rangle" +@test md(spin1 + sigmaz(SpinBasis(1//2)) * sigmax(SpinBasis(1//2)) * spin1, "\\psi") == "| \\psi \\rangle = | 0 \\rangle-1.0 | 1 \\rangle" +@test md(sigmax(SpinBasis(1//2)), "\\rho") == "\\rho = | 0 \\rangle \\langle 1 |+ | 1 \\rangle \\langle 0 |" +@test md(sigmaz(SpinBasis(1//2)), "\\rho") == "\\rho = | 0 \\rangle \\langle 0 |-1.0 | 1 \\rangle \\langle 1 |" +@test md(sigmay(SpinBasis(1//2)), "\\rho") == "\\rho = -i | 0 \\rangle \\langle 1 |+i | 1 \\rangle \\langle 0 |" +@test md(sigmax(SpinBasis(1//2)) + sigmay(SpinBasis(1//2)), "\\rho") == "\\rho = (1.0-i) | 0 \\rangle \\langle 1 |+(1.0+i) | 1 \\rangle \\langle 0 |" +@test md(sigmay(SpinBasis(1//2)) + sigmaz(SpinBasis(1//2)), "\\rho") == "\\rho = | 0 \\rangle \\langle 0 |-i | 0 \\rangle \\langle 1 |+i | 1 \\rangle \\langle 0 |-1.0 | 1 \\rangle \\langle 1 |" + + + +################################################################################ +DiracNotation.set_properties(standard_order=false) DiracNotation.set_properties(isdirac=true) +################################################################################ +@test stdout2str(psi1) == "Ket(dim=3)\n basis: NLevel(N=3)\n|State⟩ = |1⟩" +@test stdout2str(dagger(psi1)) == "Bra(dim=3)\n basis: NLevel(N=3)\n⟨State| = ⟨1|" +@test stdout2str(psi1 ⊗ psi1) == "Ket(dim=9)\n basis: [NLevel(N=3) ⊗ NLevel(N=3)]\n|State⟩ = |11⟩" +@test stdout2str(dagger(psi1 ⊗ psi1)) == "Bra(dim=9)\n basis: [NLevel(N=3) ⊗ NLevel(N=3)]\n⟨State| = ⟨11|" +@test stdout2str(dagger(psi1 ⊗ psi2)) == "Bra(dim=15)\n basis: [NLevel(N=3) ⊗ NLevel(N=5)]\n⟨State| = ⟨21|" +@test stdout2str(dm(psi1)) == "DenseOperator(dim=3x3)\n basis: NLevel(N=3)State = |1⟩⟨1|" +@test stdout2str(dm(psi1) * im) == "DenseOperator(dim=3x3)\n basis: NLevel(N=3)State = i |1⟩⟨1|" +@test stdout2str(dm(spinup(SpinBasis(1//2))) + dm(spindown(SpinBasis(1//2)))) == "DenseOperator(dim=2x2)\n basis: Spin(1/2)State = |0⟩⟨0| + |1⟩⟨1|" +@test stdout2str(dm(spinup(SpinBasis(1//2))) - dm(spindown(SpinBasis(1//2)))) == "DenseOperator(dim=2x2)\n basis: Spin(1/2)State = |0⟩⟨0| -1.0 |1⟩⟨1|" +@test stdout2str(sparse(dm(psi1))) == "SparseOperator(dim=3x3)\n basis: NLevel(N=3)State = |1⟩⟨1|" +@test stdout2str(psi1 ⊗ dagger(psi2)) == "DenseOperator(dim=3x5)\n basis left: NLevel(N=3)\n basis right: NLevel(N=5)State = |1⟩⟨2|" +@test stdout2str(sparse(psi1 ⊗ dagger(psi2))) == "SparseOperator(dim=3x5)\n basis left: NLevel(N=3)\n basis right: NLevel(N=5)State = |1⟩⟨2|" +@test stdout2str((psi1 ⊗ psi1)⊗ dagger(basisstate(NLevelBasis(10), 3))) == "DenseOperator(dim=9x10)\n basis left: [NLevel(N=3) ⊗ NLevel(N=3)]\n basis right: NLevel(N=10)State = |11⟩⟨2|" +@test stdout2str(sigmay(SpinBasis(1//2)) * spin1) == "Ket(dim=2)\n basis: Spin(1/2)\n|State⟩ = i|1⟩" +@test stdout2str( dagger(sigmay(SpinBasis(1//2)) * spin1) ) == "Bra(dim=2)\n basis: Spin(1/2)\n⟨State| = -i⟨1|" +@test stdout2str( spin1 + sigmax(SpinBasis(1//2)) * spin1 ) == "Ket(dim=2)\n basis: Spin(1/2)\n|State⟩ = |0⟩ + 1.0|1⟩" +@test stdout2str( spin1 + sigmaz(SpinBasis(1//2)) * sigmax(SpinBasis(1//2)) * spin1 ) == "Ket(dim=2)\n basis: Spin(1/2)\n|State⟩ = |0⟩ -1.0|1⟩" +@test stdout2str( coherentstate(b, alpha) ) == "Ket(dim=11)\n basis: Fock(cutoff=10)\n|State⟩ = 0.923|0⟩ + 0.369|1⟩ + 0.104|2⟩ + 0.024|3⟩ + ⋯ + 0.0|7⟩ + 0.0|8⟩ + 0.0|9⟩ + 0.0|10⟩" +@test stdout2str( destroy(b) ) == "SparseOperator(dim=11x11)\n basis: Fock(cutoff=10)State = |0⟩⟨1| +1.414 |1⟩⟨2| +1.732 |2⟩⟨3| +2.0 |3⟩⟨4| + ⋯ +2.646 |6⟩⟨7| +2.828 |7⟩⟨8| +3.0 |8⟩⟨9| +3.162 |9⟩⟨10|" @test sprint(dirac, psi1) == "Ket(dim=3)\n basis: NLevel(N=3)\n|ψ⟩ = |1⟩\n" @test sprint(dirac, dagger(psi1)) == "Bra(dim=3)\n basis: NLevel(N=3)\n⟨ψ| = ⟨1|\n" @test sprint(dirac, dm(psi1)) == "DenseOperator(dim=3x3)\n basis: NLevel(N=3)\nOperator = |1⟩⟨1|\n" +@test sprint(dirac, us ⊗ ds) == "Ket(dim=4)\n basis: [Spin(1/2) ⊗ Spin(1/2)]\n|ψ⟩ = |10⟩\n" @test sprint(dirac, (psi1 ⊗ psi1)⊗ dagger(basisstate(NLevelBasis(10), 3)) ) == "DenseOperator(dim=9x10)\n basis left: [NLevel(N=3) ⊗ NLevel(N=3)]\n basis right: NLevel(N=10)\nOperator = |11⟩⟨2|\n" @test sprint(dirac, sparse((psi1 ⊗ psi1)⊗ dagger(basisstate(NLevelBasis(10), 3))) ) == "SparseOperator(dim=9x10)\n basis left: [NLevel(N=3) ⊗ NLevel(N=3)]\n basis right: NLevel(N=10)\nOperator = |11⟩⟨2|\n" @test sprint((io, x) -> dirac(io, x, ""), psi1) == "Ket(dim=3)\n basis: NLevel(N=3)\n|ψ⟩ = |1⟩\n" @test sprint((io, x) -> dirac(io, x, ""), dm(psi1)) == "DenseOperator(dim=3x3)\n basis: NLevel(N=3)\nOperator = |1⟩⟨1|\n" +@test md(psi1, "ψ") == "| ψ \\rangle = | 1 \\rangle" +@test md(dagger(psi1), "ψ") == "\\langle ψ | = \\langle 1 |" +@test md(dm(psi1), "ρ") == "ρ = | 1 \\rangle \\langle 1 |" +@test md(dm(psi1) ⊗ dm(psi2), "ρ") == "ρ = | 21 \\rangle \\langle 21 |" +@test md(coherentstate(b, alpha), "ψ") == "| ψ \\rangle = 0.923 | 0 \\rangle+0.369 | 1 \\rangle+0.104 | 2 \\rangle+0.024 | 3 \\rangle + \\cdots +0.0 | 7 \\rangle+0.0 | 8 \\rangle+0.0 | 9 \\rangle+0.0 | 10 \\rangle" +@test md(im * coherentstate(b, alpha), "ψ") == "| ψ \\rangle = 0.923i | 0 \\rangle+0.369i | 1 \\rangle+0.104i | 2 \\rangle+0.024i | 3 \\rangle + \\cdots +0.0i | 7 \\rangle+0.0i | 8 \\rangle+0.0i | 9 \\rangle+0.0i | 10 \\rangle" +@test md(spin1 ⊗ spin1 + spindown(SpinBasis(1//2)) ⊗ spindown(SpinBasis(1//2)), "\\psi") == "| \\psi \\rangle = | 00 \\rangle+1.0 | 11 \\rangle" +@test md(spin1 ⊗ spin1 - spindown(SpinBasis(1//2)) ⊗ spindown(SpinBasis(1//2)), "\\psi") == "| \\psi \\rangle = | 00 \\rangle-1.0 | 11 \\rangle" +@test md(spin1 + sigmaz(SpinBasis(1//2)) * sigmax(SpinBasis(1//2)) * spin1, "\\psi") == "| \\psi \\rangle = | 0 \\rangle-1.0 | 1 \\rangle" +@test md(sigmax(SpinBasis(1//2)), "\\rho") == "\\rho = | 0 \\rangle \\langle 1 |+ | 1 \\rangle \\langle 0 |" +@test md(sigmaz(SpinBasis(1//2)), "\\rho") == "\\rho = | 0 \\rangle \\langle 0 |-1.0 | 1 \\rangle \\langle 1 |" +@test md(sigmay(SpinBasis(1//2)), "\\rho") == "\\rho = -i | 0 \\rangle \\langle 1 |+i | 1 \\rangle \\langle 0 |" +@test md(sigmax(SpinBasis(1//2)) + sigmay(SpinBasis(1//2)), "\\rho") == "\\rho = (1.0-i) | 0 \\rangle \\langle 1 |+(1.0+i) | 1 \\rangle \\langle 0 |" +@test md(sigmay(SpinBasis(1//2)) + sigmaz(SpinBasis(1//2)), "\\rho") == "\\rho = | 0 \\rangle \\langle 0 |-i | 0 \\rangle \\langle 1 |+i | 1 \\rangle \\langle 0 |-1.0 | 1 \\rangle \\langle 1 |" + + + +################################################################################ +DiracNotation.set_properties(standard_order=false) +DiracNotation.set_properties(isdirac=false) +################################################################################ +@test sprint(show, us ⊗ ds) == "Ket(dim=4)\n basis: [Spin(1/2) ⊗ Spin(1/2)]\n 0.0+0.0im\n 0.0+0.0im\n 1.0+0.0im\n 0.0+0.0im" +@test sprint(show, dm(us ⊗ ds)) == "DenseOperator(dim=4x4)\n basis: [Spin(1/2) ⊗ Spin(1/2)]\n 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im\n 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im\n 0.0+0.0im 0.0+0.0im 1.0+0.0im 0.0+0.0im\n 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im" +@test sprint(show, sigmax(SpinBasis(1//2)) ⊗ sigmay(SpinBasis(1//2))) == "SparseOperator(dim=4x4)\n basis: [Spin(1/2) ⊗ Spin(1/2)]\n [4, 1] = 0.0+1.0im\n [3, 2] = 0.0+1.0im\n [2, 3] = 0.0-1.0im\n [1, 4] = 0.0-1.0im" +@test stdout2str(psi1) == sprint(show, psi1) +@test stdout2str(dagger(psi1)) == sprint(show, dagger(psi1)) +@test stdout2str(psi1 ⊗ psi1) == sprint(show, psi1 ⊗ psi1) +@test stdout2str(dagger(psi1 ⊗ psi1)) == sprint(show, dagger(psi1 ⊗ psi1)) +@test stdout2str(dagger(psi1 ⊗ psi2)) == sprint(show, dagger(psi1 ⊗ psi2)) +@test stdout2str(dm(psi1)) == sprint(show, dm(psi1)) +@test stdout2str(dm(psi1) * im) == sprint(show, dm(psi1) * im) +@test stdout2str(dm(spinup(SpinBasis(1//2))) + dm(spindown(SpinBasis(1//2)))) == sprint(show, dm(spinup(SpinBasis(1//2))) + dm(spindown(SpinBasis(1//2)))) +@test stdout2str(dm(spinup(SpinBasis(1//2))) - dm(spindown(SpinBasis(1//2)))) == sprint(show, dm(spinup(SpinBasis(1//2))) - dm(spindown(SpinBasis(1//2)))) +@test stdout2str(sparse(dm(psi1))) == sprint(show, sparse(dm(psi1))) +@test stdout2str(psi1 ⊗ dagger(psi2)) == sprint(show, psi1 ⊗ dagger(psi2)) +@test stdout2str(sparse(psi1 ⊗ dagger(psi2))) == sprint(show, sparse(psi1 ⊗ dagger(psi2))) +@test stdout2str((psi1 ⊗ psi1)⊗ dagger(basisstate(NLevelBasis(10), 3))) == sprint(show, (psi1 ⊗ psi1)⊗ dagger(basisstate(NLevelBasis(10), 3))) +@test stdout2str(sigmay(SpinBasis(1//2)) * spin1) == sprint(show, sigmay(SpinBasis(1//2)) * spin1) +@test stdout2str( dagger(sigmay(SpinBasis(1//2)) * spin1) ) == sprint(show, dagger(sigmay(SpinBasis(1//2)) * spin1)) +@test stdout2str( spin1 + sigmax(SpinBasis(1//2)) * spin1 ) == sprint(show, spin1 + sigmax(SpinBasis(1//2)) * spin1) +@test stdout2str( spin1 + sigmaz(SpinBasis(1//2)) * sigmax(SpinBasis(1//2)) * spin1 ) == sprint(show, spin1 + sigmaz(SpinBasis(1//2)) * sigmax(SpinBasis(1//2)) * spin1) +@test stdout2str( coherentstate(b, alpha) ) == sprint(show, coherentstate(b, alpha)) +@test stdout2str( destroy(b) ) == sprint(show, destroy(b)) + +@test sprint(dirac, psi1) == "Ket(dim=3)\n basis: NLevel(N=3)\n|ψ⟩ = |1⟩\n" +@test sprint(dirac, dagger(psi1)) == "Bra(dim=3)\n basis: NLevel(N=3)\n⟨ψ| = ⟨1|\n" +@test sprint(dirac, dm(psi1)) == "DenseOperator(dim=3x3)\n basis: NLevel(N=3)\nOperator = |1⟩⟨1|\n" +@test sprint(dirac, us ⊗ ds) == "Ket(dim=4)\n basis: [Spin(1/2) ⊗ Spin(1/2)]\n|ψ⟩ = |10⟩\n" +@test sprint(dirac, (psi1 ⊗ psi1)⊗ dagger(basisstate(NLevelBasis(10), 3)) ) == "DenseOperator(dim=9x10)\n basis left: [NLevel(N=3) ⊗ NLevel(N=3)]\n basis right: NLevel(N=10)\nOperator = |11⟩⟨2|\n" +@test sprint(dirac, sparse((psi1 ⊗ psi1)⊗ dagger(basisstate(NLevelBasis(10), 3))) ) == "SparseOperator(dim=9x10)\n basis left: [NLevel(N=3) ⊗ NLevel(N=3)]\n basis right: NLevel(N=10)\nOperator = |11⟩⟨2|\n" +@test sprint((io, x) -> dirac(io, x, ""), psi1) == "Ket(dim=3)\n basis: NLevel(N=3)\n|ψ⟩ = |1⟩\n" +@test sprint((io, x) -> dirac(io, x, ""), dm(psi1)) == "DenseOperator(dim=3x3)\n basis: NLevel(N=3)\nOperator = |1⟩⟨1|\n" @test md(psi1, "ψ") == "| ψ \\rangle = | 1 \\rangle" @test md(dagger(psi1), "ψ") == "\\langle ψ | = \\langle 1 |" @test md(dm(psi1), "ρ") == "ρ = | 1 \\rangle \\langle 1 |" -@test md(dm(psi1) ⊗ dm(psi2), "ρ") == "ρ = | 12 \\rangle \\langle 12 |" +@test md(dm(psi1) ⊗ dm(psi2), "ρ") == "ρ = | 21 \\rangle \\langle 21 |" @test md(coherentstate(b, alpha), "ψ") == "| ψ \\rangle = 0.923 | 0 \\rangle+0.369 | 1 \\rangle+0.104 | 2 \\rangle+0.024 | 3 \\rangle + \\cdots +0.0 | 7 \\rangle+0.0 | 8 \\rangle+0.0 | 9 \\rangle+0.0 | 10 \\rangle" @test md(im * coherentstate(b, alpha), "ψ") == "| ψ \\rangle = 0.923i | 0 \\rangle+0.369i | 1 \\rangle+0.104i | 2 \\rangle+0.024i | 3 \\rangle + \\cdots +0.0i | 7 \\rangle+0.0i | 8 \\rangle+0.0i | 9 \\rangle+0.0i | 10 \\rangle" @test md(spin1 ⊗ spin1 + spindown(SpinBasis(1//2)) ⊗ spindown(SpinBasis(1//2)), "\\psi") == "| \\psi \\rangle = | 00 \\rangle+1.0 | 11 \\rangle" @@ -91,3 +228,10 @@ DiracNotation.set_properties(isdirac=true) @test md(sigmay(SpinBasis(1//2)), "\\rho") == "\\rho = -i | 0 \\rangle \\langle 1 |+i | 1 \\rangle \\langle 0 |" @test md(sigmax(SpinBasis(1//2)) + sigmay(SpinBasis(1//2)), "\\rho") == "\\rho = (1.0-i) | 0 \\rangle \\langle 1 |+(1.0+i) | 1 \\rangle \\langle 0 |" @test md(sigmay(SpinBasis(1//2)) + sigmaz(SpinBasis(1//2)), "\\rho") == "\\rho = | 0 \\rangle \\langle 0 |-i | 0 \\rangle \\langle 1 |+i | 1 \\rangle \\langle 0 |-1.0 | 1 \\rangle \\langle 1 |" + +@test mirror_world_index(1, [2,2]) == 1 +@test mirror_world_index(2, [2,2]) == 3 +@test mirror_world_index(3, [2,2]) == 2 +@test mirror_world_index(4, [2,2]) == 4 +@test mirror_world_index(2, [2,3]) == 3 +@test mirror_world_index(4, [2,3]) == 2