python使用ProjectQ生成量子算法指令集

輸出算法操作

首先介紹一個最基本的使用方法,就是使用ProjectQ來打印量子算法中所輸入的量子門操作,這裡使用到瞭ProjectQ中的DummyEngine後端用於保存操作的指令。比如最簡單的一個Bell State的制備,可以通過如下代碼實現,並且打印出所保存的基本操作:

from projectq import MainEngine
from projectq.cengines import DummyEngine
from projectq.ops import H, CX, All, Measure

backend = DummyEngine(save_commands=True)
eng = MainEngine(backend=backend)

qureg = eng.allocate_qureg(2)
H | qureg[0]
CX | (qureg[0], qureg[1])

All(Measure) | qureg
eng.flush(deallocate_qubits=True)

for cmd in backend.received_commands:
    print (cmd)

運行結果如下:

Allocate | Qureg[0]
H | Qureg[0]
Allocate | Qureg[1]
CX | ( Qureg[0], Qureg[1] )
Measure | Qureg[0]
Measure | Qureg[1]
Deallocate | Qureg[0]
Deallocate | Qureg[1]

這裡有一點需要註意的是,如果是單次運算,我們到Measure就可以結束瞭。但是如果同一個線程的任務還沒有結束的話,需要在Measure之後加上一個deallocate_qubits=True的配置項,用於解除當前分配的量子比特所占用的內存。

封裝的操作

在量子算法的實現中,我們可以用一些函數或者類來封裝一部分的量子算法操作指令,但是這可能會導致一個問題,那就是在ProjectQ上打印出來的操作指令沒有把封裝的模塊的內容輸出出來,比如如下的案例:

from projectq import MainEngine
from projectq.cengines import DummyEngine
from projectq.ops import H, CX, All, Measure, TimeEvolution, QubitOperator

backend = DummyEngine(save_commands=True)
eng = MainEngine(backend=backend)

qureg = eng.allocate_qureg(3)
H | qureg[0]
CX | (qureg[0], qureg[1])
TimeEvolution(1, QubitOperator('X2 X1')) | qureg

All(Measure) | qureg
eng.flush()

for cmd in backend.received_commands:
    print (cmd)

執行結果如下:

Allocate | Qureg[0]
H | Qureg[0]
Allocate | Qureg[1]
CX | ( Qureg[0], Qureg[1] )
Measure | Qureg[0]
Allocate | Qureg[2]
exp(-1j * (1.0 X0 X1)) | Qureg[1-2]
Measure | Qureg[1]
Measure | Qureg[2]

我們發現這裡的含時演化的操作算符沒有被分解,而是直接打印輸出瞭出來。但是如果在硬件系統中,隻能夠識別支持的指令操作,這裡的含時演化操作可能並未在量子硬件體系中被實現,因此我們就需要在將指令發送給量子硬件之前,就對其進行分解。

含時演化算符的分解

這裡我們直接調用ProjectQ的配置中的restrictedgateset方法進行操作分解,我們將單比特門操作的范圍放寬到所有的操作,但是雙比特操作隻允許CX操作,並將這個配置作為engin_list配置到ProjectQ的MainEngine中:

from projectq import MainEngine
from projectq.cengines import DummyEngine
from projectq.ops import H, CX, All, Measure, TimeEvolution, QubitOperator
from projectq.setups import restrictedgateset

engine_list = restrictedgateset.get_engine_list(one_qubit_gates="any",two_qubit_gates=(CX,))
backend = DummyEngine(save_commands=True)
eng = MainEngine(backend=backend,engine_list=engine_list)

qureg = eng.allocate_qureg(3)
H | qureg[0]
CX | (qureg[0], qureg[1])
TimeEvolution(1, QubitOperator('X2 X1')) | qureg

All(Measure) | qureg
eng.flush(deallocate_qubits=True)

for cmd in backend.received_commands:
    print (cmd)

打印輸出的結果如下:

Allocate | Qureg[0]
H | Qureg[0]
Allocate | Qureg[1]
CX | ( Qureg[0], Qureg[1] )
Measure | Qureg[0]
Allocate | Qureg[2]
H | Qureg[2]
H | Qureg[1]
CX | ( Qureg[1], Qureg[2] )
Rz(2.0) | Qureg[2]
CX | ( Qureg[1], Qureg[2] )
H | Qureg[1]
Measure | Qureg[1]
H | Qureg[2]
Measure | Qureg[2]
Deallocate | Qureg[0]
Deallocate | Qureg[1]
Deallocate | Qureg[2]

可以看到含時演化算符已經被分解並輸出瞭出來。由於已知單比特量子門加上一個CX是一個完備的量子門集合,因此一般我們可以直接使用這個集合來進行量子門操作指令集的限制。

QFT的分解

QFT是ProjectQ中所自帶支持的量子傅裡葉變換的量子門操作封裝,跟上一個章節中所介紹的含時演化算符類似的,我們可以用restrictedgateset來具體分解QFT算符:

from projectq import MainEngine
from projectq.cengines import DummyEngine
from projectq.ops import H, CX, All, Measure, TimeEvolution, QubitOperator, QFT
from projectq.setups import restrictedgateset

engine_list = restrictedgateset.get_engine_list(one_qubit_gates="any",two_qubit_gates=(CX,))
backend = DummyEngine(save_commands=True)
eng = MainEngine(backend=backend,engine_list=engine_list)

qureg = eng.allocate_qureg(3)
H | qureg[0]
CX | (qureg[0], qureg[1])
QFT | qureg

All(Measure) | qureg
eng.flush(deallocate_qubits=True)

for cmd in backend.received_commands:
    print (cmd)

輸出的結果如下:

Allocate | Qureg[2]
Allocate | Qureg[1]
H | Qureg[2]
Rz(0.785398163398) | Qureg[2]
Allocate | Qureg[0]
H | Qureg[0]
CX | ( Qureg[0], Qureg[1] )
R(0.785398163398) | Qureg[1]
CX | ( Qureg[1], Qureg[2] )
Rz(11.780972450962) | Qureg[2]
CX | ( Qureg[1], Qureg[2] )
R(0.392699081698) | Qureg[0]
Rz(0.392699081698) | Qureg[2]
CX | ( Qureg[0], Qureg[2] )
H | Qureg[1]
Rz(12.173671532661) | Qureg[2]
CX | ( Qureg[0], Qureg[2] )
R(0.785398163398) | Qureg[0]
Rz(0.785398163398) | Qureg[1]
CX | ( Qureg[0], Qureg[1] )
Rz(11.780972450962) | Qureg[1]
CX | ( Qureg[0], Qureg[1] )
H | Qureg[0]
Measure | Qureg[0]
Measure | Qureg[1]
Measure | Qureg[2]
Deallocate | Qureg[1]
Deallocate | Qureg[2]
Deallocate | Qureg[0]

如果2比特門操作也不加以限制的化,ProjectQ中會自動選取最簡易的分解形式:

from projectq import MainEngine
from projectq.cengines import DummyEngine
from projectq.ops import H, CX, All, Measure, TimeEvolution, QubitOperator, QFT
from projectq.setups import restrictedgateset

engine_list = restrictedgateset.get_engine_list(one_qubit_gates="any",two_qubit_gates="any")
backend = DummyEngine(save_commands=True)
eng = MainEngine(backend=backend,engine_list=engine_list)

qureg = eng.allocate_qureg(3)
H | qureg[0]
CX | (qureg[0], qureg[1])
QFT | qureg

All(Measure) | qureg
eng.flush(deallocate_qubits=True)

for cmd in backend.received_commands:
    print (cmd)

輸出結果如下:

Allocate | Qureg[0]
Allocate | Qureg[1]
H | Qureg[0]
CX | ( Qureg[0], Qureg[1] )
Allocate | Qureg[2]
H | Qureg[2]
CR(1.570796326795) | ( Qureg[1], Qureg[2] )
CR(0.785398163397) | ( Qureg[0], Qureg[2] )
H | Qureg[1]
CR(1.570796326795) | ( Qureg[0], Qureg[1] )
H | Qureg[0]
Measure | Qureg[0]
Measure | Qureg[1]
Measure | Qureg[2]
Deallocate | Qureg[1]
Deallocate | Qureg[2]
Deallocate | Qureg[0]

可以發現使用瞭CR來替代CX之後,分解出來的線路會更加的簡短。

總結概要

本文主要從工程實現的角度,講解在ProjectQ開源量子計算模擬器框架中,實現量子門操作分解與輸出的方法。通過這個方法,可以限制量子指令集的范圍,將量子算法中不被支持的量子門操作等價(或近似地)變化到量子硬件體系所支持的量子指令集上。

以上就是python使用ProjectQ生成量子算法指令集的詳細內容,更多關於python 用ProjectQ生成算法指令集的資料請關註WalkonNet其它相關文章!

推薦閱讀: