-
Notifications
You must be signed in to change notification settings - Fork 44
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
Restrict controlled from non-thru registers #1305
Open
mpharrigan
wants to merge
13
commits into
quantumlib:main
Choose a base branch
from
mpharrigan:2024-08/non-thru-ctrl
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.
+27
−80
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
516547e
Restrict controlled from non-thru registers
mpharrigan c5f301b
lint
mpharrigan 2e2e6e5
Merge branch 'main' into 2024-08/non-thru-ctrl
mpharrigan 05d6c5e
format
mpharrigan 816b4f4
Merge remote-tracking branch 'origin/main' into 2024-08/non-thru-ctrl
mpharrigan 8b08a5e
Merge branch 'main' into 2024-08/non-thru-ctrl
mpharrigan ecf2b25
Merge branch 'main' into 2024-08/non-thru-ctrl
mpharrigan 32b10fa
Merge branch 'main' into 2024-08/non-thru-ctrl
mpharrigan bca3a11
Move error condition to usages
mpharrigan bcda6f3
Merge branch 'main' into 2024-08/non-thru-ctrl
mpharrigan 5f5defc
Merge branch 'main' into 2024-08/non-thru-ctrl
mpharrigan 33c8d54
Merge branch 'main' into 2024-08/non-thru-ctrl
mpharrigan 7c574e6
Merge branch 'main' into 2024-08/non-thru-ctrl
mpharrigan 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 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 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 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 |
---|---|---|
|
@@ -11,51 +11,32 @@ | |
# 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. | ||
from typing import Dict, List, Tuple, TYPE_CHECKING | ||
from typing import List, TYPE_CHECKING | ||
|
||
import attrs | ||
import cirq | ||
import numpy as np | ||
import pytest | ||
|
||
import qualtran.testing as qlt_testing | ||
from qualtran import ( | ||
Bloq, | ||
BloqBuilder, | ||
CompositeBloq, | ||
Controlled, | ||
CtrlSpec, | ||
QBit, | ||
QInt, | ||
QUInt, | ||
Register, | ||
Side, | ||
Signature, | ||
) | ||
from qualtran import Bloq, CompositeBloq, Controlled, CtrlSpec, QBit, QInt, QUInt | ||
from qualtran._infra.gate_with_registers import get_named_qubits, merge_qubits | ||
from qualtran.bloqs.basic_gates import ( | ||
CSwap, | ||
GlobalPhase, | ||
IntEffect, | ||
IntState, | ||
OneState, | ||
Swap, | ||
TwoBitCSwap, | ||
XGate, | ||
XPowGate, | ||
YGate, | ||
ZeroState, | ||
ZGate, | ||
) | ||
from qualtran.bloqs.for_testing import TestAtom, TestParallelCombo, TestSerialCombo | ||
from qualtran.bloqs.mcmt import And | ||
from qualtran.cirq_interop.testing import GateHelper | ||
from qualtran.drawing import get_musical_score_data | ||
from qualtran.drawing.musical_score import Circle, SoqData, TextBox | ||
from qualtran.simulation.tensor import cbloq_to_quimb, get_right_and_left_inds | ||
|
||
if TYPE_CHECKING: | ||
from qualtran import SoquetT | ||
pass | ||
|
||
|
||
def test_ctrl_spec(): | ||
|
@@ -385,62 +366,6 @@ def test_controlled_global_phase_tensor(): | |
np.testing.assert_allclose(bloq.tensor_contract(), should_be) | ||
|
||
|
||
@attrs.frozen | ||
class TestCtrlStatePrepAnd(Bloq): | ||
"""Decomposes into a Controlled-AND gate + int effects & targets where ctrl is active. | ||
|
||
Tensor contraction should give the output state vector corresponding to applying an | ||
`And(and_ctrl)`; assuming all the control bits are active. | ||
""" | ||
|
||
ctrl_spec: CtrlSpec | ||
and_ctrl: Tuple[int, int] | ||
Comment on lines
-388
to
-397
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 we add a private flag to the |
||
|
||
@property | ||
def signature(self) -> 'Signature': | ||
return Signature([Register('x', QBit(), shape=(3,), side=Side.RIGHT)]) | ||
|
||
def build_composite_bloq(self, bb: 'BloqBuilder') -> Dict[str, 'SoquetT']: | ||
one_or_zero = [ZeroState(), OneState()] | ||
ctrl_bloq = Controlled(And(*self.and_ctrl), ctrl_spec=self.ctrl_spec) | ||
|
||
ctrl_soqs = {} | ||
for reg, cvs in zip(ctrl_bloq.ctrl_regs, self.ctrl_spec.cvs): | ||
soqs = np.empty(shape=reg.shape, dtype=object) | ||
for idx in reg.all_idxs(): | ||
soqs[idx] = bb.add(IntState(val=cvs[idx], bitsize=reg.dtype.num_qubits)) | ||
ctrl_soqs[reg.name] = soqs | ||
|
||
and_ctrl = [bb.add(one_or_zero[cv]) for cv in self.and_ctrl] | ||
|
||
ctrl_soqs = bb.add_d(ctrl_bloq, **ctrl_soqs, ctrl=and_ctrl) | ||
out_soqs = np.asarray([*ctrl_soqs.pop('ctrl'), ctrl_soqs.pop('target')]) # type: ignore[misc] | ||
|
||
for reg, cvs in zip(ctrl_bloq.ctrl_regs, self.ctrl_spec.cvs): | ||
for idx in reg.all_idxs(): | ||
ctrl_soq = np.asarray(ctrl_soqs[reg.name])[idx] | ||
bb.add(IntEffect(val=cvs[idx], bitsize=reg.dtype.num_qubits), val=ctrl_soq) | ||
return {'x': out_soqs} | ||
|
||
|
||
def _verify_ctrl_tensor_for_and(ctrl_spec: CtrlSpec, and_ctrl: Tuple[int, int]): | ||
bloq = TestCtrlStatePrepAnd(ctrl_spec, and_ctrl) | ||
bloq_tensor = bloq.tensor_contract() | ||
cirq_state_vector = GateHelper(And(*and_ctrl)).circuit.final_state_vector( | ||
initial_state=and_ctrl + (0,) | ||
) | ||
np.testing.assert_allclose(bloq_tensor, cirq_state_vector, atol=1e-8) | ||
|
||
|
||
@pytest.mark.parametrize('ctrl_spec', interesting_ctrl_specs) | ||
def test_controlled_tensor_for_and_bloq(ctrl_spec: CtrlSpec): | ||
# Test AND gate with one-sided signature (aka controlled state preparation). | ||
_verify_ctrl_tensor_for_and(ctrl_spec, (1, 1)) | ||
_verify_ctrl_tensor_for_and(ctrl_spec, (1, 0)) | ||
_verify_ctrl_tensor_for_and(ctrl_spec, (0, 1)) | ||
_verify_ctrl_tensor_for_and(ctrl_spec, (0, 0)) | ||
|
||
|
||
def test_controlled_diagrams(): | ||
ctrl_gate = XPowGate(0.25).controlled() | ||
cirq.testing.assert_has_diagram( | ||
|
This file contains 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.
See other comment -- it'll be great if we can guard this with a flag set to TRUE by default. When we relax the condition, we can get rid of the flag and also keep the tests around.