微軟基於F 的 Liqui|> 量子編程語言
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:installMicrosoft Visual Studio2017Community
:: VsDevCmd.bat目錄關係如下:
d:installMicrosoft Visual Studio2017CommunityCommon7ToolsVsDevCmd.bat
::
git clone https://github.com/StationQ/Liquid.git
:: 進入預編譯目錄
cd Liquidin
:: 運行demo,__Teleport()是內置函數,內置函數都帶__
D:gitQuantumLiquidin>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/===================================================================================================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
03 運行一個量子隱形傳態demo
03.01 腳本模式運行
下面是 《量子演算法與編程入門》 3.3.2 例3.6 腳本模式的例子
UserScript.fsx
#if INTERACTIVE
#r @"..inLiquid1.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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
把上面的UserScript.fsx文件放到Liquidin目錄中,如下運行即可。
運行結果如下:
d:gitQuantumLiquidin>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>]
1
2
3
4
5
6
7
8
9
10
11
12
03.02 自定義函數方式運行
用vs2017打開D:gitQuantumLiquidsourceLiquid.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:gitQuantumLiquidsourceinDebugLiquid.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 ()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
編譯後(注意編譯後的生成目錄sourceinDebug),運行結果如下:
d:gitQuantumLiquidsourceinDebug>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 ================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 ()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
編譯後運行:Liquid.exe CircuitDiagram(),結果如下:
d:gitQuantumLiquidsourceinDebug>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 ================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
在d:gitQuantumLiquidsourceinDebug目錄下生成線路圖:
D:gitQuantumLiquidsourceinDebug eleport.htm
D:gitQuantumLiquidsourceinDebug eleport.tex
打開teleport.htm文件內容選下:
04 Liqui|>自帶demo
LiquiddocsLiquid.chm 是類和介面說明文檔。
LiquiddocsLIQUiD.pdf 是Liqui|>的說明文檔。
LiquiddocsTutorial.md 是幾個demo的說明。
LiquidSamples 下面的幾個demo運行模式可以參考 Kraus.cmd裡面的命令。
:: 用VsDevCmd.bat啟動命令行(注意找到自己的vs2017的安裝路徑):
d:installMicrosoft Visual Studio2017CommunityCommon7ToolsVsDevCmd.bat
cd D:gitQuantumLiquidSamples
:: 比如運行如下命令:
d:gitQuantumLiquidSamples>fsi Noise1.fsx
1
2
3
4
5
運行結果:
d:gitQuantumLiquidSamples>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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
運行fsi Teleport.fsx結果如下,並在當前目錄下生成幾個線路圖
d:gitQuantumLiquidSamples>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/
---------------------
作者:longji
原文:https://blog.csdn.net/longji/article/details/85530666
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


※LeetCode中那些應該背下來的經典代碼
※Python 特殊函數(lambda,map,filter,reduce)
TAG:程序員小新人學習 |