YaoCompiler.jl icon indicating copy to clipboard operation
YaoCompiler.jl copied to clipboard

Still got an error in @code_qasm

Open ChenZhao44 opened this issue 4 years ago • 3 comments

I supposed that the CNOT gate should be available without including qelib1.inc. But I still got an error when using @code_qasm, even if I include qelib1.inc.

julia> using YaoCompiler

julia> qasm"""
       OPENQASM 2.0;
       include "test/compiler/qelib1.inc";
       """
cu3 (generic routine with 1 methods)

julia> @device function adder3()
           @ctrl 1 4=>X
           @ctrl 2 4=>X
           @ctrl 3 4=>X
       end
adder3 (generic routine with 1 methods)

julia> @code_qasm adder3()
ERROR: invalid control statement for QASM backend, got: YaoCompiler.Semantic.ctrl(Main.X, %3, %4)
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] codegen_ctrl(#unused#::YaoCompiler.TargetQASMTopLevel, ci::Core.CodeInfo, st::YaoCompiler.QASMCodeGenState)
   @ YaoCompiler ~/.julia/dev/YaoCompiler/src/compiler/codegen/qasm.jl:533
 [3] codegen_stmt(target::YaoCompiler.TargetQASMTopLevel, ci::Core.CodeInfo, st::YaoCompiler.QASMCodeGenState)
   @ YaoCompiler ~/.julia/dev/YaoCompiler/src/compiler/codegen/qasm.jl:333
 [4] codegen(target::YaoCompiler.TargetQASMTopLevel, ci::Core.CodeInfo)
   @ YaoCompiler ~/.julia/dev/YaoCompiler/src/compiler/codegen/qasm.jl:237
 [5] code_qasm(spec::RoutineSpec{GenericRoutine{:adder3}, Tuple{}}; optimize::Bool, gate::Bool, passes::Vector{Symbol})
   @ YaoCompiler ~/.julia/dev/YaoCompiler/src/compiler/reflection.jl:6
 [6] code_qasm(spec::RoutineSpec{GenericRoutine{:adder3}, Tuple{}})
   @ YaoCompiler ~/.julia/dev/YaoCompiler/src/compiler/reflection.jl:4
 [7] top-level scope
   @ ~/.julia/dev/YaoCompiler/src/compiler/reflection.jl:26

ChenZhao44 avatar Nov 12 '20 04:11 ChenZhao44

you need to using YaoCompiler.Intrinsics first, or you won't have the definition of X, with the ccx case, e.g

using YaoCompiler
using YaoCompiler.Intrinsics

qasm"""OPENQASM 2.0;
include "qelib1.inc";
"""

@device function adder3()
    @ctrl 1 4=>X
    @ctrl 2 4=>X
    @ctrl 3 4=>X
    (1,2,5) => ccx()
    (1,3,5) => ccx()
    (2,3,5) => ccx()
end

This is a bit cumbersome since there is an extra step, but I don't want to export X from YaoCompiler at least because X is too short and it causes name conflicts quite often.

Roger-luo avatar Nov 12 '20 16:11 Roger-luo

for other examples you can check the tests first, the tests should be working at least.

Roger-luo avatar Nov 12 '20 16:11 Roger-luo

actually some other code won't work due to wrong constant prop at the moment, I'm fixing it in #3

Roger-luo avatar Nov 13 '20 19:11 Roger-luo