Deutsch-Jozsa算法试验

版权声明:技术分享,csdn longji https://blog.csdn.net/longji/article/details/85859201

01 Deutsch-Jozsa算法在IBM Q上的试验

算法根据《量子算法与编程入门》 4.2 Deutsch-Jozsa算法,4.2.1 IBM Quantum Experience 介绍。
完全按照原书说明在模拟器上得到大约各25%的概率。得到一个高概率的需要做些修改。
cx q[2],q[4] 在ibmqx4的五线谱没使用成功,只能使用 cx q[3], q[4]

01.01 通过API调用方式,等概率

deutsch-jozsa01.py

# deutsch-jozsa.py
# 《量子算符与编程入门》 4.2 Deutsch-Jozsa算法
# 基于QISKit实现Deutsch-Jozsa算法
#mytoken = "mytoken"
myconfig = {
    "url":'https://quantumexperience.ng.bluemix.net/api'
}
from IBMQuantumExperience import *
api = IBMQuantumExperience(token=mytoken, config=myconfig)

# 每行前面不要有空格和tab键
# code代码的格式要求比较严格
# 1 include "qelib1.inc" 与引号之间对多只能有一个回车换号符
code = '''include "qelib1.inc";
    qreg q[5];
    creg c[5];
    
    x q[4];
    h q[2];
    h q[3];
    h q[4];
    barrier q[1],q[2],q[3],q[4];
    h q[4];
    cx q[3],q[4];
    tdg q[4];
    cx q[2],q[4];
    t q[4];
    cx q[3],q[4];
    tdg q[4];
    cx q[2],q[4];
    tdg q[3];
    t q[4];
    h q[2];
    h q[3];
    cx q[3],q[2];
    h q[4];
    h q[2];
    h q[3];
    tdg q[3];
    h q[2];
    h q[3];
    cx q[3],q[2];
    h q[2];
    h q[3];
    t q[2];
    s q[3];
    barrier q[1],q[2],q[3],q[4];
    h q[2];
    h q[3];
    measure q[2] -> c[2];
    measure q[3] -> c[3];
'''
# 我申请的个人账号没有使用物理芯片的权限
#data = api.run_experiment(qasm=code, backend='ibmqx4', shots=1024, name=None, timeout=180, access_token=mytoken)
data = api.run_experiment(qasm=code, backend='simulator', shots=1024, name=None)

result = data['result']['measure']
label = result['labels']
value = result['values']
print("测量结果:")
print(label)
print("对应的概率为:")
print(value)

print("data的完整返回结果:")
print(data)

import matplotlib.pyplot as plt
plt.bar(label, value)
plt.show()

运行结果:

测量结果:
['00100', '00000', '01000', '01100']
对应的概率为:
[0.2333984375, 0.2626953125, 0.24609375, 0.2578125]
data的完整返回结果:
{'status': 'DONE', 'idExecution': '5c307007a983fa005652776b', 'idCode': '5c307007a983fa005652776a', 'result': {'extraInfo': {'seed': 4273156216}, 'measure': {'qubits': [2, 3], 'labels': ['00100', '00000', '01000', '01100'], 'values': [0.2333984375, 0.2626953125, 0.24609375, 0.2578125]}}}

在ibmq官网日志的数据:
在这里插入图片描述

01.02 通过五线谱方式

如果在五线谱上设计,cx q[2],q[4]是被禁止的,不成功;只能修改为cx q[3],q[4]。
这样修改后,和《量子算法与编程入门》 4.2 Deutsch-Jozsa算法,4.2.1的结果数据一致。
deutsch-jozsa02.py

# deutsch-jozsa.py
# 《量子算符与编程入门》 4.2 Deutsch-Jozsa算法
# 基于QISKit实现Deutsch-Jozsa算法
# 这个token需要自己申请下
#mytoken = "mytoken"
myconfig = {
    "url":'https://quantumexperience.ng.bluemix.net/api'
}
from IBMQuantumExperience import *
api = IBMQuantumExperience(token=mytoken, config=myconfig)

# 每行前面不要有空格和tab键
# code代码的格式要求比较严格
# 1 include "qelib1.inc" 与引号之间对多只能有一个回车换号符
code = '''include "qelib1.inc";
    qreg q[5];
    creg c[5];
    
    x q[4];
    h q[2];
    h q[3];
    h q[4];
    barrier q[1],q[2],q[3],q[4];
    h q[4];
    cx q[3],q[4];
    tdg q[4];
    cx q[3],q[4];
    t q[4];
    cx q[3],q[4];
    tdg q[4];
    cx q[3],q[4];
    tdg q[3];
    t q[4];
    h q[2];
    h q[3];
    cx q[3],q[2];
    h q[4];
    h q[2];
    h q[3];
    tdg q[3];
    h q[2];
    h q[3];
    cx q[3],q[2];
    h q[2];
    h q[3];
    t q[2];
    s q[3];
    barrier q[1],q[2],q[3],q[4];
    h q[2];
    h q[3];
    measure q[2] -> c[2];
    measure q[3] -> c[3];
'''
# 我申请的个人账号没有使用物理芯片的权限
#data = api.run_experiment(qasm=code, backend='ibmqx4', shots=1024, name=None, timeout=180, access_token=mytoken)
data = api.run_experiment(qasm=code, backend='simulator', shots=1024, name=None)

result = data['result']['measure']
label = result['labels']
value = result['values']
print("测量结果:")
print(label)
print("对应的概率为:")
print(value)

print("data的完整返回结果:")
print(data)

import matplotlib.pyplot as plt
plt.bar(label, value)
plt.show()

本地运行结果:

测量结果:
['00100', '00000', '01000', '01100']
对应的概率为:
[0.12890625, 0.12109375, 0.6298828125, 0.1201171875]
data的完整返回结果:
{'status': 'DONE', 'idExecution': '5c306be3de20380059730554', 'idCode': '5c306be3de20380059730553', 'result': {'extraInfo': {'seed': 1095756424}, 'measure': {'qubits': [2, 3], 'labels': ['00100', '00000', '01000', '01100'], 'values': [0.12890625, 0.12109375, 0.6298828125, 0.1201171875]}}}

在ibmq官网日志的数据:

在这里插入图片描述

02 Deutsch-Jozsa算法在LIQui|>上试验

代码DeutschJozsa.fsx

#if INTERACTIVE
#r@"..\bin\Liquid1.dll"
#else
namesapce Microsoft.Research.Liquid
#endif
open System
open System.Collections.Generic
open Microsoft.Research.Liquid
open Util
open Operations

module Script=
    [<LQD>]
    let DeutschJozsa() = 
        show "自定义Deutsch-Jozsa算法"
        let mutable q1_0 = 0.0
        let mutable q1_1 = 0.0
        let mutable q2_0 = 0.0
        let mutable q2_1 = 0.0
        for k = 1 to 8192 do
            let ket = Ket(3)
            let qs = ket.Qubits
            X qs.Tail.Tail
            H >< qs
            CCNOT qs
            let target = !!(qs, 0, 1)
            H><target
            M><target
            if target.[0].Bit.v = 0 then
                q1_0<-q1_0+1.0
            else
                q1_1<-q1_1 + 1.0
            if target.[1].Bit.v = 0 then
                q2_0 <-q2_0 + 1.0
            else
                q2_1 <-q2_1 + 1.0
        show "q0: %f|0>+%f|1>"(q1_0/8192.0)(q1_1/8192.0)
        show "q1: %f|0>+%f|1>"(q2_0/8192.0)(q2_1/8192.0)
    
#if INTERACTIVE
do
    Script.DeutschJozsa()
#endif

运行结果:

d:\git\Quantum\Liquid\Samples>fsi DeutschJozsa.fsx
0:0000.0/自定义Deutsch-Jozsa算法
0:0000.0/
0:0000.0/===========================================================================================
0:0000.0/=    The Language-Integrated Quantum Operations (LIQUi|>) Simulator                       =
0:0000.0/=        Copyright (c) 2015,2016 Microsoft Corporation                                    =
0:0000.0/=        If you use LIQUi|> in your research, please follow the guidelines at             =
0:0000.0/=        https://github.com/StationQ/Liquid for citing LIQUi|> in your publications.     =
0:0000.0/===========================================================================================
0:0000.0/
0:0000.0/q0: 0.498535|0>+0.501465|1>
0:0000.0/q1: 0.487671|0>+0.512329|1>

在这里插入图片描述

03 Deutsch-Jozsa算法在ProjectQ上试验

DeutschJozsa.py

# DeutschJozsa.py
# ProjectQ上DeutschJozsa算法
from projectq import MainEngine
from projectq.ops import *

def DeutschJozsa():
    amp0_0 = 0
    amp0_1 = 1
    amp1_0 = 0
    amp1_1 = 0
    for times in range(0, 1024):
        eng = MainEngine()
        qs = eng.allocate_qureg(3)
        X | qs[2]
        All(Measure) | qs

        # Toffoli
        H |qs[2]
        CNOT | (qs[1], qs[2])
        Tdagger | qs[2]
        CNOT | (qs[0], qs[2])
        T | qs[2]
        CNOT | (qs[1], qs[2])
        Tdagger | qs[2]
        CNOT | (qs[0], qs[2])
        Tdagger | qs[1]
        T | qs[2]
        CNOT | (qs[0], qs[1])
        H | qs[2]
        Tdagger | qs[1]
        CNOT | (qs[0], qs[1])
        T | qs[0]
        S | qs[1]

        for i in range(0, len(qs)-1):
            H | qs[i]
        All(Measure) | qs # Measure | qs
        eng.flush()
        if int(qs[0]) == 0:
            amp0_0 += 1
        else:
            amp0_1 += 1
        if int(qs[1]) == 0:
            amp1_0 += 1
        else:
            amp1_1 += 1
    print("q0:%f|0> + %f|1>"%(amp0_0*1.0/1024, amp0_1*1.0/1024))
    print("q1:%f|0> + %f|1>"%(amp1_0*1.0/1024, amp1_1*1.0/1024))

if __name__ == "__main__":
    DeutschJozsa()

运行结果:

C:\Python36\python.exe D:/tmp/tensorflow/DeutschJozsa.py
q0:0.498047|0> + 0.502930|1>
q1:0.476562|0> + 0.523438|1>

Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/longji/article/details/85859201