Skip to content

Commit f3582b7

Browse files
fernandodelaiglesiaTakishima
authored andcommitted
Amplitude Amplification algorithm as a Gate in ops (#327)
* Amplitude Amplification algorithm as a Gate in ops * Amplitude Amplification algorithm as a Gate in ops, correct test_string_functions * Amplitude Amplification algorithm as a Gate in ops, correct coverage for _qaagate_test * Amplitude Amplification algorithm as a Gate in ops, correct test estimation statistics in phaseestimation_test * Try to triger Travis test because an apt get failure in the travis test * Address changes proposed by Damien * Address changes proposed by Damien, missed one * Address comments by Damien including eliminate the usage of algorith_inverse and eliminate QPE files form the PR * Address comments by Damien including eliminate the usage of algorith_inverse and eliminate QPE files form the PR, second try * Address comments by Damien forgot _qaagate_test * Update amplitudeamplification_test.py Wrap lines to 80 characters * Update amplitudeamplification.py Wrap lines to 80 characters * Update _qaagate.py Minor style correction * Update amplitudeamplification_test.py Cleanup imports
1 parent 6a46f45 commit f3582b7

File tree

8 files changed

+415
-2
lines changed

8 files changed

+415
-2
lines changed

docs/projectq.ops.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ The operations collection consists of various default gates and is a work-in-pro
5555
projectq.ops.StatePreparation
5656
projectq.ops.QPE
5757
projectq.ops.FlipBits
58+
projectq.ops.QAA
5859

5960

6061
Module contents

docs/projectq.setups.decompositions.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ The decomposition package is a collection of gate decomposition / replacement ru
2727
projectq.setups.decompositions.toffoli2cnotandtgate
2828
projectq.setups.decompositions.uniformlycontrolledr2cnot
2929
projectq.setups.decompositions.phaseestimation
30+
projectq.setups.decompositions.amplitudeamplification
3031

3132

3233
Submodules
@@ -180,6 +181,13 @@ projectq.setups.decompositions.phaseestimation module
180181
:members:
181182
:undoc-members:
182183

184+
projectq.setups.decompositions.amplitudeamplification module
185+
---------------------------------------------------------------
186+
187+
.. automodule:: projectq.setups.decompositions.amplitudeamplification
188+
:members:
189+
:undoc-members:
190+
183191

184192
Module contents
185193
---------------

projectq/ops/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@
3838
UniformlyControlledRz)
3939
from ._state_prep import StatePreparation
4040
from ._qpegate import QPE
41+
from ._qaagate import QAA

projectq/ops/_qaagate.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
from ._basics import BasicGate
16+
17+
18+
class QAA(BasicGate):
19+
"""
20+
Quantum Aplitude Aplification gate.
21+
22+
(Quick reference https://en.wikipedia.org/wiki/Amplitude_amplification.
23+
Complete reference G. Brassard, P. Hoyer, M. Mosca, A. Tapp (2000)
24+
Quantum Amplitude Amplification and Estimation
25+
https://arxiv.org/abs/quant-ph/0005055)
26+
27+
Quantum Amplitude Amplification (QAA) executes the algorithm, but not
28+
the final measurement required to obtain the marked state(s) with high
29+
probability. The starting state on wich the QAA algorithm is executed
30+
is the one resulting of aplying the Algorithm on the |0> state.
31+
32+
Example:
33+
.. code-block:: python
34+
35+
def func_algorithm(eng,system_qubits):
36+
All(H) | system_qubits
37+
38+
def func_oracle(eng,system_qubits,qaa_ancilla):
39+
# This oracle selects the state |010> as the one marked
40+
with Compute(eng):
41+
All(X) | system_qubits[0::2]
42+
with Control(eng, system_qubits):
43+
X | qaa_ancilla
44+
Uncompute(eng)
45+
46+
system_qubits = eng.allocate_qureg(3)
47+
# Prepare the qaa_ancilla qubit in the |-> state
48+
qaa_ancilla = eng.allocate_qubit()
49+
X | qaa_ancilla
50+
H | qaa_ancilla
51+
52+
# Creates the initial state form the Algorithm
53+
func_algorithm(eng, system_qubits)
54+
# Apply Quantum Amplitude Amplification the correct number of times
55+
num_it = int(math.pi/4.*math.sqrt(1 << 3))
56+
with Loop(eng, num_it):
57+
QAA(func_algorithm, func_oracle) | (system_qubits, qaa_ancilla)
58+
59+
All(Measure) | system_qubits
60+
61+
Warning:
62+
No qubit allocation/deallocation may take place during the call
63+
to the defined Algorithm :code:`func_algorithm`
64+
65+
Attributes:
66+
func_algorithm: Algorithm that initialite the state and to be used
67+
in the QAA algorithm
68+
func_oracle: The Oracle that marks the state(s) as "good"
69+
system_qubits: the system we are interested on
70+
qaa_ancilla: auxiliary qubit that helps to invert the amplitude of the
71+
"good" states
72+
73+
"""
74+
def __init__(self, algorithm, oracle):
75+
BasicGate.__init__(self)
76+
self.algorithm = algorithm
77+
self.oracle = oracle
78+
79+
def __str__(self):
80+
return 'QAA(Algorithm = {0}, Oracle = {1})'.format(
81+
str(self.algorithm.__name__), str(self.oracle.__name__))

projectq/ops/_qaagate_test.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
"""Tests for projectq.ops._qaagate."""
16+
17+
from projectq.ops import _qaagate, All, H, X
18+
19+
20+
def test_qaa_str():
21+
22+
def func_algorithm(): All(H)
23+
24+
def func_oracle(): All(X)
25+
26+
gate = _qaagate.QAA(func_algorithm, func_oracle)
27+
assert str(gate) == "QAA(Algorithm = func_algorithm, Oracle = func_oracle)"

projectq/setups/decompositions/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
toffoli2cnotandtgate,
3333
time_evolution,
3434
uniformlycontrolledr2cnot,
35-
phaseestimation)
35+
phaseestimation,
36+
amplitudeamplification)
3637

3738
all_defined_decomposition_rules = [
3839
rule
@@ -56,6 +57,7 @@
5657
toffoli2cnotandtgate,
5758
time_evolution,
5859
uniformlycontrolledr2cnot,
59-
phaseestimation]
60+
phaseestimation,
61+
amplitudeamplification]
6062
for rule in module.all_defined_decomposition_rules
6163
]
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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

Comments
 (0)