2016-08-12 8 views
1

私はハスケルを学んでおり、私はインターネット上で見つけたリソースに基づいていくつかの量子ゲートを実装しようとしました。 今のところ、Zゲート、Xゲート、Hゲートを正常に実装しましたが、回転ゲートの実装に問題があります。ハスケル: `cos 'の使用に起因する(浮動小数点)を推論できません

U = [[cos t -sin t] 
    [sin t cos t ]] 

Iが書いたコード:

gateR :: Integral t => t -> Matrix (Complex Double) 
gateR t = matrixToComplex [[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-ゲートを実装するのと同じ方法でゲータ(回転ゲート)を実装しようとしました次のエラーがあり、私はそれを実際に理解していません(私はまだ言語を学んでいます)。

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

cossinFloating引数を取ることができ、まだgateRのための型シグネチャはtIntegralタイプではなく、すべてのIntegral sがFloatingであることを述べています。タイプシグニチャをgateRに変更するか、tgateRに変換してfromIntegralを使用してください。

4

あなたの関数は、Integral tの制約を要求します。したがって、tfromIntegralと変換する必要があります。また、サインとコサインを適用できるように、明示的な型シグネチャが必要です。例えば。

cos (fromIntegral t :: Double) 
関連する問題