def setUp(self): super().setUp() self.driver1 = HDF5Driver( hdf5_input=self.get_resource_path('test_oovqe_h4.hdf5')) self.driver2 = HDF5Driver( hdf5_input=self.get_resource_path('test_oovqe_lih.hdf5')) self.driver3 = HDF5Driver( hdf5_input=self.get_resource_path('test_oovqe_h4_uhf.hdf5')) self.energy1_rotation = -3.0104 self.energy1 = -2.77 # energy of the VQE with pUCCD ansatz and LBFGSB optimizer self.energy2 = -7.70 self.energy3 = -2.50 self.initial_point1 = [ 0.039374, -0.47225463, -0.61891996, 0.02598386, 0.79045546, -0.04134567, 0.04944946, -0.02971617, -0.00374005, 0.77542149 ] self.seed = 50 self.optimizer = COBYLA(maxiter=1) self.transformation1 = \ FermionicTransformation(qubit_mapping=FermionicQubitMappingType.JORDAN_WIGNER, two_qubit_reduction=False) self.transformation2 = \ FermionicTransformation(qubit_mapping=FermionicQubitMappingType.JORDAN_WIGNER, two_qubit_reduction=False, freeze_core=True) self.quantum_instance = QuantumInstance( BasicAer.get_backend('statevector_simulator'), shots=1, seed_simulator=self.seed, seed_transpiler=self.seed)
def setUp(self): super().setUp() driver = HDF5Driver( hdf5_input=self.get_resource_path('test_driver_hdf5.hdf5')) temp_qmolecule = driver.run() file, self.save_file = tempfile.mkstemp(suffix='.hdf5') os.close(file) temp_qmolecule.save(self.save_file) # Tests are run on self.qmolecule which is from new saved HDF5 file # so save is tested based on getting expected values as per original driver = HDF5Driver(hdf5_input=self.save_file) self.qmolecule = driver.run()
def get_H2_Hamiltonian(bond_length): operator = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.PARITY, two_qubit_reduction=True, freeze_core=False, orbital_reduction=None) if bond_length==0.735: driver= HDF5Driver("H2/H2_equilibrium_0.735_sto-3g.hdf5") else: driver= HDF5Driver("H2/{:1.1f}_sto-3g.hdf5".format(bond_length)) qubit_op, aux_ops = operator.run(driver.run()) return qubit_op, operator
def test_swaprz(self): """ SwapRZ variational form test """ driver = HDF5Driver(self.get_resource_path('test_driver_hdf5.hdf5')) qmolecule = driver.run() operator = Hamiltonian(qubit_mapping=QubitMappingType.JORDAN_WIGNER, two_qubit_reduction=False) qubit_op, _ = operator.run(qmolecule) optimizer = SLSQP(maxiter=100) initial_state = HartreeFock( qubit_op.num_qubits, operator.molecule_info['num_orbitals'], operator.molecule_info['num_particles'], qubit_mapping=operator._qubit_mapping, two_qubit_reduction=operator._two_qubit_reduction) var_form = SwapRZ(qubit_op.num_qubits, initial_state=initial_state) algo = VQE(qubit_op, var_form, optimizer) result = algo.run( QuantumInstance(BasicAer.get_backend('statevector_simulator'), seed_simulator=aqua_globals.random_seed, seed_transpiler=aqua_globals.random_seed)) _, result = operator.process_algorithm_result(result) self.assertAlmostEqual(result['energy'], self.reference_energy, places=6)
def setUp(self): super().setUp() self.reference_energy = -1.1373060356951838 self.seed = 700 aqua_globals.random_seed = self.seed self.driver = HDF5Driver( self.get_resource_path('test_driver_hdf5.hdf5')) fermionic_transformation = FermionicTransformation( qubit_mapping=QubitMappingType.PARITY, two_qubit_reduction=False) self.qubit_op, _ = fermionic_transformation.transform(self.driver) self.fermionic_transformation = fermionic_transformation self.optimizer = SLSQP(maxiter=100) initial_state = HartreeFock( fermionic_transformation.molecule_info['num_orbitals'], fermionic_transformation.molecule_info['num_particles'], qubit_mapping=fermionic_transformation._qubit_mapping, two_qubit_reduction=fermionic_transformation._two_qubit_reduction) self.var_form = UCCSD( num_orbitals=fermionic_transformation. molecule_info['num_orbitals'], num_particles=fermionic_transformation. molecule_info['num_particles'], initial_state=initial_state, qubit_mapping=fermionic_transformation._qubit_mapping, two_qubit_reduction=fermionic_transformation._two_qubit_reduction)
def test_swaprz(self, mode): """ SwapRZ variational form test """ driver = HDF5Driver(self.get_resource_path('test_driver_hdf5.hdf5')) qmolecule = driver.run() operator = Hamiltonian(qubit_mapping=QubitMappingType.JORDAN_WIGNER, two_qubit_reduction=False) qubit_op, _ = operator.run(qmolecule) optimizer = SLSQP(maxiter=100) initial_state = HartreeFock( operator.molecule_info['num_orbitals'], operator.molecule_info['num_particles'], qubit_mapping=operator._qubit_mapping, two_qubit_reduction=operator._two_qubit_reduction) if mode == 'wrapped': warnings.filterwarnings('ignore', category=DeprecationWarning) wavefunction = SwapRZ(qubit_op.num_qubits, initial_state=initial_state) else: wavefunction = ExcitationPreserving(qubit_op.num_qubits, initial_state=initial_state) algo = VQE(qubit_op, wavefunction, optimizer) if mode == 'wrapped': warnings.filterwarnings('always', category=DeprecationWarning) result = algo.run( QuantumInstance(BasicAer.get_backend('statevector_simulator'), seed_simulator=aqua_globals.random_seed, seed_transpiler=aqua_globals.random_seed)) result = operator.process_algorithm_result(result) self.assertAlmostEqual(result.energy, self.reference_energy, places=6)
def test_excitation_preserving(self): """Test the excitation preserving wavefunction on a chemistry example.""" driver = HDF5Driver(self.get_resource_path('test_driver_hdf5.hdf5')) fermionic_transformation = FermionicTransformation( qubit_mapping=QubitMappingType.PARITY, two_qubit_reduction=False) qubit_op, _ = fermionic_transformation.transform(driver) optimizer = SLSQP(maxiter=100) initial_state = HartreeFock( fermionic_transformation.molecule_info['num_orbitals'], fermionic_transformation.molecule_info['num_particles'], qubit_mapping=fermionic_transformation._qubit_mapping, two_qubit_reduction=fermionic_transformation._two_qubit_reduction) wavefunction = ExcitationPreserving(qubit_op.num_qubits, initial_state=initial_state) solver = VQE(var_form=wavefunction, optimizer=optimizer, quantum_instance=QuantumInstance( BasicAer.get_backend('statevector_simulator'), seed_simulator=aqua_globals.random_seed, seed_transpiler=aqua_globals.random_seed)) gsc = GroundStateEigensolver(fermionic_transformation, solver) result = gsc.solve(driver) self.assertAlmostEqual(result.energy, self.reference_energy, places=4)
def test_uccsd_hf(self): """ uccsd hf test """ driver = HDF5Driver(self.get_resource_path('test_driver_hdf5.hdf5')) qmolecule = driver.run() core = Hamiltonian(qubit_mapping=QubitMappingType.PARITY, two_qubit_reduction=True) qubit_op, _ = core.run(qmolecule) optimizer = SLSQP(maxiter=100) initial_state = HartreeFock( core.molecule_info['num_orbitals'], core.molecule_info['num_particles'], qubit_mapping=core._qubit_mapping, two_qubit_reduction=core._two_qubit_reduction) var_form = UCCSD(num_orbitals=core.molecule_info['num_orbitals'], num_particles=core.molecule_info['num_particles'], initial_state=initial_state, qubit_mapping=core._qubit_mapping, two_qubit_reduction=core._two_qubit_reduction) algo = VQE(qubit_op, var_form, optimizer) result = algo.run( QuantumInstance(BasicAer.get_backend('statevector_simulator'))) result = core.process_algorithm_result(result) self.assertAlmostEqual(result.energy, self.reference_energy, places=6)
def test_excitation_preserving(self): """Test the excitation preserving wavefunction on a chemistry example.""" driver = HDF5Driver(self.get_resource_path('test_driver_hdf5.hdf5')) qmolecule = driver.run() operator = Hamiltonian(qubit_mapping=QubitMappingType.JORDAN_WIGNER, two_qubit_reduction=False) qubit_op, _ = operator.run(qmolecule) optimizer = SLSQP(maxiter=100) initial_state = HartreeFock( operator.molecule_info['num_orbitals'], operator.molecule_info['num_particles'], qubit_mapping=operator._qubit_mapping, two_qubit_reduction=operator._two_qubit_reduction) wavefunction = ExcitationPreserving(qubit_op.num_qubits, initial_state=initial_state) algo = VQE(qubit_op, wavefunction, optimizer) result = algo.run( QuantumInstance(BasicAer.get_backend('statevector_simulator'), seed_simulator=aqua_globals.random_seed, seed_transpiler=aqua_globals.random_seed)) result = operator.process_algorithm_result(result) self.assertAlmostEqual(result.energy, self.reference_energy, places=6)
def setUp(self): super().setUp() self.reference_energy = -1.1373060356951838 self.seed = 700 aqua_globals.random_seed = self.seed driver = HDF5Driver(self.get_resource_path('test_driver_hdf5.hdf5')) qmolecule = driver.run() core = Hamiltonian(qubit_mapping=QubitMappingType.PARITY, two_qubit_reduction=True) self.qubit_op, _ = core.run(qmolecule) self.core = core self.optimizer = SLSQP(maxiter=100) initial_state = HartreeFock( core.molecule_info['num_orbitals'], core.molecule_info['num_particles'], qubit_mapping=core._qubit_mapping, two_qubit_reduction=core._two_qubit_reduction) self.var_form = UCCSD( num_orbitals=core.molecule_info['num_orbitals'], num_particles=core.molecule_info['num_particles'], initial_state=initial_state, qubit_mapping=core._qubit_mapping, two_qubit_reduction=core._two_qubit_reduction)
def setUp(self): super().setUp() driver = HDF5Driver(hdf5_input=self.get_resource_path('test_driver_hdf5.hdf5')) self.qmolecule = driver.run() core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.PARITY, two_qubit_reduction=True, freeze_core=False, orbital_reduction=[]) self.qubit_op, self.aux_ops = core.run(self.qmolecule) self.reference_energy = -1.857275027031588
def _create_components_for_tests(self, path='', freeze_core=False, two_qubit_reduction=False, initial_point=None): """ Instantiate classes necessary to run the test out of HDF5 files of QMolecules.""" driver = HDF5Driver(hdf5_input=self.get_resource_path(path)) qmolecule = driver.run() core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.PARITY, two_qubit_reduction=two_qubit_reduction, freeze_core=freeze_core, orbital_reduction=[]) algo_input = core.run(qmolecule) qubit_op = algo_input[0] init_state = HartreeFock( num_orbitals=core._molecule_info['num_orbitals'], qubit_mapping=core._qubit_mapping, two_qubit_reduction=core._two_qubit_reduction, num_particles=core._molecule_info['num_particles']) var_form = UCCSD( num_orbitals=core._molecule_info['num_orbitals'], num_particles=core._molecule_info['num_particles'], active_occupied=None, active_unoccupied=None, initial_state=init_state, qubit_mapping=core._qubit_mapping, two_qubit_reduction=core._two_qubit_reduction, num_time_slices=1, method_doubles='pucc', same_spin_doubles=False, method_singles='both', skip_commute_test=True, excitation_type='d') algo = OOVQE(operator=qubit_op, var_form=var_form, optimizer=self.optimizer, core=core, qmolecule=qmolecule, expectation=MatrixExpectation(), initial_point=initial_point) return qmolecule, core, qubit_op, var_form, algo
def setUp(self): driver = HDF5Driver(hdf5_input=self._get_resource_path('test_driver_hdf5.hdf5')) self.qmolecule = driver.run()