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)
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)