Skip to content

Commit 80a5122

Browse files
committed
Implement general tensor_pow via Base.power_by_squaring
1 parent b3be671 commit 80a5122

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

src/QuantumInterface.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function tensor end
4545
const = tensor
4646
tensor() = throw(ArgumentError("Tensor function needs at least one argument."))
4747

48-
function tensor_pow end # TODO should Base.^ be the same as tensor_pow?
48+
function tensor_pow end
4949

5050
function traceout! end
5151

src/bases.jl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,7 @@ function tensor(b1::Basis, b2::CompositeBasis)
9393
end
9494
tensor(bases::Basis...) = reduce(tensor, bases)
9595

96-
function Base.:^(b::Basis, N::Integer)
97-
if N < 1
98-
throw(ArgumentError("Power of a basis is only defined for positive integers."))
99-
end
100-
tensor([b for i=1:N]...)
101-
end
96+
Base.:^(b::Basis, N::Integer) = tensor_pow(b, N)
10297

10398
"""
10499
equal_shape(a, b)

src/tensor.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,10 @@ tensor(op::AbstractOperator) = op
88
tensor(operators::AbstractOperator...) = reduce(tensor, operators)
99
tensor(state::StateVector) = state
1010
tensor(states::Vector{T}) where T<:StateVector = reduce(tensor, states)
11+
12+
"""
13+
tensor_pow(a, N)
14+
15+
Gives the tensor product of `a` `N` times.
16+
"""
17+
tensor_pow(a, N) = Base.power_by_squaring(a, N; mul=)

0 commit comments

Comments
 (0)