def test_bksf_mapping(self): """Test bksf mapping. The spectrum of bksf mapping should be half of jordan wigner mapping. """ driver = PySCFDriver(atom='H .0 .0 0.7414; H .0 .0 .0', unit=UnitsType.ANGSTROM, charge=0, spin=0, basis='sto3g') molecule = driver.run() fer_op = FermionicOperator(h1=molecule.one_body_integrals, h2=molecule.two_body_integrals) jw_op = fer_op.mapping('jordan_wigner') bksf_op = fer_op.mapping('bksf') jw_op.to_matrix() bksf_op.to_matrix() jw_eigs = np.linalg.eigvals(jw_op.matrix.toarray()) bksf_eigs = np.linalg.eigvals(bksf_op.matrix.toarray()) jw_eigs = np.sort(np.around(jw_eigs.real, 6)) bksf_eigs = np.sort(np.around(bksf_eigs.real, 6)) overlapped_spectrum = np.sum(np.isin(jw_eigs, bksf_eigs)) self.assertEqual(overlapped_spectrum, jw_eigs.size // 2)
def setUp(self): try: driver = PySCFDriver(atom='Li .0 .0 -0.8; H .0 .0 0.8', unit=UnitsType.ANGSTROM, charge=0, spin=0, basis='sto3g') except QiskitChemistryError: self.skipTest('PYSCF driver does not appear to be installed') self.qmolecule = driver.run()
def test_iqpe(self, distance): self.algorithm = 'IQPE' self.log.debug('Testing End-to-End with IQPE on H2 with ' 'inter-atomic distance {}.'.format(distance)) try: driver = PySCFDriver(atom='H .0 .0 .0; H .0 .0 {}'.format(distance), unit=UnitsType.ANGSTROM, charge=0, spin=0, basis='sto3g') except QiskitChemistryError: self.skipTest('PYSCF driver does not appear to be installed') self.molecule = driver.run() qubit_mapping = 'parity' fer_op = FermionicOperator(h1=self.molecule.one_body_integrals, h2=self.molecule.two_body_integrals) self.qubit_op = fer_op.mapping(map_type=qubit_mapping, threshold=1e-10).two_qubit_reduced_operator(2) exact_eigensolver = ExactEigensolver(self.qubit_op, k=1) results = exact_eigensolver.run() self.reference_energy = results['energy'] self.log.debug('The exact ground state energy is: {}'.format(results['energy'])) num_particles = self.molecule.num_alpha + self.molecule.num_beta two_qubit_reduction = True num_orbitals = self.qubit_op.num_qubits + (2 if two_qubit_reduction else 0) num_time_slices = 50 num_iterations = 12 state_in = HartreeFock(self.qubit_op.num_qubits, num_orbitals, num_particles, qubit_mapping, two_qubit_reduction) iqpe = IQPE(self.qubit_op, state_in, num_time_slices, num_iterations, paulis_grouping='random', expansion_mode='suzuki', expansion_order=2, shallow_circuit_concat=True) backend = qiskit.Aer.get_backend('qasm_simulator') quantum_instance = QuantumInstance(backend, shots=100, pass_manager=PassManager()) result = iqpe.run(quantum_instance) self.log.debug('top result str label: {}'.format(result['top_measurement_label'])) self.log.debug('top result in decimal: {}'.format(result['top_measurement_decimal'])) self.log.debug('stretch: {}'.format(result['stretch'])) self.log.debug('translation: {}'.format(result['translation'])) self.log.debug('final energy from QPE: {}'.format(result['energy'])) self.log.debug('reference energy: {}'.format(self.reference_energy)) self.log.debug('ref energy (transformed): {}'.format( (self.reference_energy + result['translation']) * result['stretch']) ) self.log.debug('ref binary str label: {}'.format(decimal_to_binary( (self.reference_energy + result['translation']) * result['stretch'], max_num_digits=num_iterations + 3, fractional_part_only=True ))) np.testing.assert_approx_equal(result['energy'], self.reference_energy, significant=2)
def setUp(self): try: driver = PySCFDriver(atom='Li .0 .0 .0; H .0 .0 1.595', unit=UnitsType.ANGSTROM, charge=0, spin=0, basis='sto3g') except QiskitChemistryError: self.skipTest('PYSCF driver does not appear to be installed') molecule = driver.run() self.fer_op = FermionicOperator(h1=molecule.one_body_integrals, h2=molecule.two_body_integrals)
def test_freezing_core(self): driver = PySCFDriver(atom='H .0 .0 -1.160518; Li .0 .0 0.386839', unit=UnitsType.ANGSTROM, charge=0, spin=0, basis='sto3g') molecule = driver.run() fer_op = FermionicOperator(h1=molecule.one_body_integrals, h2=molecule.two_body_integrals) fer_op, energy_shift = fer_op.fermion_mode_freezing([0, 6]) gt = -7.8187092970493755 diff = abs(energy_shift - gt) self.assertLess(diff, 1e-6) driver = PySCFDriver(atom='H .0 .0 .0; Na .0 .0 1.888', unit=UnitsType.ANGSTROM, charge=0, spin=0, basis='sto3g') molecule = driver.run() fer_op = FermionicOperator(h1=molecule.one_body_integrals, h2=molecule.two_body_integrals) fer_op, energy_shift = fer_op.fermion_mode_freezing( [0, 1, 2, 3, 4, 10, 11, 12, 13, 14]) gt = -162.58414559586748 diff = abs(energy_shift - gt) self.assertLess(diff, 1e-6)
def mol_info_pyscf(atom_coords, basis_set, unit): """ Derives the number of particles and spin orbitals of a molecular system using the pySCF driver Args: atom_coords : atom coordinates (string) basis_set : basis set (string) unit : unit of distance between atoms (qiskit_chemistry.drivers.UnitsType) Return: n_particles (int) n_spin_orbitals (int) """ driver = PySCFDriver(atom=atom_coords, unit=unit, charge=0, spin=0, basis=basis_set) molecule = driver.run() n_particles = molecule.num_alpha + molecule.num_beta n_spin_orbitals = molecule.num_orbitals * 2 return n_particles, n_spin_orbitals
# lib from Qiskit Aqua from qiskit_aqua import Operator, QuantumInstance from qiskit_aqua.algorithms import VQE, ExactEigensolver from qiskit_aqua.components.optimizers import COBYLA # lib from Qiskit Aqua Chemistry from qiskit_chemistry import FermionicOperator from qiskit_chemistry.drivers import PySCFDriver, UnitsType from qiskit_chemistry.aqua_extensions.components.variational_forms import UCCSD from qiskit_chemistry.aqua_extensions.components.initial_states import HartreeFock # using driver to get fermionic Hamiltonian # PySCF example driver = PySCFDriver(atom='H 0.0 0.0 0.0; H 0.0 0.0 0.735', unit=UnitsType.ANGSTROM, charge=0, spin=0, basis='sto3g') molecule = driver.run() # please be aware that the idx here with respective to original idx freeze_list = [0] remove_list = [-3, -2] # negative number denotes the reverse order map_type = 'parity' map_type = 'bksf' #map_type = 'bravyi_kitaev' h1 = molecule.one_body_integrals h2 = molecule.two_body_integrals nuclear_repulsion_energy = molecule.nuclear_repulsion_energy
""" qiskit_chemistry.fermionic_operator.FermionicOperator#__init__ If you are using the 'physicist' notation, you need to convert it to the 'chemist' notation first. E.g., h2 = numpy.einsum('ikmj->ijkm', h2) """ import numpy as np from qiskit_chemistry import FermionicOperator from qiskit_chemistry.drivers import PySCFDriver, UnitsType driver = PySCFDriver(atom='H 0.0 0.0 0.0; H 0.0 0.0 1.41968', unit=UnitsType.BOHR, charge=0, spin=0, basis='sto3g') molecule = driver.run() h2 = molecule.two_body_integrals h2p = np.einsum('ijkm->ikmj', h2) print("########## Chemist notation #########") print(h2) print("########## Physicist notation #########") print(h2p) # ferOp = FermionicOperator(h1=molecule.one_body_integrals, h2=molecule.two_body_integrals)
from qiskit_chemistry import FermionicOperator from qiskit_chemistry.drivers import PySCFDriver, UnitsType from pyscf import mp, fci # Use PySCF, a classical computational chemistry software package, to compute the one-body and two-body integrals in # molecular-orbital basis, necessary to form the Fermionic operator driver = PySCFDriver(atom='H .0 .0 .0; H .0 .0 0.735; H .0 .0 1.2', unit=UnitsType.ANGSTROM, spin=1, basis='sto3g') molecule = driver.run() num_particles = molecule.num_alpha + molecule.num_beta num_spin_orbitals = molecule.num_orbitals * 2 # Build the qubit operator, which is the input to the VQE algorithm in Aqua ferOp = FermionicOperator(h1=molecule.one_body_integrals, h2=molecule.two_body_integrals) map_type = 'PARITY' qubitOp = ferOp.mapping(map_type) qubitOp = qubitOp.two_qubit_reduced_operator(num_particles) num_qubits = qubitOp.num_qubits # set the backend for the quantum computation from qiskit import Aer backend = Aer.get_backend('statevector_simulator') # setup a classical optimizer for VQE from qiskit_aqua.components.optimizers import L_BFGS_B optimizer = L_BFGS_B() # setup the initial state for the variational form