/
testGateTools.py
89 lines (72 loc) · 3.92 KB
/
testGateTools.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from ..testutils import BaseTestCase, compare_files, temp_files
import pygsti
import pygsti.tools.gatetools as gatetools
import numpy as np
import unittest
A = np.array( [[0.9, 0, 0.1j, 0],
[ 0, 0, 0, 0],
[ -0.1j, 0, 0, 0],
[ 0, 0, 0, 0.1]], 'complex')
B = np.array( [[0.5, 0, 0, -0.2j],
[ 0, 0.25, 0, 0],
[ 0, 0, 0.25, 0],
[ 0.2j, 0, 0, 0.1]], 'complex')
class GateBaseTestCase(BaseTestCase):
def test_gate_tools(self):
oneRealPair = np.array( [[1+1j, 0, 0, 0],
[ 0, 1-1j,0, 0],
[ 0, 0, 2, 0],
[ 0, 0, 0, 2]], 'complex')
decomp = pygsti.decompose_gate_matrix(oneRealPair)
#decompose gate mx whose eigenvalues have a real but non-unit pair
dblRealPair = np.array( [[3, 0, 0, 0],
[ 0, 3,0, 0],
[ 0, 0, 2, 0],
[ 0, 0, 0, 2]], 'complex')
decomp = pygsti.decompose_gate_matrix(dblRealPair)
#decompose gate mx whose eigenvalues have two real but non-unit pairs
unpairedMx = np.array( [[1+1j, 0, 0, 0],
[ 0, 2-1j,0, 0],
[ 0, 0, 2+2j, 0],
[ 0, 0, 0, 1.0+3j]], 'complex')
decomp = pygsti.decompose_gate_matrix(unpairedMx)
#decompose gate mx which has all complex eigenvalue -> bail out
self.assertFalse(decomp['isValid'])
largeMx = np.identity(16,'d')
decomp = pygsti.decompose_gate_matrix(largeMx) #can only handle 1Q mxs
self.assertFalse(decomp['isValid'])
self.assertAlmostEqual( pygsti.frobeniusdist(A,A), 0.0 )
self.assertAlmostEqual( pygsti.jtracedist(A,A,mxBasis="std"), 0.0 )
self.assertAlmostEqual( pygsti.diamonddist(A,A,mxBasis="std"), 0.0 )
self.assertAlmostEqual( pygsti.frobeniusdist(A,B), (0.430116263352+0j) )
self.assertAlmostEqual( pygsti.jtracedist(A,B,mxBasis="std"), 0.260078105936)
self.assertAlmostEqual( pygsti.diamonddist(A,B,mxBasis="std"), 0.614258836298)
self.assertAlmostEqual( pygsti.frobeniusdist(A,B), pygsti.frobeniusnorm(A-B) )
self.assertAlmostEqual( pygsti.frobeniusdist(A,B), np.sqrt( pygsti.frobeniusnorm2(A-B) ) )
def test_hack_sqrt_m(self):
expected = np.array([[ 0.55368857+0.46439416j, 0.80696073-0.21242648j],
[ 1.21044109-0.31863972j, 1.76412966+0.14575444j]]
)
sqrt = gatetools._hack_sqrtm(np.array([[1, 2], [3, 4]]))
self.assertArraysAlmostEqual(sqrt, expected)
def test_frobenius_distance(self):
self.assertAlmostEqual( pygsti.frobeniusdist(A,A), 0.0 )
self.assertAlmostEqual( pygsti.frobeniusdist2(A,A), 0.0 )
def test_process_fidelity(self):
fidelity = gatetools.process_fidelity(A, B)
self.assertAlmostEqual(fidelity, 0.42686642003)
def test_fidelity_upper_bound(self):
upperBound = gatetools.fidelity_upper_bound(A)
expected = (np.array([[ 0.25]]),
np.array([[ 1.00000000e+00, -8.27013523e-16, 8.57305616e-33, 1.95140273e-15],
[ -8.27013523e-16, 1.00000000e+00, 6.28036983e-16, -8.74760501e-31],
[ 5.68444574e-33, -6.28036983e-16, 1.00000000e+00, -2.84689309e-16],
[ 1.95140273e-15, -9.27538795e-31, 2.84689309e-16, 1.00000000e+00]]))
self.assertArraysAlmostEqual(upperBound[0], expected[0])
self.assertArraysAlmostEqual(upperBound[1], expected[1])
def test_unitary_to_process_mx(self):
identity = np.identity(2)
processMx = gatetools.unitary_to_process_mx(identity)
self.assertArraysAlmostEqual(processMx, np.identity(4))
if __name__ == '__main__':
unittest.main(verbosity=2)