Skip to content

Commit 689b218

Browse files
committed
Added support for commutaor VectorPauliSum and tests.
1 parent 07468a8 commit 689b218

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

src/PauliAlgebra/paulioperations.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,37 @@ Calculate the commutator of a `PauliSum` and a `PauliString`.
373373
commutator(psum::PauliSum, pstr::PauliString) = commutator(psum, PauliSum(pstr))
374374
commutator(pstr::PauliString, psum::PauliSum) = commutator(PauliSum(pstr), psum)
375375

376+
377+
"""
378+
commutator(vpsum1::VectorPauliSum, vpsum2::VectorPauliSum)
379+
380+
Calculate the commutator of two `VectorPauliSum`s.
381+
Returns a `VectorPauliSum` with complex coefficients.
382+
383+
# Example
384+
```julia
385+
vpsum = VectorPauliSum(3, [1, 2], [1.]) # 1.0 * X + 1.0 * Y
386+
vpsum2 = VectorPauliSum(3, [7], [0.25]) # 0.25 * ZX
387+
commutator(vpsum, vpsum2) # should return a VectorPauliSum with - 0.5 YX + 0.5 XX
388+
```
389+
"""
390+
function PauliPropagation.commutator(vpsum1::VectorPauliSum, vpsum2::VectorPauliSum)
391+
nq = _checknumberofqubits(vpsum1, vpsum2)
392+
393+
TT = paulitype(vpsum1)
394+
CT = promote_type(ComplexF64, coefftype(vpsum1), coefftype(vpsum2))
395+
new_pauli_dict = Dict{TT,CT}()
396+
397+
for (pauli1, coeff1) in zip(vpsum1.terms, vpsum1.coeffs), (pauli2, coeff2) in zip(vpsum2.terms, vpsum2.coeffs)
398+
if !commutes(pauli1, pauli2)
399+
new_pstr, sign = commutator(pauli1, pauli2)
400+
new_pauli_dict[new_pstr] = get(new_pauli_dict, new_pstr, zero(CT)) + convert(CT, sign * coeff1 * coeff2)
401+
end
402+
end
403+
404+
return VectorPauliSum(nq, collect(keys(new_pauli_dict)), collect(values(new_pauli_dict)))
405+
end
406+
376407
"""
377408
commutator(pstr1::Integer, pstr2::Integer)
378409

test/test_datatypes.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,9 @@ end
103103
@test vpsum == psum # checks Pauli by Pauli
104104
@test vpsum_from_str == psum
105105
@test vpsum_from_str == vpsum
106-
end
107106

108107
# Subtest for subtracting PauliSum
109-
@testset "VectorPauliSum to PauliSums with Merging" begin
108+
# VectorPauliSum to PauliSums with Merging
110109

111110
pstr2 = createpaulistring(nq)
112111
vpsum = VectorPauliSum([pstr, pstr2, pstr])

test/test_paulioperations.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,26 @@ using Test
135135
pstr2 = PauliString(nq, [:Y, :I], [1, 2], 1.0)
136136
@test commutator(pstr1, pstr2) == PauliString(nq, [:Z, :Y], [1, 2], 2im)
137137
end
138+
139+
@testset "VectorPauliSum commutator" begin
140+
nq = 3
141+
# VectorPauliSum commutator verify with PauliSum commutator
142+
psum1 = PauliSum(nq)
143+
add!(psum1, [:X, :Y], [1, 2], 1.5)
144+
psum2 = PauliSum(nq)
145+
add!(psum2, [:Y, :I], [1, 2], -2.0)
146+
147+
expected = commutator(psum1, psum2)
148+
com_vec = commutator(VectorPauliSum(psum1), VectorPauliSum(psum2))
149+
@test PauliSum(com_vec) == expected
150+
151+
vpsum = VectorPauliSum(3, [1, 2], [1., 1.])
152+
vpsum2 = VectorPauliSum(3, [7], [0.25])
153+
expected = commutator(PauliSum(vpsum), PauliSum(vpsum2))
154+
com_vec = commutator(vpsum, vpsum2)
155+
@test PauliSum(com_vec) == expected
156+
157+
end
138158
end
139159

140160

0 commit comments

Comments
 (0)