Skip to content
This repository has been archived by the owner on Dec 18, 2021. It is now read-only.

Commit

Permalink
fix partial_tr (#10)
Browse files Browse the repository at this point in the history
* fix partial_tr

* add vscode

* swap is broken

* bump version

* loose compat

* upgrade YaoBase

* add Manifest

* rm Manifest

* ignore Manifest
  • Loading branch information
Roger-luo authored Nov 21, 2019
1 parent bd06adc commit be82968
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
*.jl.mem
.DS_Store
/dev/
Manifest.toml
Manifest.toml
.vscode
14 changes: 7 additions & 7 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "YaoSym"
uuid = "3b27209a-d3d6-11e9-3c0f-41eb92b2cb9d"
version = "0.2.0"
version = "0.2.1"

[deps]
BitBasis = "50ba71b6-fa0f-514d-ae9a-0916efc90dcf"
Expand All @@ -13,12 +13,12 @@ YaoBase = "a8f54c17-34bc-5a9d-b050-f522fe3f755f"
YaoBlocks = "418bc28f-b43b-5e0b-a6e7-61bbc1a2c1df"

[compat]
BitBasis = "~0.6"
LuxurySparse = "~0.5"
SymEngine = "~0.7"
YaoArrayRegister = "~0.5"
YaoBase = "~0.11"
YaoBlocks = "~0.7"
BitBasis = "0.6"
LuxurySparse = "0.5"
SymEngine = "0.7"
YaoArrayRegister = "0.5"
YaoBase = "0.12"
YaoBlocks = "0.7"
julia = "1"

[extras]
Expand Down
5 changes: 5 additions & 0 deletions src/symengine/patch.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using SymEngine: Basic
export simplify_expi

function Base.iszero(x::Basic)
isempty(free_symbols(x)) && iszero(N(x))
end

SymEngine.free_symbols(syms::Union{Real, Complex}) = Basic[]
SymEngine.free_symbols(syms::AbstractArray{T}) where {T<:Union{Real, Complex}} = Basic[]
SymEngine.free_symbols(syms::AbstractArray{T}) where {T<:Union{Basic, SymEngine.BasicType}} = SymEngine.free_symbols(Matrix(syms))
Expand Down
10 changes: 9 additions & 1 deletion src/symengine/register.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export SymReg, AdjointSymReg, SymRegOrAdjointSymReg, expand

YaoArrayRegister._warn_type(raw::AbstractArray{Basic}) = nothing

const SymReg{B, MT} = ArrayReg{B, Basic, MT}
const SymReg{B, MT} = ArrayReg{B, Basic, MT} where {MT <:AbstractMatrix{Basic}}
const AdjointSymReg{B, MT} = AdjointArrayReg{B, Basic, MT}
const SymRegOrAdjointSymReg{B, MT} = Union{SymReg{B, MT}, AdjointSymReg{B, MT}}

Expand Down Expand Up @@ -34,3 +34,11 @@ Base.:(*)(x::AdjointSymReg{B, MT}, y::AdjointSymReg{B, MT}) where {B, MT} = adjo
Base.:(^)(x::AdjointSymReg{B, MT}, n::Int) where {B, MT} = adjoint(parent(x)^n)

SymEngine.expand(x::SymReg{B}) where B = ArrayReg{B}(expand.(state(x)))

function YaoBase.partial_tr(r::SymReg{B}, locs) where B
orders = setdiff(1:nqubits(r), locs)
focus!(r, orders)
state = sum(rank3(r); dims=2)
relax!(r, orders)
return ArrayReg(state)
end
5 changes: 3 additions & 2 deletions test/symengine/instruct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ end

@testset "apply rot SWAP" begin
@vars θ
res = ket"01" |> rot(SWAP, θ)
@test [ComplexF64(subs(si, θ=>0.5)) for si in res.state] (product_state(bit"01") |> rot(SWAP, 0.5)).state
res = ket"01"
# res |> rot(SWAP, θ)
@test_broken [ComplexF64(subs(si, θ=>0.5)) for si in res.state] (product_state(bit"01") |> rot(SWAP, 0.5)).state
end
8 changes: 7 additions & 1 deletion test/symengine/register.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
using Test, YaoSym
using Test, YaoSym, YaoBase

@testset "constructors" begin
@test ket"011" isa SymReg
@test (ket"011")' isa AdjointSymReg
end

@testset "partial_tr" begin
@vars α β
r = α * ket"101" + β * ket"111"
@test partial_tr(r, 2:3) ==+ β) * ket"1"
end

0 comments on commit be82968

Please sign in to comment.