Skip to content

Commit

Permalink
Small test fixes (#143)
Browse files Browse the repository at this point in the history
* 🐛 Fix imports

* 🧑‍💻 Prefix with Flow

Resolve pytest warning

* 🧑‍💻 Silence cotengra warning

* 🧑‍💻 Silence expected internal warnings
  • Loading branch information
EarlMilktea authored May 24, 2024
1 parent 2a032e4 commit ec4c582
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,7 @@ profile = "black"
py_version = 38
line_length = 120
wrap_length = 120

[tool.pytest.ini_options]
# Silence cotengra warning
filterwarnings = ["ignore:Couldn't import `kahypar`"]
29 changes: 15 additions & 14 deletions tests/test_gflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@

import networkx as nx
import pytest
from numpy.random import PCG64, Generator
from numpy.random import Generator

from graphix.gflow import find_flow, find_gflow, find_pauliflow, verify_flow, verify_gflow, verify_pauliflow
from tests.random_circuit import get_rand_circuit

if TYPE_CHECKING:
from collections.abc import Iterable, Iterator

from numpy.random import Generator
from numpy.random import PCG64


seed = 30

Expand Down Expand Up @@ -297,11 +298,11 @@ def generate_test_graphs() -> list[GraphForTest]:


if sys.version_info >= (3, 9):
TestCaseType = dict[str, dict[str, tuple[bool, dict[int, set[int]]]]]
FlowTestCaseType = dict[str, dict[str, tuple[bool, dict[int, set[int]]]]]
else:
TestCaseType = Dict[str, Dict[str, Tuple[bool, Dict[int, Set[int]]]]]
FlowTestCaseType = Dict[str, Dict[str, Tuple[bool, Dict[int, Set[int]]]]]

FLOW_TEST_CASES: TestCaseType = {
FLOW_TEST_CASES: FlowTestCaseType = {
"no measurement": {
"empty flow": (True, {}),
"measure output": (False, {1: {2}}),
Expand All @@ -319,7 +320,7 @@ def generate_test_graphs() -> list[GraphForTest]:
}


GFLOW_TEST_CASES: TestCaseType = {
GFLOW_TEST_CASES: FlowTestCaseType = {
"no measurement": {
"empty flow": (True, {}),
"measure output": (False, {1: {2}}),
Expand Down Expand Up @@ -351,7 +352,7 @@ def generate_test_graphs() -> list[GraphForTest]:
},
}

PAULIFLOW_TEST_CASES: TestCaseType = {
PAULIFLOW_TEST_CASES: FlowTestCaseType = {
"graph with no flow and no gflow but pauliflow, No.1": {
"correct pauliflow": (True, {0: {1}, 1: {4}, 2: {3}, 3: {2, 4}}),
"correct pauliflow 2": (True, {0: {1, 3}, 1: {3, 4}, 2: {3}, 3: {2, 3, 4}}),
Expand All @@ -373,15 +374,15 @@ def generate_test_graphs() -> list[GraphForTest]:
}

if sys.version_info >= (3, 9):
TestDataType = tuple[GraphForTest, tuple[bool, dict[int, set[int]]]]
FlowTestDataType = tuple[GraphForTest, tuple[bool, dict[int, set[int]]]]
else:
TestDataType = Tuple[GraphForTest, Tuple[bool, Dict[int, Set[int]]]]
FlowTestDataType = Tuple[GraphForTest, Tuple[bool, Dict[int, Set[int]]]]


def iterate_compatible(
graphs: Iterable[GraphForTest],
cases: TestCaseType,
) -> Iterator[TestDataType]:
cases: FlowTestCaseType,
) -> Iterator[FlowTestDataType]:
for g in graphs:
for k, v in cases.items():
if g.label != k:
Expand Down Expand Up @@ -446,7 +447,7 @@ def test_gflow(self, test_graph: GraphForTest) -> None:
assert test_graph.gflow_exist == (g is not None)

@pytest.mark.parametrize("data", iterate_compatible(generate_test_graphs(), FLOW_TEST_CASES))
def test_verify_flow(self, data: TestDataType) -> None:
def test_verify_flow(self, data: FlowTestDataType) -> None:
test_graph, test_case = data
expected, flow = test_case
valid = verify_flow(
Expand All @@ -459,7 +460,7 @@ def test_verify_flow(self, data: TestDataType) -> None:
assert expected == valid

@pytest.mark.parametrize("data", iterate_compatible(generate_test_graphs(), GFLOW_TEST_CASES))
def test_verify_gflow(self, data: TestDataType) -> None:
def test_verify_gflow(self, data: FlowTestDataType) -> None:
test_graph, test_case = data
expected, gflow = test_case

Expand All @@ -473,7 +474,7 @@ def test_verify_gflow(self, data: TestDataType) -> None:
assert expected == valid

@pytest.mark.parametrize("data", iterate_compatible(generate_test_graphs(), PAULIFLOW_TEST_CASES))
def test_verify_pauliflow(self, data: TestDataType) -> None:
def test_verify_pauliflow(self, data: FlowTestDataType) -> None:
test_graph, test_case = data
expected, pauliflow = test_case
angles = test_graph.meas_angles
Expand Down
3 changes: 3 additions & 0 deletions tests/test_noisy_density_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import numpy as np
import numpy.typing as npt
import pytest

from graphix import Circuit
from graphix.channels import KrausChannel, depolarising_channel, two_qubit_depolarising_channel
Expand Down Expand Up @@ -102,6 +103,7 @@ def rzpat(alpha: float) -> Pattern:
return circ.transpile().pattern

# test noiseless noisy vs noiseless
@pytest.mark.filterwarnings("ignore:Simulating using densitymatrix backend with no noise.")
def test_noiseless_noisy_hadamard(self) -> None:
hadamardpattern = self.hpat()
noiselessres = hadamardpattern.simulate_pattern(backend="densitymatrix")
Expand Down Expand Up @@ -218,6 +220,7 @@ def test_noisy_preparation_hadamard(self, fx_rng: Generator) -> None:
### Test rz gate

# test noiseless noisy vs noiseless
@pytest.mark.filterwarnings("ignore:Simulating using densitymatrix backend with no noise.")
def test_noiseless_noisy_rz(self, fx_rng: Generator) -> None:
alpha = fx_rng.random()
rzpattern = self.rzpat(alpha)
Expand Down
1 change: 1 addition & 0 deletions tests/test_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def test_minimize_space_with_gflow(self, fx_bg: PCG64, jumps: int, use_rustworkx
state_mbqc = pattern.simulate_pattern()
assert np.abs(np.dot(state_mbqc.flatten().conjugate(), state.flatten())) == pytest.approx(1)

@pytest.mark.filterwarnings("ignore:Simulating using densitymatrix backend with no noise.")
@pytest.mark.parametrize("backend", ["statevector", "densitymatrix", "tensornetwork"])
def test_empty_output_nodes(self, backend: Literal["statevector", "densitymatrix", "tensornetwork"]) -> None:
pattern = Pattern(input_nodes=[0])
Expand Down

0 comments on commit ec4c582

Please sign in to comment.