1

Ive been trying to solve this issue but this line is causing me trouble

from qiskit.chemistry.core import Hamiltonian, TransformationType

I know that i have to use qiskit nature instead of qiskit chemistry but i dont know to what i have to migrade to for "Hamiltonian" and "TransformationType".

import numpy as np
from qiskit import QuantumCircuit, Aer
from qiskit.aqua import aqua_globals, QuantumInstance
from qiskit.aqua.algorithms import VQE
from qiskit.aqua.components.optimizers import SLSQP
from qiskit.circuit.library import TwoLocal
from qiskit.chemistry.drivers import PySCFDriver
from qiskit.chemistry.core import Hamiltonian, TransformationType

# Set the number of qubits and optimization parameters
n_qubits = 4
depth = 3

# Set up the molecule and driver
molecule = 'H .0 .0 -{0}; H .0 .0 {0}'
distance = 0.74
driver = PySCFDriver(atom=molecule.format(distance/2), 
unit=TransformationType.ANGSTROM, charge=0, spin=0, basis='sto3g')
qmolecule = driver.run()
hamiltonian = qmolecule.get_molecular_hamiltonian()

# Define the ansatz circuit
ansatz = TwoLocal(n_qubits, ['ry', 'rz'], 'cz', reps=depth)

# Define the optimizer
optimizer = SLSQP(maxiter=1000)

# Define the VQE algorithm
vqe = VQE(ansatz, optimizer)

# Run the VQE algorithm
result = vqe.compute_minimum_eigenvalue(hamiltonian)

# Print the energy of the ground state
print('Ground state energy: ', result.eigenvalue.real)
James Z
  • 12,209
  • 10
  • 24
  • 44
  • Can you please post the relevant code snippets that you need to be migrated? It will be easier for others to help. – Junye Huang Mar 16 '23 at 11:33
  • "from qiskit.chemistry.core import Hamiltonian, TransformationType" is the one I'm having trouble with. Ive migrated the other ones. – Kenson Wesley Mar 16 '23 at 16:32

3 Answers3

1

I would suggest going through the Nature tutorials, particularly those for ground state computation. Also the readme has an example that may help (this link here is to one compatible with the latest stable release). There is no Hamiltonian object in Nature, in qiskit.chemistry is was more a higher level wrapper over the FermionicOp there. In Nature the closest thing might be the GroundStateEigensolver. TransformationType was for Full or ParticleHole. Nature does not have Particle Hole support so there is no equivalent. In general code was refactored somewhat when logic was migrated from Aqua to Nature, and it has continued to evolve, such that there may not be direct equivalents. Best to look at a ground state computation in Nature and go from there. It looks similar with a driver etc., but different at the same time!

Steve Wood
  • 266
  • 1
  • 3
0

Just RTFM. It's all in the documentation.

https://qiskit.org/documentation/nature/index.html

  • Thank you for the response but i couldn't find anything for "from qiskit.chemistry.core import Hamiltonian, TransformationType". – Kenson Wesley Mar 16 '23 at 16:33
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 21 '23 at 20:27
0

Looking at the full code example. Hamiltonian object was not used. The hamiltonian variable is generated by hamiltonian = qmolecule.get_molecular_hamiltonian(). The information of the hamiltonian is an attribute of the new qiskit_nature.second_q.problems.electronic_structure_problem.ElectronicStructureProblem.hamiltonian.second_q_op() which will be used to calculate the ground state energy when it is sovled using GroundStateEigensolver.solve(ElectronicStructureProblem) (see full code examples below)

TransformationType here is only used for getting the angstrom unit which can be replaced with qiskit_nature.units.DistanceUnit.ANGSTROM

The following example should be equivalent to your original code (modified from the Qiskit Nature README example.

from qiskit_nature.units import DistanceUnit
from qiskit_nature.second_q.drivers import PySCFDriver

n_qubits = 4
depth = 3

# Use PySCF, a classical computational chemistry software
# package, to compute the one-body and two-body integrals in
# electronic-orbital basis, necessary to form the Fermionic operator
driver = PySCFDriver(
    atom='H .0 .0 .0; H .0 .0 0.735',
    unit=DistanceUnit.ANGSTROM,
    basis='sto3g',
)
problem = driver.run()

# setup the qubit mapper
from qiskit_nature.second_q.mappers import ParityMapper, QubitConverter
converter = QubitConverter(ParityMapper())


# setup the classical optimizer for the VQE
from qiskit.algorithms.optimizers import SLSQP

optimizer = SLSQP(maxiter=1000)

# setup the estimator primitive for the VQE
from qiskit.primitives import Estimator

estimator = Estimator()

# setup the ansatz for VQE
from qiskit.circuit.library import TwoLocal

ansatz = TwoLocal(n_qubits, ['ry', 'rz'], 'cz', reps=depth)

# set up our actual VQE instance
from qiskit.algorithms.minimum_eigensolvers import VQE

vqe = VQE(estimator, ansatz, optimizer)
# ensure that the optimizer starts in the all-zero state which corresponds to
# the Hartree-Fock starting point
vqe.initial_point = [0] * ansatz.num_parameters

# prepare the ground-state solver and run it
from qiskit_nature.second_q.algorithms import GroundStateEigensolver

algorithm = GroundStateEigensolver(converter, vqe)

electronic_structure_result = algorithm.solve(problem)
print('Ground state energy: ', electronic_structure_result.eigenvalues[0].real)
Junye Huang
  • 130
  • 1
  • 5