-
Notifications
You must be signed in to change notification settings - Fork 714
GQSP template and angle solver #6565
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
Merged
Changes from all commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
64612a3
main functions
KetpuntoG 94752b4
Update angle_solver.py
KetpuntoG df00544
some tests
KetpuntoG 7d3a320
some changes
KetpuntoG 2fac287
clarifications
KetpuntoG ca185e2
black
KetpuntoG 6c00f5b
codefactor
KetpuntoG 0522f11
Update angle_solver.py
KetpuntoG 970cc76
Merge branch 'master' into angle_solver_qsp
KetpuntoG 8d72102
codefactor
KetpuntoG bef2be2
Merge branch 'angle_solver_qsp' of https://github.com/PennyLaneAI/pen…
KetpuntoG 1c8e6fc
stop recording
KetpuntoG b57312a
Merge branch 'master' into angle_solver_qsp
KetpuntoG 6178b23
Merge branch 'master' into angle_solver_qsp
KetpuntoG e63fad7
Merge branch 'master' into angle_solver_qsp
KetpuntoG a9959a1
update function
KetpuntoG bfe3b87
[skip-ci]
KetpuntoG bc8e086
template and some tests
KetpuntoG 762fa5b
docs
KetpuntoG d99f884
[skip ci]
KetpuntoG e15f259
more tests
KetpuntoG ab6b41d
Merge branch 'angle_solver_qsp' into gqsp_template_solver
KetpuntoG a674bbc
black
KetpuntoG 137e8f3
Merge branch 'master' into angle_solver_qsp
KetpuntoG f3baeda
Update angle_solver.py
KetpuntoG 0474821
Merge branch 'master' into angle_solver_qsp
KetpuntoG 1240a12
Merge branch 'angle_solver_qsp' into gqsp_template_solver
KetpuntoG 6e3f481
Update angle_solver.py
KetpuntoG 4a9b95b
Update gqsp.py
KetpuntoG eae45d5
capture tests
KetpuntoG 5b7185a
clarifications and pylint
KetpuntoG a69e423
remove patata.py
KetpuntoG 9e0d2b5
too-many-public-method
KetpuntoG 4b2c104
Update test_templates.py
KetpuntoG 04db809
unused np and wires
KetpuntoG a1b13af
Update test_gqsp.py
KetpuntoG 08db980
Apply suggestions from code review [skip-ci]
KetpuntoG baa374b
Merge branch 'master' into gqsp_template_solver
KetpuntoG eebe03e
[skip ci]
KetpuntoG 463819f
Merge branch 'gqsp_template_solver' of https://github.com/PennyLaneAI…
KetpuntoG 1dd2460
review comments + solve conflict
KetpuntoG 3df9e29
fixing dodc
KetpuntoG 31b53f7
Update qsvt.py
KetpuntoG e18cb33
Update pennylane/templates/subroutines/gqsp.py
KetpuntoG e8b6375
Apply suggestions from code review
KetpuntoG 90dc212
thumbnail and last comments
KetpuntoG 87ba291
Merge branch 'gqsp_template_solver' of https://github.com/PennyLaneAI…
KetpuntoG f81b5a2
Merge branch 'master' into gqsp_template_solver
KetpuntoG 4bd4630
Update gqsp.py
KetpuntoG c395554
Merge branch 'gqsp_template_solver' of https://github.com/PennyLaneAI…
KetpuntoG b5b5869
Update gqsp.py
KetpuntoG 2b6866d
Merge branch 'master' into gqsp_template_solver
KetpuntoG d5fdeb3
Merge branch 'master' into gqsp_template_solver
KetpuntoG a978842
Update test_gqsp.py
KetpuntoG c3a40d5
Update pennylane/templates/subroutines/gqsp.py
KetpuntoG 6cdf86c
Merge branch 'master' into gqsp_template_solver
KetpuntoG 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,167 @@ | ||
| # Copyright 2024 Xanadu Quantum Technologies Inc. | ||
|
|
||
| # 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. | ||
| """ | ||
| Contains the GQSP template. | ||
| """ | ||
| # pylint: disable=too-many-arguments | ||
|
|
||
| import copy | ||
|
|
||
| import pennylane as qml | ||
| from pennylane.operation import Operation | ||
| from pennylane.queuing import QueuingManager | ||
| from pennylane.wires import Wires | ||
|
|
||
|
|
||
| class GQSP(Operation): | ||
| r""" | ||
| Implements the generalized quantum signal processing (GQSP) circuit. | ||
|
|
||
| This operation encodes a polynomial transformation of an input unitary operator following the algorithm | ||
| described in `arXiv:2308.01501 <https://arxiv.org/abs/2308.01501>`__ as: | ||
|
|
||
| .. math:: | ||
| U | ||
| \xrightarrow{GQSP} | ||
| \begin{pmatrix} | ||
| \text{poly}(U) & * \\ | ||
| * & * \\ | ||
| \end{pmatrix} | ||
|
|
||
| The implementation requires one auxiliary qubit. | ||
|
|
||
| Args: | ||
|
|
||
| unitary (Operator): the operator to be encoded by the GQSP circuit | ||
| angles (tensor[float]): array of angles defining the polynomial transformation. The shape of the array must be `(3, d+1)`, where `d` is the degree of the polynomial. | ||
| control (Union[Wires, int, str]): control qubit used to encode the polynomial transformation | ||
|
|
||
| .. note:: | ||
|
|
||
| The :func:`~.poly_to_angles` function can be used to calculate the angles for a given polynomial. | ||
|
|
||
| Example: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| # P(x) = 0.1 + 0.2j x + 0.3 x^2 | ||
| poly = [0.1, 0.2j, 0.3] | ||
|
|
||
| angles = qml.poly_to_angles(poly, "GQSP") | ||
|
|
||
| @qml.prod # transforms the qfunc into an Operator | ||
KetpuntoG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def unitary(wires): | ||
| qml.RX(0.3, wires) | ||
|
|
||
| dev = qml.device("default.qubit") | ||
|
|
||
| @qml.qnode(dev) | ||
| def circuit(angles): | ||
| qml.GQSP(unitary(wires = 1), angles, control = 0) | ||
| return qml.state() | ||
|
|
||
KetpuntoG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| matrix = qml.matrix(circuit, wire_order=[0, 1])(angles) | ||
|
|
||
| .. code-block:: pycon | ||
|
|
||
| >>> print(np.round(matrix,3)[:2, :2]) | ||
| [[0.387+0.198j 0.03 -0.089j] | ||
| [0.03 -0.089j 0.387+0.198j]] | ||
| """ | ||
|
|
||
| grad_method = None | ||
|
|
||
| def __init__(self, unitary, angles, control, id=None): | ||
| total_wires = qml.wires.Wires(control) + unitary.wires | ||
|
|
||
| self._hyperparameters = {"unitary": unitary, "control": qml.wires.Wires(control)} | ||
|
|
||
| super().__init__(angles, *unitary.data, wires=total_wires, id=id) | ||
|
|
||
| def _flatten(self): | ||
| data = self.parameters | ||
| return data, ( | ||
| self.hyperparameters["unitary"], | ||
| self.hyperparameters["control"], | ||
| ) | ||
|
|
||
| @classmethod | ||
| def _unflatten(cls, data, metadata): | ||
| return cls(unitary=metadata[0], angles=data[0], control=metadata[1]) | ||
|
|
||
| @classmethod | ||
| def _primitive_bind_call(cls, *args, **kwargs): | ||
| return cls._primitive.bind(*args, **kwargs) | ||
|
|
||
| def map_wires(self, wire_map: dict): | ||
| # pylint: disable=protected-access | ||
| new_op = copy.deepcopy(self) | ||
| new_op._wires = Wires([wire_map.get(wire, wire) for wire in self.wires]) | ||
| new_op._hyperparameters["unitary"] = qml.map_wires( | ||
| new_op._hyperparameters["unitary"], wire_map | ||
| ) | ||
| new_op._hyperparameters["control"] = tuple( | ||
| wire_map.get(w, w) for w in new_op._hyperparameters["control"] | ||
| ) | ||
|
|
||
| return new_op | ||
|
|
||
| @staticmethod | ||
| def compute_decomposition(*parameters, **hyperparameters): # pylint: disable=arguments-differ | ||
| r""" | ||
| Representation of the operator as a product of other operators (static method). | ||
|
|
||
| .. math:: O = O_1 O_2 \dots O_n. | ||
|
|
||
| .. seealso:: :meth:`~.Operator.decomposition`. | ||
|
|
||
| Args: | ||
| *parameters (list): trainable parameters of the operator, as stored in the ``parameters`` attribute | ||
| wires (Iterable[Any], Wires): wires that the operator acts on | ||
| **hyperparams (dict): non-trainable hyperparameters of the operator, as stored in the ``hyperparameters`` attribute | ||
|
|
||
| Returns: | ||
| list[Operator]: decomposition of the operator | ||
| """ | ||
|
|
||
| unitary = hyperparameters["unitary"] | ||
| control = hyperparameters["control"] | ||
|
|
||
| angles = parameters[0] | ||
|
|
||
| thetas, phis, lambds = angles[0], angles[1], angles[2] | ||
|
|
||
| op_list = [] | ||
|
|
||
| # These four gates adapt PennyLane's qml.U3 to the chosen U3 format in the GQSP paper. | ||
| op_list.append(qml.X(control)) | ||
| op_list.append(qml.U3(2 * thetas[0], phis[0], lambds[0], wires=control)) | ||
| op_list.append(qml.X(control)) | ||
| op_list.append(qml.Z(control)) | ||
|
|
||
| for theta, phi, lamb in zip(thetas[1:], phis[1:], lambds[1:]): | ||
|
|
||
| op_list.append(qml.ctrl(unitary, control=control, control_values=0)) | ||
|
|
||
| op_list.append(qml.X(control)) | ||
| op_list.append(qml.U3(2 * theta, phi, lamb, wires=control)) | ||
| op_list.append(qml.X(control)) | ||
| op_list.append(qml.Z(control)) | ||
|
|
||
| return op_list | ||
|
|
||
| def queue(self, context=QueuingManager): | ||
| context.remove(self.hyperparameters["unitary"]) | ||
| context.append(self) | ||
| return self | ||
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
Oops, something went wrong.
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.