<jupyter_start><jupyter_text>Load and initialize qiskit_alt<jupyter_code>import qiskit_alt
qiskit_alt.project.ensure_init()

# The following line would also initialize qiskit_alt behind the scenes, but we have made it explicity above.
# import qiskit_alt.electronic_structure
from qiskit.providers import aer  # make sure this is available
from qiskit_nature.drivers import UnitsType, Molecule
from qiskit_nature.drivers.second_quantization import (
    ElectronicStructureDriverType,
    ElectronicStructureMoleculeDriver,
)
from qiskit_nature.problems.second_quantization import ElectronicStructureProblem
from qiskit_nature.converters.second_quantization import QubitConverter
from qiskit_nature.mappers.second_quantization import JordanWignerMapper

from qiskit.algorithms import VQE
from qiskit.circuit.library import TwoLocal
from qiskit.utils import QuantumInstance
from qiskit import Aer
from qiskit.algorithms.optimizers import COBYLA

import qiskit_alt, qiskit_alt.electronic_structure  # already done above, but this does not hurt
from qiskit_alt.pauli_operators import PauliSum_to_SparsePauliOp
from qiskit.opflow import PauliSumOp<jupyter_output><empty_output><jupyter_text>Describing the $H_2$ molecule structure<jupyter_code>geometry = [["H", [0.0, 0.0, 0.0]], ["H", [0.0, 0.0, 0.735]]]
basis="sto3g"

molecule = Molecule(
    geometry=geometry, charge=0, multiplicity=1
)
driver = ElectronicStructureMoleculeDriver(
    molecule, basis=basis, driver_type=ElectronicStructureDriverType.PYSCF
)<jupyter_output><empty_output><jupyter_text>Computing the fermionic operator   Qiskit Nature<jupyter_code>es_problem = ElectronicStructureProblem(driver)
second_q_op= es_problem.second_q_ops()
qubit_converter = QubitConverter(mapper=JordanWignerMapper())
qubit_op = qubit_converter.convert(second_q_op[0])
# Print the underlying data stored backing qubit_op
qubit_op.primitive.simplify()<jupyter_output><empty_output><jupyter_text>Qiskit Alt<jupyter_code># Compute the Fermionic operator of the molecule 
fermi_op = qiskit_alt.electronic_structure.fermionic_hamiltonian(geometry, basis)

# Convert the Fermionic operator to a Pauli operator using the Jordan-Wigner transform
pauli_op = qiskit_alt.electronic_structure.jordan_wigner(fermi_op)<jupyter_output><empty_output><jupyter_text>`jordan_wigner` did the computation in Julia and converted the result to standard Python qiskit `SparsePauliOp` before returning:<jupyter_code>type(pauli_op)<jupyter_output><empty_output><jupyter_text>It displays like this.<jupyter_code>pauli_op.simplify()<jupyter_output><empty_output><jupyter_text>The two calculations of the Hamiltonian as a Pauli operator agree, up to the constant term.<jupyter_code>(pauli_op - qubit_op.primitive).simplify()<jupyter_output><empty_output><jupyter_text>This is just the nuclear-repulsion term, which is retained by qiskit_alt. Note the coefficient is the same as the line above.<jupyter_code>fermi_op[1]  # In qiskit_alt, the Fermi operator carries the identity term
nuclear_energy_term = fermi_op[1].coeff
nuclear_energy_term<jupyter_output><empty_output><jupyter_text>Computing the ground state energy<jupyter_code># Define the ansatz
ansatz = TwoLocal(
    rotation_blocks=["h", "rx"],
    entanglement_blocks="cz",
    entanglement="full",
    reps=2,
    parameter_prefix="y",
)

# Initialize the COBYLA optimizer
optimizer = COBYLA(maxiter=500)

# Select the backend for the quantum_instance
backend = QuantumInstance(Aer.get_backend('statevector_simulator'), 
                                        seed_simulator=100, 
                                        seed_transpiler=100)
# Run VQE algorithm
vqe = VQE(ansatz = ansatz, 
          optimizer = optimizer,
          quantum_instance = backend)<jupyter_output><empty_output><jupyter_text>Qiskit Nature<jupyter_code># Compute the ground-state energy of the molecule 
nature_result = vqe.compute_minimum_eigenvalue(operator=qubit_op)
nature_energy = nature_result.eigenvalue.real + nuclear_energy_term # Include constant term that is normally thrown away
print("The ground-state energy of the Hydrogen molecule is {} Hatree".format(round(nature_energy,3)))<jupyter_output>The ground-state energy of the Hydrogen molecule is -1.117 Hatree<jupyter_text>Qiskit Alt<jupyter_code># Convert the Pauli operator into a sum of Pauli operators, the required input format
pauli_sum_op = PauliSumOp(pauli_op)
# Compute the ground-state energy of the molecule
alt_result = vqe.compute_minimum_eigenvalue(operator=pauli_sum_op)
print("The ground-state energy of the Hydrogen molecule is {} Hatree".format(round(alt_result.eigenvalue.real,3)))<jupyter_output>The ground-state energy of the Hydrogen molecule is -1.117 Hatree
{'fraction_non_alphanumeric': 0.07530312699425654, 'fraction_numerical': 0.00872154860667943, 'mean_word_length': 6.633116883116883, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '550135', 'license': 'Apache-2.0', 'n_tokens_mistral': 1600, 'n_tokens_neox': 1478, 'n_words': 355, 'path': 'demos/Comparing-qiskit_alt-and-qiskit_nature-VQE-output.ipynb', 'repo_name': 'jlapeyre/qiskit-alt', 'size': '4701'}