def test_find_bistring(): bitstring_map = {"0": 1, "1": -1} builder = Grover() with patch("pyquil.api.SyncConnection") as qvm: expected_bitstring = np.asarray([0, 1], dtype=int) qvm.run_and_measure.return_value = expected_bitstring bitstring = builder.find_bitstring(qvm, bitstring_map) prog = builder.grover_circuit # Make sure it only defines the one ORACLE gate. assert len(prog.defined_gates) == 1 # Make sure that it produces the oracle we expect. assert (prog.defined_gates[0].matrix == np.array([[1, 0], [0, -1]])).all() assert (bitstring == expected_bitstring).all()
def test_bitstring_grover(): bitstring_map = {"0": 1, "1": -1} prog = Grover().construct_grover_program(bitstring_map) # Make sure it only defines the one ORACLE gate. assert len(prog.defined_gates) == 1 # Make sure that it produces the oracle we expect. assert (prog.defined_gates[0].matrix == np.array([[1, 0], [0, -1]])).all()
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) assert generated_trivial_grover.out() == trivial_grover.out()
def test_find_bitstring(): bitstring_map = {"0": 1, "1": -1} builder = Grover() with patch("pyquil.api.QuantumComputer") as qc: expected_bitstring = [0, 1] qc.run.return_value = [ "".join([str(bit) for bit in expected_bitstring]) ] returned_bitstring = builder.find_bitstring(qc, bitstring_map) prog = builder.grover_circuit # Make sure it only defines the ORACLE gate and the DIFFUSION gate. assert len(prog.defined_gates) == 2 # Make sure that it produces the oracle we expect. assert (prog.defined_gates[0].matrix == np.array([[1, 0], [0, -1]])).all() expected_bitstring = "".join([str(bit) for bit in expected_bitstring]) returned_bitstring = "".join([str(bit) for bit in returned_bitstring]) assert expected_bitstring == returned_bitstring
def test_find_bistring(): bitstring_map = {"0": 1, "1": -1} builder = Grover() with patch("pyquil.api.QVMConnection") as qvm: expected_bitstring = [0, 1] qvm.run_and_measure.return_value = [ expected_bitstring, ] returned_bitstring = builder.find_bitstring(qvm, bitstring_map) prog = builder.grover_circuit # Make sure it only defines the one ORACLE gate. assert len(prog.defined_gates) == 1 # Make sure that it produces the oracle we expect. assert (prog.defined_gates[0].matrix == np.array([[1, 0], [0, -1]])).all() expected_bitstring = "".join([str(bit) for bit in expected_bitstring]) returned_bitstring = "".join([str(bit) for bit in returned_bitstring]) assert expected_bitstring == returned_bitstring
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.""" qubits = [0] oracle, _ = x_oracle generated_one_iter_grover = Grover().oracle_grover(oracle, qubits) # First we put the input into uniform superposition. gates = [H, X, H, HADAMARD_DIFFUSION_LABEL, H] check_instructions(gates, generated_one_iter_grover)
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. """ qubits = [0] gates = [H, H, HADAMARD_DIFFUSION_LABEL, H] generated_trivial_grover = Grover().oracle_grover(identity_oracle, qubits, 1) check_instructions(gates, generated_trivial_grover)
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.""" qubits = [0] oracle, _ = x_oracle generated_x_oracle_grover = Grover().oracle_grover(oracle, qubits, num_iter=1) gates = [H, X, H, HADAMARD_DIFFUSION_LABEL, H] check_instructions(gates, generated_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.""" qubits = [0] oracle, query_qubit = x_oracle generated_x_oracle_grover = Grover().oracle_grover(oracle, qubits, 2) # First we put the input into uniform superposition. gates = [H] for _ in range(2): # Now an oracle is applied. gates.append(X) # We apply the diffusion operator. gates.append(H) gates.append(HADAMARD_DIFFUSION_LABEL) gates.append(H) check_instructions(gates, generated_x_oracle_grover)
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.quil.Program.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)) assert 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.quilbase.InstructionGroup.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)) 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.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
import numpy as np from grove.amplification.grover import Grover from pyquil import get_qc # Bitstring Map as an algorithm input SEARCHED_STRING = "1011010" N = len(SEARCHED_STRING) mapping = {} for b in range(2**N): pad_str = np.binary_repr(b, N) if pad_str == SEARCHED_STRING: mapping[pad_str] = -1 else: mapping[pad_str] = 1 # Connection qc = get_qc('9q-qvm') #============================================================================== # Grove: Grove's Search Algorithm #============================================================================== # Run algo = Grover() ret_string = algo.find_bitstring(qc, bitstring_map=mapping) print("The searched string is: {}".format(ret_string))
print(target_bitstring0) print(target_bitstring1) bit = ("0", "1") bitstring_map = {} target_bitstring_phase = -1 nontarget_bitstring_phase = 1 # We construct the bitmap for the oracle for bitstring in product(bit, repeat=N*2): bitstring = "".join(bitstring) if bitstring == target_bitstring0 or bitstring == target_bitstring1: bitstring_map[bitstring] = target_bitstring_phase else: bitstring_map[bitstring] = nontarget_bitstring_phase qvm = QVMConnection() #with patch("pyquil.api.QuantumComputer") as qc: #qvm.run.return_value = [[int(bit) for bit in target_bitstring]] grover = Grover() samples = [] for i in range(50): found_bitstring = grover.find_bitstring(qvm, bitstring_map) samples.append(found_bitstring) c = Counter(samples) print(c) plt.bar(*zip(*c.most_common()), width=.5, color='g') plt.show()