-
Notifications
You must be signed in to change notification settings - Fork 78
add translation and lowering of OperatorOp #2969
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
+324
−11
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
034e500
add translation and lowering of OperatorOp
albi3ro a9308fc
sonnet's AI recommendation to fix python bindings issue
albi3ro 8599ec6
switch to StrAttr
albi3ro 5c4504e
bump pl version
albi3ro b23289a
remove qref patch
albi3ro 6d4de2b
formatting
albi3ro daca7ae
black
albi3ro 05fa495
Merge branch 'main' into handle-operator2
albi3ro 79e060e
starting to add some tests
albi3ro 65af9f5
Merge branch 'handle-operator2' of github.com:PennyLaneAI/catalyst in…
albi3ro 98c88c4
tests, changelog, formatting
albi3ro 862375e
pylint
albi3ro 6e4eaf7
why didn't black work
albi3ro 5546ee7
Merge branch 'main' into handle-operator2
albi3ro 78b4f9e
more pylint
albi3ro ffca762
Merge branch 'handle-operator2' of github.com:PennyLaneAI/catalyst in…
albi3ro 54ba606
Merge branch 'main' into handle-operator2
albi3ro 99ec30b
Update frontend/test/lit/test_operator.py
albi3ro 1fafe40
rename to qref_operator_p
albi3ro f2e0b73
responding to feedback
albi3ro dd34ff6
remove unused import
albi3ro d586798
Merge branch 'main' into handle-operator2
albi3ro 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
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,156 @@ | ||
| # Copyright 2022-2023 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. | ||
| """Tests for operator in Catalyst.""" | ||
|
|
||
| # pylint: disable = useless-parent-delegation, missing-function-docstring, missing-class-docstring | ||
|
|
||
| # RUN: %PYTHON %s | FileCheck %s | ||
|
|
||
| import numpy as np | ||
| import pennylane as qp | ||
|
|
||
|
|
||
| class NoParams(qp.core.Operator2): | ||
|
|
||
| def __init__(self, wires): | ||
| super().__init__(wires=wires) | ||
|
|
||
|
|
||
| @qp.qjit(target="mlir", capture=True) | ||
| @qp.qnode(qp.device("null.qubit", wires=2)) | ||
| def c_no_params(): | ||
| # CHECK: [[q0:%.+]] = qref.get {{%.+}} | ||
| # CHECK: qref.operator "NoParams"() qubits([[q0]]) | ||
| # CHECK: static_data = {} | ||
| # CHECK: param_map = {} qubit_map = {wires = [0]} | ||
| NoParams(wires=0) | ||
|
|
||
| # CHECK: [[q1:%.+]] = qref.get {{%.+}} | ||
| # CHECK: [[q2:%.+]] = qref.get {{%.+}} | ||
| # CHECK: qref.operator "NoParams"() qubits([[q1]], [[q2]]) | ||
| # CHECK: static_data = {} | ||
| # CHECK: param_map = {} qubit_map = {wires = [0, 1]} | ||
| NoParams(wires=(0, 1)) | ||
| return qp.state() | ||
|
|
||
|
|
||
| print(c_no_params.mlir) | ||
|
|
||
|
|
||
| class SingleParam(qp.core.Operator2): | ||
|
|
||
| dynamic_argnames = ("x",) | ||
|
|
||
| def __init__(self, x, wires): | ||
| super().__init__(x, wires=wires) | ||
|
|
||
|
|
||
| @qp.qjit(target="mlir", capture=True) | ||
| @qp.qnode(qp.device("null.qubit", wires=3)) | ||
| def c_single_param(x: float): | ||
|
|
||
| # CHECK: [[q0:%.+]] = qref.get {{%.+}} | ||
|
|
||
| # CHECK: qref.operator "SingleParam"({{%.+}}: tensor<f64>) qubits([[q0]]) | ||
| # CHECK: static_data = {} | ||
| # CHECK: param_map = {x = [0]} qubit_map = {wires = [0]} | ||
| SingleParam(x, 0) | ||
|
albi3ro marked this conversation as resolved.
|
||
|
|
||
| # CHECK: [[q1:%.+]] = qref.get {{%.+}} | ||
| # CHECK: [[q2:%.+]] = qref.get {{%.+}} | ||
| # CHECK: qref.operator "SingleParam"({{%.+}}: tensor<4x4xf64>) qubits([[q1]], [[q2]]) | ||
| # CHECK: static_data = {} | ||
| # CHECK: param_map = {x = [0]} qubit_map = {wires = [0, 1]} | ||
| SingleParam(np.eye(4), (1, 2)) | ||
|
|
||
| return qp.state() | ||
|
|
||
|
|
||
| print(c_single_param.mlir) | ||
|
|
||
|
|
||
| class CompilableData(qp.core.Operator2): | ||
|
|
||
| compilable_argnames = ("a", "b", "thing") | ||
|
|
||
| def __init__(self, a, b, thing, wires): | ||
| super().__init__(a=a, b=b, thing=thing, wires=wires) | ||
|
|
||
|
|
||
| @qp.qjit(capture=True, target="mlir") | ||
| @qp.qnode(qp.device("null.qubit", wires=3)) | ||
| def c_compilable(): | ||
| # CHECK: [[q1:%.+]] = qref.get {{%.+}} | ||
| # CHECK: [[q2:%.+]] = qref.get {{%.+}} | ||
| # CHECK: qref.operator "CompilableData"() qubits([[q1]], [[q2]]) | ||
| # CHECK: static_data = {a = true, b = "some string", thing = [1, true, "string"]} | ||
| # CHECK: param_map = {} qubit_map = {wires = [0, 1]} | ||
|
|
||
| CompilableData(True, "some string", (1, True, "string"), wires=(0, 1)) | ||
|
|
||
| return qp.state() | ||
|
|
||
|
|
||
| print(c_compilable.mlir) | ||
|
|
||
|
|
||
| class MultipleRegisters(qp.core.Operator2): | ||
|
|
||
| wire_argnames = ("reg1", "reg2") | ||
|
|
||
| def __init__(self, reg1, reg2): | ||
| super().__init__(reg1=reg1, reg2=reg2) | ||
|
|
||
|
|
||
| @qp.qjit(capture=True, target="mlir") | ||
| @qp.qnode(qp.device("null.qubit", wires=5)) | ||
| def c_multiple_registers(): | ||
| # CHECK: [[q0:%.+]] = qref.get {{%.+}} | ||
| # CHECK: [[q2:%.+]] = qref.get {{%.+}} | ||
| # CHECK: [[q3:%.+]] = qref.get {{%.+}} | ||
| # CHECK: [[q4:%.+]] = qref.get {{%.+}} | ||
|
|
||
| # CHECK: qref.operator "MultipleRegisters"() qubits([[q0]], [[q2]], [[q3]], [[q4]]) | ||
| # CHECK: static_data = {} | ||
| # CHECK: param_map = {} qubit_map = {reg1 = [0], reg2 = [1, 2, 3]} | ||
| MultipleRegisters(0, (2, 3, 4)) | ||
| return qp.state() | ||
|
|
||
|
|
||
| print(c_multiple_registers.mlir) | ||
|
|
||
|
|
||
| class MultiParams(qp.core.Operator2): | ||
|
|
||
| dynamic_argnames = ("a", "b", "c") | ||
|
|
||
| # note also having non-standard order with dynamic inputs after wires | ||
| def __init__(self, wires, a, b, c): | ||
| super().__init__(wires, a, b, c) | ||
|
|
||
|
|
||
| @qp.qjit(capture=True, target="mlir") | ||
| @qp.qnode(qp.device("null.qubit", wires=1)) | ||
| def c_multi_params(): | ||
| # CHECK: [[q0:%.+]] = qref.get {{%.+}} | ||
|
|
||
| # pylint: disable=line-too-long | ||
| # CHECK: qref.operator "MultiParams"({{%.+}}: tensor<f64>, {{%.+}}: tensor<4x2x1xf64>, {{%.+}}: tensor<3xi64>) qubits([[q0]]) | ||
| # CHECK: static_data = {} | ||
| # CHECK: param_map = {a = [0], b = [1], c = [2]} qubit_map = {wires = [0]} | ||
| MultiParams(0, 0.5, c=np.array([1, 2, 3]), b=np.zeros((4, 2, 1))) | ||
| return qp.state() | ||
|
|
||
|
|
||
| print(c_multi_params.mlir) | ||
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.