From 410abf193e66e61bc8509d0b90fdf0ab600582f8 Mon Sep 17 00:00:00 2001 From: Roger-Luo Date: Wed, 16 Jun 2021 17:54:20 -0400 Subject: [PATCH] allow export all intrinsic operators --- src/intrinsic.jl | 14 +++++++++++++- src/types.jl | 12 ++++++++++++ test/runtests.jl | 25 ++++++++++++++++--------- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/intrinsic.jl b/src/intrinsic.jl index 4ae3687..36ca20d 100644 --- a/src/intrinsic.jl +++ b/src/intrinsic.jl @@ -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") @@ -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 @@ -63,3 +73,5 @@ end @intrinsic Ry(θ::T) where {T} @intrinsic Rz(θ::T) where {T} @intrinsic UGate(α::T, β::T, γ::T) where {T} + +end diff --git a/src/types.jl b/src/types.jl index 1811ca5..1d8d83d 100644 --- a/src/types.jl +++ b/src/types.jl @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 1dd753f..a1c7e2b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,6 +1,7 @@ using YaoLocations using MLStyle using YaoHIR +using YaoHIR.IntrinsicOperation using Test module TestIntrinsic @@ -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