微软基于F#的 Liqui|> 量子编程语言

版权声明:技术分享,csdn longji https://blog.csdn.net/longji/article/details/85530666

01 基本信息

微软提供了一款基于F#(函数式编程语言)的量子编程语言 Liqui|> 。这款量子语言更新较缓慢。没有单独的安装包,发布的二进制文件直接在github上面,可以和demo代码一同下载。运行依赖vs2017运行时。当然需要安装.net core和F#。
安装链接:http://stationq.github.io/Liquid/getting-started/
说明文档:http://research.microsoft.com/en-us/projects/liquid/
github链接:https://github.com/StationQ/Liquid

02 下载及运行

:: 安装vs2017 社区版本,带.net core 和 F#
:: 在安装目录下找到VsDevCmd.bat,运行这个文件开启命令行,不用设置环境变量即可使用fsi命令
:: 我的安装目录是d:\install\Microsoft Visual Studio\2017\Community
:: VsDevCmd.bat目录关系如下:
d:\install\Microsoft Visual Studio\2017\Community\Common7\Tools\VsDevCmd.bat
::
git clone https://github.com/StationQ/Liquid.git
:: 进入预编译目录
cd Liquid\bin
:: 运行demo,__Teleport()是内置函数,内置函数都带__
D:\git\Quantum\Liquid\bin>Liquid.exe __Teleport()
0:0000.0/===================================================================================================
0:0000.0/=    The Language-Integrated Quantum Operations (LIQUi|>) Simulator                               =
0:0000.0/=    is made available under license by Microsoft Corporation                                     =
0:0000.0/=    License terms may be viewed at https://github.com/StationQ/Liquid/blob/master/LICENSE.md    =
0:0000.0/=    Please type Y, followed by return, to indicate your acceptance of these terms                =
0:0000.0/===================================================================================================
y
0:0000.8/===================================================================================================
0:0000.8/=    Thank you for accepting the license terms                                                    =
0:0000.8/===================================================================================================

03 运行一个量子隐形传态demo

03.01 脚本模式运行

下面是 《量子算法与编程入门》 3.3.2 例3.6 脚本模式的例子
UserScript.fsx

#if INTERACTIVE
#r @"..\bin\Liquid1.dll"
#else
namesapce Microsoft.Research.Liquid
#endif
open System
open Microsoft.Research.Liquid
open Util
open Operations

module Script=
    [<LQD>]
    let UserScript() = 
        show "这是用户自定义的函数 'UserFunction'"
        let ket = Ket(3)
        let qs = ket.Qubits
        let qsT = qs.Tail
        H qs
        let Alice = qs.Head
        show "Alice 持有的未知量子态为:%s"(Alice.ToString())
        H qsT
        CNOT qsT
        CNOT qs
        H qs
        M qsT
        BC X qsT
        M qs
        BC Z !!(qs, 0, 2)
        let Bob = qs.Tail.Tail
        show "Bob 持有的量子态最终为:%s"(Bob.ToString())

#if INTERACTIVE
do
    Script.UserScript()
#endif

把上面的UserScript.fsx文件放到Liquid\bin目录中,如下运行即可。
运行结果如下:

d:\git\Quantum\Liquid\bin>fsi UserScript.fsx
0:0000.0/这是用户自定义的函数 'UserFunction'
0:0000.0/
0:0000.0/===========================================================================================
0:0000.0/=    The Language-Integrated Quantum Operations (LIQUi|>) Simulator                       =
0:0000.0/=        Copyright (c) 2015,2016 Microsoft Corporation                                    =
0:0000.0/=        If you use LIQUi|> in your research, please follow the guidelines at             =
0:0000.0/=        https://github.com/StationQ/Liquid for citing LIQUi|> in your publications.     =
0:0000.0/===========================================================================================
0:0000.0/
0:0000.0/Alice 持有的未知量子态为:            0.7071|0>+            0.7071|1>
0:0000.0/Bob 持有的量子态最终为:[            0.7071|0>+            0.7071|1>]

03.02 自定义函数方式运行

用vs2017打开D:\git\Quantum\Liquid\source\Liquid.sln,编辑Main.fs文件,修改如下:

namespace Microsoft.Research.Liquid

//module UserSample =
//    open System
//    open Util
//    open Operations
//    //open Native             // Support for Native Interop
//    //open HamiltonianGates   // Extra gates for doing Hamiltonian simulations
//    //open Tests              // All the built-in tests

//    /// <summary>
//    /// Performs an arbitrary rotation around X. 
//    /// </summary>
//    /// <param name="theta">Angle to rotate by</param>
//    /// <param name="qs">The head qubit of this list is operated on.</param>
//    let rotX (theta:float) (qs:Qubits) =
//        let gate (theta:float) =
//            let nam     = "Rx" + theta.ToString("F2")
//            new Gate(
//                Name    = nam,
//                Help    = sprintf "Rotate in X by: %f" theta,
//                Mat     = (
//                    let phi     = theta / 2.0
//                    let c       = Math.Cos phi
//                    let s       = Math.Sin phi
//                    CSMat(2,[0,0,c,0.;0,1,0.,-s;1,0,0.,-s;1,1,c,0.])),
//                Draw    = "\\gate{" + nam + "}"
//                )
//        (gate theta).Run qs

//    [<LQD>]
//    let __UserSample() =
//         show "This module is a good place to put compiled user code"

// 以下是自定义函数,自定义模块必须放在man函数的前面([<EntryPoint>]的前面)
// vs2017中重新编译后,D:\git\Quantum\Liquid\source\bin\Debug\Liquid.exe
// 运行 Liquid.exe UserTest()
module UserTest =
    open System
    open Util
    open Operations
    let ket = Ket(3)
    let qs = ket.Qubits
    let qsT = qs.Tail
    H qs
    let Alice = qs.Head
    show "Alice 持有的未知量子态为:%s"(Alice.ToString())
    H qsT
    CNOT qsT
    CNOT qs
    H qs
    M qsT
    BC X qsT
    M qs
    BC Z !!(qs, 0, 2)
    let Bob = qs.Tail.Tail
    show "Bob 持有的量子态最终为:%s"(Bob.ToString())

    [<LQD>]
    let UserTest() =
         show "This module is a good place to put compiled user code"

module Main =
    open App

    /// <summary>
    /// The main entry point for Liquid.
    /// </summary>
    [<EntryPoint>]
    let Main _ =
        RunLiquid ()

编译后(注意编译后的生成目录source\bin\Debug),运行结果如下:

d:\git\Quantum\Liquid\source\bin\Debug>Liquid.exe UserTest()
0:0000.0/
0:0000.0/===========================================================================================
0:0000.0/=    The Language-Integrated Quantum Operations (LIQUi|>) Simulator                       =
0:0000.0/=        Copyright (c) 2015,2016 Microsoft Corporation                                    =
0:0000.0/=        If you use LIQUi|> in your research, please follow the guidelines at             =
0:0000.0/=        https://github.com/StationQ/Liquid for citing LIQUi|> in your publications.     =
0:0000.0/===========================================================================================
0:0000.0/
0:0000.0/Alice 持有的未知量子态为:            0.7071|0>+            0.7071|1>
0:0000.0/Bob 持有的量子态最终为:[            0.7071|0>+            0.7071|1>]
0:0000.0/=============== Logging to: Liquid.log opened ================
0:0000.0/This module is a good place to put compiled user code
0:0000.0/=============== Logging to: Liquid.log closed ================

03.03 生成线路设计图

修改后的Main.fs文件内容

namespace Microsoft.Research.Liquid
module CircuitDiagram =
    open System
    open Util
    open Operations

    let teleport(qs:Qubits) = 
        let qsT = qs.Tail
        LabelL >!<(["src";"\\ket{0}";"\\ket{0}"],qs)
        H qsT
        CNOT qsT
        CNOT qsT
        H qs
        CNOT qsT
        H qs.Tail.Tail
        CNOT !!(qs, 0, 2)
        H qs.Tail.Tail
        LabelR "Dest"!!(qs, 2)

    [<LQD>]
    let CircuitDiagram() =
         let ket = Ket(3)
         let qs = ket.Qubits
         teleport qs
         let circ = Circuit.Compile teleport qs
         circ.Run qs
         circ.Dump()
         circ.Fold().RenderHT("teleport")

module Main =
    open App

    /// <summary>
    /// The main entry point for Liquid.
    /// </summary>
    [<EntryPoint>]
    let Main _ =
        RunLiquid ()

编译后运行:Liquid.exe CircuitDiagram(),结果如下:

d:\git\Quantum\Liquid\source\bin\Debug>Liquid.exe CircuitDiagram()
0:0000.0/
0:0000.0/===========================================================================================
0:0000.0/=    The Language-Integrated Quantum Operations (LIQUi|>) Simulator                       =
0:0000.0/=        Copyright (c) 2015,2016 Microsoft Corporation                                    =
0:0000.0/=        If you use LIQUi|> in your research, please follow the guidelines at             =
0:0000.0/=        https://github.com/StationQ/Liquid for citing LIQUi|> in your publications.     =
0:0000.0/===========================================================================================
0:0000.0/
0:0000.0/=============== Logging to: Liquid.log opened ================
0:0000.0/Writing: teleport.htm (split=100.00% scale=100.00%)
0:0000.0/Writing: teleport.tex (split=100.00% scale=100.00%)
0:0000.0/     Doing columns:  0 - 12
0:0000.0/=============== Logging to: Liquid.log closed ================

d:\git\Quantum\Liquid\source\bin\Debug目录下生成线路图:
D:\git\Quantum\Liquid\source\bin\Debug\teleport.htm
D:\git\Quantum\Liquid\source\bin\Debug\teleport.tex
打开teleport.htm文件内容选下:
在这里插入图片描述

04 Liqui|>自带demo

Liquid\docs\Liquid.chm 是类和接口说明文档。
Liquid\docs\LIQUiD.pdf 是Liqui|>的说明文档。
Liquid\docs\Tutorial.md 是几个demo的说明。

Liquid\Samples 下面的几个demo运行模式可以参考 Kraus.cmd里面的命令。

:: 用VsDevCmd.bat启动命令行(注意找到自己的vs2017的安装路径):
d:\install\Microsoft Visual Studio\2017\Community\Common7\Tools\VsDevCmd.bat
cd D:\git\Quantum\Liquid\Samples
:: 比如运行如下命令:
d:\git\Quantum\Liquid\Samples>fsi Noise1.fsx

运行结果:

d:\git\Quantum\Liquid\Samples>fsi Noise1.fsx
0:0000.0/
0:0000.0/===========================================================================================
0:0000.0/=    The Language-Integrated Quantum Operations (LIQUi|>) Simulator                       =
0:0000.0/=        Copyright (c) 2015,2016 Microsoft Corporation                                    =
0:0000.0/=        If you use LIQUi|> in your research, please follow the guidelines at             =
0:0000.0/=        https://github.com/StationQ/Liquid for citing LIQUi|> in your publications.     =
0:0000.0/===========================================================================================
0:0000.0/
0:0000.3/     , 1,1.00e-002,  37,  50,0.74
0:0000.5/     , 1,1.00e-002,  74,  98,0.76
0:0000.8/     , 1,1.00e-002, 116, 146,0.79
0:0001.0/     , 1,1.00e-002, 151, 192,0.79
0:0001.3/     , 1,1.00e-002, 181, 229,0.79
0:0001.5/     , 1,1.00e-002, 215, 274,0.78
0:0001.8/     , 1,1.00e-002, 253, 319,0.79
0:0002.0/     , 1,1.00e-002, 288, 365,0.79
0:0002.3/     , 1,1.00e-002, 324, 413,0.78
0:0002.5/     , 1,1.00e-002, 356, 462,0.77
0:0002.7/FINAL, 1,1.00e-002, 386, 500,0.77
0:0002.7/
0:0002.7/HIST, #,     prob,gate,  ec,good, all,frac
0:0002.7/HIST, 1,1.00e-002,   0,   0,  81,  81,1.00
0:0002.7/HIST, 1,1.00e-002,   0,   1, 127, 147,0.86
0:0002.7/HIST, 1,1.00e-002,   0,   2,  83, 119,0.70
0:0002.7/HIST, 1,1.00e-002,   0,   3,  43,  73,0.59
0:0002.7/HIST, 1,1.00e-002,   0,   4,  18,  27,0.67
0:0002.7/HIST, 1,1.00e-002,   0,   5,   6,   8,0.75
0:0002.7/HIST, 1,1.00e-002,   0,   6,   1,   3,0.33
0:0002.7/HIST, 1,1.00e-002,   1,   0,   7,   7,1.00
0:0002.7/HIST, 1,1.00e-002,   1,   1,   9,  12,0.75
0:0002.7/HIST, 1,1.00e-002,   1,   2,   6,   9,0.67
0:0002.7/HIST, 1,1.00e-002,   1,   3,   2,   8,0.25
0:0002.7/HIST, 1,1.00e-002,   1,   4,   1,   4,0.25
0:0002.7/HIST, 1,1.00e-002,   1,   5,   1,   1,1.00
0:0002.7/HIST, 1,1.00e-002,   1,   6,   1,   1,1.00

运行fsi Teleport.fsx结果如下,并在当前目录下生成几个线路图

d:\git\Quantum\Liquid\Samples>fsi Teleport.fsx
0:0000.0/============ TELEPORT =============
0:0000.0/
0:0000.0/===========================================================================================
0:0000.0/=    The Language-Integrated Quantum Operations (LIQUi|>) Simulator                       =
0:0000.0/=        Copyright (c) 2015,2016 Microsoft Corporation                                    =
0:0000.0/=        If you use LIQUi|> in your research, please follow the guidelines at             =
0:0000.0/=        https://github.com/StationQ/Liquid for citing LIQUi|> in your publications.     =
0:0000.0/===========================================================================================
0:0000.0/
0:0000.0/Initial State:   (0.6879-0.1311i)|0>+  (-0.6325-0.331i)|1>
0:0000.0/Final   State:   (0.6879-0.1311i)|0>+  (-0.6325-0.331i)|1> (bits:10)
0:0000.0/Initial State:   (0.6104+0.1264i)|0>+ (-0.5312-0.5738i)|1>
0:0000.0/Final   State:   (0.6104+0.1264i)|0>+ (-0.5312-0.5738i)|1> (bits:11)
0:0000.0/Initial State:   (0.9241-0.3687i)|0>+(-0.09792-0.02151i)|1>
0:0000.0/Final   State:   (0.9241-0.3687i)|0>+(-0.09792-0.02151i)|1> (bits:00)
0:0000.0/Initial State:   (0.3004-0.9176i)|0>+  (0.2357-0.1104i)|1>
0:0000.0/Final   State:   (0.3004-0.9176i)|0>+  (0.2357-0.1104i)|1> (bits:11)
0:0000.0/Initial State:   (0.5621+0.2278i)|0>+  (0.4888+0.6271i)|1>
0:0000.0/Final   State:   (0.5621+0.2278i)|0>+  (0.4888+0.6271i)|1> (bits:10)
0:0000.0/Initial State:  (0.03274+0.7188i)|0>+  (0.2551+0.6459i)|1>
0:0000.0/Final   State:  (0.03274+0.7188i)|0>+  (0.2551+0.6459i)|1> (bits:11)
0:0000.0/Initial State:  (0.01858-0.6019i)|0>+ (-0.4498+0.6596i)|1>
0:0000.0/Final   State:  (0.01858-0.6019i)|0>+ (-0.4498+0.6596i)|1> (bits:10)
0:0000.0/Initial State: (-0.0005968+0.0668i)|0>+ (-0.9265-0.3703i)|1>
0:0000.0/Final   State: (-0.0005968+0.0668i)|0>+ (-0.9265-0.3703i)|1> (bits:10)
0:0000.0/Initial State:   (0.3081-0.3178i)|0>+  (0.6709-0.5949i)|1>
0:0000.0/Final   State:   (0.3081-0.3178i)|0>+  (0.6709-0.5949i)|1> (bits:00)
0:0000.0/Initial State:  (-0.1882+0.4273i)|0>+ (-0.5706-0.6756i)|1>
0:0000.0/Final   State:  (-0.1882+0.4273i)|0>+ (-0.5706-0.6756i)|1> (bits:00)
0:0000.0/==================================
0:0000.0/
0:0000.0/============= Rendering Teleport
0:0000.0/Writing: Teleport_CN.tex (split=100.00% scale=100.00%)
0:0000.0/     Doing columns:  0 - 12
0:0000.0/Writing: Teleport_CN.htm (split=100.00% scale=100.00%)
0:0000.0/Writing: Teleport_CF.tex (split=100.00% scale=100.00%)
0:0000.0/     Doing columns:  0 - 11
0:0000.0/Writing: Teleport_CF.htm (split=100.00% scale=100.00%)
0:0000.0/Writing: Teleport_CA.tex (split=100.00% scale=100.00%)
0:0000.0/     Doing columns:  0 -  9
0:0000.0/Writing: Teleport_CA.htm (split=100.00% scale=100.00%)
0:0000.0/Original circuit gates: 8
0:0000.0/   Grown circuit gates: 5
0:0000.0/Writing: Teleport_GF.tex (split=100.00% scale=100.00%)
0:0000.0/     Doing columns:  0 -  6
0:0000.0/Writing: Teleport_GF.htm (split=100.00% scale=100.00%)
0:0000.0/============ TELEPORT =============
0:0000.0/Initial State:  (-0.3184+0.7037i)|0>+  (0.2504+0.5837i)|1>
0:0000.0/Final   State:  (-0.3184+0.7037i)|0>+  (0.2504+0.5837i)|1> (bits:01)
0:0000.0/Initial State:   (0.6232-0.2237i)|0>+   (0.632+0.4027i)|1>
0:0000.0/Final   State:   (0.6232-0.2237i)|0>+   (0.632+0.4027i)|1> (bits:00)
0:0000.0/Initial State:  (-0.227-0.03214i)|0>+  (0.7949+0.5618i)|1>
0:0000.0/Final   State:  (-0.227-0.03214i)|0>+  (0.7949+0.5618i)|1> (bits:10)
0:0000.0/Initial State: (-0.01695-0.3695i)|0>+  (-0.7352+0.568i)|1>
0:0000.0/Final   State: (-0.01695-0.3695i)|0>+  (-0.7352+0.568i)|1> (bits:00)
0:0000.0/Initial State:    (0.792-0.4167i)|0>+(-0.09876-0.4351i)|1>
0:0000.0/Final   State:    (0.792-0.4167i)|0>+(-0.09876-0.4351i)|1> (bits:11)
0:0000.0/Initial State:     (0.493+0.702i)|0>+ (-0.1212+0.4995i)|1>
0:0000.0/Final   State:     (0.493+0.702i)|0>+ (-0.1212+0.4995i)|1> (bits:00)
0:0000.0/Initial State:   (0.3513-0.6676i)|0>+ (-0.4472-0.4805i)|1>
0:0000.0/Final   State:   (0.3513-0.6676i)|0>+ (-0.4472-0.4805i)|1> (bits:11)
0:0000.0/Initial State:  (0.01652-0.5942i)|0>+ (-0.6639+0.4538i)|1>
0:0000.0/Final   State:  (0.01652-0.5942i)|0>+ (-0.6639+0.4538i)|1> (bits:01)
0:0000.0/Initial State:  (0.09732+0.6044i)|0>+(-0.7876+0.07008i)|1>
0:0000.0/Final   State:  (0.09732+0.6044i)|0>+(-0.7876+0.07008i)|1> (bits:10)
0:0000.0/Initial State:    (0.6088+0.442i)|0>+  (0.3904+0.5307i)|1>
0:0000.0/Final   State:    (0.6088+0.442i)|0>+  (0.3904+0.5307i)|1> (bits:11)
0:0000.0/==================================
0:0000.0/
0:0000.0/Original circuit gates: 8
0:0000.0/   Grown circuit gates: 5
0:0000.0/Original circuit:
0:0000.0/Grown circuit:
0:0000.0/============ TELEPORT =============
0:0000.0/Initial State:  (-0.9072+0.1581i)|0>+  (-0.2299-0.315i)|1>
0:0000.0/Final   State:  (-0.9072+0.1581i)|0>+  (-0.2299-0.315i)|1> (bits:01)
0:0000.0/Initial State:  (-0.4781+0.2981i)|0>+ (-0.3102+0.7657i)|1>
0:0000.0/Final   State:  (-0.4781+0.2981i)|0>+ (-0.3102+0.7657i)|1> (bits:11)
0:0000.0/Initial State:   (0.4019-0.5127i)|0>+ (-0.7006+0.2911i)|1>
0:0000.0/Final   State:   (0.4019-0.5127i)|0>+ (-0.7006+0.2911i)|1> (bits:01)
0:0000.0/Initial State:    (-0.7866-0.57i)|0>+  (0.2367+0.0192i)|1>
0:0000.0/Final   State:    (-0.7866-0.57i)|0>+  (0.2367+0.0192i)|1> (bits:01)
0:0000.0/Initial State:   (0.5124+0.4628i)|0>+ (-0.04466-0.722i)|1>
0:0000.0/Final   State:   (0.5124+0.4628i)|0>+ (-0.04466-0.722i)|1> (bits:10)
0:0000.0/Initial State:  (-0.4578-0.5151i)|0>+(-0.7234+0.04164i)|1>
0:0000.0/Final   State:  (-0.4578-0.5151i)|0>+(-0.7234+0.04164i)|1> (bits:01)
0:0000.0/Initial State:  (-0.3448+0.6162i)|0>+ (-0.1059+0.7002i)|1>
0:0000.0/Final   State:  (-0.3448+0.6162i)|0>+ (-0.1059+0.7002i)|1> (bits:11)
0:0000.0/Initial State:  (-0.6637+0.5566i)|0>+ (-0.3887+0.3139i)|1>
0:0000.0/Final   State:  (-0.6637+0.5566i)|0>+ (-0.3887+0.3139i)|1> (bits:00)
0:0000.0/Initial State:    (-0.84-0.3667i)|0>+  (0.3781-0.1301i)|1>
0:0000.0/Final   State:    (-0.84-0.3667i)|0>+  (0.3781-0.1301i)|1> (bits:00)
0:0000.0/Initial State:  (-0.7456+0.2398i)|0>+   (0.5366+0.314i)|1>
0:0000.0/Final   State:  (-0.7456+0.2398i)|0>+   (0.5366+0.314i)|1> (bits:11)
0:0000.0/==================================
0:0000.0/

没有更多推荐了,返回首页