如何安装量子计算编程工具Pyquil

之前我在这篇文章中介绍了如何安装IBM的量子计算编程工具QISKit,这次想要介绍一下另一种基于Python的工具,是由美国的量子计算公司Rigetti开发的Pyquil。这个框架相比于QISKit更加小众一点,但是上手更简单。同时Pyquil也像QISKit一样可以实现对量子电路的模拟,也可以在他们开发的量子芯片(云端)上运行结果。需要注意的是,目前Pyquil没有Windows版本,只有Mac和Linux版。这里给出的是Pyquil在Ubuntu虚拟机上的安装教程,然后也给出了一个简单的Bell State电路作为例程。更多的信息大家可以搜索Pyquil的documentation。

具体的教程如下(抱歉是英文的):

Installation of Pyquil

1. Install anaconda3

After the installation, if here you meet No module called conda after typing conda, or cannot create or activate a conda enviroment, you need to add conda to path. Thus you need to add a few lines into your bashrc file.

Step1. Use gedit ~/.bashrc to open the file

Step2. Add these lines into the file:

# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$(CONDA_REPORT_ERRORS=false '/home/yourusername/anaconda3/bin/conda' shell.bash hook 2> /dev/null)"
if [ $? -eq 0 ]; then
 \eval "$__conda_setup"
else
 if [ -f "/home/yourusername/anaconda3/etc/profile.d/conda.sh" ]; then
     . "/home/yourusername/anaconda3/etc/profile.d/conda.sh"
     CONDA_CHANGEPS1=false conda activate base
 else
     \export PATH="/home/yourusername/anaconda3/bin:$PATH"
 fi
fi
unset __conda_setup
# <<< conda init <<<

Remember to change all the yourusername into yours.

2. Create a new environment for Pyquil

conda create -n pyquil python=3.7

3. Activate the new environment

conda activate pyquil

4. Install Pyquil

pip install pyquil

The installation may fail because the lack of gcc, then you need to install it by:

sudo apt install gcc

5. Download the QVM and Compiler

The Forest 2.0 Downloadable SDK Preview currently contains (from the reference):

The Rigetti Quantum Virtual Machine (qvm) which allows high-performance simulation of Quil programs

The Rigetti Quil Compiler (quilc) which allows compilation and optimization of Quil programs to native gate sets

You can request the downloadable SDK from Rigetti Forest’s website. Enter your email, and a link for the SDK will be sent to your email.

6. Install the QVM and Compiler

Here we choose to install the QVM and Compiler on Linux (.deb). Change to the directory of the file you just downloaded in 5. Unpack the file and open it by:

tar -xf forest-sdk-linux-deb.tar.bz2

cd forest-sdk-2.0rc2-linux-deb

The installation will start after this line:

sudo ./forest-sdk-2.0rc2-linux-deb.run

Then you have finished the process of installing the QVM and the compiler quilc. You can check their version by:

qvm --version

quilc --version

Reference:

The documentation for Pyquil 2.0.

A simple program using Pyquil

Realization of the Bell State

from pyquil import Program, get_qc
from pyquil.gates import *
# construct a Bell State program
p = Program(H(0), CNOT(0, 1))

To run the cell below, you need to open two new terminals, and each of them run:

qvm -S

and

quilc -S,

which can enable Pyquil to run locally.

# run the program on a QVM
qc = get_qc('9q-square-qvm')
result = qc.run_and_measure(p, trials=10)
print(result[0])
print(result[1])
[0 1 0 0 1 0 1 0 0 0]
[0 1 0 0 1 0 1 0 0 0]

The call to run_and_measure will make a request to the two servers we started up in the previous section: first, to the quilc server instance to compile the Quil program into native Quil, and then to the qvm server instance to simulate and return measurement results of the program 10 times. If you open up the terminal windows where your servers are running, you should see output printed to the console regarding the requests you just made.

你可能感兴趣的:(量子计算)