Coverage for tests/test_entanglement.py: 100%

18 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.entanglement import Entanglement 

3 

4import logging 

5import math 

6import pytest 

7 

8logger = logging.getLogger(__name__) 

9 

10 

11@pytest.mark.expensive 

12@pytest.mark.unittest 

13def test_entanglement() -> None: 

14 test_cases = [ 

15 { 

16 "circuit_type": "No_Entangling", 

17 "n_qubits": 2, 

18 "n_layers": 1, 

19 "n_samples": 1000, 

20 "result": 0.0, 

21 }, 

22 { 

23 "circuit_type": "Strongly_Entangling", 

24 "n_qubits": 2, 

25 "n_layers": 1, 

26 "n_samples": 2000, 

27 "result": 0.3912, 

28 }, 

29 ] 

30 

31 for test_case in test_cases: 

32 model = Model( 

33 n_qubits=test_case["n_qubits"], 

34 n_layers=test_case["n_layers"], 

35 circuit_type=test_case["circuit_type"], 

36 data_reupload=True, 

37 initialization="random", 

38 ) 

39 

40 ent_cap = Entanglement.meyer_wallach( 

41 model, n_samples=test_case["n_samples"], seed=1000, cache=False 

42 ) 

43 

44 assert math.isclose( 

45 ent_cap, test_case["result"], abs_tol=1e-3 

46 ), f"Entangling capacity is not {test_case['result']}\ 

47 for circuit ansatz {test_case['circuit_type']}.\ 

48 Was {ent_cap} instead" 

49 

50 

51@pytest.mark.smoketest 

52def test_no_sampling() -> None: 

53 model = Model( 

54 n_qubits=2, 

55 n_layers=1, 

56 circuit_type="Hardware_Efficient", 

57 data_reupload=True, 

58 initialization="random", 

59 ) 

60 

61 _ = Entanglement.meyer_wallach(model, n_samples=-1, seed=1000, cache=False)