def test_edge_case_qubits_empty(): """ Checks that the list of qubits to apply the grover diffusion operator to must be non-empty """ with pytest.raises(AssertionError): amplify(A, A_inv, oracle, [], iters)
def grover(oracle, qubits, num_iter=None): """ Implementation of Grover's Algorithm for a given oracle. The query qubit will be left in the zero state afterwards. :param Program oracle: An oracle defined as a Program. It should send |x> to (-1)^f(x)|x>, where the range of f is {0, 1}. :param list(int) qubits: List of qubits for Grover's Algorithm. :param int num_iter: The number of iterations to repeat the algorithm for. The default is the integer closest to :math:`\\frac{\\pi}{4}\sqrt{N}`, where :math:`N` is the size of the domain. :return: A program corresponding to the desired instance of Grover's Algorithm. :rtype: Program """ if len(qubits) < 1: raise ValueError("Grover's Algorithm requires at least 1 qubits.") if num_iter is None: num_iter = int(round(np.pi * 2**(len(qubits) / 2.0 - 2.0))) many_hadamard = pq.Program().inst(list(map(H, qubits))) amp_prog = amp.amplify(many_hadamard, many_hadamard, oracle, qubits, num_iter) return amp_prog
def test_amplify_init(): """ Test the usage of amplify without init """ # Essentially Grover's to select 011 or 111 desired = cz_gate + A_inv + diffusion_operator( qubits) + A + cz_gate + A_inv + diffusion_operator(qubits) + A created = amplify(A, A_inv, cz_gate, qubits, iters, init=False) compare_progs(desired, created)
def test_amplify(): """ Test the generic usage of amplify """ # Essentially Grover's to select 011 or 111 desired = A + cz_gate + A_inv + diffusion_operator( qubits) + A + cz_gate + A_inv + diffusion_operator(qubits) + A created = amplify(A, A_inv, cz_gate, qubits, iters) compare_progs(desired, created)
def test_edge_case_amplify_0_iters(): """ Checks that the number of iterations needed to be greater than 0 """ with pytest.raises(AssertionError): amplify(A, A_inv, oracle, qubits, 0)
def test_edge_case_oracle_none(): """ Checks that U_w cannot be None """ with pytest.raises(AssertionError): amplify(A, A_inv, None, qubits, iters)