From 3757acb5b10d021ac8170e33ca16fa36fdab9281 Mon Sep 17 00:00:00 2001 From: janlukas Date: Sat, 21 Sep 2024 13:33:06 +0100 Subject: [PATCH] Homogenised formatting --- Project.toml | 2 +- src/apply_composite.jl | 26 +++++++++++++------------- src/auto_diff.jl | 2 +- src/utils.jl | 17 ++++++++++------- 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/Project.toml b/Project.toml index 9695af9..9b8304e 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "FLOYao" uuid = "6d9310a3-f1d0-41b7-8edb-11c1cf57cd2d" authors = ["janlukas.bosse and contributors"] -version = "1.4.3" +version = "1.4.4" [deps] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" diff --git a/src/apply_composite.jl b/src/apply_composite.jl index 856ebab..e846a6e 100644 --- a/src/apply_composite.jl +++ b/src/apply_composite.jl @@ -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 diff --git a/src/auto_diff.jl b/src/auto_diff.jl index d0c7b5c..7ce2595 100644 --- a/src/auto_diff.jl +++ b/src/auto_diff.jl @@ -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 diff --git a/src/utils.jl b/src/utils.jl index 3485762..554cae9 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -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")) @@ -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) @@ -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::SparseMatrixCSC) + 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::SparseMatrixCSC) @assert size(A, 1) == size(B, 1) && size(A, 2) == size(B, 2) "Dimension mismatch" @@ -365,6 +366,7 @@ function fast_add!(A::AbstractMatrix, B::SparseMatrixCSC) end return A end + function fast_add!(A::AbstractMatrix, B::SparseMatrixCOO) @assert size(A, 1) == size(B, 1) && size(A, 2) == size(B, 2) "Dimension mismatch" for (i, j, v) in zip(B.is, B.js, B.vs) @@ -378,9 +380,9 @@ function fast_add!(A::AbstractMatrix, B::AbstractMatrix) end """ - fast_overlap(y::AbstractVecOrMat, A::SparseMatrixCSC, x::AbstractVecOrMat) + 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::SparseMatrixCSC{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" @@ -395,6 +397,7 @@ function fast_overlap(y::AbstractVecOrMat{T1}, A::SparseMatrixCSC{T2}, x::Abstra end return g end + 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" g = zero(promote_type(T1, T2, T3))