Skip to content

Commit

Permalink
Adding the ability to run Grover's
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Polloreno committed Oct 12, 2017
1 parent 076356f commit b8b477e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
1 change: 0 additions & 1 deletion grove/amplification/grover.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ def _init_attr(self, bitstring_map):
self.bit_map = bitstring_map
self.unitary_function_mapping = self._compute_grover_oracle_matrix(bitstring_map)
self.n_qubits = self.unitary_function_mapping.shape[0]
print self.unitary_function_mapping
self.qubits = list(range(int(np.log2(self.n_qubits))))
self._construct_grover_circuit()

Expand Down
10 changes: 8 additions & 2 deletions grove/tests/amplification/test_grover.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,16 @@ def test_optimal_grover(x_oracle):
assert prog_equality(generated_one_iter_grover, one_iter_grover)


def test_bitstring_grover():
def test_find_bistring():
bitstring_map = {"0": 1, "1": -1}
prog = Grover().construct_grover_program(bitstring_map)
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()
22 changes: 13 additions & 9 deletions grove/tests/utils/test_utility_programs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from grove.utils.utility_programs import ControlledProgramBuilder

SIGMA_Z = np.array([[1, 0], [0, -1]])
SIGMA_Z_NAME = "Z"


def test_1_qubit_control():
Expand All @@ -15,31 +16,34 @@ def test_1_qubit_control():
.with_controls([control_qubit])
.with_target(qubit)
.with_operation(SIGMA_Z)
.with_gate_name("Z").build())
.with_gate_name(SIGMA_Z_NAME).build())
# This should be one "CZ" instruction, from control_qubit to qubit.
assert prog_len(prog) == 1
prog.synthesize()
instruction = non_action_insts(prog)[0]
assert instruction[1].operator_name == 'C[Z]'
assert instruction[1].operator_name == (ControlledProgramBuilder()
.format_gate_name("C", SIGMA_Z_NAME))
assert instruction[1].arguments == [control_qubit, qubit]


def double_control_test(instructions, target_qubit, control_qubit_one, control_qubit_two):
"""A list of asserts testing the simple case of a double controlled Z gate. Used in the next
two tests."""
assert instructions[0][1].operator_name == 'C[SQRT[Z]]'
cpg = ControlledProgramBuilder()
sqrt_z = cpg.format_gate_name("SQRT", SIGMA_Z_NAME)
assert instructions[0][1].operator_name == (cpg.format_gate_name("C", sqrt_z))
assert instructions[0][1].arguments == [control_qubit_two, target_qubit]

assert instructions[1][1].operator_name == 'C[NOT]'
assert instructions[1][1].operator_name == cpg.format_gate_name("C", "NOT")
assert instructions[1][1].arguments == [control_qubit_one, control_qubit_two]

assert instructions[2][1].operator_name == 'C[SQRT[Z]]-INV'
assert instructions[2][1].operator_name == cpg.format_gate_name("C", sqrt_z) + '-INV'
assert instructions[2][1].arguments == [control_qubit_two, target_qubit]

assert instructions[3][1].operator_name == 'C[NOT]'
assert instructions[3][1].operator_name == cpg.format_gate_name("C", "NOT")
assert instructions[3][1].arguments == [control_qubit_one, control_qubit_two]

assert instructions[4][1].operator_name == 'C[SQRT[Z]]'
assert instructions[4][1].operator_name == cpg.format_gate_name("C", sqrt_z)
assert instructions[4][1].arguments == [control_qubit_one, target_qubit]


Expand All @@ -52,7 +56,7 @@ def test_recursive_builder():
.with_controls([control_qubit_one, control_qubit_two])
.with_target(target_qubit)
.with_operation(SIGMA_Z)
.with_gate_name("Z"))
.with_gate_name(SIGMA_Z_NAME))
prog = cpg._recursive_builder(cpg.operation,
cpg.gate_name,
cpg.control_qubits,
Expand All @@ -72,7 +76,7 @@ def test_2_qubit_control():
.with_controls([control_qubit_one, control_qubit_two])
.with_target(qubit)
.with_operation(SIGMA_Z)
.with_gate_name("Z").build())
.with_gate_name(SIGMA_Z_NAME).build())
# This should be one "CZ" instruction, from control_qubit to qubit.
assert prog_len(prog) == 5
prog.synthesize()
Expand Down

0 comments on commit b8b477e

Please sign in to comment.