Skip to content

Commit

Permalink
Add tests for more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kkyusuke committed Nov 11, 2024
1 parent 8a7cb9d commit f3a754f
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/mqt/qudits/simulation/noise_tools/noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ def __init__(self, probability_depolarizing: float, probability_dephasing: float
self.probability_depolarizing = probability_depolarizing
self.probability_dephasing = probability_dephasing

def __str__(self) -> str:
return str(self.probability_depolarizing) + " " + str(self.probability_dephasing)


class SubspaceNoise:
"""Represents physical noises for each level transitions."""
Expand Down Expand Up @@ -43,6 +46,12 @@ def __init__(
Noise(probability_depolarizing, probability_dephasing),
)

def __str__(self) -> str:
result = ""
for levs, noise in self.subspace_w_probs.items():
result += str(levs[0]) + "<->" + str(levs[1]) + ":" + str(noise) + ", "
return result

def add_noise(self, lev_a: int, lev_b: int, noise: Noise) -> None:
if lev_b < lev_a:
lev_a, lev_b = lev_b, lev_a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from unittest import TestCase

import numpy as np
import pytest

from mqt.qudits.quantum_circuit import QuantumCircuit
from mqt.qudits.quantum_circuit.components.quantum_register import QuantumRegister
Expand Down Expand Up @@ -113,3 +114,11 @@ def test_generate_circuit_isolated(self):
assert [i.qasm_tag for i in instructions_og] == ["x"]
for tag in [i.qasm_tag for i in new_circ.instructions]:
assert tag in {"x", "z", "virtrz"}

@staticmethod
def test_error():
noise_model = NoiseModel()
local_error = Noise(probability_depolarizing=0.999, probability_dephasing=0.999)
noise_model.add_all_qudit_quantum_error(local_error, ["csum"])
with pytest.raises(ValueError, match="Mathematical noise has been defined multiple times!"):
noise_model.add_all_qudit_quantum_error(local_error, ["csum"])
78 changes: 78 additions & 0 deletions test/python/simulation/noise_tools/test_noise_tools_physical.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from unittest import TestCase

import numpy as np
import pytest

from mqt.qudits.quantum_circuit import QuantumCircuit
from mqt.qudits.quantum_circuit.components.quantum_register import QuantumRegister
Expand Down Expand Up @@ -32,6 +33,8 @@ def setUp(self) -> None:
noise_model.add_quantum_error_locally(sub3, ["s"])
self.noise_model = noise_model

assert set(noise_model.basis_gates) == {"csum", "z", "rh", "h", "rxy", "x", "s"}

def test_generate_circuit(self):
qreg_example = QuantumRegister("reg", 6, 6 * [5])
circ = QuantumCircuit(qreg_example)
Expand Down Expand Up @@ -134,3 +137,78 @@ def test_generate_circuit_isolated3(self):
assert [i.qasm_tag for i in instructions_og] == ["s", "s"]
for tag in [i.qasm_tag for i in new_circ.instructions]:
assert tag in {"s", "noisex", "virtrz", "noisey"}

@staticmethod
def test_str():
sub1 = SubspaceNoise(0.999, 0.999, (0, 1))
noise_model = NoiseModel()
noise_model.add_quantum_error_locally(sub1, ["z"])
assert "Gate: z, Mode: local, SubspaceNoise: 0<->1:0.999 0.999," in str(noise_model)

@staticmethod
def test_error():
noise_model = NoiseModel()

with pytest.raises(ValueError, match="The levels in the subspace noise should be different!"):
SubspaceNoise(0.999, 0.999, (0, 0))

err2 = SubspaceNoise(0.999, 0.999, (0, 1))
noise_model.add_quantum_error_locally(err2, ["z"])
with pytest.raises(ValueError, match="The same level physical noise is defined for multiple times!"):
noise_model.add_quantum_error_locally(err2, ["z"])

with pytest.raises(ValueError, match="Negative keys are for the dynamic assignment of the subspaces,"):
SubspaceNoise(0.999, 0.999, (-1, -2))

@staticmethod
def test_invalid_level():
err = SubspaceNoise(0.999, 0.999, (8, 9))
noise_model = NoiseModel()
noise_model.add_quantum_error_locally(err, ["z"])
qreg_example = QuantumRegister("reg", 6, 6 * [5])
circ = QuantumCircuit(qreg_example)
circ.z(0)
factory = NoisyCircuitFactory(noise_model, circ)
with pytest.raises(IndexError, match=r"Subspace levels exceed qudit dimensions.*"):
factory.generate_circuit()

@staticmethod
def test_no_dephasing():
err = SubspaceNoise(0.999, 0.999, (0, 1))
noise_model = NoiseModel()
noise_model.add_quantum_error_locally(err, ["z"])
qreg_example = QuantumRegister("reg", 2, 2 * [2])
circ = QuantumCircuit(qreg_example)
circ.z(0)
factory = NoisyCircuitFactory(noise_model, circ)
factory.generate_circuit()

@staticmethod
def test_invalid_gate():
err = SubspaceNoise(0.999, 0.999, (0, 1))
noise_model = NoiseModel()
noise_model.add_nonlocal_quantum_error_on_control(err, ["z"])
qreg_example = QuantumRegister("reg", 2, 2 * [2])
circ = QuantumCircuit(qreg_example)
circ.z(0)
factory = NoisyCircuitFactory(noise_model, circ)
with pytest.raises(ValueError, match=r".* is incompatible for the desidred operation."):
factory.generate_circuit()

noise_model = NoiseModel()
noise_model.add_nonlocal_quantum_error(err, ["z"])
factory = NoisyCircuitFactory(noise_model, circ)
with pytest.raises(ValueError, match=r"Nonlocal mode not applicable for gate type: .*"):
factory.generate_circuit()

@staticmethod
def test_invalid_mode():
err = SubspaceNoise(0.999, 0.999, (0, 1))
noise_model = NoiseModel()
noise_model._add_quantum_error(err, ["z"], "error_mode") # noqa: SLF001
qreg_example = QuantumRegister("reg", 2, 2 * [2])
circ = QuantumCircuit(qreg_example)
circ.z(0)
factory = NoisyCircuitFactory(noise_model, circ)
with pytest.raises(ValueError, match="Unknown mode: error_mode"):
factory.generate_circuit()

0 comments on commit f3a754f

Please sign in to comment.