# 建立量子邏輯閘 在上一節,我們學了如何用 Pennylane 建立量子電路並執行的基本流程,這節我們將針對量子電路部分,說明各種 quantum gate 的語法,讓你可以用 Pennylane 建立複雜的 circuit。 還不熟悉 quantum gate 的讀者可以先看[量子計算(上)](https://www.entangletech.tw/lesson/basic-algorithm-08)系列文章 ## Single qubit quantum gate 單 qubit quantum gate 的基本語法都是這樣的形式: ```python=+ qml.{gate}(wires = number) ``` 中間 {gate} 代表要放的 quantum gate,後面的 number 是純數字,代表你要把這個 quantum gate 放在哪個 qubit 上。以下介紹常用 quantum gate 的語法 ### I gate ```python=+ qml.Identity(wires = 0) # 對第一個 qubit 做 I gate ``` <div style="text-align: center;"> <img src="https://hackmd.io/_uploads/BJuEmY4hC.svg" alt="I gate" width="40%"/> <br> <p>I gate 的符號 </p> </div> ### X gate ```python=+ qml.PauliX(wires = 0) # 對第一個 qubit 做 X gate ``` <div style="text-align: center;"> <img src="https://hackmd.io/_uploads/H1QEzVP2A.svg" alt="X gate" width="40%"/> <br> <p>X gate 的符號 </p> </div> ### Y gate ```python=+ qml.PauliY(wires = 0) # 對第一個 qubit 做 Y gate ``` <div style="text-align: center;"> <img src="https://hackmd.io/_uploads/HJTj7YVnR.svg" alt="Y gate" width="40%"/> <br> <p>Y gate 的符號 </p> </div> ### Z gate ```python=+ qml.PauliZ(wires = 0) # 對第一個 qubit 做 Z gate ``` <div style="text-align: center;"> <img src="https://hackmd.io/_uploads/B17lEK42R.svg" alt="Z gate" width="40%"/> <br> <p>Z gate 的符號 </p> </div> ### H gate ```python=+ qml.Hadamard(wires = 0) # 對第一個 qubit 做 H gate ``` <div style="text-align: center;"> <img src="https://hackmd.io/_uploads/SJXMVYVhR.svg" alt="H gate" width="40%"/> <br> <p>H gate 的符號 </p> </div> ### S gate ```python=+ qml.S(wires = 0) # 對第一個 qubit 做 S gate ``` <div style="text-align: center;"> <img src="https://hackmd.io/_uploads/rJtV4YEh0.svg" alt="S gate" width="40%"/> <br> <p>S gate 的符號 </p> </div> ### T gate ```python=+ qml.T(wires = 0) # 對第一個 qubit 做 S gate ``` <div style="text-align: center;"> <img src="https://hackmd.io/_uploads/SkT44KN3R.svg" alt="T gate" width="40%"/> <br> <p>T gate 的符號 </p> </div> ## Single qubit rotation gate 以下是 RX, RY 與 RZ gate,可以自訂要旋轉的角度,語法通常是: ```python=+ qml.{gate}(phi = theta, wires = number) ``` 括號中第一個參數 phi 是欲旋轉的角度。 ### RX gate ```python=+ qml.RX(phi = thata, wires=0) # 對第一個 qubit 做 RX gate,旋轉角度 theta ``` 這邊要注意的是,旋轉角度 theta,在矩陣計算的時候,要記得 theta 要除以 2,常常寫程式會忽略這點。 \begin{split} RX(\theta)= \begin{bmatrix} \cos{\frac{\theta}{2}} & -i\sin{\frac{\theta}{2}} \\ -i\sin{\frac{\theta}{2}} & \cos{\frac{\theta}{2}} \end{bmatrix} \end{split} <div style="text-align: center;"> <img src="https://hackmd.io/_uploads/SkJYjzvhR.svg" alt="RX gate" width="40%"/> <br> <p>RX gate 的符號 </p> </div> ### RY gate ```python=+ qml.RY(phi = thata, wires=0) # 對第一個 qubit 做 RY gate,旋轉角度 theta ``` <div style="text-align: center;"> <img src="https://hackmd.io/_uploads/BkwAiMv3R.svg" alt="RY gate" width="40%"/> <br> <p>RY gate 的符號 </p> </div> ### RZ gate ```python=+ qml.RZ(phi = thata, wires=0) # 對第一個 qubit 做 RZ gate,旋轉角度 theta ``` <div style="text-align: center;"> <img src="https://hackmd.io/_uploads/B1ZZ2fPh0.svg" alt="RZ gate" width="40%"/> <br> <p>RZ gate 的符號 </p> </div> ## Two qubits quantum gate 語法通常如下: ```python=+ qml.{gate}(wires = [number,number ]) ``` 因為是牽涉兩個 qubits 的 quantum gate,wires 會是一個 list,第一個數字通常都是 control qubit,第二個 qubit 都是 target qubit。 ### CNOT gate ```python=+ qml.CNOT(wires=[control,target]) ``` <div style="text-align: center;"> <img src="https://hackmd.io/_uploads/Sy6yX_u2C.svg" alt="CX gate" width="30%"/> <br>CNOT (CX) gate 的符號 <p> </p> </div> ### CY gate ```python=+ qml.CY(wires=[control,target]) ``` ### CZ gate ```python=+ qml.CZ(wires=[control,target]) ``` <div style="text-align: center;"> <img src="https://hackmd.io/_uploads/BJZl4OO2C.svg" alt="CZ gate" width="60%"/> <br>CZ gate 的符號 <p> </p> </div> ### SWAP ```python=+ qml.SWAP(wires=[target,target]) ``` <div style="text-align: center;"> <img src="https://hackmd.io/_uploads/Sy_PBu_nR.svg" alt="SWAP gate" width="40%"/> <br>SWAP gate 的符號 <p> </p> </div> ### Controlled RX gate ```python=+ qml.CRX(wires=[control,target]) ``` 其他 Controlled RY/RZ 也是類似的語法,CRY 和 CRZ。 ## Multiple qubits quantum gate ### Toffoli gate ```python=+ qml.Toffoli(wires=[control, control, target]) ``` <div style="text-align: center;"> <img src="https://hackmd.io/_uploads/rJLc4__nC.svg" alt="CCNOT gate" width="25%"/> <br>Toffoli (CCNOT) gate 的符號 <p> </p> </div> ### Fredkin gate ```python=+ qml.CSWAP(wires=[control, target, target]) ``` <div style="text-align: center;"> <img src="https://hackmd.io/_uploads/B1L8Z1IYkg.svg" alt="CSWAP gate" width="25%"/> <br>Fredkin (CSWAP) gate 的符號 <p>圖片有畫錯,待我們更新 </p> </div>