Regarding your function compute_channel_operation(), you can speed it up considerably if you don't perform a list comprehension but vectorize the operation and pass in a 3D tensor with all your operationsrho = np.random.rand(2, 2)operators = [np.random.rand(2, 2) for _ in range(1000)]operators_tensor = np.asarray(operators) # same as np.random.rand(1000, 2, 2)def compute_channel_operation(rho, operators): operation = [E@[email protected]().T for i, E in enumerate(operators)] return np.sum(operation, axis=0)def compute_channel_operation2(rho, operators): return np.sum(operators @ rho @ operators.transpose(0, 2, 1).conj(), axis=0)A = compute_channel_operation(rho, operators)B = compute_channel_operation(rho, operators_tensor)C = compute_channel_operation2(rho, operators_tensor)np.allclose(A, B) # Truenp.allclose(A, C) # True%timeit compute_channel_operation(rho, operators)# 6.95 ms ± 103 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)%timeit compute_channel_operation(rho, operators_tensor)# 7.53 ms ± 141 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)%timeit compute_channel_operation2(rho, operators_tensor)# 416 µs ± 12 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) 这篇关于关于单个numpy数组中的多个numpy数组的维初始化的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-21 11:52