Skip to content

Commit

Permalink
Merge pull request #2 from ddhawan11/main
Browse files Browse the repository at this point in the history
Tests and Bug fixes
  • Loading branch information
nmayhall-vt authored Sep 19, 2023
2 parents f7cf80b + f53f89d commit 01c81b9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/PauliOperators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ include("function_base.jl")
include("function_convert.jl")
include("function_commute.jl")
include("function_otimes.jl")

include("function_osum.jl")

# Exports
export Pauli
Expand Down
8 changes: 4 additions & 4 deletions src/function_mul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ end
Multiply two `Pauli`'s together
"""
function Base.:*(p1::Pauli{N}, p2::Pauli{N}) where {N}
θ = (p1.θ + p2.θ + phase(p1.pauli) + phase(p2.pauli) + phase(p1.pauli, p2.pauli)) % 4
θ = (p1.θ + p2.θ + phase(p1.pauli, p2.pauli)) % 4
return Pauli{N}(θ, p1.pauli*p2.pauli)
end

Expand All @@ -215,7 +215,7 @@ function Base.:*(p1::Pauli{N}, p2::FixedPhasePauli{N}) where {N}
# θ = (p1.θ + phase(p2)) % 4
# θ += (2*count_ones(p1.x & p2.z)) % 4
# return Pauli{N}(θ, z, x)
θ = (p1.θ + phase(p1.pauli) + phase(p2) + phase(p1.pauli, p2)) % 4
θ = (p1.θ + phase(p1.pauli, p2)) % 4
return Pauli{N}(θ, p1.pauli*p2)
end

Expand All @@ -225,7 +225,7 @@ function Base.:*(p1::FixedPhasePauli{N}, p2::Pauli{N}) where {N}
# θ = (phase(p1) + p2.θ) % 4
# θ += (2*count_ones(p1.x & p2.z)) % 4
# return Pauli{N}(θ, z, x)
θ = (p2.θ + phase(p1) + phase(p2.pauli) + phase(p1, p2.pauli)) % 4
θ = (p2.θ + phase(p1, p2.pauli)) % 4
return Pauli{N}(θ,p1*p2.pauli)
end

Expand Down Expand Up @@ -277,4 +277,4 @@ function Base.:*(p::ScaledPauli{N}, ψ::KetBitString{N}) where N
tmp = p.pauli.x ψ.v
sign = count_ones(p.pauli.z & tmp) % 2
return p.coeff*(-1)^sign, KetBitString{N}(tmp)
end
end
6 changes: 3 additions & 3 deletions src/function_otimes.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
otimes(p1::Pauli{N}, p2::Pauli{M}) where {N,M} = Pauli{N+M}((p1.θ + p2.θ)%4, p1.pauli p2.pauli)
otimes(p1::FixedPhasePauli{N}, p2::FixedPhasePauli{M}) where {N,M} = FixedPhasePauli{N+M}(p1.z | p2.z << N, p1.x | p2.x << N)
otimes(p::Pauli{N}, fpp::FixedPhasePauli{M}) where {N,M} = otimes(p, Pauli{M}(0,fpp.z, fpp.x))
otimes(fpp::FixedPhasePauli{M}, p::Pauli{N}) where {N,M} = otimes(Pauli{M}(0,fpp.z, fpp.x), p)
otimes(p::Pauli{N}, fpp::FixedPhasePauli{M}) where {N,M} = otimes(p, Pauli{M}(0,fpp))
otimes(fpp::FixedPhasePauli{M}, p::Pauli{N}) where {N,M} = otimes(Pauli{M}(0,fpp), p)

function otimes(ps::PauliSum{N}, p::Pauli{M}) where {N,M}
out = PauliSum(N+M)
Expand All @@ -27,4 +27,4 @@ function otimes(p1::PauliSum{N}, p2::PauliSum{M}) where {N,M}
end
return out
end
const = otimes
const = otimes
4 changes: 2 additions & 2 deletions src/operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ end
Check if operator is diagonal in the computational (z) basis. E.g., does this operator consist of only I and/or Z?
"""
function is_diagonal(p::Pauli)
return count_ones(p.x) == 0
return count_ones(p.pauli.x) == 0
end


Expand All @@ -158,6 +158,6 @@ function expectation_value_sign(p::Pauli{N}, ket::KetBitString{N}) where N

println(p)

count_ones(p.z & ket.v) % 2 == 0 || return -(1im)^p.θ
count_ones(p.pauli.z & ket.v) % 2 == 0 || return -(1im)^p.θ
return (1im)^p.θ
end
22 changes: 17 additions & 5 deletions test/tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ using Random


println("Test Multiply")
for i in 1:10
N = 8
a = random_Pauli(N)
b = random_Pauli(N)

display(a * b)
# println()
# display(s2)

@test all(Matrix(a) * Matrix(b) - Matrix(a * b) .≈ 0)
end


println("Test Addition")
for i in 1:10
N = 8
a = random_Pauli(N)
Expand All @@ -100,14 +114,12 @@ using Random

s1 = a + b + d
s2 = a + c + d
display(s1 * s2)
# println()
# display(s2)

@test all(Matrix(s1) * Matrix(s2) - Matrix(s1 * s2) .≈ 0)
@test all(Matrix(s1) + Matrix(s2) - Matrix(s1 + s2) .≈ 0)
end



a = random_Pauli(6)
b = random_Pauli(6)
s = a + b
Expand Down

0 comments on commit 01c81b9

Please sign in to comment.