-
Notifications
You must be signed in to change notification settings - Fork 96
Add a phased classical action for SelectedMajoranaFermion #1778
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
maxglick
wants to merge
13
commits into
quantumlib:main
Choose a base branch
from
maxglick:selected-majorana-fermion-1699
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
13 commits
Select commit
Hold shift + click to select a range
8a546b1
Add a common base class for MultiControlPauli and its specializations.
maxglick 7d9760f
Merge branch 'main' into maxglick-patch
maxglick a80e56b
Correct the superclass of MultiControlZ.
maxglick 17b197c
Updates based on PR comments.
maxglick 6ccedf1
Merge branch 'main' into maxglick-patch
mpharrigan e78143b
Make target_bloq a cached_property.
maxglick 0ab0153
Merge remote-tracking branch 'upstream/main'
maxglick 68e1c34
Add a classical action for SelectedMajoranaFermion
maxglick 8ddf870
Merge remote-tracking branch 'upstream/main'
maxglick 60e02f4
Merge branch 'main' into selected-majorana-fermion-1699
maxglick 9305db5
Fix format / typing.
maxglick b133c37
One more reformat.
maxglick a0ba13c
Also handle Z as the target gate.
maxglick 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 |
|---|---|---|
|
|
@@ -13,7 +13,7 @@ | |
| # limitations under the License. | ||
|
|
||
| from functools import cached_property | ||
| from typing import Iterator, Sequence, Tuple, Union | ||
| from typing import Dict, Iterator, Sequence, Tuple, Union | ||
|
|
||
| import attrs | ||
| import cirq | ||
|
|
@@ -25,6 +25,7 @@ | |
| from qualtran._infra.data_types import BQUInt | ||
| from qualtran._infra.gate_with_registers import total_bits | ||
| from qualtran.bloqs.multiplexers.unary_iteration_bloq import UnaryIterationGate | ||
| from qualtran.simulation.classical_sim import ClassicalValT | ||
|
|
||
|
|
||
| @attrs.frozen | ||
|
|
@@ -137,5 +138,43 @@ def nth_operation( # type: ignore[override] | |
| yield self.target_gate(target[target_idx]).controlled_by(control) | ||
| yield cirq.CZ(*accumulator, target[target_idx]) | ||
|
|
||
| def on_classical_vals(self, **vals) -> Dict[str, 'ClassicalValT']: | ||
| if self.target_gate != cirq.X and self.target_gate != cirq.Z: | ||
| return NotImplemented | ||
| if len(self.control_registers) > 1 or len(self.selection_registers) > 1: | ||
| return NotImplemented | ||
| control_name = self.control_registers[0].name | ||
| control = vals[control_name] | ||
| selection_name = self.selection_registers[0].name | ||
| selection = vals[selection_name] | ||
| target = vals['target'] | ||
|
|
||
| # When target_gate == cirq.X, the action is (modulo phase) a single bitflip. | ||
| if control and self.target_gate == cirq.X: | ||
| max_selection = self.selection_registers[0].dtype.iteration_length_or_zero() - 1 | ||
| target = (2 ** (max_selection - selection)) ^ target | ||
|
Comment on lines
+154
to
+155
Collaborator
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. can you add a comment describing how this logic works
Contributor
Author
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. Done. |
||
| # When target_gate == cirq.Z, the action is only in the phase. | ||
|
|
||
| return {control_name: control, selection_name: selection, 'target': target} | ||
|
|
||
| def basis_state_phase(self, **vals) -> Union[complex, None]: | ||
| if self.target_gate != cirq.X and self.target_gate != cirq.Z: | ||
| return None | ||
| if len(self.control_registers) > 1 or len(self.selection_registers) > 1: | ||
| return None | ||
| control_name = self.control_registers[0].name | ||
| control = vals[control_name] | ||
| selection_name = self.selection_registers[0].name | ||
| selection = vals[selection_name] | ||
| target = vals['target'] | ||
| if control: | ||
| max_selection = self.selection_registers[0].dtype.iteration_length_or_zero() - 1 | ||
| if self.target_gate == cirq.X: | ||
| num_phases = (target >> (max_selection - selection + 1)).bit_count() | ||
| else: | ||
| num_phases = (target >> (max_selection - selection)).bit_count() | ||
| return 1 if (num_phases % 2) == 0 else -1 | ||
| return 1 | ||
|
|
||
| def __str__(self): | ||
| return f'SelectedMajoranaFermion({self.target_gate})' | ||
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.
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.
is this restriction necessary?
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.
I'm not sure - it is hard for me to understand what this gate does in the general case. Is my understanding in #1699 (comment) correct?