Skip to content

Rotation Gates #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/QSymbolicsBase/QSymbolicsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export SymQObj,QObj,
tensor,⊗,
dagger,projector,commutator,anticommutator,conj,transpose,inv,exp,vec,tr,ptrace,
I,X,Y,Z,σˣ,σʸ,σᶻ,Pm,Pp,σ₋,σ₊,
H,Rx,Ry,Rz,CNOT,CPHASE,XCX,XCY,XCZ,YCX,YCY,YCZ,ZCX,ZCY,ZCZ,
H,RotX,RotY,RotZ,CNOT,CPHASE,XCX,XCY,XCZ,YCX,YCY,YCZ,ZCX,ZCY,ZCZ,
X1,X2,Y1,Y2,Z1,Z2,X₁,X₂,Y₁,Y₂,Z₁,Z₂,L0,L1,Lp,Lm,Lpi,Lmi,L₀,L₁,L₊,L₋,L₊ᵢ,L₋ᵢ,
vac,F₀,F0,F₁,F1,inf_fock_basis,
N,n̂,Create,âꜛ,Destroy,â,basis,SpinBasis,FockBasis,
Expand All @@ -44,7 +44,7 @@ export SymQObj,QObj,
XBasisState,YBasisState,ZBasisState,FockState,CoherentState,SqueezedState,
NumberOp,CreateOp,DestroyOp,PhaseShiftOp,DisplaceOp,SqueezeOp,
XCXGate,XCYGate,XCZGate,YCXGate,YCYGate,YCZGate,ZCXGate,ZCYGate,ZCZGate,
qsimplify,qsimplify_pauli,qsimplify_commutator,qsimplify_anticommutator,qsimplify_fock,
qsimplify,qsimplify_pauli,qsimplify_commutator,qsimplify_anticommutator,qsimplify_fock,qsimplify_rot,
qexpand,
isunitary,
KrausRepr,kraus
Expand Down
18 changes: 17 additions & 1 deletion src/QSymbolicsBase/rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,22 @@ RULES_FOCK = [
@rule(~o::_isa(SqueezeOp) * ~k::_isequal(vac) => SqueezedState((~o).z, (~o).basis))
]

RULES_SIMPLIFY = [RULES_PAULI; RULES_COMMUTATOR; RULES_ANTICOMMUTATOR; RULES_FOCK]
RULES_ROT = [
@rule(~r::_isa(RotX) => I where (~r).θ == 0),
@rule(~r::_isa(RotY) => I where (~r).θ == 0),
@rule(~r::_isa(RotZ) => I where (~r).θ == 0),
@rule(~r1::_isa(RotX) * ~r2::_isa(RotX) => try RotX((~r1).θ + (~r2).θ) catch end),
@rule(~r1::_isa(RotY) * ~r2::_isa(RotY) => try RotY((~r1).θ + (~r2).θ) catch end),
@rule(~r1::_isa(RotZ) * ~r2::_isa(RotZ) => try RotZ((~r1).θ + (~r2).θ) catch end),
@rule(~r::_isa(RotX) => try RotX(mod((~r).θ, 2π)) catch end),
@rule(~r::_isa(RotY) => try RotY(mod((~r).θ, 2π)) catch end),
@rule(~r::_isa(RotZ) => try RotZ(mod((~r).θ, 2π)) catch end),
@rule(exp(~α * ~x::_isa(XGate)) => try real(~α) == 0 ? RotX(-2imag(~α)) : nothing catch end),
@rule(exp(~α * ~x::_isa(YGate)) => try real(~α) == 0 ? RotY(-2imag(~α)) : nothing catch end),
@rule(exp(~α * ~x::_isa(ZGate)) => try real(~α) == 0 ? RotZ(-2imag(~α)) : nothing catch end)
]

RULES_SIMPLIFY = [RULES_PAULI; RULES_COMMUTATOR; RULES_ANTICOMMUTATOR; RULES_FOCK; RULES_ROT]

##
# Simplification rewriters
Expand All @@ -119,6 +134,7 @@ qsimplify_pauli = Chain(RULES_PAULI)
qsimplify_commutator = Chain(RULES_COMMUTATOR)
qsimplify_anticommutator = Chain(RULES_ANTICOMMUTATOR)
qsimplify_fock = Chain(RULES_FOCK)
qsimplify_rot = Chain(RULES_ROT)

"""
qsimplify(s; rewriter=nothing)
Expand Down
27 changes: 27 additions & 0 deletions test/test_rotation.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@testitem "Test Rotation" begin
@testset "Identity tests" begin
@test isequal(qsimplify(RotX(0), rewriter=qsimplify_rot), I)
end

@testset "Fusion tests" begin
@test isequal(qsimplify(RotX(π/2) * RotX(π/2), rewriter=qsimplify_rot), RotX(1π))
@test isequal(qsimplify(RotY(π/2) * RotY(π/2), rewriter=qsimplify_rot), RotY(1π))
@test isequal(qsimplify(RotZ(π/2) * RotZ(π/2), rewriter=qsimplify_rot), RotZ(1π))

@test isequal(qsimplify(RotX(π/2) * RotX(-π/2), rewriter=qsimplify_rot), I)
@test isequal(qsimplify(RotY(π/2) * RotY(-π/2), rewriter=qsimplify_rot), I)
@test isequal(qsimplify(RotZ(π/2) * RotZ(-π/2), rewriter=qsimplify_rot), I)

@test isequal(qsimplify(2 * RotX(π) * RotX(π), rewriter=qsimplify_rot), 2RotX(2π))
@test isequal(qsimplify(2 * RotY(π) * RotY(π), rewriter=qsimplify_rot), 2RotY(2π))
@test isequal(qsimplify(2 * RotZ(π) * RotZ(π), rewriter=qsimplify_rot), 2RotZ(2π))
end

@testset "Exponential tests" begin
@test isequal(qsimplify(exp(-im * π/2 * X), rewriter=qsimplify_rot), RotX(π))
@test isequal(qsimplify(exp(-im * π/2 * Y), rewriter=qsimplify_rot), RotY(π))
@test isequal(qsimplify(exp(-im * π/2 * Z), rewriter=qsimplify_rot), RotZ(π))
end

@testset "modulo tests"
end
Loading