Skip to content

Commit

Permalink
Use float not int for QPO coefficient. (#1487)
Browse files Browse the repository at this point in the history
  • Loading branch information
cqc-alec authored Jul 9, 2024
1 parent a47578e commit 1e59d40
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 107 deletions.
3 changes: 3 additions & 0 deletions pytket/mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ ignore_errors = True
[mypy-IPython.*]
ignore_missing_imports = True
ignore_errors = True

[mypy-sympy.*]
ignore_missing_imports = True
14 changes: 7 additions & 7 deletions pytket/pytket/qasm/qasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,9 +875,7 @@ def gdef(self, tree: List) -> None:
comparison_circ = _get_gate_circuit(
PARAM_EXTRA_COMMANDS[gate],
qubit_args,
[
Symbol("param" + str(index) + "/pi") for index in range(len(symbols)) # type: ignore
],
[Symbol("param" + str(index) + "/pi") for index in range(len(symbols))],
)
# checks that each command has same string
existing_op = all(
Expand Down Expand Up @@ -1203,7 +1201,7 @@ def _get_gate_circuit(
for q in qubits:
gate_circ.add_qubit(q)
if symbols:
exprs = [symbol.as_expr() for symbol in symbols] # type: ignore
exprs = [symbol.as_expr() for symbol in symbols]
gate_circ.add_gate(optype, exprs, unitids)
else:
gate_circ.add_gate(optype, unitids)
Expand Down Expand Up @@ -1370,8 +1368,10 @@ def make_gate_definition(
if n_params is not None:
# need to add parameters to gate definition
s += "("
symbols = [Symbol("param" + str(index) + "/pi") for index in range(n_params)] # type: ignore
symbols_header = [Symbol("param" + str(index)) for index in range(n_params)] # type: ignore
symbols = [
Symbol("param" + str(index) + "/pi") for index in range(n_params)
]
symbols_header = [Symbol("param" + str(index)) for index in range(n_params)]
for symbol in symbols_header[:-1]:
s += symbol.name + ", "
s += symbols_header[-1].name + ") "
Expand Down Expand Up @@ -1580,7 +1580,7 @@ def add_zzphase(self, param: Union[float, Expr], args: List[UnitID]) -> None:
# that 0 <= param < 4
if param > 1:
# first get in to 0 <= param < 2 range
param = Decimal(str(param)) % Decimal("2") # type: ignore
param = Decimal(str(param)) % Decimal("2")
# then flip 1 <= param < 2 range into
# -1 <= param < 0
if param > 1:
Expand Down
27 changes: 12 additions & 15 deletions pytket/pytket/utils/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@


CoeffTypeAccepted = Union[int, float, complex, Expr]
CoeffTypeConverted = Union[Expr]

if TYPE_CHECKING:
from scipy.sparse import csc_matrix


def _coeff_convert(coeff: Union[CoeffTypeAccepted, str]) -> CoeffTypeConverted:
sympy_val = sympify(coeff) # type: ignore
def _coeff_convert(coeff: Union[CoeffTypeAccepted, str]) -> Expr:
sympy_val = sympify(coeff)
if not isinstance(sympy_val, Expr):
raise ValueError("Unsupported value for QubitPauliString coefficient")
return sympy_val
Expand Down Expand Up @@ -65,7 +64,7 @@ def __init__(
self,
dictionary: Optional[Dict[QubitPauliString, CoeffTypeAccepted]] = None,
) -> None:
self._dict: Dict[QubitPauliString, CoeffTypeConverted] = dict()
self._dict: Dict[QubitPauliString, Expr] = dict()
if dictionary:
for key, value in dictionary.items():
self._dict[key] = _coeff_convert(value)
Expand All @@ -74,12 +73,10 @@ def __init__(
def __repr__(self) -> str:
return self._dict.__repr__()

def __getitem__(self, key: QubitPauliString) -> CoeffTypeConverted:
def __getitem__(self, key: QubitPauliString) -> Expr:
return self._dict[key]

def get(
self, key: QubitPauliString, default: CoeffTypeAccepted
) -> CoeffTypeConverted:
def get(self, key: QubitPauliString, default: CoeffTypeAccepted) -> Expr:
return self._dict.get(key, _coeff_convert(default))

def __setitem__(self, key: QubitPauliString, value: CoeffTypeAccepted) -> None:
Expand All @@ -94,10 +91,10 @@ def __setitem__(self, key: QubitPauliString, value: CoeffTypeAccepted) -> None:
self._dict[key] = _coeff_convert(value)
self._all_qubits.update(key.map.keys())

def __getstate__(self) -> Dict[QubitPauliString, CoeffTypeConverted]:
def __getstate__(self) -> Dict[QubitPauliString, Expr]:
return self._dict

def __setstate__(self, _dict: Dict[QubitPauliString, CoeffTypeConverted]) -> None:
def __setstate__(self, _dict: Dict[QubitPauliString, Expr]) -> None:
# values assumed to be already sympified
self._dict = _dict
self._collect_qubits()
Expand Down Expand Up @@ -202,7 +199,7 @@ def __rmul__(self, multiplier: CoeffTypeAccepted) -> "QubitPauliOperator":
:return: Product operator
:rtype: QubitPauliOperator
"""
return self * _coeff_convert(multiplier)
return self.__mul__(_coeff_convert(multiplier))

@property
def all_qubits(self) -> Set[Qubit]:
Expand All @@ -220,7 +217,7 @@ def subs(self, symbol_dict: Dict[Symbol, complex]) -> None:
:type symbol_dict: Dict[Symbol, complex]
"""
for key, value in self._dict.items():
self._dict[key] = value.subs(symbol_dict) # type: ignore
self._dict[key] = value.subs(symbol_dict)

def to_list(self) -> List[Dict[str, Any]]:
"""Generate a list serialized representation of QubitPauliOperator,
Expand All @@ -234,7 +231,7 @@ def to_list(self) -> List[Dict[str, Any]]:
try:
coeff = complex_to_list(complex(v))
except TypeError:
assert type(Expr(v)) == Expr # type: ignore
assert type(Expr(v)) == Expr
coeff = str(v)
ret.append(
{
Expand All @@ -256,7 +253,7 @@ def from_list(cls, pauli_list: List[Dict[str, Any]]) -> "QubitPauliOperator":
def get_qps(obj: Dict[str, Any]) -> QubitPauliString:
return QubitPauliString.from_list(obj["string"])

def get_coeff(obj: Dict[str, Any]) -> CoeffTypeConverted:
def get_coeff(obj: Dict[str, Any]) -> Expr:
coeff = obj["coefficient"]
if type(coeff) is str:
return _coeff_convert(coeff)
Expand Down Expand Up @@ -381,7 +378,7 @@ def compress(self, abs_tol: float = 1e-10) -> None:

to_delete = []
for key, value in self._dict.items():
placeholder = value.subs({s: 1 for s in value.free_symbols}) # type: ignore
placeholder = value.subs({s: 1 for s in value.free_symbols})
if abs(re(placeholder)) <= abs_tol:
if abs(im(placeholder)) <= abs_tol:
to_delete.append(key)
Expand Down
Loading

0 comments on commit 1e59d40

Please sign in to comment.