Skip to content

Commit

Permalink
allow export all intrinsic operators
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger-luo committed Jun 16, 2021
1 parent 6ad538e commit 410abf1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
14 changes: 13 additions & 1 deletion src/intrinsic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ function intrinsic_m(ex, n::Int=1)
if ex isa Symbol
def = JLStruct(;name=Symbol(ex, :Gate), supertype=IntrinsicRoutine)
binding_name = ex
binding = :(Core.@__doc__ const $ex = $(def.name)())
binding = quote
export $ex
Core.@__doc__ const $ex = $(def.name)()
end
else
name, args, kw, whereparams, rettype = split_function_head(ex)
kw === nothing || error("cannot have kwargs in intrinsic operation")
Expand Down Expand Up @@ -51,6 +54,13 @@ function codegen_helpers(def::JLStruct, n::Int, name::Symbol)
end
end

"""
Intrinsic Quantum Operations
"""
module IntrinsicOperation

using ..YaoHIR: @intrinsic

@intrinsic X
@intrinsic Y
@intrinsic Z
Expand All @@ -63,3 +73,5 @@ end
@intrinsic Ry::T) where {T}
@intrinsic Rz::T) where {T}
@intrinsic UGate::T, β::T, γ::T) where {T}

end
12 changes: 12 additions & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,15 @@ function leaves(root::Chain)
end
return nodes
end

function Base.:(==)(lhs::Chain, rhs::Chain)
mapreduce(==, &, lhs.args, rhs.args)
end

function Base.:(==)(lhs::Gate, rhs::Gate)
lhs.operation == rhs.operation && lhs.locations == rhs.locations
end

function Base.:(==)(lhs::Ctrl, rhs::Ctrl)
lhs.gate == rhs.gate && lhs.ctrl == rhs.ctrl
end
25 changes: 16 additions & 9 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using YaoLocations
using MLStyle
using YaoHIR
using YaoHIR.IntrinsicOperation
using Test

module TestIntrinsic
Expand All @@ -16,41 +17,47 @@ end
@test YaoHIR.routine_name(TestIntrinsic.R(1.0)) == :R
@test TestIntrinsic.R(1.0).theta == 1.0

display(YaoHIR.X)
display(YaoHIR.Rx(1.0))
display(YaoHIR.Operation(YaoHIR.X, 2.0))
display(X)
display(Rx(1.0))
display(YaoHIR.Operation(X, 2.0))

circ = Chain(
Gate(YaoHIR.X, Locations(1)),
Gate(X, Locations(1)),
Core.SSAValue(1),
Ctrl(Gate(Core.SSAValue(1), Locations(3)), CtrlLocations(2))
)

print(circ)

@test YaoHIR.leaves(circ) == [Gate(YaoHIR.X, Locations(1)),
@test YaoHIR.leaves(circ) == [Gate(X, Locations(1)),
Core.SSAValue(1),
Ctrl(Gate(Core.SSAValue(1), Locations(3)), CtrlLocations(2))
]


@testset "test match" begin
gate = Gate(YaoHIR.X, Locations(2))
gate = Gate(X, Locations(2))

@match gate begin
Gate(op, locs) => begin
@test op == YaoHIR.X
@test op == X
@test locs == Locations(2)
end
end

ctrl = Ctrl(Gate(YaoHIR.X, Locations(2)), CtrlLocations(3))
ctrl = Ctrl(Gate(X, Locations(2)), CtrlLocations(3))

@match ctrl begin
Ctrl(Gate(op, locs), ctrl) => begin
@test op == YaoHIR.X
@test op == X
@test locs == Locations(2)
@test ctrl == CtrlLocations(3)
end
end
end

@testset "isequal" begin
circuit1 = Chain(Gate(H, Locations((1, ))), Gate(H, Locations((1, ))))
circuit2 = Chain(Gate(H, Locations((1, ))), Gate(H, Locations((1, ))))
@test circuit1 == circuit2
end

0 comments on commit 410abf1

Please sign in to comment.