-
Notifications
You must be signed in to change notification settings - Fork 78
Sara/quantum phase folding #2944
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
Draft
sarababaeii
wants to merge
39
commits into
main
Choose a base branch
from
sara/quantum-phase-folding
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.
Draft
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
95cf6c6
Add symbolic analysis core DS
4fc1c49
Resolved std/llvm ostream issue
97a4221
Add phase analyses pass
f4632c8
Merge remote-tracking branch 'origin/main' into sara/quantum-phase-fo…
b011200
Complete symbolic phase analyses of pure quantum circuits
4047d48
Merge remote-tracking branch 'origin/main' into sara/quantum-phase-fo…
dc6eca3
Add phase merge
5e2e88a
Clean PhaseBucket and Gate modules
406d6d6
Merge remote-tracking branch 'origin/main' into sara/quantum-phase-fo…
dadfade
Add qecl -> qecp lowering for `fabricate [magic]` and associated ops …
lillian542 fa8a468
Add conversion for magic-state subroutines back to quantum dialect (#…
lillian542 d5245d2
[no ci] bump nightly version
ringo-but-quantum d8acd70
Provide global toggles for static control flow folding (#2912)
dime10 f2d2d0e
Add support for reference semantics to the `resource-analysis` pass (…
kipawaa ac4a85b
Remove missing docs reference (#2913)
kipawaa 0b96ebb
Bugfix: support adjoint S in qecl->qecp lowering (#2922)
lillian542 c6cda64
Fix bug in Y and controlled gates
b8962dd
Fix merged rotation, different affine values in same parity
b40a303
Remove libboost-dev src installation dep (#2924)
maliasadi 3358731
[no ci] bump nightly version
ringo-but-quantum cbe8ac9
[qref 4.1] Implement semantics conversion from value to reference for…
paul0403 d0884f3
[no ci] bump nightly version
ringo-but-quantum 6eec865
Implement logic of mid-circuit register allocation
f82425e
Add mid-circuit register allocation
8ea3073
Clean Code
31d78a2
Add qubit initialization
98d3ab1
Clean merge phase
55c6fb0
Merge remote-tracking branch 'origin/main' into sara/quantum-phase-fo…
2ab9e9b
Remove 1-based qubit indices
f985aff
Merge remote-tracking branch 'origin/main' into sara/quantum-phase-fo…
5e5e887
Make code formatting
6737d87
Solve code formatting issue
0bd2633
Reformat SymbolicCircuit.cpp
81d31cc
Update qubit states after undefined operations
d44070e
Merge remote-tracking branch 'origin/main' into sara/quantum-phase-fo…
4e4b428
Make format
26b091d
Merge remote-tracking branch 'origin/main' into sara/quantum-phase-fo…
d990f2c
Add copyright text
2b71457
Fix wrong merge of phase gates conditional on bot
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| func.func @ex_425(%arg0: tensor<2xi64>) -> (!quantum.bit, !quantum.bit) { | ||
| %reg = quantum.alloc( 2) : !quantum.reg | ||
| %q0 = quantum.extract %reg[ 0] : !quantum.reg -> !quantum.bit | ||
| %q1 = quantum.extract %reg[ 1] : !quantum.reg -> !quantum.bit | ||
|
|
||
| // %2 = stablehlo.convert %arg0 : (tensor<2xi64>) -> tensor<2xcomplex<f64>> | ||
| // %3 = quantum.set_state(%2) %q1 : (tensor<2xcomplex<f64>>, !quantum.bit) -> !quantum.bit | ||
| // %q3 = quantum.custom "T"() %3 : !quantum.bit | ||
|
|
||
| %tens01 = arith.constant dense<[false]> : tensor<1xi1> | ||
| %q2 = quantum.set_basis_state(%tens01) %q1 : (tensor<1xi1>, !quantum.bit) -> !quantum.bit | ||
|
|
||
| %q3 = quantum.custom "T"() %q2 : !quantum.bit // l1 | ||
| %q4:2 = quantum.custom "CNOT"() %q0, %q3 : !quantum.bit, !quantum.bit | ||
| %q5 = quantum.custom "T"() %q4#0 : !quantum.bit // l2 | ||
| %65 = quantum.custom "T"() %q4#1 : !quantum.bit // l3 | ||
|
|
||
| func.return %q0, %q3 : !quantum.bit, !quantum.bit | ||
| } | ||
|
|
||
| func.func @ex_1() -> (!quantum.bit, !quantum.bit) { | ||
| %reg = quantum.alloc( 2) : !quantum.reg | ||
| // %i = arith.constant 1 : index | ||
| %q0 = quantum.extract %reg[ 0] : !quantum.reg -> !quantum.bit | ||
| %q1 = quantum.extract %reg[ 1] : !quantum.reg -> !quantum.bit | ||
|
|
||
| // %c0 = arith.constant 3.141592 : f64 | ||
|
|
||
| %tens01 = arith.constant dense<[0]> : tensor<1xi1> | ||
| %q18 = quantum.set_basis_state(%tens01) %q1 : (tensor<1xi1>, !quantum.bit) -> !quantum.bit | ||
|
|
||
| %q2 = quantum.custom "T"() %q0 : !quantum.bit // l1 | ||
| // %q2 = quantum.custom "RZ"(%c0) %q0 : !quantum.bit | ||
| %q3 = quantum.custom "T"() %q18 : !quantum.bit // l2 | ||
| %q4:2 = quantum.custom "CNOT"() %q2, %q3 : !quantum.bit, !quantum.bit | ||
| %q5 = quantum.custom "T"() %q4#1 {adjoint}: !quantum.bit // l3 | ||
| %q6:2 = quantum.custom "CNOT"() %q4#0, %q5 : !quantum.bit, !quantum.bit | ||
|
|
||
| %q7 = quantum.custom "Hadamard"() %q6#1 : !quantum.bit | ||
|
|
||
| %q21 = quantum.custom "PauliX"() %q6#0 : !quantum.bit | ||
|
|
||
| %q8 = quantum.custom "T"() %q21 : !quantum.bit // l4 | ||
| %q9 = quantum.custom "T"() %q7 : !quantum.bit // l5 | ||
| %q10:2 = quantum.custom "CNOT"() %q9, %q8 : !quantum.bit, !quantum.bit | ||
| %q11 = quantum.custom "T"() %q10#1 {adjoint}: !quantum.bit // l6 | ||
| %q12:2 = quantum.custom "CNOT"() %q10#0, %q11 : !quantum.bit, !quantum.bit | ||
|
|
||
|
|
||
| %q13 = quantum.custom "T"() %q12#0 : !quantum.bit // on q2 | ||
|
|
||
| %reg2 = quantum.alloc( 2) : !quantum.reg | ||
| %p0 = quantum.extract %reg2[ 0] : !quantum.reg -> !quantum.bit | ||
| %p1 = quantum.extract %reg2[ 1] : !quantum.reg -> !quantum.bit | ||
|
|
||
| // %c = arith.constant 1.1212 : f64 | ||
| // %q_temp = quantum.custom "RZ"(%c) %q13 : !quantum.bit | ||
|
|
||
| %q14 = quantum.custom "T"() %q13 : !quantum.bit | ||
| %q15 = quantum.custom "T"() %q14 : !quantum.bit | ||
| %q16 = quantum.custom "T"() %q15 : !quantum.bit | ||
| // %q17 = quantum.custom "PauliY"() %q16 : !quantum.bit | ||
| // %q18 = quantum.custom "T"() %q17 : !quantum.bit | ||
| // %q19 = quantum.custom "T"() %q18 : !quantum.bit | ||
| // %q20 = quantum.custom "T"() %q19 : !quantum.bit | ||
|
|
||
| %p2 = quantum.custom "T"() %p0 : !quantum.bit | ||
| %p3 = quantum.custom "Hadamard"() %p1 : !quantum.bit | ||
| %p4:2 = quantum.custom "CNOT"() %p3, %p2 : !quantum.bit, !quantum.bit | ||
| %p5 = quantum.custom "T"() %p4#1 : !quantum.bit | ||
| %p6:2 = quantum.custom "CNOT"() %p4#0, %p5 : !quantum.bit, !quantum.bit | ||
| %p7 = quantum.custom "T"() %p6#1 : !quantum.bit | ||
|
|
||
| %q17 = quantum.custom "PauliY"() %q16 : !quantum.bit | ||
|
|
||
| %qb = quantum.alloc_qb : !quantum.bit | ||
| %h = quantum.custom "PauliX"() %qb : !quantum.bit | ||
|
|
||
| %p8:2 = quantum.custom "SWAP"() %p7, %h : !quantum.bit, !quantum.bit | ||
| %p9 = quantum.custom "S"() %p8#1 : !quantum.bit | ||
|
|
||
| // %tens01 = arith.constant dense<[false, true]> : tensor<2xi1> | ||
| // %q18:2 = quantum.set_basis_state(%tens01) %q12#1, %q17 : (tensor<2xi1>, !quantum.bit, !quantum.bit) -> (!quantum.bit, !quantum.bit) | ||
|
|
||
| // func.return %q12#1, %q12#0 : !quantum.bit, !quantum.bit | ||
| func.return %q12#1, %q12#0 : !quantum.bit, !quantum.bit | ||
| } | ||
|
|
||
| // func.func @test() { | ||
| // %reg = quantum.alloc( 2) : !quantum.reg | ||
| // %q0 = quantum.extract %reg[ 0] : !quantum.reg -> !quantum.bit | ||
| // %q1 = quantum.extract %reg[ 1] : !quantum.reg -> !quantum.bit | ||
|
|
||
| // %c0 = arith.constant 0 : index | ||
| // %c4 = arith.constant 4 : index | ||
| // %c1 = arith.constant 1 : index | ||
|
|
||
| // %c = arith.constant 1.1212 : f64 | ||
|
|
||
| // %q2:2 = quantum.custom "CNOT"() %q0, %q1 : !quantum.bit, !quantum.bit | ||
| // %q3 = quantum.custom "RZ"(%c) %q2#0 : !quantum.bit | ||
| // %q4 = quantum.custom "T"() %q2#1 : !quantum.bit | ||
| // %q5 = quantum.custom "Hadamard"() %q3 : !quantum.bit | ||
| // %q6 = quantum.custom "PauliX"() %q4 : !quantum.bit | ||
| // %q7 = quantum.custom "PauliY"() %q5 : !quantum.bit | ||
| // %q8 = quantum.custom "PauliZ"() %q6 : !quantum.bit | ||
|
|
||
|
|
||
| // // %r = scf.for %i = %c0 to %c4 step %c1 iter_args(%x = %q1) -> !quantum.bit { | ||
| // // %q1_1 = quantum.custom "PauliX"() %x : !quantum.bit | ||
| // // scf.yield %q1_1 : !quantum.bit | ||
| // // } | ||
|
|
||
|
|
||
| // func.return | ||
| // } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice to show how we use the pass. We normally show the usage code such that in test (e.g in |
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,61 @@ | ||
| import pennylane as qp | ||
|
|
||
|
|
||
| @qp.qjit(keep_intermediate=2, capture=False) | ||
| @qp.transform(pass_name="phase-folding") | ||
| @qp.qnode(device=qp.device("lightning.qubit", wires=2)) | ||
| def circuit_base(): | ||
| qp.CNOT([0, 1]) | ||
| qp.T(1) | ||
| qp.CNOT([0, 1]) | ||
| qp.CNOT([1, 0]) | ||
| qp.T(0) | ||
| qp.CNOT([1, 0]) | ||
| return qp.probs() | ||
|
|
||
|
|
||
| @qp.qjit(keep_intermediate=2, capture=False) | ||
| @qp.transform(pass_name="phase-folding") | ||
| @qp.qnode(device=qp.device("lightning.qubit", wires=2)) | ||
| def circuit_ex424(): | ||
| qp.T(0) | ||
| qp.T(1) | ||
| qp.CNOT([0, 1]) | ||
| qp.adjoint(qp.T(1)) | ||
| qp.CNOT([0, 1]) | ||
|
|
||
| qp.H(1) | ||
| qp.T(0) | ||
| qp.T(1) | ||
| qp.CNOT([1, 0]) | ||
| qp.adjoint(qp.T(0)) | ||
| qp.CNOT([1, 0]) | ||
|
|
||
|
|
||
| # @qp.qjit(keep_intermediate=2, capture=False) | ||
| # # @qp.transform(pass_name="phase-folding") | ||
| # @qp.qnode(device=qp.device("lightning.qubit", wires=2)) | ||
| # def circuit_ex425(): | ||
| # # qp.StatePrep([0, 1], wires=1) | ||
| # qp.BasisState(0, wires=1) # becomes dynamic, so not yet | ||
| # qp.T(1) | ||
| # qp.CNOT([0, 1]) | ||
| # qp.T(0) | ||
| # qp.T(1) | ||
|
|
||
|
|
||
| @qp.qjit(keep_intermediate=2, capture=False) | ||
| @qp.transform(pass_name="phase-folding") | ||
| @qp.qnode(device=qp.device("lightning.qubit", wires=1)) | ||
| def circuit_ex426(): | ||
| qp.T(0) | ||
| qp.PauliX(0) | ||
| qp.adjoint(qp.T(0)) | ||
| qp.PauliX(0) | ||
| return qp.probs() | ||
|
|
||
|
|
||
| circuit_base() | ||
| circuit_ex424() | ||
| # circuit_ex425() | ||
| circuit_ex426() | ||
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
87 changes: 87 additions & 0 deletions
87
mlir/lib/Quantum/Transforms/SymbolicAnalysis/AffineTransform.cpp
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,87 @@ | ||
| // Copyright 2026 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. | ||
|
|
||
| #include "AffineTransform.h" | ||
|
|
||
| /* | ||
| Constructors: | ||
| */ | ||
| AffineTransform::AffineTransform(size_t n) | ||
| { | ||
| exprMatrix.reserve(n); | ||
| for (size_t i = 0; i < n; i++) { | ||
| exprMatrix.push_back(Parity::eVec(n, i + 1)); | ||
| } | ||
| } | ||
|
|
||
| AffineTransform AffineTransform::identity(size_t n) { return AffineTransform(n); } | ||
|
|
||
| /* | ||
| Operators: | ||
| */ | ||
| llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const AffineTransform &trans) | ||
| { | ||
| for (auto it = trans.exprMatrix.begin(); it != trans.exprMatrix.end(); it++) { | ||
| os << *it << '\n'; | ||
| } | ||
| return os; | ||
| } | ||
|
|
||
| std::string AffineTransform::algebraicView(size_t qubitNum) const | ||
| { | ||
| std::string res = ""; | ||
| for (size_t i = 0; i < exprMatrix.size(); i++) { | ||
| res += | ||
| ("x'" + std::to_string(i + 1) + " = " + exprMatrix[i].algebraicView(qubitNum) + '\n'); | ||
| } | ||
| return res; | ||
| } | ||
|
|
||
| /* | ||
| Getters and Setters: | ||
| */ | ||
| void AffineTransform::setRow(size_t row, const Parity &parity) { getRowMutable(row) = parity; } | ||
|
|
||
| void AffineTransform::resetRow(size_t row) { getRowMutable(row).reset(); } | ||
|
|
||
| void AffineTransform::flipAffineValueAtRow(size_t row) { getRowMutable(row).flipAffineValue(); } | ||
|
|
||
| /* | ||
| Methods: | ||
| */ | ||
| void AffineTransform::addRowWithParity(size_t row, const Parity &parity) | ||
| { | ||
| getRowMutable(row) += parity; | ||
| } | ||
|
|
||
| void AffineTransform::addRows(size_t sourceRow, size_t targetRow) | ||
| { // E_i,j | ||
| getRowMutable(targetRow) += getRow(sourceRow); | ||
| } | ||
|
|
||
| void AffineTransform::swapRows(size_t row1, size_t row2) | ||
| { | ||
| std::swap(getRowMutable(row1), getRowMutable(row2)); | ||
| } | ||
|
|
||
| void AffineTransform::extendTo(size_t newRowNum, size_t auxVarNum) | ||
| { | ||
| if (newRowNum > getRowNum()) { | ||
| exprMatrix.reserve(newRowNum); | ||
| for (size_t i = getRowNum() + 1; i <= newRowNum; i++) { | ||
| exprMatrix.push_back(Parity::eVec(newRowNum + auxVarNum, i + auxVarNum)); | ||
| } | ||
| } // it might be more efficient to resize to newRowNum, and then just turn the eVec bits on for | ||
| // the new rows. | ||
| } |
76 changes: 76 additions & 0 deletions
76
mlir/lib/Quantum/Transforms/SymbolicAnalysis/AffineTransform.h
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,76 @@ | ||
| // Copyright 2026 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. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cassert> | ||
| #include <string_view> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include "llvm/Support/raw_ostream.h" | ||
|
|
||
| #include "Parity.h" | ||
|
|
||
| class AffineTransform { | ||
| public: | ||
| // Constructors | ||
| AffineTransform() = default; | ||
| AffineTransform(const Parity *rows, size_t n) : exprMatrix(rows, rows + n) {} | ||
|
|
||
| // Static Factories | ||
| static AffineTransform identity(size_t n); | ||
|
|
||
| // Operators | ||
| friend llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const AffineTransform &trans); | ||
| std::string algebraicView(size_t qubitNum) const; | ||
|
|
||
| // Getters | ||
| [[nodiscard]] size_t getRowNum() const; | ||
| [[nodiscard]] size_t getColNum(size_t row) const; | ||
| [[nodiscard]] const Parity &getRow(size_t row) const; | ||
| [[nodiscard]] Parity &getRowMutable(size_t row) const; | ||
|
|
||
| // Setters | ||
| void setRow(size_t row, const Parity &parity); | ||
| void resetRow(size_t row); | ||
| void flipAffineValueAtRow(size_t row); | ||
|
|
||
| // Methods | ||
| void extendTo(size_t newRowNum, size_t auxVarNum); | ||
| void swapRows(size_t row1, size_t row2); | ||
| void addRows(size_t sourceRow, size_t targetRow); | ||
| void addRowWithParity(size_t row, const Parity &parity); | ||
|
|
||
| private: | ||
| std::vector<Parity> exprMatrix; // n x (n + m + 1) binary matrix | ||
|
|
||
| // Helper Methods | ||
| explicit AffineTransform(size_t n); // Identity matrix by default | ||
| }; | ||
|
|
||
| inline size_t AffineTransform::getRowNum() const { return exprMatrix.size(); } | ||
|
|
||
| inline size_t AffineTransform::getColNum(size_t row) const { return getRow(row).getVarNum(); } | ||
|
|
||
| inline const Parity &AffineTransform::getRow(size_t row) const | ||
| { | ||
| assert(row >= 0 && row < getRowNum()); | ||
| return exprMatrix[row]; | ||
| } | ||
|
|
||
| inline Parity &AffineTransform::getRowMutable(size_t row) const | ||
| { | ||
| return const_cast<Parity &>(static_cast<const AffineTransform &>(*this).getRow(row)); | ||
| } |
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.
Great to have a test file! Could you add this folder in
mlir/test/Quantum