File size: 1,747 Bytes
4d41898
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0d7f28
4d41898
 
 
 
 
e0d7f28
4d41898
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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)