How to use the projectq.meta.Compute function in projectq

To help you get started, we’ve selected a few projectq examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github QuTech-Delft / quantuminspire / docs / example_projectq_grover.py View on Github external
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]
github Huawei-HiQ / HiQsimulator / examples / maximum_search_constant_mpi.py View on Github external
All(H) | x
        
        # 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 j iterations
        with Loop(eng, j):
            # oracle adds a (-1)-phase to the solution
            oracle(eng, x, Dataset,threshold, 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
         
        #read the measure value 
        k=0
        xvalue=0
        while k
github Huawei-HiQ / HiQsimulator / examples / unknown_num_search_constant_mpi.py View on Github external
All(H) | x
        
        # 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 j iterations
        with Loop(eng, j):
            # oracle adds a (-1)-phase to the solution
            oracle(eng, x, Dataset,threshold, 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
         
        #read the measure value 
        k=0
        xvalue=0
        while k
github QuTech-Delft / quantuminspire / docs / example_projectq_grover.py View on Github external
def alternating_bits_oracle(eng, qubits, output):
    """
    Marks the solution string 1,0,1,0,...,0,1 by flipping the output qubit,
    conditioned on qubits being equal to the alternating bit-string.

    Args:
        eng (MainEngine): Main compiler engine the algorithm is being run on.
        qubits (Qureg): n-qubit quantum register Grover search is run on.
        output (Qubit): Output qubit to flip in order to mark the solution.
    """
    with Compute(eng):
        All(X) | qubits[1::2]
    with Control(eng, qubits):
        X | output
    Uncompute(eng)
github Huawei-HiQ / HiQsimulator / examples / exactgrover_mpi.py View on Github external
def alternating_bits_oracle(eng, qubits, output):
    """
    Marks the solution string 0, 1, 0,...,0 by flipping the 
    
    qubit,
    conditioned on qubits being equal to the alternating bit-string.

    Args:
        eng (MainEngine): Main compiler engine the algorithm is being run on.
        qubits (Qureg): n-qubit quantum register Grover search is run on.
        output (Qubit): Output qubit to flip in order to mark the solution.
    """
    with Compute(eng):
        X | qubits[1]
    with Control(eng, qubits):
        X | output
    Uncompute(eng)
github ProjectQ-Framework / ProjectQ / projectq / libs / math / _constantmath.py View on Github external
def add_constant(eng, c, quint):
    """
    Adds a classical constant c to the quantum integer (qureg) quint using
    Draper addition.

    Note: Uses the Fourier-transform adder from
          https://arxiv.org/abs/quant-ph/0008033.
    """

    with Compute(eng):
        QFT | quint

    for i in range(len(quint)):
        for j in range(i, -1, -1):
            if ((c >> j) & 1):
                R(math.pi / (1 << (i - j))) | quint[i]

    Uncompute(eng)
github ProjectQ-Framework / ProjectQ / projectq / setups / decompositions / rx2rz.py View on Github external
def _decompose_rx(cmd):
    """ Decompose the Rx gate."""
    qubit = cmd.qubits[0]
    eng = cmd.engine
    angle = cmd.gate.angle

    with Control(eng, cmd.control_qubits):
        with Compute(eng):
            H | qubit
        Rz(angle) | qubit
        Uncompute(eng)
github ProjectQ-Framework / ProjectQ / projectq / setups / decompositions / swap2cnot.py View on Github external
def _decompose_swap(cmd):
    """ Decompose (controlled) swap gates. """
    ctrl = cmd.control_qubits
    eng = cmd.engine
    with Compute(eng):
        CNOT | (cmd.qubits[0], cmd.qubits[1])
    with Control(eng, ctrl):
        CNOT | (cmd.qubits[1], cmd.qubits[0])
    Uncompute(eng)
github ProjectQ-Framework / ProjectQ / projectq / setups / decompositions / time_evolution.py View on Github external
# Previous __or__ operator should have apply a global phase instead:
    assert not term == ()

    # hamiltonian has only a single local operator
    if len(term) == 1:
        with Control(eng, cmd.control_qubits):
            if term[0][1] == 'X':
                Rx(time * coefficient * 2.) | qureg[term[0][0]]
            elif term[0][1] == 'Y':
                Ry(time * coefficient * 2.) | qureg[term[0][0]]
            else:
                Rz(time * coefficient * 2.) | qureg[term[0][0]]
    # hamiltonian has more than one local operator
    else:
        with Control(eng, cmd.control_qubits):
            with Compute(eng):
                # Apply local basis rotations
                for index, action in term:
                    check_indices.add(index)
                    if action == 'X':
                        H | qureg[index]
                    elif action == 'Y':
                        Rx(math.pi / 2.) | qureg[index]
                # Check that qureg had exactly as many qubits as indices:
                assert check_indices == set((range(len(qureg))))
                # Compute parity
                for i in range(len(qureg)-1):
                    CNOT | (qureg[i], qureg[i+1])
            Rz(time * coefficient * 2.) | qureg[-1]
            # Uncompute parity and basis change
            Uncompute(eng)
github Huawei-HiQ / HiQsimulator / examples / exactgrover_mpi.py View on Github external
def alternating_bits_oracle_modified(eng, qubits, output, phi):
    """
    Marks the solution string 0,1,0,... by applying phase gate to the output bits,
    conditioned on qubits being equal to the alternating bit-string.

    Args:
        eng (MainEngine): Main compiler engine the algorithm is being run on.
        qubits (Qureg): n-qubit quantum register Grover search is run on.
        output (Qubit): Output qubit to mark the solution by a phase gate with parameter phi.
    """
    with Compute(eng):
        All(X) | qubits[1::2]
    with Control(eng, qubits):
        Rz(phi) | output
        Ph(phi/2) | output

    Uncompute(eng)