Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
GiggleLiu committed Sep 21, 2024
2 parents f58c086 + 3757acb commit 2af85c6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "FLOYao"
uuid = "6d9310a3-f1d0-41b7-8edb-11c1cf57cd2d"
authors = ["janlukas.bosse <[email protected]> and contributors"]
version = "1.4.3"
version = "1.4.4"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
26 changes: 13 additions & 13 deletions src/apply_composite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,26 @@ end

# Defined in FLOYao.jl
# const RGate = RotationGate{2,<:Real,<:PauliKronBlock}
function YaoBlocks.unsafe_apply!(reg::MajoranaReg, b::RGate)
i1, i2 = kron2majoranaindices(b.block)
s, c = sincos(b.theta)
function YaoBlocks.unsafe_apply!(reg::MajoranaReg, ψ2::RGate)
i1, i2 = kron2majoranaindices(ψ2.block)
s, c = sincos(ψ2.theta)
for k in 1:size(reg.state, 2)
a, b = reg.state[i1,k], reg.state[i2,k]
reg.state[i1,k] = c * a + s * b
reg.state[i2,k] = c * b - s * a
ψ1, ψ2 = reg.state[i1,k], reg.state[i2,k]
reg.state[i1,k] = c * ψ1 + s * ψ2
reg.state[i2,k] = c * ψ2 - s * ψ1
end
return reg
end

function YaoBlocks.unsafe_apply!(reg::MajoranaReg, b::PutBlock{2,N,<:RGate}) where {N}
areconsecutive(b.locs) || throw(NonFLOException("$(b.blocks) on $(b.locs) is not a FLO gate"))
function YaoBlocks.unsafe_apply!(reg::MajoranaReg, ψ2::PutBlock{2,N,<:RGate}) where {N}
areconsecutive(ψ2.locs) || throw(NonFLOException("$(ψ2.blocks) on $(ψ2.locs) is not a FLO gate"))
# goddamnit, 1-based indexing
i1, i2 = 2 * (minimum(b.locs) - 1) .+ kron2majoranaindices(b.content.block)
s, c = sincos(b.content.theta)
i1, i2 = 2 * (minimum(ψ2.locs) - 1) .+ kron2majoranaindices(ψ2.content.block)
s, c = sincos(ψ2.content.theta)
for k in 1:size(reg.state, 2)
a, b = reg.state[i1, k], reg.state[i2, k]
reg.state[i1, k] = c * a + s * b
reg.state[i2, k] = c * b - s * a
ψ1, ψ2 = reg.state[i1, k], reg.state[i2, k]
reg.state[i1, k] = c * ψ1 + s * ψ2
reg.state[i2, k] = c * ψ2 - s * ψ1
end
return reg
end
Expand Down
2 changes: 1 addition & 1 deletion src/auto_diff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function Yao.AD.expect_g(op::AbstractAdd, in::MajoranaReg)
ham = yaoham2majoranasquares(op)
inδ = copy(in)
inδ.state .= ham * inδ.state
@inbounds for i in 1:nqudits(in)
@inbounds for i in 1:nqudits(in)
for k in 1:size(inδ.state, 1)
inδ.state[k,2i-1], inδ.state[k,2i] = -inδ.state[k,2i], inδ.state[k,2i-1]
end
Expand Down
11 changes: 6 additions & 5 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ end
Get the indices of majorana operators from a kronecker product of pauli operators
"""
function kron2majoranaindices(k::KronBlock{D, M, <:NTuple{M, PauliGate}}) where {D, M}
function kron2majoranaindices(k::KronBlock{2, M, <:NTuple{M, PauliGate}}) where {M}
locs = first.(k.locs)
perm = YaoBlocks.TupleTools.sortperm(locs)
areconsecutive(ntuple(i->locs[perm[i]], M)) || throw(NonFLOException("$k is not acting on consecutive qubits"))
Expand Down Expand Up @@ -281,8 +281,6 @@ function yaoham2majoranasquares(::Type{T}, yaoham::Add{2}) where {T<:Real}
end
return ham
end
_rmul!(A::SparseMatrixCOO, α::Real) = SparseMatrixCOO(A.is, A.js, rmul!(A.vs, α), A.m, A.n)
_rmul!(A::AbstractMatrix, α::Real) = rmul!(A, α)

function yaoham2majoranasquares(::Type{T}, yaoham::KronBlock{2}) where {T<:Real}
i1, i2 = kron2majoranaindices(yaoham)
Expand Down Expand Up @@ -351,10 +349,13 @@ random_orthogonal_matrix(n) = random_orthogonal_matrix(Float64, n)
# -------------------------------------------
# Utilities for fast sparse matrix operations
# -------------------------------------------
_rmul!(A::SparseMatrixCOO, α::Real) = SparseMatrixCOO(A.is, A.js, rmul!(A.vs, α), A.m, A.n)
_rmul!(A::AbstractMatrix, α::Real) = rmul!(A, α)

"""
fast_add!(A::AbstractMatrix, B::AbstractMatrix)
Fast implementation of `A .+= B` for sparse `B`.
Fast implementation of `A .+= B` optimised for sparse `B`.
"""
function fast_add!(A::AbstractMatrix, B::SparseMatrixCOO)
@assert size(A, 1) == size(B, 1) && size(A, 2) == size(B, 2) "Dimension mismatch"
Expand All @@ -371,7 +372,7 @@ end
"""
fast_overlap(y::AbstractVecOrMat, A::AbstractMatrix, x::AbstractVecOrMat)
Fast implementation of `tr(y' * A * x)` for sparse `A`.
Fast implementation of `tr(y' * A * x)` optimised for sparse `A`.
"""
function fast_overlap(y::AbstractVecOrMat{T1}, A::SparseMatrixCOO{T2}, x::AbstractVecOrMat{T3}) where {T1,T2,T3}
@assert size(x, 1) == size(A, 2) && size(y, 1) == size(A, 1) && size(x, 2) == size(y, 2) "Dimension mismatch"
Expand Down

0 comments on commit 2af85c6

Please sign in to comment.