我在学习Haskell,我试图根据我在互联网上找到的资源实现一些量子门。 现在,我成功实现了Z-gate,X-gate,H-gate,但是我在实现旋转门时遇到了问题。Haskell:无法推断(浮动t)由于使用'cos'引起
U = [[cos t -sin t]
[sin t cos t ]]
,我写的代码:
type Vector a = [a]
type Matrix a = [Vector a]
vectorToComplex :: Integral a => Vector a -> Vector (Complex Double)
vectorToComplex = map (\i -> fromIntegral i:+0.0)
matrixToComplex :: Integral a => Matrix a -> Matrix (Complex Double)
matrixToComplex = map vectorToComplex
--Z Gate
gateZ :: Matrix (Complex Double)
gateZ = matrixToComplex [[1,0],[0,-1]]
我试图以同样的方式来实现盖特(旋转门)我实现了Z-门:
gateR :: Integral t => t -> Matrix (Complex Double)
gateR t = matrixToComplex [[cos t,-sin t],[sin t,cos t]]
,但我有下一个错误,我真的不明白它(我仍然在学习语言)。
Could not deduce (Floating t) arising from a use of `cos'
from the context (Integral t)
bound by the type signature for
gateR :: Integral t => t -> Matrix (Complex Double)
at quantum.hs:66:8-44
Possible fix:
add (Floating t) to the context of
the type signature for
gateR :: Integral t => t -> Matrix (Complex Double)
In the expression: cos t
In the expression: [cos t, - sin t]
In the first argument of `matrixToComplex', namely
`[[cos t, - sin t], [sin t, cos t]]'