jwoehr / nuqasm2

New OPENQASM2 translator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parameters of custom gates not recognized

giovanniamedeocirillo opened this issue · comments

I created a custom gate cu1mol(theta) a, b in a file containing definitions of quantum gates.
`
OPENQASM 2.0;

include "qelib1.inc";
gate cxmol a, b {
h b;
cz a, b;
h b;
}

gate cu1mol(theta) a, b {
u1(theta/2) a;
cxmol a, b;
u1(-theta/2) b;
cxmol a, b;
u1(theta/2) b;
}
If I try to define in a qasm file a gate likecu1mol(pi/2) q[1], q[0];nuqasm2 does not recognizepi/2, thus having on the output file u1(theta/2) q[1];instead ofu1(pi/2) q[1];`

@giovanniamedeocirillo thanks, looking at this.

Yes, you are right. The source and output of nuqasm2 -a -c -d -o my_cu1mol.out my_cu1mol.qasm is attached.
my_cu1mol.zip

I figured out what the problem is.

Parameter binding is getting confused in expressions. The following gate definition works, of course, because there no expressions, just the token names.

gate cu1mol(fred) a, b {
u1(fred) a;
cxmol a, b;
u1(fred) b;
cxmol a, b;
u1(fred) b;
}

"My bad" as people say.

I'm going to eat lunch then start to fix it.

@giovanniamedeocirillo ... try the issue1_parmexpand bug fix branch and tell me if that works for you.

@jwoehr now it seems ok for cu1mol(pi/3) it returns

u1(1.0471975511965976/2) q[1]; h q[0]; cz q[1],q[0]; h q[0]; u1(-1.0471975511965976/2) q[0]; h q[0]; cz q[1],q[0]; h q[0]; u1(1.0471975511965976/2) q[0];
So now the parameter is properly interpreted, thank you so much!
I don't know if it is hard, but if it was also possible to evalutate the division by 2 (more in general the expression in which the parameter is employed) it would be the best, IMHO.

In execution something like ******bound_param theta param 1.0471975511965976 b_list [] ******the_op u1 the_reg_list [Qubit(QuantumRegister(3, 'q'), 1)] the_param_list ['1.0471975511965976/2'] ******the_op cxmol the_reg_list [Qubit(QuantumRegister(3, 'q'), 1), Qubit(QuantumRegister(3, 'q'), 0)] the_param_list None is printed on screen. Is it OK (maybe for debugging)?

@jwoehr now it seems ok for cu1mol(pi/3) it returns

u1(1.0471975511965976/2) q[1]; h q[0]; cz q[1],q[0]; h q[0]; u1(-1.0471975511965976/2) q[0]; h q[0]; cz q[1],q[0]; h q[0]; u1(1.0471975511965976/2) q[0];
So now the parameter is properly interpreted, thank you so much!

Great, I'll clean up the code and merge the branch.

I don't know if it is hard, but if it was also possible to evalutate the division by 2 (more in general the expression in which the parameter is employed) it would be the best, IMHO.

Yes, that would be better. If you open an issue on that as a feature request, I'll take a look at it.

In execution something like ******bound_param theta param 1.0471975511965976 b_list [] ******the_op u1 the_reg_list [Qubit(QuantumRegister(3, 'q'), 1)] the_param_list ['1.0471975511965976/2'] ******the_op cxmol the_reg_list [Qubit(QuantumRegister(3, 'q'), 1), Qubit(QuantumRegister(3, 'q'), 0)] the_param_list None is printed on screen. Is it OK (maybe for debugging)?

Ha! Yes, I forgot and left some debug print() statements in. Thank you!

I see I introduced a bug in my bug fix ... hold on while I fix that :)

All my test cases now work, how are things for you, @giovanniamedeocirillo ?

Default parametric gates as u1(pi/4) and rx(pi) provide an error TypeError: 'NoneType' object is not subscriptable when called inside custom gates.

@giovanniamedeocirillo , You are correct. I did not explain fully before, it was late ;)

  1. Delete the issue1_parmexpand branch
  2. Check out master again

I did introduce a bug in my patch branch, but it is fixed in the master branch.

Try that and let me know.

@jwoehr it works! Thank you so much!

Thanks for testing.

I will close this issue and cut a new release.