
Komputasi kuantum di pytorch
Lebih cepat, dapat diskalakan, debugging mudah, penyebaran mudah di mesin sungguhan
Simulasi perhitungan kuantum pada perangkat keras klasik menggunakan pytorch. Ini mendukung simulasi statevector dan simulasi pulsa pada GPU. Ini dapat meningkatkan simulasi 30+ qubit dengan beberapa GPU.
Peneliti tentang desain algoritma kuantum, pelatihan sirkuit kuantum yang diparameterisasi, kontrol optimal kuantum, pembelajaran mesin kuantum, jaringan saraf kuantum.
Grafik komputasi dinamis, komputasi gradien otomatis, dukungan GPU cepat, pemrosesan model batch.
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 )Kami juga menyiapkan banyak contoh dan tutorial menggunakan Torchquantum.
Untuk tingkat awal , Anda dapat memeriksa QNN untuk MNIST, konvolusi kuantum (Quanvolution) dan metode kernel kuantum, dan regresi kuantum.
Untuk tingkat menengah , Anda dapat memeriksa pengkodean amplitudo untuk MNIST, Clifford Gate QNN, Simpan dan Muat Model QNN, Operasi Paulus, Cara Mengonversi TQ ke Qiskit.
Untuk ahli , Anda dapat memeriksa pelatihan Parameter Shift on-chip, pemangkasan gradien VQA, VQE, VQA untuk preprasi negara, QAOA (algoritma optimasi perkiraan kuantum).
Bangun model sirkuit kuantum parameterisasi sesederhana membangun model pytorch normal.
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 Latih sirkuit kuantum untuk melakukan tugas VQE. Komputer Quantum Quito Seperti dalam skrip Simple_VQE.PY:
cd examples / vqe
python vqe . py Latih sirkuit kuantum untuk melakukan tugas klasifikasi mnist dan menggunakan komputer kuantum quito IBM asli seperti dalam mnist_example.py skrip:
cd examples / mnist
python mnist . py | Mengajukan | Keterangan |
|---|---|
| perangkat.py | Kelas QuantumDevice yang menyimpan statevector |
| encoding.py | Lapisan pengkodean untuk menyandikan nilai klasik ke domain kuantum |
| fungsional.py | Fungsi Gerbang Quantum |
| operator.py | Kelas Gerbang Quantum |
| Layers.py | Templat layer seperti RandomLayer |
| ukur.py | Pengukuran status kuantum untuk mendapatkan nilai klasik |
| graph.py | Grafik gerbang kuantum digunakan dalam mode statis |
| super_layer.py | Lapisan Templat untuk Sirkit Super |
| Plugin/Qiskit* | Konversi dan prosesor untuk penempatan yang mudah di IBMQ |
| contoh/ | Lebih banyak contoh untuk melatih model QML dan VQE |
Torchquantum menggunakan kait pra-komit untuk memastikan konsistensi gaya Python dan mencegah kesalahan umum dalam basis kode.
Untuk mengaktifkannya Hooks pra-komit, silakan bereproduksi:
pip install pre-commit
pre-commit installconcurrent untuk Qiskit)Forum Torchquantum
Hanrui wang [email protected]
Jiannan Cao, Jessica Ding, Jiai Gu, Song Han, Zhirui Hu, Zirui Li, Zhiding Liang, Pengyu Liu, Liu Yilian, Mohammadreza Tavasoli, Hanrui Wang, Zhepeng Wang, Zhuoyang Ye, Hanrui, Zhepeng Wang, Zhuoyang Ye, Hanrui, Zhepeng Wang, 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}
}