让我们举一个明显的单一阵列:
>>> a = 0.7
>>> b = (1-a**2)**0.5
>>> m = np.array([[a,b],[-b,a]])
>>> m.dot(m.conj().T)
array([[ 1., 0.],
[ 0., 1.]])
,并尝试在它的功能:
>>> is_unitary(m)
Traceback (most recent call last):
File "<ipython-input-28-8dc9ddb462bc>", line 1, in <module>
is_unitary(m)
File "<ipython-input-20-3758c2016b67>", line 5, in is_unitary
error = np.linalg.norm(np.eye(n) - matrix.dot(matrix.transpose().conjugate()))
ValueError: operands could not be broadcast together with shapes (4,4) (2,2)
这是因为
>>> m.size
4
>>> np.eye(m.size)
array([[ 1., 0., 0., 0.],
[ 0., 1., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]])
如果我们更换n = matrix.size
无线日len(m)
或m.shape[0]
什么的,我们得到
>>> is_unitary(m)
True
我可能只是使用
>>> np.allclose(np.eye(len(m)), m.dot(m.T.conj()))
True
其中allclose
有rtol
和atol
参数。
来源
2016-01-25 16:36:23
DSM