Skip to content

Commit b0ed1a1

Browse files
committed
Switch to copied implementation of tensor_pow_by_squaring
1 parent 80a5122 commit b0ed1a1

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/tensor.jl

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,30 @@ tensor(states::Vector{T}) where T<:StateVector = reduce(tensor, states)
1414
1515
Gives the tensor product of `a` `N` times.
1616
"""
17-
tensor_pow(a, N) = Base.power_by_squaring(a, N; mul=)
17+
tensor_pow(a, N) = tensor_pow_by_squaring(a, N)
18+
19+
# Copied from Base.power_by_squaring as `mul` keyword dosn't work without implementing `*`
20+
function tensor_pow_by_squaring(x, p::Integer)
21+
if p == 1
22+
return x
23+
elseif p == 2
24+
return tensor(x, x)
25+
elseif p < 1
26+
throw(DomainError("Cannot take tensor_pow to power less than one"))
27+
end
28+
t = trailing_zeros(p) + 1
29+
p >>= t
30+
while (t -= 1) > 0
31+
x = tensor(x, x)
32+
end
33+
y = x
34+
while p > 0
35+
t = trailing_zeros(p) + 1
36+
p >>= t
37+
while (t -= 1) >= 0
38+
x = tensor(x, x)
39+
end
40+
y = tensor(y, x)
41+
end
42+
return y
43+
end

0 commit comments

Comments
 (0)