ErrorMitigation / src /tests /test_noisy_qasm_runner.py
Dvirmor100's picture
style: using black and ruff to format the project
e0d7f28
Raw History Blame Contribute Delete
1.75 kB
import pytest
import pennylane as qml
from quantum_circuits.noisy_qasm_runner import NoisyQASMRunner
@pytest.fixture
def sample_qasm():
return """
OPENQASM 2.0;
include "qelib1.inc";
qreg q[3];
creg c[3];
h q[0];
cx q[0], q[1];
cx q[1], q[2];
"""
@pytest.fixture
def sample_noise():
return [
{"operation": "PhaseFlip", "probability": 0.08, "wires": [0]},
{"operation": "AmplitudeDamping", "probability": 0.03, "wires": [1, 2]},
]
def test_translate_noise_types(sample_noise):
"""Test that noise dictionaries are correctly translated into tuples."""
expected = [(qml.PhaseFlip, 0.08, [0]), (qml.AmplitudeDamping, 0.03, [1, 2])]
translated = NoisyQASMRunner.translate_noise_types(sample_noise)
assert translated == expected
def test_count_qubits_in_qasm(sample_qasm):
"""Test that the correct number of qubits is counted from QASM."""
count = NoisyQASMRunner.count_qubits_in_qasm(sample_qasm)
assert count == 3
def test_run_no_noise(sample_qasm, sample_noise):
"""Test running the circuit without noise and compare results."""
runner_no_noise = NoisyQASMRunner(sample_qasm, [])
result_no_noise = runner_no_noise.run()
result_no_noise_2 = runner_no_noise.run()
# Running again with noise
runner_with_noise = NoisyQASMRunner(sample_qasm, sample_noise)
result_with_noise = runner_with_noise.run()
# Ensure results are different due to noise impact
assert result_no_noise != result_with_noise
assert result_no_noise == result_no_noise_2
# Ensure no-noise run gives expected deterministic results
assert isinstance(result_no_noise, list)
assert all(float(val) is not None for val in result_no_noise)