|  | 
|  | 1 | +#   Copyright 2019 ProjectQ-Framework (www.projectq.ch) | 
|  | 2 | +# | 
|  | 3 | +#   Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 4 | +#   you may not use this file except in compliance with the License. | 
|  | 5 | +#   You may obtain a copy of the License at | 
|  | 6 | +# | 
|  | 7 | +#       http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 | +# | 
|  | 9 | +#   Unless required by applicable law or agreed to in writing, software | 
|  | 10 | +#   distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 11 | +#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 12 | +#   See the License for the specific language governing permissions and | 
|  | 13 | +#   limitations under the License. | 
|  | 14 | +""" | 
|  | 15 | +Registers a decomposition for quantum amplitude amplification. | 
|  | 16 | +
 | 
|  | 17 | +(Quick reference https://en.wikipedia.org/wiki/Amplitude_amplification. | 
|  | 18 | +Complete reference G. Brassard, P. Hoyer, M. Mosca, A. Tapp (2000) | 
|  | 19 | +Quantum Amplitude Amplification and Estimation | 
|  | 20 | +https://arxiv.org/abs/quant-ph/0005055) | 
|  | 21 | +
 | 
|  | 22 | +Quantum Amplitude Amplification (QAA) executes the algorithm, but not | 
|  | 23 | +the final measurement required to obtain the marked state(s) with high | 
|  | 24 | +probability. The starting state on wich the QAA algorithm is executed | 
|  | 25 | +is the one resulting of aplying the Algorithm on the |0> state. | 
|  | 26 | +
 | 
|  | 27 | +Example: | 
|  | 28 | +    .. code-block:: python | 
|  | 29 | +
 | 
|  | 30 | +       def func_algorithm(eng,system_qubits): | 
|  | 31 | +           All(H) | system_qubits | 
|  | 32 | +
 | 
|  | 33 | +       def func_oracle(eng,system_qubits,qaa_ancilla): | 
|  | 34 | +           # This oracle selects the state |010> as the one marked | 
|  | 35 | +           with Compute(eng): | 
|  | 36 | +              All(X) | system_qubits[0::2] | 
|  | 37 | +           with Control(eng, system_qubits): | 
|  | 38 | +              X | qaa_ancilla | 
|  | 39 | +           Uncompute(eng) | 
|  | 40 | +
 | 
|  | 41 | +       system_qubits = eng.allocate_qureg(3) | 
|  | 42 | +       # Prepare the qaa_ancilla qubit in the |-> state | 
|  | 43 | +       qaa_ancilla = eng.allocate_qubit() | 
|  | 44 | +       X | qaa_ancilla | 
|  | 45 | +       H | qaa_ancilla | 
|  | 46 | +
 | 
|  | 47 | +       # Creates the initial state form the Algorithm | 
|  | 48 | +       func_algorithm(eng, system_qubits) | 
|  | 49 | +       # Apply Quantum Amplitude Amplification the correct number of times | 
|  | 50 | +       num_it = int(math.pi/4.*math.sqrt(1 << 3)) | 
|  | 51 | +       with Loop(eng, num_it): | 
|  | 52 | +         QAA(func_algorithm, func_oracle) | (system_qubits, qaa_ancilla) | 
|  | 53 | +
 | 
|  | 54 | +       All(Measure) | system_qubits | 
|  | 55 | +
 | 
|  | 56 | +Warning: | 
|  | 57 | +    No qubit allocation/deallocation may take place during the call | 
|  | 58 | +    to the defined Algorithm :code:`func_algorithm` | 
|  | 59 | +
 | 
|  | 60 | +Attributes: | 
|  | 61 | +    func_algorithm: Algorithm that initialite the state and to be used | 
|  | 62 | +                    in the QAA algorithm | 
|  | 63 | +    func_oracle: The Oracle that marks the state(s) as "good" | 
|  | 64 | +    system_qubits: the system we are interested on | 
|  | 65 | +    qaa_ancilla: auxiliary qubit that helps to invert the amplitude of the | 
|  | 66 | +                 "good" states | 
|  | 67 | +
 | 
|  | 68 | +""" | 
|  | 69 | + | 
|  | 70 | +import math | 
|  | 71 | +import numpy as np | 
|  | 72 | + | 
|  | 73 | +from projectq.cengines import DecompositionRule | 
|  | 74 | +from projectq.meta import Control, Compute, Uncompute, CustomUncompute, Dagger | 
|  | 75 | +from projectq.ops import X, Z, Ph, All | 
|  | 76 | + | 
|  | 77 | +from projectq.ops import QAA | 
|  | 78 | + | 
|  | 79 | + | 
|  | 80 | +def _decompose_QAA(cmd): | 
|  | 81 | +    """ Decompose the Quantum Amplitude Apmplification algorithm as a gate. """ | 
|  | 82 | +    eng = cmd.engine | 
|  | 83 | + | 
|  | 84 | +    # System-qubit is the first qubit/qureg. Ancilla qubit is the second qubit | 
|  | 85 | +    system_qubits = cmd.qubits[0] | 
|  | 86 | +    qaa_ancilla = cmd.qubits[1] | 
|  | 87 | + | 
|  | 88 | +    # The Oracle and the Algorithm | 
|  | 89 | +    Oracle = cmd.gate.oracle | 
|  | 90 | +    A = cmd.gate.algorithm | 
|  | 91 | + | 
|  | 92 | +    # Apply the oracle to invert the amplitude of the good states, S_Chi | 
|  | 93 | +    Oracle(eng, system_qubits, qaa_ancilla) | 
|  | 94 | + | 
|  | 95 | +    # Apply the inversion of the Algorithm, | 
|  | 96 | +    # the inversion of the aplitude of |0> and the Algorithm | 
|  | 97 | + | 
|  | 98 | +    with Compute(eng): | 
|  | 99 | +        with Dagger(eng): | 
|  | 100 | +            A(eng, system_qubits) | 
|  | 101 | +        All(X) | system_qubits | 
|  | 102 | +    with Control(eng, system_qubits[0:-1]): | 
|  | 103 | +        Z | system_qubits[-1] | 
|  | 104 | +    with CustomUncompute(eng): | 
|  | 105 | +        All(X) | system_qubits | 
|  | 106 | +        A(eng, system_qubits) | 
|  | 107 | +    Ph(math.pi) | system_qubits[0] | 
|  | 108 | + | 
|  | 109 | + | 
|  | 110 | +#: Decomposition rules | 
|  | 111 | +all_defined_decomposition_rules = [DecompositionRule(QAA, _decompose_QAA)] | 
0 commit comments