-
Notifications
You must be signed in to change notification settings - Fork 83
Support decomposition to/from Adjoint Ops to the GraphSolver #3001
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
maliasadi
wants to merge
10
commits into
main
Choose a base branch
from
graph-solver/adjoint-op
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
10 commits
Select commit
Hold shift + click to select a range
3ca8f0d
Refactor and tidy up the Adjoint op support in the graph solver (git:…
maliasadi 437dd22
Add the C++ test file
maliasadi b9ebe26
Merge branch 'main' into graph-solver/adjoint-op
maliasadi 2521961
Update format
maliasadi c0f3ca6
Update tests
maliasadi 461db1e
Update
maliasadi 4cb1294
Merge branch 'main' into graph-solver/adjoint-op
maliasadi fe6c11f
Merge with main
maliasadi bd95029
Update
maliasadi 9a5f1e4
Fix the conflict in changelog
maliasadi 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 |
|---|---|---|
|
|
@@ -140,8 +140,9 @@ struct RuleTerm { | |
| * - Default: The default rule for decomposing an operator as defined in the decomposition graph. | ||
| * - Fixed: A fixed rule that cannot be changed or overridden by the solver. | ||
| * - Alternative: An alternative rule that can be used in place of the default rule. | ||
| * - AdjointGenerated: A rule synthesized by adjointing a base decomposition rule. | ||
| */ | ||
| enum class RuleOrigin : uint8_t { Default = 0, Fixed = 1, Alternative = 2 }; | ||
| enum class RuleOrigin : uint8_t { Default = 0, Fixed = 1, Alternative = 2, AdjointGenerated = 3 }; | ||
|
|
||
| /** | ||
| * @brief This represents the decomposition rules in the graph decomposition problem. | ||
|
|
@@ -185,6 +186,38 @@ using FixedDecomps = std::unordered_map<OperatorNode, RuleNode, OperatorNodeHash | |
| */ | ||
| using AltDecomps = std::unordered_map<OperatorNode, std::vector<RuleNode>, OperatorNodeHash>; | ||
|
|
||
| /** | ||
| * @brief This returns a copy of the given operator with its adjoint flag. | ||
| * | ||
| * Note taht because Adjoint is represented as a boolean flag in the IR, | ||
| * applying it twice should cancel out (Adjoint(Adjoint(op)) == op). | ||
| */ | ||
| inline OperatorNode makeAdjoint(OperatorNode op) | ||
| { | ||
| op.adjoint = !op.adjoint; | ||
| return op; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Constructs the Adjoint decomposition of a base rule. | ||
| * | ||
| * Given a rule `output -> {inputs}`, produces `Adjoint(output) -> {Adjoint(input), ...}` | ||
| * with the same multiplicities: the adjoint of a decomposition is obtained by adjointing | ||
| * every produced gate (and reversing their order, which does not affect resource/cost counting). | ||
| */ | ||
| inline RuleNode makeAdjointRule(const RuleNode &base) | ||
| { | ||
| RuleNode adj; | ||
| adj.name = base.name + "_adjoint"; | ||
| adj.output = makeAdjoint(base.output); | ||
|
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. Do we have to worry about this in-place mutating base.output? |
||
| adj.origin = RuleOrigin::AdjointGenerated; | ||
| adj.inputs.reserve(base.inputs.size()); | ||
| for (const auto &term : base.inputs) { | ||
| adj.inputs.push_back({makeAdjoint(term.op), term.multiplicity}); | ||
| } | ||
| return adj; | ||
| } | ||
|
|
||
| /** | ||
| * @brief This represents the chosen decomposition rule for an operator in | ||
| * the solution of the graph decomposition problem. | ||
|
|
@@ -196,6 +229,9 @@ struct ChosenDecompRule { | |
| std::vector<RuleTerm> inputs; | ||
| double totalCost{0.0}; | ||
| std::unordered_map<OperatorNode, std::size_t, OperatorNodeHash> basisCounts; | ||
|
|
||
| // TODO: revisit this after testing.. | ||
| RuleOrigin origin{RuleOrigin::Default}; | ||
| }; | ||
|
|
||
| /** | ||
|
|
||
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.
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.
Should we differentiate between adjoint genereated from default, adjoint generated from fixed and adjoint generated from alternative?