def test_observable(me_table, init_term, mapping, terms_exp, monkeypatch): r"""Tests the correctness of the 'observable' function used to build many-body observables. The parametrized inputs `terms_exp` are `.terms` attribute of the corresponding `QubitOperator. The equality checking is implemented in the `qchem` module itself as it could be something useful to the users as well. """ res_obs = qchem.observable(me_table, init_term=init_term, mapping=mapping) qubit_op = QubitOperator() monkeypatch.setattr(qubit_op, "terms", terms_exp) assert qchem._qubit_operators_equivalent(qubit_op, res_obs)
def test_spin_z(orbitals, mapping, terms_exp, monkeypatch): r"""Tests the correctness of the :math:`\hat{S}_z` observable built by the function `'spin_z'`. The parametrized inputs are `.terms` attribute of the `QubitOperator. The equality checking is implemented in the `qchem` module itself as it could be something useful to the users as well. """ Sz = qchem.spin_z(orbitals, mapping=mapping) Sz_qubit_op = QubitOperator() monkeypatch.setattr(Sz_qubit_op, "terms", terms_exp) assert qchem._qubit_operators_equivalent(Sz_qubit_op, Sz)
def test_particle_number_observable(n_orbitals, mapping, terms_exp, monkeypatch): r"""Tests the correctness of the particle number observable :math:`\hat{N}` generated by the ``'particle_number'`` function. The parametrized inputs are `.terms` attribute of the particle number `QubitOperator`. The equality checking is implemented in the `qchem` module itself as it could be something useful to the users as well. """ N = qchem.particle_number(n_orbitals, mapping=mapping) particle_number_qubit_op = QubitOperator() monkeypatch.setattr(particle_number_qubit_op, "terms", terms_exp) assert qchem._qubit_operators_equivalent(particle_number_qubit_op, N)
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_build_s2_observable(mol_name, n_act_elect, n_act_orb, mapping, terms_exp, monkeypatch): r"""Tests the correctness of the built total-spin observable. The parametrized inputs are `.terms` attribute of the total spin `QubitOperator. The equality checking is implemented in the `qchem` module itself as it could be something useful to the users as well. """ s2_me_table, init_term = qchem.get_spin2_matrix_elements( mol_name, ref_dir, n_active_electrons=n_act_elect, n_active_orbitals=n_act_orb ) s2_obs = qchem.observable(s2_me_table, init_term=init_term, mapping=mapping) s2_qubit_op = QubitOperator() monkeypatch.setattr(s2_qubit_op, "terms", terms_exp) assert qchem._qubit_operators_equivalent(s2_qubit_op, s2_obs)
def test_observable_conversion(mol_name, terms_ref, custom_wires, monkeypatch): r"""Test the correctness of the QubitOperator observable 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_observable = qchem.convert_observable(qOp, custom_wires) if isinstance(custom_wires, dict): custom_wires = {v: k for k, v in custom_wires.items()} assert qchem._qubit_operators_equivalent(qOp, vqe_observable, custom_wires)