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

Commit

Permalink
update (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
GiggleLiu committed May 31, 2020
1 parent f8097b4 commit 9d19404
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 86 deletions.
4 changes: 2 additions & 2 deletions 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.6.2"
version = "0.6.3"

[deps]
BitBasis = "50ba71b6-fa0f-514d-ae9a-0916efc90dcf"
Expand All @@ -18,7 +18,7 @@ LuxurySparse = "0.5, 0.6"
StaticArrays = "0.12"
StatsBase = "0.32, 0.33"
TupleTools = "1.1"
YaoBase = "0.13"
YaoBase = "0.14"
julia = "1"

[extras]
Expand Down
22 changes: 22 additions & 0 deletions src/YaoArrayRegister.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@ able to simulate a quantum circuit alone with this package in principle.
"""
module YaoArrayRegister

using YaoBase, BitBasis

export ArrayReg, AdjointArrayReg, ArrayRegOrAdjointArrayReg,
transpose_storage, datatype,
# initialization
product_state, zero_state, rand_state, uniform_state, oneto,
# additional
state, statevec, relaxedvec, rank3

# BitBasis
export @bit_str, hypercubic

# YaoBase
export AbstractRegister, AdjointRegister, AllLocs, ComputationalBasis,
DensityMatrix, LocationConflictError, NoPostProcess, NotImplementedError,
PostProcess, QubitMismatchError, RemoveMeasured, ResetTo, addbits!,
collapseto!, density_matrix, fidelity, focus!, insert_qubits!, instruct!,
invorder!, measure, measure!, nactive, nbatch, nqubits, nremain,
partial_tr, probs, purify, relax!, reorder!, select, select!, tracedist,
viewbatch, ρ, basis


include("utils.jl")
include("register.jl")
include("operations.jl")
Expand Down
26 changes: 3 additions & 23 deletions src/density_matrix.jl
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
export DensityMatrix, density_matrix, ρ
export purify

"""
DensityMatrix{B, T, MT}
Density Matrix.
- `B`: batch size
- `T`: element type
"""
struct DensityMatrix{B,T,MT<:AbstractArray{T,3}} <: AbstractRegister{B}
state::MT
end

"""
DensityMatrix(state::AbstractArray{T, 3})
DensityMatrix(state::AbstractMatrix{T})
Create a `DensityMatrix` with a state represented by array.
"""
DensityMatrix(state::MT) where {T,MT<:AbstractArray{T,3}} =
YaoBase.DensityMatrix(state::MT) where {T,MT<:AbstractArray{T,3}} =
DensityMatrix{size(state, 3),T,MT}(state)
DensityMatrix(state::AbstractMatrix) = DensityMatrix(reshape(state, size(state)..., 1))
YaoBase.DensityMatrix(state::AbstractMatrix) = DensityMatrix(reshape(state, size(state)..., 1))

"""
state(ρ::DensityMatrix)
Expand Down Expand Up @@ -63,12 +48,7 @@ end

YaoBase.probs(m::DensityMatrix{1}) = diag(view(m.state, :, :, 1))

"""
purify(r::DensityMatrix{B}; nbit_env::Int=nactive(r)) -> ArrayReg
Get a purification of target density matrix.
"""
function purify(r::DensityMatrix{B}; nbit_env::Int = nactive(r)) where {B}
function YaoBase.purify(r::DensityMatrix{B}; nbit_env::Int = nactive(r)) where {B}
Ne = 1 << nbit_env
Ns = size(r.state, 1)
state = similar(r.state, Ns, Ne, B)
Expand Down
39 changes: 17 additions & 22 deletions src/instruct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,22 @@ function _prepare_instruct(
end

function YaoBase.instruct!(
state::AbstractVecOrMat{T1},
operator::AbstractMatrix{T2},
state::AbstractVecOrMat{T},
operator::AbstractMatrix{T},
locs::Tuple{},
control_locs::NTuple{C,Int} = (),
control_bits::NTuple{C,Int} = (),
) where {T1,T2,M,C}
) where {T,M,C}
return state
end

function YaoBase.instruct!(
state::AbstractVecOrMat{T1},
operator::AbstractMatrix{T2},
state::AbstractVecOrMat{T},
operator::AbstractMatrix{T},
locs::NTuple{M,Int},
control_locs::NTuple{C,Int} = (),
control_bits::NTuple{C,Int} = (),
) where {T1,T2,M,C}

if T2 != T1
@warn "Element Type Mismatch: register $(T1), operator $(T2). Converting operator to match, this may cause performance issue"
operator = copyto!(similar(operator, T1), operator)
end
) where {T,M,C}
operator = sort_unitary(operator, locs)
locs_raw, ic = _prepare_instruct(state, operator, locs, control_locs, control_bits)
return _instruct!(state, autostatic(operator), locs_raw, ic)
Expand Down Expand Up @@ -160,12 +155,12 @@ YaoBase.instruct!(state::AbstractVecOrMat, g::AbstractMatrix, locs::Tuple{Int})
instruct!(state, g, locs...)

function YaoBase.instruct!(
state::AbstractVecOrMat{T1},
U1::AbstractMatrix{T2},
state::AbstractVecOrMat{T},
U1::AbstractMatrix{T},
loc::Int,
) where {T1,T2}
) where {T}
a, c, b, d = U1
instruct_kernel(state, loc, 1 << (loc - 1), 1 << loc, T1(a), T1(b), T1(c), T1(d))
instruct_kernel(state, loc, 1 << (loc - 1), 1 << loc, a, b, c, d)
return state
end

Expand All @@ -185,10 +180,10 @@ YaoBase.instruct!(
) where {T} = instruct!(state, g, locs...)

function YaoBase.instruct!(
state::AbstractVecOrMat{T1},
U1::SDPermMatrix{T2},
state::AbstractVecOrMat{T},
U1::SDPermMatrix{T},
loc::Int,
) where {T1,T2}
) where {T}
U1.perm[1] == 1 && return instruct!(state, Diagonal(U1), loc)
mask = bmask(loc)
b, c = U1.vals
Expand All @@ -211,12 +206,12 @@ YaoBase.instruct!(
) where {T} = instruct!(state, g, locs...)

function YaoBase.instruct!(
state::AbstractVecOrMat{T1},
U1::SDDiagonal{T2},
state::AbstractVecOrMat{T},
U1::SDDiagonal{T},
loc::Int,
) where {T1,T2}
) where {T}
mask = bmask(loc)
a, d = T1.(U1.diag)
a, d = U1.diag
step = 1 << (loc - 1)
step_2 = 1 << loc
@threads for j in 0:step_2:size(state, 1)-step
Expand Down
40 changes: 1 addition & 39 deletions src/register.jl
Original file line number Diff line number Diff line change
@@ -1,43 +1,5 @@
using YaoBase, BitBasis
import BitBasis: BitStr, BitStr64

export ArrayReg,
AdjointArrayReg,
ArrayRegOrAdjointArrayReg,
transpose_storage,
# YaoBase
nqubits,
nactive,
nremain,
nbatch,
viewbatch,
addbits!,
insert_qubits!,
datatype,
probs,
reorder!,
invorder!,
collapseto!,
fidelity,
tracedist,
# YaoBase deprecated
addbit!,
reset!,
# additional
state,
statevec,
relaxedvec,
rank3,
# BitBasis
@bit_str,
hypercubic,
# initialization
product_state,
zero_state,
rand_state,
uniform_state,
oneto

"""
ArrayReg{B, T, MT <: AbstractMatrix{T}} <: AbstractRegister{B}
Expand Down Expand Up @@ -86,7 +48,7 @@ Returns the numerical data type used by register.
is not exactly the same with `AbstractArray`, it is an iterator of several
registers.
"""
YaoBase.@interface datatype(r::ArrayReg{B,T}) where {B,T} = T
datatype(r::ArrayReg{B,T}) where {B,T} = T

function _warn_type(raw::AbstractArray{T}) where {T}
T <: Complex || @warn "Input type of `ArrayReg` is not Complex, got $(eltype(raw))"
Expand Down

0 comments on commit 9d19404

Please sign in to comment.