我是量子编码的新手,我不断地在我的代码中得到这个错误。
***hamiltonian = second_q_ops[0]***
converter = QubitConverter(mapper,two_qubit_reduction=True)
reducer = TwoQubitReduction(num_particles)
qubit_op = converter.convert(hamiltonian)
qubit_op = reducer.convert(qubit_op)
return qubit_op, num_particles, num_spin_orbitals, problem, converter
def exact_solver(problem, converter):
solver = NumPyMinimumEigensolverFactory()
calc = GroundStateEigensolver(converter, solver)
return result
backend = BasicAer.get_backend("statevector_simulator")
distances = np.arange(0.5, 4.0, 0.2)
exact_energies = []
vqe_energies = []
optimizer = SLSQP(maxiter=5)
for dist in distances:
(qubit_op, num_particles, num_spin_orbitals,
***problem, converter) = get_qubit_op(dist)***
result = exact_solver(problem,converter)
exact_energies.append(result.total_energies[0].real)
init_state = HartreeFock(num_spin_orbitals, num_particles, converter)
var_form = UCCSD(converter, `your text`
num_particles,
num_spin_orbitals,
initial_state=init_state)
vqe = VQE(var_form, optimizer, quantum_instance=backend)
vqe_calc = vqe.compute_minimum_eigenvalue(qubit_op)
vqe_result = problem.interpret(vqe_calc).total_energies[0].real
vqe_energies.append(vqe_result)
print(f"Interatomic Distance: {np.round(dist, 2)}",
f"VQE Result: {vqe_result:.5f}",
f"Exact Energy: {exact_energies[-1]:.5f}")
print("All energies have been calculated")
plt.plot(distances, exact_energies, label="Exact Energy")
plt.plot(distances, vqe_energies, label="VQE Energy")
plt.xlabel('Atomic distance (Angstrom)')
plt.ylabel('Energy')
plt.legend()
plt.show()
The problem is in bold.it says:
Traceback (most recent call last):
Input In [8] in <cell line: 132>
problem, converter) = get_qubit_op(dist)
Input In [8] in get_qubit_op
hamiltonian = second_q_ops[0]
KeyError: 0
有人能帮助我吗?谢谢完整的脚本在这个链接. Quantum code
我尝试将错误的这两个部分中的变量更改为0,但它给了我KeyError:我也试着把数字换成不同的东西,但是没有用。
1条答案
按热度按时间iyfamqjs1#
该错误是由于qiskit_nature中的接口更改造成的,其中aux_operators现在作为字典而不是列表返回。
通过在脚本顶部添加以下两行,可以临时切换回旧行为:
从长远来看,您应该将代码迁移到更新的接口。