docs/example_projectq_grover.py
def run_grover(eng, n, oracle):
    """
    Runs Grover's algorithm on n qubit using the provided quantum oracle.

    Args:
        eng (MainEngine): Main compiler engine to run Grover on.
        n (int): Number of bits in the solution.
        oracle (function): Function accepting the engine, an n-qubit register,
            and an output qubit which is flipped by the oracle for the correct
            bit string.

    Returns:
        solution (list<int>): Solution bit-string.
    """
    x = eng.allocate_qureg(n)

    # start in uniform superposition
    All(H) | x

    # number of iterations we have to run:
    num_it = int(math.pi / 4. * math.sqrt(1 << n))

    # prepare the oracle output qubit (the one that is flipped to indicate the
    # solution. start in state 1/sqrt(2) * (|0> - |1>) s.t. a bit-flip turns
    # into a (-1)-phase.
    oracle_out = eng.allocate_qubit()
    X | oracle_out
    H | oracle_out

    # run num_it iterations
    with Loop(eng, num_it):
        # oracle adds a (-1)-phase to the solution
        oracle(eng, x, oracle_out)

        # reflection across uniform superposition
        with Compute(eng):
            All(H) | x
            All(X) | x

        with Control(eng, x[0:-1]):
            Z | x[-1]

        Uncompute(eng)

    All(Measure) | x
    Measure | oracle_out

    eng.flush()
    # return result
    return [int(qubit) for qubit in x]

    Runs Grover's algorithm on n qubit using the provided quantum oracle.

    Args:
        eng (MainEngine): Main compiler engine to run Grover on.
        n (int): Number of bits in the solution.
        oracle (function): Function accepting the engine, an n-qubit register,
            and an output qubit which is flipped by the oracle for the correct
            bit string.

    Returns:
        solution (list<int>): Solution bit-string.
    
Runs Grover's algorithm on n qubit using the provided quantum oracle.
Runs Grover ' s algorithm on n qubit using the provided quantum oracle .
def run_grover(eng, n, oracle):
    x = eng.allocate_qureg(n)
    All(H) | x
    num_it = int(math.pi / 4. * math.sqrt(1 << n))
    oracle_out = eng.allocate_qubit()
    X | oracle_out
    H | oracle_out
    with Loop(eng, num_it):
        oracle(eng, x, oracle_out)
        with Compute(eng):
            All(H) | x
            All(X) | x
        with Control(eng, x[0:-1]):
            Z | x[-1]
        Uncompute(eng)
    All(Measure) | x
    Measure | oracle_out
    eng.flush()
    return [int(qubit) for qubit in x]
def run_grover ( eng , n , oracle ) : x = eng . allocate_qureg ( n ) All ( H ) | x num_it = int ( math . pi / 4. * math . sqrt ( 1 << n ) ) oracle_out = eng . allocate_qubit ( ) X | oracle_out H | oracle_out with Loop ( eng , num_it ) : oracle ( eng , x , oracle_out ) with Compute ( eng ) : All ( H ) | x All ( X ) | x with Control ( eng , x [ 0 : - 1 ] ) : Z | x [ - 1 ] Uncompute ( eng ) All ( Measure ) | x Measure | oracle_out eng . flush ( ) return [ int ( qubit ) for qubit in x ]
Runs Grover's algorithm on n qubit using the provided quantum oracle.
Runs Grover ' s algorithm on n qubit using the provided quantum oracle .
"""
    Runs Grover's algorithm on n qubit using the provided quantum oracle.

    Args:
        eng (MainEngine): Main compiler engine to run Grover on.
        n (int): Number of bits in the solution.
        oracle (function): Function accepting the engine, an n-qubit register,
            and an output qubit which is flipped by the oracle for the correct
            bit string.

    Returns:
        solution (list<int>): Solution bit-string.
    """ # start in uniform superposition # number of iterations we have to run: # prepare the oracle output qubit (the one that is flipped to indicate the # solution. start in state 1/sqrt(2) * (|0> - |1>) s.t. a bit-flip turns # into a (-1)-phase. # run num_it iterations # oracle adds a (-1)-phase to the solution # reflection across uniform superposition # return result
solution (list): Solution bit-string.
solution ( list ) : Solution bit - string .
eng
Main compiler engine to run Grover on.
Main compiler engine to run Grover on .
n
Number of bits in the solution.
Number of bits in the solution .
oracle
Function accepting the engine, an n-qubit register,
and an output qubit which is flipped by the oracle for the correct
bit string.
Function accepting the engine an n - qubit register and an output qubit which is flipped by the oracle for the correct bit string .