def test_amplify_init(): """ Test the usage of amplify without init """ # Essentially Grover's to select 011 or 111 desired = cz_gate + triple_hadamard.dagger() + diffusion_program( qubits) + triple_hadamard + cz_gate + triple_hadamard.dagger( ) + diffusion_program(qubits) + triple_hadamard created = amplification_circuit(triple_hadamard, cz_gate, qubits, iters) prog_equality(desired, created)
def test_amplify(): """ Test the generic usage of amplify """ # Essentially Grover's to select 011 or 111 desired = ( triple_hadamard.dagger() + cz_gate + triple_hadamard.dagger() + diffusion_program(qubits) + triple_hadamard + cz_gate + triple_hadamard.dagger() # We take care to only add the instructions, and not redefine the gate. + diffusion_program(qubits).instructions + triple_hadamard) created = amplification_circuit(triple_hadamard, cz_gate, qubits, iters) assert desired == created
def test_amplify(): """ Test the generic usage of amplify """ # Essentially Grover's to select 011 or 111 desired = (triple_hadamard.dagger() + cz_gate + triple_hadamard.dagger() + diffusion_program(qubits) + triple_hadamard + cz_gate + triple_hadamard.dagger() + diffusion_program(qubits) + triple_hadamard) created = amplification_circuit(triple_hadamard, cz_gate, qubits, iters) assert desired == created
def oracle_grover(oracle, qubits, num_iter=None): r"""Implementation of Grover's Algorithm for a given oracle. :param Program oracle: An oracle defined as a Program. It should send :math:`\ket{x}` to :math:`(-1)^{f(x)}\ket{x}`, where the range of f is {0, 1}. :param qubits: List of qubits for Grover's Algorithm. :type qubits: list[int or Qubit] :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 num_iter is None: num_iter = int(round(np.pi * 2 ** (len(qubits) / 2.0 - 2.0))) uniform_superimposer = pq.Program().inst([H(qubit) for qubit in qubits]) amp_prog = amplification_circuit(uniform_superimposer, oracle, qubits, num_iter) return amp_prog