Skip to content

Commit

Permalink
pylint updated in files 35-50 of 70
Browse files Browse the repository at this point in the history
  • Loading branch information
pavoljuhas committed Jul 15, 2023
1 parent 53d0d86 commit 3ccb886
Show file tree
Hide file tree
Showing 16 changed files with 559 additions and 84 deletions.
26 changes: 21 additions & 5 deletions cirq-core/cirq/ops/gateset_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# TODO(#6171): enable the check and fix pylint errors
# pylint: disable=consider-using-f-string

from typing import Tuple, List, cast
import re
import pytest
Expand All @@ -40,8 +37,27 @@ def __repr__(self) -> str:
if self._exponent == 1:
return 'cirq.ops.gateset_test.CustomX'
return f'(cirq.ops.gateset_test.CustomX**{proper_repr(self._exponent)})'
return 'cirq.ops.gateset_test.CustomXPowGate(exponent={}, global_shift={!r})'.format(
proper_repr(self._exponent), self._global_shift

# TODO(#6171): BEGIN
# pylint: disable=consider-using-f-string
string_before = (
'cirq.ops.gateset_test.CustomXPowGate(exponent={}, global_shift={!r})'.format(
proper_repr(self._exponent), self._global_shift
)
)
string_after = (
'cirq.ops.gateset_test.CustomXPowGate('
f'exponent={proper_repr(self._exponent)}, '
f'global_shift={self._global_shift!r})'
)
assert string_before == string_after
# pylint: enable=consider-using-f-string
# TODO(#6171): END

return (
'cirq.ops.gateset_test.CustomXPowGate('
f'exponent={proper_repr(self._exponent)}, '
f'global_shift={self._global_shift!r})'
)

def _num_qubits_(self) -> int:
Expand Down
35 changes: 30 additions & 5 deletions cirq-core/cirq/ops/linear_combinations.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# TODO(#6171): enable the check and fix pylint errors
# pylint: disable=consider-using-f-string

from collections import defaultdict
from typing import (
AbstractSet,
Expand Down Expand Up @@ -653,10 +650,24 @@ def expectation_from_state_vector(
ValueError: If the input vector is not the correct size or shape.
"""
if any(abs(p.coefficient.imag) > 0.0001 for p in self):
raise NotImplementedError(
# TODO(#6171): BEGIN
# pylint: disable=consider-using-f-string
string_before = (
"Cannot compute expectation value of a non-Hermitian "
"PauliString <{}>. Coefficient must be real.".format(self)
)
string_after = (
"Cannot compute expectation value of a non-Hermitian "
f"PauliString <{self}>. Coefficient must be real."
)
assert string_before == string_after
# pylint: enable=consider-using-f-string
# TODO(#6171): END

raise NotImplementedError(
"Cannot compute expectation value of a non-Hermitian "
f"PauliString <{self}>. Coefficient must be real."
)

# TODO: Avoid enforce specific complex type. This is necessary to
# prevent an `apply_unitary` bug.
Expand Down Expand Up @@ -715,10 +726,24 @@ def expectation_from_density_matrix(
ValueError: If the input vector is not the correct size or shape.
"""
if any(abs(p.coefficient.imag) > 0.0001 for p in self):
raise NotImplementedError(
# TODO(#6171): BEGIN
# pylint: disable=consider-using-f-string
string_before = (
"Cannot compute expectation value of a non-Hermitian "
"PauliString <{}>. Coefficient must be real.".format(self)
)
string_after = (
"Cannot compute expectation value of a non-Hermitian "
f"PauliString <{self}>. Coefficient must be real."
)
assert string_before == string_after
# pylint: enable=consider-using-f-string
# TODO(#6171): END

raise NotImplementedError(
"Cannot compute expectation value of a non-Hermitian "
f"PauliString <{self}>. Coefficient must be real."
)

# FIXME: Avoid enforce specific complex type. This is necessary to
# prevent an `apply_unitary` bug (Issue #2041).
Expand Down
38 changes: 30 additions & 8 deletions cirq-core/cirq/ops/raw_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# TODO(#6171): enable the check and fix pylint errors
# pylint: disable=consider-using-f-string

"""Basic types defining qubits, gates, and operations."""

import abc
Expand Down Expand Up @@ -1044,16 +1041,41 @@ def _validate_qid_shape(val: Any, qubits: Sequence['cirq.Qid']) -> None:
"""
qid_shape = protocols.qid_shape(val)
if len(qubits) != len(qid_shape):
raise ValueError(
# TODO(#6171): BEGIN
# pylint: disable=consider-using-f-string
string_before = (
'Wrong number of qubits for <{!r}>. '
'Expected {} qubits but got <{!r}>.'.format(val, len(qid_shape), qubits)
)
string_after = (
f'Wrong number of qubits for <{val!r}>. '
f'Expected {len(qid_shape)} qubits but got <{qubits!r}>.'
)
assert string_before == string_after
# pylint: enable=consider-using-f-string
# TODO(#6171): END

raise ValueError(
f'Wrong number of qubits for <{val!r}>. '
f'Expected {len(qid_shape)} qubits but got <{qubits!r}>.'
)
if any(qid.dimension != dimension for qid, dimension in zip(qubits, qid_shape)):
# TODO(#6171): BEGIN
# pylint: disable=consider-using-f-string
string_before = 'Wrong shape of qids for <{!r}>. ' 'Expected {} but got {} <{!r}>.'.format(
val, qid_shape, tuple(qid.dimension for qid in qubits), qubits
)
string_after = (
f'Wrong shape of qids for <{val!r}>. '
f'Expected {qid_shape} but got {tuple(qid.dimension for qid in qubits)} <{qubits!r}>.'
)
assert string_before == string_after
# pylint: enable=consider-using-f-string
# TODO(#6171): END

raise ValueError(
'Wrong shape of qids for <{!r}>. '
'Expected {} but got {} <{!r}>.'.format(
val, qid_shape, tuple(qid.dimension for qid in qubits), qubits
)
f'Wrong shape of qids for <{val!r}>. '
f'Expected {qid_shape} but got {tuple(qid.dimension for qid in qubits)} <{qubits!r}>.'
)
if len(set(qubits)) != len(qubits):
raise ValueError(
Expand Down
49 changes: 43 additions & 6 deletions cirq-core/cirq/ops/three_qubit_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# TODO(#6171): enable the check and fix pylint errors
# pylint: disable=consider-using-f-string

"""Common quantum gates that target three qubits."""

from typing import (
Expand Down Expand Up @@ -179,9 +176,24 @@ def __repr__(self) -> str:
if self._exponent == 1:
return 'cirq.CCZ'
return f'(cirq.CCZ**{proper_repr(self._exponent)})'
return 'cirq.CCZPowGate(exponent={}, global_shift={!r})'.format(

# TODO(#6171): BEGIN
# pylint: disable=consider-using-f-string
string_before = 'cirq.CCZPowGate(exponent={}, global_shift={!r})'.format(
proper_repr(self._exponent), self._global_shift
)
string_after = (
f'cirq.CCZPowGate(exponent={proper_repr(self._exponent)}, '
f'global_shift={self._global_shift!r})'
)
assert string_before == string_after
# pylint: enable=consider-using-f-string
# TODO(#6171): END

return (
f'cirq.CCZPowGate(exponent={proper_repr(self._exponent)}, '
f'global_shift={self._global_shift!r})'
)

def __str__(self) -> str:
if self._exponent == 1:
Expand Down Expand Up @@ -386,9 +398,19 @@ def _json_dict_(self) -> Dict[str, Any]:
return protocols.obj_to_dict_helper(self, attribute_names=["diag_angles_radians"])

def __repr__(self) -> str:
return 'cirq.ThreeQubitDiagonalGate([{}])'.format(
angles = ','.join(proper_repr(angle) for angle in self._diag_angles_radians)

# TODO(#6171): BEGIN
# pylint: disable=consider-using-f-string
string_before = 'cirq.ThreeQubitDiagonalGate([{}])'.format(
','.join(proper_repr(angle) for angle in self._diag_angles_radians)
)
string_after = f'cirq.ThreeQubitDiagonalGate([{angles}])'
assert string_before == string_after
# pylint: enable=consider-using-f-string
# TODO(#6171): END

return f'cirq.ThreeQubitDiagonalGate([{angles}])'

def _num_qubits_(self) -> int:
return 3
Expand Down Expand Up @@ -491,9 +513,24 @@ def __repr__(self) -> str:
if self._exponent == 1:
return 'cirq.TOFFOLI'
return f'(cirq.TOFFOLI**{proper_repr(self._exponent)})'
return 'cirq.CCXPowGate(exponent={}, global_shift={!r})'.format(

# TODO(#6171): BEGIN
# pylint: disable=consider-using-f-string
string_before = 'cirq.CCXPowGate(exponent={}, global_shift={!r})'.format(
proper_repr(self._exponent), self._global_shift
)
string_after = (
f'cirq.CCXPowGate(exponent={proper_repr(self._exponent)}, '
f'global_shift={self._global_shift!r})'
)
assert string_before == string_after
# pylint: enable=consider-using-f-string
# TODO(#6171): END

return (
f'cirq.CCXPowGate(exponent={proper_repr(self._exponent)}, '
f'global_shift={self._global_shift!r})'
)

def __str__(self) -> str:
if self._exponent == 1:
Expand Down
14 changes: 10 additions & 4 deletions cirq-core/cirq/ops/two_qubit_diagonal_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# TODO(#6171): enable the check and fix pylint errors
# pylint: disable=consider-using-f-string

"""Creates the gate instance for a two qubit diagonal gate.
The gate is used to create a 4x4 matrix with the diagonal elements
Expand Down Expand Up @@ -138,9 +135,18 @@ def _value_equality_values_(self) -> Any:
return tuple(self._diag_angles_radians)

def __repr__(self) -> str:
return 'cirq.TwoQubitDiagonalGate([{}])'.format(
angles = ','.join(proper_repr(angle) for angle in self._diag_angles_radians)
# TODO(#6171): BEGIN
# pylint: disable=consider-using-f-string
string_before = 'cirq.TwoQubitDiagonalGate([{}])'.format(
','.join(proper_repr(angle) for angle in self._diag_angles_radians)
)
string_after = f'cirq.TwoQubitDiagonalGate([{angles}])'
assert string_before == string_after
# pylint: enable=consider-using-f-string
# TODO(#6171): END

return f'cirq.TwoQubitDiagonalGate([{angles}])'

def _json_dict_(self) -> Dict[str, Any]:
return protocols.obj_to_dict_helper(self, attribute_names=["diag_angles_radians"])
76 changes: 69 additions & 7 deletions cirq-core/cirq/protocols/apply_channel_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# TODO(#6171): enable the check and fix pylint errors
# pylint: disable=consider-using-f-string

"""A protocol for implementing high performance channel evolutions."""

from typing import Any, Iterable, Optional, Sequence, TypeVar, Tuple, Union
Expand Down Expand Up @@ -225,29 +222,77 @@ def apply_channel(
left_shape = tuple(args.target_tensor.shape[i] for i in args.left_axes)
right_shape = tuple(args.target_tensor.shape[i] for i in args.right_axes)
if left_shape != right_shape:
raise ValueError(
# TODO(#6171): BEGIN
# pylint: disable=consider-using-f-string
string_before = (
'Invalid target_tensor shape or selected axes. '
'The selected left and right shape of target_tensor '
'are not equal. Got {!r} and {!r}.'.format(left_shape, right_shape)
)
if val_qid_shape != left_shape:
string_after = (
'Invalid target_tensor shape or selected axes. '
'The selected left and right shape of target_tensor '
f'are not equal. Got {left_shape!r} and {right_shape!r}.'
)
assert string_before == string_after
# pylint: enable=consider-using-f-string
# TODO(#6171): END

raise ValueError(
'Invalid target_tensor shape or selected axes. '
'The selected left and right shape of target_tensor '
f'are not equal. Got {left_shape!r} and {right_shape!r}.'
)
if val_qid_shape != left_shape:
# TODO(#6171): BEGIN
# pylint: disable=consider-using-f-string
string_before = (
'Invalid channel qid shape is not equal to the '
'selected left and right shape of target_tensor. '
'Got {!r} but expected {!r}.'.format(val_qid_shape, left_shape)
)
string_after = (
'Invalid channel qid shape is not equal to the '
'selected left and right shape of target_tensor. '
f'Got {val_qid_shape!r} but expected {left_shape!r}.'
)
assert string_before == string_after
# pylint: enable=consider-using-f-string
# TODO(#6171): END

raise ValueError(
'Invalid channel qid shape is not equal to the '
'selected left and right shape of target_tensor. '
f'Got {val_qid_shape!r} but expected {left_shape!r}.'
)

# Check if the specialized method is present.
if hasattr(val, '_apply_channel_'):
result = val._apply_channel_(args)
if result is not NotImplemented and result is not None:

def err_str(buf_num_str):
return (
# TODO(#6171): BEGIN
# pylint: disable=consider-using-f-string
string_before = (
"Object of type '{}' returned a result object equal to "
"auxiliary_buffer{}. This type violates the contract "
"that appears in apply_channel's documentation.".format(type(val), buf_num_str)
)
string_after = (
f"Object of type '{type(val)}' returned a result object equal to "
f"auxiliary_buffer{buf_num_str}. This type violates the contract "
"that appears in apply_channel's documentation."
)
assert string_before == string_after
# pylint: enable=consider-using-f-string
# TODO(#6171): END

return (
f"Object of type '{type(val)}' returned a result object equal to "
f"auxiliary_buffer{buf_num_str}. This type violates the contract "
"that appears in apply_channel's documentation."
)

assert result is not args.auxiliary_buffer0, err_str('0')
assert result is not args.auxiliary_buffer1, err_str('1')
Expand All @@ -266,11 +311,28 @@ def err_str(buf_num_str):
# Don't know how to apply channel. Fallback to specified default behavior.
if default is not RaiseTypeErrorIfNotProvided:
return default
raise TypeError(

# TODO(#6171): BEGIN
# pylint: disable=consider-using-f-string
string_before = (
"object of type '{}' has no _apply_channel_, _apply_unitary_, "
"_unitary_, or _kraus_ methods (or they returned None or "
"NotImplemented).".format(type(val))
)
string_after = (
f"object of type '{type(val)}' has no _apply_channel_, _apply_unitary_, "
"_unitary_, or _kraus_ methods (or they returned None or "
"NotImplemented)."
)
assert string_before == string_after
# pylint: enable=consider-using-f-string
# TODO(#6171): END

raise TypeError(
f"object of type '{type(val)}' has no _apply_channel_, _apply_unitary_, "
"_unitary_, or _kraus_ methods (or they returned None or "
"NotImplemented)."
)


def _apply_unitary(val: Any, args: 'ApplyChannelArgs') -> Optional[np.ndarray]:
Expand Down

0 comments on commit 3ccb886

Please sign in to comment.