I wrote code again using updated qiskit nature libraries but I am facing error , I need help to finding solution so that we can run this code on latest version of qiskit.

I am attaching code below......

import numpy as np

Importing standard Qiskit libraries

from qiskit import QuantumCircuit, transpile, Aer, IBMQ from qiskit.tools.jupyter import * from qiskit.visualization import * from ibm_quantum_widgets import * from qiskit.providers.aer import QasmSimulator

Loading your IBM Quantum account(s)

provider = IBMQ.load_account() :219: RuntimeWarning: scipy._lib.messagestream.MessageStream size changed, may indicate binary incompatibility. Expected 56 from C header, got 64 from PyObject In [3]: from qiskit.algorithms import VQE from qiskit_nature.second_q.algorithms import (GroundStateEigensolver, NumPyMinimumEigensolverFactory) from qiskit_nature.second_q.drivers import PySCFDriver from qiskit_nature.second_q.formats.molecule_info import MoleculeInfo from qiskit_nature.second_q.transformers import FreezeCoreTransformer from qiskit_nature.second_q.mappers import ParityMapper, QubitConverter, JordanWignerMapper

import matplotlib.pyplot as plt import numpy as np from qiskit_nature.second_q.circuit.library import UCCSD, HartreeFock from qiskit.circuit.library import EfficientSU2 from qiskit.algorithms.optimizers import COBYLA, SPSA, SLSQP from qiskit.opflow import TwoQubitReduction from qiskit import BasicAer, Aer from qiskit.utils import QuantumInstance from qiskit.utils.mitigation import CompleteMeasFitter from qiskit.providers.aer.noise import NoiseModel :219: RuntimeWarning: scipy._lib.messagestream.MessageStream size changed, may indicate binary incompatibility. Expected 56 from C header, got 64 from PyObject In [4]: def get_qubit_op(dist): # Define Molecule molecule = MoleculeInfo( # Coordinates in Angstrom ["Li", "H"], [(0.0, 0.0, 0.0), (dist, 0.0, 0.0)], multiplicity=1, # = 2*spin + 1 charge=0, )

driver = PySCFDriver.from_molecule(molecule, basis="sto3g")

# Get properties
properties = driver.run()
num_particles = properties.num_particles
num_spatial_orbitals = properties.num_spatial_orbitals

# Define Problem, Use freeze core approximation, remove orbitals.

transformer = FreezeCoreTransformer(freeze_core=True,remove_orbitals=[-3,-2])
problem = transformer.transform(properties)

num_spatial_orbitals = problem.num_spatial_orbitals
num_particles = problem.num_particles

hamiltonian = problem.hamiltonian.second_q_op()
# Do two qubit reduction
converter = QubitConverter(JordanWignerMapper())
reducer = TwoQubitReduction(num_particles)
qubit_op = converter.convert(hamiltonian)
qubit_op = reducer.convert(qubit_op)

return qubit_op, num_particles, num_spatial_orbitals, problem, converter

In [5]: def exact_solver(problem, converter): solver = NumPyMinimumEigensolverFactory() calc = GroundStateEigensolver(converter, solver) result = calc.solve(problem) 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_spatial_orbitals, problem, converter) = get_qubit_op(dist) result = exact_solver(problem,converter) exact_energies.append(result.total_energies[0].real)

var_form = UCCSD()
var_form.num_particles = num_particles
var_form.num_spatial_orbitals = (num_spatial_orbitals)
var_form.qubit_converter = converter
init_state = HartreeFock()
init_state.num_particles = num_particles
init_state.num_spatial_orbitals = (num_spatial_orbitals)
init_state.qubit_converter = converter


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
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") Traceback (most recent call last): Input In [5] in <cell line: 12> vqe_calc = vqe.compute_minimum_eigenvalue(qubit_op) File /opt/conda/lib/python3.8/site-packages/qiskit/algorithms/minimum_eigen_solvers/vqe.py:496 in compute_minimum_eigenvalue self._check_operator_ansatz(operator) File /opt/conda/lib/python3.8/site-packages/qiskit/algorithms/minimum_eigen_solvers/vqe.py:331 in _check_operator_ansatz raise AlgorithmError( AlgorithmError: 'The number of qubits of the ansatz does not match the operator, and the ansatz does not allow setting the number of qubits using num_qubits.'

Use %tb to get the full traceback. Search for solution online

  • $\begingroup$ Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. $\endgroup$
    – Community Bot
    Dec 17, 2022 at 6:45
  • $\begingroup$ If you will run this code then you will know as well what is the problem $\endgroup$ Dec 18, 2022 at 9:39


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.