Skip to content

Commit 7f4deb7

Browse files
authored
reduce codegen on YaoBlocks.mat methods (EnzymeAD#343)
1 parent ede5945 commit 7f4deb7

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

ext/ReactantYaoBlocksExt.jl

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,35 @@ function YaoBlocks.mat(
77
::Type{T}, R::RotationGate{D,Reactant.TracedRNumber{S},<:XGate}
88
) where {D,T,S}
99
M = Reactant.broadcast_to_size(zero(T), (2, 2))
10-
M[1, 1] = cos(R.theta / 2)
11-
M[2, 2] = cos(R.theta / 2)
12-
M[1, 2] = -im * sin(R.theta / 2)
13-
M[2, 1] = -im * sin(R.theta / 2)
10+
c = cos(R.theta / 2)
11+
s = -im * sin(R.theta / 2)
12+
M[1, 1] = c
13+
M[2, 2] = c
14+
M[1, 2] = s
15+
M[2, 1] = s
1416
return M
1517
end
1618

1719
function YaoBlocks.mat(
1820
::Type{T}, R::RotationGate{D,Reactant.TracedRNumber{S},<:YGate}
1921
) where {D,T,S}
2022
M = Reactant.broadcast_to_size(zero(T), (2, 2))
21-
M[1, 1] = cos(R.theta / 2)
22-
M[2, 2] = cos(R.theta / 2)
23-
M[1, 2] = -sin(R.theta / 2)
24-
M[2, 1] = sin(R.theta / 2)
23+
c = cos(R.theta / 2)
24+
s = sin(R.theta / 2)
25+
M[1, 1] = c
26+
M[2, 2] = c
27+
M[1, 2] = -s
28+
M[2, 1] = s
2529
return M
2630
end
2731

2832
function YaoBlocks.mat(
2933
::Type{T}, R::RotationGate{D,Reactant.TracedRNumber{S},<:ZGate}
3034
) where {D,T,S}
3135
M = Reactant.broadcast_to_size(zero(T), (2, 2))
32-
M[1, 1] = exp(-im * R.theta / 2)
33-
M[2, 2] = exp(im * R.theta / 2)
36+
x = exp(im * R.theta / 2)
37+
M[1, 1] = conj(x)
38+
M[2, 2] = x
3439
return M
3540
end
3641

0 commit comments

Comments
 (0)