Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/projectq.ops.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ The operations collection consists of various default gates and is a work-in-pro
projectq.ops.StatePreparation
projectq.ops.QPE
projectq.ops.FlipBits
projectq.ops.QAA


Module contents
Expand Down
8 changes: 8 additions & 0 deletions docs/projectq.setups.decompositions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The decomposition package is a collection of gate decomposition / replacement ru
projectq.setups.decompositions.toffoli2cnotandtgate
projectq.setups.decompositions.uniformlycontrolledr2cnot
projectq.setups.decompositions.phaseestimation
projectq.setups.decompositions.amplitudeamplification


Submodules
Expand Down Expand Up @@ -180,6 +181,13 @@ projectq.setups.decompositions.phaseestimation module
:members:
:undoc-members:

projectq.setups.decompositions.amplitudeamplification module
---------------------------------------------------------------

.. automodule:: projectq.setups.decompositions.amplitudeamplification
:members:
:undoc-members:


Module contents
---------------
Expand Down
1 change: 1 addition & 0 deletions projectq/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@
UniformlyControlledRz)
from ._state_prep import StatePreparation
from ._qpegate import QPE
from ._qaagate import QAA
31 changes: 31 additions & 0 deletions projectq/ops/_qaagate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2019 ProjectQ-Framework (www.projectq.ch)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from ._basics import BasicGate


class QAA(BasicGate):
"""
Quantum Aplitude Aplification gate.

See setups.decompositions for the complete implementation
"""
def __init__(self, algorithm, algorithm_inverse, oracle):
BasicGate.__init__(self)
self.algorithm = algorithm
self.algorithm_inverse = algorithm_inverse
self.oracle = oracle

def __str__(self):
return 'QAA(Algorithm = {0}, Oracle = {1})'.format(str(self.algorithm.__name__), str(self.oracle.__name__))
29 changes: 29 additions & 0 deletions projectq/ops/_qaagate_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2019 ProjectQ-Framework (www.projectq.ch)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tests for projectq.ops._qaagate."""

from projectq.ops import _qaagate, All, H, X


def test_qaa_str():

def func_algorithm(): All(H)

def func_algorithm_inverse(): All(H)

def func_oracle(): All(X)

gate = _qaagate.QAA(func_algorithm, func_algorithm_inverse, func_oracle)
assert str(gate) == "QAA(Algorithm = func_algorithm, Oracle = func_oracle)"
6 changes: 4 additions & 2 deletions projectq/setups/decompositions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
toffoli2cnotandtgate,
time_evolution,
uniformlycontrolledr2cnot,
phaseestimation)
phaseestimation,
amplitudeamplification)

all_defined_decomposition_rules = [
rule
Expand All @@ -56,6 +57,7 @@
toffoli2cnotandtgate,
time_evolution,
uniformlycontrolledr2cnot,
phaseestimation]
phaseestimation,
amplitudeamplification]
for rule in module.all_defined_decomposition_rules
]
112 changes: 112 additions & 0 deletions projectq/setups/decompositions/amplitudeamplification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Copyright 2019 ProjectQ-Framework (www.projectq.ch)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Registers a decomposition for quantum amplitude amplification.

(Quick reference https://en.wikipedia.org/wiki/Amplitude_amplification.
Complete reference G. Brassard, P. Hoyer, M. Mosca, A. Tapp (2000)
Quantum Amplitude Amplification and Estimation
https://arxiv.org/abs/quant-ph/0005055)

Quantum Amplitude Amplification (QAA) executes the algorithm, but not
the final measurement required to obtain the marked state(s) with high
probability. The starting state on wich the QAA algorithm is executed
is the one resulting of aplying the Algorithm on the |0> state.

Example:
.. code-block:: python

def func_algorithm(system_qubits):
All(H) | system_qubits

def func_algorithm_inverse(system_qubits):
All(H) | system_qubits

def func_oracle(eng,system_qubits,control):
# This oracle selects the state |010> as the one marked
# Method taken form the Grover example
with Compute(eng):
All(X) | system_qubits[0::2]
with Control(eng, system_qubits):
X | control
Uncompute(eng)

system_qubits = eng.allocate_qureg(3)
# Prepare the control qubit in the |-> state
control = eng.allocate_qubit()
X | control
H | control

# Creates the initial state form the Algorithm
func_algorithm(system_qubits)
# Apply Quantum Amplitude Amplification the correct number of times
num_it = int(math.pi/4.*math.sqrt(1 << 3))
with Loop(eng, num_it):
QAA(func_algorithm, func_algorithm_inverse, func_oracle) | (system_qubits, control)

All(Measure) | system_qubits

Attributes:
func_algorithm: Algorithm that initialite the state and to be used in the QAA algorithm
func_algorithm_inverse: inverse of the func_algorithm
func_oracle: The Oracle that marks the state(s) as "good"
system_qubits: the system we are interested on
control: auxiliary qubit that helps to invert the amplitude of the "good" states

"""

import math
import numpy as np

from projectq.cengines import DecompositionRule
from projectq.meta import Control, Compute, Uncompute, CustomUncompute
from projectq.ops import X, Z, Ph, All

from projectq.ops import QAA


def _decompose_QAA(cmd):
""" Decompose the Quantum Amplitude Apmplification algorithm as a gate. """
eng = cmd.engine

# System-qubit is the first qubit/qureg. Control qubit is the second qubit
system_qubits = cmd.qubits[0]
control = cmd.qubits[1]

# The Oracle and the Algorithm
Orcl = cmd.gate.oracle
A = cmd.gate.algorithm
A_inv = cmd.gate.algorithm_inverse

# Apply the oracle to invert the amplitude of the good states, S_Chi
Orcl(eng, system_qubits, control)

# Apply the inversion of the Algorithm, the inversion of the aplitude of |0> and the Algorithm

with Compute(eng):
A_inv(eng, system_qubits)
All(X) | system_qubits
with Control(eng, system_qubits[0:-1]):
Z | system_qubits[-1]
with CustomUncompute(eng):
All(X) | system_qubits
A(eng, system_qubits)
Ph(math.pi) | system_qubits[0]


#: Decomposition rules
all_defined_decomposition_rules = [
DecompositionRule(QAA, _decompose_QAA)
]
Loading