Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cirq/ops/pauli_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ def with_qubits(self, *new_qubits: 'cirq.Qid') -> 'PauliString':
zip(new_qubits, (self[q] for q in self.qubits))),
coefficient=self._coefficient)

def with_coefficient(self, new_coefficient: Union[int, float, complex]
) -> 'PauliString':
return PauliString(qubit_pauli_map=dict(self._qubit_pauli_map),
coefficient=new_coefficient)

def values(self) -> ValuesView[pauli_gates.Pauli]:
return self._qubit_pauli_map.values()

Expand Down
11 changes: 11 additions & 0 deletions cirq/ops/pauli_string_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,17 @@ def test_with_qubits():
assert new_pauli_string.coefficient == -1


def test_with_coefficient():
qubits = cirq.LineQubit.range(4)
qubit_pauli_map = {q: cirq.Pauli.by_index(q.x) for q in qubits}
pauli_string = cirq.PauliString(qubit_pauli_map, 1.23)
ps2 = pauli_string.with_coefficient(1.0)
assert ps2.coefficient == 1.0
assert ps2.equal_up_to_coefficient(pauli_string)
assert pauli_string != ps2
assert pauli_string.coefficient == 1.23


@pytest.mark.parametrize('qubit_pauli_map', _small_sample_qubit_pauli_maps())
def test_consistency(qubit_pauli_map):
pauli_string = cirq.PauliString(qubit_pauli_map)
Expand Down
2 changes: 2 additions & 0 deletions cirq/work/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
_MeasurementSpec,
observables_to_settings,
)
from cirq.work.observable_grouping import (
group_settings_greedy,)
from cirq.work.sampler import (
Sampler,)
from cirq.work.zeros_sampler import (
Expand Down
77 changes: 77 additions & 0 deletions cirq/work/observable_grouping.py
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
154 changes: 154 additions & 0 deletions cirq/work/observable_grouping_test.py
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
assert len(groups[4]) == len(terms) - 4