0
$\begingroup$

I am new to VQE and want to see the whole circuit of UCCSD in VQE implementation. I saw the code post here Visualize full UCC circuit in qiskit I tried to import all packages but have no idea with one/two_body. Any suggestions would be great!

Here is the code the author said he skipped the import & pyscf:

electronic_energy = ElectronicEnergy.from_raw_integrals(
    ElectronicBasis.MO, one_body_ints, two_body_ints
)
hamiltonian = electronic_energy.second_q_ops()[0]
qubit_converter = QubitConverter(mapper=JordanWignerMapper())
qubit_op = qubit_converter.convert(hamiltonian,num_particles=num_particles)
initial_state = HartreeFock(
        num_spin_orbitals=num_spin_orbitals,
        num_particles=num_particles,
        qubit_converter=qubit_converter,
    )
reps=1
var_form = UCC(
    excitations="sd",
    num_particles=num_particles,
    num_spin_orbitals=num_spin_orbitals,
    initial_state=initial_state,
    qubit_converter=qubit_converter,
    reps=reps,
)
optimizer=COBYLA(maxiter=1000)
vqe = VQE(ansatz=var_form, optimizer=optimizer,quantum_instance=Aer.get_backend("aer_simulator_statevector"))
vqe_result =vqe.compute_minimum_eigenvalue(qubit_op)
from qiskit.tools.visualization import circuit_drawer
circuit=vqe.get_optimal_circuit()
circuit_drawer(circuit, output='mpl', plot_barriers=False)
plt.show()

What I have added:

from qiskit_nature.properties.second_quantization.electronic import ElectronicEnergy
from qiskit_nature.converters.second_quantization import QubitConverter
from qiskit_nature.mappers.second_quantization import JordanWignerMapper
from qiskit_nature.circuit.library.ansatzes import UCC
from qiskit_nature.circuit.library.initial_states import HartreeFock
from qiskit_nature.properties.second_quantization.electronic.bases import  ElectronicBasis
$\endgroup$

1 Answer 1

2
$\begingroup$

that was my question.

First of all, there's an easier way of creating those UCC circuits, so unless you're unhappy with using canonical Molecular Orbitals, there's no need to go deeper. Here's a fully working program for the Hydrogen molecule in minimal basis. It is based on the qiskit-tutorials, and I simply added a couple of lines for visualization.

from qiskit import Aer
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

molecule = Molecule(geometry=[['H', [0., 0., 0.]],
                              ['H', [0., 0., 0.735]]],
                     charge=0, multiplicity=1)
driver = ElectronicStructureMoleculeDriver(molecule, basis='sto3g', driver_type=ElectronicStructureDriverType.PYSCF)

es_problem = ElectronicStructureProblem(driver)
qubit_converter = QubitConverter(JordanWignerMapper())

from qiskit.providers.aer import StatevectorSimulator
from qiskit import Aer
from qiskit.utils import QuantumInstance
from qiskit_nature.algorithms import VQEUCCFactory

quantum_instance = QuantumInstance(backend = Aer.get_backend('aer_simulator_statevector'))
vqe_solver = VQEUCCFactory(quantum_instance)
from qiskit.algorithms import VQE

from qiskit_nature.algorithms import GroundStateEigensolver
calc = GroundStateEigensolver(qubit_converter, vqe_solver)
res = calc.solve(es_problem)
print(res)
from qiskit.tools.visualization import circuit_drawer
import matplotlib.pyplot as plt
circuit=calc.solver.get_optimal_circuit()
circuit_drawer(circuit.decompose(), output='mpl', plot_barriers=False)
plt.show()
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.