-
Notifications
You must be signed in to change notification settings - Fork 282
Amplitude Amplification algorithm as a Gate in ops #327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Takishima
merged 15 commits into
ProjectQ-Framework:develop
from
fernandodelaiglesia:develop
Jul 18, 2019
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
35313bd
Amplitude Amplification algorithm as a Gate in ops
fernandodelaiglesia dfb26a6
Amplitude Amplification algorithm as a Gate in ops, correct test_stri…
fernandodelaiglesia fa6b3b1
Amplitude Amplification algorithm as a Gate in ops, correct coverage …
fernandodelaiglesia 7de4d94
Amplitude Amplification algorithm as a Gate in ops, correct test esti…
fernandodelaiglesia 17f7fe3
Try to triger Travis test because an apt get failure in the travis test
fernandodelaiglesia 63344bf
Address changes proposed by Damien
fernandodelaiglesia ab9921a
Address changes proposed by Damien, missed one
fernandodelaiglesia 02afbbf
Address comments by Damien including eliminate the usage of algorith_…
fernandodelaiglesia c50888a
Address comments by Damien including eliminate the usage of algorith_…
fernandodelaiglesia 0c25073
Address comments by Damien forgot _qaagate_test
fernandodelaiglesia a2fe977
Merge branch 'develop' into develop
Takishima 66b15a2
Update amplitudeamplification_test.py
Takishima 65a385f
Update amplitudeamplification.py
Takishima 871cc9a
Update _qaagate.py
Takishima 8ed5972
Update amplitudeamplification_test.py
Takishima File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # 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. | ||
|
|
||
| (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(eng,system_qubits): | ||
| All(H) | system_qubits | ||
|
|
||
| def func_oracle(eng,system_qubits,qaa_ancilla): | ||
| # This oracle selects the state |010> as the one marked | ||
| with Compute(eng): | ||
| All(X) | system_qubits[0::2] | ||
| with Control(eng, system_qubits): | ||
| X | qaa_ancilla | ||
| Uncompute(eng) | ||
|
|
||
| system_qubits = eng.allocate_qureg(3) | ||
| # Prepare the qaa_ancilla qubit in the |-> state | ||
| qaa_ancilla = eng.allocate_qubit() | ||
| X | qaa_ancilla | ||
| H | qaa_ancilla | ||
|
|
||
| # Creates the initial state form the Algorithm | ||
| func_algorithm(eng, 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_oracle) | (system_qubits, qaa_ancilla) | ||
|
|
||
| All(Measure) | system_qubits | ||
|
|
||
| Warning: | ||
| No qubit allocation/deallocation may take place during the call | ||
| to the defined Algorithm :code:`func_algorithm` | ||
|
|
||
| Attributes: | ||
| func_algorithm: Algorithm that initialite the state and to be used | ||
| in the QAA algorithm | ||
| func_oracle: The Oracle that marks the state(s) as "good" | ||
| system_qubits: the system we are interested on | ||
| qaa_ancilla: auxiliary qubit that helps to invert the amplitude of the | ||
| "good" states | ||
|
|
||
| """ | ||
| def __init__(self, algorithm, oracle): | ||
| BasicGate.__init__(self) | ||
| self.algorithm = algorithm | ||
| self.oracle = oracle | ||
|
|
||
| def __str__(self): | ||
| return 'QAA(Algorithm = {0}, Oracle = {1})'.format( | ||
| str(self.algorithm.__name__), str(self.oracle.__name__)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # 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_oracle(): All(X) | ||
|
|
||
| gate = _qaagate.QAA(func_algorithm, func_oracle) | ||
| assert str(gate) == "QAA(Algorithm = func_algorithm, Oracle = func_oracle)" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
projectq/setups/decompositions/amplitudeamplification.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| # 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(eng,system_qubits): | ||
| All(H) | system_qubits | ||
|
|
||
| def func_oracle(eng,system_qubits,qaa_ancilla): | ||
| # This oracle selects the state |010> as the one marked | ||
| with Compute(eng): | ||
| All(X) | system_qubits[0::2] | ||
| with Control(eng, system_qubits): | ||
| X | qaa_ancilla | ||
| Uncompute(eng) | ||
|
|
||
| system_qubits = eng.allocate_qureg(3) | ||
| # Prepare the qaa_ancilla qubit in the |-> state | ||
| qaa_ancilla = eng.allocate_qubit() | ||
| X | qaa_ancilla | ||
| H | qaa_ancilla | ||
|
|
||
| # Creates the initial state form the Algorithm | ||
| func_algorithm(eng, 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_oracle) | (system_qubits, qaa_ancilla) | ||
|
|
||
| All(Measure) | system_qubits | ||
|
|
||
| Warning: | ||
| No qubit allocation/deallocation may take place during the call | ||
| to the defined Algorithm :code:`func_algorithm` | ||
|
|
||
| Attributes: | ||
| func_algorithm: Algorithm that initialite the state and to be used | ||
| in the QAA algorithm | ||
| func_oracle: The Oracle that marks the state(s) as "good" | ||
| system_qubits: the system we are interested on | ||
| qaa_ancilla: 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, Dagger | ||
| 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. Ancilla qubit is the second qubit | ||
| system_qubits = cmd.qubits[0] | ||
| qaa_ancilla = cmd.qubits[1] | ||
|
|
||
| # The Oracle and the Algorithm | ||
| Oracle = cmd.gate.oracle | ||
| A = cmd.gate.algorithm | ||
|
|
||
| # Apply the oracle to invert the amplitude of the good states, S_Chi | ||
| Oracle(eng, system_qubits, qaa_ancilla) | ||
|
|
||
| # Apply the inversion of the Algorithm, | ||
| # the inversion of the aplitude of |0> and the Algorithm | ||
|
|
||
| with Compute(eng): | ||
| with Dagger(eng): | ||
| A(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)] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.