Dvirmor100 commited on
Commit
4d41898
·
1 Parent(s): 41957a8

tests: create basic tests for db, wasm_runner and circuit manager

Browse files
src/tests/test_circuits.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pennylane import numpy as np
2
+ from typing import Optional, List, Any, Tuple, Callable
3
+ from quantum_circuits.noisy_circuit import NoisyCircuit, print_results
4
+ import pennylane as qml
5
+
6
+
7
+ # Test 1: Multiple Noise Channels (BitFlip, Depolarizing, PhaseFlip)
8
+ def my_clean_circuit(params: Optional[Any] = None, n_qubits: Optional[int] = None) -> None:
9
+ qml.Hadamard(wires=0)
10
+ qml.CNOT(wires=[0, 1])
11
+ qml.RX(0.2, wires=0)
12
+
13
+
14
+ noise_ops1: List[Tuple[Callable, float, List[int]]] = [
15
+ (qml.BitFlip, 0.1, [0, 1]),
16
+ (qml.DepolarizingChannel, 0.05, [0]),
17
+ (qml.PhaseFlip, 0.2, [1])
18
+ ]
19
+
20
+ # Device with enough wires (2 system + ancillas)
21
+ dev_naive1 = qml.device("default.mixed", wires=7, shots=None)
22
+ nc1 = NoisyCircuit(device=dev_naive1, noise_operations=noise_ops1)
23
+
24
+ qnode_stine1 = nc1.noisy_qnode(circuit=my_clean_circuit, wires=[0, 1], use_qasm=True)
25
+ qnode_naive1 = nc1.noisy_qnode(circuit=my_clean_circuit, wires=[0, 1], use_qasm=False)
26
+
27
+ res_stine1 = np.round(qnode_stine1(), 3)
28
+ res_naive1 = np.round(qnode_naive1(), 3)
29
+
30
+ print_results("Test 1: Multiple Noise Channels (BitFlip, Depolarizing, PhaseFlip)",
31
+ res_stine1, res_naive1, qnode_stine1, qnode_naive1)
32
+
33
+
34
+ ##################################################################
35
+ # Test 2: Single Noise Channel (AmplitudeDamping)
36
+ ##################################################################
37
+ def my_clean_circuit2(params=None, n_qubits=None):
38
+ qml.Hadamard(wires=0)
39
+ qml.CNOT(wires=[0, 1])
40
+
41
+
42
+ noise_ops2 = [
43
+ (qml.AmplitudeDamping, 0.3, [0, 1])
44
+ ]
45
+
46
+ dev_naive2 = qml.device("default.mixed", wires=4, shots=None)
47
+ nc2 = NoisyCircuit(device=dev_naive2, noise_operations=noise_ops2)
48
+
49
+ qnode_stine2 = nc2.noisy_qnode(circuit=my_clean_circuit2, wires=[0, 1], use_qasm=True)
50
+ qnode_naive2 = nc2.noisy_qnode(circuit=my_clean_circuit2, wires=[0, 1], use_qasm=False)
51
+
52
+ res_stine2 = np.round(qnode_stine2(), 3)
53
+ res_naive2 = np.round(qnode_naive2(), 3)
54
+
55
+ print_results("Test 2: Single Noise Channel (AmplitudeDamping = 0.3)",
56
+ res_stine2, res_naive2, qnode_stine2, qnode_naive2)
57
+
58
+
59
+ ##################################################################
60
+ # Test 3: No Noise Channels (Noiseless Circuit)
61
+ ##################################################################
62
+ def my_clean_circuit3(params=None, n_qubits=None):
63
+ qml.Hadamard(wires=0)
64
+ qml.CNOT(wires=[0, 1])
65
+ qml.RY(0.5, wires=1)
66
+
67
+
68
+ noise_ops3 = [] # No noise
69
+
70
+ dev_naive3 = qml.device("default.mixed", wires=2, shots=None)
71
+ nc3 = NoisyCircuit(device=dev_naive3, noise_operations=noise_ops3)
72
+
73
+ qnode_stine3 = nc3.noisy_qnode(circuit=my_clean_circuit3, wires=[0, 1], use_qasm=True)
74
+ qnode_naive3 = nc3.noisy_qnode(circuit=my_clean_circuit3, wires=[0, 1], use_qasm=False)
75
+
76
+ res_stine3 = np.round(qnode_stine3(), 3)
77
+ res_naive3 = np.round(qnode_naive3(), 3)
78
+
79
+ print_results("Test 3: No Noise Channels (Noiseless Circuit)",
80
+ res_stine3, res_naive3, qnode_stine3, qnode_naive3)
81
+
82
+
83
+ ##################################################################
84
+ # Test 4: All Noise Channels Applied Together
85
+ ##################################################################
86
+ def my_clean_circuit4(params=None, n_qubits=None):
87
+ qml.Hadamard(wires=0)
88
+ qml.SWAP(wires=[0, 1])
89
+ qml.RZ(0.3, wires=1)
90
+
91
+
92
+ noise_ops4 = [
93
+ (qml.BitFlip, 0.05, [0]),
94
+ (qml.PhaseFlip, 0.05, [1]),
95
+ (qml.DepolarizingChannel, 0.05, [0, 1]),
96
+ (qml.AmplitudeDamping, 0.1, [0]),
97
+ (qml.PhaseDamping, 0.1, [1])
98
+ ]
99
+
100
+ # Total wires: 2 system wires + (1 + 1 + 2 + 1 + 1) = 7 wires.
101
+ dev_naive4 = qml.device("default.mixed", wires=7, shots=None)
102
+ nc4 = NoisyCircuit(device=dev_naive4, noise_operations=noise_ops4)
103
+
104
+ qnode_stine4 = nc4.noisy_qnode(circuit=my_clean_circuit4, wires=[0, 1], use_qasm=True)
105
+ qnode_naive4 = nc4.noisy_qnode(circuit=my_clean_circuit4, wires=[0, 1], use_qasm=False)
106
+
107
+ res_stine4 = np.round(qnode_stine4(), 3)
108
+ res_naive4 = np.round(qnode_naive4(), 3)
109
+
110
+ print_results("Test 4: All Noise Channels Applied Together",
111
+ res_stine4, res_naive4, qnode_stine4, qnode_naive4)
112
+
113
+
114
+ ##################################################################
115
+ # Test 5: Multiple Noise Channels with Varying Probabilities
116
+ ##################################################################
117
+ def my_clean_circuit5(params=None, n_qubits=None):
118
+ qml.Hadamard(wires=0)
119
+ qml.CNOT(wires=[0, 1])
120
+ qml.CZ(wires=[1, 2])
121
+ qml.RY(0.4, wires=2)
122
+
123
+
124
+ noise_ops5 = [
125
+ (qml.BitFlip, 0.2, [0, 1]),
126
+ (qml.PhaseFlip, 0.15, [1, 2]),
127
+ (qml.DepolarizingChannel, 0.1, [0, 2]),
128
+ (qml.AmplitudeDamping, 0.25, [0]),
129
+ (qml.PhaseDamping, 0.2, [2])
130
+ ]
131
+
132
+ # Total wires: 3 system wires + (2 + 2 + 2 + 1 + 1) = 11 wires.
133
+ dev_naive5 = qml.device("default.mixed", wires=11, shots=None)
134
+ nc5 = NoisyCircuit(device=dev_naive5, noise_operations=noise_ops5)
135
+
136
+ qnode_stine5 = nc5.noisy_qnode(circuit=my_clean_circuit5, wires=[0, 1, 2], use_qasm=True)
137
+ qnode_naive5 = nc5.noisy_qnode(circuit=my_clean_circuit5, wires=[0, 1, 2], use_qasm=False)
138
+
139
+ res_stine5 = np.round(qnode_stine5(), 3)
140
+ res_naive5 = np.round(qnode_naive5(), 3)
141
+
142
+ print_results("Test 5: Multiple Noise Channels with Varying Probabilities",
143
+ res_stine5, res_naive5, qnode_stine5, qnode_naive5)
src/tests/test_db_handler.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import pandas as pd
3
+
4
+ from database.db_handler import extract_circuit_data
5
+
6
+
7
+ def test_extract_circuit_data():
8
+ """Test function for extract_circuit_data."""
9
+ test_data = {
10
+ "qasm": ["test_qasm"],
11
+ "noise_data": [json.dumps(json.dumps({"error_rates": [0.01, 0.02]}))],
12
+ "noisy_result": [json.dumps(json.dumps({"expectation_values": [0.8, 0.6]}))],
13
+ "noiseless_result": [json.dumps(json.dumps({"expectation_values": [0.9, 0.7]}))]
14
+ }
15
+ df = pd.DataFrame(test_data)
16
+
17
+ result = extract_circuit_data(df, 0)
18
+
19
+ assert result.qasm_circuit == "test_qasm"
20
+ assert result.noise_data == {"error_rates": [0.01, 0.02]}
21
+ assert result.noisy_result == [0.8, 0.6]
22
+ assert result.noiseless_result == [0.9, 0.7]
23
+ assert result.model_dump() == {
24
+ "qasm_circuit": "test_qasm",
25
+ "noise_data": {"error_rates": [0.01, 0.02]},
26
+ "noisy_result": [0.8, 0.6],
27
+ "noiseless_result": [0.9, 0.7]
28
+ }
src/tests/test_noisy_qasm_runner.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ import pennylane as qml
3
+ from quantum_circuits.noisy_qasm_runner import NoisyQASMRunner
4
+
5
+
6
+ @pytest.fixture
7
+ def sample_qasm():
8
+ return """
9
+ OPENQASM 2.0;
10
+ include "qelib1.inc";
11
+ qreg q[3];
12
+ creg c[3];
13
+ h q[0];
14
+ cx q[0], q[1];
15
+ cx q[1], q[2];
16
+ """
17
+
18
+
19
+ @pytest.fixture
20
+ def sample_noise():
21
+ return [
22
+ {"operation": "PhaseFlip", "probability": 0.08, "wires": [0]},
23
+ {"operation": "AmplitudeDamping", "probability": 0.03, "wires": [1, 2]}
24
+ ]
25
+
26
+
27
+ def test_translate_noise_types(sample_noise):
28
+ """Test that noise dictionaries are correctly translated into tuples."""
29
+ expected = [
30
+ (qml.PhaseFlip, 0.08, [0]),
31
+ (qml.AmplitudeDamping, 0.03, [1, 2])
32
+ ]
33
+ translated = NoisyQASMRunner.translate_noise_types(sample_noise)
34
+ assert translated == expected
35
+
36
+
37
+ def test_count_qubits_in_qasm(sample_qasm):
38
+ """Test that the correct number of qubits is counted from QASM."""
39
+ count = NoisyQASMRunner.count_qubits_in_qasm(sample_qasm)
40
+ assert count == 3
41
+
42
+
43
+ def test_run_no_noise(sample_qasm, sample_noise):
44
+ """Test running the circuit without noise and compare results."""
45
+ runner_no_noise = NoisyQASMRunner(sample_qasm, [])
46
+ result_no_noise = runner_no_noise.run()
47
+ result_no_noise_2 = runner_no_noise.run()
48
+
49
+ # Running again with noise
50
+ runner_with_noise = NoisyQASMRunner(sample_qasm, sample_noise)
51
+ result_with_noise = runner_with_noise.run()
52
+
53
+ # Ensure results are different due to noise impact
54
+ assert result_no_noise != result_with_noise
55
+
56
+ assert result_no_noise == result_no_noise_2
57
+
58
+ # Ensure no-noise run gives expected deterministic results
59
+ assert isinstance(result_no_noise, list)
60
+ assert all(float(val) is not None for val in result_no_noise)