-
Notifications
You must be signed in to change notification settings - Fork 282
Phase Estimation as a Gate in ops #260
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 47 commits into
ProjectQ-Framework:develop
from
fernandodelaiglesia:develop
May 6, 2019
Merged
Changes from 37 commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
b887f27
First testing version of Phase Estimation with a lot of hardcoding
c13e2f3
Adapt to All(Measure)
fernandodelaiglesia bb5fb57
Adding operators for more than 1 quibit, first version
fernandodelaiglesia 6a18cbf
Adding operators for more than 1 quibit, first versioni: testing
fernandodelaiglesia 8261385
Work in progress: create a PhaseX gate to tests via class
fernandodelaiglesia 630247d
Work in progress: create a PhaseX gate to tests via class. Clean garb…
fernandodelaiglesia d512a24
Work in progress: create a PhaseX gate to tests via class. Some enhan…
fernandodelaiglesia 764f60a
Work in progress: create a PhaseX gate to tests via class. PhaseX tes…
fernandodelaiglesia d94be6a
Work in progress: Debugging algorithm
fernandodelaiglesia 50cf592
Work in progress: Debugging algorithm
fernandodelaiglesia 9e81bcc
Adding 2qubit example
4225eac
adding 2 qubit Gate
fernandodelaiglesia 1f9a4f7
Initial version
fernandodelaiglesia cf8f58d
Merge branch 'develop' into develop
fernandodelaiglesia 6a4a7f8
Merge remote-tracking branch 'upstream/develop' into develop
fernandodelaiglesia 660cb20
Merge branch 'develop' of https://github.com/fernandodelaiglesia/Proj…
fernandodelaiglesia 143e816
Merge branch 'develop' into develop
fernandodelaiglesia aed12cd
Merge branch 'develop' into develop
fernandodelaiglesia 4874006
Merge remote-tracking branch 'upstream/develop' into develop
fernandodelaiglesia 34b5abe
Merge branch 'develop' into develop
fernandodelaiglesia 0091149
Merge remote-tracking branch 'upstream/develop' into develop
fernandodelaiglesia 54e7cc2
Create Phase Estimation as a new Gate in operations
fernandodelaiglesia 3ed0b01
Merge branch 'develop' of https://github.com/fernandodelaiglesia/Proj…
fernandodelaiglesia ac03bdc
Solving travis checks
fernandodelaiglesia 2d37dd4
python 2 compatibility + error in StatePreparation normalization
fernandodelaiglesia 8876d34
test coverage includes now the string
fernandodelaiglesia ed82cae
Improve the check test for no eigenvector test
fernandodelaiglesia 602e786
Merge branch 'develop' into develop
fernandodelaiglesia 0776589
Merge remote-tracking branch 'upstream/develop' into develop
fernandodelaiglesia 77a84b3
Start modifying to decomposition
fernandodelaiglesia 7a47ab2
Merge branch 'develop' of https://github.com/fernandodelaiglesia/Proj…
fernandodelaiglesia 24f47f6
QPE as decomposition and gate
fernandodelaiglesia 4cfc30f
QPE as decomposition and gate: correct a detail in the test
fernandodelaiglesia c6e5ebe
try to get the travis-ci freeze solved
fernandodelaiglesia 238f2e1
Resolve merge conflicts in docs/projectq.ops.rst incorporating both l…
fernandodelaiglesia a072724
Solve a name not defined error in the phaseestimation tests
fernandodelaiglesia 53639e7
Solve coverage in tests
fernandodelaiglesia 8c2d0ea
Merge branch 'develop' into develop
fernandodelaiglesia 2d20b80
Address comments in review + change how to assert the tests
fernandodelaiglesia 1172088
Merge branch 'develop' of https://github.com/fernandodelaiglesia/Proj…
fernandodelaiglesia a676d29
Enhance statistis in the tests bi more executions
fernandodelaiglesia 393c8f1
Correct bad calculation in tests
fernandodelaiglesia 016248d
Refine test
fernandodelaiglesia d8262a8
Merge branch 'develop' into develop
fernandodelaiglesia 4f920e3
Merge remote-tracking branch 'upstream/develop' into develop
fernandodelaiglesia edfa648
Merge branch 'develop' of https://github.com/fernandodelaiglesia/Proj…
fernandodelaiglesia 867fecb
Address Andi comments: add detail in the examples and atributes and r…
fernandodelaiglesia 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,29 @@ | ||
| # Copyright 2017 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 QPE(BasicGate): | ||
| """ | ||
| Quantum Phase Estimation gate. | ||
|
|
||
| See setups.decompositions for the complete implementation | ||
| """ | ||
| def __init__(self, unitary): | ||
| BasicGate.__init__(self) | ||
| self.unitary = unitary | ||
|
|
||
| def __str__(self): | ||
| return "QPE_"+str(self.unitary) | ||
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,23 @@ | ||
| # Copyright 2017 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._qpegate.""" | ||
|
|
||
| from projectq.ops import _qpegate, X | ||
|
|
||
|
|
||
| def test_qpe_str(): | ||
| unitary = X | ||
| gate = _qpegate.QPE(unitary) | ||
| assert str(gate) == "QPE_X" |
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,97 @@ | ||
| # Copyright 2018 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 phase estimation. | ||
|
|
||
| (reference https://en.wikipedia.org/wiki/Quantum_phase_estimation_algorithm) | ||
|
|
||
| The Quantum Phase Estimation (QPE) executes the algorithm up to the inverse | ||
| QFT included. The following steps measuring the ancillas and computing the | ||
| phase should be executed outside of the QPE. | ||
|
|
||
| The decomposition uses as ancillas (qpe_ancillas) the first qubit/qureg in | ||
| the Command and as system qubits teh second qubit/qureg in the Command. | ||
|
|
||
| The unitary operator for which the phase estimation is estimated (unitary) | ||
| is the gate in Command | ||
|
|
||
| Example: | ||
| .. code-block:: python | ||
|
|
||
| n_qpe_ancillas = 5 | ||
| qpe_ancillas = eng.allocate_qureg(n_qpe_ancillas) | ||
| system_qubits = eng.allocate_qureg(2) | ||
| U = unitary_specfic_to_the_problem() | ||
fernandodelaiglesia marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # Apply Quantum Phase Estimation | ||
| QPE(U) | (qpe_ancillas, system_qubits) | ||
|
|
||
| All(Measure) | qpe_ancillas | ||
| # Compute the phase from the ancilla measurement | ||
| #(https://en.wikipedia.org/wiki/Quantum_phase_estimation_algorithm) | ||
| phasebinlist = [int(q) for q in qpe_ancillas] | ||
| phase_in_bin = ''.join(str(j) for j in phasebinlist) | ||
| phase_int = int(phase_in_bin,2) | ||
| phase = phase_int / (2 ** n_qpe_ancillas) | ||
|
|
||
| Attributes: | ||
| unitary (BasicGate): Unitary Operation or function to apply | ||
| on the system_qubits (e.g.: function(system_qubits, time)) | ||
|
|
||
| """ | ||
|
|
||
| import numpy as np | ||
|
|
||
| from projectq.cengines import DecompositionRule | ||
| from projectq.meta import Control, Loop, get_control_count | ||
| from projectq.ops import H, Tensor, get_inverse, QFT | ||
|
|
||
| from projectq.ops import QPE | ||
|
|
||
|
|
||
| def _decompose_QPE(cmd): | ||
| """ Decompose the Quantum Phase Estimation gate. """ | ||
| eng = cmd.engine | ||
|
|
||
| # Ancillas is the first qubit/qureg. System-qubit is the second qubit/qureg | ||
| qpe_ancillas = cmd.qubits[0] | ||
| system_qubits = cmd.qubits[1] | ||
|
|
||
| # Hadamard on the ancillas | ||
| Tensor(H) | qpe_ancillas | ||
|
|
||
| # The Unitary Operator | ||
| U = cmd.gate.unitary | ||
|
|
||
| # Control U on the system_qubits | ||
| if (callable(U)): | ||
| # If U is a function | ||
| for i in range(len(qpe_ancillas)): | ||
| with Control(eng, qpe_ancillas[i]): | ||
| U(system_qubits, time=2**i) | ||
| else: | ||
| for i in range(len(qpe_ancillas)): | ||
| ipower = int(2**i) | ||
| with Loop(eng, ipower): | ||
| with Control(eng, qpe_ancillas[i]): | ||
| U | system_qubits | ||
|
|
||
| # Inverse QFT on the ancillas | ||
| get_inverse(QFT) | qpe_ancillas | ||
|
|
||
| #: Decomposition rules | ||
| all_defined_decomposition_rules = [ | ||
| DecompositionRule(QPE, _decompose_QPE) | ||
| ] | ||
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.