贝尔不等式的验证
在量子计算机上运行一个实验,以演示使用Estimator原型违反CHSH不等式。
import numpy as npfrom qiskit import QuantumCircuit
from qiskit.circuit import Parameter
from qiskit.quantum_info import SparsePauliOpfrom qiskit_ibm_runtime import QiskitRuntimeService
from qiskit_ibm_runtime import EstimatorV2 as Estimatorimport matplotlib.pyplot as plt
import matplotlib.ticker as tck#要在硬件上运行,请选择队列中作业数量最少的后端
service = QiskitRuntimeService(channel="ibm_quantum")
backend = service.least_busy(operational=True, simulator=False, min_num_qubits=127)
backend.name
创建一个参数化的CHSH电路
首先,我们用参数θ来编写电路,我们称之为θ。Estimator原型机通过直接提供可观测值的期望值,极大地简化了电路的构建和输出分析。许多令人感兴趣的问题,特别是对于噪声系统的近期应用,都可以用期望值来表示。估计器(V2)原语可以根据提供的可观察对象自动更改度量基础。
theta = Parameter("$\\theta$")chsh_circuit = QuantumCircuit(2)
chsh_circuit.h(0)
chsh_circuit.cx(0, 1)
chsh_circuit.ry(theta, 0)
chsh_circuit.draw(output="mpl", idle_wires=False, style="iqp")
创建参数化CHSH电路后,下一步将创建要分配给电路的相位值列表。您可以使用以下代码创建21个相位值的列表,范围从0到2π,间距相等,即0 0,0.1π,0.2π,…,1.9π, 2π。
number_of_phases = 21
phases = np.linspace(0, 2 * np.pi, number_of_phases)
# Phases need to be expressed as list of lists in order to work
individual_phases = [[ph] for ph in phases]
现在我们需要计算期望值的可观察对象。在我们的例子中,我们正在查看每个量子位的正交基,让第一个量子位的参数化- Y -旋转相对于第二个量子位基几乎连续地扫描测量基。因此,我们将选择可观测值ZZ、ZX、XZ和XX。
# <CHSH1> = <AB> - <Ab> + <aB> + <ab> -> <ZZ> - <ZX> + <XZ> + <XX>
observable1 = SparsePauliOp.from_list([("ZZ", 1), ("ZX", -1), ("XZ", 1), ("XX", 1)])# <CHSH2> = <AB> + <Ab> - <aB> + <ab> -> <ZZ> + <ZX> - <XZ> + <XX>
observable2 = SparsePauliOp.from_list([("ZZ", 1), ("ZX", 1), ("XZ", -1), ("XX", 1)])from qiskit.transpiler.preset_passmanagers import generate_preset_pass_managertarget = backend.target
pm = generate_preset_pass_manager(target=target, optimization_level=3)chsh_isa_circuit = pm.run(chsh_circuit)
chsh_isa_circuit.draw(output="mpl", idle_wires=False, style="iqp")isa_observable1 = observable1.apply_layout(layout=chsh_isa_circuit.layout)
isa_observable2 = observable2.apply_layout(layout=chsh_isa_circuit.layout)
为了在对Estimator的一次调用中执行整个实验。我们可以创建一个Qiskit Runtime Estimator原型机来计算我们的期望值。EstimatorV2.run()方法接受一个原始统一块(pub)的可迭代对象。每个PUB都是一个可迭代对象,格式为(circuit, observables, parameter_values: Optional, precision: Optional)
# To run on a local simulator:
# Use the StatevectorEstimator from qiskit.primitives instead.estimator = Estimator(mode=backend)pub = (chsh_isa_circuit, # ISA circuit[[isa_observable1], [isa_observable2]], # ISA Observablesindividual_phases, # Parameter values
)job_result = estimator.run(pubs=[pub]).result()chsh1_est = job_result[0].data.evs[0]
chsh2_est = job_result[0].data.evs[1]
fig, ax = plt.subplots(figsize=(10, 6))# results from hardware
ax.plot(phases / np.pi, chsh1_est, "o-", label="CHSH1", zorder=3)
ax.plot(phases / np.pi, chsh2_est, "o-", label="CHSH2", zorder=3)# classical bound +-2
ax.axhline(y=2, color="0.9", linestyle="--")
ax.axhline(y=-2, color="0.9", linestyle="--")# quantum bound, +-2√2
ax.axhline(y=np.sqrt(2) * 2, color="0.9", linestyle="-.")
ax.axhline(y=-np.sqrt(2) * 2, color="0.9", linestyle="-.")
ax.fill_between(phases / np.pi, 2, 2 * np.sqrt(2), color="0.6", alpha=0.7)
ax.fill_between(phases / np.pi, -2, -2 * np.sqrt(2), color="0.6", alpha=0.7)# set x tick labels to the unit of pi
ax.xaxis.set_major_formatter(tck.FormatStrFormatter("%g $\\pi$"))
ax.xaxis.set_major_locator(tck.MultipleLocator(base=0.5))# set labels, and legend
plt.xlabel("Theta")
plt.ylabel("CHSH witness")
plt.legend()
plt.show()
参考:CHSH Inequality | IBM Quantum Learning