Skip to content

Commit

Permalink
Merge branch 'master' into core-np1.24
Browse files Browse the repository at this point in the history
  • Loading branch information
pavoljuhas authored Mar 21, 2023
2 parents b84d1bd + 2c51eca commit 17c17d6
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 18 deletions.
10 changes: 1 addition & 9 deletions check/format-incremental
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,8 @@ if (( only_print == 1 )); then
args+=("--check" "--diff")
fi

# Warn users about bug in black: https://github.com/psf/black/issues/1629
# Once that is fixed upstream, we can just do:
# black "${args[@]}" "${format_files[@]}"
# exit $?
LOGS="$(black "${args[@]}" "${format_files[@]}" 2>&1)"
black "${args[@]}" "${format_files[@]}"
BLACKSTATUS=$?
echo "${LOGS}"
if [[ "$BLACKSTATUS" == "123" && "$LOGS" =~ ^.*"INTERNAL ERROR: Black produced different code on the second pass of the formatter.".*$ ]]; then
echo -e "\033[31mWarning, it seems we ran into https://github.com/psf/black/issues/1629. Typically, this can be fixed by adding a trailing comma. If you get stuck, please file a cirq issue on github.\033[0m"
fi

if [[ "$FLYNTSTATUS" != "0" || "$BLACKSTATUS" != "0" ]]; then
exit 1
Expand Down
6 changes: 3 additions & 3 deletions cirq-core/cirq/devices/grid_qubit.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

import functools
from typing import Any, Dict, Iterable, List, Optional, Tuple, Set, TypeVar, TYPE_CHECKING
from typing import Any, Dict, Iterable, List, Optional, Tuple, Set, TypeVar, TYPE_CHECKING, Union

import abc

Expand Down Expand Up @@ -75,7 +75,7 @@ def _with_row_col(self: TSelf, row: int, col: int) -> TSelf:
def __complex__(self) -> complex:
return self.col + 1j * self.row

def __add__(self: TSelf, other: Tuple[int, int]) -> 'TSelf':
def __add__(self: TSelf, other: Union[Tuple[int, int], TSelf]) -> 'TSelf':
if isinstance(other, _BaseGridQid):
if self.dimension != other.dimension:
raise TypeError(
Expand All @@ -94,7 +94,7 @@ def __add__(self: TSelf, other: Tuple[int, int]) -> 'TSelf':
)
return self._with_row_col(row=self.row + other[0], col=self.col + other[1])

def __sub__(self: TSelf, other: Tuple[int, int]) -> 'TSelf':
def __sub__(self: TSelf, other: Union[Tuple[int, int], TSelf]) -> 'TSelf':
if isinstance(other, _BaseGridQid):
if self.dimension != other.dimension:
raise TypeError(
Expand Down
6 changes: 3 additions & 3 deletions cirq-core/cirq/devices/line_qubit.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

import functools
from typing import Any, Dict, Iterable, List, Optional, Sequence, Set, TypeVar, TYPE_CHECKING
from typing import Any, Dict, Iterable, List, Optional, Sequence, Set, TypeVar, TYPE_CHECKING, Union

import abc

Expand Down Expand Up @@ -69,7 +69,7 @@ def neighbors(self, qids: Optional[Iterable[ops.Qid]] = None) -> Set['_BaseLineQ
def _with_x(self: TSelf, x: int) -> TSelf:
"""Returns a qubit with the same type but a different value of `x`."""

def __add__(self: TSelf, other: int) -> TSelf:
def __add__(self: TSelf, other: Union[int, TSelf]) -> TSelf:
if isinstance(other, _BaseLineQid):
if self.dimension != other.dimension:
raise TypeError(
Expand All @@ -81,7 +81,7 @@ def __add__(self: TSelf, other: int) -> TSelf:
raise TypeError(f"Can only add ints and {type(self).__name__}. Instead was {other}")
return self._with_x(self.x + other)

def __sub__(self: TSelf, other: int) -> TSelf:
def __sub__(self: TSelf, other: Union[int, TSelf]) -> TSelf:
if isinstance(other, _BaseLineQid):
if self.dimension != other.dimension:
raise TypeError(
Expand Down
6 changes: 3 additions & 3 deletions cirq-core/cirq/ops/linear_combinations.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,10 +839,10 @@ def __pow__(self, exponent: int):
if exponent == 0:
return PauliSum(value.LinearDict({frozenset(): 1 + 0j}))
if exponent > 0:
base = self.copy()
result = self.copy()
for _ in range(exponent - 1):
base *= base
return base
result *= self
return result
return NotImplemented

def __truediv__(self, a: value.Scalar):
Expand Down
16 changes: 16 additions & 0 deletions cirq-core/cirq/ops/linear_combinations_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,22 @@ def test_pauli_sum_pow():
for psum in [psum1, psum2, psum3, psum4]:
assert cirq.approx_eq(psum**0, identity)

# tests for exponents greater than two for both even and odd
psum5 = cirq.Z(q0) * cirq.Z(q1) + cirq.Z(q2) + cirq.Z(q3)
correctresult = psum5.copy()
for e in range(1, 9):
assert correctresult == psum5**e
correctresult *= psum5

psum6 = cirq.X(q0) * cirq.Y(q1) + cirq.Z(q2) + cirq.X(q3)
assert psum6 * psum6 * psum6 * psum6 * psum6 * psum6 * psum6 * psum6 == psum6**8

# test to ensure pow doesn't make any change to the original value
psum7 = cirq.X(q0) * cirq.Y(q1) + cirq.Z(q2)
psum7copy = psum7.copy()
assert psum7**5 == psum7 * psum7 * psum7 * psum7 * psum7
assert psum7copy == psum7


# Using the entries of table 1 of https://arxiv.org/abs/1804.09130 as golden values.
@pytest.mark.parametrize(
Expand Down
2 changes: 2 additions & 0 deletions dev_tools/notebooks/isolated_notebook_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@
# https://github.com/networkx/networkx/issues/4718 pinned networkx 2.5.1 to 4.4.2
# however, jupyter brings in 5.0.6
'decorator<5',
# TODO(#5967): allow numpy-1.24 when it is supported in Cirq and numba
'numpy>=1.16,<1.24',
]


Expand Down

0 comments on commit 17c17d6

Please sign in to comment.