Exemplo n.º 1
0
def test_amplify_init():
    """
    Test the usage of amplify without init
    """
    # Essentially Grover's to select 011 or 111
    desired = cz_gate + triple_hadamard.dagger() + diffusion_program(
        qubits) + triple_hadamard + cz_gate + triple_hadamard.dagger(
        ) + diffusion_program(qubits) + triple_hadamard
    created = amplification_circuit(triple_hadamard, cz_gate, qubits, iters)

    prog_equality(desired, created)
Exemplo n.º 2
0
def test_trivial_grover():
    """Testing that we construct the correct circuit for Grover's Algorithm with one step, and the
     identity_oracle on one qubit.
     """
    trivial_grover = Program()
    qubit0 = trivial_grover.alloc()
    # First we put the input into uniform superposition.
    trivial_grover.inst(H(qubit0))
    # No oracle is applied, so we just apply the diffusion operator.
    trivial_grover.inst(H(qubit0))
    trivial_grover.inst(Z(qubit0))
    trivial_grover.inst(H(qubit0))
    qubits = [qubit0]
    generated_trivial_grover = Grover().oracle_grover(identity_oracle, qubits,
                                                      1)
    generated_trivial_grover.synthesize()
    trivial_grover.synthesize()
    assert prog_equality(generated_trivial_grover, trivial_grover)
Exemplo n.º 3
0
def test_diffusion_operator():
    """
    Checks that the diffusion operator outputs the correct operation
    """
    created = diffusion_program([0, 1])
    desired = pq.Program()
    for def_gate in created.defined_gates:
        desired.defgate(def_gate.name, def_gate.matrix)
    desired.inst(X(0))
    desired.inst(X(1))
    desired.inst(H(1))
    desired.inst(RZ(-np.pi, 0))
    # There should be only one defined gate -- the CNOT.
    desired.inst((created.defined_gates[0].name, 0, 1))
    desired.inst(RZ(-np.pi, 0))
    desired.inst(H(1))
    desired.inst(X(0))
    desired.inst(X(1))
    assert prog_equality(created, desired)
Exemplo n.º 4
0
def test_x_oracle_one_grover(x_oracle):
    """Testing that Grover's algorithm with an oracle that applies an X gate to the query bit works,
     with one iteration."""
    x_oracle_grover = Program()
    qubit0 = x_oracle_grover.alloc()
    qubits = [qubit0]
    oracle, query_qubit = x_oracle
    with patch("pyquil.quilbase.InstructionGroup.alloc") as mock_alloc:
        mock_alloc.return_value = qubit0
    generated_x_oracle_grover = Grover().oracle_grover(oracle, qubits, 1)
    # First we put the input into uniform superposition.
    x_oracle_grover.inst(H(qubit0))
    # Now an oracle is applied.
    x_oracle_grover.inst(X(query_qubit))
    # We now apply the diffusion operator.
    x_oracle_grover.inst(H(qubit0))
    x_oracle_grover.inst(Z(qubit0))
    x_oracle_grover.inst(H(qubit0))
    synthesize_programs(generated_x_oracle_grover, x_oracle_grover)
    assert prog_equality(generated_x_oracle_grover, x_oracle_grover)
Exemplo n.º 5
0
def test_optimal_grover(x_oracle):
    """Testing that Grover's algorithm with an oracle that applies an X gate to the query bit works,
     and defaults to the optimal number of iterations."""
    grover_precircuit = Program()
    qubit0 = grover_precircuit.alloc()
    qubits = [qubit0]
    oracle, query_qubit = x_oracle
    with patch("pyquil.quilbase.InstructionGroup.alloc") as mock_alloc:
        mock_alloc.return_value = qubit0
        generated_one_iter_grover = Grover().oracle_grover(oracle, qubits)
    # First we put the input into uniform superposition.
    grover_precircuit.inst(H(qubit0))
    # We only do one iteration, which is the result of rounding pi * sqrt(N)/4
    iter = Program()

    # An oracle is applied.
    iter.inst(X(query_qubit))
    # We now apply the diffusion operator.
    iter.inst(H(qubit0))
    iter.inst(Z(qubit0))
    iter.inst(H(qubit0))
    one_iter_grover = grover_precircuit + iter
    synthesize_programs(generated_one_iter_grover, one_iter_grover)
    assert prog_equality(generated_one_iter_grover, one_iter_grover)