-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5e9a8bf
Update docstrings for PauliSum.
MichaelBroughton 02afbe8
thank you pylint.
MichaelBroughton 1a5d386
Tanuj feedback.
MichaelBroughton 07c2907
Tanuj feedback 2.
MichaelBroughton b6262e5
Merge branch 'master' into paulisum_docs
CirqBot 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 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 | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -368,9 +368,61 @@ class PauliSum: | |||||||||||||||||
Under the hood, this class is backed by a LinearDict with coefficient-less | ||||||||||||||||||
PauliStrings as keys. PauliStrings are reconstructed on-the-fly during | ||||||||||||||||||
iteration. | ||||||||||||||||||
|
||||||||||||||||||
PauliSums can be constructed explicitly: | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
>>> a, b = cirq.GridQubit.rect(1, 2) | ||||||||||||||||||
>>> sum = cirq.PauliSum() | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
or implicitly: | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
>>> a, b = cirq.GridQubit.rect(1, 2) | ||||||||||||||||||
>>> psum = cirq.X(a) * cirq.X(b) + 3.0 * cirq.Y(a) | ||||||||||||||||||
>>> psum | ||||||||||||||||||
cirq.PauliSum( | ||||||||||||||||||
cirq.LinearDict({ | ||||||||||||||||||
frozenset({ | ||||||||||||||||||
(cirq.GridQubit(0, 0), cirq.X), (cirq.GridQubit(0, 1), cirq.X)}): (1+0j), | ||||||||||||||||||
frozenset({ | ||||||||||||||||||
(cirq.GridQubit(0, 0), cirq.Y)}): (3+0j)} | ||||||||||||||||||
) | ||||||||||||||||||
) | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
basic arithmetic and expectation operations are supported as well: | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
>>> a, b = cirq.GridQubit.rect(1, 2) | ||||||||||||||||||
>>> psum = cirq.X(a) * cirq.X(b) + 3.0 * cirq.Y(a) | ||||||||||||||||||
>>> two_psum = 2 * psum | ||||||||||||||||||
>>> four_psum = two_psum + two_psum | ||||||||||||||||||
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. Add a |
||||||||||||||||||
>>> expectation = four_psum.expectation_from_state_vector( | ||||||||||||||||||
... np.array([0.707106, 0, 0, 0.707106], dtype=complex), | ||||||||||||||||||
... qubit_map={a: 0, b: 1} | ||||||||||||||||||
... ) | ||||||||||||||||||
>>> expectation | ||||||||||||||||||
4.0 | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
""" | ||||||||||||||||||
|
||||||||||||||||||
def __init__(self, linear_dict: Optional[value.LinearDict[UnitPauliStringT]] = None): | ||||||||||||||||||
"""Construct a PauliSum from a linear dictionary. | ||||||||||||||||||
|
||||||||||||||||||
Note, the preferred method of constructing PauliSum objects is either implicitly | ||||||||||||||||||
or via the `from_pauli_strings` function. | ||||||||||||||||||
|
||||||||||||||||||
Args: | ||||||||||||||||||
linear_dict: Set of (`cirq.Qid`, `cirq.Pauli`) tuples to construct the sum | ||||||||||||||||||
from. | ||||||||||||||||||
|
||||||||||||||||||
Raises: | ||||||||||||||||||
ValueError: If structure of `linear_dict` contains tuples other than the | ||||||||||||||||||
form (`cirq.Qid`, `cirq.Pauli`). | ||||||||||||||||||
""" | ||||||||||||||||||
if linear_dict is None: | ||||||||||||||||||
linear_dict = value.LinearDict() | ||||||||||||||||||
if not _is_linear_dict_of_unit_pauli_string(linear_dict): | ||||||||||||||||||
|
@@ -388,6 +440,16 @@ def _value_equality_values_(self): | |||||||||||||||||
def wrap(val: PauliSumLike) -> 'PauliSum': | ||||||||||||||||||
"""Convert a `cirq.PauliSumLike` object to a PauliSum | ||||||||||||||||||
MichaelBroughton marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
|
||||||||||||||||||
Attemps to convert an existing int, float, complex, `cirq.PauliString`, | ||||||||||||||||||
`cirq.PauliSum` or `cirq.SingleQubitPauliStringGateOperation` into | ||||||||||||||||||
a `cirq.PauliSum` object. For example: | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
>>> my_psum = cirq.PauliSum.wrap(2.345) | ||||||||||||||||||
>>> my_psum | ||||||||||||||||||
cirq.PauliSum(cirq.LinearDict({frozenset(): (2.345+0j)})) | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
Args: | ||||||||||||||||||
`cirq.PauliSumLike` to convert to PauliSum. | ||||||||||||||||||
|
||||||||||||||||||
|
@@ -400,7 +462,7 @@ def wrap(val: PauliSumLike) -> 'PauliSum': | |||||||||||||||||
|
||||||||||||||||||
@classmethod | ||||||||||||||||||
def from_pauli_strings(cls, terms: Union[PauliString, List[PauliString]]) -> 'PauliSum': | ||||||||||||||||||
"""Returns a PauliSum by combining PauliString terms. | ||||||||||||||||||
"""Returns a PauliSum by combining `cirq.PauliString` terms. | ||||||||||||||||||
|
||||||||||||||||||
Args: | ||||||||||||||||||
terms: `cirq.PauliString` or List of `cirq.PauliString`s to use inside | ||||||||||||||||||
|
@@ -431,7 +493,7 @@ def from_boolean_expression( | |||||||||||||||||
qubit_map: map of string (boolean variable name) to qubit. | ||||||||||||||||||
|
||||||||||||||||||
Return: | ||||||||||||||||||
The PauliString that represents the Boolean expression. | ||||||||||||||||||
The PauliSum that represents the Boolean expression. | ||||||||||||||||||
|
||||||||||||||||||
Raises: | ||||||||||||||||||
ValueError: If `boolean_expr` is of an unsupported type. | ||||||||||||||||||
|
@@ -472,6 +534,7 @@ def from_boolean_expression( | |||||||||||||||||
|
||||||||||||||||||
@property | ||||||||||||||||||
def qubits(self) -> Tuple[raw_types.Qid, ...]: | ||||||||||||||||||
"""The sorted list of qubits used in this PauliSum.""" | ||||||||||||||||||
qs = {q for k in self._linear_dict.keys() for q, _ in k} | ||||||||||||||||||
return tuple(sorted(qs)) | ||||||||||||||||||
MichaelBroughton marked this conversation as resolved.
Show resolved
Hide resolved
MichaelBroughton marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
|
||||||||||||||||||
|
@@ -487,8 +550,7 @@ def with_qubits(self, *new_qubits: 'cirq.Qid') -> 'PauliSum': | |||||||||||||||||
qubits. | ||||||||||||||||||
|
||||||||||||||||||
Raises: | ||||||||||||||||||
ValueError: If incorrect number of replacement qubits | ||||||||||||||||||
are provided. | ||||||||||||||||||
ValueError: If len(new_qubits) != len(self.qubits). | ||||||||||||||||||
|
||||||||||||||||||
""" | ||||||||||||||||||
qubits = self.qubits | ||||||||||||||||||
|
@@ -509,12 +571,12 @@ def copy(self) -> 'PauliSum': | |||||||||||||||||
return factory(self._linear_dict.copy()) | ||||||||||||||||||
|
||||||||||||||||||
def matrix(self, qubits: Optional[Iterable[raw_types.Qid]] = None) -> np.ndarray: | ||||||||||||||||||
"""Get the matrix representing this PauliSum. | ||||||||||||||||||
"""Returns the matrix of this PauliSum in computational basis of qubits. | ||||||||||||||||||
|
||||||||||||||||||
Args: | ||||||||||||||||||
qubits: Iterable of qubits, specifying ordering to use | ||||||||||||||||||
in the returned matrix. If none is provided | ||||||||||||||||||
the default ordering of `self.qubits` is used. | ||||||||||||||||||
qubits: Iterable of qubits, ordering to determine the computational | ||||||||||||||||||
basis of qubits. If none is provided the default ordering of | ||||||||||||||||||
`self.qubits` is used. | ||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
Returns: | ||||||||||||||||||
np.ndarray representing the matrix of this PauliSum expression. | ||||||||||||||||||
|
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.
nit: Can we show an example which constructs a non-empty pauli sum? Maybe using the
.from_pauli_strings
method, since that's the recommended way?