From 7335e7157de517f7f6b6d115fc6c4468364bcbf6 Mon Sep 17 00:00:00 2001 From: sarahsor Date: Tue, 21 Mar 2023 15:02:21 -0400 Subject: [PATCH 1/5] Fixed type annotations in _BaseLineQid and _BaseGridQid (#6043) --- cirq-core/cirq/devices/grid_qubit.py | 6 +++--- cirq-core/cirq/devices/line_qubit.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cirq-core/cirq/devices/grid_qubit.py b/cirq-core/cirq/devices/grid_qubit.py index acbdd9109290..89bfce0d273c 100644 --- a/cirq-core/cirq/devices/grid_qubit.py +++ b/cirq-core/cirq/devices/grid_qubit.py @@ -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 @@ -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( @@ -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( diff --git a/cirq-core/cirq/devices/line_qubit.py b/cirq-core/cirq/devices/line_qubit.py index 788db7230fc6..6f685fe77784 100644 --- a/cirq-core/cirq/devices/line_qubit.py +++ b/cirq-core/cirq/devices/line_qubit.py @@ -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 @@ -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( @@ -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( From cc8fc312e6ffcd6e357a0b118e68da8f95b00ee8 Mon Sep 17 00:00:00 2001 From: sarahsor Date: Tue, 21 Mar 2023 15:02:46 -0400 Subject: [PATCH 2/5] Fixed type annotations in _BaseLineQid and _BaseGridQid (#6043) From 4607dd1ee17f03918a674fdc1a93efd0c4b757b8 Mon Sep 17 00:00:00 2001 From: Pavol Juhas Date: Tue, 21 Mar 2023 13:21:24 -0700 Subject: [PATCH 3/5] Remove outdated workaround for a bug in black formatter (#6045) The bug appears to be fixed in our required version of black per https://github.com/psf/black/issues/1629 --- check/format-incremental | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/check/format-incremental b/check/format-incremental index eca80ac44991..5ee15cfb70c3 100755 --- a/check/format-incremental +++ b/check/format-incremental @@ -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 From 402260c6a2f434f2c6bc9915c0647fa385cca16c Mon Sep 17 00:00:00 2001 From: Tarun Singhania <37648118+TarunSinghania@users.noreply.github.com> Date: Wed, 22 Mar 2023 02:17:56 +0530 Subject: [PATCH 4/5] Update and correct pow function for PauliSum (#6019) * Update and correct pow function for PauliSum Updated pow function for PauliSum to use binary exponentiation * Format and remainder initialisation correction +Corrected formatting +Corrected initialisation of remainder to identity * Format and remainder initialisation correction +Corrected formatting +Corrected initialisation of remainder to identity * using local variable instead of self and reformatting * reverting to regular linear multiplication and adding tests * Adding test for range (1,9) * trailing blank * remove trailing blank test * Getting rid of extraneous variables * Fix up final formatting issue --------- Co-authored-by: Tanuj Khattar Co-authored-by: Pavol Juhas --- cirq-core/cirq/ops/linear_combinations.py | 6 +++--- cirq-core/cirq/ops/linear_combinations_test.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/cirq-core/cirq/ops/linear_combinations.py b/cirq-core/cirq/ops/linear_combinations.py index d5892e7fddb2..f9335d10379f 100644 --- a/cirq-core/cirq/ops/linear_combinations.py +++ b/cirq-core/cirq/ops/linear_combinations.py @@ -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): diff --git a/cirq-core/cirq/ops/linear_combinations_test.py b/cirq-core/cirq/ops/linear_combinations_test.py index afc853ce1c57..6e2c3a647b5b 100644 --- a/cirq-core/cirq/ops/linear_combinations_test.py +++ b/cirq-core/cirq/ops/linear_combinations_test.py @@ -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( From 2c51eca391825a4ff2bcaafd7ec44644ae08a625 Mon Sep 17 00:00:00 2001 From: Pavol Juhas Date: Tue, 21 Mar 2023 15:04:57 -0700 Subject: [PATCH 5/5] Ensure compatible version of numpy in isolated_notebook_test.py (#6038) Keep numpy version in the isolated Python environment at 1.23 as numpy-1.24 is incompatible with cirq and numba. Fixes stuck notebook tests which import numba. Related to #5967 --- dev_tools/notebooks/isolated_notebook_test.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dev_tools/notebooks/isolated_notebook_test.py b/dev_tools/notebooks/isolated_notebook_test.py index 1ac7b03312c3..6ac26b5956bb 100644 --- a/dev_tools/notebooks/isolated_notebook_test.py +++ b/dev_tools/notebooks/isolated_notebook_test.py @@ -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', ]