《量子算法与编程入门》4.3 Grover算法
01 grover算法在Qiskit上试验
grover.py
# grover.py # 《量子算符与编程入门》 4.3 Grover算法 # 基于QISKit实现Grover算法 # 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]; h q[0]; h q[1]; x q[2]; h q[2]; h q[2]; h q[1]; h q[2]; cx q[2],q[1]; h q[1]; h q[2]; tdg q[2]; h q[0]; h q[2]; cx q[2],q[0]; h q[0]; h q[2]; t q[2]; h q[1]; h q[2]; cx q[2],q[1]; h q[1]; h q[2]; tdg q[1]; tdg q[2]; h q[0]; h q[2]; cx q[2],q[0]; h q[0]; h q[2]; h q[0]; h q[1]; cx q[1],q[0]; h q[0]; h q[1]; t q[2]; tdg q[1]; h q[2]; h q[0]; h q[1]; cx q[1],q[0]; h q[0]; h q[1]; t q[0]; s q[1]; h q[0]; h q[1]; x q[0]; x q[1]; h q[1]; h q[0]; h q[1]; cx q[1],q[0]; h q[0]; h q[1]; x q[0]; h q[1]; h q[0]; x q[1]; measure q[0] -> c[0]; h q[1]; measure q[1] -> c[1]; ''' # 我申请的个人账号没有使用物理芯片的权限 # 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()
用模拟设备运行结果:
C:\Python36\python.exe D:/tmp/tensorflow/grover.py 测量结果: ['00011'] 对应的概率为: [1] data的完整返回结果: {'status': 'DONE', 'idExecution': '5c30a53a65bbf3e2f', 'idCode': '5c30a53965bbf3e2e', 'result': {'extraInfo': {'seed': }, 'measure': {'qubits': [0, 1], 'labels': ['00011'], 'values': [1]}}}

在ibmqx4上的试验结果

02 grover算法在Liqui|>上试验
grover.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= [
] let Grover() = show "Grover算法" let ket = Ket(3) let qs = ket.Qubits X qs.Tail.Tail H >< qs CCNOT qs let target = !!(qs, 0, 1) H >< target X >< target H target.Tail CNOT target H target.Tail X >< target H >< qs M >< target for q in target do show "测量得到的结果是: %s"(q.ToString()) #if INTERACTIVE do Script.Grover() #endif
运行结果:
d:\git\Quantum\Liquid\Samples>fsi grover.fsx 0:0000.0/Grover算法 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/测量得到的结果是: 0|0>+ 1|1> 0:0000.0/测量得到的结果是: 0|0>+ 1|1>
03 grover算法在ProjectQ上试验
grover.py
# grover.py # ProjectQ上Grover算法 from projectq.backends import CircuitDrawer from projectq import MainEngine from projectq.ops import * def grover(eng, t): qs = eng.allocate_qureg(3) X | qs[2] All(H) | qs 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] H | qs[0] H | qs[1] X | qs[0] X | qs[1] H | qs[1] CNOT | (qs[0], qs[1]) H | qs[1] X | qs[0] X | qs[1] All(H) | qs All(Measure) | qs eng.flush() if t == 'simulator': print("执行Grover算法,检索到的元素是:" + str(int(qs[0])) + str(int(qs[1]))) elif t == 'drawer': drawing_engine = CircuitDrawer() latex = drawing_engine.get_latex() circuit = open("grover.tex", 'w') circuit.write(latex) circuit.close(); else: pass if __name__ == "__main__": eng = MainEngine() types = 'simulator' grover(eng, types) drawing_engine = CircuitDrawer() eng = MainEngine(drawing_engine) types = 'drawer' grover(eng, types)
运行结果:
C:\Python36\python.exe D:/tmp/tensorflow/grover.py 执行Grover算法,检索到的元素是:11 Process finished with exit code 0
运行pdflatex grover.tex生成pdf没成功。
如有错误之处,欢迎批评指正。:。
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/204259.html原文链接:https://javaforall.net
