Skip to content

Commit

Permalink
Merge branch 'main' into channels
Browse files Browse the repository at this point in the history
  • Loading branch information
apkille committed Jul 19, 2024
2 parents bcddbc5 + c9d8eed commit 77f737d
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# News

## v0.3.3 - 2024-07-12

- Added single qubit simplification rules.
- Removed evaluation of metadata equality in `Base.isequal`.

## 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"

[deps]
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
Expand Down
2 changes: 1 addition & 1 deletion src/QSymbolicsBase/QSymbolicsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Base.isequal(::Symbolic{Complex}, ::SymQObj) = false

# TODO check that this does not cause incredibly bad runtime performance
# use a macro to provide specializations if that is indeed the case
propsequal(x,y) = all(n->isequal(getproperty(x,n),getproperty(y,n)), propertynames(x))
propsequal(x,y) = all(n->(n==:metadata || isequal(getproperty(x,n),getproperty(y,n))), propertynames(x))


##
Expand Down
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 @@ -38,6 +38,7 @@ println("Starting tests with $(Threads.nthreads()) threads out of `Sys.CPU_THREA
@doset "trace"
@doset "expand"
@doset "throws"
@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
7 changes: 7 additions & 0 deletions test/test_sym_expressions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ using QuantumSymbolics
@test isequal(Z1 - Z2, Z1 + (-Z2))
@test_broken isequal(Z1 - 2*Z2 + 2*X1, -2*Z2 + Z1 + 2*X1)
@test_broken isequal(Z1 - 2*Z2 + 2*X1, Z1 + 2*(-Z2+X1))

state1 = XBasisState(1, SpinBasis(1//2))
state2 = XBasisState(1, SpinBasis(1//2))
state3 = XBasisState(2, SpinBasis(1//2))

@test isequal(state1, state2)
@test !isequal(state1, state3)

0 comments on commit 77f737d

Please sign in to comment.