-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
align_left
/align_right
transformers to replace AlignLeft
/`A…
…lignRight` (#4891) * Add / to replace / * Make context an optional parameter with default value None
- Loading branch information
1 parent
fdf85f0
commit 4d445c4
Showing
9 changed files
with
193 additions
and
13 deletions.
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
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
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Copyright 2022 The Cirq Developers | ||
# | ||
# 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 | ||
# | ||
# https://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. | ||
|
||
"""Transformer passes which align operations to the left or right of the circuit.""" | ||
|
||
from typing import Optional, TYPE_CHECKING | ||
from cirq import circuits, ops | ||
from cirq.transformers import transformer_api | ||
|
||
if TYPE_CHECKING: | ||
import cirq | ||
|
||
|
||
@transformer_api.transformer | ||
def align_left( | ||
circuit: 'cirq.AbstractCircuit', *, context: Optional['cirq.TransformerContext'] = None | ||
) -> 'cirq.Circuit': | ||
"""Align gates to the left of the circuit. | ||
Note that tagged operations with tag in `context.ignore_tags` will continue to stay in their | ||
original position and will not be aligned. | ||
Args: | ||
circuit: Input circuit to transform. | ||
context: `cirq.TransformerContext` storing common configurable options for transformers. | ||
Returns: | ||
Copy of the transformed input circuit. | ||
""" | ||
if context is None: | ||
context = transformer_api.TransformerContext() | ||
|
||
ret = circuits.Circuit() | ||
for i, moment in enumerate(circuit): | ||
for op in moment: | ||
if isinstance(op, ops.TaggedOperation) and set(op.tags).intersection( | ||
context.ignore_tags | ||
): | ||
ret.append([ops.Moment()] * (i + 1 - len(ret))) | ||
ret[i] = ret[i].with_operation(op) | ||
else: | ||
ret.append(op) | ||
return ret | ||
|
||
|
||
@transformer_api.transformer | ||
def align_right( | ||
circuit: 'cirq.AbstractCircuit', *, context: Optional['cirq.TransformerContext'] = None | ||
) -> 'cirq.Circuit': | ||
"""Align gates to the right of the circuit. | ||
Note that tagged operations with tag in `context.ignore_tags` will continue to stay in their | ||
original position and will not be aligned. | ||
Args: | ||
circuit: Input circuit to transform. | ||
context: `cirq.TransformerContext` storing common configurable options for transformers. | ||
Returns: | ||
Copy of the transformed input circuit. | ||
""" | ||
return align_left(circuit[::-1], context=context)[::-1] |
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# Copyright 2022 The Cirq Developers | ||
# | ||
# 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 | ||
# | ||
# https://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. | ||
|
||
import cirq | ||
|
||
|
||
def test_align_basic_no_context(): | ||
q1 = cirq.NamedQubit('q1') | ||
q2 = cirq.NamedQubit('q2') | ||
c = cirq.Circuit( | ||
[ | ||
cirq.Moment([cirq.X(q1)]), | ||
cirq.Moment([cirq.Y(q1), cirq.X(q2)]), | ||
cirq.Moment([cirq.X(q1)]), | ||
] | ||
) | ||
cirq.testing.assert_same_circuits( | ||
cirq.align_left(c), | ||
cirq.Circuit( | ||
cirq.Moment([cirq.X(q1), cirq.X(q2)]), | ||
cirq.Moment([cirq.Y(q1)]), | ||
cirq.Moment([cirq.X(q1)]), | ||
), | ||
) | ||
cirq.testing.assert_same_circuits( | ||
cirq.align_right(c), | ||
cirq.Circuit( | ||
cirq.Moment([cirq.X(q1)]), | ||
cirq.Moment([cirq.Y(q1)]), | ||
cirq.Moment([cirq.X(q1), cirq.X(q2)]), | ||
), | ||
) | ||
|
||
|
||
def test_align_left_no_compile_context(): | ||
q1 = cirq.NamedQubit('q1') | ||
q2 = cirq.NamedQubit('q2') | ||
cirq.testing.assert_same_circuits( | ||
cirq.align_left( | ||
cirq.Circuit( | ||
[ | ||
cirq.Moment([cirq.X(q1)]), | ||
cirq.Moment([cirq.Y(q1), cirq.X(q2)]), | ||
cirq.Moment([cirq.X(q1), cirq.Y(q2).with_tags("nocompile")]), | ||
cirq.Moment([cirq.Y(q1)]), | ||
cirq.measure(*[q1, q2], key='a'), | ||
] | ||
), | ||
context=cirq.TransformerContext(ignore_tags=["nocompile"]), | ||
), | ||
cirq.Circuit( | ||
[ | ||
cirq.Moment([cirq.X(q1), cirq.X(q2)]), | ||
cirq.Moment([cirq.Y(q1)]), | ||
cirq.Moment([cirq.X(q1), cirq.Y(q2).with_tags("nocompile")]), | ||
cirq.Moment([cirq.Y(q1)]), | ||
cirq.measure(*[q1, q2], key='a'), | ||
] | ||
), | ||
) | ||
|
||
|
||
def test_align_right_no_compile_context(): | ||
q1 = cirq.NamedQubit('q1') | ||
q2 = cirq.NamedQubit('q2') | ||
cirq.testing.assert_same_circuits( | ||
cirq.align_right( | ||
cirq.Circuit( | ||
[ | ||
cirq.Moment([cirq.X(q1)]), | ||
cirq.Moment([cirq.Y(q1), cirq.X(q2).with_tags("nocompile")]), | ||
cirq.Moment([cirq.X(q1), cirq.Y(q2)]), | ||
cirq.Moment([cirq.Y(q1)]), | ||
cirq.measure(*[q1, q2], key='a'), | ||
] | ||
), | ||
context=cirq.TransformerContext(ignore_tags=["nocompile"]), | ||
), | ||
cirq.Circuit( | ||
[ | ||
cirq.Moment([cirq.X(q1)]), | ||
cirq.Moment([cirq.Y(q1), cirq.X(q2).with_tags("nocompile")]), | ||
cirq.Moment([cirq.X(q1)]), | ||
cirq.Moment([cirq.Y(q1), cirq.Y(q2)]), | ||
cirq.measure(*[q1, q2], key='a'), | ||
] | ||
), | ||
) |
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