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

Commit

Permalink
new product state and density matrix interfaces (#89)
Browse files Browse the repository at this point in the history
* new product state and density matrix interceas

* add reduced density matrix test

* fix tests
  • Loading branch information
GiggleLiu authored Dec 7, 2021
1 parent 3734091 commit e7897b6
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "YaoArrayRegister"
uuid = "e600142f-9330-5003-8abb-0ebd767abc51"
version = "0.7.9"
version = "0.7.10"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
4 changes: 4 additions & 0 deletions src/density_matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ state(ρ::DensityMatrix) = ρ.state
YaoBase.nqubits::DensityMatrix) = log2dim1(state(ρ))
YaoBase.nactive::DensityMatrix) = nqubits(ρ)
YaoBase.nbatch(dm::DensityMatrix{B}) where {B} = B
function YaoBase.density_matrix(reg::ArrayReg, qubits)
freg = focus!(copy(reg), qubits)
return density_matrix(freg)
end
YaoBase.density_matrix(reg::ArrayReg{1}) = DensityMatrix(reg.state * reg.state')
function YaoBase.density_matrix(reg::ArrayReg{B}) where {B}
M = size(reg.state, 1)
Expand Down
15 changes: 14 additions & 1 deletion src/register.jl
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,24 @@ julia> product_state(bit"100"; nbatch=2)
ArrayReg{2, Complex{Float64}, Array...}
active qubits: 3/3
julia> product_state(ComplexF32, bit"101"; nbatch=2)
julia> r1 = product_state(ComplexF32, bit"100"; nbatch=2)
ArrayReg{2, Complex{Float32}, Array...}
active qubits: 3/3
julia> r2 = product_state(ComplexF32, [0, 0, 1]; nbatch=2)
ArrayReg{2, Complex{Float32}, Array...}
active qubits: 3/3
julia> r1 ≈ r2 # because we read bit strings from right to left, vectors from left to right.
true
```
"""
product_state(bit_str::BitStr; nbatch::Int = 1) =
product_state(ComplexF64, bit_str; nbatch = nbatch)

product_state(bit_str::AbstractVector; nbatch::Int = 1) =
product_state(ComplexF64, bit_str; nbatch = nbatch)

"""
product_state([T=ComplexF64], total::Int, bit_config::Integer; nbatch=1, no_transpose_storage=false)
Expand Down Expand Up @@ -355,6 +365,9 @@ product_state(total::Int, bit_config::Integer; kwargs...) =
product_state(::Type{T}, bit_str::BitStr{N}; kwargs...) where {T,N} =
product_state(T, N, buffer(bit_str); kwargs...)

product_state(::Type{T}, bit_configs::AbstractVector; kwargs...) where T =
product_state(T, bit_literal(bit_configs...); kwargs...)

function product_state(
::Type{T},
total::Int,
Expand Down
13 changes: 11 additions & 2 deletions test/density_matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,23 @@ end
@test reg_p |> isnormalized
@test reg_p |> exchange_sysenv |> probs |> maximum 1
reg_p = purify(reg |> ρ; nbit_env = 0)
@test Yao.fidelity(reg, reg_p) [1]
@test fidelity(reg, reg_p) 1

reg = rand_state(6; nbatch = 10)
reg_p = purify(reg |> ρ)
@test reg_p |> isnormalized
@test reg_p |> exchange_sysenv |> probs |> maximum 1
reg_p = purify(reg |> ρ; nbit_env = 0)
@test Yao.fidelity(reg, reg_p) ones(10)
@test fidelity(reg, reg_p) ones(10)
reg_p = purify(reg |> ρ; nbit_env = 2)
@test reg_p |> nqubits == 8
end

@testset "reduce density matrix" begin
reg = (product_state(bit"00000") + product_state(bit"11111")) / sqrt(2)
rdm = density_matrix(reg, (1,))
@test Matrix(rdm) [1/2 0; 0 1/2]
reg = product_state([1,0,0])
rdm = density_matrix(reg, (1,2))
@test Matrix(rdm) [0 0 0 0; 0 1 0 0; 0 0 0 0; 0 0 0 0]
end
2 changes: 2 additions & 0 deletions test/register.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ end
@testset "test product state" begin
st = state(product_state(T, bit"100"; nbatch = 1))
@test !(st isa Transpose)
st2 = state(product_state(T, [0, 0, 1]; nbatch = 1))
@test st2 st
st = state(product_state(T, bit"100"; nbatch = 2, no_transpose_storage = true))
@test !(st isa Transpose)
st = state(product_state(T, bit"100"; nbatch = 2))
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ end
@testset "test measure" begin
include("measure.jl")
end

@testset "test density matrix" begin
include("density_matrix.jl")
end

2 comments on commit e7897b6

@Roger-luo
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/50109

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.7.10 -m "<description of version>" e7897b6b0f9a99a5885bfab087e1556da714a03d
git push origin v0.7.10

Please sign in to comment.