我正在学习Haskell,我试图根据我在Internet上发现的资源来实现一些量子门。目前,我已经成功实现了Z门,X门,H门,但是实现旋转门时遇到了问题。
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(旋转门): gateR :: Integral t => t -> Matrix (Complex Double) gateR t = matrixToComplex [[cos t,-sin t],[sin t,cos t]] 但是我遇到下一个错误,但我不太了解(我仍在学习该语言)。
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]]'