示例#1
0
    def test_random_operators_are_reproducible(self):
        op1 = random_diagonal_coulomb_hamiltonian(5, seed=5947)
        op2 = random_diagonal_coulomb_hamiltonian(5, seed=5947)
        numpy.testing.assert_allclose(op1.one_body, op2.one_body)
        numpy.testing.assert_allclose(op1.two_body, op2.two_body)

        op1 = random_interaction_operator(5, seed=8911)
        op2 = random_interaction_operator(5, seed=8911)
        numpy.testing.assert_allclose(op1.one_body_tensor, op2.one_body_tensor)
        numpy.testing.assert_allclose(op1.two_body_tensor, op2.two_body_tensor)

        op1 = random_quadratic_hamiltonian(5, seed=17711)
        op2 = random_quadratic_hamiltonian(5, seed=17711)
        numpy.testing.assert_allclose(op1.combined_hermitian_part,
                                      op2.combined_hermitian_part)
        numpy.testing.assert_allclose(op1.antisymmetric_part,
                                      op2.antisymmetric_part)

        op1 = random_antisymmetric_matrix(5, seed=24074)
        op2 = random_antisymmetric_matrix(5, seed=24074)
        numpy.testing.assert_allclose(op1, op2)

        op1 = random_hermitian_matrix(5, seed=56753)
        op2 = random_hermitian_matrix(5, seed=56753)
        numpy.testing.assert_allclose(op1, op2)

        op1 = random_unitary_matrix(5, seed=56486)
        op2 = random_unitary_matrix(5, seed=56486)
        numpy.testing.assert_allclose(op1, op2)
    def test_integration_random_diagonal_coulomb_hamiltonian(self):
        hamiltonian1 = normal_ordered(
            get_fermion_operator(
                random_diagonal_coulomb_hamiltonian(n_qubits=7)))
        hamiltonian2 = normal_ordered(
            get_fermion_operator(
                random_diagonal_coulomb_hamiltonian(n_qubits=7)))

        reference = normal_ordered(commutator(hamiltonian1, hamiltonian2))
        result = commutator_ordered_diagonal_coulomb_with_two_body_operator(
            hamiltonian1, hamiltonian2)

        self.assertTrue(result.isclose(reference))
 def test_multiply(self):
     n_qubits = 5
     op1 = random_diagonal_coulomb_hamiltonian(n_qubits)
     op2 = op1 * 1.5
     op3 = 1.5 * op1
     self.assertEqual(get_fermion_operator(op1) * 1.5,
                      get_fermion_operator(op2),
                      get_fermion_operator(op3))
示例#4
0
def test_simulate_trotter_unsupported_trotter_step_raises_error():
    qubits = cirq.LineQubit.range(2)
    control = cirq.LineQubit(-1)
    hamiltonian = random_diagonal_coulomb_hamiltonian(2, seed=0)
    time = 1.0

    class EmptyTrotterAlgorithm(TrotterAlgorithm):
        supported_types = {openfermion.DiagonalCoulombHamiltonian}

    algorithm = EmptyTrotterAlgorithm()
    with pytest.raises(ValueError):
        _ = next(
            simulate_trotter(qubits,
                             hamiltonian,
                             time,
                             order=0,
                             algorithm=algorithm))
    with pytest.raises(ValueError):
        _ = next(
            simulate_trotter(qubits,
                             hamiltonian,
                             time,
                             order=1,
                             algorithm=algorithm))
    with pytest.raises(ValueError):
        _ = next(
            simulate_trotter(qubits,
                             hamiltonian,
                             time,
                             order=0,
                             algorithm=algorithm,
                             control_qubit=control))
    with pytest.raises(ValueError):
        _ = next(
            simulate_trotter(qubits,
                             hamiltonian,
                             time,
                             order=1,
                             algorithm=algorithm,
                             control_qubit=control))
        if initial_guess_array is None:
            # coverage: ignore
            initial_guess_array = numpy.ones((3, black_box.dimension))
        a = black_box.evaluate(initial_guess)
        b = black_box.evaluate_with_cost(initial_guess_array[0], 1.0)
        return OptimizationResult(optimal_value=min(a, b),
                                  optimal_parameters=initial_guess,
                                  num_evaluations=1,
                                  cost_spent=0.0,
                                  status=0,
                                  message='success')


# Construct a Hamiltonian for testing
test_hamiltonian = random_diagonal_coulomb_hamiltonian(4,
                                                       real=True,
                                                       seed=26191)
test_fermion_op = openfermion.get_fermion_operator(test_hamiltonian)


def test_hamiltonian_variational_study_init_qubit_operator():

    ansatz = SwapNetworkTrotterAnsatz(test_hamiltonian)
    study = HamiltonianVariationalStudy('study', ansatz,
                                        openfermion.QubitOperator((0, 'X')))
    assert study.hamiltonian == openfermion.QubitOperator((0, 'X'))


def test_hamiltonian_variational_study_noise():

    ansatz = SwapNetworkTrotterAnsatz(test_hamiltonian)
 def test_divide(self):
     n_qubits = 5
     op1 = random_diagonal_coulomb_hamiltonian(n_qubits)
     op2 = op1 / 1.5
     self.assertEqual(get_fermion_operator(op1) / 1.5,
                      get_fermion_operator(op2))
示例#7
0
def test_simulate_trotter_bad_order_raises_error():
    qubits = cirq.LineQubit.range(2)
    hamiltonian = random_diagonal_coulomb_hamiltonian(2, seed=0)
    time = 1.0
    with pytest.raises(ValueError):
        _ = next(simulate_trotter(qubits, hamiltonian, time, order=-1))