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_x_oracle_two_grover(x_oracle): """Testing that Grover's algorithm with an oracle that applies an X gate to the query bit works, with two iterations.""" x_oracle_grover = Program() qubit0 = x_oracle_grover.alloc() qubits = [qubit0] oracle, query_qubit = x_oracle with patch("pyquil.quil.Program.alloc") as mock_alloc: mock_alloc.return_value = qubit0 generated_x_oracle_grover = Grover().oracle_grover(oracle, qubits, 2) # First we put the input into uniform superposition. x_oracle_grover.inst(H(qubit0)) # Two iterations. for _ in range(2): # Now an oracle is applied. x_oracle_grover.inst(X(query_qubit)) # We apply the diffusion operator. x_oracle_grover.inst(H(qubit0)) x_oracle_grover.inst(Z(qubit0)) x_oracle_grover.inst(H(qubit0)) assert 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.quil.Program.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 assert generated_one_iter_grover == one_iter_grover
def test_alloc_deprecated(): p = Program() with pytest.warns(DeprecationWarning): p.alloc()
def test_inline_alloc(): p = Program() p += H(p.alloc()) assert p.out() == "H 0\n"
def test_multiple_instantiate(): p = Program() q = p.alloc() p.inst(H(q)) assert p.out() == 'H 0\n' assert p.out() == 'H 0\n'
def x_oracle(): """Applies an X gate to the ancilla on all inputs. Returns the program, and the query Qubit.""" p = Program() qubit = p.alloc() p.inst(X(qubit)) return p, qubit
def test_inline_alloc(): p = Program() p += H(p.alloc()) assert address_qubits(p).out() == "H 0\n"