
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}
}