在pyquil程序上实现错误缓解的Python库。
em-pyquil的Python项目详细描述
在Rigetti Forest上实现的错误缓解™在
量子计算机与外界不必要的相互作用 世界在计算中引入了噪音 容错量子计算是当前技术所无法企及的。 在这方面,使用NISQ设备可以考虑 在没有额外资源的情况下减轻错误的影响 要求。其中一种缓解技术(用于 短路深度电路)是所谓的“外推到 “零噪音限制”或我们将提到的“误差缓解(EM)” 已成功implemented recently。 假设噪声的作用很弱,可以用 一些小参数。任何 observable依赖于这个参数,并且可以扩展 与第0阶项相对应 无噪声值。建议获得 不同噪声参数下的期望值,然后应用 理查森对极限的延迟方法 接近无噪值。在
这个软件包提供了一个易于使用的接口,可以将EM技术的纠错代码应用到任何PyQuil程序中。在
此包中的EM按here所述实现。在
安装
EMPyQuil可以使用pip安装,也可以直接从源代码安装。在
要将EMPyQuil安装为PyPI包,请执行以下操作:
pip install em_pyquil
如果您希望直接从源代码安装EMPyQuil,请执行以下操作:
pip install git+https://github.com/gate42qc/em-pyquil
使用
这个包提供了两个函数:apply_em
和{QuantumComputer
对象一起使用。
这些函数将重写传递的run
方法,使它们返回减少错误的结果。在
因此,如果您有一些代码并希望运行它的错误缓解版本,您只需要在QuantumComputer
上调用apply_em
用于运行程序的对象。在
frompyquil.apiimportget_qcfrompyquil.quilimportProgramfrompyquil.gatesimportXfromem_pyquilimportapply_em,apply_noisenoisy_qc=apply_noise(get_qc("3q-qvm"))em_qc=apply_em(get_qc("3q-qvm"))p=Program(X(0))em_res=em_qc.run_and_measure(p,1000)# results obtained running the program with noise and filtered with EM techniquenoisy_res=noisy_qc.run_and_measure(p,1000)# results obtained running the program with just noise model without filtering
示例
在examples directory中有更多的例子。在
应用EM技术运行程序
我们在程序中运行EM 多次使用噪声模型 噪声参数的不同值 (在当前实现中,噪声参数是门时间)。 然后我们将收集结果位串,得到每个量子位在试验中的平均结果, 然后用理查森方法外推结果。 外推结果四舍五入到0或1并返回。在
所以在下面一行之后:
em_qc = apply_em(get_qc("3q-qvm"))
em_qc
的工作方式与QuantumComputer
实例的工作方式完全相同
但它将多次运行程序并返回外推结果。
这允许您使用EM运行现有程序
不改变我们现有的代码。在
:注
不要在同一个apply_noise
对象上调用apply_noise
或noisy_qc
。在
通过给出基本参数和系数列表,可以自定义EM中使用的噪声参数:
^{pr2}$比较噪波和噪波+电磁波结果
在这个包中还有另一个函数:apply_noise
您可以用来
获取使用类似噪声的QuantumComputer
实例
建模到apply_em
,以便可以比较结果:
importnumpyasnpimportmatplotlib.pyplotaspltimportrandomfrompyquil.apiimportget_qcfrompyquil.quilimportProgramfrompyquil.gatesimportRX,RZ,CZfromem_pyquilimportapply_em,apply_noisedefget_dagger_of_native_gate(gate):ifgate.name=="RZ":returnRZ(-gate.params[0],gate.qubits[0])ifgate.name=="RX":returnRX(-gate.params[0],gate.qubits[0])ifgate.name=="CZ":returnCZ(*gate.qubits)raiseValueError("Unsupported gate: "+str(gate))defget_random_circuit(qubits,length,two_qubit_gate_portion=0.3):p=Program()foriinrange(int(length/2)):if(len(qubits)>1)and(random.random()<two_qubit_gate_portion):random_gate=CZ(*(random.sample(qubits,2)))else:theta=2*np.pi*random.random()qubit=random.choice(qubits)random_gate=random.choice([RZ(theta,qubit),RX(np.pi/2,qubit),RX(-np.pi/2,qubit)])p.inst(random_gate)forgateinreversed(Program(p.out()).instructions):p.inst(get_dagger_of_native_gate(gate))returnProgram('PRAGMA PRESERVE_BLOCK')+p+Program('PRAGMA END_PRESERVE_BLOCK')defrun_with_qc(qc,lengths):data=[]foriinlengths:fidelities=[]forjinrange(20):p=get_random_circuit(qc.qubits(),i)res=qc.run_and_measure(p,10000)fidelity=1-np.mean(res[0]*res[1])fidelities.append(fidelity)mean_fidelity=np.mean(np.array(fidelities))data.append(mean_fidelity)returndatanoisy_qc=apply_noise(get_qc("3q-qvm"))em_qc=apply_em(get_qc("3q-qvm"))lengths=np.arange(2,100,5)em_data=run_with_qc(em_qc,lengths)noisy_data=run_with_qc(noisy_qc,lengths)plt.xlabel('Circuit depth')plt.ylabel('Fidelity')plt.plot(lengths,noisy_data,'o-',label="Only Noise")plt.plot(lengths,em_data,'o-',label="Noise + EM")plt.legend()plt.show()
- 项目
标签: