-
Notifications
You must be signed in to change notification settings - Fork 59
[Gridsynth 1] Gridsynth decomposition #2140
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
Open
josephleekl
wants to merge
60
commits into
main
Choose a base branch
from
rs-decomp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
60 commits
Select commit
Hold shift + click to select a range
7d393f7
rs-decomp
josephleekl 6189426
update
josephleekl b3be643
update
josephleekl ba25e5a
update
josephleekl 2ab894f
update
josephleekl 1947de4
update
josephleekl 45a64b7
update
josephleekl 5402851
remove unnecessary include
josephleekl 8748550
update
josephleekl e49db23
Merge branch 'main' into rs-decomp
josephleekl 79d6712
dummy impl
josephleekl b10d42c
remove debug
josephleekl 328a0be
clean
josephleekl 4648c0e
decomp takes theta and epsilon
josephleekl d3635d1
Merge branch 'main' into rs-decomp
josephleekl b7e4f75
Merge branch 'main' into rs-decomp
josephleekl fbde78a
support RZ and updated frontend
josephleekl 9114b32
format
josephleekl 04fb5af
Merge branch 'main' into rs-decomp
josephleekl e5f2bf4
fix sourceop has more than one input/output
josephleekl c3cd186
allow direct lowering to ppr
josephleekl b929fe5
Merge branch 'main' into rs-decomp
josephleekl 828c179
format
josephleekl 5caaa53
clean up
josephleekl 8fb7605
fix mac segfault
josephleekl f29c9ba
extractop allow dynamic/static indices
josephleekl 6537d4f
Merge branch 'main' into rs-decomp
josephleekl 2afb190
change rsdecomposition to gridsynth
josephleekl 93c04c4
fix missing dialect prefix in xdsl detection
mehrdad2m d8ba2c0
Merge branch 'main' of https://github.com/PennyLaneAI/catalyst into r…
lazypanda10117 e39bb99
Merge branch 'main' of https://github.com/PennyLaneAI/catalyst into r…
lazypanda10117 acf887a
Merge branch 'main' into rs-decomp
josephleekl 183e262
Merge branch 'main' into rs-decomp
josephleekl 8126f9b
update mlir pass
josephleekl f42e091
Merge branch 'main' into rs-decomp
josephleekl 41b95e8
Update mlir/include/Quantum/Transforms/Passes.td
josephleekl c4e8a64
update RSDecomp file name
josephleekl b62ab74
use pennylane branch for doc
josephleekl a148efe
update dep version for docs
josephleekl f59612b
add test
josephleekl 5cc42fb
add docs
josephleekl 1ae3c54
Update frontend/catalyst/compiler.py
josephleekl 07153e2
docs
josephleekl 94e550f
small cleanup
josephleekl d8be26a
fix phaseshift phase
josephleekl 17a0778
review comments
josephleekl 80cabdf
isExternal -> getCallableRegion
paul0403 18dd269
Apply suggestions from code review
josephleekl fb2c012
Merge branch 'main' into rs-decomp
josephleekl ea84709
code review
josephleekl 2977e98
Add `estimated_iterations` attribute to forOp in GridsynthPatterns
sengthai 067a398
use heap alloc and add doc
josephleekl 736b357
review comments - template declarefunc, alloc
josephleekl b454fbe
break up big getOrCreateDecompositionFunc function
josephleekl 9ef0342
remove unnecessary using namespace
josephleekl e8cc971
Merge branch 'main' into rs-decomp
josephleekl 22acc0d
add frontend lit test
josephleekl f4aeb50
codefactor
josephleekl a28ae7f
use size_t instead of int64_t for runtime func
josephleekl b78d2b7
update test
josephleekl 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,266 @@ | ||
| # Copyright 2025 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. | ||
|
|
||
| """ | ||
| Unit tests for the gridsynth decomposition pass. | ||
| """ | ||
|
|
||
| # RUN: %PYTHON %s | FileCheck %s | ||
| # pylint: disable=line-too-long | ||
|
|
||
| from functools import partial | ||
|
|
||
| import pennylane as qml | ||
|
|
||
| from catalyst import qjit | ||
| from catalyst.passes import gridsynth | ||
|
|
||
| # Pipeline to stop after quantum compilation (where gridsynth runs) | ||
| # This prevents lowerings that might fail for qec.ppr. | ||
| pipe = [("pipe", ["quantum-compilation-stage"])] | ||
|
|
||
| # ============================================================================== | ||
| # Test 1: RZ Registration (Clifford+T basis) | ||
| # ============================================================================== | ||
|
|
||
|
|
||
| def test_rz_registration(): | ||
| """Test that the gridsynth pass is correctly registered for RZ.""" | ||
|
|
||
| @qjit(target="mlir") | ||
| @gridsynth(epsilon=0.01) | ||
| @qml.qnode(qml.device("lightning.qubit", wires=1)) | ||
| def circuit(x: float): | ||
| qml.RZ(x, wires=0) | ||
| return qml.probs() | ||
|
|
||
| # CHECK-LABEL: test_rz_registration | ||
| print("test_rz_registration") | ||
| # CHECK: transform.named_sequence @__transform_main | ||
| # CHECK: transform.apply_registered_pass "gridsynth" with options = {{[{]}}"epsilon" = 1.000000e-02 : f64, "ppr-basis" = false{{[}]}} | ||
| # CHECK-LABEL: func.func public @circuit | ||
| # CHECK: quantum.custom "RZ" | ||
| print(circuit.mlir) | ||
|
|
||
|
|
||
| test_rz_registration() | ||
|
|
||
| # ============================================================================== | ||
| # Test 2: RZ Lowering (Clifford+T basis) | ||
| # ============================================================================== | ||
|
|
||
|
|
||
| def test_rz_lowering(): | ||
| """Test that RZ is correctly lowered to the decomposition function.""" | ||
|
|
||
| @qjit(target="mlir", pipelines=pipe) | ||
| @gridsynth(epsilon=0.01) | ||
| @qml.qnode(qml.device("lightning.qubit", wires=1)) | ||
| def circuit(x: float): | ||
| qml.RZ(x, wires=0) | ||
| return qml.probs() | ||
|
|
||
| # CHECK-LABEL: test_rz_lowering | ||
| print("test_rz_lowering") | ||
|
|
||
| # CHECK-LABEL: func.func private @__catalyst_decompose_RZ{{.*}} | ||
| # CHECK: scf.index_switch | ||
| # CHECK: case 0 { | ||
| # CHECK: quantum.custom "T" | ||
| # CHECK: } | ||
|
|
||
| # CHECK-LABEL: func.func public @circuit{{.*}} | ||
| # CHECK-NOT: quantum.custom "RZ" | ||
| # CHECK: call @__catalyst_decompose_RZ{{.*}} | ||
| print(circuit.mlir_opt) | ||
|
|
||
|
|
||
| test_rz_lowering() | ||
|
|
||
| # ============================================================================== | ||
| # Test 3: PhaseShift Registration | ||
| # ============================================================================== | ||
|
|
||
|
|
||
| def test_phaseshift_registration(): | ||
| """Test that the gridsynth pass is correctly registered for PhaseShift.""" | ||
|
|
||
| @qjit(target="mlir") | ||
| @gridsynth(epsilon=0.01) | ||
| @qml.qnode(qml.device("lightning.qubit", wires=1)) | ||
| def circuit(x: float): | ||
| qml.PhaseShift(x, wires=0) | ||
| return qml.probs() | ||
|
|
||
| # CHECK-LABEL: test_phaseshift_registration | ||
| print("test_phaseshift_registration") | ||
| # CHECK: transform.apply_registered_pass "gridsynth" | ||
| # CHECK-LABEL: func.func public @circuit | ||
| # CHECK: quantum.custom "PhaseShift" | ||
| print(circuit.mlir) | ||
|
|
||
|
|
||
| test_phaseshift_registration() | ||
|
|
||
| # ============================================================================== | ||
| # Test 4: PhaseShift Lowering | ||
| # ============================================================================== | ||
|
|
||
|
|
||
| def test_phaseshift_lowering(): | ||
| """Test that PhaseShift is decomposed into RZ + GlobalPhase.""" | ||
|
|
||
| @qjit(target="mlir", pipelines=pipe) | ||
| @gridsynth(epsilon=0.01) | ||
| @qml.qnode(qml.device("lightning.qubit", wires=1)) | ||
| def circuit(x: float): | ||
| qml.PhaseShift(x, wires=0) | ||
| return qml.probs() | ||
|
|
||
| # CHECK-LABEL: test_phaseshift_lowering | ||
| print("test_phaseshift_lowering") | ||
|
|
||
| # CHECK-LABEL: func.func private @__catalyst_decompose_RZ{{.*}} | ||
|
|
||
| # CHECK-LABEL: func.func public @circuit{{.*}} | ||
| # CHECK: call @__catalyst_decompose_RZ{{.*}} | ||
| # CHECK: quantum.gphase | ||
| print(circuit.mlir_opt) | ||
|
|
||
|
|
||
| test_phaseshift_lowering() | ||
|
|
||
| # ============================================================================== | ||
| # Test 5: PPR Registration | ||
| # ============================================================================== | ||
|
|
||
|
|
||
| def test_ppr_registration(): | ||
| """Test that ppr_basis=True is passed to the transform.""" | ||
|
|
||
| @qjit(target="mlir") | ||
| @gridsynth(epsilon=0.01, ppr_basis=True) | ||
| @qml.qnode(qml.device("lightning.qubit", wires=1)) | ||
| def circuit(x: float): | ||
| qml.RZ(x, wires=0) | ||
| return qml.probs() | ||
|
|
||
| # CHECK-LABEL: test_ppr_registration | ||
| print("test_ppr_registration") | ||
| # CHECK: transform.apply_registered_pass "gridsynth" with options = {{[{]}}"epsilon" = 1.000000e-02 : f64, "ppr-basis" = true{{[}]}} | ||
| print(circuit.mlir) | ||
|
|
||
|
|
||
| test_ppr_registration() | ||
|
|
||
| # ============================================================================== | ||
| # Test 6: PPR Lowering | ||
| # ============================================================================== | ||
|
|
||
|
|
||
| def test_ppr_lowering(): | ||
| """Test that PPR basis generates qec.ppr operations.""" | ||
|
|
||
| @qjit(target="mlir", pipelines=pipe) | ||
| @gridsynth(epsilon=0.01, ppr_basis=True) | ||
| @qml.qnode(qml.device("lightning.qubit", wires=1)) | ||
| def circuit(x: float): | ||
| qml.RZ(x, wires=0) | ||
| return qml.probs() | ||
|
|
||
| # CHECK-LABEL: test_ppr_lowering | ||
| print("test_ppr_lowering") | ||
|
|
||
| # CHECK-LABEL: func.func private @__catalyst_decompose_RZ_ppr_basis{{.*}} | ||
| # CHECK: scf.index_switch | ||
| # CHECK: case 1 { | ||
| # CHECK: qec.ppr ["X"](2) | ||
| # CHECK: } | ||
|
|
||
| # CHECK-LABEL: func.func public @circuit{{.*}} | ||
| # CHECK: call @__catalyst_decompose_RZ_ppr_basis{{.*}} | ||
| print(circuit.mlir_opt) | ||
|
|
||
|
|
||
| test_ppr_lowering() | ||
|
|
||
|
|
||
| # ============================================================================== | ||
| # Test 7: Capture Workflow Lowering (Clifford+T) | ||
| # ============================================================================== | ||
|
|
||
|
|
||
| def test_capture_workflow_clifford(): | ||
| """Test the capture workflow with qml.transforms.gridsynth (Clifford+T).""" | ||
| qml.capture.enable() | ||
|
|
||
| @qjit(target="mlir", pipelines=pipe) | ||
| @partial(qml.transforms.gridsynth, epsilon=0.01, ppr_basis=False) | ||
| @qml.qnode(qml.device("lightning.qubit", wires=1)) | ||
| def circuit(x: float): | ||
| qml.RZ(x, wires=0) | ||
| return qml.probs() | ||
|
|
||
| # CHECK-LABEL: test_capture_workflow_clifford | ||
| print("test_capture_workflow_clifford") | ||
|
|
||
| # CHECK-LABEL: func.func private @__catalyst_decompose_RZ{{.*}} | ||
| # CHECK: scf.index_switch | ||
| # CHECK: case 0 { | ||
| # CHECK: quantum.custom "T" | ||
| # CHECK: } | ||
|
|
||
| # CHECK-LABEL: func.func public @circuit{{.*}} | ||
| # CHECK-NOT: quantum.custom "RZ" | ||
| # CHECK: call @__catalyst_decompose_RZ{{.*}} | ||
| print(circuit.mlir_opt) | ||
|
|
||
| qml.capture.disable() | ||
|
|
||
|
|
||
| test_capture_workflow_clifford() | ||
|
|
||
| # ============================================================================== | ||
| # Test 8: Capture Workflow Lowering (PPR) | ||
| # ============================================================================== | ||
|
|
||
|
|
||
| def test_capture_workflow_ppr(): | ||
| """Test the capture workflow with qml.transforms.gridsynth (PPR).""" | ||
| qml.capture.enable() | ||
|
|
||
| @qjit(target="mlir", pipelines=pipe) | ||
| @partial(qml.transforms.gridsynth, epsilon=0.01, ppr_basis=True) | ||
| @qml.qnode(qml.device("lightning.qubit", wires=1)) | ||
| def circuit(x: float): | ||
| qml.RZ(x, wires=0) | ||
| return qml.probs() | ||
|
|
||
| # CHECK-LABEL: test_capture_workflow_ppr | ||
| print("test_capture_workflow_ppr") | ||
|
|
||
| # CHECK-LABEL: func.func private @__catalyst_decompose_RZ_ppr_basis{{.*}} | ||
| # CHECK: scf.index_switch | ||
| # CHECK: case 1 { | ||
| # CHECK: qec.ppr ["X"](2) | ||
| # CHECK: } | ||
|
|
||
| # CHECK-LABEL: func.func public @circuit{{.*}} | ||
| # CHECK: call @__catalyst_decompose_RZ_ppr_basis{{.*}} | ||
| print(circuit.mlir_opt) | ||
|
|
||
| qml.capture.disable() | ||
|
|
||
|
|
||
| test_capture_workflow_ppr() |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here