Coverage for tests/test_coefficients.py: 100%

14 statements  

« prev     ^ index     » next       coverage.py v7.6.5, created at 2024-11-15 11:13 +0000

1from qml_essentials.model import Model 

2from qml_essentials.coefficients import Coefficients 

3 

4import numpy as np 

5import logging 

6import pytest 

7 

8logger = logging.getLogger(__name__) 

9 

10 

11@pytest.mark.unittest 

12def test_coefficients() -> None: 

13 test_cases = [ 

14 { 

15 "circuit_type": "Circuit_1", 

16 "n_qubits": 1, 

17 "n_layers": 3, 

18 "n_bins": 10, 

19 "n_samples": 200, 

20 "n_input_samples": 2, 

21 "output_qubit": 0, 

22 }, 

23 { 

24 "circuit_type": "Circuit_9", 

25 "n_qubits": 4, 

26 "n_layers": 1, 

27 "n_bins": 10, 

28 "n_samples": 200, 

29 "n_input_samples": 2, 

30 "output_qubit": 0, 

31 }, 

32 { 

33 "circuit_type": "Circuit_9", 

34 "n_qubits": 4, 

35 "n_layers": 1, 

36 "n_bins": 10, 

37 "n_samples": 200, 

38 "n_input_samples": 2, 

39 "output_qubit": [0, 1, 2, 3], 

40 }, 

41 ] 

42 

43 for test_case in test_cases: 

44 model = Model( 

45 n_qubits=test_case["n_qubits"], 

46 n_layers=test_case["n_layers"], 

47 circuit_type=test_case["circuit_type"], 

48 data_reupload=True, 

49 initialization="random", 

50 output_qubit=test_case["output_qubit"], 

51 ) 

52 

53 coeffs = Coefficients.sample_coefficients(model) 

54 

55 assert len(coeffs) == model.degree * 2 + 1, "Wrong number of coefficients" 

56 assert np.isclose( 

57 np.sum(coeffs).imag, 0.0, rtol=1.0e-5 

58 ), "Imaginary part is not zero"