ProjectQ和IBMQ量子计算机

IBMQ提供了使用量子计算机的接口,ProjectQ用Python语言提供了一套量子计算描述语言,能够用于包含IBMQ真实量子计算机以及量子的经典模拟(通过算符运算)在内的多个后台。

由于教学(让学生做习题并且能够在实际量子计算机上验证)和研究(考虑量子博弈的真人实验)的需要,需要用量子计算机。就学习了一下。发现,非常简单易用,也很有系统性。

首先,在量子计算中,最基本的量子单位是自旋。于是,自旋的状态(二维Hilbert空间矢量)和算符(二维Hilbert空间矢量上的算符,例如Pauli矩阵、Hadamard门、围绕Z轴的旋转、测量等),就是这个ProjectQ的基本单元。当然,一些常用的高级功能例如CNOT门,以及Hadamard门和CNOT合起来的作用——称作纠缠算符等,也在ProjectQ中做了实现。

具体的文档见ProjectQIBMQ上的说明。下面用一个例子来说明一下整体结构,这样以后再一次学习起来简单。

这是我试着编写的程序quantum.py:

  1. 导入后台(模拟器、IBMQ、tex)和算符(H、测量、CNOT)

  2. from projectq import MainEngine
    from projectq.ops import H, Measure, CNOT
    from projectq.backends import CircuitDrawer
    import projectq.setups.ibm
    from projectq.backends import IBMBackend

  3. 这部分接收程序运行参数,在这里就是决定用什么后台。

  4. import sys, getopt

    def main(argv):
    s=1 #indicator of running this program via simulator or not
    tex=0 #indicator of converting this program into tex or not
    IBMQ=0 #indicator of running this program via IBMQ or not
    #take parameters from command-line input
    try:
    opts, args = getopt.getopt(argv, "s:", ["tex=", "IBMQ="])
    except getopt.GetoptError:
    print ("Error: please use the command as quantum.py -s --tex --IBMQ ")
    sys.exit(2)
    for opt, arg in opts:
    if opt == "-h":
    print("quantum.py -r --tex --IBMQ ")
    sys.exit()
    elif opt =="-s":
    s = int(arg) #when s=1, we need to run this program via simulator
    elif opt == "--IBMQ":
    IBMQ = int(arg) # when tex=1, we need to convert this program into a tex file
    s = 0 #when tex=1, we set s=0 (not to run this program in force)
    elif opt == "--tex":
    tex = int(arg) # when tex=1, we need to convert this program into a tex file
    s = 0 #when tex=1, we set s=0 (not to run this program in force)
    IBMQ = 0 #when tex=1, we set IBMQ=0 (not to run this program in force)
    #parameter input ends here

  5. 按照运行参数,开辟一个针对相应后台的环境。在这个环境之上,所有的命令不再需要关心后台的问题。

  6. # create a main compiler engine
    if(tex):
    drawing_engine = CircuitDrawer()
    eng = MainEngine(drawing_engine)
    elif(s):
    eng = MainEngine()
    elif(IBMQ):
    eng = MainEngine(IBMBackend(use_hardware=True, num_runs=1024, verbose=False, device='ibmqx4'))
    # allocate 2 qubit

  7. 在环境里面初始化qubit,并且把需要的算符一个个作用到这些个qubit上。

  8. qunum = eng.allocate_qureg(2)

    # put qubit 1 in superposition
    H | qunum[0]
    # put the two qubit in engtanglement
    CNOT | (qunum[0],qunum[1])
    # measure
    Measure | (qunum[0],qunum[1])

  9. 运行这些初始化状态和算符。

  10. eng.flush()

  11. 输出结果。

  12. if(tex):
    print(drawing_engine.get_latex())
    if(s):
    print("q1={}".format(int(qunum[0])))
    print("q2={}".format(int(qunum[1])))
    if(IBMQ):
    # access the probabilities via the back-end:
    results = eng.backend.get_probabilities(qunum)
    for state in results:
    print("Measured {} with p = {}.".format(state, results[state]))
    # return one (random) measurement outcome.
    return [int(q) for q in qunum]

最后运行python quantum.py就可以在不同的后台运行了。如果需要在IBMQ上运行,需要IBMQ的帐号和密码。