Esempio n. 1
0
def test_not_available_transformation():
    r"""Test that an error is raised if the chosen fermionic-to-qubit transformation
    is neither 'jordan_wigner' nor 'bravyi_kitaev'."""

    with pytest.raises(TypeError, match="transformation is not available"):
        qchem.decompose(
            os.path.join(ref_dir, "lih"),
            mapping="not_available_transformation",
            core=[0],
            active=[1, 2],
        )
Esempio n. 2
0
def test_transformation(name, core, active, mapping, coeffs_ref, pauli_strings_ref, tol):
    r"""Test the correctness of the decomposed Hamiltonian for the (:math: `H_2, H_2O, LiH`)
    molecules using Jordan-Wigner and Bravyi-Kitaev transformations."""

    hf_file = os.path.join(ref_dir, name)

    qubit_hamiltonian = qchem.decompose(hf_file, mapping=mapping, core=core, active=active)

    coeffs = np.array(list(qubit_hamiltonian.terms.values()))
    pauli_strings = list(qubit_hamiltonian.terms.keys())

    assert np.allclose(coeffs, coeffs_ref, **tol)
    assert pauli_strings == pauli_strings_ref
def test_integration_mol_file_to_vqe_cost(name, core, active, mapping,
                                          expected_cost, custom_wires, tol):
    r"""Test if the output of `decompose()` works with `convert_observable()`
    to generate `ExpvalCost()`"""

    ref_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                           "test_ref_files")
    hf_file = os.path.join(ref_dir, name)
    qubit_hamiltonian = qchem.decompose(
        hf_file,
        mapping=mapping,
        core=core,
        active=active,
    )

    vqe_hamiltonian = qchem.convert_observable(qubit_hamiltonian, custom_wires)
    assert len(vqe_hamiltonian.ops) > 1  # just to check if this runs

    num_qubits = len(vqe_hamiltonian.wires)
    assert num_qubits == 2 * len(active)

    if custom_wires is None:
        wires = num_qubits
    elif isinstance(custom_wires, dict):
        wires = qchem.structure._process_wires(custom_wires)
    else:
        wires = custom_wires[:num_qubits]
    dev = qml.device("default.qubit", wires=wires)

    # can replace the ansatz with more suitable ones later.
    def dummy_ansatz(phis, wires):
        for phi, w in zip(phis, wires):
            qml.RX(phi, wires=w)

    phis = np.load(os.path.join(ref_dir, "dummy_ansatz_parameters.npy"))

    dummy_cost = qml.ExpvalCost(dummy_ansatz, vqe_hamiltonian, dev)
    res = dummy_cost(phis)

    assert np.abs(res - expected_cost) < tol["atol"]
Esempio n. 4
0
# <https://en.wikipedia.org/wiki/Jordan%E2%80%93Wigner_transformation>`__ or `Bravyi-Kitaev
# <https://arxiv.org/abs/1208.5986>`__ transformation [#seeley2012]_ to map it to a
# linear combination of tensor products of Pauli operators
#
# .. math::
#     \sum_j C_j \prod_i \sigma_i^{(j)},
#
# where :math:`C_j` is a scalar coefficient and :math:`\sigma_i^{(j)}` denotes the :math:`j`-th
# Pauli matrix :math:`X`, :math:`Y` or :math:`Z` acting on the :math:`i`-th qubit.
# To perform the fermionic-to-qubit transformation of the electronic Hamiltonian, the one-body
# and two-body Coulomb matrix elements :math:`h_{pq}` and :math:`h_{pqrs}`
# [#jensenbook]_ describing the fermionic Hamiltonian are retrieved from the
# previously generated file ``'./pyscf/sto-3g/water.hdf5'``.

qubit_hamiltonian = qchem.decompose(hf_file,
                                    mapping="jordan_wigner",
                                    core=core,
                                    active=active)
print(
    "Electronic Hamiltonian of the water molecule represented in the Pauli basis"
)
print(qubit_hamiltonian)

##############################################################################
# Finally, the :func:`~.pennylane_qchem.qchem.molecular_hamiltonian`
# function is used to automate the construction of the electronic Hamiltonian using
# the functions described above.
#
# An example usage is shown below:

H, qubits = qchem.molecular_hamiltonian(name,
                                        'h2o.xyz',