Skip to content
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

Update docstrings for PauliSum. #5596

Merged
merged 5 commits into from
Jul 7, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 47 additions & 4 deletions cirq-core/cirq/ops/linear_combinations.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,8 @@ def _pauli_string_from_unit(unit: UnitPauliStringT, coefficient: Union[int, floa
class PauliSum:
"""Represents operator defined by linear combination of PauliStrings.

Since PauliStrings store their own coefficients, this class
does not implement the LinearDict interface. Instead, you can
Since `cirq.PauliString`s store their own coefficients, this class
MichaelBroughton marked this conversation as resolved.
Show resolved Hide resolved
does not implement the `cirq.LinearDict` interface. Instead, you can
add and subtract terms and then iterate over the resulting
(simplified) expression.

MichaelBroughton marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -386,12 +386,29 @@ def _value_equality_values_(self):

@staticmethod
def wrap(val: PauliSumLike) -> 'PauliSum':
"""Convert a `cirq.PauliSumLike` object to a PauliSum
MichaelBroughton marked this conversation as resolved.
Show resolved Hide resolved

Args:
`cirq.PauliSumLike` to convert to PauliSum.

Returns:
PauliSum representation of `val`.
"""
if isinstance(val, PauliSum):
return val
return PauliSum() + val

@classmethod
def from_pauli_strings(cls, terms: Union[PauliString, List[PauliString]]) -> 'PauliSum':
"""Returns a PauliSum by combining PauliString terms.
MichaelBroughton marked this conversation as resolved.
Show resolved Hide resolved

Args:
terms: `cirq.PauliString` or List of `cirq.PauliString`s to use inside
of this PauliSum object.
Returns:
PauliSum object representing the addition of all the `cirq.PauliString`
terms in `terms`.
"""
if isinstance(terms, PauliString):
terms = [terms]
termdict: DefaultDict[UnitPauliStringT, value.Scalar] = defaultdict(lambda: 0)
Expand Down Expand Up @@ -459,6 +476,21 @@ def qubits(self) -> Tuple[raw_types.Qid, ...]:
return tuple(sorted(qs))
MichaelBroughton marked this conversation as resolved.
Show resolved Hide resolved
MichaelBroughton marked this conversation as resolved.
Show resolved Hide resolved

def with_qubits(self, *new_qubits: 'cirq.Qid') -> 'PauliSum':
"""Return a new PauliSum on `new_qubits`.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can use some example here / highlight that self.qubits will give a sorted list of all qubits used in the pauli sum; and new qubits will be mapped 1:1 with the sorted qubits.

The fact that pauli sum will always return a sorted list of qubits is tricky and could be helpful to provide some examples.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were you thinking maybe a code snippet ?

Args:
*new_qubits: `cirq.Qid` objects to replace existing
qubit objects in this PauliSum.

Returns:
PauliSum with new_qubits replacing the previous
qubits.

Raises:
ValueError: If incorrect number of replacement qubits
MichaelBroughton marked this conversation as resolved.
Show resolved Hide resolved
are provided.

"""
qubits = self.qubits
if len(new_qubits) != len(qubits):
raise ValueError('Incorrect number of qubits for PauliSum.')
Expand All @@ -469,12 +501,23 @@ def with_qubits(self, *new_qubits: 'cirq.Qid') -> 'PauliSum':
return PauliSum.from_pauli_strings(new_pauli_strings)

def copy(self) -> 'PauliSum':
"""Return a copy of this PauliSum.

Returns: A copy of this PauliSum.
"""
factory = type(self)
return factory(self._linear_dict.copy())

def matrix(self, qubits: Optional[Iterable[raw_types.Qid]] = None) -> np.ndarray:
"""Reconstructs matrix of self from underlying Pauli operations in
computational basis of qubits.
"""Get the matrix representing this PauliSum.
MichaelBroughton marked this conversation as resolved.
Show resolved Hide resolved

Args:
qubits: Iterable of qubits, specifying ordering to use
in the returned matrix. If none is provided
MichaelBroughton marked this conversation as resolved.
Show resolved Hide resolved
the default ordering of `self.qubits` is used.

Returns:
np.ndarray representing the matrix of this PauliSum expression.

Raises:
TypeError: if any of the gates in self does not provide a unitary.
Expand Down