Skip to content

Commit

Permalink
Address #22 (#66)
Browse files Browse the repository at this point in the history
* single qubit rules

* spell check fix

* update changelog and project.tom

* codecov
  • Loading branch information
apkille authored Jul 9, 2024
1 parent 369a30e commit 93ce6e9
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# News

## v0.3.3 - dev

- Add single qubit simplification rules.

## v0.3.2 - 2024-07-02

- Added documentation for `express`.
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "QuantumSymbolics"
uuid = "efa7fd63-0460-4890-beb7-be1bbdfbaeae"
authors = ["QuantumSymbolics.jl contributors"]
version = "0.3.2"
version = "0.3.3-dev"

[deps]
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
Expand Down
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,4 @@ express(MixedState(X1)/2+SProjector(Z1)/2, CliffordRepr())

!!! warning "Stabilizer state expressions"

The state written as $\frac{|Z₁⟩⊗|Z₁⟩+|Z₂⟩⊗|Z₂⟩}{√2}$ is a well known stabilizer state, namely a Bell state. However, automatically expressing it as a stabilizer is a prohibitively expensive computational operation in general. We do not perform that computation automatically. If you want to ensure that states you define can be automatically converted to tableaux for Clifford simulations, avoid using sumation of kets. On the other hand, in all of our Clifford Monte-Carlo simulations, `⊗` is fully supported, as well as [`SProjector`](@ref), [`MixedState`](@ref), [`StabilizerState`](@ref), and sumation of density matrices.
The state written as $\frac{|Z₁⟩⊗|Z₁⟩+|Z₂⟩⊗|Z₂⟩}{√2}$ is a well known stabilizer state, namely a Bell state. However, automatically expressing it as a stabilizer is a prohibitively expensive computational operation in general. We do not perform that computation automatically. If you want to ensure that states you define can be automatically converted to tableaux for Clifford simulations, avoid using summation of kets. On the other hand, in all of our Clifford Monte-Carlo simulations, `⊗` is fully supported, as well as [`SProjector`](@ref), [`MixedState`](@ref), [`StabilizerState`](@ref), and summation of density matrices.
34 changes: 33 additions & 1 deletion src/QSymbolicsBase/rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function hasscalings(xs)
end
end
_isa(T) = x->isa(x,T)
_isequal(obj) = x->(x==obj)
_vecisa(T) = x->all(_isa(T), x)

##
Expand All @@ -30,7 +31,38 @@ RULES_PAULI = [
@rule(~o1::_isa(XGate)*~o2::_isa(ZGate) => -im*Y),
@rule(~o1::_isa(HGate)*~o2::_isa(XGate)*~o3::_isa(HGate) => Z),
@rule(~o1::_isa(HGate)*~o2::_isa(YGate)*~o3::_isa(HGate) => -Y),
@rule(~o1::_isa(HGate)*~o2::_isa(ZGate)*~o3::_isa(HGate) => X)
@rule(~o1::_isa(HGate)*~o2::_isa(ZGate)*~o3::_isa(HGate) => X),

@rule(~o::_isa(XGate)*~k::_isequal(X1) => X1),
@rule(~o::_isa(YGate)*~k::_isequal(X1) => -im*X2),
@rule(~o::_isa(ZGate)*~k::_isequal(X1) => X2),

@rule(~o::_isa(XGate)*~k::_isequal(X2) => -X2),
@rule(~o::_isa(YGate)*~k::_isequal(X2) => im*X1),
@rule(~o::_isa(ZGate)*~k::_isequal(X2) => X1),

@rule(~o::_isa(XGate)*~k::_isequal(Y1) => im*Y2),
@rule(~o::_isa(YGate)*~k::_isequal(Y1) => Y1),
@rule(~o::_isa(ZGate)*~k::_isequal(Y1) => Y2),

@rule(~o::_isa(XGate)*~k::_isequal(Y2) => -im*Y1),
@rule(~o::_isa(YGate)*~k::_isequal(Y2) => -Y2),
@rule(~o::_isa(ZGate)*~k::_isequal(Y2) => Y1),

@rule(~o::_isa(XGate)*~k::_isequal(Z1) => Z2),
@rule(~o::_isa(YGate)*~k::_isequal(Z1) => im*Z2),
@rule(~o::_isa(ZGate)*~k::_isequal(Z1) => Z1),

@rule(~o::_isa(XGate)*~k::_isequal(Z2) => Z1),
@rule(~o::_isa(YGate)*~k::_isequal(Z2) => -im*Z1),
@rule(~o::_isa(ZGate)*~k::_isequal(Z2) => -Z2),

@rule(~o::_isa(HGate)*~k::_isequal(X1) => Z1),
@rule(~o::_isa(HGate)*~k::_isequal(X2) => Z2),
@rule(~o::_isa(HGate)*~k::_isequal(Y1) => (X1+im*X2)/sqrt(2)),
@rule(~o::_isa(HGate)*~k::_isequal(Y2) => (X1-im*X2)/sqrt(2)),
@rule(~o::_isa(HGate)*~k::_isequal(Z1) => X1),
@rule(~o::_isa(HGate)*~k::_isequal(Z2) => X2)
]

# Commutator identities
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ println("Starting tests with $(Threads.nthreads()) threads out of `Sys.CPU_THREA
@doset "dagger"
@doset "zero_obj"
@doset "expand"
@doset "pauli"

VERSION >= v"1.9" && @doset "doctests"
get(ENV,"JET_TEST","")=="true" && @doset "jet"
Expand Down
4 changes: 4 additions & 0 deletions test/test_expand.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ using Test

@op A; @op B; @op C; @op D;

@testset "expand errors" begin
@test_throws ErrorException qexpand(X)
end

@testset "expand rules" begin
@test isequal(qexpand(commutator(A, B)), A*B - B*A)
@test isequal(qexpand(anticommutator(A, B)), A*B + B*A)
Expand Down
50 changes: 50 additions & 0 deletions test/test_pauli.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using QuantumSymbolics
using Test

@testset "simplify errors" begin
@test_throws ErrorException qsimplify(X)
end

@testset "MulOperator tests" begin
@test isequal(qsimplify(X*X,rewriter=qsimplify_pauli), I)
@test isequal(qsimplify(Y*Y,rewriter=qsimplify_pauli), I)
@test isequal(qsimplify(Z*Z,rewriter=qsimplify_pauli), I)
@test isequal(qsimplify(X*Y,rewriter=qsimplify_pauli), im*Z)
@test isequal(qsimplify(Y*Z,rewriter=qsimplify_pauli), im*X)
@test isequal(qsimplify(Z*X,rewriter=qsimplify_pauli), im*Y)
@test isequal(qsimplify(Y*X,rewriter=qsimplify_pauli), -im*Z)
@test isequal(qsimplify(Z*Y,rewriter=qsimplify_pauli), -im*X)
@test isequal(qsimplify(X*Z,rewriter=qsimplify_pauli), -im*Y)
@test isequal(qsimplify(H*X*H,rewriter=qsimplify_pauli), Z)
@test isequal(qsimplify(H*Y*H,rewriter=qsimplify_pauli), -Y)
@test isequal(qsimplify(H*Z*H,rewriter=qsimplify_pauli), X)
end

@testset "ApplyKet tests" begin
@test isequal(qsimplify(X*X1,rewriter=qsimplify_pauli), X1)
@test isequal(qsimplify(Y*X1,rewriter=qsimplify_pauli), -im*X2)
@test isequal(qsimplify(Z*X1,rewriter=qsimplify_pauli), X2)

@test isequal(qsimplify(X*X2,rewriter=qsimplify_pauli), -X2)
@test isequal(qsimplify(Y*X2,rewriter=qsimplify_pauli), im*X1)
@test isequal(qsimplify(Z*X2,rewriter=qsimplify_pauli), X1)

@test isequal(qsimplify(X*Y1,rewriter=qsimplify_pauli), im*Y2)
@test isequal(qsimplify(Y*Y1,rewriter=qsimplify_pauli), Y1)
@test isequal(qsimplify(Z*Y1,rewriter=qsimplify_pauli), Y2)

@test isequal(qsimplify(X*Z1,rewriter=qsimplify_pauli), Z2)
@test isequal(qsimplify(Y*Z1,rewriter=qsimplify_pauli), im*Z2)
@test isequal(qsimplify(Z*Z1,rewriter=qsimplify_pauli), Z1)

@test isequal(qsimplify(X*Z2,rewriter=qsimplify_pauli), Z1)
@test isequal(qsimplify(Y*Z2,rewriter=qsimplify_pauli), -im*Z1)
@test isequal(qsimplify(Z*Z2,rewriter=qsimplify_pauli), -Z2)

@test isequal(qsimplify(H*X1,rewriter=qsimplify_pauli), Z1)
@test isequal(qsimplify(H*X2,rewriter=qsimplify_pauli), Z2)
@test isequal(qsimplify(H*Y1,rewriter=qsimplify_pauli), (X1+im*X2)/sqrt(2))
@test isequal(qsimplify(H*Y2,rewriter=qsimplify_pauli), (X1-im*X2)/sqrt(2))
@test isequal(qsimplify(H*Z1,rewriter=qsimplify_pauli), X1)
@test isequal(qsimplify(H*Z2,rewriter=qsimplify_pauli), X2)
end

0 comments on commit 93ce6e9

Please sign in to comment.