Hi just saw this post, and just want to show off Yao.jl here, you can use Yao’s operator system to construct matrix (it is not just a circuit simulator!), the syntax is also more readable than plain matrices
julia> using Yao
julia> function yao(K)
if K > 1 && K % 1 == 0
kron(yao(K-1), -Z)
elseif K == 1
1/sqrt(2) * X
end
end
yao (generic function with 1 method)
julia> @time mat(yao(25)); # with compile time
7.625354 seconds (23.85 M allocations: 2.792 GiB, 6.06% gc time, 90.30% compilation time)
julia> @time mat(yao(25)); # without compile time
0.563007 seconds (1.63 k allocations: 1.500 GiB, 10.98% gc time)
and on my machine the qutip version
In [1]: import numpy as np
...: from qutip import *
...:
...: def psitest(K):
...: if K > 1 and K % 1 == 0:
...: return tensor(psitest(K-1), - sigmaz())
...: elif K == 1:
...: return (1/np.sqrt(2)) * sigmax()
...:
In [2]:
In [2]: %timeit psitest(25)
1.6 s ± 3.69 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
with Julia SparseArrays
julia> using SparseArrays
julia> function ψtest(K)
if K > 1 && K % 1 == 0
return kron(ψtest(K-1), sparse([-1. 0.;0. 1.]))
elseif K == 1
return 1/√(2) * sparse([0. 1.;1. 0.])
end
end
ψtest (generic function with 1 method)
julia> @time ψtest(16);
0.021314 seconds (36.51 k allocations: 5.085 MiB, 88.99% compilation time)
julia> @time ψtest(25);
1.408559 seconds (290 allocations: 1.500 GiB, 17.22% gc time)