From da545572e2885f39c296a9ed6f8ba81f84fa453e Mon Sep 17 00:00:00 2001 From: rht Date: Mon, 6 Jan 2020 17:04:36 -0500 Subject: [PATCH] 4th commit --- pyquil/experiment/_group.py | 1 + pyquil/experiment/_main.py | 10 +++++++--- pyquil/experiment/_memory.py | 5 +++-- pyquil/experiment/_setting.py | 2 +- pyquil/simulation/_numpy.py | 1 + pyquil/simulation/_reference.py | 1 + 6 files changed, 14 insertions(+), 6 deletions(-) diff --git a/pyquil/experiment/_group.py b/pyquil/experiment/_group.py index e0c9389b4..1103bbe1a 100644 --- a/pyquil/experiment/_group.py +++ b/pyquil/experiment/_group.py @@ -189,6 +189,7 @@ def _max_weight_operator(ops: Iterable[PauliTerm]) -> Union[None, PauliTerm]: mapping = dict() # type: Dict[int, str] for op in ops: for idx, op_str in op: + assert isinstance(idx, int) if idx in mapping: if mapping[idx] != op_str: return None diff --git a/pyquil/experiment/_main.py b/pyquil/experiment/_main.py index a1b1ec842..ff790ff34 100644 --- a/pyquil/experiment/_main.py +++ b/pyquil/experiment/_main.py @@ -286,7 +286,7 @@ def get_meas_qubits(self) -> List[int]: """ meas_qubits: Set[int] = set() for settings in self: - meas_qubits.update(settings[0].out_operator.get_qubits()) + meas_qubits.update(cast(List[int], settings[0].out_operator.get_qubits())) return sorted(meas_qubits) def get_meas_registers(self, qubits: Optional[Sequence[int]] = None) -> List[int]: @@ -377,8 +377,12 @@ def build_setting_memory_map(self, setting: ExperimentSetting) -> Dict[str, List """ meas_qubits = self.get_meas_qubits() - in_pt = PauliTerm.from_list([(op, meas_qubits.index(q)) for q, op in setting.in_operator]) - out_pt = PauliTerm.from_list([(op, meas_qubits.index(q)) for q, op in setting.out_operator]) + in_pt = PauliTerm.from_list( + [(op, meas_qubits.index(cast(int, q))) for q, op in setting.in_operator] + ) + out_pt = PauliTerm.from_list( + [(op, meas_qubits.index(cast(int, q))) for q, op in setting.out_operator] + ) preparation_map = pauli_term_to_preparation_memory_map(in_pt) measurement_map = pauli_term_to_measurement_memory_map(out_pt) diff --git a/pyquil/experiment/_memory.py b/pyquil/experiment/_memory.py index ddf693df2..0a54e58d8 100644 --- a/pyquil/experiment/_memory.py +++ b/pyquil/experiment/_memory.py @@ -15,7 +15,7 @@ ############################################################################## import itertools -from typing import Dict, List, Tuple +from typing import Dict, List, Tuple, cast import numpy as np @@ -102,7 +102,7 @@ def pauli_term_to_euler_memory_map( gamma_label = f"{prefix}_{suffix_gamma}" # assume the pauli indices are equivalent to the memory region - memory_size = max(term.get_qubits()) + 1 + memory_size = max(cast(List[int], term.get_qubits())) + 1 memory_map = { alpha_label: [0.0] * memory_size, @@ -113,6 +113,7 @@ def pauli_term_to_euler_memory_map( tuples = {"X": tuple_x, "Y": tuple_y, "Z": tuple_z, "I": tuple_z} for qubit, operator in term: + assert isinstance(qubit, int) if operator not in tuples: raise ValueError(f"Unknown operator {operator}") memory_map[alpha_label][qubit] = tuples[operator][0] diff --git a/pyquil/experiment/_setting.py b/pyquil/experiment/_setting.py index 37c1bda56..05186a7bb 100644 --- a/pyquil/experiment/_setting.py +++ b/pyquil/experiment/_setting.py @@ -123,7 +123,7 @@ def _pauli_to_product_state(in_state: PauliTerm) -> TensorProductState: else: return TensorProductState( [ - _OneQState(label=pauli_label, index=0, qubit=qubit) + _OneQState(label=pauli_label, index=0, qubit=cast(int, qubit)) for qubit, pauli_label in in_state._ops.items() ] ) diff --git a/pyquil/simulation/_numpy.py b/pyquil/simulation/_numpy.py index bd39c105e..b27525634 100644 --- a/pyquil/simulation/_numpy.py +++ b/pyquil/simulation/_numpy.py @@ -166,6 +166,7 @@ def _term_expectation(wf: np.ndarray, term: PauliTerm) -> Any: # Computes wf2 = wf for qubit_i, op_str in term._ops.items(): + assert isinstance(qubit_i, int) # Re-use QUANTUM_GATES since it has X, Y, Z op_mat = QUANTUM_GATES[op_str] wf2 = targeted_tensordot(gate=op_mat, wf=wf2, wf_target_inds=[qubit_i]) diff --git a/pyquil/simulation/_reference.py b/pyquil/simulation/_reference.py index 437a3c109..30f1f83ad 100644 --- a/pyquil/simulation/_reference.py +++ b/pyquil/simulation/_reference.py @@ -30,6 +30,7 @@ def _term_expectation(wf: np.ndarray, term: PauliTerm, n_qubits: int) -> Any: # Computes wf2 = wf for qubit_i, op_str in term._ops.items(): + assert isinstance(qubit_i, int) # Re-use QUANTUM_GATES since it has X, Y, Z op_mat = QUANTUM_GATES[op_str] op_mat = lifted_gate_matrix(matrix=op_mat, qubit_inds=[qubit_i], n_qubits=n_qubits)