I'm quite new to quantum coding and i keep on getting this error in my code.
***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
Can anyone help me? Thanks Full script in this link. Quantum code
I tried Changing the variabes in those two parts of the error to 0 but it gave me KeyError: Ill Geometry. I also tried changing the number to different things but it didn't work.
CodePudding user response:
That error is due to a interface change in qiskit_nature, where aux_operators are now returned as dictionaries instead of lists.
You can temporarily switch back to the old behavior by adding the following two lines to the top of your script:
from qiskit_nature.settings import settings
settings.dict_aux_operators = False
in the long run, you should migrate your code to the updated interface.