-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add cirq.google.optimized_for_xmon() #647
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
Merged
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
0f4a4b5
[WIP] optimization playground
Strilanc f1073c0
Implement EjectFullW optimizer
Strilanc de00b46
doc
Strilanc cbdb073
Merge branch 'master' of github.com:quantumlib/cirq into optimization…
Strilanc 01c98b2
r
Strilanc 977b02b
Merge branch 'diff3' into optimization_cleanup
Strilanc ee07218
Define cirq.google.optimize_for_xmon
Strilanc fc727bc
Merge branch 'master' of github.com:quantumlib/cirq into optimization…
Strilanc 0d0f5ae
Fix sign error in EjectFullW and silent merge error in eject_z_test.py
Strilanc c4dce77
Fix sign error in EjectFullW and silent merge error in eject_z_test.py
Strilanc 4a46caf
condition
Strilanc 36ed1e0
cover
Strilanc 18c1f77
Merge branch 'master' of github.com:quantumlib/cirq into fix-w
Strilanc b25a890
Merge branch 'fix-w' into optimization_cleanup
Strilanc a6b0a74
fix
Strilanc 08b7416
Merge branch 'fix-w' into optimization_cleanup
Strilanc c0bfb40
test_ccz
Strilanc 72d9aee
Refactor drop_negligible into an OptimizationPass using batch_remove
Strilanc 7cd58e6
Fix merge_interactions failing to convert into xmon gates
Strilanc e9c5acb
Merge branch 'master' of github.com:quantumlib/cirq into optimize-1
Strilanc 185b517
fix tests
Strilanc 07c1fde
Merge branch 'optimize-0' into optimization_cleanup
Strilanc 094b3cb
Merge branch 'optimize-1' into optimization_cleanup
Strilanc 3fca893
cut bool
Strilanc 86dc1d0
naming things
Strilanc 091aebe
Merge branch 'optimize-1' into optimization_cleanup
Strilanc 27cadfb
Cleanup imports in merge_rotations_test
Strilanc 6a64583
Merge branch 'master' of github.com:quantumlib/cirq into optimization…
Strilanc 1febf3d
Add cirq.testing.assert_circuits_with_terminal_measurements_are_equiv…
Strilanc 9de3e03
lint
Strilanc ff8a4fc
compare_unitaries
Strilanc dc26303
dindent
Strilanc c511210
Merge branch 'optimize-3' into optimization_cleanup
Strilanc d8aa6c5
Merge branch 'optimize-2' into optimization_cleanup
Strilanc 75b9501
fix
Strilanc 2fb7103
Fix
Strilanc 37ef6ff
lint
Strilanc 9007754
Merge branch 'master' of github.com:quantumlib/cirq into optimization…
Strilanc 296bdea
Merge branch 'master' of github.com:quantumlib/cirq into optimization…
Strilanc 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # Copyright 2018 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. | ||
|
|
||
| """A combination of several optimizations targeting XmonDevice.""" | ||
|
|
||
| from cirq import circuits | ||
| from cirq.google import ( | ||
| convert_to_xmon_gates, | ||
| merge_rotations, | ||
| merge_interactions, | ||
| eject_full_w, | ||
| eject_z, | ||
| ) | ||
|
|
||
| _TOLERANCE = 1e-5 | ||
|
|
||
| _OPTIMIZERS = [ | ||
| convert_to_xmon_gates.ConvertToXmonGates(), | ||
|
|
||
| merge_interactions.MergeInteractions(tolerance=_TOLERANCE), | ||
| merge_rotations.MergeRotations(tolerance=_TOLERANCE), | ||
| eject_full_w.EjectFullW(tolerance=_TOLERANCE), | ||
| eject_z.EjectZ(tolerance=_TOLERANCE), | ||
| circuits.DropNegligible(tolerance=_TOLERANCE), | ||
| ] | ||
|
|
||
|
|
||
| def optimized_for_xmon(circuit: circuits.Circuit) -> circuits.Circuit: | ||
| """Optimizes a circuit with XmonDevice in mind. | ||
|
|
||
| Starts by converting the circuit's operations to the xmon gate set, then | ||
| begins merging interactions and rotations, ejecting pi-rotations and phasing | ||
| operations, dropping unnecessary operations, and pushing operations earlier. | ||
|
|
||
| Args: | ||
| circuit: The circuit to optimize. | ||
|
|
||
| Returns: | ||
| The optimized circuit. | ||
| """ | ||
| copy = circuit.copy() | ||
| for optimizer in _OPTIMIZERS: | ||
| optimizer.optimize_circuit(copy) | ||
|
|
||
| return circuits.Circuit().from_ops( | ||
| copy.all_operations(), | ||
| strategy=circuits.InsertStrategy.EARLIEST) |
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,58 @@ | ||
| # Copyright 2018 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 pytest | ||
|
|
||
| import cirq | ||
| import cirq.google as cg | ||
|
|
||
| from cirq.testing import ( | ||
| assert_circuits_with_terminal_measurements_are_equivalent, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('n,d', [ | ||
| (3, 2), | ||
| (4, 3), | ||
| (4, 4), | ||
| (5, 4), | ||
| (22, 4), | ||
| ]) | ||
| def test_swap_field(n: int, d: int): | ||
| before = cirq.Circuit.from_ops( | ||
| cirq.ISWAP(cirq.LineQubit(j), cirq.LineQubit(j + 1)) | ||
| for i in range(d) | ||
| for j in range(i % 2, n - 1, 2) | ||
| ) | ||
| before.append(cirq.measure(*before.all_qubits())) | ||
|
|
||
| after = cg.optimized_for_xmon(before) | ||
|
|
||
| assert len(after) == d*4 + 2 | ||
| if n <= 5: | ||
| assert_circuits_with_terminal_measurements_are_equivalent( | ||
| before, after, atol=1e-4) | ||
|
|
||
|
|
||
| def test_ccz(): | ||
| before = cirq.Circuit.from_ops( | ||
| cirq.CCZ(cirq.GridQubit(5, 5), | ||
| cirq.GridQubit(5, 6), | ||
| cirq.GridQubit(5, 7))) | ||
|
|
||
| after = cg.optimized_for_xmon(before) | ||
|
|
||
| assert len(after) <= 22 | ||
|
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. Is the optimized length currently 22?
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. Yes. But I'm happy if it goes down, too. |
||
| assert_circuits_with_terminal_measurements_are_equivalent( | ||
| before, after, atol=1e-4) | ||
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.
No terminal measurements? Would it be useful to have separate tests for both cases (with measurements and without)?
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.
Intentional.