示例#1
0
def test_integration_hamiltonian_to_vqe_cost(monkeypatch, mol_name, terms_ref,
                                             expected_cost, tol):
    r"""Test if `convert_hamiltonian()` in qchem integrates with `VQECost()` in pennylane"""

    qOp = QubitOperator()
    if terms_ref is not None:
        monkeypatch.setattr(qOp, "terms", terms_ref)
    vqe_hamiltonian = qchem.convert_hamiltonian(qOp)

    # maybe make num_qubits a @property of the Hamiltonian class?
    num_qubits = max(
        1, len(set([w for op in vqe_hamiltonian.ops for w in op.wires])))

    dev = qml.device("default.qubit", wires=num_qubits)
    print(vqe_hamiltonian.terms)

    # 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)

    dummy_cost = qml.VQECost(dummy_ansatz, vqe_hamiltonian, dev)
    params = [0.1 * i for i in range(num_qubits)]
    res = dummy_cost(params)

    assert np.allclose(res, expected_cost, **tol)
示例#2
0
def test_hamiltonian_conversion(mol_name, terms_ref, monkeypatch):
    r"""Test the correctness of the QubitOperator Hamiltonian conversion from
    OpenFermion to Pennylane.

    The parametrized inputs are `.terms` attribute of the output `QubitOperator`s based on
    the same set of test molecules as `test_gen_hamiltonian_pauli_basis`.

    The equality checking is implemented in the `qchem` module itself as it could be
    something useful to the users as well.
    """
    qOp = QubitOperator()
    if terms_ref is not None:
        monkeypatch.setattr(qOp, "terms", terms_ref)

    vqe_hamiltonian = qchem.convert_hamiltonian(qOp)

    assert qchem._qubit_operators_equivalent(qOp, vqe_hamiltonian)
def test_integration_mol_file_to_vqe_cost(hf_filename, docc_mo, act_mo,
                                          type_of_transformation,
                                          expected_cost, tol):
    r"""Test if the output of `decompose_hamiltonian()` works with `convert_hamiltonian()`
    to generate `VQECost()`"""
    ref_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                           "test_ref_files")

    transformed_hamiltonian = qchem.decompose_hamiltonian(
        hf_filename,
        ref_dir,
        mapping=type_of_transformation,
        docc_mo_indices=docc_mo,
        active_mo_indices=act_mo,
    )

    vqe_hamiltonian = qchem.convert_hamiltonian(transformed_hamiltonian)
    assert len(vqe_hamiltonian.ops) > 1  # just to check if this runs

    num_qubits = max(
        1,
        len(
            set([
                w for op in vqe_hamiltonian.ops for ws in op.wires for w in ws
            ])))
    assert num_qubits == 2 * len(act_mo)

    dev = qml.device("default.qubit", wires=num_qubits)

    # 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.VQECost(dummy_ansatz, vqe_hamiltonian, dev)
    res = dummy_cost(phis)

    assert np.abs(res - expected_cost) < tol["atol"]