# Hadamard gate for qudit (dimesion 8)

I am using the code for generating the gates for qu8it

The resultant gate is not unitary (and so not hermitian too). I am a bit confused with the result. Is it possible for a quantum gate to be non-unitary? When I take the just real or just imaginary part of the matrix, then it is unitary. Is the code have some missing part or am I missing some fact about qudits?

This is the code thanks to @unknown :

 TestA:=function(p)local Ig,Zg,Xg,Hg,Pg,grp,cen,sgrp,scen,C;
Ig:=IdentityMat(p);
Zg:=DiagonalMat(List([0..p-1],x->E(p)^x));
Xg:=IdentityMat(p){\mod([0..p-1]+1,p)+1};
Hg:=List([0..p-1],x->List([0..p-1],y->E(p)^(x*y)))/ER(p);
Pg:=DiagonalMat(List([0..p-1],x->E(2*p)^(x*(x-\mod(p,2)))));
grp:=Group([Hg,Pg,Zg]);cen:=Center(grp);sgrp:=Size(grp);scen:=Size(cen);
Print(" |G| = ",String(sgrp,-5));Print(" |Cen(G)| = ",String(scen,-5));Print(" |G/Cen(G)| = ",String(sgrp/scen,-5));Print("\n");
if(p=8)then
Print("Z=\n");PrintArray(Zg);
Print("X=\n");PrintArray(Xg);
Print("H=\n");PrintArray(Hg);
Print("P=\n");PrintArray(Pg);
C:=KroneckerProduct([[1,0],[0,1]],[[1,1],[1,-1]]/ER(2));C:=C{[1,3,2,4]}{[1,3,2,4]};
Zg:=C^-1*Zg*C;
Xg:=C^-1*Xg*C;
Hg:=C^-1*Hg*C;
Pg:=C^-1*Pg*C;
grp:=Group([Hg,Pg,Zg]);
Print(" all elements are monomial? ",ForAll(Elements(grp),IsMonomialMatrix),"\n");
fi;
return grp;end;

g2:=TestA(2);;
g4:=TestA(4);;
g8:=TestA(8);;


    a=1j
b=0.707+0.707j
qudit_8_H = Qobj([ [   1/4*b-1/4*b**3,   1/4*b-1/4*b**3,   1/4*b-1/4*b**3,   1/4*b-1/4*b**3,   1/4*b-1/4*b**3,   1/4*b-1/4*b**3,   1/4*b-1/4*b**3,   1/4*b-1/4*b**3 ],
[   1/4*b-1/4*b**3,          1/4+1/4*a,   1/4*b+1/4*b**3,         -1/4+1/4*a,  -1/4*b+1/4*b**3,         -1/4-1/4*a,  -1/4*b-1/4*b**3,          1/4-1/4*a ],
[   1/4*b-1/4*b**3,   1/4*b+1/4*b**3,  -1/4*b+1/4*b**3,  -1/4*b-1/4*b**3,   1/4*b-1/4*b**3,   1/4*b+1/4*b**3,  -1/4*b+1/4*b**3,  -1/4*b-1/4*b**3 ],
[   1/4*b-1/4*b**3,         -1/4+1/4*a,  -1/4*b-1/4*b**3,          1/4+1/4*a,  -1/4*b+1/4*b**3,          1/4-1/4*a,   1/4*b+1/4*b**3,         -1/4-1/4*a ],
[   1/4*b-1/4*b**3,  -1/4*b+1/4*b**3,   1/4*b-1/4*b**3,  -1/4*b+1/4*b**3,   1/4*b-1/4*b**3,  -1/4*b+1/4*b**3,   1/4*b-1/4*b**3,  -1/4*b+1/4*b**3 ],
[   1/4*b-1/4*b**3,         -1/4-1/4*a,   1/4*b+1/4*b**3,          1/4-1/4*a,  -1/4*b+1/4*b**3,          1/4+1/4*a,  -1/4*b-1/4*b**3,         -1/4+1/4*a ],
[   1/4*b-1/4*b**3,  -1/4*b-1/4*b**3,  -1/4*b+1/4*b**3,   1/4*b+1/4*b**3,   1/4*b-1/4*b**3,  -1/4*b-1/4*b**3,  -1/4*b+1/4*b**3,   1/4*b+1/4*b**3 ],
[   1/4*b-1/4*b**3,          1/4-1/4*a,  -1/4*b-1/4*b**3,         -1/4-1/4*a,  -1/4*b+1/4*b**3,         -1/4+1/4*a,   1/4*b+1/4*b**3,          1/4+1/4*a ] ]
)


All these gates are in fact unitary. Since you're not using GAP and working with floating point, it's possible there are numerical precision issues that are affecting your tests. Here's a test that shows the check for unitarity in GAP :

TestB:=function(p)local Ig,Zg,Xg,Hg,Pg,gates;
Ig:=IdentityMat(p);
Zg:=DiagonalMat(List([0..p-1],x->E(p)^x));
Xg:=IdentityMat(p){\mod([0..p-1]+1,p)+1};
Hg:=List([0..p-1],x->List([0..p-1],y->E(p)^(x*y)))/ER(p);
Pg:=DiagonalMat(List([0..p-1],x->E(2*p)^(x*(x-\mod(p,2)))));
Print("check Z unitary : ",Zg*TransposedMat(GaloisCyc(Zg,-1))=Ig,"\n");
Print("check X unitary : ",Xg*TransposedMat(GaloisCyc(Xg,-1))=Ig,"\n");
Print("check H unitary : ",Hg*TransposedMat(GaloisCyc(Hg,-1))=Ig,"\n");
Print("check P unitary : ",Pg*TransposedMat(GaloisCyc(Pg,-1))=Ig,"\n");
Print("Z=\n");PrintArray(Zg);
Print("X=\n");PrintArray(Xg);
Print("sqrt(p)*H=\n");PrintArray(ER(p)*Hg);
Print("P=\n");PrintArray(Pg);
return Hg;
end;


The results (takes much less time than TestA because the group is not being calculated) :

H8:=TestB(8);;
check Z unitary : true
check X unitary : true
check H unitary : true
check P unitary : true
Z=
[ [        1,        0,        0,        0,        0,        0,        0,        0 ],
[        0,     E(8),        0,        0,        0,        0,        0,        0 ],
[        0,        0,     E(4),        0,        0,        0,        0,        0 ],
[        0,        0,        0,   E(8)^3,        0,        0,        0,        0 ],
[        0,        0,        0,        0,       -1,        0,        0,        0 ],
[        0,        0,        0,        0,        0,    -E(8),        0,        0 ],
[        0,        0,        0,        0,        0,        0,    -E(4),        0 ],
[        0,        0,        0,        0,        0,        0,        0,  -E(8)^3 ] ]
X=
[ [  0,  1,  0,  0,  0,  0,  0,  0 ],
[  0,  0,  1,  0,  0,  0,  0,  0 ],
[  0,  0,  0,  1,  0,  0,  0,  0 ],
[  0,  0,  0,  0,  1,  0,  0,  0 ],
[  0,  0,  0,  0,  0,  1,  0,  0 ],
[  0,  0,  0,  0,  0,  0,  1,  0 ],
[  0,  0,  0,  0,  0,  0,  0,  1 ],
[  1,  0,  0,  0,  0,  0,  0,  0 ] ]
sqrt(p)*H=
[ [        1,        1,        1,        1,        1,        1,        1,        1 ],
[        1,     E(8),     E(4),   E(8)^3,       -1,    -E(8),    -E(4),  -E(8)^3 ],
[        1,     E(4),       -1,    -E(4),        1,     E(4),       -1,    -E(4) ],
[        1,   E(8)^3,    -E(4),     E(8),       -1,  -E(8)^3,     E(4),    -E(8) ],
[        1,       -1,        1,       -1,        1,       -1,        1,       -1 ],
[        1,    -E(8),     E(4),  -E(8)^3,       -1,     E(8),    -E(4),   E(8)^3 ],
[        1,    -E(4),       -1,     E(4),        1,    -E(4),       -1,     E(4) ],
[        1,  -E(8)^3,    -E(4),    -E(8),       -1,   E(8)^3,     E(4),     E(8) ] ]
P=
[ [       1,       0,       0,       0,       0,       0,       0,       0 ],
[       0,   E(16),       0,       0,       0,       0,       0,       0 ],
[       0,       0,    E(4),       0,       0,       0,       0,       0 ],
[       0,       0,       0,  -E(16),       0,       0,       0,       0 ],
[       0,       0,       0,       0,       1,       0,       0,       0 ],
[       0,       0,       0,       0,       0,  -E(16),       0,       0 ],
[       0,       0,       0,       0,       0,       0,    E(4),       0 ],
[       0,       0,       0,       0,       0,       0,       0,   E(16) ] ]

• thank you! I have actually printed with s specific precision too but qutip still says this is not unitary. When I check by hand, it is unitary. Anyway, thanks for the clarification Oct 28 at 23:20
• just for double check: E(4) is equal to i and E(8) is equal to 0.707+0.707i. Is that correct? Oct 28 at 23:29
• yes. $E(n)=\exp(2 \pi \imath/n)$ and $ER(n)=\sqrt n$; Oct 28 at 23:44