diff --git a/graphix/parameter.py b/graphix/parameter.py index af41f9fc4..2d6c50783 100644 --- a/graphix/parameter.py +++ b/graphix/parameter.py @@ -317,7 +317,11 @@ def __hash__(self) -> int: ExpressionOrSupportsFloat = Expression | SupportsFloat -ExpressionOrSupportsComplex = Expression | SupportsComplex +# `ExpressionOrSupportsComplex` is based on +# `ExpressionOrSupportsFloat` rather than `Expression`, because `int` +# and `float` implement `SupportsFloat` but not `SupportsComplex`, +# even though `complex(int)` and `complex(float)` are valid. +ExpressionOrSupportsComplex = ExpressionOrSupportsFloat | SupportsComplex T = TypeVar("T") diff --git a/graphix/sim/density_matrix.py b/graphix/sim/density_matrix.py index 2c7882280..7b77fe6ec 100644 --- a/graphix/sim/density_matrix.py +++ b/graphix/sim/density_matrix.py @@ -10,7 +10,7 @@ import math from collections.abc import Collection, Iterable from dataclasses import dataclass -from typing import TYPE_CHECKING, SupportsComplex +from typing import TYPE_CHECKING import numpy as np from typing_extensions import override @@ -25,6 +25,7 @@ if TYPE_CHECKING: from collections.abc import Mapping, Sequence + from typing import SupportsComplex, SupportsFloat from graphix.noise_models.noise_model import Noise from graphix.parameter import ExpressionOrSupportsFloat, Parameter @@ -82,7 +83,7 @@ def check_size_consistency(mat: Matrix) -> None: if len(input_list) != 0 and isinstance(input_list[0], Iterable): def get_row( - item: Iterable[ExpressionOrSupportsComplex] | State | Expression | SupportsComplex, + item: Iterable[ExpressionOrSupportsComplex] | State | Expression | SupportsFloat | SupportsComplex, ) -> list[ExpressionOrSupportsComplex]: if isinstance(item, Iterable): return list(item) diff --git a/pyproject.toml b/pyproject.toml index d79ba4f90..41a60e0df 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -142,13 +142,6 @@ exclude = [ '^examples/qnn\.py$', '^examples/rotation\.py$', '^examples/tn_simulation\.py$', - '^graphix/linalg\.py$', - '^tests/test_density_matrix\.py$', - '^tests/test_linalg\.py$', - '^tests/test_noisy_density_matrix\.py$', - '^tests/test_statevec\.py$', - '^tests/test_statevec_backend\.py$', - '^tests/test_tnsim\.py$', ] follow_imports = "silent" follow_untyped_imports = true # required for qiskit, requires mypy >=1.14 @@ -167,14 +160,6 @@ exclude = [ "examples/qnn.py", "examples/rotation.py", "examples/tn_simulation.py", - "graphix/linalg.py", - "tests/test_density_matrix.py", - "tests/test_linalg.py", - "tests/test_noisy_density_matrix.py", - "tests/test_statevec.py", - "tests/test_statevec_backend.py", - "tests/test_tnsim.py", - "tests/test_transpiler.py", ] reportUnknownArgumentType = "information" reportUnknownLambdaType = "information" diff --git a/stubs/quimb/__init__.pyi b/stubs/quimb/__init__.pyi index e69de29bb..f0c82a25d 100644 --- a/stubs/quimb/__init__.pyi +++ b/stubs/quimb/__init__.pyi @@ -0,0 +1,8 @@ +from collections.abc import Iterator +from typing import Generic, TypeVar + +_T = TypeVar("_T") + +class oset(Generic[_T]): # noqa: N801 + def __iter__(self) -> Iterator[_T]: ... + def popleft(self) -> _T: ... diff --git a/stubs/quimb/tensor/__init__.pyi b/stubs/quimb/tensor/__init__.pyi index 9ab686443..8366b5182 100644 --- a/stubs/quimb/tensor/__init__.pyi +++ b/stubs/quimb/tensor/__init__.pyi @@ -4,6 +4,7 @@ from typing import Literal import numpy as np import numpy.typing as npt from cotengra.oe import PathOptimizer +from quimb import oset from typing_extensions import Self class Tensor: @@ -30,6 +31,7 @@ class Tensor: class TensorNetwork: tensor_map: dict[str, Tensor] + tag_map: dict[str, str] def __init__( self, @@ -40,10 +42,10 @@ class TensorNetwork: ) -> None: ... def _get_tids_from_tags( self, tags: Sequence[str] | str | int | None, which: Literal["all", "any", "!all", "!any"] = "all" - ) -> set[str]: ... + ) -> oset[str]: ... def _get_tids_from_inds( self, inds: Sequence[str] | str | int | None, which: Literal["all", "any", "!all", "!any"] = "all" - ) -> set[str]: ... + ) -> oset[str]: ... def __iter__(self) -> Iterator[Tensor]: ... def add( self, diff --git a/tests/test_density_matrix.py b/tests/test_density_matrix.py index 2fba05abf..f61d2ea73 100644 --- a/tests/test_density_matrix.py +++ b/tests/test_density_matrix.py @@ -10,6 +10,7 @@ import pytest import graphix.random_objects as randobj +from graphix import command from graphix.branch_selector import RandomBranchSelector from graphix.channels import KrausChannel, dephasing_channel, depolarising_channel from graphix.fundamentals import ANGLE_PI, Plane @@ -44,16 +45,9 @@ def test_init_without_data_fail(self) -> None: DensityMatrix(nqubit=-2) def test_init_with_invalid_data_fail(self, fx_rng: Generator) -> None: - with pytest.raises(ValueError): - DensityMatrix("hello") - with pytest.raises(TypeError): - DensityMatrix(1) # Length is not a power of two with pytest.raises(ValueError): - DensityMatrix([1, 2, [3]]) - # setting an array element with a sequence (numpy error) - with pytest.raises(ValueError): - DensityMatrix([1, 2, [3], 4]) + DensityMatrix([1, 2, 3]) # check with hermitian dm but not unit trace with pytest.raises(ValueError): @@ -111,7 +105,9 @@ def test_init_with_state_sucess(self, fx_rng: Generator) -> None: states = [PlanarState(plane=i, angle=j) for i, j in zip(rand_planes, rand_angles, strict=True)] vec = Statevec(data=states) # flattens input! - expected_dm = np.outer(vec.psi, vec.psi.conj()) + expected_dm = np.outer( + vec.psi.astype(np.complex128, copy=False), vec.psi.conj().astype(np.complex128, copy=False) + ) # input with a State object dm = DensityMatrix(data=states) @@ -140,7 +136,9 @@ def test_init_with_statevec_sucess(self, fx_rng: Generator) -> None: states = [PlanarState(plane=i, angle=j) for i, j in zip(rand_planes, rand_angles, strict=True)] vec = Statevec(data=states) # flattens input! - expected_dm = np.outer(vec.psi, vec.psi.conj()) + expected_dm = np.outer( + vec.psi.astype(np.complex128, copy=False), vec.psi.conj().astype(np.complex128, copy=False) + ) # input with a Statevec object dm = DensityMatrix(data=vec) @@ -167,7 +165,9 @@ def test_init_with_densitymatrix_sucess(self, fx_rng: Generator) -> None: print("planes", rand_planes) states = [PlanarState(plane=i, angle=j) for i, j in zip(rand_planes, rand_angles, strict=True)] vec = Statevec(data=states) - expected_dm = np.outer(vec.psi, vec.psi.conj()) + expected_dm = np.outer( + vec.psi.astype(np.complex128, copy=False), vec.psi.conj().astype(np.complex128, copy=False) + ) # input with a huge density matrix dm_list = [state.get_densitymatrix() for state in states] @@ -202,7 +202,9 @@ def test_evolve_single_success(self, fx_rng: Generator) -> None: for i in range(n): sv = Statevec(nqubit=n) sv.evolve_single(op, i) - expected_density_matrix = np.outer(sv.psi, sv.psi.conj()) + expected_density_matrix = np.outer( + sv.psi.astype(np.complex128, copy=False), sv.psi.conj().astype(np.complex128, copy=False) + ) dm = DensityMatrix(nqubit=n) dm.evolve_single(op, i) assert np.allclose(dm.rho, expected_density_matrix) @@ -250,13 +252,6 @@ def test_expectation_single_success(self, fx_rng: Generator) -> None: # watch out ordering. Expval unitary is cpx so psi1 on the right to match DM. assert np.allclose(np.dot(psi.conjugate(), psi1), dm.expectation_single(op, target_qubit)) - def test_tensor_fail(self) -> None: - dm = DensityMatrix(nqubit=1) - with pytest.raises(ValueError): - dm.tensor("hello") - with pytest.raises(TypeError): - dm.tensor(1) - @pytest.mark.parametrize("n", range(3)) def test_tensor_without_data_success(self, n: int) -> None: dm_a = DensityMatrix(nqubit=n) @@ -502,15 +497,15 @@ def test_evolve_fail(self, fx_rng: Generator) -> None: # check not square matrix with pytest.raises(ValueError): - dm.evolve(fx_rng.uniform(size=(2, 3)), (0, 1)) + dm.evolve(fx_rng.uniform(size=(2, 3)).astype(np.complex128), (0, 1)) # check higher dimensional matrix with pytest.raises(ValueError): - dm.evolve(fx_rng.uniform(size=(2, 2, 3)), (0, 1)) + dm.evolve(fx_rng.uniform(size=(2, 2, 3)).astype(np.complex128), (0, 1)) # check square but with incorrect dimension (non-qubit type) with pytest.raises(ValueError): - dm.evolve(fx_rng.uniform(size=(5, 5)), (0, 1)) + dm.evolve(fx_rng.uniform(size=(5, 5)).astype(np.complex128), (0, 1)) # TODO: the test for normalization is done at initialization with data. # Now check that all operations conserve the norm. @@ -920,51 +915,44 @@ def test_entangle_nodes(self) -> None: @pytest.mark.parametrize("pr_calc", [False, True]) def test_measure(self, pr_calc: bool) -> None: - circ = Circuit(1) - circ.rx(0, ANGLE_PI / 2) - pattern = circ.transpile().pattern - measure_method = DefaultMeasureMethod() backend = DensityMatrixBackend(branch_selector=RandomBranchSelector(pr_calc=pr_calc)) - backend.add_nodes(pattern.input_nodes) + backend.add_nodes([0]) backend.add_nodes([1, 2]) backend.entangle_nodes((0, 1)) backend.entangle_nodes((1, 2)) - measure_method.measure(backend, pattern[-4]) + measure_method.measure(backend, command.M(0)) expected_matrix_1 = np.kron(np.array([[1, 0], [0, 0]]), np.ones((2, 2)) / 2) expected_matrix_2 = np.kron(np.array([[0, 0], [0, 1]]), np.array([[0.5, -0.5], [-0.5, 0.5]])) assert np.allclose(backend.state.rho, expected_matrix_1) or np.allclose(backend.state.rho, expected_matrix_2) def test_correct_byproduct(self) -> None: - circ = Circuit(1) - circ.rx(0, ANGLE_PI / 2) - pattern = circ.transpile().pattern measure_method = DefaultMeasureMethod() - backend = DensityMatrixBackend() - backend.add_nodes(pattern.input_nodes) + dm_backend = DensityMatrixBackend() + dm_backend.add_nodes([0]) # node 0 initialized in Backend - backend.add_nodes([1, 2]) - backend.entangle_nodes((0, 1)) - backend.entangle_nodes((1, 2)) - measure_method.measure(backend, pattern[-4]) - measure_method.measure(backend, pattern[-3]) - backend.correct_byproduct(pattern[-2], measure_method) - backend.correct_byproduct(pattern[-1], measure_method) - backend.finalize(pattern.output_nodes) - rho = backend.state.rho - - backend = StatevectorBackend() - backend.add_nodes(pattern.input_nodes) + dm_backend.add_nodes([1, 2]) + dm_backend.entangle_nodes((0, 1)) + dm_backend.entangle_nodes((1, 2)) + measure_method.measure(dm_backend, command.M(0)) + measure_method.measure(dm_backend, command.M(1, angle=-ANGLE_PI / 2, s_domain={0})) + dm_backend.correct_byproduct(command.X(2, {1}), measure_method) + dm_backend.correct_byproduct(command.Z(2, {0}), measure_method) + rho = dm_backend.state.rho + + sv_backend = StatevectorBackend() + sv_backend.add_nodes([0]) # node 0 initialized in Backend - backend.add_nodes([1, 2]) - backend.entangle_nodes((0, 1)) - backend.entangle_nodes((1, 2)) - measure_method.measure(backend, pattern[-4]) - measure_method.measure(backend, pattern[-3]) - backend.correct_byproduct(pattern[-2], measure_method) - backend.correct_byproduct(pattern[-1], measure_method) - backend.finalize(pattern.output_nodes) - psi = backend.state.psi - - assert np.allclose(rho, np.outer(psi, psi.conj())) + sv_backend.add_nodes([1, 2]) + sv_backend.entangle_nodes((0, 1)) + sv_backend.entangle_nodes((1, 2)) + measure_method.measure(sv_backend, command.M(0)) + measure_method.measure(sv_backend, command.M(1, angle=-ANGLE_PI / 2, s_domain={0})) + sv_backend.correct_byproduct(command.X(2, {1}), measure_method) + sv_backend.correct_byproduct(command.Z(2, {0}), measure_method) + psi = sv_backend.state.psi + + assert np.allclose( + rho, np.outer(psi.astype(np.complex128, copy=False), psi.conj().astype(np.complex128, copy=False)) + ) diff --git a/tests/test_statevec.py b/tests/test_statevec.py index f1566ff16..9b0cc37c0 100644 --- a/tests/test_statevec.py +++ b/tests/test_statevec.py @@ -51,7 +51,7 @@ def test_basicstates_success(self) -> None: # even more tests? def test_default_tensor_success(self, fx_rng: Generator) -> None: - nqb = fx_rng.integers(2, 5) + nqb = int(fx_rng.integers(2, 5)) print(f"nqb is {nqb}") vec = Statevec(nqubit=nqb) assert np.allclose(vec.psi, np.ones((2,) * nqb) / (np.sqrt(2)) ** nqb) @@ -59,7 +59,7 @@ def test_default_tensor_success(self, fx_rng: Generator) -> None: vec = Statevec(nqubit=nqb, data=BasicStates.MINUS_I) sv_list = [BasicStates.MINUS_I.get_statevector() for _ in range(nqb)] - sv = functools.reduce(np.kron, sv_list) + sv = functools.reduce(lambda a, b: np.kron(a, b).astype(np.complex128, copy=False), sv_list) assert np.allclose(vec.psi, sv.reshape((2,) * nqb)) assert len(vec.dims()) == nqb @@ -69,7 +69,7 @@ def test_default_tensor_success(self, fx_rng: Generator) -> None: state = PlanarState(rand_plane, rand_angle) vec = Statevec(nqubit=nqb, data=state) sv_list = [state.get_statevector() for _ in range(nqb)] - sv = functools.reduce(np.kron, sv_list) + sv = functools.reduce(lambda a, b: np.kron(a, b).astype(np.complex128, copy=False), sv_list) assert np.allclose(vec.psi, sv.reshape((2,) * nqb)) assert len(vec.dims()) == nqb @@ -79,7 +79,7 @@ def test_default_tensor_success(self, fx_rng: Generator) -> None: states = [PlanarState(plane=i, angle=j) for i, j in zip(rand_planes, rand_angles, strict=True)] vec = Statevec(nqubit=nqb, data=states) sv_list = [state.get_statevector() for state in states] - sv = functools.reduce(np.kron, sv_list) + sv = functools.reduce(lambda a, b: np.kron(a, b).astype(np.complex128, copy=False), sv_list) assert np.allclose(vec.psi, sv.reshape((2,) * nqb)) assert len(vec.dims()) == nqb @@ -135,8 +135,8 @@ def test_copy_success(self, fx_rng: Generator) -> None: # try calling with incorrect number of qubits compared to inferred one def test_copy_fail(self, fx_rng: Generator) -> None: - nqb = fx_rng.integers(2, 5) - length = 2**nqb + nqb = int(fx_rng.integers(2, 5)) + length = 1 << nqb rand_vec = fx_rng.random(length) + 1j * fx_rng.random(length) rand_vec /= np.sqrt(np.sum(np.abs(rand_vec) ** 2)) test_vec = Statevec(data=rand_vec) @@ -148,4 +148,4 @@ def test_copy_fail(self, fx_rng: Generator) -> None: def test_normalize() -> None: statevec = Statevec(nqubit=1, data=BasicStates.PLUS) statevec.remove_qubit(0) - assert _get_statevec_norm_numeric(statevec) == 1 + assert _get_statevec_norm_numeric(statevec.psi.astype(np.complex128, copy=False)) == 1 diff --git a/tests/test_statevec_backend.py b/tests/test_statevec_backend.py index 8de0c7e5f..0163b08d9 100644 --- a/tests/test_statevec_backend.py +++ b/tests/test_statevec_backend.py @@ -15,19 +15,21 @@ if TYPE_CHECKING: from numpy.random import Generator + from graphix import Pattern + class TestStatevec: @pytest.mark.parametrize( "state", [BasicStates.PLUS, BasicStates.ZERO, BasicStates.ONE, BasicStates.PLUS_I, BasicStates.MINUS_I] ) - def test_measurement_into_each_xyz_basis(self, state: BasicStates) -> None: + def test_measurement_into_each_xyz_basis(self, state: PlanarState) -> None: n = 3 k = 0 # for measurement into |-> returns [[0, 0], ..., [0, 0]] (whose norm is zero) statevector = state.get_statevector() m_op = np.outer(statevector, statevector.T.conjugate()) sv = Statevec(nqubit=n) - sv.evolve(m_op, [k]) + sv.evolve(m_op.astype(np.complex128, copy=False), [k]) sv.remove_qubit(k) sv2 = Statevec(nqubit=n - 1) @@ -38,14 +40,14 @@ def test_measurement_into_minus_state(self) -> None: k = 0 m_op = np.outer(BasicStates.MINUS.get_statevector(), BasicStates.MINUS.get_statevector().T.conjugate()) sv = Statevec(nqubit=n) - sv.evolve(m_op, [k]) + sv.evolve(m_op.astype(np.complex128, copy=False), [k]) with pytest.raises(AssertionError): sv.remove_qubit(k) class TestStatevecNew: # test initialization only - def test_init_success(self, hadamardpattern, fx_rng: Generator) -> None: + def test_init_success(self, hadamardpattern: Pattern, fx_rng: Generator) -> None: # plus state (default) backend = StatevectorBackend() backend.add_nodes(hadamardpattern.input_nodes) @@ -73,7 +75,7 @@ def test_init_success(self, hadamardpattern, fx_rng: Generator) -> None: # data input and Statevec input - def test_init_fail(self, hadamardpattern, fx_rng: Generator) -> None: + def test_init_fail(self, hadamardpattern: Pattern, fx_rng: Generator) -> None: rand_angle = fx_rng.random(2) * 2 * ANGLE_PI rand_plane = fx_rng.choice(np.array(Plane), 2) @@ -91,7 +93,9 @@ def test_clifford(self) -> None: # Applies clifford gate "Z" vec.evolve_single(clifford.matrix, 0) backend.apply_clifford(node=0, clifford=clifford) - np.testing.assert_allclose(vec.psi, backend.state.psi) + np.testing.assert_allclose( + vec.psi.astype(np.complex128, copy=False), backend.state.psi.astype(np.complex128, copy=False) + ) def test_deterministic_measure_one(self, fx_rng: Generator) -> None: # plus state & zero state (default), but with tossed coins diff --git a/tests/test_tnsim.py b/tests/test_tnsim.py index 6129c3c05..78ab6c2bd 100644 --- a/tests/test_tnsim.py +++ b/tests/test_tnsim.py @@ -10,7 +10,7 @@ from graphix.branch_selector import RandomBranchSelector from graphix.clifford import Clifford -from graphix.command import C, E, X, Z +from graphix.command import C, Command, E, X, Z from graphix.fundamentals import ANGLE_PI from graphix.ops import Ops from graphix.random_objects import rand_circuit @@ -19,13 +19,9 @@ from graphix.transpiler import Circuit -def random_op(sites: int, dtype: type, rng: Generator) -> npt.NDArray: +def random_op(sites: int, rng: Generator) -> npt.NDArray[np.complex128]: size = 2**sites - if dtype is np.complex64: - return rng.normal(size=(size, size)).astype(np.float32) + 1j * rng.normal(size=(size, size)).astype(np.float32) - if dtype is np.complex128: - return rng.normal(size=(size, size)).astype(np.float64) + 1j * rng.normal(size=(size, size)).astype(np.float64) - return rng.normal(size=(size, size)).astype(dtype) + return rng.normal(size=(size, size)).astype(np.float64) + 1j * rng.normal(size=(size, size)).astype(np.complex128) CZ = Ops.CZ @@ -34,7 +30,7 @@ def random_op(sites: int, dtype: type, rng: Generator) -> npt.NDArray: class TestTN: def test_add_node(self, fx_rng: Generator) -> None: - node_index = fx_rng.integers(0, 1000) + node_index = int(fx_rng.integers(0, 1000)) tn = MBQCTensorNet(branch_selector=RandomBranchSelector()) tn.add_qubit(node_index) @@ -43,10 +39,9 @@ def test_add_node(self, fx_rng: Generator) -> None: assert (tn.tensors[0].data == plus).all() def test_add_nodes(self, fx_rng: Generator) -> None: - node_index = set(fx_rng.integers(0, 1000, 20)) + node_index = list(map(int, fx_rng.integers(0, 1000, 20))) tn = MBQCTensorNet(branch_selector=RandomBranchSelector()) - tn.graph_prep = "sequential" tn.add_qubits(node_index) assert set(tn.tag_map.keys()) == {str(ind) for ind in node_index} | {"Open"} @@ -74,10 +69,11 @@ def test_entangle_nodes(self, fx_rng: Generator) -> None: assert contracted == pytest.approx(contracted_ref) def test_apply_one_site_operator(self, fx_rng: Generator) -> None: - cmds = [ - X(node=0, domain=[15]), - Z(node=0, domain=[15]), - C(node=0, clifford=fx_rng.choice(list(Clifford))), + clifford = Clifford(fx_rng.integers(len(Clifford))) + cmds: list[Command] = [ + X(node=0, domain={15}), + Z(node=0, domain={15}), + C(node=0, clifford=clifford), ] random_vec = fx_rng.normal(size=2) @@ -99,7 +95,7 @@ def test_apply_one_site_operator(self, fx_rng: Generator) -> None: ops = [ np.array([[0.0, 1.0], [1.0, 0.0]]), np.array([[1.0, 0.0], [0.0, -1.0]]), - cmds[2].clifford.matrix, + clifford.matrix, ] contracted_ref = np.einsum("i,ij,jk,kl,l", random_vec, ops[2], ops[1], ops[0], plus) assert contracted == pytest.approx(contracted_ref) @@ -109,7 +105,7 @@ def test_expectation_value1(self, fx_rng: Generator) -> None: state = circuit.simulate_statevector().statevec pattern = circuit.transpile().pattern tn_mbqc = pattern.simulate_pattern(backend="tensornetwork", rng=fx_rng) - random_op1 = random_op(1, np.complex128, fx_rng) + random_op1 = random_op(1, fx_rng) value1 = state.expectation_value(random_op1, [0]) value2 = tn_mbqc.expectation_value(random_op1, [0]) assert value1 == pytest.approx(value2) @@ -119,7 +115,7 @@ def test_expectation_value2(self, fx_rng: Generator) -> None: state = circuit.simulate_statevector().statevec pattern = circuit.transpile().pattern tn_mbqc = pattern.simulate_pattern(backend="tensornetwork", rng=fx_rng) - random_op2 = random_op(2, np.complex128, fx_rng) + random_op2 = random_op(2, fx_rng) input_list = [0, 1] for qargs in itertools.permutations(input_list): value1 = state.expectation_value(random_op2, list(qargs)) @@ -131,7 +127,7 @@ def test_expectation_value3(self, fx_rng: Generator) -> None: state = circuit.simulate_statevector().statevec pattern = circuit.transpile().pattern tn_mbqc = pattern.simulate_pattern(backend="tensornetwork", rng=fx_rng) - random_op3 = random_op(3, np.complex128, fx_rng) + random_op3 = random_op(3, fx_rng) input_list = [0, 1, 2] for qargs in itertools.permutations(input_list): value1 = state.expectation_value(random_op3, list(qargs)) @@ -143,7 +139,7 @@ def test_expectation_value3_sequential(self, fx_rng: Generator) -> None: state = circuit.simulate_statevector().statevec pattern = circuit.transpile().pattern tn_mbqc = pattern.simulate_pattern(backend="tensornetwork", graph_prep="sequential", rng=fx_rng) - random_op3 = random_op(3, np.complex128, fx_rng) + random_op3 = random_op(3, fx_rng) input_list = [0, 1, 2] for qargs in itertools.permutations(input_list): value1 = state.expectation_value(random_op3, list(qargs)) @@ -155,7 +151,7 @@ def test_expectation_value3_subspace1(self, fx_rng: Generator) -> None: state = circuit.simulate_statevector().statevec pattern = circuit.transpile().pattern tn_mbqc = pattern.simulate_pattern(backend="tensornetwork", rng=fx_rng) - random_op1 = random_op(1, np.complex128, fx_rng) + random_op1 = random_op(1, fx_rng) input_list = [0, 1, 2] for qargs in itertools.permutations(input_list, 1): value1 = state.expectation_value(random_op1, list(qargs)) @@ -167,7 +163,7 @@ def test_expectation_value3_subspace2(self, fx_rng: Generator) -> None: state = circuit.simulate_statevector().statevec pattern = circuit.transpile().pattern tn_mbqc = pattern.simulate_pattern(backend="tensornetwork", rng=fx_rng) - random_op2 = random_op(2, np.complex128, fx_rng) + random_op2 = random_op(2, fx_rng) input_list = [0, 1, 2] for qargs in itertools.permutations(input_list, 2): value1 = state.expectation_value(random_op2, list(qargs)) @@ -179,7 +175,7 @@ def test_expectation_value3_subspace2_sequential(self, fx_rng: Generator) -> Non state = circuit.simulate_statevector().statevec pattern = circuit.transpile().pattern tn_mbqc = pattern.simulate_pattern(backend="tensornetwork", graph_prep="sequential", rng=fx_rng) - random_op2 = random_op(2, np.complex128, fx_rng) + random_op2 = random_op(2, fx_rng) input_list = [0, 1, 2] for qargs in itertools.permutations(input_list, 2): value1 = state.expectation_value(random_op2, list(qargs)) @@ -193,7 +189,7 @@ def test_hadamard(self, fx_rng: Generator) -> None: pattern.standardize() state = circuit.simulate_statevector().statevec tn_mbqc = pattern.simulate_pattern(backend="tensornetwork", rng=fx_rng) - random_op1 = random_op(1, np.complex128, fx_rng) + random_op1 = random_op(1, fx_rng) value1 = state.expectation_value(random_op1, [0]) value2 = tn_mbqc.expectation_value(random_op1, [0]) assert value1 == pytest.approx(value2) @@ -204,7 +200,7 @@ def test_s(self, fx_rng: Generator) -> None: pattern = circuit.transpile().pattern state = circuit.simulate_statevector().statevec tn_mbqc = pattern.simulate_pattern(backend="tensornetwork", rng=fx_rng) - random_op1 = random_op(1, np.complex128, fx_rng) + random_op1 = random_op(1, fx_rng) value1 = state.expectation_value(random_op1, [0]) value2 = tn_mbqc.expectation_value(random_op1, [0]) assert value1 == pytest.approx(value2) @@ -215,7 +211,7 @@ def test_x(self, fx_rng: Generator) -> None: pattern = circuit.transpile().pattern state = circuit.simulate_statevector().statevec tn_mbqc = pattern.simulate_pattern(backend="tensornetwork", rng=fx_rng) - random_op1 = random_op(1, np.complex128, fx_rng) + random_op1 = random_op(1, fx_rng) value1 = state.expectation_value(random_op1, [0]) value2 = tn_mbqc.expectation_value(random_op1, [0]) assert value1 == pytest.approx(value2) @@ -226,7 +222,7 @@ def test_y(self, fx_rng: Generator) -> None: pattern = circuit.transpile().pattern state = circuit.simulate_statevector().statevec tn_mbqc = pattern.simulate_pattern(backend="tensornetwork", rng=fx_rng) - random_op1 = random_op(1, np.complex128, fx_rng) + random_op1 = random_op(1, fx_rng) value1 = state.expectation_value(random_op1, [0]) value2 = tn_mbqc.expectation_value(random_op1, [0]) assert value1 == pytest.approx(value2) @@ -237,7 +233,7 @@ def test_z(self, fx_rng: Generator) -> None: pattern = circuit.transpile().pattern state = circuit.simulate_statevector().statevec tn_mbqc = pattern.simulate_pattern(backend="tensornetwork", rng=fx_rng) - random_op1 = random_op(1, np.complex128, fx_rng) + random_op1 = random_op(1, fx_rng) value1 = state.expectation_value(random_op1, [0]) value2 = tn_mbqc.expectation_value(random_op1, [0]) assert value1 == pytest.approx(value2) @@ -249,7 +245,7 @@ def test_rx(self, fx_rng: Generator) -> None: pattern = circuit.transpile().pattern state = circuit.simulate_statevector().statevec tn_mbqc = pattern.simulate_pattern(backend="tensornetwork", rng=fx_rng) - random_op1 = random_op(1, np.complex128, fx_rng) + random_op1 = random_op(1, fx_rng) value1 = state.expectation_value(random_op1, [0]) value2 = tn_mbqc.expectation_value(random_op1, [0]) assert value1 == pytest.approx(value2) @@ -261,7 +257,7 @@ def test_ry(self, fx_rng: Generator) -> None: pattern = circuit.transpile().pattern state = circuit.simulate_statevector().statevec tn_mbqc = pattern.simulate_pattern(backend="tensornetwork", rng=fx_rng) - random_op1 = random_op(1, np.complex128, fx_rng) + random_op1 = random_op(1, fx_rng) value1 = state.expectation_value(random_op1, [0]) value2 = tn_mbqc.expectation_value(random_op1, [0]) assert value1 == pytest.approx(value2) @@ -273,7 +269,7 @@ def test_rz(self, fx_rng: Generator) -> None: pattern = circuit.transpile().pattern state = circuit.simulate_statevector().statevec tn_mbqc = pattern.simulate_pattern(backend="tensornetwork", rng=fx_rng) - random_op1 = random_op(1, np.complex128, fx_rng) + random_op1 = random_op(1, fx_rng) value1 = state.expectation_value(random_op1, [0]) value2 = tn_mbqc.expectation_value(random_op1, [0]) assert value1 == pytest.approx(value2) @@ -284,7 +280,7 @@ def test_i(self, fx_rng: Generator) -> None: pattern = circuit.transpile().pattern state = circuit.simulate_statevector().statevec tn_mbqc = pattern.simulate_pattern(backend="tensornetwork", rng=fx_rng) - random_op1 = random_op(1, np.complex128, fx_rng) + random_op1 = random_op(1, fx_rng) value1 = state.expectation_value(random_op1, [0]) value2 = tn_mbqc.expectation_value(random_op1, [0]) assert value1 == pytest.approx(value2) @@ -296,7 +292,7 @@ def test_cz(self, fx_rng: Generator) -> None: pattern.standardize() state = circuit.simulate_statevector().statevec tn_mbqc = pattern.simulate_pattern(backend="tensornetwork", rng=fx_rng) - random_op2 = random_op(2, np.complex128, fx_rng) + random_op2 = random_op(2, fx_rng) value1 = state.expectation_value(random_op2, [0, 1]) value2 = tn_mbqc.expectation_value(random_op2, [0, 1]) assert value1 == pytest.approx(value2) @@ -308,7 +304,7 @@ def test_cnot(self, fx_rng: Generator) -> None: pattern.standardize() state = circuit.simulate_statevector().statevec tn_mbqc = pattern.simulate_pattern(backend="tensornetwork", rng=fx_rng) - random_op2 = random_op(2, np.complex128, fx_rng) + random_op2 = random_op(2, fx_rng) value1 = state.expectation_value(random_op2, [0, 1]) value2 = tn_mbqc.expectation_value(random_op2, [0, 1]) assert value1 == pytest.approx(value2) @@ -322,7 +318,7 @@ def test_ccx(self, fx_bg: PCG64, jumps: int, fx_rng: Generator) -> None: pattern.minimize_space() state = circuit.simulate_statevector().statevec tn_mbqc = pattern.simulate_pattern(backend="tensornetwork", rng=fx_rng) - random_op3 = random_op(3, np.complex128, rng) + random_op3 = random_op(3, rng) value1 = state.expectation_value(random_op3, [0, 1, 2]) value2 = tn_mbqc.expectation_value(random_op3, [0, 1, 2]) assert value1 == pytest.approx(value2) @@ -338,7 +334,7 @@ def test_with_graphtrans(self, fx_bg: PCG64, jumps: int, fx_rng: Generator) -> N pattern.perform_pauli_measurements() state = circuit.simulate_statevector().statevec tn_mbqc = pattern.simulate_pattern(backend="tensornetwork", rng=fx_rng) - random_op3 = random_op(3, np.complex128, rng) + random_op3 = random_op(3, rng) input_list = [0, 1, 2] for qargs in itertools.permutations(input_list): value1 = state.expectation_value(random_op3, list(qargs)) @@ -357,7 +353,7 @@ def test_with_graphtrans_sequential(self, fx_bg: PCG64, jumps: int, fx_rng: Gene pattern.perform_pauli_measurements() state = circuit.simulate_statevector().statevec tn_mbqc = pattern.simulate_pattern(backend="tensornetwork", graph_prep="sequential", rng=fx_rng) - random_op3 = random_op(3, np.complex128, rng) + random_op3 = random_op(3, rng) input_list = [0, 1, 2] for qargs in itertools.permutations(input_list): value1 = state.expectation_value(random_op3, list(qargs)) @@ -407,8 +403,8 @@ def test_evolve(self, fx_bg: PCG64, jumps: int, fx_rng: Generator) -> None: pattern.perform_pauli_measurements() state = circuit.simulate_statevector().statevec tn_mbqc = pattern.simulate_pattern(backend="tensornetwork", rng=fx_rng) - random_op3 = random_op(3, np.complex128, rng) - random_op3_exp = random_op(3, np.complex128, rng) + random_op3 = random_op(3, rng) + random_op3_exp = random_op(3, rng) state.evolve(random_op3, [0, 1, 2]) tn_mbqc.evolve(random_op3, [0, 1, 2], decompose=False)