Ejemplo n.º 1
0
def test_simulate_trotter_bad_hamiltonian_type_raises_error():
    qubits = cirq.LineQubit.range(2)
    hamiltonian = openfermion.FermionOperator()
    time = 1.0
    with pytest.raises(TypeError):
        _ = next(simulate_trotter(qubits, hamiltonian, time, algorithm=None))
    with pytest.raises(TypeError):
        _ = next(
            simulate_trotter(qubits,
                             hamiltonian,
                             time,
                             algorithm=LINEAR_SWAP_NETWORK))
Ejemplo n.º 2
0
def test_simulate_trotter_unsupported_trotter_step_raises_error():
    qubits = cirq.LineQubit.range(2)
    control = cirq.LineQubit(-1)
    hamiltonian = openfermion.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))
Ejemplo n.º 3
0
def test_simulate_trotter_simulate(hamiltonian, time, initial_state,
                                   exact_state, order, n_steps, algorithm,
                                   result_fidelity):

    n_qubits = openfermion.count_qubits(hamiltonian)
    qubits = cirq.LineQubit.range(n_qubits)

    start_state = initial_state

    circuit = cirq.Circuit(
        simulate_trotter(qubits, hamiltonian, time, n_steps, order, algorithm))

    final_state = circuit.final_state_vector(initial_state=start_state)
    correct_state = exact_state
    assert fidelity(final_state, correct_state) > result_fidelity
    # Make sure the time wasn't too small
    assert fidelity(final_state, start_state) < 0.95 * result_fidelity
Ejemplo n.º 4
0
def test_simulate_trotter_simulate_controlled(hamiltonian, time, initial_state,
                                              exact_state, order, n_steps,
                                              algorithm, result_fidelity):

    n_qubits = openfermion.count_qubits(hamiltonian)
    qubits = cirq.LineQubit.range(n_qubits)

    control = cirq.LineQubit(-1)
    zero = [1, 0]
    one = [0, 1]
    start_state = (numpy.kron(zero, initial_state) +
                   numpy.kron(one, initial_state)) / numpy.sqrt(2)

    circuit = cirq.Circuit(
        simulate_trotter(qubits, hamiltonian, time, n_steps, order, algorithm,
                         control))

    final_state = circuit.final_state_vector(initial_state=start_state)
    correct_state = (numpy.kron(zero, initial_state) +
                     numpy.kron(one, exact_state)) / numpy.sqrt(2)
    assert fidelity(final_state, correct_state) > result_fidelity
    # Make sure the time wasn't too small
    assert fidelity(final_state, start_state) < 0.95 * result_fidelity
Ejemplo n.º 5
0
def test_simulate_trotter_bad_order_raises_error():
    qubits = cirq.LineQubit.range(2)
    hamiltonian = openfermion.random_diagonal_coulomb_hamiltonian(2, seed=0)
    time = 1.0
    with pytest.raises(ValueError):
        _ = next(simulate_trotter(qubits, hamiltonian, time, order=-1))
Ejemplo n.º 6
0
def test_simulate_trotter_omit_final_swaps():
    n_qubits = 5
    qubits = cirq.LineQubit.range(n_qubits)
    hamiltonian = openfermion.DiagonalCoulombHamiltonian(one_body=numpy.ones(
        (n_qubits, n_qubits)),
                                                         two_body=numpy.ones(
                                                             (n_qubits,
                                                              n_qubits)))
    time = 1.0

    circuit_with_swaps = cirq.Circuit(
        simulate_trotter(qubits,
                         hamiltonian,
                         time,
                         order=0,
                         algorithm=LINEAR_SWAP_NETWORK))
    circuit_without_swaps = cirq.Circuit(
        simulate_trotter(qubits,
                         hamiltonian,
                         time,
                         order=0,
                         algorithm=LINEAR_SWAP_NETWORK,
                         omit_final_swaps=True))

    assert len(circuit_without_swaps) < len(circuit_with_swaps)

    circuit_with_swaps = cirq.Circuit(simulate_trotter(
        qubits,
        hamiltonian,
        time,
        order=1,
        n_steps=3,
        algorithm=SPLIT_OPERATOR),
                                      strategy=cirq.InsertStrategy.NEW)
    circuit_without_swaps = cirq.Circuit(simulate_trotter(
        qubits,
        hamiltonian,
        time,
        order=1,
        n_steps=3,
        algorithm=SPLIT_OPERATOR,
        omit_final_swaps=True),
                                         strategy=cirq.InsertStrategy.NEW)

    assert len(circuit_without_swaps) < len(circuit_with_swaps)

    hamiltonian = lih_hamiltonian
    qubits = cirq.LineQubit.range(4)
    circuit_with_swaps = cirq.Circuit(
        simulate_trotter(qubits,
                         hamiltonian,
                         time,
                         order=0,
                         algorithm=LOW_RANK))
    circuit_without_swaps = cirq.Circuit(
        simulate_trotter(qubits,
                         hamiltonian,
                         time,
                         order=0,
                         algorithm=LOW_RANK,
                         omit_final_swaps=True))

    assert len(circuit_without_swaps) < len(circuit_with_swaps)