Skip to content
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

inline does not happen as expected #25

Closed
Roger-luo opened this issue May 1, 2021 · 1 comment
Closed

inline does not happen as expected #25

Roger-luo opened this issue May 1, 2021 · 1 comment

Comments

@Roger-luo
Copy link
Member

Roger-luo commented May 1, 2021

MWE, need https://github.com/QuantumBFS/YaoTargetQASM.jl for the syntax, on 1.6.1

using OpenQASM
using YaoTargetQASM
using YaoCompiler
using YaoCompiler.Intrinsics
using CompilerPluginTools

qasm"""
OPENQASM 2.0;
gate cx c,t { CX c,t; }
gate u1(lambda) q { U(0,0,lambda) q; }
gate t a { u1(pi/4) a; }
gate h a { u2(0,pi) a; }
gate tdg a { u1(-pi/4) a; }

gate ccx a,b,c
{
  h c;
  cx b,c; tdg c;
  cx a,c; t c;
  cx b,c; tdg c;
  cx a,c; t b; t c; h c;
  cx a,b; t a; tdg b;
  cx a,b;
}

gate majority a,b,c 
{ 
  cx c,b; 
  cx c,a; 
  ccx a,b,c; 
}
"""

ast = code_qasm(typeof(majority()))

This gives you the following

julia> ast = code_qasm(typeof(majority()))
OPENQASM 2.0;
qreg qreg_1[3];
CX qreg_1[2], qreg_1[1];
CX qreg_1[2], qreg_1[0];
h qreg_1[2];
CX qreg_1[1], qreg_1[2];
tdg qreg_1[2];
CX qreg_1[0], qreg_1[2];
t qreg_1[2];
CX qreg_1[1], qreg_1[2];
tdg qreg_1[2];
CX qreg_1[0], qreg_1[2];
t qreg_1[1];
t qreg_1[2];
h qreg_1[2];
CX qreg_1[0], qreg_1[1];
t qreg_1[0];
tdg qreg_1[1];
CX qreg_1[0], qreg_1[1];

but h and t should be inlined instead of invoke, this seems to only happen when ccx is called inside another function, and for ccx itself it is fine

julia> code_qasm(typeof(ccx()))
OPENQASM 2.0;
qreg qreg_1[3];
h qreg_1[2];
CX qreg_1[1], qreg_1[2];
rz(0) qreg_1[2];
ry(0) qreg_1[2];
rz(-0.7853981633974483) qreg_1[2];
CX qreg_1[0], qreg_1[2];
rz(0) qreg_1[2];
ry(0) qreg_1[2];
rz(0.7853981633974483) qreg_1[2];
CX qreg_1[1], qreg_1[2];
rz(0) qreg_1[2];
ry(0) qreg_1[2];
rz(-0.7853981633974483) qreg_1[2];
CX qreg_1[0], qreg_1[2];
rz(0) qreg_1[1];
ry(0) qreg_1[1];
rz(0.7853981633974483) qreg_1[1];
rz(0) qreg_1[2];
ry(0) qreg_1[2];
rz(0.7853981633974483) qreg_1[2];
h qreg_1[2];
CX qreg_1[0], qreg_1[1];
rz(0) qreg_1[0];
ry(0) qreg_1[0];
rz(0.7853981633974483) qreg_1[0];
rz(0) qreg_1[1];
ry(0) qreg_1[1];
rz(-0.7853981633974483) qreg_1[1];
CX qreg_1[0], qreg_1[1];

the CodeInfo can be obtained via

code_typed(Intrinsics.apply, (AnyReg, typeof(majority())); interp=YaoInterpreter())

I think need some help from you @vchuravy

@Roger-luo
Copy link
Member Author

When apply is called via a non-intrinsic user-defined Operation, it is forced to inline by making inlineable=true in CodeInfo

https://github.com/QuantumBFS/YaoCompiler.jl/blob/master/src/compiler/syntax.jl#L248

thus I'm not sure why inlining pass decide not to inline them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant