def test_conversion_from_folder(names, tmpdir, requires_babel): r"""Test the conversion of molecular structure file with different formats""" filename = os.path.join(ref_dir, names[0]) qchem.read_structure(filename, outpath=tmpdir) with open(tmpdir.join("structure.xyz")) as g: gen_file = g.readlines()[2:] with open(os.path.join(ref_dir, names[1])) as f: ref_file = f.readlines()[2:] assert gen_file == ref_file
def test_reading_xyz_file(tmpdir): r"""Test reading of the generated file 'structure.xyz'""" ref_symbols = ["C", "C", "N", "H", "H", "H", "H", "H"] ref_coords = np.array([ 0.68219113, -0.85415621, -1.04123909, -1.34926445, 0.23621577, 0.61794044, 1.29068294, 0.25133357, 1.40784596, 0.83525895, -2.88939124, -1.16974047, 1.26989596, 0.19275206, -2.69852891, -2.57758643, -1.05824663, 1.61949529, -2.17129532, 2.04090421, 0.11338357, 2.06547065, 2.00877887, 1.20186582, ]) name = os.path.join(ref_dir, "gdb3.mol5.XYZ") symbols, coordinates = qchem.read_structure(name, outpath=tmpdir) assert symbols == ref_symbols assert np.allclose(coordinates, ref_coords)
def test_subprocess_run(monkeypatch, requires_babel): r"""Test 'subprocess.run' function running babel to convert the molecular structure file to xyz format""" with monkeypatch.context() as m: def fake_run(*args, **kwargs): raise subprocess.CalledProcessError(1, "obabel") m.setattr(subprocess, "run", fake_run) with pytest.raises( RuntimeError, match= "Open Babel error. See the following Open Babel output for details" ): qchem.read_structure("fake_mol_geo.SDF")
def test_reading_xyz_file(tmpdir): r"""Test reading of the generated file 'structure.xyz'""" ref_geometry = [ ["C", (0.361, -0.452, -0.551)], ["C", (-0.714, 0.125, 0.327)], ["N", (0.683, 0.133, 0.745)], ["H", (0.442, -1.529, -0.619)], ["H", (0.672, 0.102, -1.428)], ["H", (-1.364, -0.56, 0.857)], ["H", (-1.149, 1.08, 0.060)], ["H", (1.093, 1.063, 0.636)], ] name = os.path.join(ref_dir, "gdb3.mol5.XYZ") geometry = qchem.read_structure(name, outpath=tmpdir) assert geometry == ref_geometry
Sit down, brew a hot drink, and let's take a look! Importing the molecular structure --------------------------------- The first step is to import the QChem package from PennyLane. """ from pennylane import qchem ############################################################################## # In this example, we construct the electronic Hamiltonian of one of the most unique # molecules: water. We begin by reading the positions of the oxygen and hydrogen atoms. The # equilibrium geometry of water is read from the file :download:`h2o.xyz </demonstrations/h2o.xyz>` # and stored in a list containing the symbol and the Cartesian coordinates of each atomic # species: geometry = qchem.read_structure('h2o.xyz') print("The total number of atoms is: {}".format(len(geometry))) print(geometry) ############################################################################## # .. note:: # # The xyz format is supported out of the box. # If `Open Babel <http://openbabel.org/wiki/Main_Page>`_ is installed, any # format recognized by Open Babel is also supported by PennyLane, such as # ``.mol`` and ``.sdf``. # # Please see the :func:`~.pennylane_qchem.qchem.read_structure` and Open Babel documentation # for more information on installing Open Babel. # # Calling the function :func:`~.pennylane_qchem.qchem.read_structure` also creates the file
Sit down, brew a hot drink, and let's take a look! Importing the molecular structure --------------------------------- The first step is to import the QChem package from PennyLane. """ from pennylane import qchem ############################################################################## # In this example, we construct the electronic Hamiltonian of one of the most unique # molecules: water. We begin by reading the positions of the oxygen and hydrogen atoms. The # equilibrium geometry of water is read from the file :download:`h2o.xyz </demonstrations/h2o.xyz>` # which retrieves the symbol and the Cartesian coordinates of each atomic species: symbols, coordinates = qchem.read_structure('h2o.xyz') print("The total number of atoms is: {}".format(len(symbols))) print(symbols) print(coordinates) ############################################################################## # .. note:: # # The xyz format is supported out of the box. # If `Open Babel <http://openbabel.org/wiki/Main_Page>`_ is installed, any # format recognized by Open Babel is also supported by PennyLane, such as # ``.mol`` and ``.sdf``. # # Please see the :func:`~.pennylane_qchem.qchem.read_structure` and Open Babel documentation # for more information on installing Open Babel. #
1.7: "vqe_parallel/h2_1.70.xyz", 1.9: "vqe_parallel/h2_1.90.xyz", 2.1: "vqe_parallel/h2_2.10.xyz", } ############################################################################## # The next step is to create the qubit Hamiltonians for each value of the inter-atomic distance. # We do this by first reading the molecular geometry from the external file using the # :func:`~.pennylane_qchem.qchem.read_structure` function and passing the atomic symbols # and coordinates to :func:`~.pennylane_qchem.qchem.molecular_hamiltonian`. hamiltonians = [] for separation, file in data.items(): symbols, coordinates = qchem.read_structure(file) h = qchem.molecular_hamiltonian(symbols, coordinates, name=str(separation))[0] hamiltonians.append(h) ############################################################################## # Each Hamiltonian can be written as a linear combination of fifteen tensor products of Pauli # matrices. Let's take a look more closely at one of the Hamiltonians: h = hamiltonians[0] print("Number of terms: {}\n".format(len(h.ops))) for op in h.ops: print("Measurement {} on wires {}".format(op.name, op.wires)) ############################################################################## # Defining the energy function
# set of 3 Gaussian functions are contracted to represent an atomic Slater-type orbital (STO): basis_set = 'sto-3g' ############################################################################## # At this stage, to compute the molecule's Hamiltonian in the Pauli basis, several # calculations need to be performed. With PennyLane, these can all be done in a # single line by calling the function :func:`~.pennylane_qchem.qchem.molecular_hamiltonian`. # The charge, multiplicity, and basis set can also be specified as keyword arguments. Finally, # the number of active electrons and active orbitals may be indicated, as well as the # fermionic-to-qubit mapping, which can be either Jordan-Wigner (``jordan_wigner``) or Bravyi-Kitaev # (``bravyi_kitaev``). The atomic symbols and their nuclear coordinates can be read directly # from the geometry file. The outputs of the function are the qubit Hamiltonian of the molecule # and the number of qubits needed to represent it: symbols, coordinates = qchem.read_structure(geometry) h, qubits = qchem.molecular_hamiltonian(symbols, coordinates, charge=charge, mult=multiplicity, basis=basis_set, active_electrons=2, active_orbitals=2, mapping='jordan_wigner') print('Number of qubits = ', qubits) print('Hamiltonian is ', h) ############################################################################## # That's it! From here on, we can use PennyLane as usual, employing its entire stack of
Importing the molecular structure --------------------------------- The first step is to import the QChem package from PennyLane. """ from pennylane import qchem ############################################################################## # In this example, we construct the electronic Hamiltonian of one of the most unique # molecules: water. We begin by reading the positions of the oxygen and hydrogen atoms. The # equilibrium geometry of water is read from the file :download:`h2o.xyz </demonstrations/h2o.xyz>` # and stored in a list containing the symbol and the Cartesian coordinates of each atomic # species: geometry = qchem.read_structure( '/workspace/my-examples-for-quantum-computing/pennylaneai/qml-demos/alldata/h2o.xyz' ) print("The total number of atoms is: {}".format(len(geometry))) print(geometry) ############################################################################## # .. note:: # # The xyz format is supported out of the box. # If `Open Babel <http://openbabel.org/wiki/Main_Page>`_ is installed, any # format recognized by Open Babel is also supported by PennyLane, such as # ``.mol`` and ``.sdf``. # # Please see the :func:`~.pennylane_qchem.qchem.read_structure` and Open Babel documentation # for more information on installing Open Babel. #