
Pytorch中的量子計算
更快,可擴展,輕鬆調試,在真實機器上輕鬆部署
使用Pytorch在經典硬件上模擬量子計算。它支持GPU上的狀態向量模擬和脈沖模擬。它可以擴展到具有多個GPU的30個以上量子位的模擬。
量子算法設計,參數化量子電路訓練,量子最佳控制,量子機學習,量子神經網絡的研究人員。
動態計算圖,自動梯度計算,快速GPU支持,批處理模型變形處理。
git clone https://github.com/mit-han-lab/torchquantum.git
cd torchquantum
pip install --editable . import torchquantum as tq
import torchquantum . functional as tqf
qdev = tq . QuantumDevice ( n_wires = 2 , bsz = 5 , device = "cpu" , record_op = True ) # use device='cuda' for GPU
# use qdev.op
qdev . h ( wires = 0 )
qdev . cnot ( wires = [ 0 , 1 ])
# use tqf
tqf . h ( qdev , wires = 1 )
tqf . x ( qdev , wires = 1 )
# use tq.Operator
op = tq . RX ( has_params = True , trainable = True , init_params = 0.5 )
op ( qdev , wires = 0 )
# print the current state (dynamic computation graph supported)
print ( qdev )
# obtain the qasm string
from torchquantum . plugin import op_history2qasm
print ( op_history2qasm ( qdev . n_wires , qdev . op_history ))
# measure the state on z basis
print ( tq . measure ( qdev , n_shots = 1024 ))
# obtain the expval on a observable by stochastic sampling (doable on simulator and real quantum hardware)
from torchquantum . measurement import expval_joint_sampling
expval_sampling = expval_joint_sampling ( qdev , 'ZX' , n_shots = 1024 )
print ( expval_sampling )
# obtain the expval on a observable by analytical computation (only doable on classical simulator)
from torchquantum . measurement import expval_joint_analytical
expval = expval_joint_analytical ( qdev , 'ZX' )
print ( expval )
# obtain gradients of expval w.r.t. trainable parameters
expval [ 0 ]. backward ()
print ( op . params . grad )
# Apply gates to qdev with tq.QuantumModule
ops = [
{ 'name' : 'hadamard' , 'wires' : 0 },
{ 'name' : 'cnot' , 'wires' : [ 0 , 1 ]},
{ 'name' : 'rx' , 'wires' : 0 , 'params' : 0.5 , 'trainable' : True },
{ 'name' : 'u3' , 'wires' : 0 , 'params' : [ 0.1 , 0.2 , 0.3 ], 'trainable' : True },
{ 'name' : 'h' , 'wires' : 1 , 'inverse' : True }
]
qmodule = tq . QuantumModule . from_op_history ( ops )
qmodule ( qdev )我們還使用Torchquantum準備了許多示例和教程。
對於開始級別,您可以檢查QNN中的MNIST,量子卷積(Quanvolution)和量子內核方法以及量子回歸。
對於中間級別,您可以檢查MNIST,Clifford Gate QNN,保存和加載QNN型號,Paulisum操作,如何將TQ轉換為Qiskit的振幅編碼。
對於專家,您可以檢查片上的參數移位訓練,VQA梯度修剪,VQE,VQA進行狀態預療法,QAOA(量子近似優化算法)。
構造參數化的量子電路模型與構建正常的Pytorch模型一樣簡單。
import torch . nn as nn
import torch . nn . functional as F
import torchquantum as tq
import torchquantum . functional as tqf
class QFCModel ( nn . Module ):
def __init__ ( self ):
super (). __init__ ()
self . n_wires = 4
self . measure = tq . MeasureAll ( tq . PauliZ )
self . encoder_gates = [ tqf . rx ] * 4 + [ tqf . ry ] * 4 +
[ tqf . rz ] * 4 + [ tqf . rx ] * 4
self . rx0 = tq . RX ( has_params = True , trainable = True )
self . ry0 = tq . RY ( has_params = True , trainable = True )
self . rz0 = tq . RZ ( has_params = True , trainable = True )
self . crx0 = tq . CRX ( has_params = True , trainable = True )
def forward ( self , x ):
bsz = x . shape [ 0 ]
# down-sample the image
x = F . avg_pool2d ( x , 6 ). view ( bsz , 16 )
# create a quantum device to run the gates
qdev = tq . QuantumDevice ( n_wires = self . n_wires , bsz = bsz , device = x . device )
# encode the classical image to quantum domain
for k , gate in enumerate ( self . encoder_gates ):
gate ( qdev , wires = k % self . n_wires , params = x [:, k ])
# add some trainable gates (need to instantiate ahead of time)
self . rx0 ( qdev , wires = 0 )
self . ry0 ( qdev , wires = 1 )
self . rz0 ( qdev , wires = 3 )
self . crx0 ( qdev , wires = [ 0 , 2 ])
# add some more non-parameterized gates (add on-the-fly)
qdev . h ( wires = 3 )
qdev . sx ( wires = 2 )
qdev . cnot ( wires = [ 3 , 0 ])
qdev . qubitunitary ( wires = [ 1 , 2 ], params = [[ 1 , 0 , 0 , 0 ],
[ 0 , 1 , 0 , 0 ],
[ 0 , 0 , 0 , 1j ],
[ 0 , 0 , - 1j , 0 ]])
# perform measurement to get expectations (back to classical domain)
x = self . measure ( qdev ). reshape ( bsz , 2 , 2 )
# classification
x = x . sum ( - 1 ). squeeze ()
x = F . log_softmax ( x , dim = 1 )
return x 訓練量子電路執行VQE任務。 Quito Quantum計算機如simple_vqe.py腳本:
cd examples / vqe
python vqe . py 訓練量子電路執行MNIST分類任務,並在MNIST_EXAMPLE.PY腳本中使用真實的IBM Quito Quantum Computer上部署
cd examples / mnist
python mnist . py | 文件 | 描述 |
|---|---|
| 裝置 | 存儲國家向量的Quantumdevice類 |
| encoding.py | 編碼圖層以編碼經典值與量子域 |
| 功能 | 量子門功能 |
| 操作員 | 量子門類 |
| layers.py | 圖層模板,例如Randomlayer |
| py | 量子狀態的測量以獲取經典價值 |
| graph.py | 靜態模式下使用的量子門圖 |
| super_layer.py | 超循環的層模板 |
| 插件/Qiskit* | 轉換器和處理器,以便在IBMQ上輕鬆部署 |
| 例子/ | 培訓QML和VQE模型的更多示例 |
Torchquantum使用預加入鉤子來確保Python樣式的一致性並防止其代碼庫中的常見錯誤。
要啟用預先承諾的掛鉤,請複制:
pip install pre-commit
pre-commit installconcurrent發包問題)Torchquantum論壇
Hanrui Wang [email protected]
Jiannan Cao,Jessica Ding,Jiai GU,Song Han,Zhirui Hu,Zirui Li,Zhiding Liang,Pengyu Liu,Yilian Liu,Mohammadreza Tavasoli,Hanrui Wang,Zhepeng Wang,Zhuoyang Ye,Zhuoyang Ye
@inproceedings{hanruiwang2022quantumnas,
title = {Quantumnas: Noise-adaptive search for robust quantum circuits},
author = {Wang, Hanrui and Ding, Yongshan and Gu, Jiaqi and Li, Zirui and Lin, Yujun and Pan, David Z and Chong, Frederic T and Han, Song},
booktitle = {The 28th IEEE International Symposium on High-Performance Computer Architecture (HPCA-28)},
year = {2022}
}