Created
November 18, 2019 16:10
-
-
Save appleby/294bcd7075944fe4b0b443fc77442cd0 to your computer and use it in GitHub Desktop.
Possible patch for paulis.py to resolve mypy + Number difficulties
This file contains hidden or 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
| diff --git a/pyquil/paulis.py b/pyquil/paulis.py | |
| index fc312677..c167a279 100644 | |
| --- a/pyquil/paulis.py | |
| +++ b/pyquil/paulis.py | |
| @@ -89,7 +89,7 @@ class PauliTerm(object): | |
| if not _valid_qubit(index): | |
| raise ValueError(f"{index} is not a valid qubit") | |
| self._ops[index] = op | |
| - if not isinstance(coefficient, Number): | |
| + if not isinstance(coefficient, (Number, int, float, complex)): | |
| raise ValueError("coefficient of PauliTerm must be a Number.") | |
| self.coefficient = complex(coefficient) | |
| @@ -214,7 +214,7 @@ class PauliTerm(object): | |
| :returns: The product of this PauliTerm and term. | |
| :rtype: PauliTerm or PauliSum | |
| """ | |
| - if isinstance(term, Number): | |
| + if isinstance(term, (Number, int, float, complex)): | |
| return term_with_coeff(self, self.coefficient * term) | |
| elif isinstance(term, PauliSum): | |
| return (PauliSum([self]) * term).simplify() | |
| @@ -227,14 +227,14 @@ class PauliTerm(object): | |
| return term_with_coeff(new_term, new_term.coefficient * new_coeff) | |
| - def __rmul__(self, other: Number) -> 'PauliTerm': | |
| + def __rmul__(self, other: ExpressionValueDesignator) -> 'PauliTerm': | |
| """Multiplies this PauliTerm with another object, probably a number. | |
| :param other: A number or PauliTerm to multiply by | |
| :returns: A new PauliTerm | |
| :rtype: PauliTerm | |
| """ | |
| - assert isinstance(other, Number) | |
| + assert isinstance(other, (Number, int, float, complex)) | |
| return self * other | |
| def __pow__(self, power: int) -> 'PauliTerm': | |
| @@ -263,7 +263,7 @@ class PauliTerm(object): | |
| :returns: A PauliSum object representing the sum of this PauliTerm and other | |
| :rtype: PauliSum | |
| """ | |
| - if isinstance(other, Number): | |
| + if isinstance(other, (Number, int, float, complex)): | |
| return self + PauliTerm("I", 0, other) | |
| elif isinstance(other, PauliSum): | |
| return other + self | |
| @@ -271,17 +271,17 @@ class PauliTerm(object): | |
| new_sum = PauliSum([self, other]) | |
| return new_sum.simplify() | |
| - def __radd__(self, other: Number) -> 'PauliTerm': | |
| + def __radd__(self, other: ExpressionValueDesignator) -> 'PauliTerm': | |
| """Adds this PauliTerm with a Number. | |
| :param other: A PauliTerm object or a Number | |
| :returns: A new PauliTerm | |
| :rtype: PauliTerm | |
| """ | |
| - assert isinstance(other, Number) | |
| + assert isinstance(other, (Number, int, float, complex)) | |
| return PauliTerm("I", 0, other) + self | |
| - def __sub__(self, other: Union['PauliTerm', Number]) -> 'PauliSum': | |
| + def __sub__(self, other: Union['PauliTerm', ExpressionValueDesignator]) -> 'PauliSum': | |
| """Subtracts a PauliTerm from this one. | |
| :param other: A PauliTerm object or a Number | |
| @@ -290,7 +290,7 @@ class PauliTerm(object): | |
| """ | |
| return self + -1. * other | |
| - def __rsub__(self, other: Union['PauliTerm', Number]) -> 'PauliSum': | |
| + def __rsub__(self, other: Union['PauliTerm', ExpressionValueDesignator]) -> 'PauliSum': | |
| """Subtracts this PauliTerm from a Number or PauliTerm. | |
| :param other: A PauliTerm object or a Number | |
| @@ -348,7 +348,7 @@ class PauliTerm(object): | |
| for op, index in terms_list: | |
| if op != "I": | |
| pterm._ops[index] = op | |
| - if not isinstance(coefficient, Number): | |
| + if not isinstance(coefficient, (Number, int, float, complex)): | |
| raise ValueError("coefficient of PauliTerm must be a Number.") | |
| pterm.coefficient = complex(coefficient) | |
| return pterm | |
| @@ -484,7 +484,7 @@ def sZ(q: int) -> PauliTerm: | |
| return PauliTerm("Z", q) | |
| -def term_with_coeff(term: PauliTerm, coeff: Number) -> PauliTerm: | |
| +def term_with_coeff(term: PauliTerm, coeff: ExpressionValueDesignator) -> PauliTerm: | |
| """ | |
| Change the coefficient of a PauliTerm. | |
| @@ -493,7 +493,7 @@ def term_with_coeff(term: PauliTerm, coeff: Number) -> PauliTerm: | |
| :returns: A new PauliTerm that duplicates term but sets coeff | |
| :rtype: PauliTerm | |
| """ | |
| - if not isinstance(coeff, Number): | |
| + if not isinstance(coeff, (Number, int, float, complex)): | |
| raise ValueError("coeff must be a Number") | |
| new_pauli = term.copy() | |
| # We cast to a complex number to ensure that internally the coefficients remain compatible. | |
| @@ -566,7 +566,7 @@ class PauliSum(object): | |
| :return: A new PauliSum object given by the multiplication. | |
| :rtype: PauliSum | |
| """ | |
| - if not isinstance(other, (Number, PauliTerm, PauliSum)): | |
| + if not isinstance(other, (Number, int, float, complex, PauliTerm, PauliSum)): | |
| raise ValueError("Cannot multiply PauliSum by term that is not a Number, PauliTerm, or" | |
| "PauliSum") | |
| elif isinstance(other, PauliSum): | |
| @@ -577,7 +577,7 @@ class PauliSum(object): | |
| new_sum = PauliSum(new_terms) | |
| return new_sum.simplify() | |
| - def __rmul__(self, other: Number) -> 'PauliSum': | |
| + def __rmul__(self, other: ExpressionValueDesignator) -> 'PauliSum': | |
| """ | |
| Multiples together this PauliSum with PauliSum, PauliTerm or Number objects. The new term | |
| is then simplified according to the Pauli Algebra rules. | |
| @@ -586,7 +586,7 @@ class PauliSum(object): | |
| :return: A new PauliSum object given by the multiplication. | |
| :rtype: PauliSum | |
| """ | |
| - assert isinstance(other, Number) | |
| + assert isinstance(other, (Number, int, float, complex)) | |
| new_terms = [term.copy() for term in self.terms] | |
| for term in new_terms: | |
| term.coefficient *= other | |
| @@ -628,7 +628,7 @@ class PauliSum(object): | |
| """ | |
| if isinstance(other, PauliTerm): | |
| other = PauliSum([other]) | |
| - elif isinstance(other, Number): | |
| + elif isinstance(other, (Number, int, float, complex)): | |
| other = PauliSum([other * ID()]) | |
| new_terms = [term.copy() for term in self.terms] | |
| new_terms.extend(other.terms) | |
| @@ -644,7 +644,7 @@ class PauliSum(object): | |
| :return: A new PauliSum object given by the addition. | |
| :rtype: PauliSum | |
| """ | |
| - assert isinstance(other, Number) | |
| + assert isinstance(other, (Number, int, float, complex)) | |
| return self + other | |
| def __sub__(self, other: PauliT) -> 'PauliSum': |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment