Skip to content

Commit

Permalink
Fix typing
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-mills-cqc committed Dec 8, 2024
1 parent 53fc103 commit 0ddce33
Show file tree
Hide file tree
Showing 17 changed files with 107 additions and 106 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ jobs:
pip install .[tests]
cd tests
- name: Run mypy
run: mypy -p qermit --warn-unused-ignores
run: mypy -p qermit tests --warn-unused-ignores
- name: Linting check
run: ruff check
- name: Format check
Expand Down
14 changes: 7 additions & 7 deletions tests/ccl_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import numpy as np # type: ignore
import numpy as np
from pytket import Circuit, OpType, Qubit
from pytket.extensions.qiskit import AerBackend # type: ignore
from pytket.pauli import Pauli, QubitPauliString # type: ignore
from pytket.predicates import CliffordCircuitPredicate # type: ignore
from pytket.extensions.qiskit import AerBackend
from pytket.pauli import Pauli, QubitPauliString
from pytket.predicates import CliffordCircuitPredicate
from pytket.utils import QubitPauliOperator

from qermit import ( # type: ignore
from qermit import (
AnsatzCircuit,
MitEx,
ObservableExperiment,
ObservableTracker,
SymbolsDict,
)
from qermit.clifford_noise_characterisation import ( # type: ignore
from qermit.clifford_noise_characterisation import (
gen_CDR_MitEx,
)
from qermit.clifford_noise_characterisation.ccl import ( # type: ignore
from qermit.clifford_noise_characterisation.ccl import (
ccl_likelihood_filtering_task_gen,
ccl_result_batching_task_gen,
ccl_state_task_gen,
Expand Down
8 changes: 4 additions & 4 deletions tests/cdr_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@

from typing import cast

import numpy as np # type: ignore
import numpy as np
from pytket import Qubit
from pytket.extensions.qiskit import AerBackend # type: ignore
from pytket.pauli import Pauli, QubitPauliString # type: ignore
from pytket.extensions.qiskit import AerBackend
from pytket.pauli import Pauli, QubitPauliString
from pytket.utils import QubitPauliOperator

from qermit.clifford_noise_characterisation.cdr_post import ( # type: ignore
from qermit.clifford_noise_characterisation.cdr_post import (
_PolyCDRCorrect,
cdr_calibration_task_gen,
cdr_correction_task_gen,
Expand Down
16 changes: 8 additions & 8 deletions tests/dfsc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,31 @@
import copy

from pytket import Circuit, Qubit
from pytket.circuit import fresh_symbol # type: ignore
from pytket.extensions.qiskit import AerBackend # type: ignore
from pytket.pauli import Pauli, QubitPauliString # type: ignore
from pytket.transform import Transform # type: ignore
from pytket.circuit import fresh_symbol
from pytket.extensions.qiskit import AerBackend
from pytket.pauli import Pauli, QubitPauliString
from pytket.transform import Transform
from pytket.utils import QubitPauliOperator

from qermit import ( # type: ignore
from qermit import (
MeasurementCircuit,
MitEx,
ObservableExperiment,
ObservableTracker,
SymbolsDict,
)
from qermit.clifford_noise_characterisation import ( # type: ignore
from qermit.clifford_noise_characterisation import (
gen_DFSC_MitEx,
)
from qermit.clifford_noise_characterisation.dfsc import ( # type: ignore
from qermit.clifford_noise_characterisation.dfsc import (
DFSC_characterisation_task_gen,
DFSC_circuit_task_gen,
DFSC_collater_task_gen,
DFSC_correction_task_gen,
get_clifford_mcs,
preparation_circuit_for_partition,
)
from qermit.taskgraph.mitex import get_basic_measurement_circuit # type: ignore
from qermit.taskgraph.mitex import get_basic_measurement_circuit

sym_0 = fresh_symbol("alpha")
sym_1 = fresh_symbol("beta")
Expand Down
6 changes: 3 additions & 3 deletions tests/frame_randomisation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@


from pytket import Circuit
from pytket.extensions.qiskit import AerBackend # type: ignore
from pytket.extensions.qiskit import AerBackend

from qermit.frame_randomisation import ( # type: ignore
from qermit.frame_randomisation import (
FrameRandomisation,
gen_Frame_Randomisation_MitRes,
)
from qermit.frame_randomisation.frame_randomisation import ( # type: ignore
from qermit.frame_randomisation.frame_randomisation import (
frame_randomisation_circuits_task_gen,
)

Expand Down
8 changes: 4 additions & 4 deletions tests/full_spam_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
# limitations under the License.


import numpy as np # type: ignore
import numpy as np
from pytket import Bit, Circuit, Qubit
from pytket.extensions.qiskit import AerBackend # type: ignore
from pytket.extensions.qiskit import AerBackend

from qermit.spam import ( # type: ignore
from qermit.spam import (
CorrectionMethod,
)
from qermit.spam.full_spam_correction import ( # type: ignore
from qermit.spam.full_spam_correction import (
gen_full_tomography_spam_characterisation_task,
gen_full_tomography_spam_circuits_task,
gen_full_tomography_spam_correction_task,
Expand Down
12 changes: 6 additions & 6 deletions tests/measurement_reduction_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@


from pytket import Bit, Circuit, Qubit
from pytket.extensions.qiskit import AerBackend # type: ignore
from pytket.partition import ( # type: ignore
from pytket.extensions.qiskit import AerBackend
from pytket.partition import (
GraphColourMethod,
PauliPartitionStrat,
)
from pytket.pauli import Pauli, QubitPauliString # type: ignore
from pytket.transform import CXConfigType # type: ignore
from pytket.pauli import Pauli, QubitPauliString
from pytket.transform import CXConfigType
from pytket.utils import QubitPauliOperator

from qermit import ( # type: ignore
from qermit import (
ObservableTracker,
SymbolsDict,
)
from qermit.taskgraph.measurement_reduction import ( # type: ignore
from qermit.taskgraph.measurement_reduction import (
gen_MeasurementReduction_MitEx,
measurement_reduction_task_gen,
)
Expand Down
10 changes: 5 additions & 5 deletions tests/mitex_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@
import copy

import pytest
from pytket.circuit import Circuit, OpType, Qubit, fresh_symbol # type: ignore
from pytket.extensions.qiskit import AerBackend # type: ignore
from pytket.pauli import Pauli, QubitPauliString # type: ignore
from pytket.circuit import Circuit, OpType, Qubit, fresh_symbol
from pytket.extensions.qiskit import AerBackend
from pytket.pauli import Pauli, QubitPauliString
from pytket.utils import QubitPauliOperator

from qermit import ( # type: ignore
from qermit import (
AnsatzCircuit,
CircuitShots,
MitEx,
ObservableExperiment,
ObservableTracker,
SymbolsDict,
)
from qermit.taskgraph.mitex import ( # type: ignore
from qermit.taskgraph.mitex import (
collate_circuit_shots_task_gen,
filter_observable_tracker_task_gen,
gen_compiled_shot_split_MitRes,
Expand Down
6 changes: 3 additions & 3 deletions tests/mitres_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@


from pytket import Circuit
from pytket.extensions.qiskit import AerBackend # type: ignore
from pytket.extensions.qiskit import AerBackend

from qermit import ( # type: ignore
from qermit import (
CircuitShots,
MitRes,
)
from qermit.taskgraph.mitres import ( # type: ignore
from qermit.taskgraph.mitres import (
backend_handle_task_gen,
backend_res_task_gen,
gen_shot_split_MitRes,
Expand Down
39 changes: 19 additions & 20 deletions tests/noise_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_to_ptm() -> None:

def test_from_ptm() -> None:
# Test that the error distribution to and from ptm is the same as the initial
distribution = {
distribution: dict[tuple[Pauli, ...], float] = {
(Pauli.X, Pauli.X): 0.1,
(Pauli.Y, Pauli.Z): 0.2,
(Pauli.Z, Pauli.X): 0.3,
Expand Down Expand Up @@ -150,7 +150,7 @@ def test_qermit_pauli_commute_coeff() -> None:
def test_noise_model_logical_error_propagation() -> None:
pytket_ciruit = Circuit(2).H(0).CX(0, 1).measure_all()

error_distribution_dict = {
error_distribution_dict: dict[tuple[Pauli, ...], float] = {
(Pauli.X, Pauli.I): 0.1,
}
error_distribution = ErrorDistribution(
Expand All @@ -163,23 +163,23 @@ def test_noise_model_logical_error_propagation() -> None:
pytket_ciruit, n_rand=10000
)

ideal_error = QubitPauliString(map={Qubit(0): Pauli.Z, Qubit(1): Pauli.X})
assert list(logical_distribution.distribution.keys()) == [ideal_error]
assert abs(logical_distribution.distribution[ideal_error] - 0.1) <= 0.01
ideal_error_one = QubitPauliString(map={Qubit(0): Pauli.Z, Qubit(1): Pauli.X})
assert list(logical_distribution.distribution.keys()) == [ideal_error_one]
assert abs(logical_distribution.distribution[ideal_error_one] - 0.1) <= 0.01

logical_distribution = noise_model.counter_propagate(
error_counter = noise_model.counter_propagate(
pytket_ciruit, n_counts=10000, direction=Direction.forward
)
ideal_error = QermitPauli.from_qubit_pauli_string(
ideal_error_two = QermitPauli.from_qubit_pauli_string(
QubitPauliString(map={Qubit(0): Pauli.X, Qubit(1): Pauli.I})
)
assert list(logical_distribution.keys()) == [ideal_error]
assert abs(logical_distribution[ideal_error] - 1000) <= 1
assert list(error_counter.keys()) == [ideal_error_two]
assert abs(error_counter[ideal_error_two] - 1000) <= 1


def test_error_distribution_utilities(tmp_path_factory) -> None:
# Test that the probabilities must be less than 1.
error_distribution_dict = {(Pauli.X, Pauli.I): 1.1}
error_distribution_dict: dict[tuple[Pauli, ...], float] = {(Pauli.X, Pauli.I): 1.1}
with pytest.raises(Exception):
error_distribution = ErrorDistribution(
distribution=error_distribution_dict,
Expand Down Expand Up @@ -290,12 +290,12 @@ def test_error_distribution_post_select() -> None:


def test_to_dict(tmpdir_factory) -> None:
error_distribution_dict_zzmax = {}
error_distribution_dict_zzmax: dict[tuple[Pauli, ...], float] = {}
error_distribution_dict_zzmax[(Pauli.X, Pauli.I)] = 0.0002
error_distribution_dict_zzmax[(Pauli.I, Pauli.X)] = 0.0002
error_distribution_dict_zzmax[(Pauli.I, Pauli.I)] = 0.9996

error_distribution_dict_cz = {}
error_distribution_dict_cz: dict[tuple[Pauli, ...], float] = {}
error_distribution_dict_cz[(Pauli.Z, Pauli.Z)] = 0.002
error_distribution_dict_cz[(Pauli.I, Pauli.Z)] = 0.002
error_distribution_dict_cz[(Pauli.I, Pauli.I)] = 0.996
Expand Down Expand Up @@ -339,7 +339,7 @@ def test_transpiler_backend() -> None:
circuit.ZZMax(0, 1).ZZMax(1, 2)
circuit.measure_all()

error_distribution_dict = {}
error_distribution_dict: dict[tuple[Pauli, ...], float] = {}
error_distribution_dict[(Pauli.X, Pauli.I)] = 0.002
error_distribution_dict[(Pauli.I, Pauli.X)] = 0.002

Expand All @@ -363,14 +363,13 @@ def test_transpiler_backend() -> None:

def test_pauli_error_transpile() -> None:
error_distribution_dict = {
error: 0 for error in product([Pauli.I, Pauli.X, Pauli.Y, Pauli.Z], repeat=2)
error: 0.0 for error in product([Pauli.I, Pauli.X, Pauli.Y, Pauli.Z], repeat=2)
}

error_rate = 0.5
error_distribution_dict[(Pauli.X, Pauli.I)] = error_rate
error_distribution_dict[(Pauli.I, Pauli.X)] = error_rate
error_distribution_dict[(Pauli.I, Pauli.I)] = 1 - 2 * error_rate
# error_distribution_dict[(Pauli.I, Pauli.I)] = 1 - 2*error_rate

error_distribution = ErrorDistribution(
error_distribution_dict, rng=np.random.default_rng(seed=2)
Expand All @@ -389,7 +388,7 @@ def test_pauli_error_transpile() -> None:
def test_noise_model() -> None:
mp.set_start_method("spawn", force=True)

error_distribution_dict = {}
error_distribution_dict: dict[tuple[Pauli, ...], float] = {}
error_rate = 0.5
error_distribution_dict[(Pauli.X, Pauli.I)] = error_rate
error_distribution_dict[(Pauli.I, Pauli.X)] = error_rate
Expand Down Expand Up @@ -454,7 +453,7 @@ def test_back_propagate_random_error() -> None:
qubit_list = cliff_circ.qubits

error_rate = 0.5
error_distribution_dict = {}
error_distribution_dict: dict[tuple[Pauli, ...], float] = {}
error_distribution_dict[(Pauli.X, Pauli.I)] = error_rate
error_distribution_dict[(Pauli.I, Pauli.X)] = error_rate
error_distribution_dict[(Pauli.I, Pauli.I)] = 1 - 2 * error_rate
Expand Down Expand Up @@ -483,7 +482,7 @@ def test_effective_error_distribution() -> None:
qubits = cliff_circ.qubits

error_rate = 0.5
error_distribution_dict = {}
error_distribution_dict: dict[tuple[Pauli, ...], float] = {}
error_distribution_dict[(Pauli.X, Pauli.I)] = error_rate
error_distribution_dict[(Pauli.I, Pauli.X)] = error_rate

Expand All @@ -493,13 +492,13 @@ def test_effective_error_distribution() -> None:
)
noise_model = NoiseModel({OpType.CZ: error_distribution})

error_distribution = noise_model.get_effective_pre_error_distribution(
logical_error_distribution = noise_model.get_effective_pre_error_distribution(
cliff_circ, n_rand=10000
)

assert all(
abs(count - 2500) < 100
for count in error_distribution.pauli_error_counter.values()
for count in logical_error_distribution.pauli_error_counter.values()
)

cliff_circ = Circuit()
Expand Down
16 changes: 8 additions & 8 deletions tests/pec_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@
import pytest
from pytket import Circuit, OpType, Qubit
from pytket.extensions.qiskit import (
AerBackend, # type: ignore
IBMQEmulatorBackend, # type: ignore
AerBackend,
IBMQEmulatorBackend,
)
from pytket.pauli import Pauli, QubitPauliString # type: ignore
from pytket.predicates import CliffordCircuitPredicate, GateSetPredicate # type: ignore
from pytket.pauli import Pauli, QubitPauliString
from pytket.predicates import CliffordCircuitPredicate, GateSetPredicate
from pytket.utils import QubitPauliOperator
from qiskit_ibm_provider import IBMProvider
from qiskit_ibm_provider import IBMProvider # type: ignore

from qermit import ( # type: ignore
from qermit import (
AnsatzCircuit,
ObservableExperiment,
ObservableTracker,
SymbolsDict,
)
from qermit.probabilistic_error_cancellation import ( # type: ignore
from qermit.probabilistic_error_cancellation import (
gen_PEC_learning_based_MitEx,
)
from qermit.probabilistic_error_cancellation.pec_learning_based import ( # type: ignore
from qermit.probabilistic_error_cancellation.pec_learning_based import (
collate_results_task_gen,
gen_get_clifford_training_set,
gen_get_noisy_circuits,
Expand Down
10 changes: 5 additions & 5 deletions tests/spam_methods_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@


from pytket import Circuit, Qubit
from pytket.architecture import Architecture # type: ignore
from pytket.circuit import Node # type: ignore
from pytket.extensions.qiskit import AerBackend # type: ignore
from pytket.architecture import Architecture
from pytket.circuit import Node
from pytket.extensions.qiskit import AerBackend

from qermit import CircuitShots
from qermit.noise_model import MockQuantinuumBackend # type: ignore
from qermit.spam import ( # type: ignore
from qermit.noise_model import MockQuantinuumBackend
from qermit.spam import (
CorrectionMethod,
gen_FullyCorrelated_SPAM_MitRes,
gen_UnCorrelated_SPAM_MitRes,
Expand Down
Loading

0 comments on commit 0ddce33

Please sign in to comment.