Skip to content

Commit 398efce

Browse files
committed
Fixtures added.
1 parent d090f35 commit 398efce

File tree

2 files changed

+80
-37
lines changed

2 files changed

+80
-37
lines changed

test/conftest.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
import numpy as np
2+
import tensorflow as tf
13
from typing import Any, Dict
24
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
5+
from c3.utils.tf_utils import (
6+
tf_super,
7+
tf_choi_to_chi,
8+
tf_abs,
9+
super_to_choi,
10+
tf_project_to_comp,
11+
)
312
import pytest
413

514

@@ -101,3 +110,57 @@ def get_result_qiskit() -> Dict[str, Dict[str, Any]]:
101110
"c3_qasm_perfect_simulator": perfect_counts,
102111
}
103112
return counts_dict
113+
114+
115+
@pytest.fixture
116+
def get_error_process():
117+
"""Fixture for a constant unitary
118+
119+
Returns
120+
-------
121+
np.array
122+
Unitary on a large Hilbert space that needs to be projected down correctly and
123+
compared to an ideal representation in the computational space.
124+
"""
125+
U_actual = (
126+
1
127+
/ np.sqrt(2)
128+
* np.array(
129+
[
130+
[1, 0, 0, -1.0j, 0, 0, 0, 0, 0],
131+
[0, 1, 0, 0, -1.0j, 0, 0, 0, 0],
132+
[0, 0, 0, 0, 0, 0, 0, 0, 0],
133+
[-1.0j, 0, 0, 1, 0, 0, 0, 0, 0],
134+
[0, -1.0j, 0, 0, 1, 0, 0, 0, 0],
135+
[0, 0, 0, 0, 0, 0, 0, 0, 0],
136+
[0, 0, 0, 0, 0, 0, 0, 0, 0],
137+
[0, 0, 0, 0, 0, 0, 0, 45, 0],
138+
[0, 0, 0, 0, 0, 0, 0, 0, 0],
139+
]
140+
)
141+
)
142+
143+
lvls = [3, 3]
144+
U_ideal = (
145+
1
146+
/ np.sqrt(2)
147+
* np.array(
148+
[[1, 0, -1.0j, 0], [0, 1, 0, -1.0j], [-1.0j, 0, 1, 0], [0, -1.0j, 0, 1]]
149+
)
150+
)
151+
Lambda = tf.matmul(
152+
tf.linalg.adjoint(tf_project_to_comp(U_actual, lvls, to_super=False)), U_ideal
153+
)
154+
return Lambda
155+
156+
157+
@pytest.fixture
158+
def get_average_fidelitiy(get_error_process):
159+
lvls = [3, 3]
160+
Lambda = get_error_process
161+
d = 4
162+
err = tf_super(Lambda)
163+
choi = super_to_choi(err)
164+
chi = tf_choi_to_chi(choi, dims=lvls)
165+
fid = tf_abs((chi[0, 0] / d + 1) / (d + 1))
166+
return fid

test/test_average_fidelity.py

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,23 @@
1-
import tensorflow as tf
1+
"""
2+
Tests for average fidelity and helper functions.
3+
"""
4+
25
import numpy as np
3-
from c3.utils.tf_utils import (
4-
tf_choi_to_chi, super_to_choi, tf_super, tf_abs, tf_project_to_comp
5-
)
66
from numpy.testing import assert_array_almost_equal as almost_equal
7-
8-
dims = [3, 3]
9-
lvls = dims
10-
U_actual = np.array(
11-
[[1, 0, 0, -1.j, 0, 0, 0, 0, 0],
12-
[0, 1, 0, 0, -1.j, 0, 0, 0, 0],
13-
[0, 0, 0, 0, 0, 0, 0, 0, 0],
14-
[-1.j, 0, 0, 1, 0, 0, 0, 0, 0],
15-
[0, -1.j, 0, 0, 1, 0, 0, 0, 0],
16-
[0, 0, 0, 0, 0, 0, 0, 0, 0],
17-
[0, 0, 0, 0, 0, 0, 0, 0, 0],
18-
[0, 0, 0, 0, 0, 0, 0, 45, 0],
19-
[0, 0, 0, 0, 0, 0, 0, 0, 0]]
20-
) / np.sqrt(2)
21-
U_ideal = np.array(
22-
[[1, 0, -1.j, 0],
23-
[0, 1, 0, -1.j],
24-
[-1.j, 0, 1, 0],
25-
[0, -1.j, 0, 1]]) / np.sqrt(2)
26-
27-
d = 4
28-
29-
Lambda = tf.matmul(
30-
tf.linalg.adjoint(tf_project_to_comp(U_actual, lvls, to_super=False)), U_ideal
31-
)
32-
err = tf_super(Lambda)
33-
choi = super_to_choi(err)
34-
chi = tf_choi_to_chi(choi, dims=lvls)
35-
fid = tf_abs((chi[0, 0] / d + 1) / (d + 1))
7+
import pytest
368

379

38-
def test_error_channel():
39-
almost_equal(Lambda, np.eye(4))
10+
@pytest.mark.unit
11+
def test_error_channel(get_error_process):
12+
"""
13+
Check that the given process performs an identity in the computational subspace.
14+
"""
15+
almost_equal(get_error_process, np.eye(4))
4016

4117

42-
def test_fid():
43-
almost_equal(fid, 1)
18+
@pytest.mark.unit
19+
def test_fid(get_average_fidelitiy):
20+
"""
21+
Check that the average fideltiy of an identity is maximal.
22+
"""
23+
almost_equal(get_average_fidelitiy, 1)

0 commit comments

Comments
 (0)