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 12 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
8 changes: 7 additions & 1 deletion ext/QuantumOpticsExt/QuantumOpticsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using QuantumInterface: samebases
using QuantumSymbolics
using QuantumSymbolics:
HGate, XGate, YGate, ZGate, CPHASEGate, CNOTGate, PauliP, PauliM,
HGate, XGate, YGate, ZGate, RotXGate, RotYGate, RotZGate, CPHASEGate, CNOTGate, PauliP, PauliM,
XCXGate, XCYGate, XCZGate, YCXGate, YCYGate, YCZGate, ZCXGate, ZCYGate, ZCZGate,
XBasisState, YBasisState, ZBasisState,
NumberOp, CreateOp, DestroyOp,
Expand All @@ -31,6 +31,9 @@
const _x = sigmax(_b2)
const _y = sigmay(_b2)
const _hadamard = (sigmaz(_b2)+sigmax(_b2))/√2
const _rotx(θ) = cos(θ/2)*_id - im*sin(θ/2)*_x
const _roty(θ) = cos(θ/2)*_id - im*sin(θ/2)*_y
const _rotz(θ) = cos(θ/2)*_id - im*sin(θ/2)*_z

Check warning on line 36 in ext/QuantumOpticsExt/QuantumOpticsExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/QuantumOpticsExt/QuantumOpticsExt.jl#L34-L36

Added lines #L34 - L36 were not covered by tests
const _cnot = _l00⊗_id + _l11⊗_x
const _cphase = _l00⊗_id + _l11⊗_z
const _phase = _l00 + im*_l11
Expand All @@ -47,6 +50,9 @@
express_nolookup(::XGate, ::QuantumOpticsRepr) = _x
express_nolookup(::YGate, ::QuantumOpticsRepr) = _y
express_nolookup(::ZGate, ::QuantumOpticsRepr) = _z
express_nolookup(g::RotXGate, ::QuantumOpticsRepr) = _rotx(g.θ)
express_nolookup(g::RotYGate, ::QuantumOpticsRepr) = _roty(g.θ)
express_nolookup(g::RotZGate, ::QuantumOpticsRepr) = _rotz(g.θ)

Check warning on line 55 in ext/QuantumOpticsExt/QuantumOpticsExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/QuantumOpticsExt/QuantumOpticsExt.jl#L53-L55

Added lines #L53 - L55 were not covered by tests
express_nolookup(::CPHASEGate, ::QuantumOpticsRepr) = _cphase
express_nolookup(::CNOTGate, ::QuantumOpticsRepr) = _cnot

Expand Down
6 changes: 3 additions & 3 deletions src/QSymbolicsBase/QSymbolicsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export SymQObj,QObj,
tensor,⊗,
dagger,projector,commutator,anticommutator,conj,transpose,inv,exp,vec,tr,ptrace,
I,X,Y,Z,σˣ,σʸ,σᶻ,Pm,Pp,σ₋,σ₊,
H,CNOT,CPHASE,XCX,XCY,XCZ,YCX,YCY,YCZ,ZCX,ZCY,ZCZ,
H,Rx,Ry,Rz,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 @@ -42,11 +42,11 @@ export SymQObj,QObj,
SConjugate,STranspose,SProjector,SDagger,SInvOperator,SExpOperator,SVec,STrace,SPartialTrace,
MixedState,IdentityOp,
SApplyKet,SApplyBra,SMulOperator,SSuperOpApply,SCommutator,SAnticommutator,SBraKet,SOuterKetBra,
HGate,XGate,YGate,ZGate,CPHASEGate,CNOTGate,
HGate,XGate,YGate,ZGate,RotXGate,RotYGate,RotZGate,CPHASEGate,CNOTGate,
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
37 changes: 37 additions & 0 deletions src/QSymbolicsBase/predefined.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,37 @@
ishermitian(::HGate) = true
isunitary(::HGate) = true

abstract type AbstractRotGate <: AbstractSingleQubitGate end
ishermitian(::AbstractRotGate) = false
isunitary(::AbstractRotGate) = true

Check warning on line 111 in src/QSymbolicsBase/predefined.jl

View check run for this annotation

Codecov / codecov/patch

src/QSymbolicsBase/predefined.jl#L110-L111

Added lines #L110 - L111 were not covered by tests
isexpr(::AbstractRotGate) = true
iscall(::AbstractRotGate) = true
arguments(x::AbstractRotGate) = [x.θ]

@withmetadata struct RotXGate <: AbstractRotGate
θ
end
operation(::RotXGate) = Rx
head(::RotXGate) = :RotX
children(x::RotXGate) = [:RotX, x.θ]
symbollabel(x::RotXGate) = "Rx($(x.θ))"

Check warning on line 122 in src/QSymbolicsBase/predefined.jl

View check run for this annotation

Codecov / codecov/patch

src/QSymbolicsBase/predefined.jl#L120-L122

Added lines #L120 - L122 were not covered by tests

@withmetadata struct RotYGate <: AbstractRotGate
θ
end
operation(::RotYGate) = Ry
head(::RotYGate) = :RotY
children(x::RotYGate) = [:RotY, x.θ]
symbollabel(x::RotYGate) = "Ry($(x.θ))"

Check warning on line 130 in src/QSymbolicsBase/predefined.jl

View check run for this annotation

Codecov / codecov/patch

src/QSymbolicsBase/predefined.jl#L128-L130

Added lines #L128 - L130 were not covered by tests

@withmetadata struct RotZGate <: AbstractRotGate
θ
end
operation(::RotZGate) = Rz
head(::RotZGate) = :RotZ
children(x::RotZGate) = [:RotZ, x.θ]
symbollabel(x::RotZGate) = "Rz($(x.θ))"

Check warning on line 138 in src/QSymbolicsBase/predefined.jl

View check run for this annotation

Codecov / codecov/patch

src/QSymbolicsBase/predefined.jl#L136-L138

Added lines #L136 - L138 were not covered by tests

@withmetadata struct CNOTGate <: AbstractTwoQubitGate end
symbollabel(::CNOTGate) = "CNOT"
ishermitian(::CNOTGate) = true
Expand Down Expand Up @@ -143,6 +174,12 @@
const Pp = const σ₊ = PauliP()
"""Hadamard gate"""
const H = HGate()
"""Rotation around the X axis"""
const Rx = RotXGate
"""Rotation around the Y axis"""
const Ry = RotYGate
"""Rotation around the Z axis"""
const Rz = RotZGate
"""CNOT gate"""
const CNOT = CNOTGate()
"""CPHASE gate"""
Expand Down
24 changes: 23 additions & 1 deletion src/QSymbolicsBase/rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,28 @@ 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(RotXGate) => I where (~r).θ == 0),
@rule(~r::_isa(RotYGate) => I where (~r).θ == 0),
@rule(~r::_isa(RotZGate) => I where (~r).θ == 0),
@rule(~r::_isa(RotXGate) => (-im*X) where (~r).θ in [π, 1π]),
@rule(~r::_isa(RotYGate) => (-im*Y) where (~r).θ in [π, 1π]),
@rule(~r::_isa(RotZGate) => (-im*Z) where (~r).θ in [π, 1π]),
@rule(~r::_isa(RotXGate) => try Rx(mod((~r).θ, 4π)) catch end),
@rule(~r::_isa(RotYGate) => try Ry(mod((~r).θ, 4π)) catch end),
@rule(~r::_isa(RotZGate) => try Rz(mod((~r).θ, 4π)) catch end),
@rule(~r::_isa(RotXGate) => try (~r).θ ≥ 2π ? -Rx((~r).θ - 2π) : nothing catch end),
@rule(~r::_isa(RotYGate) => try (~r).θ ≥ 2π ? -Ry((~r).θ - 2π) : nothing catch end),
@rule(~r::_isa(RotZGate) => try (~r).θ ≥ 2π ? -Rz((~r).θ - 2π) : nothing catch end),
@rule(~r1::_isa(RotXGate) * ~r2::_isa(RotXGate) => try Rx((~r1).θ + (~r2).θ) catch end),
@rule(~r1::_isa(RotYGate) * ~r2::_isa(RotYGate) => try Ry((~r1).θ + (~r2).θ) catch end),
@rule(~r1::_isa(RotZGate) * ~r2::_isa(RotZGate) => try Rz((~r1).θ + (~r2).θ) catch end),
@rule(exp(~α * ~x::_isa(XGate)) => try if real(~α) == 0 Rx(-2imag(~α)) end catch end),
@rule(exp(~α * ~x::_isa(YGate)) => try if real(~α) == 0 Ry(-2imag(~α)) end catch end),
@rule(exp(~α * ~x::_isa(ZGate)) => try if real(~α) == 0 Rz(-2imag(~α)) end catch end)
]

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

##
# Simplification rewriters
Expand All @@ -119,6 +140,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
67 changes: 67 additions & 0 deletions test/test_rotation.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
@testitem "Test Ration" begin
@testset "Identity tests" begin
@test isequal(qsimplify(Rx(0), rewriter=qsimplify_rot), I)
@test isequal(qsimplify(Ry(0), rewriter=qsimplify_rot), I)
@test isequal(qsimplify(Rz(0), rewriter=qsimplify_rot), I)
end

@testset "Pauli tests" begin
@test isequal(qsimplify(Rx(π), rewriter=qsimplify_rot), -im*X)
@test isequal(qsimplify(Ry(π), rewriter=qsimplify_rot), -im*Y)
@test isequal(qsimplify(Rz(π), rewriter=qsimplify_rot), -im*Z)

@test isequal(qsimplify(Rx(1π), rewriter=qsimplify_rot), -im*X)
@test isequal(qsimplify(Ry(1π), rewriter=qsimplify_rot), -im*Y)
@test isequal(qsimplify(Rz(1π), rewriter=qsimplify_rot), -im*Z)
end

@testset "Fusion tests" begin
@test isequal(qsimplify(Rx(π/3) * Rx(π/3), rewriter=qsimplify_rot), Rx(2π/3))
@test isequal(qsimplify(Ry(π/3) * Ry(π/3), rewriter=qsimplify_rot), Ry(2π/3))
@test isequal(qsimplify(Rz(π/3) * Rz(π/3), rewriter=qsimplify_rot), Rz(2π/3))

@test isequal(qsimplify(Rx(π/2) * Rx(-π/2), rewriter=qsimplify_rot), I)
@test isequal(qsimplify(Ry(π/2) * Ry(-π/2), rewriter=qsimplify_rot), I)
@test isequal(qsimplify(Rz(π/2) * Rz(-π/2), rewriter=qsimplify_rot), I)

@test isequal(qsimplify(2 * Rx(π/2) * Rx(π/2), rewriter=qsimplify_rot), -2im*X)
@test isequal(qsimplify(2 * Ry(π/2) * Ry(π/2), rewriter=qsimplify_rot), -2im*Y)
@test isequal(qsimplify(2 * Rz(π/2) * Rz(π/2), rewriter=qsimplify_rot), -2im*Z)
end

@testset "Modulo tests" begin
@test isequal(qsimplify(Rx(2π), rewriter=qsimplify_rot), -I)
@test isequal(qsimplify(Ry(2π), rewriter=qsimplify_rot), -I)
@test isequal(qsimplify(Rz(2π), rewriter=qsimplify_rot), -I)

@test isequal(qsimplify(Rx(3π), rewriter=qsimplify_rot), im*X)
@test isequal(qsimplify(Ry(3π), rewriter=qsimplify_rot), im*Y)
@test isequal(qsimplify(Rz(3π), rewriter=qsimplify_rot), im*Z)

@test isequal(qsimplify(Rx(4π), rewriter=qsimplify_rot), I)
@test isequal(qsimplify(Ry(4π), rewriter=qsimplify_rot), I)
@test isequal(qsimplify(Rz(4π), rewriter=qsimplify_rot), I)

@test isequal(qsimplify(Rx(5π), rewriter=qsimplify_rot), -im*X)
@test isequal(qsimplify(Ry(5π), rewriter=qsimplify_rot), -im*Y)
@test isequal(qsimplify(Rz(5π), rewriter=qsimplify_rot), -im*Z)

@test isequal(qsimplify(Rx(π) * Rx(π), rewriter=qsimplify_rot), -I)
@test isequal(qsimplify(Ry(π) * Ry(π), rewriter=qsimplify_rot), -I)
@test isequal(qsimplify(Rz(π) * Rz(π), rewriter=qsimplify_rot), -I)

@test isequal(qsimplify(2 * Rx(3π/2) * Rx(2π/2), rewriter=qsimplify_rot), -2Rx(π/2))
@test isequal(qsimplify(2 * Ry(3π/2) * Ry(2π/2), rewriter=qsimplify_rot), -2Ry(π/2))
@test isequal(qsimplify(2 * Rz(3π/2) * Rz(2π/2), rewriter=qsimplify_rot), -2Rz(π/2))
end

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

@test isequal(qsimplify(2 * exp(-im * 2π/2 * X) * exp(-im * 5π/2 * X), rewriter=qsimplify_rot), 2im*X)
@test isequal(qsimplify(2 * exp(-im * 2π/2 * Y) * exp(-im * 5π/2 * Y), rewriter=qsimplify_rot), 2im*Y)
@test isequal(qsimplify(2 * exp(-im * 2π/2 * Z) * exp(-im * 5π/2 * Z), rewriter=qsimplify_rot), 2im*Z)
end
end
Loading