2016-08-12 45 views
1

我在学习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]]' 

回答

9

cossin只能采取Floating的说法,但对于gateR类型签名说tIntegral类型,并且不是所有Integral s为Floating。请使用fromIntegral更改gateR的型号签名或在gateR之内转换t

4

您的功能只需要约束Integral t,因此您需要将您的t转换为fromIntegral,并且可能需要一个明确的类型签名,以便您可以应用正弦和余弦。例如。

cos (fromIntegral t :: Double)