Skip to content

Commit bf23fc0

Browse files
mhuckapavoljuhas
andauthored
Improve docstring for unitary() & other text in unitary_protocol.py (#7582)
The behavior of the function `unitary(…)` changed with respect to NumPy array arguments. I found the docstring not clear enough during some recent debugging on another issue. I rewrote that part, then noticed inconsistencies in formatting and language elsewhehre, so ended up editing other parts of this file too. --------- Co-authored-by: Pavol Juhas <pavol.juhas@gmail.com>
1 parent ad37bb7 commit bf23fc0

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

cirq-core/cirq/protocols/unitary_protocol.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from cirq.protocols.apply_unitary_protocol import apply_unitaries, ApplyUnitaryArgs
2525
from cirq.protocols.decompose_protocol import _try_decompose_into_operations_and_qubits
2626

27-
# This is a special indicator value used by the unitary method to determine
27+
# This is a special indicator value used by the function unitary() to determine
2828
# whether or not the caller provided a 'default' argument. It must be of type
2929
# np.ndarray to ensure the method has the correct type signature in that case.
3030
# It is checked for using `is`, so it won't have a false positive if the user
@@ -44,17 +44,17 @@ def _unitary_(self) -> np.ndarray | NotImplementedType | None:
4444
This method is used by the global `cirq.unitary` method. If this method
4545
is not present, or returns NotImplemented, it is assumed that the
4646
receiving object doesn't have a unitary matrix (resulting in a TypeError
47-
or default result when calling `cirq.unitary` on it). (The ability to
47+
or default result when calling `cirq.unitary` on it). The ability to
4848
return NotImplemented is useful when a class cannot know if it has a
49-
matrix until runtime, e.g. cirq.X**c normally has a matrix but
50-
cirq.X**sympy.Symbol('a') doesn't.)
49+
matrix until runtime; e.g., `cirq.X**c` normally has a matrix but
50+
`cirq.X**sympy.Symbol('a')` doesn't.
5151
5252
The order of cells in the matrix is always implicit with respect to the
53-
object being called. For example, for gates the matrix must be ordered
53+
object being called. For example, for gates, the matrix must be ordered
5454
with respect to the list of qubits that the gate is applied to. For
5555
operations, the matrix is ordered to match the list returned by its
5656
`qubits` attribute. The qubit-to-amplitude order mapping matches the
57-
ordering of numpy.kron(A, B), where A is a qubit earlier in the list
57+
ordering of `numpy.kron(A, B)`, where A is a qubit earlier in the list
5858
than the qubit B.
5959
6060
Returns:
@@ -68,7 +68,7 @@ def _has_unitary_(self) -> bool:
6868
6969
This method is used by the global `cirq.has_unitary` method. If this
7070
method is not present, or returns NotImplemented, it will fallback
71-
to using _unitary_ with a default value, or False if neither exist.
71+
to using `_unitary_()` with a default value, or False if neither exist.
7272
7373
Returns:
7474
True if the value has a unitary matrix representation, False
@@ -81,17 +81,17 @@ def unitary(
8181
) -> np.ndarray | TDefault:
8282
"""Returns a unitary matrix describing the given value.
8383
84-
The matrix is determined by any one of the following techniques:
84+
The matrix is determined by the first of these strategies that succeeds:
8585
86-
- If the value is a numpy array, it is returned directly.
87-
- The value has a `_unitary_` method that returns something besides None or
88-
NotImplemented. The matrix is whatever the method returned.
86+
- If the value is a NumPy array, it is returned directly.
87+
- The value has a `_unitary_` method that returns something besides `None` or
88+
`NotImplemented`. The matrix is whatever the method returned.
89+
- The value has an `_apply_unitary_` method, and it returns something
90+
besides `None` or `NotImplemented`. The matrix is created by applying
91+
`_apply_unitary_` to an identity matrix.
8992
- The value has a `_decompose_` method that returns a list of operations,
9093
and each operation in the list has a unitary effect. The matrix is
9194
created by aggregating the sub-operations' unitary effects.
92-
- The value has an `_apply_unitary_` method, and it returns something
93-
besides None or NotImplemented. The matrix is created by applying
94-
`_apply_unitary_` to an identity matrix.
9595
9696
If none of these techniques succeeds, it is assumed that `val` doesn't have
9797
a unitary effect. The order in which techniques are attempted is
@@ -100,8 +100,8 @@ def unitary(
100100
Args:
101101
val: The value to describe with a unitary matrix.
102102
default: Determines the fallback behavior when `val` doesn't have
103-
a unitary effect. If `default` is not set, a TypeError is raised. If
104-
`default` is set to a value, that value is returned.
103+
a unitary effect. If `default` is not set, a `TypeError` is raised.
104+
If `default` is set to a value, that value is returned.
105105
106106
Returns:
107107
If `val` has a unitary effect, the corresponding unitary matrix.
@@ -135,13 +135,13 @@ def unitary(
135135
f"type: {type(val)}\n"
136136
f"value: {val!r}\n"
137137
"\n"
138-
"The value failed to satisfy any of the following criteria:\n"
138+
"The given value failed to satisfy any of the following criteria:\n"
139139
"- A `_unitary_(self)` method that returned a value "
140-
"besides None or NotImplemented.\n"
140+
"besides `None` or `NotImplemented`.\n"
141141
"- A `_decompose_(self)` method that returned a "
142142
"list of unitary operations.\n"
143-
"- An `_apply_unitary_(self, args) method that returned a value "
144-
"besides None or NotImplemented."
143+
"- An `_apply_unitary_(self, args)` method that returned a value "
144+
"besides `None` or `NotImplemented`."
145145
)
146146

147147

0 commit comments

Comments
 (0)