QuantumBFS / YaoLang.jl

YaoLang: The next DSL for Yao and quantum programs.
https://yaoquantum.org/YaoLang.jl/dev/
Apache License 2.0
31 stars 6 forks source link

Errors in @code_qasm #66

Closed ChenZhao44 closed 3 years ago

ChenZhao44 commented 3 years ago

Errors when generating qasm codes for a quantum circuit.

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

julia> @code_qasm adder3()
ERROR: TypeError: in typeassert, expected Core.Const, got a value of type Core.PartialStruct
Stacktrace:
 [1] obtain_ssa_const(x::Any, ci::Core.CodeInfo)
   @ YaoLang.Compiler ~/.julia/dev/YaoLang/src/compiler/codegen/qasm.jl:154
 [2] YaoLang.Compiler.RegMap(target::YaoLang.Compiler.TargetQASMTopLevel, ci::Core.CodeInfo)
   @ YaoLang.Compiler ~/.julia/dev/YaoLang/src/compiler/codegen/qasm.jl:68
 [3] QASMCodeGenState
   @ ~/.julia/dev/YaoLang/src/compiler/codegen/qasm.jl:104 [inlined]
 [4] codegen(target::YaoLang.Compiler.TargetQASMTopLevel, ci::Core.CodeInfo)
   @ YaoLang.Compiler ~/.julia/dev/YaoLang/src/compiler/codegen/qasm.jl:189
 [5] code_qasm(spec::YaoLang.Compiler.RoutineSpec{YaoLang.Compiler.GenericRoutine{:adder3}, Tuple{}}; optimize::Bool, gate::Bool, passes::Vector{Symbol})
   @ YaoLang.Compiler ~/.julia/dev/YaoLang/src/compiler/reflection.jl:6
 [6] code_qasm(spec::YaoLang.Compiler.RoutineSpec{YaoLang.Compiler.GenericRoutine{:adder3}, Tuple{}})
   @ YaoLang.Compiler ~/.julia/dev/YaoLang/src/compiler/reflection.jl:4
 [7] top-level scope
   @ ~/.julia/dev/YaoLang/src/compiler/reflection.jl:26
Roger-luo commented 3 years ago

should be fixed in YaoCompiler now

Roger-luo commented 3 years ago

note: this example still won't work since ccx in not a valid QASM intrinsic instruction, one needs to include qelib1.inc etc to define a ccx gate in terms of cx and single-qubit gates instead of using @ctrl

ChenZhao44 commented 3 years ago

Maybe it would be better to generate the include statement and throw a warning when generating QASM codes if there are Toffoli gates in a function with @device. From my understanding, it would be confusing for YaoLang users to include qelib1.inc when using @device.

Roger-luo commented 3 years ago

The definition of ccx is only needed when you want to generate qasm.

Note the point is there should be a definition of how toffoli gate is decomposed into 2 qubit gates. It doesn't have to be including the qelib file. I have clarified this in error msg. You can declare a toffoli gate via a device macro too.

I don't think auto generate the include statement is a correct behavior - this file is specific to IBM quantum experience, it will cause the program fail since not all platforms have the definition. Compiler should only honestly translate the original program instead of adding something that might not equivalent.

In the future we can consider providing gate decomposition passes in the optimizer for qasm compile target. But now this piece of code shouldn't work directly on codegen.