-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Observable Measurement - 2 - Grouping #3372
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
CirqBot
merged 8 commits into
quantumlib:master
from
mpharrigan:2020-10-operator-estimation-2-grouping
Oct 21, 2020
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d89bc46
Observable Measurement - Grouping
mpharrigan 5bb8494
Update cirq/work/observable_grouping.py
mpharrigan 45ef8ad
Update cirq/work/observable_grouping.py
mpharrigan 42cc47c
docstring nit
mpharrigan f46aaff
Merge remote-tracking branch 'mine/2020-10-operator-estimation-2-grou…
mpharrigan 863ce4c
tests
mpharrigan e447c95
fight with the ci as always
mpharrigan 24c8890
Merge remote-tracking branch 'origin/master' into 2020-10-operator-es…
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 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 |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| # Copyright 2020 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 | ||
| # | ||
| # http://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. | ||
|
|
||
| from typing import Iterable, Dict, List, TYPE_CHECKING, cast | ||
|
|
||
| from cirq import ops, value | ||
| from cirq.work.observable_settings import (InitObsSetting, _max_weight_state, | ||
| _max_weight_observable) | ||
|
|
||
| if TYPE_CHECKING: | ||
| pass | ||
|
|
||
|
|
||
| def group_settings_greedy(settings: Iterable[InitObsSetting]) \ | ||
| -> Dict[InitObsSetting, List[InitObsSetting]]: | ||
| """Greedily group settings which can be simultaneously measured. | ||
|
|
||
| We construct a dictionary keyed by `max_setting` (see docstrings | ||
| for `_max_weight_state` and `_max_weight_observable`) where the value | ||
| is a list of settings compatible with `max_setting`. For each new setting, | ||
| we try to find an existing group to add it and update `max_setting` for | ||
| that group if necessary. Otherwise, we make a new group. | ||
|
|
||
| In practice, this greedy algorithm performs comparably to something | ||
| more complicated by solving the clique cover problem on a graph | ||
| of simultaneously-measurable settings. | ||
|
|
||
| Args: | ||
| settings: The settings to group. | ||
|
|
||
| Returns: | ||
| A dictionary keyed by `max_setting` which need not exist in the | ||
| input list of settings. Each dictionary value is a list of | ||
| settings compatible with `max_setting`. | ||
| """ | ||
| grouped_settings = {} # type: Dict[InitObsSetting, List[InitObsSetting]] | ||
| for setting in settings: | ||
| for max_setting, simul_settings in grouped_settings.items(): | ||
| trial_grouped_settings = simul_settings + [setting] | ||
| new_max_weight_state = _max_weight_state( | ||
| stg.init_state for stg in trial_grouped_settings) | ||
| new_max_weight_obs = _max_weight_observable( | ||
| stg.observable for stg in trial_grouped_settings) | ||
| compatible_init_state = new_max_weight_state is not None | ||
| compatible_observable = new_max_weight_obs is not None | ||
| can_be_inserted = (compatible_init_state and compatible_observable) | ||
| if can_be_inserted: | ||
| new_max_weight_state = cast(value.ProductState, | ||
| new_max_weight_state) | ||
| new_max_weight_obs = cast(ops.PauliString, new_max_weight_obs) | ||
| del grouped_settings[max_setting] | ||
| new_max_setting = InitObsSetting(new_max_weight_state, | ||
| new_max_weight_obs) | ||
| grouped_settings[new_max_setting] = trial_grouped_settings | ||
| break | ||
|
|
||
| else: | ||
| # made it through entire dict without finding a compatible group, | ||
| # thus a new group needs to be created | ||
| # Strip coefficients before using as key | ||
| new_max_weight_obs = setting.observable.with_coefficient(1.0) | ||
| new_max_setting = InitObsSetting(setting.init_state, | ||
| new_max_weight_obs) | ||
| grouped_settings[new_max_setting] = [setting] | ||
|
|
||
| return grouped_settings |
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,154 @@ | ||
| # Copyright 2020 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 | ||
| # | ||
| # http://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_group_settings_greedy_one_group(): | ||
| qubits = cirq.LineQubit.range(2) | ||
| q0, q1 = qubits | ||
| terms = [ | ||
| cirq.X(q0), | ||
| cirq.Y(q1), | ||
| ] | ||
| settings = list(cirq.work.observables_to_settings(terms, qubits)) | ||
| grouped_settings = cirq.work.group_settings_greedy(settings) | ||
| assert len(grouped_settings) == 1 | ||
|
|
||
| group_max_obs_should_be = [ | ||
| cirq.X(q0) * cirq.Y(q1), | ||
| ] | ||
| group_max_settings_should_be = list( | ||
| cirq.work.observables_to_settings(group_max_obs_should_be, qubits)) | ||
| assert set(grouped_settings.keys()) == set(group_max_settings_should_be) | ||
|
|
||
| the_group = grouped_settings[group_max_settings_should_be[0]] | ||
| assert set(the_group) == set(settings) | ||
|
|
||
|
|
||
| def test_group_settings_greedy_two_groups(): | ||
| qubits = cirq.LineQubit.range(2) | ||
| q0, q1 = qubits | ||
| terms = [ | ||
| cirq.X(q0) * cirq.X(q1), | ||
| cirq.Y(q0) * cirq.Y(q1), | ||
| ] | ||
| settings = list(cirq.work.observables_to_settings(terms, qubits)) | ||
| grouped_settings = cirq.work.group_settings_greedy(settings) | ||
| assert len(grouped_settings) == 2 | ||
|
|
||
| group_max_obs_should_be = terms.copy() | ||
| group_max_settings_should_be = list( | ||
| cirq.work.observables_to_settings(group_max_obs_should_be, qubits)) | ||
| assert set(grouped_settings.keys()) == set(group_max_settings_should_be) | ||
|
|
||
|
|
||
| def test_group_settings_greedy_single_item(): | ||
| qubits = cirq.LineQubit.range(2) | ||
| q0, q1 = qubits | ||
| term = cirq.X(q0) * cirq.X(q1) | ||
|
|
||
| settings = list(cirq.work.observables_to_settings([term], qubits)) | ||
| grouped_settings = cirq.work.group_settings_greedy(settings) | ||
| assert len(grouped_settings) == 1 | ||
| assert list(grouped_settings.keys())[0] == settings[0] | ||
| assert list(grouped_settings.values())[0][0] == settings[0] | ||
|
|
||
|
|
||
| def test_group_settings_greedy_empty(): | ||
| assert cirq.work.group_settings_greedy([]) == dict() | ||
|
|
||
|
|
||
| def test_group_settings_greedy_init_state_compat(): | ||
| q0, q1 = cirq.LineQubit.range(2) | ||
| settings = [ | ||
| cirq.work.InitObsSetting(init_state=cirq.KET_PLUS(q0) * | ||
| cirq.KET_ZERO(q1), | ||
| observable=cirq.X(q0)), | ||
| cirq.work.InitObsSetting(init_state=cirq.KET_PLUS(q0) * | ||
| cirq.KET_ZERO(q1), | ||
| observable=cirq.Z(q1)), | ||
| ] | ||
| grouped_settings = cirq.work.group_settings_greedy(settings) | ||
| assert len(grouped_settings) == 1 | ||
|
|
||
|
|
||
| def test_group_settings_greedy_init_state_compat_sparse(): | ||
| q0, q1 = cirq.LineQubit.range(2) | ||
| settings = [ | ||
| cirq.work.InitObsSetting(init_state=cirq.KET_PLUS(q0), | ||
| observable=cirq.X(q0)), | ||
| cirq.work.InitObsSetting(init_state=cirq.KET_ZERO(q1), | ||
| observable=cirq.Z(q1)), | ||
| ] | ||
| grouped_settings = cirq.work.group_settings_greedy(settings) | ||
| # pylint: disable=line-too-long | ||
| grouped_settings_should_be = { | ||
| cirq.work.InitObsSetting(init_state=cirq.KET_PLUS(q0) * cirq.KET_ZERO(q1), | ||
| observable=cirq.X(q0) * cirq.Z(q1)): | ||
| settings | ||
| } | ||
| assert grouped_settings == grouped_settings_should_be | ||
|
|
||
|
|
||
| def test_group_settings_greedy_init_state_incompat(): | ||
| q0, q1 = cirq.LineQubit.range(2) | ||
| settings = [ | ||
| cirq.work.InitObsSetting(init_state=cirq.KET_PLUS(q0) * | ||
| cirq.KET_PLUS(q1), | ||
| observable=cirq.X(q0)), | ||
| cirq.work.InitObsSetting(init_state=cirq.KET_ZERO(q1), | ||
| observable=cirq.Z(q1)), | ||
| ] | ||
| grouped_settings = cirq.work.group_settings_greedy(settings) | ||
| assert len(grouped_settings) == 2 | ||
|
|
||
|
|
||
| def test_group_settings_greedy_hydrogen(): | ||
| qubits = cirq.LineQubit.range(4) | ||
| q0, q1, q2, q3 = qubits | ||
| terms = [ | ||
| 0.1711977489805745 * cirq.Z(q0), 0.17119774898057447 * cirq.Z(q1), | ||
| -0.2227859302428765 * cirq.Z(q2), -0.22278593024287646 * cirq.Z(q3), | ||
| 0.16862219157249939 * cirq.Z(q0) * cirq.Z(q1), | ||
| 0.04532220205777764 * cirq.Y(q0) * cirq.X(q1) * cirq.X(q2) * cirq.Y(q3), | ||
| -0.0453222020577776 * cirq.Y(q0) * cirq.Y(q1) * cirq.X(q2) * cirq.X(q3), | ||
| -0.0453222020577776 * cirq.X(q0) * cirq.X(q1) * cirq.Y(q2) * cirq.Y(q3), | ||
| 0.04532220205777764 * cirq.X(q0) * cirq.Y(q1) * cirq.Y(q2) * cirq.X(q3), | ||
| 0.12054482203290037 * cirq.Z(q0) * cirq.Z(q2), 0.16586702409067802 * | ||
| cirq.Z(q0) * cirq.Z(q3), 0.16586702409067802 * cirq.Z(q1) * cirq.Z(q2), | ||
| 0.12054482203290037 * cirq.Z(q1) * cirq.Z(q3), | ||
| 0.1743484418396392 * cirq.Z(q2) * cirq.Z(q3) | ||
| ] | ||
| settings = cirq.work.observables_to_settings(terms, qubits) | ||
| grouped_settings = cirq.work.group_settings_greedy(settings) | ||
| assert len(grouped_settings) == 5 | ||
|
|
||
| group_max_obs_should_be = [ | ||
| cirq.Y(q0) * cirq.X(q1) * cirq.X(q2) * cirq.Y(q3), | ||
| cirq.Y(q0) * cirq.Y(q1) * cirq.X(q2) * cirq.X(q3), | ||
| cirq.X(q0) * cirq.X(q1) * cirq.Y(q2) * cirq.Y(q3), | ||
| cirq.X(q0) * cirq.Y(q1) * cirq.Y(q2) * cirq.X(q3), | ||
| cirq.Z(q0) * cirq.Z(q1) * cirq.Z(q2) * cirq.Z(q3) | ||
| ] | ||
| group_max_settings_should_be = cirq.work.observables_to_settings( | ||
| group_max_obs_should_be, qubits) | ||
|
|
||
| assert set(grouped_settings.keys()) == set(group_max_settings_should_be) | ||
| groups = list(grouped_settings.values()) | ||
| assert len(groups[0]) == 1 | ||
| assert len(groups[1]) == 1 | ||
| assert len(groups[2]) == 1 | ||
| assert len(groups[3]) == 1 | ||
balopat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert len(groups[4]) == len(terms) - 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.
Uh oh!
There was an error while loading. Please reload this page.