Пример #1
0
def test_single_qubit_basis_transfrom():
    '''
    Testing whether transformations using the binary form 
    and the transformation through direct computation agree
    '''
    H, n_qubits, binary_sol, coeff_sol = prepare_test_hamiltonian()

    single_qub_H, old_basis, new_basis = BinaryHamiltonian.init_from_qubit_hamiltonian(
        H).single_qubit_form()

    H_brute_force = brute_force_transformation(H, old_basis, new_basis)

    assert (equal_qubit_hamiltonian(single_qub_H.to_qubit_hamiltonian(),
                                    H_brute_force))

    H = -1.0 * paulis.X(0) * paulis.X(1) * paulis.X(2) + 2.0 * paulis.Y(
        0) * paulis.Y(1)

    single_qub_H, old_basis, new_basis = BinaryHamiltonian.init_from_qubit_hamiltonian(
        H).single_qubit_form()

    H_brute_force = brute_force_transformation(H, old_basis, new_basis)

    assert (equal_qubit_hamiltonian(single_qub_H.to_qubit_hamiltonian(),
                                    H_brute_force))
Пример #2
0
def test_qubit_wise_commuting():
    '''
    Testing whether method is_qubit_wise_commuting correctly 
    recognizes qubit wise commuting parts.
    '''
    not_qwc = -1.0 * paulis.Z(0) * paulis.Z(1) - 0.5 * paulis.Y(0) * paulis.Y(1)
    not_qwc = BinaryHamiltonian.init_from_qubit_hamiltonian(not_qwc)
    qwc = paulis.Z(0) * paulis.Z(1) + paulis.Z(1) * paulis.Y(2)
    qwc = BinaryHamiltonian.init_from_qubit_hamiltonian(qwc)

    assert not not_qwc.is_qubit_wise_commuting()
    assert qwc.is_qubit_wise_commuting()
Пример #3
0
def test_get_qubit_wise():
    '''
    Testing whether the get_qubit_wise methods correctly gives the all-Z form of the hamiltonian
    '''
    H, _, _, _ = prepare_test_hamiltonian()
    H = BinaryHamiltonian.init_from_qubit_hamiltonian(H)
    qwc, qwc_U = H.get_qubit_wise()

    # Check qwc has all z
    for term, val in qwc.items():
        for qub in term:
            assert qub[1] == 'Z'

    # Checking the expectation values are the same
    U = tq.gates.ExpPauli(angle="a", paulistring=tq.PauliString.from_string('X(0)Y(1)'))
    variables = {"a": np.random.rand(1) * 2 * np.pi}

    e_ori = tq.ExpectationValue(H=H.to_qubit_hamiltonian(), U=U)
    e_qwc = tq.ExpectationValue(H=qwc, U=U + qwc_U)
    e_integrated = tq.ExpectationValue(H=H.to_qubit_hamiltonian(), U=U, optimize_measurements=True)
    result_ori = tq.simulate(e_ori, variables)
    result_qwc = tq.simulate(e_qwc, variables)
    result_integrated = tq.simulate(e_qwc, variables)

    assert np.isclose(result_ori, result_qwc)
    assert np.isclose(result_ori, result_integrated)

    # Checking the optimized expectation values are the same
    initial_values = {k: np.random.uniform(0.0, 6.0, 1) for k in e_ori.extract_variables()}
    sol1 = tq.minimize(method='bfgs', objective=e_ori, initial_values=initial_values)
    sol2 = tq.minimize(method='bfgs', objective=e_qwc, initial_values=initial_values)
    sol3 = tq.minimize(method='bfgs', objective=e_integrated, initial_values=initial_values)

    assert np.isclose(sol1.energy, sol2.energy)
    assert np.isclose(sol1.energy, sol3.energy)
Пример #4
0
def test_to_qubit_hamiltonian():
    '''
    Testing transformation to qubit hamiltonian 
    '''
    H, n_qubits, binary_sol, coeff_sol = prepare_test_hamiltonian()

    binary_hamiltonian = BinaryHamiltonian.init_from_qubit_hamiltonian(H)

    assert (equal_qubit_hamiltonian(H,
                                    binary_hamiltonian.to_qubit_hamiltonian()))
Пример #5
0
def test_binary_hamiltonian_initialization():
    '''
    Testing binary form of the hamiltonian
    '''
    H, n_qubits, binary_sol, coeff_sol = prepare_test_hamiltonian()

    H_binary = BinaryHamiltonian.init_from_qubit_hamiltonian(H)

    binary_equal_matrix = np.array(H_binary.get_binary()) == binary_sol

    assert (binary_equal_matrix.all())
    assert (all(np.array(H_binary.get_coeff()) == coeff_sol))
Пример #6
0
def test_commuting_groups():
    '''
    Testing whether the partitioning gives commuting parts
    '''
    H, _, _, _ = prepare_test_hamiltonian()
    H = H + paulis.X(0) + paulis.Y(0)
    H = BinaryHamiltonian.init_from_qubit_hamiltonian(H)

    commuting_parts = H.commuting_groups()

    for part in commuting_parts:
        assert part.is_commuting()
Пример #7
0
def get_qwc_unitary(H: QubitOperator):
    '''
    Get the unitary that transform commuting operators to qwc operators
    '''
    qh = QubitHamiltonian.from_openfermion(H)
    bh = BinaryHamiltonian.init_from_qubit_hamiltonian(qh)

    qwc, lag, sig = bh.single_qubit_form()

    num = len(lag)
    U = QubitOperator.identity()

    for idx in range(num):
        l = QubitHamiltonian.from_paulistrings(lag[idx].to_pauli_strings())
        s = QubitHamiltonian.from_paulistrings(sig[idx].to_pauli_strings())
        U *= 1 / 2**(1 / 2) * (l.to_openfermion() + s.to_openfermion())
    return U
Пример #8
0
def ExpectationValue(U,
                     H,
                     optimize_measurements: bool = False,
                     *args,
                     **kwargs) -> Objective:
    """
    Initialize an Objective which is just a single expectationvalue
    """
    if optimize_measurements:
        binary_H = BinaryHamiltonian.init_from_qubit_hamiltonian(H)
        commuting_parts = binary_H.commuting_groups()
        result = Objective()
        for cH in commuting_parts:
            qwc, Um = cH.get_qubit_wise()
            Etmp = ExpectationValue(H=qwc,
                                    U=U + Um,
                                    optimize_measurements=False)
            result += Etmp
        return result
    else:
        return Objective.ExpectationValue(U=U, H=H, *args, **kwargs)