def test_list_atom(self): """ Check input with list of strings """ atom = ['H 0 0 0', 'H 0 0 1'] driver = PySCFDriver(atom=atom, unit=UnitsType.ANGSTROM, charge=0, spin=0, basis='sto3g') molecule = driver.run() self.assertAlmostEqual(molecule.hf_energy, -1.0661086493179366, places=5)
def setUp(self): super().setUp() self.core_energy = 0.7199 self.num_molecular_orbitals = 2 self.num_electrons = 2 self.spin_number = 0 self.wf_symmetry = 1 self.orb_symmetries = [1, 1] self.mo_onee = [[1.2563, 0.0], [0.0, 0.4719]] self.mo_eri = [0.6757, 0.0, 0.1809, 0.6646, 0.0, 0.6986] try: driver = PySCFDriver( atom="H .0 .0 .0; H .0 .0 0.735", unit=UnitsType.ANGSTROM, charge=0, spin=0, basis="sto3g", ) qmolecule = driver.run() with tempfile.NamedTemporaryFile() as dump: FCIDumpDriver.dump(qmolecule, dump.name) # pylint: disable=import-outside-toplevel from pyscf.tools import fcidump as pyscf_fcidump self.dumped = pyscf_fcidump.read(dump.name) except QiskitNatureError: self.skipTest("PYSCF driver does not appear to be installed.") except ImportError: self.skipTest("PYSCF driver does not appear to be installed.")
def setUp(self): super().setUp() try: driver = PySCFDriver(molecule=TestDriver.MOLECULE) except QiskitNatureError: self.skipTest("PYSCF driver does not appear to be installed") self.qmolecule = driver.run()
def setUp(self): super().setUp() driver = PySCFDriver( atom="H .0 .0 .0; H .0 .0 0.735", unit=UnitsType.ANGSTROM, charge=0, spin=0, basis="sto3g", ) self.qmolecule = driver.run()
def test_h4(self): """Test for H4 chain""" atom = "H 0 0 0; H 0 0 1; H 0 0 2; H 0 0 3" driver = PySCFDriver(atom=atom, unit=UnitsType.ANGSTROM, charge=0, spin=0, basis="sto3g") molecule = driver.run() self.assertAlmostEqual(molecule.hf_energy, -2.09854593699776, places=5)
def setUp(self): super().setUp() try: driver = PySCFDriver(atom='H .0 .0 .0; H .0 .0 0.735', unit=UnitsType.ANGSTROM, charge=0, spin=0, basis='sto3g') except QiskitNatureError: self.skipTest('PYSCF driver does not appear to be installed') self.qmolecule = driver.run()
def test_zmatrix(self): """Check z-matrix input""" atom = "H; H 1 1.0" driver = PySCFDriver(atom=atom, unit=UnitsType.ANGSTROM, charge=0, spin=0, basis="sto3g") molecule = driver.run() self.assertAlmostEqual(molecule.hf_energy, -1.0661086493179366, places=5)
def test_h3(self): """Test for H3 chain, see also issue 1148""" atom = "H 0 0 0; H 0 0 1; H 0 0 2" driver = PySCFDriver(atom=atom, unit=UnitsType.ANGSTROM, charge=0, spin=1, basis="sto3g") molecule = driver.run() self.assertAlmostEqual(molecule.hf_energy, -1.523996200246108, places=5)
def test_mp2_h2(self): """ Just one double excitation expected - see issue 1151 """ driver = PySCFDriver(atom="H 0 0 0; H 0 0 0.7", unit=UnitsType.ANGSTROM, charge=0, spin=0, basis='sto3g') molecule = driver.run() mp2info = MP2Info(molecule) terms = mp2info.mp2_terms() self.assertEqual(1, len(terms.keys())) np.testing.assert_array_almost_equal( [-0.06834019757197064, -0.012232934733533095], terms['0_1_2_3'], decimal=6)
def get_qubit_op(self, dist, mapper='jw'): # 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=self.molecule_name + str(dist), unit=UnitsType.ANGSTROM, charge=0, spin=0, basis='sto3g') molecule = driver.run() es_problem = ElectronicStructureProblem(driver) if mapper == 'jw': qubit_converter = QubitConverter(mapper=JordanWignerMapper()) elif mapper == 'parity': qubit_converter = QubitConverter(mapper=ParityMapper(), two_qubit_reduction=True) #second_q_ops = es_problem.second_q_ops() #num_particles = es_problem.num_particles #molecule_data = es_problem.molecule_data #electronic_operator = second_q_ops[0] return es_problem, qubit_converter, molecule.nuclear_repulsion_energy
def setUp(self): super().setUp() driver = PySCFDriver(molecule=TestDriver.MOLECULE) self.qmolecule = driver.run()
def test_readme_sample(self): """ readme sample test """ # pylint: disable=import-outside-toplevel,redefined-builtin def print(*args): """ overloads print to log values """ if args: self.log.debug(args[0], *args[1:]) # --- Exact copy of sample code ---------------------------------------- from qiskit_nature import FermionicOperator from qiskit_nature.drivers import PySCFDriver, UnitsType from qiskit.opflow import TwoQubitReduction # 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', unit=UnitsType.ANGSTROM, 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 ferm_op = FermionicOperator(h1=molecule.one_body_integrals, h2=molecule.two_body_integrals) map_type = 'PARITY' qubit_op = ferm_op.mapping(map_type) qubit_op = TwoQubitReduction( num_particles=num_particles).convert(qubit_op) num_qubits = qubit_op.num_qubits # setup a classical optimizer for VQE from qiskit.algorithms.optimizers import L_BFGS_B optimizer = L_BFGS_B() # setup the initial state for the variational form from qiskit_nature.circuit.library import HartreeFock init_state = HartreeFock(num_spin_orbitals, num_particles) # setup the variational form for VQE from qiskit.circuit.library import TwoLocal var_form = TwoLocal(num_qubits, ['ry', 'rz'], 'cz') # add the initial state var_form.compose(init_state, front=True) # set the backend for the quantum computation from qiskit import Aer backend = Aer.get_backend('statevector_simulator') # setup and run VQE from qiskit.algorithms import VQE algorithm = VQE(var_form, optimizer=optimizer, quantum_instance=backend) result = algorithm.compute_minimum_eigenvalue(qubit_op) print(result.eigenvalue.real) # ---------------------------------------------------------------------- self.assertAlmostEqual(result.eigenvalue.real, -1.8572750301938803, places=6)
class TestAdaptVQEUCCSD(QiskitNatureTestCase): """ Test Adaptive VQE with UCCSD""" def setUp(self): super().setUp() # np.random.seed(50) self.seed = 50 algorithm_globals.random_seed = self.seed try: self.driver = PySCFDriver(atom='H .0 .0 .0; H .0 .0 0.735', unit=UnitsType.ANGSTROM, basis='sto3g') except QiskitNatureError: self.skipTest('PYSCF driver does not appear to be installed') return molecule = self.driver.run() self.num_particles = molecule.num_alpha + molecule.num_beta self.num_spin_orbitals = molecule.num_orbitals * 2 fer_op = FermionicOperator(h1=molecule.one_body_integrals, h2=molecule.two_body_integrals) map_type = 'PARITY' qubit_op = fer_op.mapping(map_type) self.qubit_op = TwoQubitReduction( num_particles=self.num_particles).convert(qubit_op) self.num_qubits = self.qubit_op.num_qubits self.init_state = HartreeFock(self.num_spin_orbitals, self.num_particles) self.var_form_base = None def test_uccsd_adapt(self): """ UCCSD test for adaptive features """ self.var_form_base = UCCSD(self.num_spin_orbitals, self.num_particles, initial_state=self.init_state) self.var_form_base.manage_hopping_operators() # assert that the excitation pool exists self.assertIsNotNone(self.var_form_base.excitation_pool) # assert that the hopping ops list has been reset to be empty self.assertEqual(self.var_form_base._hopping_ops, []) def test_vqe_adapt(self): """ AdaptVQE test """ try: # pylint: disable=import-outside-toplevel from qiskit import Aer backend = Aer.get_backend('statevector_simulator') except ImportError as ex: # pylint: disable=broad-except self.skipTest( "Aer doesn't appear to be installed. Error: '{}'".format( str(ex))) return class CustomFactory(VQEUCCSDFactory): """A custom MESFactory""" def get_solver(self, transformation): num_orbitals = transformation.molecule_info['num_orbitals'] num_particles = transformation.molecule_info['num_particles'] initial_state = HartreeFock(num_orbitals, num_particles) var_form = UCCSD(num_orbitals, num_particles, initial_state=initial_state) vqe = VQE(var_form=var_form, quantum_instance=self._quantum_instance, optimizer=L_BFGS_B()) return vqe algorithm = AdaptVQE(FermionicTransformation(), solver=CustomFactory(QuantumInstance(backend)), threshold=0.00001, delta=0.1, max_iterations=1) result = algorithm.solve(driver=self.driver) self.assertEqual(result.num_iterations, 1) self.assertEqual(result.finishing_criterion, 'Maximum number of iterations reached') algorithm = AdaptVQE(FermionicTransformation(), solver=CustomFactory(QuantumInstance(backend)), threshold=0.00001, delta=0.1) result = algorithm.solve(driver=self.driver) self.assertAlmostEqual(result.electronic_energies[0], -1.85727503, places=2) self.assertEqual(result.num_iterations, 2) self.assertAlmostEqual(result.final_max_gradient, 0.0, places=5) self.assertEqual(result.finishing_criterion, 'Threshold converged') def test_vqe_adapt_check_cyclicity(self): """ AdaptVQE index cycle detection """ param_list = [ ([1, 1], True), ([1, 11], False), ([11, 1], False), ([1, 12], False), ([12, 2], False), ([1, 1, 1], True), ([1, 2, 1], False), ([1, 2, 2], True), ([1, 2, 21], False), ([1, 12, 2], False), ([11, 1, 2], False), ([1, 2, 1, 1], True), ([1, 2, 1, 2], True), ([1, 2, 1, 21], False), ([11, 2, 1, 2], False), ([1, 11, 1, 111], False), ([11, 1, 111, 1], False), ([1, 2, 3, 1, 2, 3], True), ([1, 2, 3, 4, 1, 2, 3], False), ([11, 2, 3, 1, 2, 3], False), ([1, 2, 3, 1, 2, 31], False), ([1, 2, 3, 4, 1, 2, 3, 4], True), ([11, 2, 3, 4, 1, 2, 3, 4], False), ([1, 2, 3, 4, 1, 2, 3, 41], False), ([1, 2, 3, 4, 5, 1, 2, 3, 4], False), ] for seq, is_cycle in param_list: with self.subTest(msg="Checking index cyclicity in:", seq=seq): self.assertEqual(is_cycle, AdaptVQE._check_cyclicity(seq))